player uses bfs to find the shortest route to a chest while avoiding enemies
This commit is contained in:
parent
0969564c3f
commit
5d83e51c73
76
hero.py
76
hero.py
@ -21,6 +21,7 @@ class Player(Creature):
|
|||||||
self.direction = 0
|
self.direction = 0
|
||||||
self.queue=[]
|
self.queue=[]
|
||||||
self.goalchest=False
|
self.goalchest=False
|
||||||
|
self.step_counter=0
|
||||||
|
|
||||||
def rotate(self, clockwise=True):
|
def rotate(self, clockwise=True):
|
||||||
if clockwise:
|
if clockwise:
|
||||||
@ -37,7 +38,7 @@ class Player(Creature):
|
|||||||
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)
|
||||||
|
print("moved to ", new_position)
|
||||||
# def move(self): # OLD
|
# def move(self): # OLD
|
||||||
# possible_steps = self.model.grid.get_neighborhood(
|
# possible_steps = self.model.grid.get_neighborhood(
|
||||||
# self.pos,
|
# self.pos,
|
||||||
@ -195,85 +196,108 @@ class Player(Creature):
|
|||||||
|
|
||||||
# pole to element wziety z kolejki path
|
# pole to element wziety z kolejki path
|
||||||
|
|
||||||
def actionPlanner(self, cell):
|
def actionPlanner(self, cell_from, cell_to):
|
||||||
x0 = self.pos[0]
|
x0 = cell_from[0]
|
||||||
y0 = self.pos[1]
|
y0 = cell_from[1]
|
||||||
x = cell[0]
|
x = cell_to[0]
|
||||||
y = cell[1]
|
y = cell_to[1]
|
||||||
if (x > x0):
|
if (x > x0):
|
||||||
if (self.direction == 1):
|
if (self.direction == 1):
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
elif (self.direction == 0):
|
elif (self.direction == 0):
|
||||||
self.queue.append("tr")
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
elif (self.direction == 2):
|
elif (self.direction == 2):
|
||||||
self.queue.append("tl")
|
self.queue.append("tl")
|
||||||
|
self.rotate(False)
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
else:
|
else:
|
||||||
self.queue.append("tr")
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
self.queue.append("tr")
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
self.queue.insert("fwd")
|
self.queue.insert("fwd")
|
||||||
elif (x < x0):
|
elif (x < x0):
|
||||||
if (self.direction == 3):
|
if (self.direction == 3):
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
elif (self.direction == 0):
|
elif (self.direction == 0):
|
||||||
self.queue.append("tl")
|
self.queue.append("tl")
|
||||||
|
self.rotate(False)
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
elif (self.direction == 2):
|
elif (self.direction == 2):
|
||||||
self.queue.append("tr")
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
else:
|
else:
|
||||||
self.queue.append("tr")
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
self.queue.append("tr")
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
|
self.queue.append("fwd")
|
||||||
|
elif (y > y0):
|
||||||
|
if (self.direction == 0):
|
||||||
|
self.queue.append("fwd")
|
||||||
|
elif (self.direction == 1):
|
||||||
|
self.queue.append("tl")
|
||||||
|
self.rotate(False)
|
||||||
|
self.queue.append("fwd")
|
||||||
|
elif (self.direction == 3):
|
||||||
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
else:
|
|
||||||
if (y > y0):
|
|
||||||
if (self.direction == 0):
|
|
||||||
self.queue.append("fwd")
|
|
||||||
elif (self.direction == 1):
|
|
||||||
self.queue.append("tl")
|
|
||||||
self.queue.append("fwd")
|
|
||||||
elif (self.direction == 3):
|
|
||||||
self.queue.append("tr")
|
|
||||||
self.queue.append("fwd")
|
|
||||||
else:
|
|
||||||
self.queue.append("tr")
|
|
||||||
self.queue.append("tr")
|
|
||||||
self.queue.append("fwd")
|
|
||||||
else:
|
else:
|
||||||
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
|
self.queue.append("fwd")
|
||||||
|
elif (y < y0):
|
||||||
if (self.direction == 2):
|
if (self.direction == 2):
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
elif (self.direction == 3):
|
elif (self.direction == 3):
|
||||||
self.queue.append("tl")
|
self.queue.append("tl")
|
||||||
|
self.rotate(False)
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
elif (self.direction == 1):
|
elif (self.direction == 1):
|
||||||
self.queue.append("tr")
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
else:
|
else:
|
||||||
self.queue.append("tr")
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
self.queue.append("tr")
|
self.queue.append("tr")
|
||||||
|
self.rotate(True)
|
||||||
self.queue.append("fwd")
|
self.queue.append("fwd")
|
||||||
# if (len(self.path)==0):
|
elif (len(self.path)==0):
|
||||||
# self.queue.append("op")
|
self.queue.append("op")
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
|
self.step_counter+=1
|
||||||
if self.health > 0:
|
if self.health > 0:
|
||||||
# self.moveFwd()
|
# self.moveFwd()
|
||||||
# self.rotate(False)
|
# self.rotate(False)
|
||||||
|
print("position: ", self.pos)
|
||||||
if not self.goalchest: #jeśli nie ma wyznaczonej skrzynki do której idzie to robi bfs żeby ją wyznaczyć
|
if not self.goalchest: #jeśli nie ma wyznaczonej skrzynki do której idzie to robi bfs żeby ją wyznaczyć
|
||||||
self.path=self.findShortestPathToTarget()
|
self.path=self.findShortestPathToTarget()
|
||||||
print("the player should follow this path:",self.path)
|
print("the player should follow this path:", self.path)
|
||||||
for cell in self.path: #iteruje po kolejnych krotkach ze współrzędnymi pól
|
for i in range(len(self.path)-1): #iteruje po kolejnych krotkach ze współrzędnymi pól
|
||||||
self.actionPlanner(cell) #dla każdego pola dodaje do kolejki kilka akcji potrzebnych żeby do niego dojść
|
#actionPlanner ma się wykonać i-1 razy, bo dla ostatniego pola już nie
|
||||||
|
self.actionPlanner(self.path[i], self.path[i+1]) #dla każdego pola dodaje do kolejki kilka akcji potrzebnych żeby do niego dojść
|
||||||
print(self.queue)
|
print(self.queue)
|
||||||
if len(self.queue)!=0:
|
while len(self.queue)!=0:
|
||||||
self.action=self.queue.pop(0)
|
self.action=self.queue.pop(0)
|
||||||
if self.action=="tr":
|
if self.action=="tr":
|
||||||
self.rotate(True)
|
self.rotate(True)
|
||||||
|
print("tr'd, direction: ", self.direction)
|
||||||
|
break
|
||||||
elif self.action=="tl":
|
elif self.action=="tl":
|
||||||
self.rotate(False)
|
self.rotate(False)
|
||||||
|
print("tl'd, direction: ", self.direction)
|
||||||
|
break
|
||||||
elif self.action=="fwd":
|
elif self.action=="fwd":
|
||||||
self.moveFwd()
|
self.moveFwd()
|
||||||
|
break
|
||||||
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