Ant.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from intelligine.core.exceptions import PheromoneException
  2. from intelligine.synergy.object.Bug import Bug
  3. from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER, \
  4. COL_TRANSPORTER, COL_TRANSPORTER_NOT_CARRYING, \
  5. COL_FIGHTER, MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, \
  6. PHEROMON_DIR_EXPLO, LAST_PHERMONES_POINTS, CARRIED
  7. from intelligine.synergy.object.Food import Food
  8. from intelligine.simulation.object.pheromone.MovementPheromoneGland import MovementPheromoneGland
  9. from intelligine.simulation.object.brain.AntBrain import AntBrain
  10. class Ant(Bug):
  11. def __init__(self, collection, context):
  12. super().__init__(collection, context)
  13. context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
  14. context.metas.collections.add_list(self.get_id(), [COL_TRANSPORTER,
  15. COL_TRANSPORTER_NOT_CARRYING,
  16. COL_FIGHTER])
  17. self._carried = []
  18. self._last_pheromones_points = {}
  19. # TODO: Faire un body_part schema pour ces trucs la
  20. self._movement_pheromone_gland = MovementPheromoneGland(self, self._context)
  21. self._brain.switch_to_mode(MOVE_MODE_EXPLO)
  22. def _get_brain_instance(self):
  23. return AntBrain(self._context, self)
  24. def get_movement_pheromone_gland(self):
  25. return self._movement_pheromone_gland
  26. def put_carry(self, obj, position=None):
  27. if position is None:
  28. position = self._get_position()
  29. self._carried.remove(obj)
  30. obj.set_position(position)
  31. self._context.metas.states.remove(self.get_id(), CARRYING)
  32. def get_carried(self):
  33. # TODO: cas ou plusieurs ?
  34. return self._carried[0]
  35. def carry(self, obj):
  36. self._carried.append(obj)
  37. self._context.metas.states.add(self.get_id(), CARRYING)
  38. # TODO: On gere une liste de carried (mais pas juste la dessous). Ne gerer qu'un objet carried ?
  39. self._context.metas.value.set(CARRIED, self.get_id(), obj.get_id())
  40. # TODO: pour le moment hardcode
  41. if isinstance(obj, Food):
  42. self.get_brain().switch_to_mode(MOVE_MODE_GOHOME)
  43. self.set_last_pheromone_point(PHEROMON_DIR_EXPLO, obj.get_position())
  44. self.get_movement_pheromone_gland().appose()
  45. def is_carrying(self):
  46. if len(self._carried):
  47. return True
  48. return False
  49. # TODO: Est-ce ici que doit etre ce code ?
  50. def set_position(self, position):
  51. if self._position is not None and position != self._position:
  52. self._brain.host_moved()
  53. super().set_position(position)
  54. if self.is_carrying():
  55. for obj_carried in self._carried:
  56. obj_carried.set_position(position)
  57. # TODO: N'est plus utiliser ! delete it !
  58. def get_last_pheromone_point(self, pheromone_name):
  59. if pheromone_name in self._last_pheromones_points:
  60. return self._last_pheromones_points[pheromone_name]
  61. return self._start_position
  62. def set_last_pheromone_point(self, pheromone_name, position):
  63. self._last_pheromones_points[pheromone_name] = position
  64. self._context.metas.value.set(LAST_PHERMONES_POINTS, self.get_id(), self._last_pheromones_points)
  65. def initialize(self):
  66. super().initialize()
  67. if self.get_movement_pheromone_gland().is_enabled():
  68. try:
  69. self.get_movement_pheromone_gland().appose()
  70. except PheromoneException:
  71. pass
  72. def get_colony(self):
  73. return self.get_collection()