AntBrain.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. _brain_part_move_class = AntMoveBrainPart
  11. _brain_part_take_class = AntTakeBrainPart
  12. _brain_part_put_class = AntPutBrainPart
  13. def __init__(self, context, host):
  14. super().__init__(context, host)
  15. # TODO: Gerer les BrainPart avec un dictionnaire ?
  16. self._set_brain_part(BRAIN_PART_MOVE, self._get_move_brain_part_instance())
  17. self._set_brain_part(BRAIN_PART_TAKE, self._get_take_brain_part_instance())
  18. self._set_brain_part(BRAIN_PART_PUT, self._get_put_brain_part_instance())
  19. self._movement_mode = MOVE_MODE_EXPLO
  20. self._distance_from_objective = 0 # TODO rename: distance_since_objective
  21. self._pheromone_searching = PHEROMON_DIR_EXPLO
  22. def _get_move_brain_part_instance(self):
  23. return self._brain_part_move_class()
  24. def _get_take_brain_part_instance(self):
  25. return self._brain_part_take_class()
  26. def _get_put_brain_part_instance(self):
  27. return self._brain_part_put_class()
  28. def switch_to_mode(self, mode):
  29. self._movement_mode = mode
  30. self._update_pheromone_gland(mode)
  31. self._context.metas.value.set(MOVE_MODE, self._host.get_id(), mode)
  32. self._distance_from_objective = 0
  33. self._update_pheromone_searching(mode)
  34. def _update_pheromone_gland(self, mode):
  35. if mode == MOVE_MODE_EXPLO:
  36. pheromone_direction_type = PHEROMON_DIR_HOME
  37. elif mode == MOVE_MODE_GOHOME:
  38. pheromone_direction_type = PHEROMON_DIR_EXPLO
  39. elif mode == MOVE_MODE_NURSE:
  40. pheromone_direction_type = None
  41. else:
  42. raise NotImplementedError()
  43. if pheromone_direction_type:
  44. self._host.get_movement_pheromone_gland().set_pheromone_type(pheromone_direction_type)
  45. self._host.get_movement_pheromone_gland().enable()
  46. else:
  47. self._host.get_movement_pheromone_gland().disable()
  48. def _update_pheromone_searching(self, mode):
  49. if mode == MOVE_MODE_EXPLO:
  50. pheromone_searching = PHEROMON_DIR_EXPLO
  51. elif mode == MOVE_MODE_GOHOME:
  52. pheromone_searching = PHEROMON_DIR_HOME
  53. elif mode == MOVE_MODE_NURSE:
  54. pheromone_searching = PHEROMON_DIR_NONE
  55. else:
  56. raise NotImplementedError()
  57. self._pheromone_searching = pheromone_searching
  58. self._context.metas.value.set(PHEROMONE_SEARCHING, self._host.get_id(), pheromone_searching)
  59. def get_movement_mode(self):
  60. return self._movement_mode
  61. def host_moved(self, distance=1):
  62. self._distance_from_objective += 1
  63. def set_distance_from_objective(self, distance):
  64. self._distance_from_objective = distance
  65. def get_distance_from_objective(self):
  66. return self._distance_from_objective