MV/MZ Smarter pathfinding plugin

Following up on this thread, I've begun work on a plugin to improve customizability of the pathfinding. Currently I'm focused on the pathfinding for touch/mouse movement, but it could theoretically be used for events too.

I currently have the following customizations working:

  • Easier for other plugins to substitute in custom behaviour
  • Avoid damage floors unless there's no other path
  • Avoid zigzagging on diagonal stairs using Cae_SlopeMove
  • Avoid map transfer events unless you click directly on them
  • Better pathing around complex obstacles (by increasing the search limit)

I'm currently working on detection of "teleporters", which are map transfer events that don't change the current map. This is proving to be especially hard, because I wanted to include ones placed on otherwise impassable spaces.

I've attached the current version, if anyone wants to play around with it. It's still a bit rough, so I'd prefer if you don't use it in a real project yet.

If anyone can think of other improvements for touch/mouse movement, feel free to say mention them. I'm sure there are quite a few things I haven't thought of.
I have teleporters working now. If you click on a space that can only be reached by a teleporter, the player character will navigate to the teleporter entrance. It unfortunately still stops after teleporting, however; this is because pathfinding always stops if an event is triggered.

Other things I'm contemplating:

  • An option to tag events as not interrupting pathfinding (so that the navigation will continue after the event has completed). This would be good for events that are supposed to just silently do something, like flipping a switch to toggle between bridge overpass and underpass. It could also be used for simple flavour text events, I suppose.
  • An option to tag events as damaging or obstacles, for the purposes of pathfinding. This would, for example, mean that spike events can be avoided just the same as damage floor would be. Tagging as obstacles would mean you can have an event that you can walk over, but it won't choose to step on it when pathfinding. (Not sure if that's useful.)
  • Some way of understanding bridges. I think this might be extremely difficult though and not worth the effort. I think there are at least 3 ways bridges can be created – an event that flips between Below and Above Characters based on a switch; a tileset swap that flips certain tiles between star and regular passability; or the OverpassTile official plugin.
  • An option to avoid tiles based on region ID or terrain tags. I'm including terrain tags here because I used them to create state-afflicting terrains in one of my unreleased plugins. This would be a per-map (or maybe even per-tileset) option rather than a global option.
It unfortunately still stops after teleporting, however; this is because pathfinding always stops if an event is triggered.
this is true, but if you use Mac15001900 RunNearbyEvents, than you can set a tag
to avoid this if there is an event.

I use this as well for a different mechanic if you click, the sensor stops movement,
but with the plugin linked and a tag you specified let the player walking on
player touch.

so you can see if this works, as if it does, you may include this or let them know
so the player can keep walking 🙂.

Nice plugin though for smarter pathfinding.
this is true, but if you use Mac15001900 RunNearbyEvents, than you can set a tag
to avoid this if there is an event.
Really? I don't see anything like that in that plugin – <NoTrigger> means an event can't be triggered at all, not that it'll trigger but then let pathfinding continue…

That said, the plugin looks very useful, as I was recently struggling with getting something similar to work in my project for events whose images are larger than one tile.
this parameter: Force passthrough tag
let the player keep walking, as player touch (or sensor) stops the player
and run the event.

so if you have tinting and have a conditional branch => player stops
but with that tag, it keeps working.

I use it with regions for tinting in this case.

so if you have a teleport => player touch => teleport => it may continue to walk
or dash afterwards.

as teleport are events, its worth a shot 🙂
Oddly enough, it both works and doesn't. That is to say, I have two almost identical events, and <NoStop> works on one but not the other.
if its back and forth, than its weird.
so if small distance isn't enough or not triggered,
it may or may not be a bug in the plugin.

I do use it on some multiple event with similar conditions.
path finding might be different in this case.

if same maps works on teleport forwards but not backwards,
maybe something is interfering.

I don't get the code really when looking through so I cannot really
help on it or how it calculates x/y from tiles and such.

keyboard part can work, mouse and touch is different on navigating,
but if you are able to figure it out to make it work nicely, you get a
really nice smart path finding for mouse/touch support 🙂

so keep up the good work, hopefully you can get it to work or with
some help to link them clean =)
I figured out why it worked on one event but not the other. The event that works is Below Characters, and the event that doesn't is Same as Characters. From a player's point of view, you can walk into both of them; but the game considers the one impassable, and so Mac's plugin clears the destination when interacting with it even if <NoStop> is present.
I hadn't that problem, but if it does, it may be a reason, so you can
work around it if it helps improve touch/mouse support in some way.

