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