Ant.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, TYPE, TYPE_ANT
  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. context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
  21. def _get_brain_instance(self):
  22. return AntBrain(self._context, self)
  23. def get_movement_pheromone_gland(self):
  24. return self.get_body_part(BODY_PART_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()