fsm_ant.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from simulation.board import Board
  2. from simulation.logic.dumb_scouter import DumbScouter
  3. from simulation.logic.gatherer import Gatherer
  4. from simulation.logic.sensing_scouter import SensingScouter
  5. class FSMAnt(DumbScouter):
  6. # Max value an ant has to store to stop being starved
  7. MAX_HARVESTING = 100
  8. # Value an ant eat to do a step
  9. EAT_VALUE = 1
  10. # Value an ant collect by stepping on food
  11. HARVEST_VALUE = 10
  12. # Multiplication factor for drop value (see blob variable) when an ant is starving
  13. RATIO_DROP_STARVE = 2
  14. USE_DIAGONAL = False
  15. def __init__(self, board, knowledge, x, y, drop_value):
  16. """
  17. :type board: Board
  18. :type knowledge: dict
  19. :type x: int
  20. :type y: int
  21. :type drop_value: float
  22. """
  23. DumbScouter.__init__(self, board, knowledge, x, y, drop_value)
  24. self.gatherer_logic = Gatherer(board, knowledge, x, y, drop_value, FSMAnt.USE_DIAGONAL)
  25. self.scouting_logic = SensingScouter(board, knowledge, x, y, drop_value)
  26. self.harvest = FSMAnt.MAX_HARVESTING
  27. self.starving = False
  28. self.init_drop = drop_value
  29. def move(self):
  30. if self.starving:
  31. self.gatherer_logic.move()
  32. self.x = self.gatherer_logic.x
  33. self.y = self.gatherer_logic.y
  34. else:
  35. self.scouting_logic.move()
  36. self.x = self.scouting_logic.x
  37. self.y = self.scouting_logic.y
  38. def init_gathering(self):
  39. self.gatherer_logic.reset()
  40. self.gatherer_logic.x = self.x
  41. self.gatherer_logic.y = self.y
  42. def init_scouting(self):
  43. self.scouting_logic.x = self.x
  44. self.scouting_logic.y = self.y
  45. self.drop_value = self.init_drop
  46. def update(self):
  47. if self.harvest > 0 and self.starving:
  48. self.drop_value = FSMAnt.RATIO_DROP_STARVE * self.init_drop
  49. DumbScouter.update(self)
  50. if len(self.knowledge['food']) != 0 and not self.starving:
  51. self.harvest -= FSMAnt.EAT_VALUE
  52. self.harvest = max(0, self.harvest)
  53. if self.board.has_food(self.x, self.y):
  54. if len(self.knowledge['food']) == 1:
  55. self.harvest = FSMAnt.MAX_HARVESTING
  56. else:
  57. self.harvest += FSMAnt.HARVEST_VALUE
  58. self.harvest = min(self.harvest, FSMAnt.MAX_HARVESTING)
  59. if self.harvest == 0 and not self.starving:
  60. self.starving = True
  61. self.init_gathering()
  62. if self.harvest == FSMAnt.MAX_HARVESTING and self.starving:
  63. self.starving = False
  64. self.init_scouting()