Mercurial > jelly
changeset 15:9bd38901e351
Better coward behavior
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Thu, 15 Mar 2012 08:59:42 +0100 |
| parents | 52aed14950b2 |
| children | e5e4eb7b9443 |
| files | jelly/mobs.py jelly/util.py |
| diffstat | 2 files changed, 17 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/jelly/mobs.py Thu Mar 15 00:24:34 2012 +0100 +++ b/jelly/mobs.py Thu Mar 15 08:59:42 2012 +0100 @@ -7,7 +7,7 @@ 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 = { @@ -23,12 +23,15 @@ 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) @@ -54,8 +57,11 @@ 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):
--- a/jelly/util.py Thu Mar 15 00:24:34 2012 +0100 +++ b/jelly/util.py Thu Mar 15 08:59:42 2012 +0100 @@ -8,6 +8,12 @@ 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(
