How do I change the Escape formula?

Hi! I'm extremely new to plugins and .json coding, and I'd like to alter the formula for escaping from battles.

Specifically, I'd like to have there be a base escape chance that equipment or passive skills can affect.
(E.g. The PC has a default 50% chance to escape a default battle. If they wear Running Shoes, that bumps them up by an additional 20% chance. If they learn the skill "Light Feet", they have a permanent 10% chance to escape. If they're wearing heavy armor, their escape chance drops by 10%. A certain enemy type might be easier or harder to flee from. Etc.)

What little I know about coding suggests that this will either be absurdly easy OR absurdly hard to implement. But on the off-chance anyone's feeling charitable, I'd appreciate your help!
It's absurdly easy to simply change the formula. This is the code in the base engine that determines the formula:

JavaScript:
BattleManager.makeEscapeRatio = function() {
    this._escapeRatio = (0.5 * $gameParty.agility()) / $gameTroop.agility();
};

So just put in whatever formula you want there (the part after the = sign in case that's not obvious), save it as EscapeRatio.js (or whatever name you want), and add it as a plugin.

(Side note: If you use VisuStella, there might also be a place in one of the plugin parameters to put the formula. You could poke around and see if that exists.)

Making escape ratio be determined by a trait that can be modified by items is a little bit trickier, but that's only because you need to add code to parse that trait from note tags. To base escape ratio exclusively on a trait, your formula would look something like this:

JavaScript:
const YOUR_TRAIT = 1001; // can be anything at all – number, string, whatever – as long as it doesn't conflict with an existing trait
BattleManager.makeEscapeRatio = function() {
    this._escapeRatio = $gameParty.members.reduce((current, next) =>
        Math.max(current, next.traitsPi(YOUR_TRAIT, 0))
    , 0);
};

That doesn't cover the code to parse the trait though. However, if you wanted to repurpose a built-in trait, the above would work just as it is (YOUR_TRAIT would be Game_BattlerBase.TRAIT_SOMETHING, which you can look up in rmmz_objects.js, and the 0 might have to be something other than 0, depending on the trait).

Keep in mind that that's just an example – it means the game takes the escape ratios from equipment and multiplies them together for each actor, then takes the largest one. Maybe that's not the correct mechanic for you. There's also traitsSum which adds them instead of multiplying. You could use Math.min to take the lowest. There are almost infinite possibilities.

Note: That also doesn't touch two other escape-related mechanics: 1) If you have the pre-emptive strike, your escape chance is always 100%, and 2) Every time you fail to escape, the chance goes up by 10% for your next attempt.
Base Code:
JavaScript:
BattleManager.makeEscapeRatio = function() {
    this._escapeRatio = (0.5 * $gameParty.agility()) / $gameTroop.agility();
};

Edited Code:
JavaScript:
BattleManager.makeEscapeRatio = function() {
    let escapeChance = (0.5 * $gameParty.agility()) / $gameTroop.agility();
    let actors = $gameParty.allMembers();
  
    if (actors.some(a => a.hasArmor(runningShoesId)) escapeChance += .2;
    if (actors.some(a => a.isAtypeEquipped(heavyArmorId)) escapeChance -= .1;
    if (actors.some(a => a.hasSkill(lightFeetId)) escapeChance += .1;
  
    this._escapeRatio = escapeChance;
};

For loop if you want to check every actor:
JavaScript:
BattleManager.makeEscapeRatio = function() {
    let escapeChance = (0.5 * $gameParty.agility()) / $gameTroop.agility();
    let actors = $gameParty.allMembers();

    for (let i = 0; i > actors.length; i++) {
  
    if (actors[i].hasArmor(runningShoesId)) escapeChance += .2;
    if (actors[i].isAtypeEquipped(heavyArmorId)) escapeChance -= .1;
    if (actors[i].hasSkill(lightFeetId)) escapeChance += .1;

    }
  
    this._escapeRatio = escapeChance;
};

Might be wrong, have to double check later but I don't got time rn.

Edit: I was looking at some of the battler methods and they make the code cleaner
YOU ARE BOTH AMAZING. Thank you!!! This will definitely solve several issues. You've also given me a few things to look into more closely - after all, it's hard to search for information when you don't know the right words, lol.

Be well!

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-change-the-escape-formula.182560/