Commits

Radomir Dopieralski committed 9bd3890

Better coward behavior

  • Participants
  • Parent commits 52aed14

Comments (0)

Files changed (2)

File jelly/mobs.py

 from pygame import locals as pg
 
 from jelly import sprites
-from jelly.util import DX, DY, dist, pos2dir
+from jelly.util import DX, DY, dist, pos2dir, dist2
 
 
 KEY_DIRS = {
     if dist(level.pc.pos, mob.pos) <= 1:
         level.attack(mob, pos2dir(mob.pos, level.pc.pos), level.pc)
         return True
+    return False
 
 
 def fitness_behavior(mob, level, fitness):
     x, y = mob.pos
-    possible = [d for (d, (dx, dy)) in enumerate(zip(DX, DY))
-                if level[x + dx, y + dy]]
+    possible = [d for d in [0, 2, 4, 6, 1, 3, 5, 7]
+                if not level.is_blocked((x + DX[d], y + DY[d]))]
+    if not possible:
+        return False
     direction = fitness(possible)
     try:
         level.walk(mob, direction)
 
 def coward_behavior(mob, level):
     x, y = mob.pos
-    return fitness_behavior(mob, level, lambda choices: max(choices,
-        key=lambda d: dist((x+DX[d], y+DY[d]), level.pc.pos)))
+    def coward_fitness(choices):
+        return max(
+            choices,
+            key=lambda d: dist2((x + DX[d], y + DY[d]), level.pc.pos))
+    return fitness_behavior(mob, level, coward_fitness)
 
 
 def aggressive_behavior(mob, level):

File jelly/util.py

 def dist((x1, y1), (x2, y2)):
     return max(abs(x2 - x1), abs(y2 - y1))
 
+def sum_dist((x1, y1), (x2, y2)):
+    return abs(x2 - x1) + abs(y2 - y1)
+
+def dist2(pos1, pos2):
+    return dist(pos1, pos2) + sum_dist(pos1, pos2)
+
 def pos2dir(pos1, pos2):
     x, y = pos1
     return min(