Ant.py 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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)
  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. def is_carrying(self):
  43. if len(self._carried):
  44. return True
  45. return False
  46. # TODO: Est-ce ici que doit etre ce code ?
  47. def set_position(self, position):
  48. super().set_position(position)
  49. if self.is_carrying():
  50. for obj_carried in self._carried:
  51. obj_carried.set_position(position)
  52. def get_last_pheromone_point(self, pheromone_name):
  53. if pheromone_name in self._last_pheromones_points:
  54. return self._last_pheromones_points[pheromone_name]
  55. return self._start_position
  56. def set_last_pheromone_point(self, pheromone_name, position):
  57. self._last_pheromones_points[pheromone_name] = position
  58. self._context.metas.value.set(LAST_PHERMONES_POINTS, self.get_id(), self._last_pheromones_points)
  59. def initialize(self):
  60. super().initialize()
  61. try:
  62. DirectionPheromone.appose(self._context,
  63. self.get_position(),
  64. self.get_movement_pheromone_gland().get_movement_molecules())
  65. except BestPheromoneHere as best_pheromone_here:
  66. pass
  67. def get_colony(self):
  68. return self.get_collection()