Skip to content

Instantly share code, notes, and snippets.

@migueldeicaza
Forked from anonymous/gist:4515011
Last active December 11, 2015 09:19
Show Gist options
  • Save migueldeicaza/4579408 to your computer and use it in GitHub Desktop.
Save migueldeicaza/4579408 to your computer and use it in GitHub Desktop.
Simple AI loop using Async code, to use as basis on "How to avoid garbage with Async" post.
private async void Think()
{
Loop:
Program.MineCycles++;
Say("Walking to the mine!");
await this.WaitSeconds(2.0f);
Say("Entering the mine!");
await this.WaitSeconds(1.0f);
// YUCK!
var random = _injector.Resolve<Random>();
while (_fatigue < 6)
{
await this.WaitSeconds(3.0f);
++_fatigue;
if (random.NextDouble() < 0.2)
{
++_goldCarried;
Say("Got {0} nuggets!", _goldCarried);
}
}
Say("Exiting the mine with {0} nuggets!", _goldCarried);
await this.WaitSeconds(1.0f);
Say("Walking home!");
await this.WaitSeconds(3.0f);
Say("Back at the shack!");
await this.WaitSeconds(2.0f);
Say("Dumping {0} nuggets!", _goldCarried);
await this.WaitSeconds(1.0f);
_goldCarried = 0;
if (_fatigue <= 0)
return;
Say("Going to sleep!");
await this.WaitSeconds(2.0f);
while (_fatigue > 0)
{
Say("ZZZZZZZ...");
await this.WaitSeconds(1.0f);
_fatigue--;
}
Say("Waking up!");
await GetReady();
Say("Getting ready to mine!");
await this.WaitSeconds(2.0f);
goto Loop;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment