MoveAction.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from synergine.synergy.event.Action import Action
  2. from intelligine.synergy.event.move.MoveEvent import MoveEvent
  3. from random import randint, choice, randrange
  4. from synergine.synergy.event.exception.ActionAborted import ActionAborted
  5. from xyzworld.cst import POSITION
  6. from intelligine.cst import PREVIOUS_DIRECTION, BLOCKED_SINCE
  7. from intelligine.synergy.event.move.direction import directions_same_level, directions_slighty
  8. from intelligine.synergy.event.move.direction import get_position_with_direction_decal
  9. class MoveAction(Action):
  10. _listen = MoveEvent
  11. def __init__(self, object_id, parameters):
  12. super().__init__(object_id, parameters)
  13. self._move_to_point = None
  14. self._move_to_direction = None
  15. def prepare(self, context):
  16. object_point = context.metas.value.get(POSITION, self._object_id)
  17. direction = self._get_prepared_direction(context, object_point)
  18. self._set_prepared_direction(context, object_point, direction)
  19. def _get_prepared_direction(self, context, object_point):
  20. return self._get_random_direction(context)
  21. def _set_prepared_direction(self, context, object_point, direction):
  22. move_to_point = get_position_with_direction_decal(direction, object_point)
  23. if self._direction_point_is_possible(context, move_to_point):
  24. self._move_to_point = move_to_point
  25. self._move_to_direction = direction
  26. else:
  27. # TODO: mettre self._dont_move = True ?
  28. pass
  29. def _get_random_direction(self, context):
  30. try:
  31. blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
  32. except KeyError:
  33. blocked_since = 0
  34. direction_name = None
  35. if blocked_since <= 3: #TODO: config
  36. try:
  37. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, self._object_id)
  38. # TODO: Faut mettre ca en plus propre (proba d'aller tou droit, config, etc)
  39. if randrange(100) < 75: # 75% de change d'aller tout droit
  40. # Dans le futur: les fourmis vont moins tout droit quand elle se croient et se touche
  41. return previous_direction
  42. directions_list = directions_slighty[previous_direction]
  43. # TODO: TMP tant que 1 niveau (z)
  44. directions_list = [direction for direction in directions_list if direction > 9 and direction < 19]
  45. direction_name = choice(directions_list)
  46. except KeyError:
  47. pass
  48. if not direction_name:
  49. direction_name = randint(directions_same_level[0], directions_same_level[1])
  50. return direction_name
  51. @staticmethod
  52. def _direction_point_is_possible(context, direction_point):
  53. return context.position_is_penetrable(direction_point)
  54. def run(self, obj, context, synergy_manager):
  55. try:
  56. self._apply_move(obj, context)
  57. except ActionAborted:
  58. blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id, allow_empty=True, empty_value=0)
  59. context.metas.value.set(BLOCKED_SINCE, self._object_id, blocked_since+1)
  60. def _apply_move(self, obj, context):
  61. # TODO: il ne faut pas choisir une direction 14.
  62. if self._move_to_point is None or self._move_to_direction == 14:
  63. raise ActionAborted()
  64. obj.set_position(self._move_to_point)
  65. context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
  66. context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)