Advertisement
Guest User

Untitled

a guest
May 24th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ShopBot() {
  2.     var npc, tickCount, town, wpArea, path,
  3.         cycles = 0,
  4.         cyclesText = new Text("Cycles in last minute: " + cycles, 50, 260, 2, 1),
  5.         leadRetry = 10,
  6.         leadTimeout = 20; // NPC move timeout in seconds
  7.  
  8.     this.mover = function (npc, path) {
  9.         var i, j;
  10.  
  11.         for (i = 0; i < path.length; i += 2) {
  12.             Pather.moveTo(path[i] - 3, path[i + 1] - 3);
  13.  
  14.             moveNPC(npc, path[i], path[i + 1]);
  15.  
  16.             for (j = 0; j < leadTimeout; j += 1) {
  17.                 if (getDistance(npc.x, npc.y, path[i], path[i + 1]) < 2) {
  18.                     break;
  19.                 }
  20.  
  21.                 if (j > 0 && j % leadRetry === 0) {
  22.                     moveNPC(npc, path[i], path[i + 1]);
  23.                 }
  24.  
  25.                 delay(1000);
  26.             }
  27.  
  28.             if (j === leadTimeout) {
  29.                 return false;
  30.             }
  31.         }
  32.  
  33.         delay(1000);
  34.  
  35.         return true;
  36.     };
  37.  
  38.     this.openMenu = function (npc) {
  39.         if (npc.type !== 1) {
  40.             throw new Error("Unit.openMenu: Must be used on NPCs.");
  41.         }
  42.  
  43.         if (getUIFlag(0x08)) {
  44.             return true;
  45.         }
  46.  
  47.         var i, j;
  48.  
  49.         for (i = 0; i < 3; i += 1) {
  50.             if (getDistance(me, npc) > 3) {
  51.                 Pather.moveToUnit(npc);
  52.             }
  53.  
  54.             //npc.interact();
  55.             sendPacket(1, 0x13, 4, 1, 4, npc.gid);
  56.  
  57.             for (j = 0; j < 40; j += 1) {
  58.                 if (j % 8 === 0) {
  59.                     me.cancel();
  60.                     delay(300);
  61.                     //npc.interact();
  62.                     sendPacket(1, 0x13, 4, 1, 4, npc.gid);
  63.                 }
  64.  
  65.                 if (getUIFlag(0x08)) {
  66.                     return true;
  67.                 }
  68.  
  69.                 delay(25);
  70.             }
  71.         }
  72.  
  73.         return false;
  74.     },
  75.  
  76.     this.shopItems = function () {
  77.         var i, items,
  78.             npc = getInteractedNPC();
  79.  
  80.         if (!npc) {
  81.             return false;
  82.         }
  83.  
  84.         for (i = 0; i < 10; i += 1) {
  85.             delay(150);
  86.  
  87.             if (i % 2 === 0) {
  88.                 //sendPacket(1, 0x2f, 4, 1, 4, npc.gid);
  89.                 sendPacket(1, 0x38, 4, 1, 4, npc.gid, 4, 0);
  90.             }
  91.  
  92.             if (npc.itemcount > 0) {
  93.                 //delay(200);
  94.  
  95.                 break;
  96.             }
  97.         }
  98.  
  99.         items = npc.getItems();
  100.  
  101.         if (!items || !items.length) {
  102.             return false;
  103.         }
  104.  
  105.         me.overhead(items.length + " items");
  106.  
  107.         for (i = 0; i < items.length; i += 1) {
  108.             if (Config.ShopBot.ScanIDs.indexOf(items[i].classid) > -1) {
  109.                 //print("Scanning " + items[i].name);
  110.  
  111.                 if (Pickit.checkItem(items[i]).result === 1) {
  112.                     try {
  113.                         if (Storage.Inventory.CanFit(items[i]) && me.getStat(14) + me.getStat(15) >= items[i].getItemCost(0)) {
  114.                             delay(1000);
  115.  
  116.                             if (npc.startTrade()) {
  117.                                 Misc.logItem("Shopped", items[i]);
  118.                                 items[i].buy();
  119.                             }
  120.                         }
  121.                     } catch (e) {
  122.                         print(e);
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.  
  128.         return true;
  129.     };
  130.  
  131.     this.useWp = function (area) {
  132.         var i, unit;
  133.  
  134.         if (me.area === area) {
  135.             return true;
  136.         }
  137.  
  138.         unit = getUnit(2, "Waypoint");
  139.  
  140.         if (!unit) {
  141.             return false;
  142.         }
  143.  
  144.         if (me.inTown) {
  145.             me.cancel();
  146.         }
  147.  
  148.         for (i = 0; i < 80; i += 1) {
  149.             if (i % 20 === 0) {
  150.                 if (getDistance(me, unit) > 5) {
  151.                     Pather.moveToUnit(unit);
  152.                 }
  153.  
  154.                 unit.interact(area);
  155.             }
  156.  
  157.             delay(75);
  158.  
  159.             if (me.area === area) {
  160.                 return true;
  161.             }
  162.         }
  163.  
  164.         return false;
  165.     };
  166.  
  167.     Town.doChores();
  168.  
  169.     switch (Config.ShopBot.ShopNPC.toLowerCase()) {
  170.     case "Ormus":
  171.         wpArea = 101;
  172.         town = 75;
  173.         path = [5147, 5089, 5156, 5075, 5157, 5063, 5160, 5050];
  174.  
  175.         if (!Town.goToTown(3) || !Town.move(NPC.Ormus)) {
  176.             throw new Error("Failed to get to NPC");
  177.         }
  178.  
  179.         npc = getUnit(1, NPC.Ormus);
  180.  
  181.         break;
  182.        
  183.     case "anya":
  184.         wpArea = 107;
  185.         town = 125;
  186.         path = [5122, 5119, 5129, 5105, 5123, 5087, 5115, 5068];
  187.  
  188.         if (!Town.goToTown(5) || !Town.move(NPC.Anya)) {
  189.             throw new Error("Failed to get to NPC");
  190.         }
  191.  
  192.         npc = getUnit(1, NPC.Anya);
  193.  
  194.         break;
  195.     default:
  196.         throw new Error("Invalid shopbot NPC.");
  197.     }
  198.  
  199.     if (!npc) {
  200.         throw new Error("Failed to find NPC.");
  201.     }
  202.  
  203.     if (!this.mover(npc, path)) {
  204.         throw new Error("Failed to move NPC");
  205.     }
  206.  
  207.     Town.move("waypoint");
  208.  
  209.     tickCount = getTickCount();
  210.  
  211.     while (true) {
  212.         if (getTickCount() - tickCount >= 60 * 1000) {
  213.             cyclesText.text = "Cycles in last minute: " + cycles;
  214.             tickCount = getTickCount();
  215.             cycles = 0;
  216.         }
  217.  
  218.         if (me.area === town) {
  219.             if (this.openMenu(npc)) {
  220.                 this.shopItems();
  221.             }
  222.  
  223.             me.cancel();
  224.         }
  225.  
  226.         if (me.area === town) {
  227.             this.useWp(wpArea);
  228.         }
  229.  
  230.         if (me.area === wpArea) {
  231.             this.useWp(town);
  232.         }
  233.  
  234.         cycles += 1;
  235.     }
  236.  
  237.     return true;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement