AntBrain.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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
  5. from intelligine.cst import PHEROMONE_SEARCHING
  6. from intelligine.cst import BRAIN_PART_MOVE
  7. from intelligine.simulation.object.brain.part.take.AntTakeBrainPart import AntTakeBrainPart
  8. class AntBrain(Brain):
  9. _brain_part_move_class = AntMoveBrainPart
  10. _brain_part_take_class = AntTakeBrainPart
  11. def __init__(self, context, host):
  12. super().__init__(context, host)
  13. # TODO: Gerer les BrainPart avec un dictionnaire ?
  14. self._set_brain_part(BRAIN_PART_MOVE, self._get_move_brain_part_instance())
  15. self._set_brain_part(BRAIN_PART_TAKE, self._get_take_brain_part_instance())
  16. self._movement_mode = MOVE_MODE_EXPLO
  17. self._distance_from_objective = 0 # TODO rename: distance_since_objective
  18. self._pheromone_searching = PHEROMON_DIR_EXPLO
  19. def _get_move_brain_part_instance(self):
  20. return self._brain_part_move_class()
  21. def _get_take_brain_part_instance(self):
  22. return self._brain_part_take_class()
  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. else:
  35. raise NotImplementedError()
  36. self._host.get_movement_pheromone_gland().set_pheromone_type(pheromone_direction_type)
  37. def _update_pheromone_searching(self, mode):
  38. if mode == MOVE_MODE_EXPLO:
  39. pheromone_searching = PHEROMON_DIR_EXPLO
  40. elif mode == MOVE_MODE_GOHOME:
  41. pheromone_searching = PHEROMON_DIR_HOME
  42. else:
  43. raise NotImplementedError()
  44. self._pheromone_searching = pheromone_searching
  45. self._context.metas.value.set(PHEROMONE_SEARCHING, self._host.get_id(), pheromone_searching)
  46. def get_movement_mode(self):
  47. return self._movement_mode
  48. def host_moved(self, distance=1):
  49. self._distance_from_objective += 1
  50. def set_distance_from_objective(self, distance):
  51. self._distance_from_objective = distance
  52. def get_distance_from_objective(self):
  53. return self._distance_from_objective