Ant.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, PUT_FAIL_COUNT
  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. self._put_fail_count = 0
  25. def die(self):
  26. super().die()
  27. self._remove_state(TRANSPORTER)
  28. self._remove_state(ATTACKER)
  29. self._remove_col(COL_TRANSPORTER)
  30. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  31. self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
  32. self._remove_col(COL_FIGHTER)
  33. def get_movement_molecule_gland(self):
  34. return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
  35. def put_carry(self, obj, position=None):
  36. if position is None:
  37. position = self._get_position()
  38. self._carried = None
  39. obj.set_position(position)
  40. obj.set_is_carried(False, self)
  41. self._context.metas.states.remove(self.get_id(), CARRYING)
  42. self._context.metas.value.unset(CARRY, self.get_id())
  43. self._add_col(COL_TRANSPORTER_NOT_CARRYING)
  44. self._remove_col(COL_TRANSPORTER_CARRYING)
  45. def get_carried(self):
  46. return self._carried
  47. def carry(self, obj):
  48. self._carried = obj
  49. self._context.metas.states.add(self.get_id(), CARRYING)
  50. self._add_col(COL_TRANSPORTER_CARRYING)
  51. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  52. obj.set_is_carried(True, self)
  53. self._context.metas.value.set(CARRY, self.get_id(), obj.get_id())
  54. # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
  55. if isinstance(obj, Food):
  56. self.get_brain().switch_to_mode(MODE_GOHOME)
  57. self.get_movement_molecule_gland().appose()
  58. def is_carrying(self):
  59. if self._carried:
  60. return True
  61. return False
  62. def set_position(self, position):
  63. if self._position is not None and position != self._position:
  64. self._brain.host_moved()
  65. super().set_position(position)
  66. if self.is_carrying():
  67. self._carried.set_position(position)
  68. def initialize(self):
  69. super().initialize()
  70. if self.get_movement_molecule_gland().is_enabled():
  71. try:
  72. self.get_movement_molecule_gland().appose()
  73. except MoleculeException:
  74. pass
  75. def get_colony(self):
  76. return self.get_collection()
  77. def get_put_fail_count(self):
  78. return self._put_fail_count
  79. def increment_put_fail_count(self):
  80. self._put_fail_count += 1
  81. self._context.metas.value.set(PUT_FAIL_COUNT, self.get_id(), self._put_fail_count)
  82. def reinit_put_fail_count(self):
  83. self._put_fail_count = 0
  84. self._context.metas.value.set(PUT_FAIL_COUNT, self.get_id(), self._put_fail_count)