Skip to content

Commit

Permalink
Merge pull request #53 from THUAI-ssast/fix-observations-are-gone-unn…
Browse files Browse the repository at this point in the history
…ecessarily
  • Loading branch information
Ethkuil authored May 15, 2023
2 parents af498b3 + dc95fb0 commit 1c2fdc0
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions Assets/Scripts/ControllerOutsideGameLogic/AiPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -104,22 +105,17 @@ public string getAction()
}
}

// Use async to avoid blocking the main thread
public async Task SendObservationAsync(string observation)
public void SendObservation(string observation)
{
if (p.HasExited)
{
return;
}

await Task.Run(async () =>
{
await p.StandardInput.WriteLineAsync(observation).ConfigureAwait(false);
await p.StandardInput.FlushAsync().ConfigureAwait(false);
}).ConfigureAwait(false);
p.StandardInput.WriteLine(observation);
p.StandardInput.Flush();
}


public void Close()
{
if (!p.HasExited)
Expand Down Expand Up @@ -151,14 +147,14 @@ void Awake()
}

// Assign the player that this AI should control
public async void Init(int playerId, dynamic config)
public void Init(int playerId, dynamic config)
{
this.playerId = playerId;

adapter = new ExternalAiAdapter((string)config.command);

// Send start observation
await adapter.SendObservationAsync(EncodeStartObservation());
adapter.SendObservation(EncodeStartObservation());
}

private void TryGetAndInterpretAction()
Expand All @@ -173,13 +169,39 @@ private void TryGetAndInterpretAction()
MapPresenter.Instance.InterpretAction(playerId, action);
}

private async void FixedUpdate()
IEnumerator StopCoroutineInSeconds(Coroutine myCoroutine, float seconds)
{
yield return new WaitForSeconds(seconds);
StopCoroutine(myCoroutine);
}

private IEnumerator SendObservationCoroutineCore(string observation)
{
adapter.SendObservation(observation);
yield return null;
}

private IEnumerator SendObservationCoroutine()
{
yield return new WaitForFixedUpdate();
// Here we get the latest observation
string observation = EncodeRoutineObservation();
Coroutine sendObservationCoroutine = StartCoroutine(SendObservationCoroutineCore(observation));
// set timeout for sending observation
// In fact, even if a delay of 0.1s will make the game stop again and again. 0.01s will make it slow.
// But that doesn't matter when headless as the game will end anyway.
// TODO: it's good to make `timeout` a config. But it's not necessary for now and I'm lazy.
float timeout = 0.5f;
StartCoroutine(StopCoroutineInSeconds(sendObservationCoroutine, timeout));
}

private void FixedUpdate()
{
TryGetAndInterpretAction();
// To preserve ai can see the effect of **its** action, we send observation after the action is interpreted

// Send observation to agent
await adapter.SendObservationAsync(EncodeRoutineObservation());
StartCoroutine(SendObservationCoroutine());
}

private string EncodeStartObservation()
Expand Down

0 comments on commit 1c2fdc0

Please sign in to comment.