Speed Eval v1.2

Introduction
Allows the use of formulas to adjust the invocation speed of skills and items, and adjusting base speed formula.

How to use
Save it as APHO_SpeedEval.js and put it in your plugin folder.
If you are using YEP_BattleEngineCore.js, this needs to be placed below it.

Use the following notetags in your skills/items:
JavaScript:
<speed>
//Code goes here; perform operations on 'speed'.
</speed>
The code will be run after all the default speed calculations.
You can use 'a' to reference the user.


Examples
JavaScript:
<speed>
speed += a.atk;
</speed>
This will increase the invocation speed by the user's Attack.

JavaScript:
<speed>
speed = 0;
</speed>
This will set the invocation speed to 0, ignoring Agility or the invocation speed set in the editor.

The base speed can also be adjusted from the plugin parameter 'Base speed'.
By default, RMMV has some random variance added to base speed.
This plugin's default settings will remove the random variance.

Script
Spoiler: CODE
JavaScript:
//==========================================
// APHO_SpeedEval.js
//==========================================
/*:
* @title Speed Eval
* @author Apho
* @plugindesc v1.1 Allows the use of formulas to adjust the invocation speed of skills and items, and adjusting base speed formula.
*
* @param Base speed
* @desc Formula to determine base speed.
* @default agi
* @help
*
* This plugin allows the developer to use evals to adjust the invocation speed
* of skills and items.
* To do so, use the following notetags in your skills/items:
*
* <speed>
* //Code goes here; perform operations on 'speed'.
* </speed>
*
* The code will be run after all the default speed calculations.
* You can use 'a' to reference the user.
*
* EXAMPLES:
* <speed>
* speed += a.atk;
* </speed>
* This will increase the invocation speed by the user's Attack.
*
* <speed>
* speed = 0;
* </speed>
* This will set the invocation speed to 0, ignoring Agility or the invocation
* speed set in the editor.
*
* The base speed can also be adjusted from the plugin parameter 'Base speed'.
* By default, RMMV has some random variance added to base speed.
* This plugin's default settings will remove the random variance.
*
* TERMS OF USE
* - Free for commercial and non-commercial use, as long as I get a free copy
*   of the game.
* - Edits allowed for personal use.
* - Do not repost or claim as your own, even if edited.
*
* VERSION HISTORY
* v1.1 - 2022/9/10 - Optimized code to improve compatibility with other plugins.
* v1.0 - 2022/9/9 - Initial release.
*/
var Apho = Apho || {};
Apho.SpeedEvalParams = PluginManager.parameters('APHO_SpeedEval');
Apho.SpeedEvalParams.BaseSpeed = Apho.SpeedEvalParams['Base speed'];

const AphoDataBaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function()
{
    AphoDataBaseLoaded.call(this);
    if (!AphoDataBaseLoaded.call(this)) return false;
    if (!Apho.SpeedEvalLoaded)
    {
        Apho.CheckSpeedEval($dataSkills);
        Apho.CheckSpeedEval($dataItems);
        Apho.SpeedEvalLoaded = true;
    }
    return true;
};

Apho.CheckSpeedEval = function(group)
{
    const SpeedEvalOpen = '<speed>';
    const SpeedEvalClose = '</speed>';
    for (var n = 1; n < group.length; n++)
    {
        var obj = group[n];
        var NoteData = obj.note.split(/[\r\n]+/);
        var ReadSpeedEval = false;
        obj.SpeedEval = '';
        for (var i = 0; i < NoteData.length; i++)
        {
            var line = NoteData[i];
            if (line.match(SpeedEvalOpen))
            {
                ReadSpeedEval = true;
            } else if (line.match(SpeedEvalClose))
            {
                ReadSpeedEval = false;
            }
            else if (ReadSpeedEval == true)
            {
                obj.SpeedEval = obj.SpeedEval + line + '\n';
            }
        }
    }
};

Game_Action.prototype.speed = function()
{
    var agi = this.subject().agi;
    var speed = eval(Apho.SpeedEvalParams.BaseSpeed);
    if (this.item())
    {
        speed += this.item().speed;
    }
    if (this.isAttack())
    {
        speed += this.subject().attackSpeed();
    }
    if(this.item().SpeedEval)
    {
        var a = this.subject();
        eval(this.item().SpeedEval);
    }
    return speed;
};

FAQ

Terms of use

  • Free for commercial and non-commercial use, as long as I get a free copy of the game.
  • Edits allowed for personal use.
  • Do not repost or claim as your own, even if edited.
  • Do not use for training AI models. This includes, but is not limited to, inputting the plugin contents or part thereof into AI models that train on user input.
