MoveAction.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from synergine.synergy.event.Action import Action
  2. from socialintengine.synergy.event.move.MoveEvent import MoveEvent
  3. from random import randint, choice
  4. from xyzworld.cst import POSITION, POSITIONS
  5. from socialintengine.cst import IMPENETRABLE, PREVIOUS_DIRECTION, BLOCKED_SINCE
  6. from synergine.synergy.Simulation import Simulation
  7. from socialintengine.synergy.event.move.direction import directions_same_level, directions_modifiers, directions_slighty
  8. class MoveAction(Action):
  9. _listen = MoveEvent
  10. def __init__(self, object_id, parameters):
  11. super().__init__(object_id, parameters)
  12. self._move_to_point = None
  13. self._move_to_direction = None
  14. def prepare(self, context):
  15. object_point = context.metas.value.get(POSITION, self._object_id)
  16. choosed_direction_name, choosed_direction_point = self._get_random_direction_point(context, object_point)
  17. if self._direction_point_is_possible(context, choosed_direction_point):
  18. self._move_to_point = choosed_direction_point
  19. self._move_to_direction = choosed_direction_name
  20. def _get_random_direction_point(self, context, reference_point):
  21. z, x, y = reference_point
  22. direction_name = self._get_random_direction_name(context)
  23. directions_modifier = directions_modifiers[direction_name]
  24. new_position = (z + directions_modifier[0], x + directions_modifier[1], y + directions_modifier[2])
  25. return (direction_name, new_position)
  26. def _get_random_direction_name(self, context):
  27. try:
  28. blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
  29. except KeyError:
  30. blocked_since = 0
  31. direction_name = None
  32. if blocked_since <= 3: #TODO: config
  33. try:
  34. previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, self._object_id)
  35. directions_list = directions_slighty[previous_direction]
  36. # TODO: TMP tant que 1 niveau (z)
  37. directions_list = [direction for direction in directions_list if direction > 9 and direction < 19]
  38. direction_name = choice(directions_list)
  39. except KeyError:
  40. pass
  41. if not direction_name:
  42. direction_name = randint(directions_same_level[0], directions_same_level[1])
  43. return direction_name
  44. def _direction_point_is_possible(self, context, direction_point):
  45. objects_ids_on_this_point = context.metas.list.get(POSITIONS, direction_point, allow_empty=True)
  46. for object_id_on_this_point in objects_ids_on_this_point:
  47. if context.metas.list.have(Simulation.STATE, object_id_on_this_point, IMPENETRABLE):
  48. return False
  49. return True
  50. def run(self, obj, collection, context):
  51. if self._move_to_point is not None:
  52. obj.set_position(self._move_to_point)
  53. context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
  54. context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)
  55. else:
  56. try:
  57. blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
  58. except:
  59. blocked_since = 0
  60. context.metas.value.set(BLOCKED_SINCE, self._object_id, blocked_since+1)