Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //console.log(user.data);
- /********************************\
- ,' GHAZZ' AUTOCRAFT SCRIPT! '.
- ( Tries to be smart! )
- `. Deletes "bad" items! .'
- ) Autocrafts! (
- ,' Upgrades Inventory size! '.
- ( Autoequips better items! )
- `. Tries to handle Dupes! .'
- \********************************/
- // These values are here for your convenience.
- //var wantedSpace = 5; //The script will try to free up at least this much space when scrapping.
- var wantedSpace = Math.floor(user.data.properties.maxInventory.parts / 4.5); // autosetting
- var dumpQuality = -1; // this quality gets scrapped on drop. -1 to disable.
- var sellQuality = 2; // this quality and lower gets sold automatically
- var minQuality = 0; // this quality and lower get ignored as crafting targets.
- var autoRemoveInferior = true; // TODO: remove items with lower potential than equipped items
- //var autoMinMod = false; // TODO remove all items of same type with lower mod/stats when considering deletes.
- //var minMod = 0; // TODO remove all items of lower mod value than this when selling.
- // These arrays are what quality primary will be targeted when consuming each quality.
- // The first value is 0*, second is 1* etc.
- var craftInventory = [4, 4, 5, 5, 6, 6, 7, 7]; // Default, Ghazz' choice.
- //var craftInventory = [0, 1, 2, 3, 4, 5, 6, 7]; // only use same quality.
- //var craftInventory = [1, 2, 3, 4, 5, 6, 7, 7]; // use quality-1 to craft upgrades
- //var craftInventory = [3, 3, 3, 3, 4, 6, 7, 7]; // mid tier focus
- //var craftInventory = [4, 4, 4, 4, 6, 7, 7, 7]; // greed, greed, greed!
- //var craftInventory = [4, 4, 4, 5, 6, 7, 7, 7]; // use quality-2 to craft upgrades
- //var craftInventory = [-1, 4, 4, 4, 6, 7, 7, 7]; // testing, testing.
- //var craftInventory = [2, 7, 7, 7, 7, 7, 7, 7]; // Just upgrade it all.
- // These item types will be autosold.
- var unwantedParts = ['barrelClip', 'barrelExtender', 'barrelSplitterTwo', 'clip', 'shieldRing', 'stock', 'uClip'];
- // uncomment next line to keep all parts.
- var unwantedParts = [];
- // code starts here
- var ourItems = user.data.inventory.parts; // get our inventory
- function statSum(stats) { // just lump it all together, and oh yeah, reverse weights weight.
- var sumRet = 0;
- for (var tk in stats) {
- if (tk != 'weight') sumRet += stats[tk];
- else sumRet -= stats[tk];
- }
- return sumRet;
- } //TODO: Fix this to do smarter things.
- function statCompare(a, b) {
- return statSum(a.stats) - statSum(b.stats);
- }
- // Thanks to Grote for helping me clean up this function (I have since messed it up again)
- // His rewrite inspired how a lot of the rest of the script is built.
- function commonCleanup(neededSlots) { // Deletes items, tries to be sensible with it.
- var soldItemIds = []; // functions, the poor mans class
- var soldValue = 0;
- var soldQuality = 0;
- function deletePart(part) {
- if (!(soldItemIds.includes(part.id))) { //not sold
- soldValue += part.value; //count our money
- soldQuality = Math.max(part.quality,soldQuality); // find highest quality sold
- soldItemIds.push(part.id); // put the item on the list.
- return true;
- } else return false;
- }
- if (autoRemoveInferior) {
- /*
- pick a random part. get a list of all items with its type. count number equipped.
- if we have more than are equipped, and more than one if not, delete the item with lowest .original stat
- */
- var suspect = randitem(user.data.inventory.parts);
- }
- quality = -1;
- while (quality++ < 7 && neededSlots > 0) {
- //get an easy to use reference to the inventory, containing only objects of current rarity
- var ourItems = user.data.inventory.parts.filter(function (item) { return item.quality <= quality; });
- //Delete all items that are below quality or not on the list. //no break statement becasue you want to lose them anyway
- for (var item of ourItems) {
- if ((item.quality < sellQuality
- || unwantedParts.includes(item.type)
- ) && deletePart(item)) {
- neededSlots--;
- ourItems.splice(ourItems.indexOf(item), 1);
- }
- }
- //Delete all 0 mod items untill we reach the needed Slots
- for(var item of ourItems) {
- if (neededSlots <= 0) break;
- if (item.plus == 0 && deletePart(item)) {
- neededSlots--;
- ourItems.splice(ourItems.indexOf(item), 1);
- }
- }
- //Delete the cheapest item of the current rarity untill you hit the neededSlots
- while (ourItems.length > 0 && neededSlots > 0) { //also check there are still more items
- var partToDelete = ourItems.reduce(function (lowest, highest) { return lowest.value < highest.value ? lowest : highest; });
- if (deletePart(partToDelete)) neededSlots--;
- ourItems.splice(ourItems.indexOf(partToDelete), 1);
- }
- }
- inventoryLib.scrapSelected(soldItemIds); // sell all the items.
- inventoryLib.get(); // get a new equipment list
- systemLog.push('Sold <b class="rarity-' + soldQuality + '-text">' + soldItemIds.length + ' parts</b> for <b>' + soldValue + '</b> credits.'); //tell them.
- }
- //--------------//
- // //
- // Crafting //
- // //
- //--------------//
- //"should" are used by array.filter.
- function shouldCraft(primary, consumed, craftGrid) {
- return (primary.id != consumed.id // not same item
- && primary.type == consumed.type // same type
- && primary.mod == consumed.mod // mod value is compatible.
- && primary.quality >= consumed.quality // primary is not lower quality
- && primary.quality != 0 // never upgrade greys
- && consumed.plus == 0 // consuming an item with plus is not optimal.
- && primary.plus < (primary.quality + 1) * 5 // not fully upgraded
- && primary.quality <= crafting.limits.quality //we have enough factories to craft this
- && craftGrid[consumed.quality] >= primary.quality //check that we are within crafting limits
- );
- }
- function shouldSwap(current, candidate, minq, maxq) {
- // console.log(candidate.quality,minq, candidate.quality <= (minq + 2), candidate.quality >= minq, candidate.quality >= minq, candidate.type == current.type && statSum(candidate) > statSum(current));
- return (candidate.quality <= (minq + 2) //check that we are within two of minimum quality
- && candidate.quality >= (maxq - 2) // and also that we are within two of max
- && candidate.type == current.type // same type
- && statSum(candidate.stats) >= statSum(current.stats) // candidate is "better"
- ); //TODO: check weight.
- } //TODO: should check ".original", to see if this item has better potential.
- //"best" and "worst" are used by array.reduce.
- function bestCraft(first, second) {// we choose the first item if it is
- return (first.quality >= second.quality //same or better quality
- && first.plus >= second.plus // same or better upgrades
- && statSum(first) > statSum(second) // "better" stats
- ) ? first : second; // return the chosen object
- }
- function worstStats(first, second) {
- return (statSum(second.part) - statSum(first.part)) ? first : second;
- }
- function updateEquipList(item, list) {
- // find max and min qualities.
- var minQ = 8;
- var maxQ = 0;
- for (part of user.data.characters[0].constructions[list].parts) { //go through our list and find stuff...
- if (part.part.quality < minQ) minQ = part.part.quality; // minimum quality
- if (part.part.quality > maxQ) maxQ = part.part.quality; // maximum quality
- if (part.part.id == item.id) { // Check for dupes....
- console.log("found dupe ", item); // Tell them.
- //inventoryActions.scrapPart(item); // delete it! now!
- userActions.saveConstruction(); //save again.
- return false;
- }
- }
- var compatibleItems = [];
- // making a list of equipped items of same type
- compatibleItems = user.data.characters[0].constructions[list].parts.filter(function (fnitem) { return shouldSwap(fnitem.part, item, minQ, maxQ); })
- if (compatibleItems.length == 0)
- return false; // no results, so no replace, break here.
- else {
- userActions.setView(list);
- candidate = compatibleItems.reduce(worstStats); //find the worst item
- // console.log(candidate, ' got selected from ', compatibleItems);
- var xpos = candidate.x; var ypos = candidate.y; // init
- Construction.removeFromSetup(user.data.characters[0].constructions[list], candidate.part, xpos, ypos); //remove
- //if (Construction.addToSetup(user.data.characters[0].constructions[list], item, xpos, ypos)) //replace
- Construction.addToSetup(user.data.characters[0].constructions[list], item, xpos, ypos); //replace
- systemLog.push('Equipping <b class="rarity-' + item.quality + '-text"> ' + item.name + ' +' + item.plus + '</b>'); // tell the user
- userActions.saveConstruction(); //save
- userActions.update();
- }
- }
- function craftStorage(item) {
- var compatibleItems = [];
- //build a list containing items to be crafted into
- compatibleItems = user.data.inventory.parts.filter(function (fnitem) { return shouldCraft(fnitem, item, craftInventory); });
- // find the best candidate for a craft if there are results
- if (compatibleItems.length > 0)
- candidate = compatibleItems.reduce(bestCraft);
- else return false; // no results, so no craft.
- systemLog.push('<b class="rarity-' + item.quality + '-text">Upgrading</b> <b class="rarity-' + candidate.quality + '-text">' + candidate.name + " +" + (candidate.plus + 1), '</b>');
- return craftingLib.combine(candidate.id, item.id, true);
- }
- function randitem(items) { //does not pick first item
- return items[Math.floor(Math.random() * (items.length - 1)) + 1];
- }
- function statSum(stats) { // just lump it all together, and oh yeah, reverse weights weight.
- var sumRet = 0;
- for (var tk in stats) {
- if (tk != 'weight') sumRet += stats[tk];
- else sumRet -= stats[tk];
- }
- return sumRet;
- }
- function compareStats(champion, challenger) {
- var champ = statSum(champion['stats']);
- var challe = statSum(challenger['stats']);
- return champ >= challe;
- }
- //
- // ---------------- //
- // Handle New items //
- // ---------------- //
- function checkNewItem(item) { // ooo! shinies!
- // if (craftEquipped(item)) return true; else // can we craft it onto our equipment right away?
- if (updateEquipList(item, 'shield')) return true; else // is it better than our current equipment?
- if (updateEquipList(item, 'mainWeapon')) return true; else// well, is it?
- if (craftStorage(item)) return true; else // can we craft it into something else?
- return false; //found nothing...
- }
- /* Triggers */
- var triggeredby = data.scriptEvent; // figure out what we are reacting to
- checkNewItem(randitem(user.data.inventory.parts)); // always do a random spot check.
- if (triggeredby == "newParts") { // Shinies!
- checkNewItem(data.parts[0]); // see if it fits anywhere
- //checkNewItem(randitem(user.data.inventory.parts)); // do a random spot check.
- } else if (triggeredby == "inventoryFull") { // We have run out of room, time to scrap stuff.
- userActions.upgradeInventory('parts'); //always try to upgrade inventory space.
- var itemsGone = commonCleanup(wantedSpace, 0); // run commonCleanup...
- } else if (triggeredby == "init") { // Oh, I guess we should make sure stuff is set up...
- //Thanks go to Grote for the next line, it removes the normal chat log.
- chatActions.addSystemMessage = function (msg) { if (systemLog.length > 20) { systemLog.splice(0, 1) } }
- } else if (triggeredby == "scriptSaved") { // saved,
- // placeholder, to reduce console spam...
- } else if (triggeredby == "failedCraft") { // failed, lets tell them.
- systemLog.push('Failed upgrade ' + data[primaryPartId]);
- console.log('Failed upgrade ' + data[primaryPartId]);
- } else { // Not sure what is going on, better tell someone.
- console.log("Unused event - " + data.scriptEvent);
- console.log(data);
- }
- //console.log(user.data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement