added BFS based search for finding chests and avoiding creatures

This commit is contained in:
Niebby 2021-04-13 15:41:09 +02:00
parent d77f74ca59
commit 9049316049

View File

@ -114,8 +114,8 @@ class Player(Creature):
self.pos,
moore=False,
include_center=False)
new_position = (
self.pos[0] + self.directions[self.direction][0], self.pos[1] + self.directions[self.direction][1])
new_position = (self.pos[0] + self.directions[self.direction][0],
self.pos[1] + self.directions[self.direction][1])
if new_position in possible_steps:
self.model.grid.move_agent(self, new_position)
@ -227,10 +227,51 @@ class Player(Creature):
# self.weapon3 = chest.loot
# 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):
if self.health > 0:
self.moveFwd()
self.rotate(False)
print(self.findShortestPathToTarget())
cellmates = self.model.grid.get_cell_list_contents([self.pos])
if len(cellmates) > 1:
if isinstance(cellmates[0], Box):