AntBrain.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from intelligine.simulation.object.brain.Brain import Brain
  2. from intelligine.simulation.object.brain.part.AntMoveBrainPart import AntMoveBrainPart
  3. from intelligine.cst import MOVE_MODE, MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, PHEROMON_DIR_HOME, PHEROMON_DIR_EXPLO
  4. from intelligine.cst import PHEROMONE_SEARCHING
  5. from intelligine.cst import BRAIN_PART_MOVE
  6. class AntBrain(Brain):
  7. _brain_part_move_class = AntMoveBrainPart
  8. def __init__(self, context, host):
  9. super().__init__(context, host)
  10. self._set_brain_part(BRAIN_PART_MOVE, self._get_move_brain_part_instance())
  11. self._movement_mode = MOVE_MODE_EXPLO
  12. self._distance_from_objective = 0 # TODO rename: distance_since_objective
  13. self._pheromone_searching = PHEROMON_DIR_EXPLO
  14. def _get_move_brain_part_instance(self):
  15. return self._brain_part_move_class()
  16. def switch_to_mode(self, mode):
  17. self._movement_mode = mode
  18. self._update_pheromone_gland(mode)
  19. self._context.metas.value.set(MOVE_MODE, self._host.get_id(), mode)
  20. self._distance_from_objective = 0
  21. self._update_pheromone_searching(mode)
  22. def _update_pheromone_gland(self, mode):
  23. if mode == MOVE_MODE_EXPLO:
  24. pheromone_direction_type = PHEROMON_DIR_HOME
  25. elif mode == MOVE_MODE_GOHOME:
  26. pheromone_direction_type = PHEROMON_DIR_EXPLO
  27. else:
  28. raise NotImplementedError()
  29. self._host.get_movement_pheromone_gland().set_pheromone_type(pheromone_direction_type)
  30. def _update_pheromone_searching(self, mode):
  31. if mode == MOVE_MODE_EXPLO:
  32. pheromone_searching = PHEROMON_DIR_EXPLO
  33. elif mode == MOVE_MODE_GOHOME:
  34. pheromone_searching = PHEROMON_DIR_HOME
  35. else:
  36. raise NotImplementedError()
  37. self._pheromone_searching = pheromone_searching
  38. self._context.metas.value.set(PHEROMONE_SEARCHING, self._host.get_id(), pheromone_searching)
  39. def get_movement_mode(self):
  40. return self._movement_mode
  41. def host_moved(self, distance=1):
  42. self._distance_from_objective += 1
  43. def set_distance_from_objective(self, distance):
  44. self._distance_from_objective = distance
  45. def get_distance_from_objective(self):
  46. return self._distance_from_objective