Update A_star so that it generates some movements (not always correct)

This commit is contained in:
Michał Czekański 2020-04-26 13:47:22 +02:00
parent deeba7c740
commit 5facbfb2fd

View File

@ -35,7 +35,7 @@ class AutomaticMovement:
def updatePlayerCoords(self): def updatePlayerCoords(self):
if self.actualTarget is not None: if self.actualTarget is not None:
self.player.move(self.nextMove) self.player.move(self.nextMove)
self.movesList.remove(0) self.movesList.remove(self.nextMove)
if len(self.movesList) != 0: if len(self.movesList) != 0:
self.nextMove = self.movesList[0] self.nextMove = self.movesList[0]
else: else:
@ -67,21 +67,15 @@ class AutomaticMovement:
print("PATH NOT FOUND") print("PATH NOT FOUND")
return None return None
elem: AStarNode = fringe.get()[2] elem: AStarNode = fringe.get()[2]
print("CURRENT STATE: {}, ACTION: {}".format(elem.state, elem.action))
for (pr, count, node) in fringe.queue:
print("PR: {}, ST: {}, AC: {}".format(pr, node.state, node.action))
if self.goalTest(elem.state): if self.goalTest(elem.state):
movesList = [] movesList = []
p = elem.parent while elem.action is not None:
while p is not None:
movesList.append(elem.action) movesList.append(elem.action)
p = elem.parent elem = elem.parent
movesList.reverse()
return movesList return movesList
explored.append(elem) explored.append(elem)
@ -90,11 +84,6 @@ class AutomaticMovement:
# Sprawdzam czy nie jest poza mapa # Sprawdzam czy nie jest poza mapa
coordsWithUiOffset = [newState[0] + self.leftUiWidth, newState[1]] coordsWithUiOffset = [newState[0] + self.leftUiWidth, newState[1]]
# DEBUG
if movement == Movement.FORWARD:
a = 1
a+=1
if 0 <= newState[0] <= self.map.width - self.moveOffset and 0 <= newState[1] <= self.map.height - self.moveOffset: if 0 <= newState[0] <= self.map.width - self.moveOffset and 0 <= newState[1] <= self.map.height - self.moveOffset:
#if self.map.getTileOnCoord(coordsWithUiOffset) is not None: #if self.map.getTileOnCoord(coordsWithUiOffset) is not None:
newNode = AStarNode(elem, movement, newState) newNode = AStarNode(elem, movement, newState)
@ -104,9 +93,6 @@ class AutomaticMovement:
if not any(newNode.state == node[2].state for node in fringe.queue) \ if not any(newNode.state == node[2].state for node in fringe.queue) \
and not any(newNode.state == node.state for node in explored): and not any(newNode.state == node.state for node in explored):
# there can't be nodes with same priority # there can't be nodes with same priority
'''
while any(newPriority == item[0] for item in fringe.queue):
newPriority -= 1'''
fringe.put((newPriority, self.testCount, newNode)) fringe.put((newPriority, self.testCount, newNode))
self.testCount += 1 self.testCount += 1
# If state is in fringe queue ... # If state is in fringe queue ...
@ -134,7 +120,8 @@ class AutomaticMovement:
stateAfterForward = self.newStateAfterAction(elemState, Movement.FORWARD) stateAfterForward = self.newStateAfterAction(elemState, Movement.FORWARD)
if 0 <= stateAfterForward[0] <= self.map.width - self.moveOffset and 0 <= stateAfterForward[1] <= self.map.height - self.moveOffset: if 0 <= stateAfterForward[0] <= self.map.width - self.moveOffset and 0 <= stateAfterForward[1] <= self.map.height - self.moveOffset:
coordsWithUiOffset = [stateAfterForward[0] + self.leftUiWidth, stateAfterForward[1]] coordsWithUiOffset = [stateAfterForward[0] + self.leftUiWidth, stateAfterForward[1]]
facingEntity = self.map.getEntityOnCoord(coordsWithUiOffset) #facingEntity = self.map.getEntityOnCoord(coordsWithUiOffset) # TU JEST BLAD BO U CELU JEST ZAWSZE JAKIES ENTITY
facingEntity = None
if facingEntity is None: if facingEntity is None:
result.append((Movement.FORWARD, stateAfterForward)) result.append((Movement.FORWARD, stateAfterForward))