I might or may not use the plugin, but it is definitely interesting though.
specially if they use mouse for movement and/or navigating the menu.

I keep an eye out, hope you can make it work smoothly =)
An option to avoid tiles based on region ID or terrain tags. I'm including terrain tags here because I used them to create state-afflicting terrains in one of my unreleased plugins. This would be a per-map (or maybe even per-tileset) option rather than a global option.
This is working now, along with parameters to customize the other avoidance features. I also added avoidance of ladders (because it was trivial) but left it disabled by default.

I'm now trying to find a way to handle pathing both under and over bridges.
Some way of understanding bridges. I think this might be extremely difficult though and not worth the effort. I think there are at least 3 ways bridges can be created – an event that flips between Below and Above Characters based on a switch; a tileset swap that flips certain tiles between star and regular passability; or the OverpassTile official plugin.
This is now working! I had to adjust the pathfinding algorithm to add a 3rd coordinate, since it used the coordinates as a key to identify the tile, but it works perfectly on one of my own maps that has a lot of bridge events (as long as the search limit is increased), and I also tested it with tileset swaps and the OverpassTile plugin – that seems to work as well.

I think I'd say this current version is ready to be tried out in real projects, if people want to experiment with it. Consider it to have the same terms as my miscellaneous plugin thread, with the exception that I'd really appreciate being credited for this one, as it was somewhat tricky. I'll probably either add it to that thread later, or open a dedicated thread for it in the plugin releases forum (not sure which yet).

I haven't yet gotten around to testing it in MZ. I'll do that eventually, but if someone gets to it before me, feel free to let me know how it goes.
I've made a few more changes. The big ones are:

  • Separate search limit for the airship. A high search limit causes lags on wide open maps, but to the airship, all maps are wide open, so it should generally use a lower limit than other things.
  • Customize search limit on specific maps, or on all maps using a specific tileset. If most of your maps are wide open but you have three maps that are maze-like, you may want to use a much higher limit on just those three maps.
  • When you click on a moving event, the pathfinding can now dynamically track that event, rather than just pathing to the location it was on when you clicked it.
  • The calculation of path costs for a tile has been changed to sum up all relevant factors, rather than using an arbitrary priority system to select one factor.

There may be a few more that I've forgotten. I haven't yet tested it in MZ however.
I've made a few more changes. The big ones are:

  • Separate search limit for the airship. A high search limit causes lags on wide open maps, but to the airship, all maps are wide open, so it should generally use a lower limit than other things.
  • Customize search limit on specific maps, or on all maps using a specific tileset. If most of your maps are wide open but you have three maps that are maze-like, you may want to use a much higher limit on just those three maps.
  • When you click on a moving event, the pathfinding can now dynamically track that event, rather than just pathing to the location it was on when you clicked it.
  • The calculation of path costs for a tile has been changed to sum up all relevant factors, rather than using an arbitrary priority system to select one factor.

There may be a few more that I've forgotten. I haven't yet tested it in MZ however.
May I use this for a GameJam and credit you? Just asking for safety. Thanks!
Go for it!

Here's the latest version. Feel free to use either this or the previous version, but this version does have a lot of performance improvements.

