Ant.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. COL_TRANSPORTER_CARRYING
  6. from intelligine.synergy.object.Food import Food
  7. from intelligine.simulation.object.pheromone.MovementPheromoneGland import MovementPheromoneGland
  8. from intelligine.simulation.object.brain.AntBrain import AntBrain
  9. class Ant(Bug):
  10. _body_parts = {
  11. BODY_PART_PHEROMONE_GLAND: MovementPheromoneGland
  12. }
  13. def __init__(self, collection, context):
  14. super().__init__(collection, context)
  15. context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
  16. context.metas.collections.add_list(self.get_id(), [COL_TRANSPORTER,
  17. COL_TRANSPORTER_NOT_CARRYING,
  18. COL_FIGHTER])
  19. self._carried = None
  20. self._brain.switch_to_mode(MOVE_MODE_EXPLO)
  21. context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
  22. def die(self):
  23. super().die()
  24. self._remove_state(TRANSPORTER)
  25. self._remove_state(ATTACKER)
  26. self._remove_col(COL_TRANSPORTER)
  27. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  28. self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
  29. self._remove_col(COL_FIGHTER)
  30. def _get_brain_instance(self):
  31. return AntBrain(self._context, self)
  32. def get_movement_pheromone_gland(self):
  33. return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
  34. def put_carry(self, obj, position=None):
  35. if position is None:
  36. position = self._get_position()
  37. self._carried = None
  38. obj.set_position(position)
  39. self._context.metas.states.remove(self.get_id(), CARRYING)
  40. def get_carried(self):
  41. return self._carried
  42. def carry(self, obj):
  43. self._carried = obj
  44. self._context.metas.states.add(self.get_id(), CARRYING)
  45. self._context.metas.value.set(CARRIED, self.get_id(), obj.get_id())
  46. # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
  47. if isinstance(obj, Food):
  48. self.get_brain().switch_to_mode(MOVE_MODE_GOHOME)
  49. self.get_movement_pheromone_gland().appose()
  50. def is_carrying(self):
  51. if self._carried:
  52. return True
  53. return False
  54. def set_position(self, position):
  55. if self._position is not None and position != self._position:
  56. self._brain.host_moved()
  57. super().set_position(position)
  58. if self.is_carrying():
  59. self._carried.set_position(position)
  60. def initialize(self):
  61. super().initialize()
  62. if self.get_movement_pheromone_gland().is_enabled():
  63. try:
  64. self.get_movement_pheromone_gland().appose()
  65. except PheromoneException:
  66. pass
  67. def get_colony(self):
  68. return self.get_collection()