Ant.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, 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 = None
  18. # TODO: Faire un body_part schema pour ces trucs la
  19. self._movement_pheromone_gland = MovementPheromoneGland(self, self._context)
  20. self._brain.switch_to_mode(MOVE_MODE_EXPLO)
  21. def _get_brain_instance(self):
  22. return AntBrain(self._context, self)
  23. def get_movement_pheromone_gland(self):
  24. return self._movement_pheromone_gland
  25. def put_carry(self, obj, position=None):
  26. if position is None:
  27. position = self._get_position()
  28. self._carried = None
  29. obj.set_position(position)
  30. self._context.metas.states.remove(self.get_id(), CARRYING)
  31. def get_carried(self):
  32. return self._carried
  33. def carry(self, obj):
  34. self._carried = obj
  35. self._context.metas.states.add(self.get_id(), CARRYING)
  36. self._context.metas.value.set(CARRIED, self.get_id(), obj.get_id())
  37. # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
  38. if isinstance(obj, Food):
  39. self.get_brain().switch_to_mode(MOVE_MODE_GOHOME)
  40. self.get_movement_pheromone_gland().appose()
  41. def is_carrying(self):
  42. if self._carried:
  43. return True
  44. return False
  45. def set_position(self, position):
  46. if self._position is not None and position != self._position:
  47. self._brain.host_moved()
  48. super().set_position(position)
  49. if self.is_carrying():
  50. self._carried.set_position(position)
  51. def initialize(self):
  52. super().initialize()
  53. if self.get_movement_pheromone_gland().is_enabled():
  54. try:
  55. self.get_movement_pheromone_gland().appose()
  56. except PheromoneException:
  57. pass
  58. def get_colony(self):
  59. return self.get_collection()