Advertisement
theBGuy

Untitled

Apr 24th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ShopBot() {
  2.     let overlayText = {
  3.         title: new Text("kolbot shopbot", 50, 245, 2, 1),
  4.         cycles: new Text("Cycles in last minute:", 50, 260, 2, 1),
  5.         frequency: new Text("Valid item frequency:", 50, 275, 2, 1),
  6.         totalCycles: new Text("Total cycles:", 50, 290, 2, 1),
  7.     };
  8.  
  9.     let tickCount,
  10.         cycles = 0,
  11.         validItems = 0,
  12.         totalCycles = 0;
  13.  
  14.     Pather.teleport = false;
  15.     this.pickEntries = [];
  16.     this.npcs = {};
  17.  
  18.     this.buildPickList = function () {
  19.         let nipfile, filepath = "pickit/shopbot.nip",
  20.             filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length);
  21.  
  22.         if (!FileTools.exists(filepath)) {
  23.             Misc.errorReport("ÿc1NIP file doesn't exist: ÿc0" + filepath);
  24.             return false;
  25.         }
  26.  
  27.         try {
  28.             nipfile = File.open(filepath, 0);
  29.         } catch (fileError) {
  30.             Misc.errorReport("ÿc1Failed to load NIP: ÿc0" + filename);
  31.         }
  32.  
  33.         if (!nipfile) return false;
  34.  
  35.         let lines = nipfile.readAllLines();
  36.         nipfile.close();
  37.  
  38.         for (let i = 0; i < lines.length; i += 1) {
  39.             let info = {
  40.                 line: i + 1,
  41.                 file: filename,
  42.                 string: lines[i]
  43.             };
  44.  
  45.             let line = NTIP.ParseLineInt(lines[i], info);
  46.             line && this.pickEntries.push(line);
  47.         }
  48.  
  49.         return true;
  50.     };
  51.  
  52.     this.openMenu = function (npc) {
  53.         if (!npc || npc.type !== 1) throw new Error("Unit.openMenu: Must be used on NPCs.");
  54.  
  55.         let interactedNPC = getInteractedNPC();
  56.  
  57.         if (interactedNPC && interactedNPC.name !== npc.name) {
  58.             sendPacket(1, 0x30, 4, interactedNPC.type, 4, interactedNPC.gid);
  59.             me.cancel();
  60.         }
  61.  
  62.         if (getUIFlag(0x08)) return true;
  63.  
  64.         for (let i = 0; i < 10; i += 1) {
  65.             npc.distance > 5 && Pather.walkTo(npc.x, npc.y);
  66.  
  67.             if (!getUIFlag(0x08)) {
  68.                 sendPacket(1, 0x13, 4, 1, 4, npc.gid);
  69.                 sendPacket(1, 0x2f, 4, 1, 4, npc.gid);
  70.             }
  71.  
  72.             let tick = getTickCount();
  73.  
  74.             while (getTickCount() - tick < Math.max(Math.round((i + 1) * 250 / (i / 3 + 1)), me.ping + 1)) {
  75.                 if (getUIFlag(0x08)) {
  76.                     return true;
  77.                 }
  78.  
  79.                 delay(10);
  80.             }
  81.         }
  82.  
  83.         me.cancel();
  84.  
  85.         return false;
  86.     };
  87.  
  88.     this.shopItems = function (npc, menuId) {
  89.         let bought;
  90.  
  91.         if (!Storage.Inventory.CanFit({sizex: 2, sizey: 4}) && AutoMule.getMuleItems().length > 0) {
  92.             D2Bot.printToConsole("Mule triggered");
  93.             scriptBroadcast("mule");
  94.             scriptBroadcast("quit");
  95.             return true;
  96.         }
  97.  
  98.         if (!npc) return false;
  99.  
  100.         for (let i = 0; i < 10; i += 1) {
  101.             delay(150);
  102.  
  103.             i % 2 === 0 && sendPacket(1, 0x38, 4, 1, 4, npc.gid, 4, 0);
  104.  
  105.             if (npc.itemcount > 0) {
  106.                 break;
  107.             }
  108.         }
  109.  
  110.         let items = npc.getItemsEx().filter(function (item) {
  111.             return (Config.ShopBot.ScanIDs.includes(item.classid) || Config.ShopBot.ScanIDs.length === 0);
  112.         });
  113.  
  114.         if (!items.length) return false;
  115.  
  116.         me.overhead(npc.itemcount + " items, " + items.length + " valid");
  117.  
  118.         validItems += items.length;
  119.         overlayText.frequency.text = "Valid base items / cycle: " + ((validItems / totalCycles).toFixed(2).toString());
  120.  
  121.         for (let i = 0; i < items.length; i += 1) {
  122.             if (Storage.Inventory.CanFit(items[i]) && Pickit.canPick(items[i]) &&
  123.                     me.gold >= items[i].getItemCost(0) &&
  124.                     NTIP.CheckItem(items[i], this.pickEntries)
  125.             ) {
  126.                 beep();
  127.                 D2Bot.printToConsole("Match found!", 7);
  128.                 delay(1000);
  129.  
  130.                 if (npc.startTrade(menuId)) {
  131.                     Misc.logItem("Shopped", items[i]);
  132.                     items[i].buy();
  133.                     bought = true;
  134.                 }
  135.  
  136.                 Config.ShopBot.QuitOnMatch && scriptBroadcast("quit");
  137.             }
  138.         }
  139.  
  140.         if (bought) {
  141.             me.cancelUIFlags();
  142.             Town.stash();
  143.         }
  144.  
  145.         return true;
  146.     };
  147.  
  148.     this.shopAtNPC = function (name) {
  149.         let wp, menuId = "Shop";
  150.  
  151.         switch (name) {
  152.         case NPC.Charsi:
  153.             menuId = "Repair";
  154.         // eslint-disable-next-line no-fallthrough
  155.         case NPC.Akara:
  156.         case NPC.Gheed:
  157.             wp = 1;
  158.  
  159.             break;
  160.         case NPC.Fara:
  161.             menuId = "Repair";
  162.         // eslint-disable-next-line no-fallthrough
  163.         case NPC.Elzix:
  164.         case NPC.Drognan:
  165.             wp = 40;
  166.  
  167.             break;
  168.         case NPC.Hratli:
  169.             menuId = "Repair";
  170.         // eslint-disable-next-line no-fallthrough
  171.         case NPC.Asheara:
  172.         case NPC.Ormus:
  173.             wp = 75;
  174.  
  175.             break;
  176.         case NPC.Halbu:
  177.             menuId = "Repair";
  178.         // eslint-disable-next-line no-fallthrough
  179.         case NPC.Jamella:
  180.             wp = 103;
  181.  
  182.             break;
  183.         case NPC.Larzuk:
  184.             menuId = "Repair";
  185.         // eslint-disable-next-line no-fallthrough
  186.         case NPC.Malah:
  187.         case NPC.Anya:
  188.             wp = 109;
  189.  
  190.             break;
  191.         default:
  192.             throw new Error("Invalid NPC");
  193.         }
  194.  
  195.         if (!Pather.useWaypoint(wp)) return false;
  196.  
  197.         let npc = this.npcs[name] || getUnit(1, name);
  198.  
  199.         if (!npc || npc.distance > 5) {
  200.             Town.move(name);
  201.             npc = getUnit(1, name);
  202.         }
  203.  
  204.         if (!npc) return false;
  205.  
  206.         !this.npcs[name] && (this.npcs[name] = copyUnit(npc));
  207.         Config.ShopBot.CycleDelay && delay(Config.ShopBot.CycleDelay);
  208.         this.openMenu(npc) && this.shopItems(npc, menuId);
  209.  
  210.         return true;
  211.     };
  212.  
  213.     // START
  214.     for (let i = 0; i < Config.ShopBot.ScanIDs.length; i += 1) {
  215.         if (isNaN(Config.ShopBot.ScanIDs[i])) {
  216.             if (NTIPAliasClassID.hasOwnProperty(Config.ShopBot.ScanIDs[i].replace(/\s+/g, "").toLowerCase())) {
  217.                 Config.ShopBot.ScanIDs[i] = NTIPAliasClassID[Config.ShopBot.ScanIDs[i].replace(/\s+/g, "").toLowerCase()];
  218.             } else {
  219.                 Misc.errorReport("ÿc1Invalid ShopBot entry:ÿc0 " + Config.ShopBot.ScanIDs[i]);
  220.                 Config.ShopBot.ScanIDs.splice(i, 1);
  221.                 i -= 1;
  222.             }
  223.         }
  224.     }
  225.  
  226.     typeof Config.ShopBot.ShopNPC === "string" && (Config.ShopBot.ShopNPC = [Config.ShopBot.ShopNPC]);
  227.  
  228.     for (let i = 0; i < Config.ShopBot.ShopNPC.length; i += 1) {
  229.         Config.ShopBot.ShopNPC[i] = Config.ShopBot.ShopNPC[i].toLowerCase();
  230.     }
  231.  
  232.     if (Config.ShopBot.MinGold && me.gold < Config.ShopBot.MinGold) return true;
  233.  
  234.     this.buildPickList();
  235.     print("Shopbot: Pickit entries: " + this.pickEntries.length);
  236.     Town.doChores();
  237.  
  238.     tickCount = getTickCount();
  239.  
  240.     while (!Config.ShopBot.Cycles || totalCycles < Config.ShopBot.Cycles) {
  241.         if (getTickCount() - tickCount >= 60 * 1000) {
  242.             overlayText.cycles.text = "Cycles in last minute: " + cycles.toString();
  243.             overlayText.totalCycles.text = "Total cycles: " + totalCycles.toString();
  244.             cycles = 0;
  245.             tickCount = getTickCount();
  246.         }
  247.  
  248.         for (let i = 0; i < Config.ShopBot.ShopNPC.length; i += 1) {
  249.             this.shopAtNPC(Config.ShopBot.ShopNPC[i]);
  250.         }
  251.  
  252.         if (me.inTown) {
  253.             let area = getArea(),
  254.                 wp = getPresetUnit(me.area, 2, [119, 156, 237, 398, 429][me.act - 1]),
  255.                 wpX = wp.roomx * 5 + wp.x,
  256.                 wpY = wp.roomy * 5 + wp.y,
  257.                 redPortal = (getUnits(2, 60).sort((a, b) => a.distance - b.distance)).first(),
  258.                 exit = area.exits[0];
  259.  
  260.             for (let i = 1; i < area.exits.length; i++) {
  261.                 if (getDistance(me, exit) > getDistance(me, area.exits[i])) {
  262.                     exit = area.exits[i];
  263.                 }
  264.             }
  265.  
  266.             if (me.area === sdk.areas.Harrogath && !!redPortal && getDistance(me, redPortal) < 20
  267.                 && Pather.usePortal(null, null, redPortal)) {
  268.                 delay(3000);
  269.                 Pather.usePortal(sdk.areas.Harrogath);
  270.  
  271.                 if (totalCycles === 0) {
  272.                     delay(10000);
  273.                 }
  274.  
  275.                 delay(1500);
  276.             } else if (me.area === sdk.areas.RogueEncampment && !!redPortal && getDistance(me, redPortal) < 20
  277.                 && Pather.usePortal(null, null, redPortal)) {
  278.                 delay(3000);
  279.                 Pather.usePortal(1);
  280.  
  281.                 if (totalCycles === 0) {
  282.                     delay(10000);
  283.                 }
  284.  
  285.                 delay(1500);
  286.             } else if (getDistance(me, exit) < (getDistance(me, wpX, wpY) + 6)) {
  287.                 Pather.moveToExit(me.area + 1, true);
  288.                 Pather.moveToExit(me.area - 1, true);
  289.             } else {
  290.                 Pather.useWaypoint([35, 48, 101, 107, 113][me.act - 1]);
  291.             }
  292.         }
  293.  
  294.         cycles += 1;
  295.         totalCycles += 1;
  296.     }
  297.  
  298.     return true;
  299. }
  300.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement