Ant.py 3.5KB

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. 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: pour le moment hardcode
  39. if isinstance(obj, Food):
  40. self.get_brain().switch_to_mode(MOVE_MODE_GOHOME)
  41. self.set_last_pheromone_point(PHEROMON_DIR_EXPLO, obj.get_position())
  42. self.get_movement_pheromone_gland().appose()
  43. def is_carrying(self):
  44. if len(self._carried):
  45. return True
  46. return False
  47. # TODO: Est-ce ici que doit etre ce code ?
  48. def set_position(self, position):
  49. if self._position is not None:
  50. self._brain.host_moved()
  51. super().set_position(position)
  52. if self.is_carrying():
  53. for obj_carried in self._carried:
  54. obj_carried.set_position(position)
  55. def get_last_pheromone_point(self, pheromone_name):
  56. if pheromone_name in self._last_pheromones_points:
  57. return self._last_pheromones_points[pheromone_name]
  58. return self._start_position
  59. def set_last_pheromone_point(self, pheromone_name, position):
  60. self._last_pheromones_points[pheromone_name] = position
  61. self._context.metas.value.set(LAST_PHERMONES_POINTS, self.get_id(), self._last_pheromones_points)
  62. def initialize(self):
  63. super().initialize()
  64. try:
  65. DirectionPheromone.appose(self._context,
  66. self.get_position(),
  67. self.get_movement_pheromone_gland().get_movement_molecules())
  68. except BestPheromoneHere as best_pheromone_here:
  69. pass
  70. def get_colony(self):
  71. return self.get_collection()