EZ 1 script Magic Equipment buffs

Got any custom JavaScript additions/tweaks you think other people would like to see? Post 'em here!
Post Reply
Humility
UOX3 Neophyte
Posts: 28
Joined: Mon Nov 21, 2016 7:51 am
Has thanked: 4 times
Been thanked: 5 times

EZ 1 script Magic Equipment buffs

Post by Humility »

I wrote this to make having magic items a little easier in the game.
You'll have to update the script number in the script or set it to 5052 in your jsfileassociations script.
but all you should have to do is set script trigger to the script number on any piece of equipment and it will randomly roll magic properties for it, from the handful that I currently have working. which is buffs to str,dex,int,health,stam,mana, and a new thorns effect.

For artifact grade items, you will have to manually edit the buffList tag to set the values for each effect you want either through a command like set,a new script or through the DFNs and also manually set the flavorText tag to add the little quip at the end of the tooltip that gives the item some character/backstory.
1 ring.png
1 ring.png (144.35 KiB) Viewed 6513 times
Edit [ Feel free to respond to this with any suggestions for effects or updates or hit me up on discord in the scripting chat]
// Stat-boosting Equipment  Humility, 7-1-2023
function onEquip(pEquipper, iEquipped) {
    var buffMan = new characterBuffInterface(pEquipper, iEquipped); // Instantiate interface object
    if (iEquipped.GetTag("statBonusFlags") < 1) { buffMan.NewLoot(); }
    var ItemBuffArray = buffMan.GetItemBuffs();
    buffMan.Strength.Increment(ItemBuffArray[0]);       //strength
    buffMan.Dexterity.Increment(ItemBuffArray[1]);      //dex
    buffMan.Intelligence.Increment(ItemBuffArray[2]);   //int
    buffMan.Thorns.Increment(ItemBuffArray[3]);         //thorns
    buffMan.MaxHealth.Increment(ItemBuffArray[6]);      //maxhealth
    buffMan.MaxStam.Increment(ItemBuffArray[7]);        //maxstamina
    buffMan.MaxMana.Increment(ItemBuffArray[8]);        //maxmana
    buffMan.charObj.AddScriptTrigger(5052);
    Console.Print(buffMan.charObj.scripttrigger.length)
    //if (indexof(5052) != -1) { Console.Print("Trigger 5052 found"); }
    return true;
}

function onUnequip(pUnequipper, iUnequipped) {
    var buffMan = new characterBuffInterface(pUnequipper, iUnequipped); // Instantiate interface object
    if (iUnequipped.GetTag("statBonusFlags") < 1) { buffMan.NewLoot(); }
    var ItemBuffArray = buffMan.GetItemBuffs();
    buffMan.Strength.Decrement(ItemBuffArray[0]);       //strength
    buffMan.Dexterity.Decrement(ItemBuffArray[1]);      //dex
    buffMan.Intelligence.Decrement(ItemBuffArray[2]);   //int
    buffMan.Thorns.Decrement(ItemBuffArray[3]);         //thorns
    buffMan.MaxHealth.Decrement(ItemBuffArray[6]);      //maxhealth
    buffMan.MaxStam.Decrement(ItemBuffArray[7]);        //maxstamina
    buffMan.MaxMana.Decrement(ItemBuffArray[8]);        //maxmana
    // check all equipment to see if any of them have script trigger 5052
    if (!buffMan.OneOfUs()) { //no other scripts found with 5052 magic item script remove it from player.
        buffMan.charObj.RemoveScriptTrigger(5052);
    }

    return true;
}

function onTooltip(myObj) {
    if (typeof myObj.accountNum != "undefined" || myObj.npc) { return myObj.name; } else {

        var buffMan = new characterBuffInterface(null, myObj); // Instantiate interface object
        if (myObj.GetTag("statBonusFlags") < 1) { buffMan.NewLoot(); }
        var ItemBuffArray = buffMan.GetItemBuffs();
        if (typeof ItemBuffArray == "undefined") { return false; } // missing tags wreaking havoc
        // Setup our template colors
        var buffGreen = "<BASEFONT COLOR=#008000>";
        var debuffRed = "<BASEFONT COLOR=#FF0000>";
        var statWhite = "<BASEFONT COLOR=#D0D0F0 size= 2>";
        var flavorGold = "<BASEFONT COLOR=#FFD700 size= 5 >";
        var clBase = "</BASEFONT>"; // close the basefont tag
        var flags = buffMan.GetFlags(myObj.GetTag("statBonusFlags"));
        var Stats = [
            "Strength", "Dexterity", "Intelligence", "Thorns",
            "Faster Casting", "Faster Cast Recovery", "Health",
            "Stamina", "Mana", "Health Regeneration", "Mana Regeneration",
            "Stamina Regeneration", "Resist All Elements",
            "Damage Reduction", "Luck", "Gold Find"
        ];
        var tooltipText = "";
        Stats.forEach(function (stat, index) {
            if (ItemBuffArray[index] > 0) {
                tooltipText += (parseInt(ItemBuffArray[index]) > 0 ? buffGreen + " " : debuffRed);
                tooltipText += (index > 0 ? "\n+":"+") + ItemBuffArray[index].toString() + statWhite + " " + stat + clBase; //aesthetics
             }
        });
        var myFlayvah = myObj.GetTag("flavorText");
        if (myFlayvah != "0") { tooltipText += flavorGold + "\n"+ myFlayvah+clBase +"\n"; }
        tooltipText += statWhite;

        return tooltipText;
    }
}
//B></B> <BIG></BIG> <SMALL></SMALL> <EM></EM> <I></I> <U></U> <H1></H1> <H2></H2> <H3></H3> <H4></H4> <H5></H5> <H6></H6>
//<a href=""></a> <div align="right"><div align="left"></DIV> <left></left> <P> <CENTER></CENTER> <BR></BR> <BASEFONT color=#ffffff size=1-7></BASEFONT>
function onNameRequest(myObj, requestedBy) {
    if (typeof myObj.accountNum != "undefined" || myObj.npc) {
        //Console.Print(requestedBy.name.toString());
        return myObj.name //+ "\n" + "thorns: xAmount"; //testing whether it will screw up the status bar and such.
    } else {
        return "<BASEFONT COLOR=#00BFFF size=7>" + myObj.name + "</BASEFONT>"; //magic item blue
    }
}

//o------------------------------------------------------------------------------------------------o
//| Function    -   characterBuffInterface(buffChar)
//| Date        -   06/29/2023
//o------------------------------------------------------------------------------------------------o
//| Purpose     -   Constructor to supply API functions and interface to apply complex equipment buffs
//| Changes     -
//|
//|
//|
//|
//o------------------------------------------------------------------------------------------------o

function characterBuffInterface(pChar, myObj) {
    var self = this;
    this.itemObj = myObj;
    this.charObj = pChar;
    // initialize player variables.

    //  ACHTUNG!!! pay close attention to the casing!! to keep it in line with the server and to allow for function casing we are using both!
    this.strength = 0; // the strength value variable
    this.Strength = {}; // the Strength function container
    //  Danger will robinson!!!! pay close attention to the casing!! to keep it in line with the server and to allow for function casing we are using both!
    this.Strength.Get = function () { // Gets the current values from the character object and sets them to JS object
        self.strength = self.charObj.strength;
    }
    this.Strength.Set = function () { // updates character object with JS object changes
        self.charObj.strength = self.strength;
    }
    this.Strength.Increment = function (valuePlus) { // increments value in JS object (defaults to 1)
        self.Strength.Get();
        self.strength += (typeof valuePlus == "undefined" ? 1 : parseInt(valuePlus));
        self.Strength.Set();
    }
    this.Strength.Decrement = function (valueMinus) { // decrements value in JS object (defaults to 1)
        self.Strength.Get();
        self.strength -= (typeof valueMinus == "undefined" ? 1 : parseInt(valueMinus));
        self.Strength.Set();
    }

    this.dexterity = 0; // the dex value variable
    this.Dexterity = {}; // the Dex function container
    //  Danger will robinson!!!! pay close attention to the casing!! to keep it in line with the server and to allow for function casing we are using both!
    this.Dexterity.Get = function () { // Gets the current values from the character object and sets them to JS object
        self.dexterity = self.charObj.dexterity;
    }
    this.Dexterity.Set = function () { // updates character object with JS object changes
        self.charObj.dexterity = self.dexterity;
    }
    this.Dexterity.Increment = function (valuePlus) { // increments value in JS object (defaults to 1)
        self.Dexterity.Get();
        self.dexterity += (typeof valuePlus == "undefined" ? 1 : parseInt(valuePlus));
        self.Dexterity.Set();
    }
    this.Dexterity.Decrement = function (valueMinus) { // decrements value in JS object (defaults to 1)
        self.Dexterity.Get();
        self.dexterity -= (typeof valueMinus == "undefined" ? 1 : parseInt(valueMinus));
        self.Dexterity.Set();
    }

    this.intelligence = 0; // the intelligence value variable
    this.Intelligence = {}; // the Intelligence function container
    //  Danger will robinson!!!! pay close attention to the casing!! to keep it in line with the server and to allow for function casing we are using both!
    this.Intelligence.Get = function () { // Gets the current values from the character object and sets them to JS object
        self.intelligence = self.charObj.intelligence;
    }
    this.Intelligence.Set = function () { // updates character object with JS object changes
        self.charObj.intelligence = self.intelligence;
    }
    this.Intelligence.Increment = function (valuePlus) { // increments value in JS object (defaults to 1)
        self.Intelligence.Get();
        self.intelligence += (typeof valuePlus == "undefined" ? 1 : parseInt(valuePlus));
        self.Intelligence.Set();
    }
    this.Intelligence.Decrement = function (valueMinus) { // decrements value in JS object (defaults to 1)
        self.Intelligence.Get();
        self.intelligence -= (typeof valueMinus == "undefined" ? 1 : parseInt(valueMinus));
        self.Intelligence.Set();
    }


    this.thorns = 0; // the thorns value variable
    this.Thorns = {}; // the Thorns function container
    //  Danger will robinson!!!! pay close attention to the casing!! to keep it in line with the server and to allow for function casing we are using both!
    this.Thorns.Get = function () { // Gets the current values from the character object's tag and sets them to JS object
        self.thorns = parseInt(self.charObj.GetTag("thorns"));
    }
    this.Thorns.Set = function () { // updates character object's Tag with JS object changes
        if (self.thorns < 1) { // if we somehow get bugged out and go negative we will delete the tag so re-equipping the items will correct it.
            self.charObj.SetTag("thorns", null);
        } else {
            self.charObj.SetTag("thorns", self.thorns.toString());
        }
        Console.Print("Thorns: " + self.charObj.GetTag("thorns").toString());
    }
    this.Thorns.Increment = function (valuePlus) { // increments value in JS object (defaults to 1)
        self.Thorns.Get();
        self.thorns += (typeof valuePlus == "undefined" ? 1 : parseInt(valuePlus));
        self.Thorns.Set();
    }
    this.Thorns.Decrement = function (valueMinus) { // decrements value in JS object (defaults to 1)
        self.Thorns.Get();
        self.thorns -= (typeof valueMinus == "undefined" ? 1 : parseInt(valueMinus));
        self.Thorns.Set();
    }



    this.fCasting = 0;
    this.fCasting.Get = function () { // nonexistant character variable - unimplemented
        self.fCasting = self.charObj.fCasting;
    }
    this.fCasting.Set = function (newStr) {
        self.charObj.fCasting = newStr;
    }
    this.fCasting.Increment = function (valuePlus) {
        self.fCasting = self.charObj.fCasting;
        self.fCasting += valuePlus;
    }
    this.fCasting.Decrement = function (valueMinus) {
        self.fCasting = self.charObj.fCasting;
        self.fCasting -= valueMinus;
    }


    this.fCastRecover = 0;
    this.fCastRecover.Get = function () { // nonexistant character variable - unimplemented
        self.fCastRecover = self.charObj.fCastRecover;
    }
    this.fCastRecover.Set = function (newStr) {
        self.charObj.fCastRecover = newStr;
    }
    this.fCastRecover.Increment = function (valuePlus) {
        self.fCastRecover = self.charObj.fCastRecover;
        self.fCastRecover += valuePlus;
    }
    this.fCastRecover.Decrement = function (valueMinus) {
        self.fCastRecover = self.charObj.fCastRecover;
        self.fCastRecover -= valueMinus;
    }


    this.maxhp = 0; // the health value variable
    this.MaxHealth = {}; // the health function container
    //  Danger will robinson!!!! pay close attention to the casing!! to keep it in line with the server and to allow for function casing we are using both!
    this.MaxHealth.Get = function () { // Gets the current values from the character object and sets them to JS object
        self.maxhp = self.charObj.maxhp;
    }
    this.MaxHealth.Set = function () { // updates character object with JS object changes
        self.charObj.maxhp = self.maxhp;
    }
    this.MaxHealth.Increment = function (valuePlus) { // increments value in JS object (defaults to 1)
        self.MaxHealth.Get();
        self.maxhp += (typeof valuePlus == "undefined" ? 1 : parseInt(valuePlus));
        self.MaxHealth.Set();
    }
    this.MaxHealth.Decrement = function (valueMinus) { // decrements value in JS object (defaults to 1)
        self.MaxHealth.Get();
        self.maxhp -= (typeof valueMinus == "undefined" ? 1 : parseInt(valueMinus));
        self.MaxHealth.Set();
    }


    this.maxmana = 0; // the health value variable
    this.MaxMana = {}; // the health function container
    //  Danger will robinson!!!! pay close attention to the casing!! to keep it in line with the server and to allow for function casing we are using both!
    this.MaxMana.Get = function () { // Gets the current values from the character object and sets them to JS object
        self.maxmana = self.charObj.maxhp;
    }
    this.MaxMana.Set = function () { // updates character object with JS object changes
        self.charObj.maxmana = self.maxmana;
    }
    this.MaxMana.Increment = function (valuePlus) { // increments value in JS object (defaults to 1)
        self.MaxMana.Get();
        self.maxmana += (typeof valuePlus == "undefined" ? 1 : parseInt(valuePlus));
        self.MaxMana.Set();
    }
    this.MaxMana.Decrement = function (valueMinus) { // decrements value in JS object (defaults to 1)
        self.MaxMana.Get();
        self.maxmana -= (typeof valueMinus == "undefined" ? 1 : parseInt(valueMinus));
        self.MaxMana.Set();
    }


    this.maxstamina = 0; // the health value variable
    this.MaxStam = {}; // the health function container
    //  Danger will robinson!!!! pay close attention to the casing!! to keep it in line with the server and to allow for function casing we are using both!
    this.MaxStam.Get = function () { // Gets the current values from the character object and sets them to JS object
        self.maxstamina = self.charObj.maxstamina;
    }
    this.MaxStam.Set = function () { // updates character object with JS object changes
        self.charObj.maxstamina = self.maxstamina;
    }
    this.MaxStam.Increment = function (valuePlus) { // increments value in JS object (defaults to 1)
        self.MaxStam.Get();
        self.maxstamina += (typeof valuePlus == "undefined" ? 1 : parseInt(valuePlus));
        self.MaxStam.Set();
    }
    this.MaxStam.Decrement = function (valueMinus) { // decrements value in JS object (defaults to 1)
        self.MaxStam.Get();
        self.maxstamina -= (typeof valueMinus == "undefined" ? 1 : parseInt(valueMinus));
        self.MaxStam.Set();
    }

    this.healthRegen = 0;
    this.healthRegen.Get = function () { // nonexistant character variable - unimplemented
        self.healthRegen = self.charObj.healthRegen;
    }
    this.healthRegen.Set = function (newStr) {
        self.charObj.healthRegen = newStr;
    }
    this.healthRegen.Increment = function (valuePlus) {
        self.healthRegen = self.charObj.healthRegen;
        self.healthRegen += valuePlus;
    }
    this.healthRegen.Decrement = function (valueMinus) {
        self.healthRegen = self.charObj.healthRegen;
        self.healthRegen -= valueMinus;
    }


    this.manaRegen = 0;
    this.manaRegen.Get = function () { // nonexistant character variable - unimplemented
        self.manaRegen = self.charObj.manaRegen;
    }
    this.manaRegen.Set = function (newStr) {
        self.charObj.manaRegen = newStr;
    }
    this.manaRegen.Increment = function (valuePlus) {
        self.manaRegen = self.charObj.manaRegen;
        self.manaRegen += valuePlus;
    }
    this.manaRegen.Decrement = function (valueMinus) {
        self.manaRegen = self.charObj.manaRegen;
        self.manaRegen -= valueMinus;
    }


    this.stamRegen = 0;
    this.stamRegen.Get = function () { // nonexistant character variable - unimplemented
        self.stamRegen = self.charObj.stamRegen;
    }
    this.stamRegen.Set = function (newStr) {
        self.charObj.stamRegen = newStr;
    }
    this.stamRegen.Increment = function (valuePlus) {
        self.stamRegen = self.charObj.stamRegen;
        self.stamRegen += valuePlus;
    }
    this.stamRegen.Decrement = function (valueMinus) {
        self.stamRegen = self.charObj.stamRegen;
        self.stamRegen -= valueMinus;
    }


    this.resistAllElem = 0;
    this.resistAllElem.Get = function () { // nonexistant character variable - unimplemented
        self.resistAllElem = self.charObj.resistAllElem;
    }
    this.resistAllElem.Set = function (newStr) {
        self.charObj.resistAllElem = newStr;
    }
    this.resistAllElem.Increment = function (valuePlus) {
        self.resistAllElem = self.charObj.resistAllElem;
        self.resistAllElem += valuePlus;
    }
    this.resistAllElem.Decrement = function (valueMinus) {
        self.resistAllElem = self.charObj.resistAllElem;
        self.resistAllElem -= valueMinus;
    }


    this.physDamReduct = 0;
    this.physDamReduct.Get = function () { // nonexistant character variable - unimplemented
        self.physDamReduct = self.charObj.physDamReduct;
    }
    this.physDamReduct.Set = function (newStr) {
        self.charObj.physDamReduct = newStr;
    }
    this.physDamReduct.Increment = function (valuePlus) {
        self.physDamReduct = self.charObj.physDamReduct;
        self.physDamReduct += valuePlus;
    }
    this.physDamReduct.Decrement = function (valueMinus) {
        self.physDamReduct = self.charObj.physDamReduct;
        self.physDamReduct -= valueMinus;
    }

    this.goldFind = 0;
    this.goldFind.Get = function () { // nonexistant character variable - unimplemented
        self.goldFind = self.charObj.goldFind;
    }
    this.goldFind.Set = function (newStr) {
        self.charObj.goldFind = newStr;
    }
    this.goldFind.Increment = function (valuePlus) {
        self.goldFind = self.charObj.goldFind;
        self.goldFind += valuePlus;
    }
    this.goldFind.Decrement = function (valueMinus) {
        self.goldFind = self.charObj.goldFind;
        self.goldFind -= valueMinus;
    }


    this.GetFlags = function (string) {
        bits = parseInt(string);
        return [
            bits & 1, bits >>> 1 & 1, bits >>> 2 & 1, bits >>> 3 & 1, bits >>> 4 & 1, bits >>> 5 & 1, bits >>> 6 & 1, bits >>> 7 & 1,
            bits >>> 8 & 1, bits >>> 9 & 1, bits >>> 10 & 1, bits >>> 11 & 1, bits >>> 12 & 1, bits >>> 13 & 1, bits >>> 14 & 1, bits >>> 15 & 1, bits >>> 16 & 1
        ]
    }

    this.GetItemBuffs = function () {
        var flags = self.GetFlags(self.itemObj.GetTag("statBonusFlags").toString()); //[1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536]
        //self.SerializeTag(self.itemObj);
        var buffString = self.itemObj.GetTag("buffList").toString();
        Console.Print("buffList [" + buffString + "]\n");
        return self.DeserializeTag(buffString);
    }

    this.SerializeTag = function (myObject) { // for debug quick setting the buffs tag
        var buffString = "1,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0"
        myObject.SetTag("buffList", buffString);
    }
    this.DeserializeTag = function (tagString) {
        if (typeof tagString != "undefined") {
            var buffArray = tagString.split(",");
            return buffArray
        } else {
            Console.Print("Magic Item Script - Deserialize passed undefined variable instead of string.\n")
           return false;
         }
    }

    this.OneOfUs = function () {
        myObj = self.itemObj;
        //search for other items with script "5052"
        for (i = 0; i != 25; i++) {
            mItem = self.charObj.FindItemLayer(i);
            if (ValidateObject(mItem) && i != 15 && i != 16 && i != 21) {
                l = mItem.scriptTriggers.length;
                for (j = 0; j != l; j++) {
                    if (mItem.scriptTriggers[j] == 5052 && mItem.serial != myObj.serial) {
                        Console.Print("found one!\n");
                        return true;
                    }
                }

            }
        }
        return false; //no other items with this script exist in equipment list
    }

    this.NewLoot = function () {
        Console.Print("Running New Loot function.\n");

            var flags = 0;
            var bitTotals = 0;
            var buffList = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
            var availablePowers = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768];
            var availablePotency = [5, 5, 5, 500, 0, 0, 25, 25, 25, 0, 0, 0, 0, 0, 0, 0];
            var maxPowers = 5; // Maximum number of powers to apply
            var probabilityMultiplier = 1; // Initial probability multiplier

            // Generate random values for flags and buffList
            for (var i = 0; i < maxPowers; i++) {
                var randomIndex = Math.floor(Math.random() * availablePowers.length);
                var randomPower = availablePowers.splice(randomIndex, 1)[0];

                var randomPotency = Math.round(Math.random() * availablePotency[randomIndex]);
                buffList[randomIndex]=randomPotency;
                bitTotals += randomPower;
                Console.Print(bitTotals.toString() + "\n");

                // Reduce the probability for the next power
                probabilityMultiplier *= 0.8; // Adjust the diminishing factor as desired

                // Adjust the availablePowers array based on the reduced probability
                availablePowers = availablePowers.filter(function (power) {
                    return Math.random() < probabilityMultiplier;
                });
            }

            // Set the flags and buffList
            self.itemObj.SetTag("statBonusFlags", bitTotals.toString());
            self.itemObj.SetTag("buffList", buffList.join(","));
       
    }



}

// character stuff for when the character processes the buffs after the script is applied to them.
function onSpellCast(pUser, SpellID) { // add faster casting code here
    return 500;
}

function onDamage(pAttacked, pAttacker, dValue, dType) {
    var myThorns = parseInt(pAttacked.GetTag("thorns").toString());   // thorns
    if (ValidateObject(pAttacker)) {
        pAttacker.Damage((myThorns > 0 ? ((RandomInt(pAttacked.hidamage - pAttacked.lodamage) + pAttacked.loDamage) * .01) * myThorns : 0), 0, pAttacked, false); //check this math.
    }
    var mydReflect = parseInt(pAttacked.GetTag("dReflect").toString()); //damage Reflection
    if (ValidateObject(pAttacker)) {
        pAttacker.Damage((mydReflect > 0 ? (dValue * .01) * myThorns : 0), 0, pAttacked, false);
    }

}

function onDeathBlow(pKilled, pKiller) {
    return false; //debug immortality.
}

// myChar.SetTimer( timer.SPELLRECOVERYTIME, 500 );
function RandomInt(rvalue) {
    //returns random integer with maximum given
    return Math.floor(Math.random() * rvalue);
}
function _restorecontext_() { }
These users thanked the author Humility for the post:
Xuri
Humility
UOX3 Neophyte
Posts: 28
Joined: Mon Nov 21, 2016 7:51 am
Has thanked: 4 times
Been thanked: 5 times

Post by Humility »

I made some improvements to the script but some of it required Xuri fixing some minor issues with the server.
Without the latest rc6 fixes, this script still works just fine but will cause some issues with tweak if the admin or a gm has one of these items equipped, for that admin or GM.

The updated magic Item script:

Code: Select all

// Stat-boosting Equipment  Humility, 7-5-2023

//known issue: for servers before the current PR 99 rC6
// if an admin equips this item, while equipped, tweak won't work for them.
// the onNameRequest function causes a loss of context that can't be corrected without the updated server.
// this is basically an admin item testing problem.

//const web = ["<BASEFONT COLOR=#", "</BASEFONT>", "<B>", "</B>", "<BIG>", "</BIG>", "<SMALL>", "</SMALL>", "<EM>", "</EM>", "<I>", "</I>", "<U>", "</U>", "<H1>", "</H1>", "<H2>", "</H2>", "<H3>", "</H3>", "<H4>", "</H4>", "<H5>", "</H5>", "<H6>", "</H6>",
//    "<a href=\"\">", "</a>", "<div align=\"right\">", "<div align=\"left\">", "</DIV>", "<left>", "</left>", "<P>", "<CENTER>", "</CENTER>", "<BR>", "</BR>"]
//const fColor = ["008000", "FF0000", "D0D0F0", "FFD700", "00BFFF"]; // buffgreen, debuffRed, statWhite, flavahGold, magicBlue
//const fSz = [" size=1", " size=2", " size=3", " size=4", " size=5", " size=6", " size=7"]
function onEquip(pEquipper, iEquipped) {
    //pEquipper.SetTimer(Timer.SPELLTIME, 0) //buggy spellcasting somewhere, i needed a way to clear the timer
    var buffMan = new characterBuffInterface(pEquipper, iEquipped); // Instantiate interface object
    if (iEquipped.GetTag("statBonusFlags") < 1) {
        buffMan.NewLoot();
    }
    var ItemBuffArray = buffMan.GetItemBuffs();
    buffMan.Strength.Increment(ItemBuffArray[0]);       // strength
    buffMan.Dexterity.Increment(ItemBuffArray[1]);      // dex
    buffMan.Intelligence.Increment(ItemBuffArray[2]);   // int
    buffMan.Thorns.Increment(ItemBuffArray[3]);         // thorns
    buffMan.MaxHealth.Increment(ItemBuffArray[6]);      // maxhealth
    buffMan.MaxStam.Increment(ItemBuffArray[7]);        // maxstamina
    buffMan.MaxMana.Increment(ItemBuffArray[8]);        // maxmana
    buffMan.charObj.AddScriptTrigger(5052);
    Console.Print(buffMan.charObj.scripttrigger.length);
    return true;
}

function onUnequip(pUnequipper, iUnequipped) {
    var buffMan = new characterBuffInterface(pUnequipper, iUnequipped); // Instantiate interface object
    if (iUnequipped.GetTag("statBonusFlags") < 1) { buffMan.NewLoot(); }
    var ItemBuffArray = buffMan.GetItemBuffs();
    buffMan.Strength.Decrement(ItemBuffArray[0]);       //strength
    buffMan.Dexterity.Decrement(ItemBuffArray[1]);      //dex
    buffMan.Intelligence.Decrement(ItemBuffArray[2]);   //int
    buffMan.Thorns.Decrement(ItemBuffArray[3]);         //thorns
    buffMan.MaxHealth.Decrement(ItemBuffArray[6]);      //maxhealth
    buffMan.MaxStam.Decrement(ItemBuffArray[7]);        //maxstamina
    buffMan.MaxMana.Decrement(ItemBuffArray[8]);        //maxmana
    // check all equipment to see if any of them have script trigger 5052
    if (!buffMan.OneOfUs()) { //no other scripts found with 5052 magic item script remove it from player.
        buffMan.charObj.RemoveScriptTrigger(5052);
    }

    return true;
}

function onTooltip(myObj) {
    if (typeof myObj.accountNum != "undefined" || myObj.npc) { return myObj.name; } else {

        var buffMan = new characterBuffInterface(null, myObj); // Instantiate interface object
        if (myObj.GetTag("statBonusFlags") < 1) { buffMan.NewLoot(); }
        var ItemBuffArray = buffMan.GetItemBuffs();
        if (typeof ItemBuffArray == "undefined") { return false; } // missing tags wreaking havoc
        // Setup our template colors
        var buffGreen = "<BASEFONT COLOR=#008000>";
        var debuffRed = "<BASEFONT COLOR=#FF0000>";
        var statWhite = "<BASEFONT COLOR=#D0D0F0 size= 2>";
        var flavorGold = "<BASEFONT COLOR=#FFD700 size= 5 >";
        var clBase = "</BASEFONT>"; // close the basefont tag
        var flags = buffMan.GetFlags(myObj.GetTag("statBonusFlags"));
        var Stats = [
            "Strength", "Dexterity", "Intelligence", "Thorns",
            "Faster Casting", "Faster Cast Recovery", "Health",
            "Stamina", "Mana", "Health Regeneration", "Mana Regeneration",
            "Stamina Regeneration", "Resist All Elements",
            "Damage Reduction", "Luck", "Gold Find"
        ];
        var tooltipText = "";
        Stats.forEach(function (stat, index) {
            if (ItemBuffArray[index] > 0) {
                tooltipText += (parseInt(ItemBuffArray[index]) > 0 ? buffGreen + " " : debuffRed);
                tooltipText += (index > 0 ? "\n+" : "+") + ItemBuffArray[index].toString() + statWhite + " " + stat + clBase; //aesthetics
            }
        });
        var myFlayvah = myObj.GetTag("flavorText");
        if (myFlayvah != "0") { tooltipText += flavorGold + "\n" + myFlayvah + clBase + "\n"; }
        tooltipText += statWhite;

        return tooltipText;
    }
}
//onNameRequest(myObj, requestedBy, requestSource)
//Potential values for requestSource:
//0  - Speech / System Messages
//1  - Guild Menus
//2  - Stat Window(self)
//3  - Stat Window(other)
//4  - Tooltip
//5  - Paperdoll Journal
//6  - Paperdoll
//7  - Single Click / All - Names
//8  - System
//9  - Secure Trade Window
//10 - NRS_Script
function onNameRequest(myObj, requestedBy, requestSource) {
    //Console.Print("Name Request By:" + requestedBy.toString())
    Console.Print("Name Request Source:" + requestSource.toString())
    switch (requestSource) {
        case 0:
            return "";
        case 1:
            return "";

        case 2:
            if (typeof myObj.accountNum != "undefined" || myObj.npc) {
                return "The Enchanted " + myObj.name;
            } else {
                return "<BASEFONT COLOR=#00BFFF size=7>" + " a Magic " + capitalizeFirstCharacter(myObj.name) + "</BASEFONT>"; //magic item blue
            }
            break;
        case 3:
            return "Case 3:";

        case 4:
            if (typeof myObj.accountNum != "undefined" || myObj.npc) {
                return "The Enchanted " + myObj.name;
            } else {
               return "<BASEFONT COLOR=#00BFFF size=7>" + capitalizeFirstCharacter(myObj.name) + "</BASEFONT>"; //magic item blue
            }
            break;

        case 5:
            return "";
        case 6:
            return "";
       case 7:
            return ""
            break;
        case 8:
            return "";
        case 9:
            return "";

        case 10:
            if (typeof myObj.accountNum != "undefined" || myObj.npc) {
                return "The Enchanted " + myObj.name;
            } else {
                return "<BASEFONT COLOR=#00BFFF size=7>" + capitalizeFirstCharacter(myObj.name) + "</BASEFONT>"; //magic item blue
            }
            break;
        default:
    return "";
            break;

    }

    return "";

}

//o------------------------------------------------------------------------------------------------o
//|	Function	-   characterBuffInterface(buffChar)
//|	Date		-	06/29/2023
//o------------------------------------------------------------------------------------------------o
//|	Purpose		-   Constructor to supply API functions and interface to apply complex equipment buffs
//|	Changes		-
//o------------------------------------------------------------------------------------------------o

function characterBuffInterface(pChar, myObj) {
    var self = this;
    this.itemObj = myObj;
    this.charObj = pChar;

    // Create value objects
    this.Strength = createValueObject("Strength", "strength");
    this.Dexterity = createValueObject("Dexterity", "dexterity");
    this.Intelligence = createValueObject("Intelligence", "intelligence");
    this.Thorns = createValueObject("Thorns", "thorns");
    this.FCasting = createValueObject("FCasting", "fCasting");
    this.FCastRecovery = createValueObject("FCastRecovery", "fCastRecovery");
    this.MaxHealth = createValueObject("MaxHealth", "maxhealth");
    this.MaxStam = createValueObject("MaxStam", "maxstamina");
    this.MaxMana = createValueObject("MaxMana", "maxmana");
    this.HealthRegen = createValueObject("HealthRegen", "healthRegen");
    this.ManaRegen = createValueObject("ManaRegen", "manaRegen");
    this.StamRegen = createValueObject("StamRegen", "stamRegen");
    this.EResist = createValueObject("EResist", "eresist");
    this.DReduction = createValueObject("DReduction", "dRed");
    this.Luck = createValueObject("Luck", "luck");
    this.GFind = createValueObject("GFind", "gFind");

    // Function to create value object and related functions
    function createValueObject(valueName, sKey) {
        var value = 0; // The value variable
        var valueObject = {}; // The value function container

        // Getter function
        valueObject.Get = function (myKey) {
            if (typeof self.charObj[myKey] != "undefined") {
                // Access the value from the object variable
                value = self.charObj[myKey];
            } else {
                // Access the value from the tag function return
                value = parseInt(self.charObj.GetTag(myKey));
            }
        };

        // Setter function
        valueObject.Set = function (myKey) {
            if (typeof self.charObj[myKey] != "undefined") {
                // Update the value in the object variable
                self.charObj[myKey] = value;
            } else {
                // Update the value using the tag function
                if (value < 1) {
                    self.charObj.SetTag(myKey, null);
                } else {
                    self.charObj.SetTag(myKey, value.toString());
                }
            }
        };

        // Increment function
        valueObject.Increment = function (valuePlus) {
            valueObject.Get(sKey);
            value += (typeof valuePlus === "undefined" ? 1 : parseInt(valuePlus));
            valueObject.Set(sKey);
        };

        // Decrement function
        valueObject.Decrement = function (valueMinus) {
            valueObject.Get(sKey);
            value -= (typeof valueMinus === "undefined" ? 1 : parseInt(valueMinus));
            valueObject.Set(sKey);
        };


        return valueObject;
    }

    // Create Strength value object
    this.GetFlags = function (string) {
        bits = parseInt(string);
        return [
            bits & 1, bits >>> 1 & 1, bits >>> 2 & 1, bits >>> 3 & 1, bits >>> 4 & 1, bits >>> 5 & 1, bits >>> 6 & 1, bits >>> 7 & 1,
            bits >>> 8 & 1, bits >>> 9 & 1, bits >>> 10 & 1, bits >>> 11 & 1, bits >>> 12 & 1, bits >>> 13 & 1, bits >>> 14 & 1, bits >>> 15 & 1, bits >>> 16 & 1
        ]
    }

    this.GetItemBuffs = function () {
        var flags = self.GetFlags(self.itemObj.GetTag("statBonusFlags").toString()); //[1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536]
        //self.SerializeTag(self.itemObj);
        var buffString = self.itemObj.GetTag("buffList").toString();
        Console.Print("buffList [" + buffString + "]\n");
        return self.DeserializeTag(buffString);
    }

    this.SerializeTag = function (myObject) { // for debug quick setting the buffs tag
        var buffString = "1,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0"
        myObject.SetTag("buffList", buffString);
    }
    this.DeserializeTag = function (tagString) {
        if (typeof tagString != "undefined") {
            var buffArray = tagString.split(",");
            return buffArray
        } else {
            Console.Print("Magic Item Script - Deserialize passed undefined variable instead of string.\n")
            return false;
        }
    }

    this.OneOfUs = function () {
        myObj = self.itemObj;
        //search for other items with script "5052"
        for (i = 0; i != 25; i++) {
            mItem = self.charObj.FindItemLayer(i);
            if (ValidateObject(mItem) && i != 15 && i != 16 && i != 21) {
                l = mItem.scriptTriggers.length;
                for (j = 0; j != l; j++) {
                    if (mItem.scriptTriggers[j] == 5052 && mItem.serial != myObj.serial) {
                        Console.Print("found one!\n");
                        return true;
                    }
                }

            }
        }
        return false; //no other items with this script exist in equipment list
    }

    this.NewLoot = function () {
        Console.Print("Running New Loot function.\n");

        var flags = 0;
        var bitTotals = 0;
        var buffList = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        var availablePowers = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768];
        var availablePotency = [5, 5, 5, 500, 0, 0, 25, 25, 25, 0, 0, 0, 0, 0, 0, 0];
        var maxPowers = 5; // Maximum number of powers to apply
        var probabilityMultiplier = 1; // Initial probability multiplier

        // Generate random values for flags and buffList
        for (var i = 0; i < maxPowers; i++) {
            var randomIndex = Math.floor(Math.random() * availablePowers.length);
            var randomPower = availablePowers.splice(randomIndex, 1)[0];

            var randomPotency = Math.round(Math.random() * availablePotency[randomIndex]);
            buffList[randomIndex] = randomPotency;
            bitTotals += randomPower;
            Console.Print(bitTotals.toString() + "\n");

            // Reduce the probability for the next power
            probabilityMultiplier *= 0.8; // Adjust the diminishing factor as desired

            // Adjust the availablePowers array based on the reduced probability
            availablePowers = availablePowers.filter(function (power) {
                return Math.random() < probabilityMultiplier;
            });
        }

        // Set the flags and buffList
        self.itemObj.SetTag("statBonusFlags", bitTotals.toString());
        self.itemObj.SetTag("buffList", buffList.join(","));
        var newName = "<BASEFONT COLOR=#00BFFF size=7> A magic " + self.itemObj.name + "</BASEFONT>";
        //self.itemObj.name = newName;
    }
    return this;
}

