Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //console.log(user.data);
  2.  
  3.    /********************************\
  4.  ,'      GHAZZ' AUTOCRAFT SCRIPT!    '.
  5. (           Tries to be smart!         )
  6.  `.        Deletes "bad" items!      .'
  7.    )           Autocrafts!          (  
  8.  ,'      Upgrades Inventory size!    '.
  9. (        Autoequips better items!      )
  10.  `.       Tries to handle Dupes!     .'
  11.    \********************************/
  12.  
  13. // These values are here for your convenience.
  14. //var wantedSpace = 5; //The script will try to free up at least this much space when scrapping.
  15. var wantedSpace = Math.floor(user.data.properties.maxInventory.parts / 4.5); // autosetting
  16.  
  17. var dumpQuality = -1; // this quality gets scrapped on drop. -1 to disable.
  18. var sellQuality = 2; // this quality and lower gets sold automatically
  19. var minQuality = 0; // this quality and lower get ignored as crafting targets.
  20.  
  21. var autoRemoveInferior = true; // TODO: remove items with lower potential than equipped items
  22. //var autoMinMod = false; // TODO remove all items of same type with lower mod/stats when considering deletes.
  23. //var minMod = 0; // TODO remove all items of lower mod value than this when selling.
  24.  
  25. // These arrays are what quality primary will be targeted when consuming each quality.
  26. // The first value is 0*, second is 1* etc.
  27.  
  28. var craftInventory = [4, 4, 5, 5, 6, 6, 7, 7]; // Default, Ghazz' choice.
  29. //var craftInventory = [0, 1, 2, 3, 4, 5, 6, 7]; // only use same quality.
  30. //var craftInventory = [1, 2, 3, 4, 5, 6, 7, 7]; // use quality-1 to craft upgrades
  31. //var craftInventory = [3, 3, 3, 3, 4, 6, 7, 7]; // mid tier focus
  32. //var craftInventory = [4, 4, 4, 4, 6, 7, 7, 7]; // greed, greed, greed!
  33. //var craftInventory = [4, 4, 4, 5, 6, 7, 7, 7]; // use quality-2 to craft upgrades
  34. //var craftInventory = [-1, 4, 4, 4, 6, 7, 7, 7]; // testing, testing.
  35. //var craftInventory = [2, 7, 7, 7, 7, 7, 7, 7]; // Just upgrade it all.
  36.  
  37. // These item types will be autosold.
  38. var unwantedParts = ['barrelClip', 'barrelExtender', 'barrelSplitterTwo', 'clip', 'shieldRing', 'stock', 'uClip'];
  39. // uncomment next line to keep all parts.
  40. var unwantedParts = [];
  41.  
  42. // code starts here
  43.  
  44. var ourItems = user.data.inventory.parts; // get our inventory
  45.  
  46. function statSum(stats) { // just lump it all together, and oh yeah, reverse weights weight.
  47.     var sumRet = 0;
  48.     for (var tk in stats) {
  49.         if (tk != 'weight') sumRet += stats[tk];
  50.         else sumRet -= stats[tk];
  51.     }
  52.     return sumRet;
  53. } //TODO: Fix this to do smarter things.
  54.  
  55. function statCompare(a, b) {
  56.     return statSum(a.stats) - statSum(b.stats);
  57. }
  58.  
  59. // Thanks to Grote for helping me clean up this function (I have since messed it up again)
  60. // His rewrite inspired how a lot of the rest of the script is built.
  61. function commonCleanup(neededSlots) { // Deletes items, tries to be sensible with it.
  62.     var soldItemIds = []; // functions, the poor mans class
  63.     var soldValue = 0;
  64.     var soldQuality = 0;
  65.  
  66.     function deletePart(part) {
  67.         if (!(soldItemIds.includes(part.id))) { //not sold
  68.             soldValue += part.value; //count our money
  69.             soldQuality = Math.max(part.quality,soldQuality); // find highest quality sold
  70.             soldItemIds.push(part.id); // put the item on the list.
  71.             return true;
  72.         } else return false;
  73.     }
  74.  
  75.     if (autoRemoveInferior) {
  76.         /*
  77.           pick a random part. get a list of all items with its type. count number equipped.
  78.           if we have more than are equipped, and more than one if not, delete the item with lowest .original stat
  79.         */
  80.         var suspect = randitem(user.data.inventory.parts);
  81.  
  82.  
  83.     }
  84.  
  85.     quality = -1;
  86.     while (quality++ < 7 && neededSlots > 0) {
  87.         //get an easy to use reference to the inventory, containing only objects of current rarity
  88.         var ourItems = user.data.inventory.parts.filter(function (item) { return item.quality <= quality; });
  89.         //Delete all items that are below quality or not on the list. //no break statement becasue you want to lose them anyway
  90.         for (var item of ourItems) {
  91.             if ((item.quality < sellQuality
  92.                  || unwantedParts.includes(item.type)
  93.                 ) && deletePart(item)) {
  94.                 neededSlots--;
  95.                 ourItems.splice(ourItems.indexOf(item), 1);
  96.             }
  97.         }
  98.         //Delete all 0 mod items untill we reach the needed Slots
  99.         for(var item of ourItems) {
  100.             if (neededSlots <= 0) break;
  101.             if (item.plus == 0 && deletePart(item)) {
  102.                 neededSlots--;
  103.                 ourItems.splice(ourItems.indexOf(item), 1);
  104.             }
  105.         }
  106.         //Delete the cheapest item of the current rarity untill you hit the neededSlots
  107.         while (ourItems.length > 0 && neededSlots > 0) { //also check there are still more items
  108.             var partToDelete = ourItems.reduce(function (lowest, highest) { return lowest.value < highest.value ? lowest : highest; });
  109.             if (deletePart(partToDelete)) neededSlots--;
  110.             ourItems.splice(ourItems.indexOf(partToDelete), 1);
  111.         }
  112.     }
  113.     inventoryLib.scrapSelected(soldItemIds);  // sell all the items.
  114.     inventoryLib.get(); // get a new equipment list
  115.     systemLog.push('Sold <b class="rarity-' + soldQuality + '-text">' + soldItemIds.length + ' parts</b> for <b>' + soldValue + '</b> credits.'); //tell them.
  116. }
  117.  
  118. //--------------//
  119. //              //
  120. //   Crafting   //    
  121. //              //
  122. //--------------//
  123.  
  124. //"should" are used by array.filter.
  125. function shouldCraft(primary, consumed, craftGrid) {
  126.     return (primary.id != consumed.id // not same item
  127.         && primary.type == consumed.type // same type
  128.         && primary.mod == consumed.mod // mod value is compatible.
  129.         && primary.quality >= consumed.quality // primary is not lower quality
  130.         && primary.quality != 0 // never upgrade greys
  131.         && consumed.plus == 0 // consuming an item with plus is not optimal.
  132.         && primary.plus < (primary.quality + 1) * 5 // not fully upgraded
  133.         && primary.quality <= crafting.limits.quality //we have enough factories to craft this
  134.         && craftGrid[consumed.quality] >= primary.quality //check that we are within crafting limits
  135.         );
  136. }
  137. function shouldSwap(current, candidate, minq, maxq) {
  138.     //    console.log(candidate.quality,minq, candidate.quality <= (minq + 2), candidate.quality >= minq, candidate.quality >= minq, candidate.type == current.type && statSum(candidate) > statSum(current));
  139.     return (candidate.quality <= (minq + 2) //check that we are within two of minimum quality
  140.         && candidate.quality >= (maxq - 2) // and also that we are within two of max
  141.         && candidate.type == current.type // same type
  142.         && statSum(candidate.stats) >= statSum(current.stats) // candidate is "better"
  143.         ); //TODO: check weight.
  144. } //TODO: should check ".original", to see if this item has better potential.
  145.  
  146. //"best" and "worst" are used by array.reduce.
  147. function bestCraft(first, second) {// we choose the first item if it is
  148.     return (first.quality >= second.quality //same or better quality
  149.         && first.plus >= second.plus // same or better upgrades
  150.         && statSum(first) > statSum(second) // "better" stats
  151.         ) ? first : second; // return the chosen object
  152. }
  153. function worstStats(first, second) {
  154.     return (statSum(second.part) - statSum(first.part)) ? first : second;
  155. }
  156.  
  157. function updateEquipList(item, list) {
  158.     // find max and min qualities.
  159.     var minQ = 8;
  160.     var maxQ = 0;
  161.     for (part of user.data.characters[0].constructions[list].parts) { //go through our list and find stuff...
  162.         if (part.part.quality < minQ) minQ = part.part.quality; // minimum quality
  163.         if (part.part.quality > maxQ) maxQ = part.part.quality; // maximum quality
  164.         if (part.part.id == item.id) { // Check for dupes....
  165.             console.log("found dupe ", item); // Tell them.
  166.             //inventoryActions.scrapPart(item); // delete it! now!
  167.             userActions.saveConstruction(); //save again.
  168.             return false;
  169.         }
  170.     }
  171.     var compatibleItems = [];
  172.     // making a list of equipped items of same type
  173.     compatibleItems = user.data.characters[0].constructions[list].parts.filter(function (fnitem) { return shouldSwap(fnitem.part, item, minQ, maxQ); })
  174.     if (compatibleItems.length == 0)
  175.         return false; // no results, so no replace, break here.
  176.     else {
  177.         userActions.setView(list);
  178.         candidate = compatibleItems.reduce(worstStats); //find the worst item
  179.         //        console.log(candidate, ' got selected from ', compatibleItems);
  180.         var xpos = candidate.x; var ypos = candidate.y; // init
  181.         Construction.removeFromSetup(user.data.characters[0].constructions[list], candidate.part, xpos, ypos); //remove
  182.         //if (Construction.addToSetup(user.data.characters[0].constructions[list], item, xpos, ypos)) //replace
  183.         Construction.addToSetup(user.data.characters[0].constructions[list], item, xpos, ypos); //replace
  184.         systemLog.push('Equipping <b class="rarity-' + item.quality + '-text"> ' + item.name + ' +' + item.plus + '</b>'); // tell the user
  185.         userActions.saveConstruction(); //save
  186.         userActions.update();
  187.     }
  188. }
  189.  
  190. function craftStorage(item) {
  191.     var compatibleItems = [];
  192.     //build a list containing items to be crafted into
  193.     compatibleItems = user.data.inventory.parts.filter(function (fnitem) { return shouldCraft(fnitem, item, craftInventory); });
  194.     // find the best candidate for a craft if there are results
  195.     if (compatibleItems.length > 0)
  196.         candidate = compatibleItems.reduce(bestCraft);
  197.     else return false; // no results, so no craft.
  198.  
  199.     systemLog.push('<b class="rarity-' + item.quality + '-text">Upgrading</b> <b class="rarity-' + candidate.quality + '-text">' + candidate.name + " +" + (candidate.plus + 1), '</b>');
  200.     return craftingLib.combine(candidate.id, item.id, true);
  201. }
  202.  
  203. function randitem(items) { //does not pick first item
  204.     return items[Math.floor(Math.random() * (items.length - 1)) + 1];
  205. }
  206. function statSum(stats) { // just lump it all together, and oh yeah, reverse weights weight.
  207.     var sumRet = 0;
  208.     for (var tk in stats) {
  209.         if (tk != 'weight') sumRet += stats[tk];
  210.         else sumRet -= stats[tk];
  211.     }
  212.     return sumRet;
  213. }
  214. function compareStats(champion, challenger) {
  215.     var champ = statSum(champion['stats']);
  216.     var challe = statSum(challenger['stats']);
  217.     return champ >= challe;
  218. }
  219.  
  220.  
  221. //
  222. // ---------------- //
  223. // Handle New items //
  224. // ---------------- //
  225.  
  226. function checkNewItem(item) { // ooo! shinies!
  227.     //    if (craftEquipped(item)) return true; else // can we craft it onto our equipment right away?
  228.     if (updateEquipList(item, 'shield')) return true; else // is it better than our current equipment?
  229.         if (updateEquipList(item, 'mainWeapon')) return true; else// well, is it?
  230.             if (craftStorage(item)) return true; else // can we craft it into something else?
  231.                 return false; //found nothing...
  232. }
  233.  
  234. /* Triggers */
  235. var triggeredby = data.scriptEvent; // figure out what we are reacting to
  236. checkNewItem(randitem(user.data.inventory.parts)); // always do a random spot check.
  237.  
  238. if (triggeredby == "newParts") { // Shinies!
  239.     checkNewItem(data.parts[0]); // see if it fits anywhere
  240.  
  241.     //checkNewItem(randitem(user.data.inventory.parts)); // do a random spot check.
  242.  
  243. } else if (triggeredby == "inventoryFull") { // We have run out of room, time to scrap stuff.
  244.  
  245.     userActions.upgradeInventory('parts'); //always try to upgrade inventory space.
  246.     var itemsGone = commonCleanup(wantedSpace, 0); // run commonCleanup...
  247.  
  248. } else if (triggeredby == "init") { // Oh, I guess we should make sure stuff is set up...
  249.     //Thanks go to Grote for the next line, it removes the normal chat log.
  250.     chatActions.addSystemMessage = function (msg) { if (systemLog.length > 20) { systemLog.splice(0, 1) } }
  251.  
  252. } else if (triggeredby == "scriptSaved") { // saved,
  253.     // placeholder, to reduce console spam...
  254.  
  255. } else if (triggeredby == "failedCraft") { // failed, lets tell them.
  256.     systemLog.push('Failed upgrade ' + data[primaryPartId]);
  257.     console.log('Failed upgrade ' + data[primaryPartId]);
  258.  
  259. } else { // Not sure what is going on, better tell someone.
  260.  
  261.     console.log("Unused event - " + data.scriptEvent);
  262.     console.log(data);
  263.  
  264. }
  265. //console.log(user.data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement