DESCRIPTION
I noticed something really interesting with the way that Events move when they are assigned to "Move Away from Player" in a move route. While most of the time this movement command works correctly, there are at least two situations where the event will incorrectly fail to move away from the player:
For example, in the screenshot below, the Treasure Chest is set to run away from the player, because he doesn't want his valuable contents pilfered!
Paralyzed with Fear!
While the Chest could distance itself from Greedy Reid by moving up or down, he sits still instead and waits to have his insides stolen.
While "Move Away from Player" move commands are the only case I can think of where this behavior rears its ugly head by default, anything that makes use of the moveAwayFromCharacter method will be subject to it.
ANALYSIS
When I looked through the code, I found that this is most likely due to a bug or design gap created by copying behavior from the moveTowardCharacter method, where the event is (correctly) told to never move along the X-axis or Y-axis if the moving event (e.g. Treasure Chest) and the target event (e.g. Reid) are already at the same X-coordinate or Y-coordinate, respectively. After all, there is no way the event could get closer to its target if the difference is already zero.
However, in the case of moveAwayFromCharacter, disabling movement along an axis when at the same coordinate as the player makes no sense. When at the same Y-coordinate as the player, if the event cannot move away from the player with a Left/Right move, the event should attempt a move either Up or Down. (The current code allows this second attempt if the Up/Down direction is the smaller difference in distances, but specifically disallows it when the difference is zero, which makes no sense.)
EXAMPLE CODE FIX
The code fix here is fairly simple and quite safe, affecting only the moveAwayFromCharacter method. I will suggest two different ways the code for this method could be rewritten for the next patch of RPG Maker MZ to improve the moveAwayFromCharacter behavior, with comments and very readable (but not concise) code. The code could certainly be made more concise (such as not using 3 lines for simple if-then processing).
You have my permission to use any of the code I'm suggesting here. I would greatly appreciate credit if you do use it, but I do not require credit.
Here's the original code, as you can find it right now in MZ:
Here's a very "efficient" rewrite of the method that attempts up to one additional move away from the player when any of the above scenarios are described. I believe it will have zero impact on performance for any movement type other than Move Away From, zero impact on performance when none of the above "problem scenarios" are in play, and a completely negligible impact on performance even when those problem scenarios are in play. However, in the rare case where the chosen direction is also impassable, the event will sit still for a step.
Alternatively, here's a more "reliable" rewrite of the method that attempts a move in all valid directions (which would move the event further from the target), until either a movement succeeds or all valid directions have been exhausted. This is the "smartest" behavior for an event that wants to move away from the player or another event. I believe it will have zero impact on performance for any movement type other than Move Away From, but it will have a very tiny impact on performance for Events that are processing a Move Away From move, as several extra lines of simple evaluations must be executed at the beginning and end.
WHY DO WE NEED THIS FIX?
The current way that moveAwayFromCharacter (and therefore the Move Away From Player move command) is handled looks sloppy to the player in certain scenarios, like the ones described in this thread. Among some of the biggest culprits in an actual game would be:
I noticed something really interesting with the way that Events move when they are assigned to "Move Away from Player" in a move route. While most of the time this movement command works correctly, there are at least two situations where the event will incorrectly fail to move away from the player:
- Whenever the event is aligned with the player along one axis, and can't move in the direction it wants to along the other axis (due to impassable terrain, another event blocking the way, etc.)
- Whenever the event is at the same location as the player (but has the ability to move, because it's a "Through" event or has a priority of Below/Above Characters)
For example, in the screenshot below, the Treasure Chest is set to run away from the player, because he doesn't want his valuable contents pilfered!
Paralyzed with Fear!
While the Chest could distance itself from Greedy Reid by moving up or down, he sits still instead and waits to have his insides stolen.
While "Move Away from Player" move commands are the only case I can think of where this behavior rears its ugly head by default, anything that makes use of the moveAwayFromCharacter method will be subject to it.
ANALYSIS
When I looked through the code, I found that this is most likely due to a bug or design gap created by copying behavior from the moveTowardCharacter method, where the event is (correctly) told to never move along the X-axis or Y-axis if the moving event (e.g. Treasure Chest) and the target event (e.g. Reid) are already at the same X-coordinate or Y-coordinate, respectively. After all, there is no way the event could get closer to its target if the difference is already zero.
However, in the case of moveAwayFromCharacter, disabling movement along an axis when at the same coordinate as the player makes no sense. When at the same Y-coordinate as the player, if the event cannot move away from the player with a Left/Right move, the event should attempt a move either Up or Down. (The current code allows this second attempt if the Up/Down direction is the smaller difference in distances, but specifically disallows it when the difference is zero, which makes no sense.)
EXAMPLE CODE FIX
The code fix here is fairly simple and quite safe, affecting only the moveAwayFromCharacter method. I will suggest two different ways the code for this method could be rewritten for the next patch of RPG Maker MZ to improve the moveAwayFromCharacter behavior, with comments and very readable (but not concise) code. The code could certainly be made more concise (such as not using 3 lines for simple if-then processing).
You have my permission to use any of the code I'm suggesting here. I would greatly appreciate credit if you do use it, but I do not require credit.
Here's the original code, as you can find it right now in MZ:
JavaScript:
Game_Character.prototype.moveAwayFromCharacter = function(character) {
const sx = this.deltaXFrom(character.x);
const sy = this.deltaYFrom(character.y);
if (Math.abs(sx) > Math.abs(sy)) {
this.moveStraight(sx > 0 ? 6 : 4);
if (!this.isMovementSucceeded() && sy !== 0) {
this.moveStraight(sy > 0 ? 2 : 8);
}
} else if (sy !== 0) {
this.moveStraight(sy > 0 ? 2 : 8);
if (!this.isMovementSucceeded() && sx !== 0) {
this.moveStraight(sx > 0 ? 6 : 4);
}
}
};
Here's a very "efficient" rewrite of the method that attempts up to one additional move away from the player when any of the above scenarios are described. I believe it will have zero impact on performance for any movement type other than Move Away From, zero impact on performance when none of the above "problem scenarios" are in play, and a completely negligible impact on performance even when those problem scenarios are in play. However, in the rare case where the chosen direction is also impassable, the event will sit still for a step.
JavaScript:
Game_Character.prototype.moveAwayFromCharacter = function(character) {
const sx = this.deltaXFrom(character.x);
const sy = this.deltaYFrom(character.y);
if (Math.abs(sx) > Math.abs(sy)) {
this.moveStraight(sx > 0 ? 6 : 4);
if (!this.isMovementSucceeded()) { // sy > 0 should not be a condition to consider a move in the Y direction
// If sy (difference in Y coordinate between this character and the target) is not zero, do the original
// processing; if it is zero, choose Up or Down at random to try to move
this.moveStraight(sy !== 0 ? (sy > 0 ? 2 : 8) : (Math.random() < 0.5 ? 2 : 8));
}
} else if (sy !== 0) {
this.moveStraight(sy > 0 ? 2 : 8);
if (!this.isMovementSucceeded()) { // sx > 0 should not be a condition to consider a move in the X direction
this.moveStraight(sx !== 0 ? (sx > 0 ? 6 : 4) : (Math.random() < 0.5 ? 6 : 4));
}
// If neither of the above is true, that means that this character and the target are at the same location;
// if this is the case, choose Up, Down, Left, or Right at Random
} else {
this.moveStraight(2 + Math.randomInt(4) * 2);
}
};
Alternatively, here's a more "reliable" rewrite of the method that attempts a move in all valid directions (which would move the event further from the target), until either a movement succeeds or all valid directions have been exhausted. This is the "smartest" behavior for an event that wants to move away from the player or another event. I believe it will have zero impact on performance for any movement type other than Move Away From, but it will have a very tiny impact on performance for Events that are processing a Move Away From move, as several extra lines of simple evaluations must be executed at the beginning and end.
JavaScript:
Game_Character.prototype.moveAwayFromCharacter = function(character) {
const sx = this.deltaXFrom(character.x);
const sy = this.deltaYFrom(character.y);
var attempt_move_x = false; // Whether to make an additional attempt to move in X direction
var attempt_move_y = false; // Whether to make an additional attempt to move in Y direction
if (Math.abs(sx) > Math.abs(sy)) {
this.moveStraight(sx > 0 ? 6 : 4);
if (!this.isMovementSucceeded()) {
// If sy is indeed nonzero (this character and target aren't at same Y-coord), do the original processing
if (sy !== 0) {
this.moveStraight(sy > 0 ? 2 : 8);
// Otherwise, note that we should make an attempt later on to randomly move Up or Down
} else {
attempt_move_y = true;
}
}
} else if (sy !== 0) {
this.moveStraight(sy > 0 ? 2 : 8);
if (!this.isMovementSucceeded()) {
// If sx is indeed nonzero, do the original processing
if (sx !== 0) {
this.moveStraight(sx > 0 ? 6 : 4);
// Otherwise, note that we should make an attempt later on to randomly move Left or Right
} else {
attempt_move_x = true;
}
}
// If neither of the above is true, that means that this character and the target are at the same location;
// if this is true, note that attempts should be made later on to move in either direction (prioritizing
// the up/down direction like this method already does for equal, nonzero difference between Xs and Ys)
} else {
attempt_move_x = true;
attempt_move_y = true;
}
// If we need to attempt a move in the Y-direction, choose Up or Down at random. If that direction doesn't
// result in successful movement, try the other direction.
if (attempt_move_y === true) {
const possible_y_dirs = [2, 8];
var chosen_dir = Math.round(Math.random()); // This is picking either 0 or 1 as the index in the array to use
this.moveStraight(possible_y_dirs[chosen_dir]);
// If the movement succeeded, we're done with movement - we don't need to also check the X direction; if the
// movement didn't succeed, attempt to move in the direction not randomly chosen
if (!this.isMovementSucceeded()) { // If movement didn't succeed
this.moveStraight(possible_y_dirs[1 - chosen_dir]); // 1 - chosen_dir represents the index (0 or 1) not chosen
// If this second attempt to move (in the other Y direction) did succeed, don't check X direction
if (this.isMovementSucceeded()) {
attempt_move_x = false;
}
} else { // If movement did succeed
attempt_move_x = false;
}
}
// After running any applicable second-chance attempts to move in the Y-direction, if we still need to attempt
// a move in the X-direction, choose Left or Right at random. If unsuccessful, try the other direction.
if (attempt_move_x === true) {
const possible_x_dirs = [6, 4];
var chosen_dir = Math.round(Math.random());
this.moveStraight(possible_x_dirs[chosen_dir]);
// If the movement succeeded, we're done with movement - do nothing else and end the method; if the movement
// didn't succeed, attempt to move in the direction not randomly chosen
if (!this.isMovementSucceeded()) { // If movement didn't succeed
this.moveStraight(possible_x_dirs[1 - chosen_dir]); // 1 - chosen_dir represents the index (0 or 1) not chosen
}
}
};
WHY DO WE NEED THIS FIX?
The current way that moveAwayFromCharacter (and therefore the Move Away From Player move command) is handled looks sloppy to the player in certain scenarios, like the ones described in this thread. Among some of the biggest culprits in an actual game would be:
- Events that the player needs to chase and catch - these events would act very stupidly by giving up after their "ideal move" is disabled as long as the player is lined up with the event, making the chase trivially easy.
- Evented decoration/"particles", such as butterflies, that are supposed to move away from the player - these events would actually stand still forever (until the player moves) if they were to ever be located on the same tile as the player
- Any custom plugins or custom JS Move Routes that call moveAwayFromCharacter, such as an NPC Cat that is always trying to run away from an NPC Dog, will also not work properly under the current behavior