// character stuff for when the character processes the buffs after the script is applied to them.
function onSpellCast(pUser, SpellID) { // add faster casting code here
    var myFCR = parseInt(pUser.GetTag("fCastRecovery").toString());
    myFCR *= 250;
    return 1000 - myFCR;
}

function onDamage(pAttacked, pAttacker, dValue, dType) {
    if (ValidateObject(pAttacked) && ValidateObject(pAttacker)) {
        var baseMaxDmg = 50;
        var thornsCap = 5000;
        var statCap = parseInt(GetServerSetting("STATCAP"));
        Console.Print("statCap: " + statCap + "\n");
        var myThorns = parseInt(pAttacked.GetTag("thorns").toString());
        var stMnAvg = (Math.max(1, pAttacked.strength) + Math.max(1, pAttacked.intelligence) + Math.max(1, pAttacked.dexterity)) / 3;
        var statBoost = Math.max( (15 / statCap) ,stMnAvg / statCap);
        var tDmgAmt = Math.min(50, Math.floor((myThorns / thornsCap) * baseMaxDmg) * .7);
        Console.Print("t Damage Amount: " + tDmgAmt + "\n");
        var sDmgAmt = Math.min(50, Math.floor((statBoost * baseMaxDmg) * .3));
        Console.Print("s Damage Amount: " + sDmgAmt + "\n");
        var dmgAmt = tDmgAmt + sDmgAmt;
        pAttacker.Damage(dmgAmt, 0, pAttacked, false);
    }

    var mydReflect = parseInt(pAttacked.GetTag("dReflect").toString());
    if (ValidateObject(pAttacker)) {
        pAttacker.Damage(mydReflect > 0 ? (dValue * 0.01) * mydReflect : 0, 0, pAttacked, false);
    }
}
function onDeathBlow(pKilled, pKiller) {
    return false; //debug immortality.
}

// myChar.SetTimer( timer.SPELLRECOVERYTIME, 500 );
function RandomInt(rvalue) {
    //returns random integer with maximum given
    return Math.floor(Math.random() * rvalue);
}

function capitalizeFirstCharacter(str) {
    if (str.length === 0) {
        return str; // Return empty string if the input is empty
    } else {
        return str.charAt(0).toUpperCase() + str.slice(1);
    }
}

function _restorecontext_() { }
Post Reply