Edern76 / DementiaRL

A WIP roguelike written with Python3/TDL set in a dark-fantasy universe.
Other
8 stars 3 forks source link

Astar pathfinding not working correctly #34

Closed Edern76 closed 7 years ago

Edern76 commented 7 years ago

Astar only works when the monster is adjacent to the player (some rare exceptions), and even then it only has a random chance to actually work.

HexDecimal commented 7 years ago

Are you still having issues with this? I didn't get any of the debug path not found errors when I test your latest commits.

Edern76 commented 7 years ago

Yes, it works almost perfectly when the monster is in the same room as the player, but when the player is in a tunnel and the monster is in a corner at the exit of this tunnel, the monster just stands in place there (I'll upload a gif when I have access to my computer).

HexDecimal commented 7 years ago

I noticed that, but it seemed to be related to the monster having visibility on the player, rather than a path-finding issue. Whenever it sees the player it generates a new path, saves it, and follows it one step. When the player is out of view it does none of these things and stands still.

Perhaps have the monster run this code if it has done nothing else on its turn:

# add to GameObject class
def moveNextStepOnPath(self):
    """Move the next step on a path generated by moveAstar, if it can."""
    if self.astarPath: # if self.astarPath is non-empty
        x, y = self.astarPath.pop(0) # take the next step, removing it from the list
        if isBlocked(x, y):
            self.astarPath = [] # delete the path on an obstacle
        else:
            self.x, self.y = x, y

You might also want to have moveAstar call this method immediately after generating the path, replacing a lot of its own logic.

Don't forget to add something like self.astarPath = [] to __init__

Edern76 commented 7 years ago

Thanks a lot ! I'll try adding this code and adding monsters' LOS as soon as I get back home.

Edern76 commented 7 years ago

I did as you said, and it actually improved pathfinding a lot ! After making them move to the next step in their paths, they managed to move around corners when they were at a certain distance of the player. What I did in order to allow it to work at close range was to add a check for diagonals, if a diagonal tile isn't blocking movement and moving there makes the monster Y or X coordinate to be the same as the player, then the monster moves there. Movement is still sometimes a little bit wonky, but that's not a very big issue.

Thanks a lot for your help ! We'll make sure to include you (and the /r/roguelikedev community as a whole) in the special thanks section of the credits :)

@Malan-Tai and @lasTH76, if you have no objections I'll close (at last !) this issue.