Guest User

Battle INF Script to End All Scripts

a guest
Feb 20th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** ============= Battle INF Script to End All Scripts ==========
  2.  * Authored by Sam 'Halithor' Marquart.
  3.  * Version 1.2.
  4.  * https://github.com/Halithor/BattleINF-Script-To-End-All-Scripts
  5.  * Licenced under MIT (https://opensource.org/licenses/MIT).
  6.  *
  7.  * Features:
  8.  * - Sells items below a threshold.
  9.  * - Crafts new items into equipped items and, optionally, inventory items.
  10.  *      + Inventory items is useful for getting higher rarity gear to be more powerful than current gear
  11.  * - Equips items that are better than currently equipped items.
  12.  * - Sells items that have reached maximum Plus, and aren't better than the currently equipped item of that type.
  13.  *      + Sends items to the market if they're above the threshold.
  14.  * - Dips all of your items into the fountain, automagically, to increase the age modifiers.
  15.  * - Removes duplicate items of a given type, leaving only the strongest in your inventory.
  16.  *
  17.  
  18.  */
  19. (function () {
  20.     items = items || [];
  21.     // Settings for the game. Edit these to fit your character better.
  22.     var settings = {
  23.         sellBelow: 5, // The threshold below which we automatically sell (exclusive), without combining. Increase this as you like.
  24.         keepAbove: 5, // The rarity above which we DO NOT ever auto sell (exclusive).
  25.         sendToMarketAbove: 5, // The rarity above which instead of selling at max, we send the item to the market. OVERRIDES keepAbove
  26.         openInvSlots: 5, // The number of spots that will ALWAYS be held open in your inventory. I recommend setting this to the drops you get per cycle.
  27.         keepAge: 6, // Age level which to NEVER sell above. 6 is two days (Master).
  28.         craftInventory: true, // Whether or not to craft items in your inventory.
  29.         sellDuplicateInventory: false, // If we should sell any duplicates of a given item type in the inventory, and only keep the strongest item. **THIS IS DANGEROUS**.
  30.         forceEquipHighestStrength: false // If you always want to equip the highest rarity, no matter the stats. I recommend this to be true if you don't craft the inventory.
  31.     };
  32.  
  33.     // A list of the  words describing rarities, corresponding to the integer value.
  34.     var _rarities = ["None", "Gray", "Green", "Blue", "Red", "Orange", "Purple", "Teal"];
  35.     // Same as above, but instead provides color codes.
  36.     var _rarityColor = ["white", "#666", "#4CAF50", "#2196F3", "#F44336", "#FF9800", "#9C27B0", "#E91E63"];
  37.     // The words used to describe the different age levels in the game, corresponding by index.
  38.     var _ages = ["Worn", "Fine", "Refined", "Aged", "Exotic", "Famous", "Master", "Heroic", "Ancient", "Fabled", "Ascended", "Legendary", "Eternal"];
  39.     // Time thresholds (in milliseconds) for the different age levels. Again by index.
  40.     var _ageThresholds = [0, 900000, 1800000, 3600000, 7200000, 86400000, 172800000, 345600000, 691200000, 1382400000, 2764800000, 5529600000, 11059200000];
  41.  
  42.     // Posts a message to the user via notifications.
  43.     function postMessage(text) {
  44.         //console.log(text);
  45.         API.notifications.create("" + text, 10);
  46.     }
  47.  
  48.     // String that displays most info about an item.
  49.     function getItemString(item) {
  50.         return '<span style="color: ' + _rarityColor[item.rarity] + ';"><i class="fa fa-star"></i>' + item.rarity + "." + item.mod + " " + _ages[item.ageLevel] + " " + item.name + " +" + item.plus + "</span>";
  51.     }
  52.  
  53.     // Calculates the relative starting strength of an item.
  54.     function getItemStrength(item) {
  55.         return (item.mod * 2) + item.rarity;
  56.     }
  57.  
  58.     // Checks if an item is at the maximum plus value it can reach.
  59.     function isItemAtMaxPlus(item) {
  60.         return item.plus >= (5 + item.rarity * 5);
  61.     }
  62.  
  63.     // Checks to see if two items share the same type and subType.
  64.     function isSameItemType(first, second) {
  65.         return first.type == second.type && first.subType == second.subType
  66.     }
  67.  
  68.     // Checks to see if an item is equipped.
  69.     function isItemEquipped(item) {
  70.         var equipment = ScriptAPI.$user.character.equipment;
  71.         for (var i in equipment) {
  72.             if (equipment[i].id == item.id) {
  73.                 return true;
  74.             }
  75.         }
  76.         return false;
  77.     }
  78.  
  79.     // Checks if the first item is stronger than the second, based on getItemStrength(), then a bunch of other stuff.
  80.     function isItemStronger(first, second) {
  81.         if (getItemStrength(first) == getItemStrength(second)) {
  82.             if (first.plus == first.plus) {
  83.                 if (first.ageLevel == second.ageLevel) {
  84.                     // At this point they're pretty much the same damn item. Compare them based on actual stats. Will return false if different types.
  85.                     return isItemBetter(first, second);
  86.                 } else {
  87.                     return first.ageLevel > second.ageLevel;
  88.                 }
  89.             } else {
  90.                 return first.plus > second.plus;
  91.             }
  92.         } else {
  93.             return getItemStrength(first) > getItemStrength(second);
  94.         }
  95.     }
  96.  
  97.     // Returns if two items are compatible for crafting.
  98.     function canCraftItems(first, second) {
  99.         return isSameItemType(first, second)
  100.             && first.rarity == second.rarity
  101.             && first.mod == second.mod;
  102.     }
  103.  
  104.     // Sends an item to the market screen.
  105.     function sendItemToMarket(item) {
  106.         API.market.addToMarket(item);
  107.     }
  108.  
  109.     // Calculate the item age level. Used to make sure selling is safe, even if you haven't dipped everything.
  110.     function getItemAgeLevel(item) {
  111.         var now = Math.round(new Date().getTime());
  112.         var diff = now - item.ts;
  113.         var i = 0;
  114.         while (diff > _ageThresholds[i] && i < _ageThresholds.length) {
  115.             i++;
  116.         }
  117.         return i - 1; // Off by one based on the iteration.
  118.     }
  119.  
  120.     // Find all options for crafting.
  121.     function findCraftingCandidates(items, secondary) {
  122.         var candidates = [];
  123.         for (var j = 0; j < items.length; j++) {
  124.             var candidate = items[j];
  125.             if (canCraftItems(candidate, secondary) && !isItemAtMaxPlus(candidate)) {
  126.                 candidates.push(candidate);
  127.             }
  128.         }
  129.         return candidates;
  130.     }
  131.  
  132.     // Given a candidate list, craft the candidates. Attempts to craft each option on the list, until it finds one
  133.     // that's successful, or runs out of candidates. Designed to still craft when there's items stuck below max plus
  134.     // value (due to Impurity).
  135.     function craftCandidates(candidates, item) {
  136.         var canIndex = 0;
  137.  
  138.         function callback(data) {
  139.             if (!data.success) {
  140.                 canIndex++;
  141.                 if (canIndex < candidates.length) {
  142.                     API.inventory.craft(candidates[canIndex], item, callback);
  143.                 }
  144.                 return;
  145.             }
  146.  
  147.             var newItem = data.newItem;
  148.  
  149.             postMessage("<b>Crafting:</b> " + getItemString(newItem));
  150.  
  151.             if (!isItemEquipped(newItem)) {
  152.                 var unequippedItem = equipIfBetter(newItem);
  153.                 // If we changed equipment, try to sell the un-equipped item.
  154.                 if (unequippedItem) {
  155.                     sellIfMax(unequippedItem);
  156.                 } else {
  157.                     sellIfMax(newItem);
  158.                 }
  159.             }
  160.         }
  161.  
  162.         API.inventory.craft(candidates[canIndex], item, callback);
  163.     }
  164.  
  165.     // Less picky than find primary craft. Used for inventory duplicates.
  166.     function findDuplicateTypes(items, item) {
  167.         var duplicates = [];
  168.         for (var j = 0; j < items.length; j++) {
  169.             var candidate = items[j];
  170.             if (isSameItemType(item, candidate)) {
  171.                 duplicates.push(candidate);
  172.             }
  173.         }
  174.         if (duplicates.length > 0) {
  175.             return duplicates;
  176.         }
  177.         return undefined;
  178.     }
  179.  
  180.     // Will the additional items push the inventory to full? Defaults to the setting if not provided a number.
  181.     function getInventoryFull(itemsLeft) {
  182.         if (!itemsLeft) {
  183.             itemsLeft = settings.openInvSlots;
  184.         }
  185.         var val = (ScriptAPI.$user.inventory.items.length + itemsLeft >= ScriptAPI.$user.upgrades.inventoryMax.value);
  186.         return val;
  187.     }
  188.  
  189.     // Gets the user's inventory, sorted by strength, plus, then age level.
  190.     function getSortedInventory() {
  191.         return ScriptAPI.$user.inventory.items.sort(function (a, b) {
  192.             if (getItemStrength(a) == getItemStrength(b)) {
  193.                 if (a.plus == b.plus) {
  194.                     if (a.ageLevel == b.ageLevel) {
  195.                         return 0;
  196.                     } else {
  197.                         return a.ageLevel > b.ageLevel ? -1 : 1;
  198.                     }
  199.                 } else {
  200.                     return a.plus > b.plus ? -1 : 1;
  201.                 }
  202.             } else {
  203.                 return getItemStrength(a) > getItemStrength(b) ? -1 : 1;
  204.             }
  205.         });
  206.     }
  207.  
  208.  
  209.     // Find the item equipped matching the given item.
  210.     function findEquipped(primary) {
  211.         var equipment = ScriptAPI.$user.character.equipment;
  212.         for (var i in equipment) {
  213.             var candidate = equipment[i];
  214.             if (isSameItemType(primary, candidate)) {
  215.                 return candidate;
  216.             }
  217.         }
  218.         return undefined;
  219.     }
  220.  
  221.     // Returns true if first is better than second. False otherwise.
  222.     // Does not care about rarity, age, etc. Purely based on stats.
  223.     // This is one of the things you can change to value items differently!
  224.     function isItemBetter(first, second) {
  225.         if (!isSameItemType(first, second)) {
  226.             return false; // Don't even compare if not the same.
  227.         }
  228.  
  229.         if (first.type == "weapon") {
  230.             // Damage consideration
  231.             var damageFirst = (first.stats.attackMax + first.stats.attackMin) / 2;
  232.             var damageSecond = (second.stats.attackMax + second.stats.attackMin) / 2;
  233.             var damageRatio = damageSecond > 0 ? damageFirst / damageSecond : 1.0;
  234.             // Overkill consideration
  235.             var overkillDiff = first.stats.overkill - second.stats.overkill;
  236.             // Healing ratio.
  237.             var healRatio = second.stats.heal > 0 ? (first.stats.heal / second.stats.heal) : 1.0;
  238.             healRatio = healRatio == 0 ? 1.0 : healRatio;
  239.  
  240.             var armorRatio = second.stats.defense > 0 ? first.stats.defense / second.stats.defense : 1.0;
  241.             if (first.stats.defense == 0 || second.stats.defense == 0) {
  242.                 armorRatio = 1.0; // Don't compare on something one item doesn't have.
  243.             }
  244.  
  245.             var hpBonusDiff = (first.stats.hpBonus - second.stats.hpBonus);
  246.  
  247.             var weaponValue = damageRatio + healRatio + armorRatio + (overkillDiff / 15) + (hpBonusDiff / 50);
  248.             return weaponValue > 3.0;
  249.         } else {
  250.             // Everything that's not a weapon is armor.
  251.             var armorRatio = first.stats.defense / second.stats.defense;
  252.             var hpBonusDiff = (first.stats.hpBonus - second.stats.hpBonus);
  253.  
  254.             var armorValue = armorRatio + (hpBonusDiff / 50);
  255.             return armorValue > 1.0;
  256.         }
  257.     }
  258.  
  259.  
  260.     // Sells an item, only checking age and lock status.
  261.     function sellItem(item) {
  262.         if (getItemAgeLevel(item) < settings.keepAge && !item.lock) {
  263.             //postMessage("Selling " + getItemString(item));
  264.             API.inventory.sell(item);
  265.         }
  266.     }
  267.  
  268.     // Sells or Markets an item, only if it's at max level.
  269.     function sellIfMax(item) {
  270.         if (isItemAtMaxPlus(item)) {
  271.             if (item.rarity > settings.sendToMarketAbove) {
  272.                 postMessage("<b>Market Max:</b> " + getItemString(item));
  273.                 sendItemToMarket(item);
  274.             } else if (item.rarity <= settings.keepAbove) {
  275.                 postMessage("<b>Selling Max:</b> " + getItemString(item));
  276.                 sellItem(item);
  277.             }
  278.             return true;
  279.         }
  280.         return false;
  281.     }
  282.  
  283.     // Sells the inventory item in the last occupied location, sorted by strength.
  284.     function sellLastInventoryItem() {
  285.         var item = getSortedInventory()[ScriptAPI.$user.upgrades.inventoryMax.value - 1];
  286.         if (item) {
  287.             sellItem(item);
  288.         }
  289.     }
  290.  
  291.     // Similar to above, but forces the spots open based on the setting
  292.     function openLastInventorySpots() {
  293.         var inv = getSortedInventory();
  294.         for (var i = 0; i < settings.openInvSlots; i++) {
  295.             var item = inv[ScriptAPI.$user.upgrades.inventoryMax.value - 1 - i];
  296.             if (item) {
  297.                 sellItem(item);
  298.             }
  299.         }
  300.     }
  301.  
  302.     // Tries to equip the item if better. Returns the unequiped item if it works, false otherwise.
  303.     function equipIfBetter(item, callback) {
  304.         var equipped = findEquipped(item);
  305.  
  306.         if (equipped && (isItemBetter(item, equipped) || (settings.forceEquipHighestStrength && isItemStronger(item, equipped)) )) {
  307.             postMessage("<b>Equip:</b> " + equipped.name + " to " + getItemString(item));
  308.             API.inventory.unequip(equipped, function() {
  309.                 API.inventory.equip(item);
  310.             });
  311.  
  312.             return equipped;
  313.         }
  314.         if (callback) {
  315.             callback(false);
  316.         }
  317.         return false;
  318.     }
  319.  
  320.     // Get a sorted list of the inventory items.
  321.     var inventory = getSortedInventory();
  322.     // Get the equipment
  323.     var equipment = ScriptAPI.$user.character.equipment;
  324.     // Iterate through each new item, trying to craft it, or handling it otherwise.
  325.     items.forEach(function (item) {
  326.  
  327.         if (item.rarity < settings.sellBelow && !(item.rarity > settings.keepAbove)) {
  328.             sellItem(item);
  329.             return;
  330.         }
  331.  
  332.         // Now we want to combine the item with the inventory into more powerful items. First, we want to upgrade your currently
  333.         // equipped items fully. Then we want to try to combine between items in your inventory. After combining, we look to see
  334.         // if any of the items we have are better than equipped, if so, switch them. Finally, if anything has
  335.         // reached the max Plus value that it can, and it isn't equipped,  sell it.
  336.  
  337.         // Find a good candidate to craft this item into.
  338.         var isEquipped = false;
  339.         var candidates = findCraftingCandidates(equipment, item);
  340.  
  341.         if (settings.craftInventory) {
  342.             candidates = candidates.concat(findCraftingCandidates(inventory, item));
  343.         }
  344.  
  345.         if (candidates.length > 0) {
  346.             // We can craft this into something.
  347.             craftCandidates(candidates, item);
  348.         } else {
  349.             // Deal with the new item.
  350.  
  351.             // First try to equip it.
  352.             var unequippedItem = equipIfBetter(item);
  353.             if (unequippedItem) {
  354.                 sellIfMax(unequippedItem);
  355.             }
  356.             // Now look at the inventory and remove any duplicates.
  357.             if (settings.sellDuplicateInventory) {
  358.                 // Use the unequipped item for the duplicates.
  359.                 if (unequippedItem) {
  360.                     item = unequippedItem;
  361.                 }
  362.                 // Remove all duplicates in the inventory, keeping only the strongest.
  363.                 var duplicates = findDuplicateTypes(inventory, item);
  364.                 var tempItem = item;
  365.                 if (duplicates) {
  366.                     for (var i = 0; i < duplicates.length; i++) {
  367.                         var dup = duplicates[i];
  368.                         if (isItemStronger(item, dup)) {
  369.                             sellItem(dup);
  370.                         } else {
  371.                             sellItem(tempItem);
  372.                             tempItem = dup;
  373.                         }
  374.                     }
  375.                 }
  376.             }
  377.         }
  378.     });
  379.  
  380.     // Given our current inventory, sell stuff to keep some slots open. Hopefully, these slots will
  381.     if (getInventoryFull()) {
  382.         openLastInventorySpots();
  383.     }
  384.  
  385.     var now = Math.floor(new Date().getTime());
  386.  
  387.     // Used to reduce the number of AgeUp calls made to the server.
  388.     function checkAgeAndAgeUp(item) {
  389.         if ((now - item.ts) > _ageThresholds[item.ageLevel + 1]) {
  390.             ScriptAPI.$craftingService.ageUpItem(item);
  391.         }
  392.     }
  393.  
  394.     // Age up everything that we own, if it's ready..
  395.     inventory.forEach(checkAgeAndAgeUp);
  396.     equipment.forEach(checkAgeAndAgeUp);
  397.  
  398. }());
Add Comment
Please, Sign In to add comment