Lars Yencken avatar Lars Yencken committed bd7ae32

Improve logging and reduce collision projection.

Comments (0)

Files changed (3)

 if settings.DEBUG:
     _l = logging.getLogger()
     _l.setLevel(logging.DEBUG)
-    _l.addHandler(logging.FileHandler('game_logs/debug.log'))
+    if os.path.exists(settings.LOG_FILE):
+        os.remove(settings.LOG_FILE)
+    _l.addHandler(logging.FileHandler(settings.LOG_FILE))
     for im in glob.glob('game_logs/*.png'):
         os.remove(im)
 
         "Give orders to all of our ants."
         t = time.time()
         self.turn_number += 1
+        logging.info('turn %d starting' % self.turn_number)
         self.remember_hills(ants)
         self.remember_seen(ants)
         self.find_components(ants)
 
 DEBUG = False
 
-from local_settings import *
+# how far in the future do we avoid collisions
+MAX_STEP_COLLISION = 2
+
+# how many ants can attack the same hill
+MAX_ATTACKING = 5
+
+LOG_FILE = 'game_logs/debug.log'
+
+try:
+    from local_settings import *
+except ImportError:
+    pass
+
     def do_move_direction(self, loc, direction):
         "Move in a given direction, handling conflicting orders."
         new_loc = self.ants.destination(loc, direction)
-        if (self.ants.unoccupied(new_loc) and new_loc not in self.plans[1]):
+        if new_loc not in self.plans[1]:
             self.ants.issue_order((loc, direction))
             assert new_loc not in self.plans[1]
             self.plans[1].add(new_loc)
         "Slow but exact pathfinding."
         t = time.time()
         p = self.find_path(loc, dest)
-        assert self.game.components[loc] == self.game.components[dest]
         if p:
-            assert len(p) == len(set(p))
-            if settings.DEBUG:
-                logging.debug('finding path %s to %s (%.0fms, %d, OK)' % (
-                        loc, dest, 1000*(time.time() - t), loc.distance(dest),
-                    ))
             new_loc = p[1]
-            ds = self.ants.direction(loc, new_loc)
-            for d in ds:
-                if self.do_move_direction(loc, d):
-                    for i, l in enumerate(p):
+            d, = self.ants.direction(loc, new_loc)
+            if self.do_move_direction(loc, d):
+                if settings.DEBUG:
+                    logging.debug(
+                            'finding path %s to %s (%.0fms, %d, OK)' % (
+                            loc, dest, 1000*(time.time() - t),
+                            loc.distance(dest),
+                        ))
+                for i, l in enumerate(p):
+                    step = i + 1
+                    if step < settings.MAX_STEP_COLLISION:
                         self.plans[i + 1].add(l)
-                    self.targets[dest] = loc
-                    return True
-                else:
-                    settings.DEBUG and logging.debug(
-                            'but an ant was in the way')
+                self.targets[dest] = loc
+                return True
+            else:
+                if settings.DEBUG:
+                    logging.debug(
+                            'finding path %s to %s (%.0fms, %d, CLASH)' % (
+                            loc, dest, 1000*(time.time() - t),
+                            loc.distance(dest),
+                        ))
+                return False
 
         if settings.DEBUG:
-            logging.debug('finding path %s to %s (%.0fms, %d, OK)' % (
+            logging.debug('finding path %s to %s (%.0fms, %d, FAIL)' % (
                     loc, dest, 1000*(time.time() - t), loc.distance(dest),
                 ))
         return False
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.