AntBrain.py 2.9KB

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