MV Pathfinding or not pathfinding

I'm not looking strictly for pathfinding plugins, because on map design these plugins don't work (I have checked most respectable plugins by @Shaz SmartPathfinding plugin and Yanfly's Move Route Core and they behave the same way).

So I thought I approach problem from other side, wich is a plugin that will allow me to move it on a path drawn with regions something like that:

2021-08-06 18_53_40-Window.png


Event starts in Region 1, event is moving to region 10, but always stays on path made from region 5.

Is there such plugin? Because I spend few hours on google and i found nothing reasonable.
(Region 1 and region 10 represents event moving toward another event (1 - source, 10 - target)
Hi there!
But your goal is to make the event go to region 10, but strictly following region 5 only?
OR
is to make the event go to region 10, and since you are not being able to do that with any pathfinding plugin, do you have the idea to mark the path with region 5?

Another way of doing that is using a tool that can generate the move routes command to follow a path:
Visual Move Routes by Nebula Games
Yeah, mine won't work for something like your image as it's based on the default MV player pathfinding logic, which only looks within about a 12-tile range.

I've thought about a plugin for event movement by using regions, so if you can't find something and the above doesn't help, I can probably make one for you.
If you're comfortable writing your own js, this pseudocode should work.

Code:
//this gets called in a move route on repeat

Game_CharacterBase.prototype.followPath(regionNumber) = function()
{
//cache where we were
if (!this.lastTileOnPath){
this.lastTileOnPath = [this.x,this.y]
}

var maxDistance=0;
var nextDirection=-1;
//for loop - > check all 4 spaces around the character horizontally
{
if ( // tile region is the same as the input regionNumber AND this is not the same tile as lastTileOnPath)
   {
   var currDistance = //distance between this.lastTileOnPath and the current xy coordinates of the tile
   if (currDistance>maxDistance){
      // if this gets us furthest from our origin point.
      maxDistance=currDistance;   
      nextDirection = //current direction of the tile we're looking at
      // note that in case of a tie this will favor whichever direction is selected first by your algorithm
      }
   }
}
// okay at this point we've checked through, and we've found whichever tile is most distant from the tile we entered from.
//for instance if we are in a situation like this, where we came from X, are at O, and can pick between A and B
//[ ] [X] [ ]
//[ ] [o] [A]
//[ ] [B] [ ]
//we will always pick B

if (nextDirection==-1)
{
  // we don't have any valid locations.  That means we're at the end of the path.
  //set a self switch or something to break out of this repeated move route.
}else{
   //we have our next target.
   //save our current position
   this.lastTileOnPath = [this.x,this.y]

   //fire off a move command to go there
   this.whateverTheMoveFunctionIs(nextDirection)
}

}

This algorithm is fairly limited (it basically will only follow lines), but it should handle slightly complicated cases like doing a figure 8.
I've been thinking about this all day.
I have thought this would be a neat plugin to have.

This might work for me, maybe it will work for you.
I tried to make it look professional, like all the other plugin makers out there.

I can probably make one for you.
(But I'd rather have something from Shaz 🙂)



Here is what it would look like from the event page you would have to make.
1628322180168.png


And here is the plugin code you would have to add to your plugins folder. (Half of it is comments)

EDIT: Note, I have updated the code to fix an issue of the event returning to its starting position. Find it on GitHub at https://github.com/anautodidact/RPG-MV-public-plugins/blob/main/sal_routeMovement.js just make sure to click on the "Raw" link, then right click and select "Save Page As".

JavaScript:
//=================
//sal_routeMovement.js
//=================
/*:
 * @plugindesc (v.1.0) An experimental plugin to move event along region ID
 *
 * @author autodidact
 *
 * @help
 * Place _sal.routeMovement.moveAlongRegion(this, [self-switch])
 * into the Set Movement Route command and change the target to this event.
 *
 * Moving events to a certain x,y destination can be difficult.
 * But using region Ids to define a route may make it a little easier.
 *
 * This script is very simple with only one script command.
 * First, define your region id path for an event to walk along.
 * Second, put your event directly ON the starting region tile that
 * starts the path.
 * Third, put
 * _sal.routeMovement.moveAlongRegion(this, [self-switch])
 * into a set movement route. The self switch is optional.
 *
 * Using no self-switch on a movement route with an event trigger of
 * "Action Button" will allow the event to follow the route back to
 * its original starting pointing.
 *
 * Add the self switch to the script call to remove further movement
 * by region by switching to a different event page. You will have to
 * create an event page with the self switch check.
 *
 * TERMS OF USE
 * This plugin is currently free for all use.
 * If you happen to find this useful, feel free to credit autodidact.
 * And I would love to hear about your game :)
 *
 * COMPATIBILITY
 * No known issues.
 */

var _sal = _sal || {};
_sal.routeMovement = {};


(function($){

//No parameters
//var parameters = PluginManager.parameters("sal_routeMovement");


_sal.routeMovement.moveAlongRegion = function(thisevent, selfswitch) {
let down = 2;
let right = 6;
let left = 4;
let up = 8;

let currentevent = thisevent;
let thisX = currentevent.x; console.log(thisX);
let thisY = currentevent.y; console.log(thisY);
let thisR = $gameMap.regionId(thisX, thisY); console.log(thisR);
//this.lastPos = [thisX, thisY]; //Not implementing this right now.

//Note, rather than keeping and checking a varialbe for the last location, my check code determines that the region event has started in does exist in a direction, and that it will not walk backwards.
//Seems to work.

  if (thisR == $gameMap.regionId(thisX, thisY+1) && currentevent._direction != 8) { //check if the same region is down
    console.log("movedown");
    currentevent.setDirection(2); //turn down
        currentevent.moveStraight(2); //move down
  }
  else if (thisR == $gameMap.regionId(thisX, thisY-1) && currentevent._direction != 2) { //check if the same region is up
    currentevent.setDirection(8); //turn up
        currentevent.moveStraight(8); //move up
  }
  else if (thisR == $gameMap.regionId(thisX-1, thisY) && currentevent._direction != 6) { //check if the same region is left
    currentevent.setDirection(4); //turn left
        currentevent.moveStraight(4); //move left
  }
  else if (thisR == $gameMap.regionId(thisX+1, thisY) && currentevent._direction != 4) { //check if the same region is right //
    currentevent.setDirection(6); //turn right
        currentevent.moveStraight(6); //move right
  }
  else {
    if (selfswitch) {
      selfswitch = selfswitch.toUpperCase();

      if (selfswitch == 'A' || selfswitch == 'B' || selfswitch == 'C' || selfswitch == 'D') {
        var key = [$gameMap._mapId, thisevent._eventId, selfswitch];
        $gameSelfSwitches.setValue(key, true);
      }
    }
  }

}

//this function is for easier script call from eventing page.
_sal.checkRegion = _sal.routeMovement.moveAlongRegion;

}(_sal));

It works for the below example. (Ignore the door event). NOTE: The event starts on the region.

But unlike restart's code, it probably won't work for a figure 8. It also wouldn't account for branching options or for different event paths to cross paths.
1628322036522.png
@Eliaquim, Yes, event must strictly move on path drawn from specific region (given as function parameter (regionId), or plugin parameter, it doesn't matter at all)

@Shaz, I would gladly use such plugin.

@autodidact I will check your plugin and see how it works. I expect some issues since I don't know you well as rest of members and might have some compability issues with Yanfly's library and Srd's (SumRnddude). I give edit if no one replies within an hour, or post results if someone's reply.

@Restart If I will get some free time and @autodidact code fails I'll try to write something on my own 🙂

Thank you all for your time and apprieciate your help.

Here's my post edit:
@autodidact - plugin works perfectly and I'm stunned it's so simple plugin. Now making infamous escort misions will be far easier to make 🙂
Yay, I am glad it works for you!
Honestly, I haven't fully tested it.

But it doesn't overwrite any code, so I suspect it should be fully compatible with Yanfly's library and SRD's. But again, I haven't tested much.
Let me know if you have any issues.
So far I noticed few things:

1. When event colides with either player or another event, the walking event returns to starting position (easy to overcome - setting "through" on solves problem.)

2. When walking event have speed set to float number (like e.g. 2.86) not flat number ( 3 ) it's walking looks like the NPC is limp (or it's just my ty hardware?? I need to do more visual testings to be 100% sure)

3. Event always takes the shortest route so in this example (at circled point) event will turn his left. Ignoring path straightforward
(Of course it's also easy to overcome by eventing - detect X and Y and do some stuff - i don't know... NPC will ask protagonist where to go now.)
2021-08-07 16_50_32-Window.png


But anyway I give solid 9.5/10 - at first I was a little confused where in plugin is variable to set region ID where event can walk, but then realised your plugin takes any region as valid region to walk.
Oh, no 3 separate problems. (These reasons are why we need a skilled person like @Shaz.)

Issue 1) Looks like I'll need to implement @Restart's psuedocode to check the last tile on path and not return to it.

Issue 2) Hm, I can't replicate issue number 2. It looks fine from my empty project. Can you try an empty project? Perhaps it is a conflict?

Issue 3) Yes, I noted it doesn't currently work for branching paths.
What is your use case? Why would an NPC need to have two paths? Are they the same path for two different NPCs? Will the NPC have to return to the branch and continue along the alternate route? The code will need to get more complicated to handle this, but knowing the use case might help program it effectively.

I'll try working on issue 1 today and see if I can update the code I previously posted.
I'll think about issue 3 and how it can be resolved.
For the first two issues you can scratch them - I did some tricks to hide that problems - collisions, I set event's Through option enabled and for limp movement I set default flat value, replacing escort quest for chasing quest.

For the 3rd issue, why event need branched road, when encounters another event (mob to fight) will have to avoid it, or in fabular case.
(For example event says and player have two choices:
- Road is splitting, where do you wanna go?
* Left
* Right
)

To be honest it's not that extremly necessary to my project since it's pretty linear, but I found these issues while I was just messing around.

Look - the map is quite big, but walkable path is pretty narrow - there is not enough space to make decent eventing npc looking for alternatives routes, and most of my maps are such designed - big landscapes, narrow paths (no more than 3 grids width, with few exceptions, but in their case scenario kicks in, altering usage of wide passages)
This is the first plugin I have ever tried to make for someone else, so thank you for your patience.
I'm having a strange problem getting my last tile check to work right.
I have two ideas on the path alternate branches problem.


Regarding path branching, (this only applies to player path choosing)
If you want to work with the current script, you can use branches at the cost of a self-switch and an event page. See below images.

In this picture, there are two possible destinations: A town and a castle.
1628374616320.png


The first region the event travels along stops at the campfire. To do that run this code, which switches self-switch "B" at end of route. (image demonstrates)
1628374585693.png


To manage the path branch, use the code below. (Note, you must use a second "Set Movement Route" to place the event back onto the region.)
1628374673359.png


Anyways, just wanted to mention you don't have to check for x and y.
Hopefully I'll have something much better later.
I was really hoping someone else would write something awesome, but it looks like its just me. :0
That's okay, I get to exercise some muscles.

Here is an update to the code. https://github.com/anautodidact/RPG-MV-public-plugins/blob/main/sal_routeMovement.js
This fixes issue #1. The event will now halt movement when it gets to an event that is blocking its path instead of returning to its starting point (Or you can set Through to On to walk through all other events).

I also refactored the code. It looks like real programming code, hard to read and understand 🙂
Hopefully this gets me a few steps closer to 1) doing user defined actions when event is blocked 2) managing event movement branches.

But since I already provided a possible scenario for branching, I am going to let this issue rest a little.

If there is any trouble with the current code, please let me know.
@autodidact Right now there's really heavy storm comming directly onto my house so I need to secure PC and other electronic devices so I'll be testing it tomorrow and I let you know about results, right now all I can say, Thank you for your time and effort. If I ever could help you, then let me know 🙂

Edit: It's ok now. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/pathfinding-or-not-pathfinding.139320/