Ant.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from intelligine.core.exceptions import MoleculeException
  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, MODE_EXPLO, MODE_GOHOME, BODY_PART_PHEROMONE_GLAND, TYPE, TYPE_ANT, \
  5. COL_TRANSPORTER_CARRYING, MODE_NURSE, MODE_HOME, CARRY
  6. from intelligine.synergy.object.Food import Food
  7. from intelligine.simulation.object.molecule.MovementMoleculeGland import MovementMoleculeGland
  8. from intelligine.simulation.object.brain.AntBrain import AntBrain
  9. class Ant(Bug):
  10. _body_parts = {
  11. BODY_PART_PHEROMONE_GLAND: MovementMoleculeGland
  12. }
  13. _brain_class = AntBrain
  14. def __init__(self, collection, context):
  15. super().__init__(collection, context)
  16. context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
  17. context.metas.collections.add_list(self.get_id(), [COL_TRANSPORTER,
  18. COL_TRANSPORTER_NOT_CARRYING,
  19. COL_FIGHTER])
  20. self._carried = None
  21. # TODO: Comme pour lorsque une action put est faite, lancer un algo de choix de la mission a suivre.
  22. self._brain.switch_to_mode(MODE_EXPLO)
  23. context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
  24. def die(self):
  25. super().die()
  26. self._remove_state(TRANSPORTER)
  27. self._remove_state(ATTACKER)
  28. self._remove_col(COL_TRANSPORTER)
  29. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  30. self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
  31. self._remove_col(COL_FIGHTER)
  32. def get_movement_molecule_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. obj.set_is_carried(False, self)
  40. self._context.metas.states.remove(self.get_id(), CARRYING)
  41. self._context.metas.value.unset(CARRY, self.get_id())
  42. self._add_col(COL_TRANSPORTER_NOT_CARRYING)
  43. self._remove_col(COL_TRANSPORTER_CARRYING)
  44. def get_carried(self):
  45. return self._carried
  46. def carry(self, obj):
  47. self._carried = obj
  48. self._context.metas.states.add(self.get_id(), CARRYING)
  49. self._add_col(COL_TRANSPORTER_CARRYING)
  50. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  51. obj.set_is_carried(True, self)
  52. self._context.metas.value.set(CARRY, self.get_id(), obj.get_id())
  53. # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
  54. if isinstance(obj, Food):
  55. self.get_brain().switch_to_mode(MODE_GOHOME)
  56. self.get_movement_molecule_gland().appose()
  57. def is_carrying(self):
  58. if self._carried:
  59. return True
  60. return False
  61. def set_position(self, position):
  62. if self._position is not None and position != self._position:
  63. self._brain.host_moved()
  64. super().set_position(position)
  65. if self.is_carrying():
  66. self._carried.set_position(position)
  67. def initialize(self):
  68. super().initialize()
  69. if self.get_movement_molecule_gland().is_enabled():
  70. try:
  71. self.get_movement_molecule_gland().appose()
  72. except MoleculeException:
  73. pass
  74. def get_colony(self):
  75. return self.get_collection()