fix truncate
authorBen Doumenc <bdoumenc@gmail.com>
Sat, 31 May 2014 21:06:19 +0000 (23:06 +0200)
committerBen Doumenc <bdoumenc@gmail.com>
Sat, 31 May 2014 21:06:19 +0000 (23:06 +0200)
evolution/main.py
evolution/systems.py
evolution/utils.py

index 5a414f3..c71c457 100644 (file)
@@ -24,7 +24,7 @@ if __name__ == "__main__":
     movement = Movement()
     behavior = Behavior()
     framerate = 40
-    population = 16
+    population = 6
     food = 180
     genePool = 6
     for _ in xrange(0, population):
index e9e6f4c..4ab9211 100644 (file)
@@ -104,7 +104,8 @@ class Behavior:
                     break
         if target:
             # And apply behavior
-            entity.forces += (target.pos - entity.pos).normalize()
+            vector = (target.pos - entity.pos)
+            entity.forces += vector.normalize()
             entity.behaviorData["target"] = target
             return True
         return False
index 8a7bf6a..13737b2 100644 (file)
@@ -18,6 +18,9 @@ class Vector:
     def __mul__(self, other):
         return Vector(*map(lambda x: x*other, self))
 
+    def __div__(self, other):
+        return Vector(*map(lambda x: x/other, self))
+
     def __getitem__(self, index):
         return self.data[index]
 
@@ -34,7 +37,12 @@ class Vector:
         return self
 
     def truncate(self, upper):
-        self.data = map(lambda x: min(x, upper), self)
+        def _trunc(value):
+            if value > 0:
+                return min(value, upper)
+            else:
+                return max(value, -1*upper)
+        self.data = map(_trunc, self)
         return self