Rough changelog:

  • Support setting directional path costs on events (eg, it can cost more to enter from above than from below, or you could have one-way passages – IIRC I used it for a slippery stairway where you can go down but not up)
  • Improve performance by caching the path found, instead of recalculating it with every step. It still recalculates it if it finds the next step to be impossible (which could happen, for example, because another moving event happened to get in the way, or because you triggered a PathThru event that altered another tile's passability).
  • The mechanism to track moving events now tries very hard not to trigger on events that don't move, including those that have an autonomous move route but don't have actual move commands in the route. It counts script commands as movement (because they could be anything and there's no easy way to tell).
  • Support for the MZ version of the OverpassTile plugin. This is untested though.
  • Some tweaks to make bridge detection work for events – that is to say, if you use the pathfinding for an event (rather than the player), they can now cross bridges.
Go for it!

Here's the latest version. Feel free to use either this or the previous version, but this version does have a lot of performance improvements.

Rough changelog:

  • Support setting directional path costs on events (eg, it can cost more to enter from above than from below, or you could have one-way passages – IIRC I used it for a slippery stairway where you can go down but not up)
  • Improve performance by caching the path found, instead of recalculating it with every step. It still recalculates it if it finds the next step to be impossible (which could happen, for example, because another moving event happened to get in the way, or because you triggered a PathThru event that altered another tile's passability).
  • The mechanism to track moving events now tries very hard not to trigger on events that don't move, including those that have an autonomous move route but don't have actual move commands in the route. It counts script commands as movement (because they could be anything and there's no easy way to tell).
  • Support for the MZ version of the OverpassTile plugin. This is untested though.
  • Some tweaks to make bridge detection work for events – that is to say, if you use the pathfinding for an event (rather than the player), they can now cross bridges.
Thanks!... Now I just gotta figure out how to put it on an enemy to chase the player around a giant map lol
The basic way would be to put something like this in a move route script:

JavaScript:
this.moveStraight(this.findDirectionTo($gamePlayer.x, $gamePlayer.y))

Or you could use a plugin that provides commands to do it a bit more easily, like Shaz's pathfinding plugin.
The basic way would be to put something like this in a move route script:

JavaScript:
this.moveStraight(this.findDirectionTo($gamePlayer.x, $gamePlayer.y))

Or you could use a plugin that provides commands to do it a bit more easily, like Shaz's pathfinding plugin.
The thing is, I already tried that and even separate values but the enemy just randomly stutters and stops near the player a lot of the time for whatever reason. I guess my map is just "too big" so it breaks automatically.
If it's happening when the enemy is near the player, then I doubt it's related to the size of the map. Can you be more clear about what you mean by "randomly stutters and stops"?
If it's happening when the enemy is near the player, then I doubt it's related to the size of the map. Can you be more clear about what you mean by "randomly stutters and stops"?
I have two versions, one with autorun on a separate Event and one that triggers when the player steps on a field:


I used TheOdie's script with $gamePlayer coords and once with separate values:

//This somewhat solves the "getting stuck in place" part but makes the enemies hug the walls
$gameMap._events[this._eventId].moveStraight($gameMap._events[this._eventId].findDirectionTo( $gamePlayer.x,$gamePlayer.y ));

//This made the enemies slightly smarter but they tend to stop randomly right next to the player
$gameMap._events[this._eventId].moveStraight($gameMap._events[this._eventId].findDirectionTo( $gameVariables.value(213),$gameVariables.value(214) ));

I hope the screenshots and videos suffice. Thank you!
Assuming you're putting this in a Move Route script command, there is no reason to do this:

JavaScript:
$gameMap._events[this._eventId]

It is completely equivalent to just this, which is shorter. Even if for whatever reason you can't put it in a Move Route script command and must put it in a regular script command, the following is again 100% equivalent and also shorter:

JavaScript:
this.character(0)

That wouldn't be the cause of your issue though (since, as I said, both forms are 100% equivalent). Note that the shortened forms are context-sensitive though – one only works in Move Route scripts and the other only works in event Script calls.

Okay, now I'll try to describe what I think are the issues you're seeing, and you can confirm if I got it right.

In the first video, there are two occasions where I see the event is about to touch you but then you back away and the event in turn reverses direction as well. Also, there is one place where it looks like it got stuck at a corner, trying to go down (into the wall) when the correct choice would have been to go left. I'm not quite sure what would cause it to get stuck, unless there's a bug in the passability settings. Reversing direction could happen if it thought the route that appears longer is actually shorter.

In the second video, it appears that the event stops before reaching its goal on several occasions. In other words, it simply won't progress any further until the player moves again. This seems like it could be a bug in the system that caches the path to avoid expensive recomputation on every step – that cache is automatically cleared when the destination changes, so the fact that it comes unstuck when the player moves is a pretty good sign that this is the cause.

Does that seem correct? I see 3 separate issues – getting stuck at a corner, reversing direction when you back away, and stopping randomly. The first and third might be the same, but they might not be.

For the issue of seemingly choosing a "longer" path than optimal, I'd suggest checking a few things:
  • What's the search limit? Did you change it or leave it at the default? This isn't that complicated of a map, so I wouldn't expect problems with the default search limit, but it's possible it could be related.
  • Can you reproduce similar issues by moving the player with the mouse? Like, click in various locations and see whether the player chooses the shortest path to get there.
  • Are there any invisible events on the map? If so, be sure to set them to THROUGH so the enemy can pass over them.

Original thread: https://forums.rpgmakerweb.com/threads/smarter-pathfinding-plugin.180004/