New pathfinding that avoids most of the mobs getting stuck, closes #35

This commit is contained in:
Nicolas Margulies
2020-12-10 22:21:09 +01:00
parent 50d806cdcf
commit cc6033e8e4
3 changed files with 74 additions and 48 deletions

View File

@ -43,11 +43,14 @@ class Monster(FightingEntity):
# If they can't move and they are already close to the player,
# They hit.
if target and (self.y, self.x) in target.paths:
# Move to target player
next_y, next_x = target.paths[(self.y, self.x)]
moved = self.check_move(next_y, next_x, True)
if not moved and self.distance_squared(target) <= 1:
self.map.logs.add_message(self.hit(target))
# Move to target player by choosing the best avaliable path
for next_y, next_x in target.paths[(self.y, self.x)]:
moved = self.check_move(next_y, next_x, True)
if moved:
break
if self.distance_squared(target) <= 1:
self.map.logs.add_message(self.hit(target))
break
else:
# Move in a random direction
# If the direction is not available, try another one