PheromonesManager.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from intelligine.cst import PHEROMON_INFOS
  2. class PheromonesManager():
  3. def __init__(self, context):
  4. self._context = context
  5. def get_pheromones(self, position, prepare=[]):
  6. point_pheromones = self._context.metas.value.get(PHEROMON_INFOS,
  7. position,
  8. allow_empty=True,
  9. empty_value={})
  10. current_check = point_pheromones
  11. for prepare_key in prepare:
  12. if prepare_key not in current_check:
  13. current_check[prepare_key] = {}
  14. current_check = current_check[prepare_key]
  15. return point_pheromones
  16. def set_pheromones(self, position, pheromones):
  17. self._context.metas.value.set(PHEROMON_INFOS, position, pheromones)
  18. def get_info(self, position, address, allow_empty=False, empty_value=None):
  19. pheromones = self.get_pheromones(position, address[:-1])
  20. pheromone = pheromones
  21. for key in address[:-1]:
  22. pheromone = pheromone[key]
  23. if address[-1] not in pheromone:
  24. if allow_empty:
  25. pheromone[address[-1]] = empty_value
  26. else:
  27. raise IndexError()
  28. return pheromone[address[-1]]
  29. def increment(self, position, address, increment_value):
  30. pheromones = self.get_pheromones(position, address[:-1])
  31. pheromone = pheromones
  32. for key in address[:-1]:
  33. pheromone = pheromone[key]
  34. if address[-1] not in pheromone:
  35. pheromone[address[-1]] = 0
  36. pheromone[address[-1]] += increment_value
  37. self.set_pheromones(position, pheromones)