added BFS based search for finding chests and avoiding creatures
This commit is contained in:
parent
d77f74ca59
commit
9049316049
45
agent2.py
45
agent2.py
@ -114,8 +114,8 @@ class Player(Creature):
|
|||||||
self.pos,
|
self.pos,
|
||||||
moore=False,
|
moore=False,
|
||||||
include_center=False)
|
include_center=False)
|
||||||
new_position = (
|
new_position = (self.pos[0] + self.directions[self.direction][0],
|
||||||
self.pos[0] + self.directions[self.direction][0], self.pos[1] + self.directions[self.direction][1])
|
self.pos[1] + self.directions[self.direction][1])
|
||||||
if new_position in possible_steps:
|
if new_position in possible_steps:
|
||||||
self.model.grid.move_agent(self, new_position)
|
self.model.grid.move_agent(self, new_position)
|
||||||
|
|
||||||
@ -227,10 +227,51 @@ class Player(Creature):
|
|||||||
# self.weapon3 = chest.loot
|
# self.weapon3 = chest.loot
|
||||||
# chest.loot = buffer
|
# chest.loot = buffer
|
||||||
|
|
||||||
|
def findShortestPathToTarget(self):
|
||||||
|
visited = []
|
||||||
|
precedessors = {}
|
||||||
|
queue = [self.pos]
|
||||||
|
found_target = False
|
||||||
|
|
||||||
|
while queue:
|
||||||
|
cur_pos = queue.pop(0)
|
||||||
|
|
||||||
|
#check for target
|
||||||
|
cell_contents = self.model.grid.get_cell_list_contents([cur_pos])
|
||||||
|
if cell_contents and any([isinstance(thing, Box) for thing in cell_contents]):
|
||||||
|
found_target = cur_pos
|
||||||
|
break
|
||||||
|
|
||||||
|
#enqueue safe unvisited neighbours
|
||||||
|
neighbours = self.model.grid.get_neighborhood(
|
||||||
|
cur_pos,
|
||||||
|
moore=False,
|
||||||
|
include_center=False)
|
||||||
|
for cell in neighbours:
|
||||||
|
#if cell hasn't been visited and the contents don't include creatures
|
||||||
|
if cell not in visited and not any([isinstance(thing, Creature) for thing in self.model.grid.get_cell_list_contents([cell])]):
|
||||||
|
queue.append(cell)
|
||||||
|
precedessors[cell] = cur_pos
|
||||||
|
|
||||||
|
visited.append(cur_pos)
|
||||||
|
|
||||||
|
if found_target:
|
||||||
|
path = [found_target]
|
||||||
|
while True:
|
||||||
|
if path[0] == self.pos:
|
||||||
|
break
|
||||||
|
precedessor = precedessors[path[0]]
|
||||||
|
path.insert(0, precedessor)
|
||||||
|
return path
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
if self.health > 0:
|
if self.health > 0:
|
||||||
self.moveFwd()
|
self.moveFwd()
|
||||||
self.rotate(False)
|
self.rotate(False)
|
||||||
|
print(self.findShortestPathToTarget())
|
||||||
cellmates = self.model.grid.get_cell_list_contents([self.pos])
|
cellmates = self.model.grid.get_cell_list_contents([self.pos])
|
||||||
if len(cellmates) > 1:
|
if len(cellmates) > 1:
|
||||||
if isinstance(cellmates[0], Box):
|
if isinstance(cellmates[0], Box):
|
||||||
|
Loading…
Reference in New Issue
Block a user