Fixed giving commands to dead tanks
authorJustin Worthe <justin.worthe@entelect.co.za>
Sun, 20 Oct 2013 18:00:15 +0000 (20:00 +0200)
committerJustin Worthe <justin.worthe@entelect.co.za>
Sun, 20 Oct 2013 18:00:15 +0000 (20:00 +0200)
Entelect.BattleCity.Challenge/AiAgent.cs
Entelect.BattleCity.Challenge/GameInProgress.cs

index df9afb4..02ebd3b 100644 (file)
@@ -17,11 +17,15 @@ namespace Entelect.BattleCity.Challenge
             ChallengeService.unit tank = null;
             bool bulletInAir = false;
 
-            foreach (var unit in me.units)
+            if (me != null && me.units != null)
             {
-                if (unit.id == _tankId)
+                foreach (var unit in me.units)
                 {
-                    tank = unit;
+                    if (unit.id == _tankId)
+                    {
+                        Console.WriteLine("Tank found in list of tanks");
+                        tank = unit;
+                    }
                 }
             }
 
@@ -31,6 +35,23 @@ namespace Entelect.BattleCity.Challenge
                 return null;
             }
 
+            /*if (game.events != null && game.events.unitEvents != null)
+            {
+                foreach (var unitEvent in game.events.unitEvents)
+                {
+                    if (unitEvent.unit != null && unitEvent.unit.id == _tankId && unitEvent.bullet != null)
+                    {
+                        Console.WriteLine("Tank was shot");
+                        return null;
+                    }
+                }
+                Console.WriteLine("No relevant events in unit events list", _tankId);
+            }
+            else
+            {
+                Console.WriteLine("No events in unit events list", _tankId);
+            }*/
+
             if (me.bullets != null)
             {
                 foreach (var bullet in me.bullets)
index 6581eb7..27c7b64 100644 (file)
@@ -7,6 +7,8 @@ namespace Entelect.BattleCity.Challenge
 {
     public class GameInProgress
     {
+        private static ChallengeService.action _nullAction = ChallengeService.action.NONE;
+
         private ChallengeService.ChallengeClient _service;
         private ChallengeService.game _currentState;
         private ChallengeService.player _me;
@@ -34,11 +36,12 @@ namespace Entelect.BattleCity.Challenge
         {
             while (true)
             {
-                long currentTick = DateTime.Now.Ticks;
                 long nextTick = _currentState.nextTickTime.Ticks;
+                long currentTick = DateTime.Now.Ticks;
+
                 if (currentTick > nextTick)
                 {
-                    Console.Error.WriteLine("Current game state is out of date");
+                    Console.Error.WriteLine("Waiting for next tick. Current time: {0}, next game tick at: {1}", currentTick, nextTick);
                     updateGameStatus();
                     continue;
                 }
@@ -78,6 +81,10 @@ namespace Entelect.BattleCity.Challenge
                 Console.WriteLine(tank2Move.ToString());
                 _service.setAction(tank2Move.Tank, tank2Move.Action);
             }
+            else
+            {
+                Console.WriteLine("No tanks to set actions for");
+            }
         }
 
         private void waitForNextTick()
@@ -85,23 +92,25 @@ namespace Entelect.BattleCity.Challenge
             var nextTick = _currentState.nextTickTime.Ticks;
             var currentTick = DateTime.Now.Ticks;
 
-            long sleepTime = nextTick - currentTick;
-            if (sleepTime < 0L)
+            var sleepTime = TimeSpan.FromTicks(nextTick - currentTick)+TimeSpan.FromMilliseconds(500);
+            if (sleepTime.Ticks < 0L)
             {
                 Console.Error.WriteLine("ERROR: Gone passed the next tick time");
             }
             else
             {
-                Console.WriteLine("Sleeping until {1} for {0}ms", sleepTime, _currentState.nextTickTime.ToString());
+                Console.WriteLine("Sleeping until {1} for {0} processor ticks", sleepTime.Ticks, nextTick);
                 try
                 {
-                    Thread.Sleep(TimeSpan.FromTicks(sleepTime));
+                    Thread.Sleep(sleepTime);
                 }
                 catch (Exception ex)
                 {
                     Console.Error.WriteLine("Exception thrown while waiting for next tick");
                     Console.Error.WriteLine("Exception message: "+ ex.Message);
                 }
+
+                Console.WriteLine("Time after sleep: {0}", DateTime.Now.Ticks);
             }
         }
 
@@ -117,10 +126,12 @@ namespace Entelect.BattleCity.Challenge
                 if (player.name.Equals(_currentState.playerName))
                 {
                     _me = player;
+                    meFound = true;
                 }
                 else
                 {
                     _enemy = player;
+                    enemyFound = true;
                 }
             }