// CMS Tools: Advanced Faster ATB
// by PepsiOtaku
// Version 2.0

#define AUTO_DLLMAIN
#include <DynRPG/DynRPG.h>

using namespace std;

// Advanced Faster ATB by PepsiOtaku

int battleSpeedVariableID;
int battleSpeedVarValue;
int battleSpeedDefault;

// create an array for all possible battle speed multipliers
int battlePercents[20] = { 0, 25, 50, 75, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600 };

bool onStartup(char *pluginName) {
	std::map<std::string, std::string> configuration = RPG::loadConfiguration(pluginName);
	// Variable used to store the Battle Speed value
    battleSpeedVariableID = atoi(configuration["BattleSpeedVariable"].c_str());
    // ini paramater for the default Speed. Recommended values are multiples of 100
    battleSpeedDefault = atoi(configuration["DefaultSpeed"].c_str());
    RPG::battleSpeed = battleSpeedDefault; // Upon startup, set the battle speed to the value defined in DynRPG.ini
    return true;
}

bool onSetVariable(int id, int value) {
    if (id == battleSpeedVariableID){ // if the variable's ID equals the value stored in the ini
        if (value == 0){ // if no value has been set to this variable, use the default battle speed
            RPG::battleSpeed = battleSpeedDefault;
            // RPG::variables[4002] = battleSpeedDefault;// used for testing
        } else if (value <= 16) {
            for (int i=0; i<=3; i++) { // loop & check for values -4 to -1
                if (value == (i-4)){ // if the looped value equals the variable's value
                    RPG::battleSpeed = battlePercents[i]; // set the battle speed to the value stored in Array "battlePercents" for i.
                    // RPG::variables[4002] = battlePercents[i]; // used for testing
                }
            }
            for (int i=4; i<=19; i++) { // loop & check for values 1-16
                if (value == (i-3)){ // if the looped value equals the variable's value
                    RPG::battleSpeed = battlePercents[i]; // set the battle speed to the value stored in Array "battlePercents" for i.
                    // RPG::variables[4002] = battlePercents[i]; // used for testing
                }
            }
        } else {
            RPG::battleSpeed = battleSpeedDefault;// if variable contains any other value, use the default battle speed
        }
    }
	return true;
}

void onLoadGame(int id, char *data, int length) { // follow the same procedure as onSetVariable, but use "battleSpeedVarValue" instead of "value"
    battleSpeedVarValue = RPG::variables[battleSpeedVariableID]; // Set battleSpeedVarValue to the value of the variable stored in the ini
    if (battleSpeedVarValue == 0){
        RPG::battleSpeed = battleSpeedDefault;
    } else if (battleSpeedVarValue <= 16) {
        for (int i=0; i<=3; i++) { // loop & check for values -4 to -1
                if (battleSpeedVarValue == (i-4)){ // if the looped value equals the variable's value
                    RPG::battleSpeed = battlePercents[i]; // set the battle speed to the value stored in Array "battlePercents" for i.
                }
            }
            for (int i=4; i<=19; i++) { // loop & check for values 1-16
                if (battleSpeedVarValue == (i-3)){ // if the looped value equals the variable's value
                    RPG::battleSpeed = battlePercents[i]; // set the battle speed to the value stored in Array "battlePercents" for i.
                }
            }
    } else {
        RPG::battleSpeed = battleSpeedDefault;
    }

}

