Ant.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. import random
  10. class Ant(Bug):
  11. _body_parts = {
  12. BODY_PART_PHEROMONE_GLAND: MovementMoleculeGland
  13. }
  14. _brain_class = AntBrain
  15. def __init__(self, collection, context):
  16. super().__init__(collection, context)
  17. context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
  18. context.metas.collections.add_list(self.get_id(), [COL_TRANSPORTER,
  19. COL_TRANSPORTER_NOT_CARRYING,
  20. COL_FIGHTER])
  21. self._carried = None
  22. # TODO: Comme pour lorsque une action put est faite, lancer un algo de choix de la mission a suivre.
  23. if random.choice([0, 1]):
  24. self._brain.switch_to_mode(MODE_EXPLO)
  25. else:
  26. self._brain.switch_to_mode(MODE_NURSE)
  27. context.metas.list.add(TYPE, self.get_id(), TYPE_ANT)
  28. self._put_fail_count = 0
  29. def die(self):
  30. super().die()
  31. self._remove_state(TRANSPORTER)
  32. self._remove_state(ATTACKER)
  33. self._remove_col(COL_TRANSPORTER)
  34. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  35. self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
  36. self._remove_col(COL_FIGHTER)
  37. def get_movement_molecule_gland(self):
  38. return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
  39. def put_carry(self, obj, position=None):
  40. if position is None:
  41. position = self._get_position()
  42. self._carried = None
  43. obj.set_position(position)
  44. obj.set_is_carried(False, self)
  45. self._context.metas.states.remove(self.get_id(), CARRYING)
  46. self._context.metas.value.unset(CARRY, self.get_id())
  47. self._add_col(COL_TRANSPORTER_NOT_CARRYING)
  48. self._remove_col(COL_TRANSPORTER_CARRYING)
  49. def get_carried(self):
  50. return self._carried
  51. def carry(self, obj):
  52. self._carried = obj
  53. self._context.metas.states.add(self.get_id(), CARRYING)
  54. self._add_col(COL_TRANSPORTER_CARRYING)
  55. self._remove_col(COL_TRANSPORTER_NOT_CARRYING)
  56. obj.set_is_carried(True, self)
  57. self._context.metas.value.set(CARRY, self.get_id(), obj.get_id())
  58. # TODO: pour le moment hardcode, a gerer dans AntTakeBrainPart (callback en fct de ce qui est depose)
  59. if isinstance(obj, Food):
  60. self.get_brain().switch_to_mode(MODE_GOHOME)
  61. self.get_movement_molecule_gland().appose()
  62. def is_carrying(self):
  63. if self._carried:
  64. return True
  65. return False
  66. def set_position(self, position):
  67. if self._position is not None and position != self._position:
  68. self._brain.host_moved()
  69. super().set_position(position)
  70. if self.is_carrying():
  71. self._carried.set_position(position)
  72. def initialize(self):
  73. super().initialize()
  74. if self.get_movement_molecule_gland().is_enabled():
  75. try:
  76. self.get_movement_molecule_gland().appose()
  77. except MoleculeException:
  78. pass
  79. def get_colony(self):
  80. return self.get_collection()
  81. def get_put_fail_count(self):
  82. return self._put_fail_count
  83. def increment_put_fail_count(self):
  84. self._put_fail_count += 1
  85. self._context.metas.value.set(PUT_FAIL_COUNT, self.get_id(), self._put_fail_count)
  86. def reinit_put_fail_count(self):
  87. self._put_fail_count = 0
  88. self._context.metas.value.set(PUT_FAIL_COUNT, self.get_id(), self._put_fail_count)