If i am setting the route-move away from player, it usually runs in straight line and gets stuck either in the corner of the map, or behind the object. Is there a way around this, to make event look for escape patches instead of just going in opposite direction?
How to make event properly run from player?
RPG Maker MV Supportpathfinding is a somewhat complex matter and a topic for a lot of research.
as you already found out, the default pathfinding formula is not the best.
the problem is that more effective pathfinding requires a lot of computer power to calculate, and also depends on the map itself - a labyrinthine map will make pathfindin much more complex than an open map.
you can search for pathfinding plugins to try to improve that behaviour.
There have been several tries - but don't expect too much from them, because too complex pathfinding often creates lag.
as you already found out, the default pathfinding formula is not the best.
the problem is that more effective pathfinding requires a lot of computer power to calculate, and also depends on the map itself - a labyrinthine map will make pathfindin much more complex than an open map.
you can search for pathfinding plugins to try to improve that behaviour.
There have been several tries - but don't expect too much from them, because too complex pathfinding often creates lag.
MV's default pathfinding uses node-limited A* for pathing to a particular goal: it's a very common algorithm, nothing special. To path away, you could invert the heuristic. Maybe something like this:
Note that pathing away is generally more likely to hit the search limit than pathing toward, so I've halved the search limit here.
Move route Script examples (for the character to which the move route is assigned):
Edit - and to make it replace the existing "move away from character" method, you can try adding this to the bottom of the plugin code:
JavaScript:
/*:
* @target MV
* @plugindesc Defines method findDirectionAway for Game_Character.
* @author Caethyril
* @url https://forums.rpgmakerweb.com/threads/167088/
* @help Free to use and/or modify for any project, no credit required.
*/
// New - edited from findDirectionTo, rpg_objects.js.
Game_Character.prototype.findDirectionAway = function(rootX, rootY) {
var searchLimit = Math.ceil(this.searchLimit() / 2); // half search limit, rounded up
var mapWidth = $gameMap.width();
var nodeList = [];
var openList = [];
var closedList = [];
var start = {};
var best = start;
start.parent = null;
start.x = this.x;
start.y = this.y;
start.g = 0;
start.f = $gameMap.distance(start.x, start.y, rootX, rootY);
nodeList.push(start);
openList.push(start.y * mapWidth + start.x);
while (nodeList.length > 0) {
var bestIndex = 0;
for (var i = 0; i < nodeList.length; i++) {
if (nodeList[i].f > nodeList[bestIndex].f) { // invert heuristic
bestIndex = i;
}
}
var current = nodeList[bestIndex];
var x1 = current.x;
var y1 = current.y;
var pos1 = y1 * mapWidth + x1;
var g1 = current.g;
nodeList.splice(bestIndex, 1);
openList.splice(openList.indexOf(pos1), 1);
closedList.push(pos1);
if (g1 >= searchLimit) {
continue;
}
for (var j = 0; j < 4; j++) {
var direction = 2 + j * 2;
var x2 = $gameMap.roundXWithDirection(x1, direction);
var y2 = $gameMap.roundYWithDirection(y1, direction);
var pos2 = y2 * mapWidth + x2;
if (closedList.contains(pos2)) {
continue;
}
if (!this.canPass(x1, y1, direction)) {
continue;
}
var g2 = g1 + 1;
var index2 = openList.indexOf(pos2);
if (index2 < 0 || g2 < nodeList[index2].g) {
var neighbor;
if (index2 >= 0) {
neighbor = nodeList[index2];
} else {
neighbor = {};
nodeList.push(neighbor);
openList.push(pos2);
}
neighbor.parent = current;
neighbor.x = x2;
neighbor.y = y2;
neighbor.g = g2;
neighbor.f = g2 + $gameMap.distance(x2, y2, rootX, rootY);
if (!best || neighbor.f - neighbor.g > best.f - best.g) { // invert heuristic
best = neighbor;
}
}
}
}
var node = best;
while (node.parent && node.parent !== start) {
node = node.parent;
}
var deltaX1 = $gameMap.deltaX(node.x, start.x);
var deltaY1 = $gameMap.deltaY(node.y, start.y);
if (deltaY1 > 0) {
return 2;
} else if (deltaX1 < 0) {
return 4;
} else if (deltaX1 > 0) {
return 6;
} else if (deltaY1 < 0) {
return 8;
}
var deltaX2 = this.deltaXFrom(rootX);
var deltaY2 = this.deltaYFrom(rootY);
if (Math.abs(deltaX2) > Math.abs(deltaY2)) {
return deltaX2 > 0 ? 4 : 6;
} else if (deltaY2 !== 0) {
return deltaY2 > 0 ? 8 : 2;
}
return 0;
};
Move route Script examples (for the character to which the move route is assigned):
- Path away from tile X = 5, Y = 6:
JavaScript:this.moveStraight(this.findDirectionAway(5, 6)); - Path away from map event 12:
JavaScript:var c = $gameMap.event(12); this.moveStraight(this.findDirectionAway(c.x, c.y));
Edit - and to make it replace the existing "move away from character" method, you can try adding this to the bottom of the plugin code:
JavaScript:
// Override - replace "Move Away" type movement.
Game_Character.prototype.moveAwayFromCharacter = function(character) {
this.moveStraight(this.findDirectionAway(character.x, character.y));
};
This is excellent plugin, thank you a lot! Zero lag, and very good patchfinding, i have the map with many corners and dead ends, and it gets stuck in only one of them, and only when i approach from specific direction. From all the other corners if finds the way out, just what i needed!
Great! Just remember that "lag" builds up. You will only actually see stuttering when your system doesn't have enough resources to stay at the standard framerate (60 fps). However, the default search limit is low (12 tiles); in that case,
If you like, you could try increasing the search limit by replacing this line:
...with this:
...or a fixed number of tiles, e.g.
That will generally make each "path away" check need more CPU time. For narrow corridors, +1 range is not a large burden. For large open spaces, it is much more significant. Whether it is worth the trade depends on your game. 👋
findDirectionAway only looks up to 6 tiles away, so there shouldn't be any major problems.If you like, you could try increasing the search limit by replacing this line:
JavaScript:
var searchLimit = Math.ceil(this.searchLimit() / 2); // half search limit, rounded up
JavaScript:
var searchLimit = this.searchLimit();
JavaScript:
var searchLimit = 9;
Tried that, had some slowdowns, and the corner doesn't get fixed. But that's ok, i will use it as a feature.
The thing i don't understand, is why a powerful machine that can run even the second Horison game gets laggy when.... 2d game calculates patch? Seriously?
I also had the problem in previos game where field with lots of event and some animations were incredibly laggy, despite the location being rather small. But that game was made in VX.
Why does it happen? The RM developers never tried to do optimisation, or there is an other reason?
The thing i don't understand, is why a powerful machine that can run even the second Horison game gets laggy when.... 2d game calculates patch? Seriously?
I also had the problem in previos game where field with lots of event and some animations were incredibly laggy, despite the location being rather small. But that game was made in VX.
Why does it happen? The RM developers never tried to do optimisation, or there is an other reason?
Many reasons! E.g.
- MV/Z make web games: they run inside an application, an internet browser, that is very general-purpose. A general purpose tool (e.g. internet browser) will never be as efficient as one designed to do a specific task (e.g. precompiled game app).
- JavaScript is an embedded language that is compiled by the browser on demand, a.k.a. "just in time". This adds some slowdown at runtime; the time constraint might also affect the efficiency of the compiled result compared to precompiled code (e.g. C++).
- Up until VX Ace, I believe the rendering was all done on the CPU? A GPU is far superior to a CPU for highly-parallel tasks, like rendering. MV/Z allow the intense rendering to be done on the GPU, via WebGL, so they can afford to have higher resolutions at 60 fps, even with all the browser overhead.
- As mentioned, RPG Maker's A* is like, the basic pathfinding algorithm. It can be improved in many ways, but most of the most impactful improvements have to be judged and implemented on a case-by-case basis. There's a lot of technical info here, in case you're interested:
- I often see inefficient RPG Maker event designs. Sometimes it's an easy fix, sometimes not. Isolated instances of stuff like that are usually not a problem, but when they stack up then you get myths like "Parallel events cause lag".
@caethyril Is there a way to make the stock findDirectionTo() function lag less? Whatever you did for findDirectionAway (just reducing the search limit?) has almost no lag but the vanilla function lags horribly.
@AquaEcho - for
Here's some MV/Z plugin code to reduce the
Just replace
As I said earlier, resource requirements for pathfinding depend a lot on your use case. In case it helps, here's a neat tool for visualising various algorithms, including basic A*:
(By default
There may be other factors at play, too, e.g. plugins that change
Consider starting your own thread, so you can give appropriate context~ 👋
Since the OP's question seems to have been answered...
findDirectionAway all I did was change the 3 commented lines and remove some checks that would otherwise end early when reaching the target. Of those changes, the only one that should (in general) reduce resource requirements is the search limit reduction.Here's some MV/Z plugin code to reduce the
searchLimit value (default 12):
JavaScript:
Game_Character.prototype.searchLimit = function() { return 12; };
12 tiles with a lower number, e.g. 6.As I said earlier, resource requirements for pathfinding depend a lot on your use case. In case it helps, here's a neat tool for visualising various algorithms, including basic A*:
(By default
findDirectionTo uses non-diagonal A* with a weight-1 Manhattan distance heuristic. That's the default setup, with "Allow Diagonal" unchecked.)There may be other factors at play, too, e.g. plugins that change
findDirectionTo (e.g. 8-dir support) or introduce new movement without the necessary pathfinding adjustments (e.g. the MZ version of OverpassTile has a check for gateway tiles). Always start testing problems in the simplest possible setting, i.e. without any other plugins.Consider starting your own thread, so you can give appropriate context~ 👋
Since the OP's question seems to have been answered...
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-event-properly-run-from-player.167088/