Ant.py 3.4KB

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