look for food when wandering
authorBen Doumenc <bdoumenc@gmail.com>
Sat, 31 May 2014 16:28:09 +0000 (18:28 +0200)
committerBen Doumenc <bdoumenc@gmail.com>
Sat, 31 May 2014 16:28:09 +0000 (18:28 +0200)
evolution/entities.py
evolution/main.py
evolution/systems.py

index 7fb90e4..34698e3 100644 (file)
@@ -27,13 +27,13 @@ class Scene:
         self.logger = logging.getLogger(self.name)
 
     def add(self, entity):
-        self.logger.debug("Scene: add %s", entity)
+        self.logger.info("Scene: add %s", entity)
         self.all.add(entity)
         for type in entity.TYPES:
             self.byType.setdefault(type, []).append(entity)
 
     def remove(self, entity):
-        self.logger.debug("Scene: remove %s", entity)
+        self.logger.info("Scene: remove %s", entity)
         self.all.remove(entity)
         for type in entity.TYPES:
             entities = self.byType.get(type, [])
index 00fab3f..6b6fb9a 100644 (file)
@@ -15,7 +15,8 @@ def generateRandomGenes(entity, size):
     
 
 if __name__ == "__main__":
-    logging.basicConfig(format='%(module)s:%(levelname)s:%(message)s', filename='output.log',level=logging.DEBUG)
+    # logging.basicConfig(format='%(module)s:%(levelname)s:%(message)s', filename='output.log',level=logging.DEBUG)
+    logging.basicConfig(format='%(module)s:%(levelname)s:%(message)s', level=logging.INFO)
 
     random.seed(1)
     scene = Scene("mainScene")
index ab70b90..6c79ed2 100644 (file)
@@ -74,9 +74,9 @@ class Behavior:
             Behavior.SEEK: self.seek,
         }
 
-    def setState(self, e, state):
-        e.scene.logger.debug("setState: [%s] %s -> %s", e, e.behavior, state)
-        e.behaviorData = {}
+    def setState(self, e, state, **kw):
+        e.scene.logger.debug("setState: [%s] %s -> %s (%s)", e, e.behavior, state, kw)
+        e.behaviorData = kw
         e.behavior = state
 
     def update(self, scene):
@@ -86,7 +86,7 @@ class Behavior:
                 self.setState(entity, Behavior.WANDER)
             entity.stats["life"] = entity.stats["life"] - 1
             if entity.stats["life"] == 0:
-                scene.logger.debug("[%s] died by exhaustion. Was %s", entity, entity.getGenes())
+                scene.logger.info("[%s] died by exhaustion. Was %s", entity, entity.getGenes())
                 entity.removeFromScene()
                 return
             self.behaviors.get(entity.behavior)(scene, entity)
@@ -99,16 +99,6 @@ class Behavior:
                 e.use(entity)
                 self.setState(entity, Behavior.WANDER)
                 return
-        # Check for surrounding food
-        perceptionDistance = entity.stats.get("perception", 1) * 4
-        if not entity.behaviorData.get("target", None):
-            scene.logger.debug("Seek: [%s] Looking for food", entity)
-            entities = scene.getAround(entity, perceptionDistance, Scene.Types.GOODS)
-            for e in entities:
-                if e.type == Scene.GoodTypes.FOOD:
-                    scene.logger.debug("Seek: [%s] Found target %s", entity, e)
-                    entity.behaviorData["target"] = e
-                    break
 
         if (entity.behaviorData.has_key("target")):
             # And move
@@ -116,15 +106,27 @@ class Behavior:
         else:
             self.setState(entity, Behavior.WANDER)
 
-    def wander(self, scene, e):
+    def wander(self, scene, entity):
         def reset():
-            e.behaviorData["count"] = 5
-            e.direction = Vector(random.randint(-1, 1), random.randint(-1, 1)).normalize()
+            entity.behaviorData["count"] = 10
+            entity.direction = Vector(random.randint(-1, 1), random.randint(-1, 1)).normalize()
 
-        if not e.behaviorData.has_key("count"): reset()
-        e.behaviorData["count"] = e.behaviorData.get("count", 5) - 1
-        if e.behaviorData["count"] == 0:
-            self.setState(e, Behavior.SEEK)
+        if not entity.behaviorData.has_key("count"): reset()
+        entity.behaviorData["count"] = entity.behaviorData["count"] - 1
+
+        target = None
+        # Check for surrounding food
+        perceptionDistance = entity.stats.get("perception", 1) * 4
+        entities = scene.getAround(entity, perceptionDistance, Scene.Types.GOODS)
+        for e in entities:
+            if e.type == Scene.GoodTypes.FOOD:
+                scene.logger.info("Wander: [%s] Found target %s", entity, e)
+                target = e
+                break
+        if target:
+            self.setState(entity, Behavior.SEEK, target=target)
+        elif entity.behaviorData["count"] == 0:
+            reset()
         
 
     def close(self):