AntBrain.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from intelligine.simulation.object.brain.Brain import Brain
  2. from intelligine.simulation.object.brain.part.attack.AttackBrainPart import AttackBrainPart
  3. from intelligine.simulation.object.brain.part.move.AntMoveBrainPart import AntMoveBrainPart
  4. from intelligine.cst import MOVE_MODE, MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, PHEROMON_DIR_HOME, PHEROMON_DIR_EXPLO, \
  5. BRAIN_PART_TAKE, BRAIN_PART_PUT, MOVE_MODE_NURSE, PHEROMON_DIR_NONE, BRAIN_PART_ATTACK
  6. from intelligine.cst import PHEROMONE_SEARCHING
  7. from intelligine.cst import BRAIN_PART_MOVE
  8. from intelligine.simulation.object.brain.part.transport.AntPutBrainPart import AntPutBrainPart
  9. from intelligine.simulation.object.brain.part.transport.AntTakeBrainPart import AntTakeBrainPart
  10. class AntBrain(Brain):
  11. # TODO: methode __init_ pour la classe ? Pour surcharger ici.
  12. _brain_parts = {
  13. BRAIN_PART_MOVE: AntMoveBrainPart,
  14. BRAIN_PART_TAKE: AntTakeBrainPart,
  15. BRAIN_PART_PUT: AntPutBrainPart,
  16. BRAIN_PART_ATTACK: AttackBrainPart
  17. }
  18. def __init__(self, context, host):
  19. super().__init__(context, host)
  20. self._movement_mode = MOVE_MODE_EXPLO
  21. self._distance_from_objective = 0 # TODO rename: distance_since_objective
  22. self._pheromone_searching = PHEROMON_DIR_EXPLO
  23. def switch_to_mode(self, mode):
  24. self._movement_mode = mode
  25. self._update_pheromone_gland(mode)
  26. self._context.metas.value.set(MOVE_MODE, self._host.get_id(), mode)
  27. self._distance_from_objective = 0
  28. self._update_pheromone_searching(mode)
  29. def _update_pheromone_gland(self, mode):
  30. if mode == MOVE_MODE_EXPLO:
  31. pheromone_direction_type = PHEROMON_DIR_HOME
  32. elif mode == MOVE_MODE_GOHOME:
  33. pheromone_direction_type = PHEROMON_DIR_EXPLO
  34. elif mode == MOVE_MODE_NURSE:
  35. pheromone_direction_type = None
  36. else:
  37. raise NotImplementedError()
  38. if pheromone_direction_type:
  39. self._host.get_movement_pheromone_gland().set_pheromone_type(pheromone_direction_type)
  40. self._host.get_movement_pheromone_gland().enable()
  41. else:
  42. self._host.get_movement_pheromone_gland().disable()
  43. def _update_pheromone_searching(self, mode):
  44. if mode == MOVE_MODE_EXPLO:
  45. pheromone_searching = PHEROMON_DIR_EXPLO
  46. elif mode == MOVE_MODE_GOHOME:
  47. pheromone_searching = PHEROMON_DIR_HOME
  48. elif mode == MOVE_MODE_NURSE:
  49. pheromone_searching = PHEROMON_DIR_NONE
  50. else:
  51. raise NotImplementedError()
  52. self._pheromone_searching = pheromone_searching
  53. self._context.metas.value.set(PHEROMONE_SEARCHING, self._host.get_id(), pheromone_searching)
  54. def get_movement_mode(self):
  55. return self._movement_mode
  56. def host_moved(self, distance=1):
  57. self._distance_from_objective += 1
  58. def set_distance_from_objective(self, distance):
  59. self._distance_from_objective = distance
  60. def get_distance_from_objective(self):
  61. return self._distance_from_objective