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.queue=[]
|
||||
self.goalchest=False
|
||||
self.step_counter=0
|
||||
|
||||
def rotate(self, clockwise=True):
|
||||
if clockwise:
|
||||
@ -37,7 +38,7 @@ class Player(Creature):
|
||||
self.pos[1] + self.directions[self.direction][1])
|
||||
if new_position in possible_steps:
|
||||
self.model.grid.move_agent(self, new_position)
|
||||
|
||||
print("moved to ", new_position)
|
||||
# def move(self): # OLD
|
||||
# possible_steps = self.model.grid.get_neighborhood(
|
||||
# self.pos,
|
||||
@ -195,85 +196,108 @@ class Player(Creature):
|
||||
|
||||
# pole to element wziety z kolejki path
|
||||
|
||||
def actionPlanner(self, cell):
|
||||
x0 = self.pos[0]
|
||||
y0 = self.pos[1]
|
||||
x = cell[0]
|
||||
y = cell[1]
|
||||
def actionPlanner(self, cell_from, cell_to):
|
||||
x0 = cell_from[0]
|
||||
y0 = cell_from[1]
|
||||
x = cell_to[0]
|
||||
y = cell_to[1]
|
||||
if (x > x0):
|
||||
if (self.direction == 1):
|
||||
self.queue.append("fwd")
|
||||
elif (self.direction == 0):
|
||||
self.queue.append("tr")
|
||||
self.rotate(True)
|
||||
self.queue.append("fwd")
|
||||
elif (self.direction == 2):
|
||||
self.queue.append("tl")
|
||||
self.rotate(False)
|
||||
self.queue.append("fwd")
|
||||
else:
|
||||
self.queue.append("tr")
|
||||
self.rotate(True)
|
||||
self.queue.append("tr")
|
||||
self.rotate(True)
|
||||
self.queue.insert("fwd")
|
||||
elif (x < x0):
|
||||
if (self.direction == 3):
|
||||
self.queue.append("fwd")
|
||||
elif (self.direction == 0):
|
||||
self.queue.append("tl")
|
||||
self.rotate(False)
|
||||
self.queue.append("fwd")
|
||||
elif (self.direction == 2):
|
||||
self.queue.append("tr")
|
||||
self.rotate(True)
|
||||
self.queue.append("fwd")
|
||||
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 == 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")
|
||||
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:
|
||||
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):
|
||||
self.queue.append("fwd")
|
||||
elif (self.direction == 3):
|
||||
self.queue.append("tl")
|
||||
self.rotate(False)
|
||||
self.queue.append("fwd")
|
||||
elif (self.direction == 1):
|
||||
self.queue.append("tr")
|
||||
self.rotate(True)
|
||||
self.queue.append("fwd")
|
||||
else:
|
||||
self.queue.append("tr")
|
||||
self.rotate(True)
|
||||
self.queue.append("tr")
|
||||
self.rotate(True)
|
||||
self.queue.append("fwd")
|
||||
# if (len(self.path)==0):
|
||||
# self.queue.append("op")
|
||||
elif (len(self.path)==0):
|
||||
self.queue.append("op")
|
||||
|
||||
def step(self):
|
||||
self.step_counter+=1
|
||||
if self.health > 0:
|
||||
# self.moveFwd()
|
||||
# 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ć
|
||||
self.path=self.findShortestPathToTarget()
|
||||
print("the player should follow this path:",self.path)
|
||||
for cell in self.path: #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ść
|
||||
print("the player should follow this path:", self.path)
|
||||
for i in range(len(self.path)-1): #iteruje po kolejnych krotkach ze współrzędnymi pól
|
||||
#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)
|
||||
if len(self.queue)!=0:
|
||||
while len(self.queue)!=0:
|
||||
self.action=self.queue.pop(0)
|
||||
if self.action=="tr":
|
||||
self.rotate(True)
|
||||
print("tr'd, direction: ", self.direction)
|
||||
break
|
||||
elif self.action=="tl":
|
||||
self.rotate(False)
|
||||
print("tl'd, direction: ", self.direction)
|
||||
break
|
||||
elif self.action=="fwd":
|
||||
self.moveFwd()
|
||||
break
|
||||
cellmates = self.model.grid.get_cell_list_contents([self.pos])
|
||||
if len(cellmates) > 1:
|
||||
if isinstance(cellmates[0], Box):
|
||||
|
Loading…
Reference in New Issue
Block a user