Version history
  • v1.2 - 2023/6/1 - Fixed a bug that caused incompatibility issues with other turn order display plugins.
  • v1.1 - 2022/9/10 - Optimized code to improve compatibility with other plugins.
  • v1.0 - 2022/9/9 - Initial release.
As a developer by day, reviewing other folks' code is something of a habit!
When peeking at the underlying code here, a few things prop up that concern me as "best practices"...

Spoiler: my observations
First being:
JavaScript:
var parameters = PluginManager.parameters('APHO_SpeedEval');
var BaseSpeed = parameters['Base speed'];

By keeping these as floating "var"s, they would overwrite (or be overwritten depending on plugin order) any other variables that are named as such in the global namespace. The global namespace being... anything that isn't encapsulated in a more confined scope. To handle this, most folks around here I've seen use IIFE (as in, wrap your plugin code inside a "immediately-invoked function expression"). You could also make clever use of your own hand-made "Apho" object to create a namespace of sorts.

Second being:

JavaScript:
const AphoDataBaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function()
{
    if (!AphoDataBaseLoaded.call(this)) return false;
    if (!Apho.SpeedEvalLoaded)
    {
        this.AphoCheckSpeedEval($dataSkills);
        this.AphoCheckSpeedEval($dataItems);
        Apho.SpeedEvalLoaded = true;
    }
    return true;
};

What is the point of capturing the const AphoDataBaseLoaded = DataManager.isDatabaseLoaded; line? It is a reference to the method itself, which is simply "has this database been loaded?". The point being it doesn't really give any value. You're still overwriting this function with your own extra stuff, of which most folks use this sort of function aliasing to "extend" functions with additional logic. I don't think this function is operating quite how you think it is.

Third being:
You could change DataManager.AphoCheckSpeedEval = function(group) to Apho.AphoCheckSpeedEval = function(group) and update your this.AphoCheckSpeedEval(group) >> Apho.AphoCheckSpeedEval(group) and it would operate exactly the same and keep unnecessary methods out of the static DataManager class that probably don't need to be there?

Fourth being:
In your overwrite of the speed function, you've got this bit at the top that re-evaluates the static values of the parameters every time you run the method, which is often. "eval" is a somewhat expensive function to run and you could probably achieve similar results by performing the eval in the beginning of the plugin code setup once and never again.

Fifth being:
I'm sure this overwrite of the speed function works for your usage and intent, but if someone else were to leverage... some other plugin that also modifies speed in this function alongside your plugin, your plugin would either get overwritten, or overwrite. This would be a use case for the function aliasing you attempted to use, but didn't quite do correctly. Also, not to mention, you are silently removing the RNG aspect that is natively a part of speed calculations and also not telling anyone that you're doing this.

tldr: I think this plugin could benefit from some polish before it gets used, else folks may come back here asking "why isn't it working as I expect it to?".
Thanks for the feedback, I'll be working on improving it. Still new to this, sorry...

What is the point of capturing the const AphoDataBaseLoaded = DataManager.isDatabaseLoaded; line? It is a reference to the method itself, which is simply "has this database been loaded?". The point being it doesn't really give any value. You're still overwriting this function with your own extra stuff, of which most folks use this sort of function aliasing to "extend" functions with additional logic. I don't think this function is operating quite how you think it is.
I was trying to alias and call the original function. Guess I forgot to actually call the original function😅

In your overwrite of the speed function, you've got this bit at the top that re-evaluates the static values of the parameters every time you run the method, which is often. "eval" is a somewhat expensive function to run and you could probably achieve similar results by performing the eval in the beginning of the plugin code setup once and never again.
Base speed can change throughout a battle by agility buffs and debuffs though?

I'm sure this overwrite of the speed function works for your usage and intent, but if someone else were to leverage... some other plugin that also modifies speed in this function alongside your plugin, your plugin would either get overwritten, or overwrite. This would be a use case for the function aliasing you attempted to use, but didn't quite do correctly. Also, not to mention, you are silently removing the RNG aspect that is natively a part of speed calculations and also not telling anyone that you're doing this.
I overwrote the speed function instead of aliasing it because I wanted to remove the RNG aspect and use a user defined formula instead. Not sure if there's any other way I could have done it?

edit: updated the OP with what is hopefully a better code/explanation
I'm looking for a way for an item to set the speed for all skills to 0. Would that be an interesting add-on for this plugin? Would it even be doable?
You could have the item apply a state, and in each of your skills' notetags, check if the user has the state; if so, set the speed to 0.

JavaScript:
<speed>
a.isStateAffected(n) ? speed = 0 : speed = speed;
</speed>

Replace 'n' with the id of your state.

Original thread: https://forums.rpgmakerweb.com/threads/speed-eval-v1-2.151183/