Advertisement
kolton

ItemMover.js

Jun 28th, 2014
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. *   @filename   ItemMover.js
  3. *   @author     kolton
  4. *   @desc       Easy item management that uses screenhook events
  5. */
  6.  
  7. function ItemMover() {
  8.     var i, activeAction, item, items,
  9.         hooks = [];
  10.  
  11.     // Storage override
  12.     Storage.Stash.MoveTo = function (item) {
  13.         var i, spot, tick;
  14.  
  15.         if (Packet.itemToCursor(item)) {
  16.             for (i = 0; i < 15; i += 1) {
  17.                 spot = Storage.Stash.FindSpot(item); // Returns inverted coords...
  18.  
  19.                 if (spot) {
  20.                     // 18 [DWORD id] [DWORD xpos] [DWORD ypos] [DWORD buffer]
  21.                     sendPacket(1, 0x18, 4, item.gid, 4, spot.y, 4, spot.x, 4, 0x4);
  22.                 }
  23.  
  24.                 tick = getTickCount();
  25.  
  26.                 while (getTickCount() - tick < Math.max(1000, me.ping * 2 + 200)) {
  27.                     if (!me.itemoncursor) {
  28.                         return true;
  29.                     }
  30.  
  31.                     delay(10);
  32.                 }
  33.             }
  34.         }
  35.  
  36.         return false;
  37.     };
  38.  
  39.     function hookHandler(click, x, y) {
  40.         // Get the hook closest to the clicked location
  41.         function sortHooks(h1, h2) {
  42.             return Math.abs(h1.y - y) - Math.abs(h2.y - y);
  43.         }
  44.  
  45.         if (click === 0) { // Left click
  46.             // Sort hooks
  47.             hooks.sort(sortHooks);
  48.  
  49.             switch (hooks[0].text) {
  50.             case "Click on action to start/stop:":
  51.                 return true; // Do nothing
  52.             default:
  53.                 // Don't start new action until the current one finishes
  54.                 if (activeAction && activeAction !== hooks[0].text) {
  55.                     return true;
  56.                 }
  57.  
  58.                 // Toggle current action on/off
  59.                 activeAction = activeAction ? false : hooks[0].text;
  60.  
  61.                 break;
  62.             }
  63.  
  64.             hooks[0].color = hooks[0].color === 2 ? 1 : 2;
  65.  
  66.             return true; // Block click
  67.         }
  68.  
  69.         return false;
  70.     }
  71.  
  72.     function sortPickList(a, b) { // Sort items by size to pick biggest first
  73.         if (b.sizex === a.sizex && b.sizey === a.sizey) { // Same size -  sort by distance
  74.             return getDistance(me, a) - getDistance(me, b);
  75.         }
  76.  
  77.         return b.sizex * b.sizey - a.sizex * a.sizey;
  78.     }
  79.  
  80.     hooks.push(new Text("Click on action to start/stop:", 25, 190, 4, 1, 0, false, hookHandler));
  81.     hooks.push(new Text("Empty Inventory", 25, 200 + hooks.length * 17, 2, 1, 0, false, hookHandler));
  82.     hooks.push(new Text("Empty Stash", 25, 200 + hooks.length * 17, 2, 1, 0, false, hookHandler));
  83.     hooks.push(new Text("Empty Cube", 25, 200 + hooks.length * 17, 2, 1, 0, false, hookHandler));
  84.     hooks.push(new Text("Inventory to Stash", 25, 200 + hooks.length * 17, 2, 1, 0, false, hookHandler));
  85.     hooks.push(new Text("Pick Items", 25, 200 + hooks.length * 17, 2, 1, 0, false, hookHandler));
  86.  
  87.     while (true) {
  88.         switch (activeAction) {
  89.         case "Empty Inventory":
  90.             items = me.findItems(-1, 0, 3);
  91.  
  92.             if (items) {
  93.                 while (activeAction && items.length > 0) {
  94.                     Packet.dropItem(items.shift());
  95.                 }
  96.             }
  97.  
  98.             activeAction = false;
  99.  
  100.             break;
  101.         case "Empty Stash":
  102.         case "Empty Cube":
  103.             if (!me.inTown || !Town.openStash()) {
  104.                 me.overhear("Failed to open stash");
  105.  
  106.                 activeAction = false;
  107.  
  108.                 break;
  109.             }
  110.  
  111.             items = me.findItems(-1, 0, activeAction === "Empty Stash" ? 7 : 6);
  112.  
  113.             if (items) {
  114.                 while (activeAction && items.length > 0) {
  115.                     item = items.shift();
  116.  
  117.                     if (item.classid !== 549) { // Don't drop the cube
  118.                         Packet.dropItem(item);
  119.                     }
  120.                 }
  121.             }
  122.  
  123.             me.cancel();
  124.  
  125.             activeAction = false;
  126.  
  127.             break;
  128.         case "Inventory to Stash":
  129.             items = me.findItems(-1, 0, 3);
  130.  
  131.             if (items) {
  132.                 while (activeAction && items.length > 0) {
  133.                     item = items.shift();
  134.  
  135.                     if (Storage.Stash.CanFit(item)) {
  136.                         Storage.Stash.MoveTo(item);
  137.                     }
  138.                 }
  139.             }
  140.  
  141.             activeAction = false;
  142.  
  143.             break;
  144.         case "Pick Items":
  145.             item = getUnit(4, -1, 3);
  146.             items = [];
  147.  
  148.             if (item) {
  149.                 do {
  150.                     items.push(copyUnit(item));
  151.                 } while (item.getNext());
  152.             }
  153.  
  154.             while (activeAction && items.length > 0) {
  155.                 items.sort(sortPickList);
  156.  
  157.                 item = items.shift();
  158.  
  159.                 if (Town.ignoredItemTypes.indexOf(item.itemType) === -1 && Storage.Inventory.CanFit(item)) {
  160.                     Pickit.pickItem(item);
  161.                 }
  162.             }
  163.  
  164.             activeAction = false;
  165.  
  166.             break;
  167.         default:
  168.             for (i = 0; i < hooks.length; i += 1) {
  169.                 if (hooks[i].color === 1) {
  170.                     hooks[i].color = 2;
  171.                 }
  172.             }
  173.  
  174.             break;
  175.         }
  176.  
  177.         delay(100);
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement