AntBrain.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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
  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. else:
  40. raise NotImplementedError()
  41. self._host.get_movement_pheromone_gland().set_pheromone_type(pheromone_direction_type)
  42. def _update_pheromone_searching(self, mode):
  43. if mode == MOVE_MODE_EXPLO:
  44. pheromone_searching = PHEROMON_DIR_EXPLO
  45. elif mode == MOVE_MODE_GOHOME:
  46. pheromone_searching = PHEROMON_DIR_HOME
  47. else:
  48. raise NotImplementedError()
  49. self._pheromone_searching = pheromone_searching
  50. self._context.metas.value.set(PHEROMONE_SEARCHING, self._host.get_id(), pheromone_searching)
  51. def get_movement_mode(self):
  52. return self._movement_mode
  53. def host_moved(self, distance=1):
  54. self._distance_from_objective += 1
  55. def set_distance_from_objective(self, distance):
  56. self._distance_from_objective = distance
  57. def get_distance_from_objective(self):
  58. return self._distance_from_objective