Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: jumpjack on Wed 04/01/2023 10:34:04

Title: How to prevent character from getting stuck?
Post by: jumpjack on Wed 04/01/2023 10:34:04
Sometimes it happens that my character moves to a location where it cannot move away anymore, nor by mouse neither by keyboard. Apparently if walks to a nonwalkable area, and it can't get out anymore.
I don't know how it can get there, but once it happens, how can I unlock it? How can I "undo" the character to previous position upon detecting no further possible moves?

Notes:
- V. 3.6.0 RC4
- character is not at room edge (https://github.com/adventuregamestudio/ags/blob/2d43bffc2cb07935ae72d7c5677ad622c8b4d37e/Changes.txt#L2049)
-  I am not using "adjust speed" or "scaling" (https://github.com/adventuregamestudio/ags/blob/2d43bffc2cb07935ae72d7c5677ad622c8b4d37e/Changes.txt#L3350)
- room is static, not scrolling


edit:
I don't know if this can help, anyway I logged these positions before character getting stuck:
(w and w are walkable area in current screen/room position)

2023/01/04 12:09:21 - x,y,w,w = 164,83,1,1
2023/01/04 12:09:21 - x,y,w,w = 168,85,1,1
2023/01/04 12:09:21 - x,y,w,w = 171,86,1,1
2023/01/04 12:09:21 - x,y,w,w = 174,85,1,1
2023/01/04 12:09:21 - x,y,w,w = 178,83,1,1
2023/01/04 12:09:21 - x,y,w,w = 175,80,0,0

Here I even detected a move inside a non-walkable area:

2023/01/04 12:09:21 - x,y,w,w = 170,86,1,1
2023/01/04 12:09:21 - x,y,w,w = 173,82,1,1
2023/01/04 12:09:21 - x,y,w,w = 177,80,1,1
2023/01/04 12:09:21 - x,y,w,w = 178,79,1,1
2023/01/04 12:09:21 - x,y,w,w = 175,80,0,0
2023/01/04 12:09:21 - x,y,w,w = 171,82,0,0

I implemented this "emergency function" which brings the character back to last valid position while saving debug data previously logged:

   if (GetWalkableAreaAtRoom(  player.x,  player.y) == 0) {
    for (int index=0; index < 1000; index++) {
      //debugPrint(String.Format("x,y,w,w = %d,%d,%d,%d",  prevXarr[index],  prevYarr[index],  prevWarr[index],  prevW2arr[index]), false);
      if (prevWarr[index] !=0) {
        player.x = prevXarr[index];
        player.y = prevYarr[index];
      }
    }
  }

I put it inside repeatedly_execute() in global script: given that in no case the character is allowed to be into a nonwalkable position, as soon this condition is detected, the position is brought back to last valid one. I don't know if this is the best method, or if it would be better to just call cEgo.PlaceOnWalkableArea() upon detecting character on forbidden position, but I wanted to log the positions which led to the wrong position.
Title: Re: How to prevent character from getting stuck?
Post by: Khris on Wed 04/01/2023 14:47:18
The pathfinding algorithm shouldn't move the character outside a walkable area, ever.
How are you moving the character exactly? Standard WalkTo clicks / keyboard movement only?
Title: Re: How to prevent character from getting stuck?
Post by: Crimson Wizard on Wed 04/01/2023 15:28:24
If this is what actually happens, then it of course should be considered an engine bug. I would separate solving the bug and workarounds; and I'm mostly interested in the bug details atm. There's a ticket opened about same problem: https://github.com/adventuregamestudio/ags/issues/1879
Ideally we'd need a walkable mask, and an example of starting position / Walk command arguments which causes character to stuck. This would help to reproduce and diagnose the problem.
Title: Re: How to prevent character from getting stuck?
Post by: jumpjack on Wed 04/01/2023 15:53:59
Quote from: Crimson Wizard on Wed 04/01/2023 15:28:24If this is what actually happens, then it of course should be considered an engine bug. I would separate solving the bug and workarounds; and I'm mostly interested in the bug details atm. There's a ticket opened about same problem: https://github.com/adventuregamestudio/ags/issues/1879
Ideally we'd need a walkable mask, and an example of starting position / Walk command arguments which causes character to stuck. This would help to reproduce and diagnose the problem.
Yes, that is MY ticket. :-)
I just attached there the game and the mask.
Title: Re: How to prevent character from getting stuck?
Post by: eri0o on Wed 04/01/2023 16:34:47
I see you are using that keyboard module that comes with the template. That control is quite unnatural when compared to more modern games. I think the controls from my Controlz (https://www.adventuregamestudio.co.uk/forums/index.php?topic=57427.0) module would work better for the isometric world, specially if your character is designed with 8 directions.
Title: Re: How to prevent character from getting stuck?
Post by: jumpjack on Thu 05/01/2023 09:00:28
Thanks. I already modified the original script, which moved character horizontally and vertically, but I needed diagonal moves.
My character has only 4 directions, unlike the one in the demo, which I am currently still using.
Title: Re: How to prevent character from getting stuck?
Post by: lafouine88 on Wed 22/01/2025 21:57:03
Hi guys

I found this old post since I'm facing the exact same issue as jumpjack. And I also followed the discussions here
https://github.com/adventuregamestudio/ags/issues/1879 (https://github.com/adventuregamestudio/ags/issues/1879)
and here
https://github.com/adventuregamestudio/ags/pull/2658 (https://github.com/adventuregamestudio/ags/pull/2658)

I see that the posts are very recent. I just don't understand it all since I find the github interface a bit confusing. From what I get, ivan-mogilko is working on a new fix for pathfinding ? Or did he post somewhere a little code to fix (there s a lot of mentionning of 'teleporting the player back to a walkable area if he gets out). I was thinking maybe @eri0o who seems to follow the topic, or @Crimson Wizard  could give me a heads up.

Thanks
Title: Re: How to prevent character from getting stuck?
Post by: Crimson Wizard on Wed 22/01/2025 21:59:46
@lafouine88 the fix was added in 3.6.2 Beta 6:
https://www.adventuregamestudio.co.uk/forums/ags-engine-editor-releases/ags-3-6-2-beta-1-a-wip-3-6-update/msg636668738/#msg636668738

It's not entirely perfect, as pathfinding remains the same, but it moves character to the nearest walkable pixel in case it ended up on a non-walkable one.

Note that this behavior has already been present for the regular Walk command, it was only WalkStraight that did not have it. So in effect this fix is for WalkStraight.
Title: Re: How to prevent character from getting stuck?
Post by: lafouine88 on Wed 22/01/2025 22:28:25
Great! Thanks! I'll check this ASAP  :grin: