def test_astar(self, view): me = view.get_me() graph = SquareGrid(view.get_terr(), me.loaded) me_pos = me.get_pos() plant_pos = (random.randrange(0, graph.width), random.randrange(0, graph.height)) if self.my_plant: plant_pos = self.my_plant.get_pos() print(AStar.a_star_search(me_pos, plant_pos, graph))
def act(self, view, msg): debug = "off" me = view.get_me() pos = (mx, my) = me.get_pos() radius = self.radius gate = self.gate self.get_plant(view) terrain = view.get_terr().values helper = self.helper # Eat if hungry or if this is an exceptionally energy-rich spot. full = me.energy >= (cells.ENERGY_CAP / 2) hungry = (me.energy < self.target_range) energy_here = view.get_energy().get(mx, my) food = (energy_here > 0) if not full and ((hungry and food) or energy_here > 100): debug = "eat1" action = cells.ACT_EAT elif hungry: (dx, dy) = AStar.a_star_search(pos, self.my_plant.get_pos(), SquareGrid(view.get_terr(), me.loaded))[1] #(dx, dy) = helper.dir_to(pos, self.my_plant.get_pos()) pos = (dx, dy) debug = "move1" action = cells.ACT_MOVE while not helper.can_move(view, pos): pos = self.hungry_move(view) debug = "move1.1" elif self.my_plant: plant_pos = (px, py) = self.my_plant.get_pos() plant_terrain = terrain[px][py] plant_dist = helper.length( abs(pos[0] - plant_pos[0]), abs(pos[1] - plant_pos[1])) if (not me.loaded and (plant_dist > radius or (plant_dist < radius and plant_dist < terrain[mx][my])) and #(plant_dist > radius or (plant_dist < radius and plant_dist + plant_terrain < terrain[mx][my])) and #(plant_dist > radius or (plant_dist < radius and plant_dist + terrain[mx][my]) > radius) and helper.can_lift(view)): # always lift towards the plant # Check that the lift won't take too much dirt debug = "lift1" action = cells.ACT_LIFT elif (me.loaded and ((plant_dist == radius and abs(mx - plant_pos[0]) >= gate) or terrain[mx][my] < plant_dist < radius)): #(plant_dist < radius and plant_dist + plant_terrain > terrain[mx][my]))): #terrain[mx][my] + plant_dist < radius)): # always drop away from the plant # Make sure that the drop won't leave it stranded on the wall (may be done as another check elsewhere) debug = "drop1" action = cells.ACT_DROP else: plant_pos = self.my_plant.get_pos() (dx, dy) = (random.randrange(-1, 2), random.randrange(-1, 2)) plant_dist = helper.length( abs(pos[0] + dx - plant_pos[0]), abs(pos[1] + dy - plant_pos[1])) if plant_dist > (1.5 * radius): # Move back towards the plant if too far away (dx, dy) = (dx * -1, dy * -1) pos = (mx + dx, my + dy) action = cells.ACT_MOVE debug = "move2" while not helper.can_move(view, pos): debug = "move2.1" (dx, dy) = (random.randrange(-1, 2), random.randrange(-1, 2)) pos = (mx + dx, my + dy) else: (dx, dy) = (random.randrange(-1, 2), random.randrange(-1, 2)) pos = (mx + dx, my + dy) action = cells.ACT_MOVE debug = "move3" while not helper.can_move(view, pos): debug = "move3.1" (dx, dy) = (random.randrange(-1, 2), random.randrange(-1, 2)) pos = (mx + dx, my + dy) print(helper.display(view, action, pos, self)) print(debug) print(self.test_astar(view)) return cells.Action(action, pos)