Ant.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from intelligine.core.exceptions import PheromoneException
  2. from intelligine.synergy.object.Bug import Bug
  3. from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER, COL_TRANSPORTER, COL_TRANSPORTER_NOT_CARRYING, \
  4. COL_FIGHTER, MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, CARRIED, BODY_PART_PHEROMONE_GLAND
  5. from intelligine.synergy.object.Food import Food
  6. from intelligine.simulation.object.pheromone.MovementPheromoneGland import MovementPheromoneGland
  7. from intelligine.simulation.object.brain.AntBrain import AntBrain
  8. class Ant(Bug):
  9. _body_parts = {
  10. BODY_PART_PHEROMONE_GLAND: MovementPheromoneGland
  11. }
  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 = None
  19. self._brain.switch_to_mode(MOVE_MODE_EXPLO)
  20. def _get_brain_instance(self):
  21. return AntBrain(self._context, self)
  22. def get_movement_pheromone_gland(self):
  23. return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
  24. def put_carry(self, obj, position=None):
  25. if position is None:
  26. position = self._get_position()
  27. self._carried = None
  28. obj.set_position(position)
  29. self._context.metas.states.remove(self.get_id(), CARRYING)
  30. def get_carried(self):
  31. return self._carried
  32. def carry(self, obj):
  33. self._carried = obj
  34. self._context.metas.states.add(self.get_id(), CARRYING)
  35. self._context.metas.value.set(CARRIED, self.get_id(), obj.get_id())
  36. # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
  37. if isinstance(obj, Food):
  38. self.get_brain().switch_to_mode(MOVE_MODE_GOHOME)
  39. self.get_movement_pheromone_gland().appose()
  40. def is_carrying(self):
  41. if self._carried:
  42. return True
  43. return False
  44. def set_position(self, position):
  45. if self._position is not None and position != self._position:
  46. self._brain.host_moved()
  47. super().set_position(position)
  48. if self.is_carrying():
  49. self._carried.set_position(position)
  50. def initialize(self):
  51. super().initialize()
  52. if self.get_movement_pheromone_gland().is_enabled():
  53. try:
  54. self.get_movement_pheromone_gland().appose()
  55. except PheromoneException:
  56. pass
  57. def get_colony(self):
  58. return self.get_collection()