Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 15.05 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2. *       @filename       Misc.js
  3. *       @author         kolton
  4. *       @desc           misc library containing Skill, Misc and Sort classes
  5. */
  6.  
  7. var gameStart = getTickCount();
  8.  
  9. function timer(tick) {
  10.         if (!tick) {
  11.                 return "";
  12.         }
  13.  
  14.         var min, sec;
  15.  
  16.         min = Math.floor((getTickCount() - tick) / 60000).toString();
  17.  
  18.         if (min <= 9) {
  19.                 min = "0" + min;
  20.         }
  21.  
  22.         sec = (Math.floor((getTickCount() - tick) / 1000) % 60).toString();
  23.  
  24.         if (sec <= 9) {
  25.                 sec = "0" + sec;
  26.         }
  27.  
  28.         return min + "m " + sec + "s";
  29. }
  30.  
  31. var Skill = {
  32.         // Cast a skill on self, Unit or coords
  33.         cast: function (skillId, hand, x, y) {
  34.                 if (me.inTown && !this.townSkill(skillId)) {
  35.                         return false;
  36.                 }
  37.  
  38.                 if (!me.getSkill(skillId, 1)) {
  39.                         return false;
  40.                 }
  41.  
  42.                 if (typeof skillId === "undefined") {
  43.                         throw new Error("Skill.cast: Must supply a skill ID");
  44.                 }
  45.  
  46.                 var i, n, clickType, shift;
  47.  
  48.                 if (typeof hand === "undefined") {
  49.                         hand = 0;
  50.                 }
  51.  
  52.                 if (typeof x === "undefined") {
  53.                         x = me.x;
  54.                 }
  55.  
  56.                 if (typeof y === "undefined") {
  57.                         y = me.y;
  58.                 }
  59.  
  60.                 switch (hand) {
  61.                 case 0:
  62.                         clickType = 3;
  63.                         shift = 0;
  64.                         break;
  65.                 case 1:
  66.                         clickType = 0;
  67.                         shift = 1;
  68.                         break;
  69.                 case 2: // For melee skills that don't need shift
  70.                         clickType = 0;
  71.                         shift = 0;
  72.                         break;
  73.                 }
  74.  
  75.                 if (!this.setSkill(skillId, hand)) {
  76.                         return false;
  77.                 }
  78.  
  79. MainLoop:
  80.                 for (n = 0; n < 3; n += 1) {
  81.                         if (typeof x === "object") {
  82.                                 clickMap(clickType, shift, x);
  83.                         } else {
  84.                                 clickMap(clickType, shift, x, y);
  85.                         }
  86.  
  87.                         delay(30);
  88.  
  89.                         if (typeof x === "object") {
  90.                                 clickMap(clickType + 2, shift, x);
  91.                         } else {
  92.                                 clickMap(clickType + 2, shift, x, y);
  93.                         }
  94.  
  95.                         for (i = 0; i < 4; i += 1) {
  96.                                 if (me.attacking) {
  97.                                         break MainLoop;
  98.                                 }
  99.  
  100.                                 delay(40);
  101.                         }
  102.                 }
  103.  
  104.                 while (me.attacking) {
  105.                         delay(10);
  106.                 }
  107.  
  108.                 if (this.isTimed(skillId)) { // account for lag, state 121 doesn't kick in immediately
  109.                         for (i = 0; i < 10; i += 1) {
  110.                                 if (me.getState(121)) {
  111.                                         break;
  112.                                 }
  113.  
  114.                                 delay(10);
  115.                         }
  116.                 }
  117.  
  118.                 return true;
  119.         },
  120.  
  121.         // Put a skill on desired slot
  122.         setSkill: function (skillId, hand) {
  123.                 if (!me.getSkill(skillId, 1)) {
  124.                         return false;
  125.                 }
  126.  
  127.                 if (typeof hand === "undefined") {
  128.                         hand = 0;
  129.                 }
  130.  
  131.                 // Check if the skill is already set
  132.                 if (me.getSkill(hand === 0 ? 2 : 3) === skillId) {
  133.                         return true;
  134.                 }
  135.  
  136.                 if (me.setSkill(skillId, hand)) {
  137.                         return true;
  138.                 }
  139.  
  140.                 return false;
  141.         },
  142.  
  143.         // Timed skills
  144.         isTimed: function (skillId) {
  145.                 return [15, 25, 27, 51, 56, 59, 62, 64, 121, 225, 223, 228, 229, 234, 244, 247, 249, 250, 256, 268, 275, 277, 279].indexOf(skillId) > -1;
  146.         },
  147.  
  148.         // Skills that cn be cast in town
  149.         townSkill: function (skillId) {
  150.                 return [32, 40, 43, 50, 52, 58, 60, 68, 75, 85, 94, 117, 221, 222, 226, 227, 235, 236, 237, 246, 247, 258, 267, 268, 277, 278, 279].indexOf(skillId) > -1;
  151.         }
  152. };
  153.  
  154. var Misc = {
  155.         // Click something
  156.         click: function (button, shift, x, y) {
  157.                 if (arguments.length < 2) {
  158.                         throw new Error("Misc.click: Needs at least 2 arguments.");
  159.                 }
  160.  
  161.                 switch (arguments.length) {
  162.                 case 2:
  163.                         clickMap(button, shift, me.x, me.y);
  164.                         delay(20);
  165.                         clickMap(button + 2, shift, me.x, me.y);
  166.                         break;
  167.                 case 3:
  168.                         if (typeof (x) !== "object") {
  169.                                 throw new Error("Misc.click: Third arg must be a Unit.");
  170.                         }
  171.  
  172.                         clickMap(button, shift, x);
  173.                         delay(20);
  174.                         clickMap(button + 2, shift, x);
  175.                         break;
  176.                 case 4:
  177.                         clickMap(button, shift, x, y);
  178.                         delay(20);
  179.                         clickMap(button + 2, shift, x, y);
  180.                         break;
  181.                 }
  182.  
  183.                 return true;
  184.         },
  185.  
  186.         // Check if a player is in your party
  187.         inMyParty: function (name) {
  188.                 var player, myPartyId;
  189.  
  190.                 try {
  191.                         player = getParty();
  192.  
  193.                         if (!player) {
  194.                                 return false;
  195.                         }
  196.  
  197.                         myPartyId = player.partyid;
  198.                         player = getParty(name); // May throw an error
  199.  
  200.                         if (player && player.partyid !== 65535 && player.partyid === myPartyId) {
  201.                                 return true;
  202.                         }
  203.                 } catch (e) {
  204.                         player = getParty();
  205.  
  206.                         if (player) {
  207.                                 myPartyId = player.partyid;
  208.  
  209.                                 while (player.getNext()) {
  210.                                         if (player.partyid !== 65535 && player.partyid === myPartyId) {
  211.                                                 return true;
  212.                                         }
  213.                                 }
  214.                         }
  215.                 }
  216.  
  217.                 return false;
  218.         },
  219.  
  220.         // Open a chest Unit
  221.         openChest: function (unit) {
  222.                 if (!unit || unit.mode || unit.x === 12526 || unit.x === 12565) { // Skip invalid, opened and Countess chests
  223.                         return false;
  224.                 }
  225.  
  226.                 if (me.classid !== 6 && unit.islocked && !me.findItem("key", 0, 3)) { // locked chest, no keys
  227.                         return false;
  228.                 }
  229.  
  230.                 var i, tick;
  231.  
  232.                 for (i = 0; i < 3; i += 1) {
  233.                         if (getDistance(me, unit) < 4 || Pather.moveToUnit(unit, 2, 0)) {
  234.                                 unit.interact();
  235.                         }
  236.  
  237.                         tick = getTickCount();
  238.  
  239.                         while (getTickCount() - tick < 1000) {
  240.                                 if (unit.mode) {
  241.                                         return true;
  242.                                 }
  243.  
  244.                                 delay(10);
  245.                         }
  246.                 }
  247.  
  248.                 return false;
  249.         },
  250.  
  251.         // Open all chests that have preset units in an area
  252.         openChestsInArea: function (area) {
  253.                 if (!area) {
  254.                         area = me.area;
  255.                 }
  256.  
  257.                 var chest,
  258.                         presetUnits = getPresetUnits(area),
  259.                         chestIds = [5, 6, 87, 92, 104, 105, 106, 107, 143, 140, 141, 144, 146, 147, 148, 176, 177, 181, 183, 198, 240, 241, 242, 243, 329, 330, 331, 332, 333, 334, 335,
  260.                                                 336, 354, 355, 356, 371, 387, 389, 390, 391, 397, 405, 406, 407, 413, 420, 424, 425, 430, 431, 432, 433, 454, 455, 501, 502, 504, 505, 580, 581];
  261.  
  262.                 if (!presetUnits) {
  263.                         return false;
  264.                 }
  265.  
  266.                 while (presetUnits.length > 0) {
  267.                         presetUnits.sort(Sort.presetUnits);
  268.  
  269.                         if (chestIds.indexOf(presetUnits[0].id) > -1) {
  270.                                 Pather.moveToUnit(presetUnits[0], 2, 0);
  271.  
  272.                                 chest = getUnit(2);
  273.  
  274.                                 if (chest) {
  275.                                         do {
  276.                                                 if (chestIds.indexOf(chest.classid) > -1 && getDistance(me, chest) < 5 && this.openChest(chest)) {
  277.                                                         Pickit.pickItems();
  278.                                                 }
  279.                                         } while (chest.getNext());
  280.                                 }
  281.                         }
  282.  
  283.                         presetUnits.shift();
  284.                 }
  285.  
  286.                 return true;
  287.         },
  288.  
  289.         // Use a shrine Unit
  290.         getShrine: function (unit) {
  291.                 var i, tick;
  292.  
  293.                 for (i = 0; i < 3; i += 1) {
  294.                         if (getDistance(me, unit) < 4 || Pather.moveToUnit(unit, 2, 0)) {
  295.                                 unit.interact();
  296.                         }
  297.  
  298.                         tick = getTickCount();
  299.  
  300.                         while (getTickCount() - tick < 1000) {
  301.                                 if (unit.mode) {
  302.                                         return true;
  303.                                 }
  304.  
  305.                                 delay(10);
  306.                         }
  307.                 }
  308.  
  309.                 return false;
  310.         },
  311.  
  312.         // Check all shrines in area and get the first one of specified type
  313.         getShrinesInArea: function (area, type) {
  314.                 var i, coords, shrine,
  315.                         shrineLocs = [],
  316.                         shrineIds = [2, 81, 83],
  317.                         unit = getPresetUnits(area);
  318.  
  319.                 if (unit) {
  320.                         for (i = 0; i < unit.length; i += 1) {
  321.                                 if (shrineIds.indexOf(unit[i].id) > -1) {
  322.                                         shrineLocs.push([unit[i].roomx * 5 + unit[i].x, unit[i].roomy * 5 + unit[i].y]);
  323.                                 }
  324.                         }
  325.                 }
  326.  
  327.                 while (shrineLocs.length > 0) {
  328.                         shrineLocs.sort(Sort.points);
  329.  
  330.                         coords = shrineLocs.shift();
  331.  
  332.                         Pather.moveTo(coords[0], coords[1], 2, false, true);
  333.  
  334.                         shrine = getUnit(2, "shrine");
  335.  
  336.                         if (shrine) {
  337.                                 do {
  338.                                         if (shrine.objtype === type) {
  339.                                                 this.getShrine(shrine);
  340.  
  341.                                                 return true;
  342.                                         }
  343.                                 } while (shrine.getNext());
  344.                         }
  345.                 }
  346.  
  347.                 return false;
  348.         },
  349.  
  350.         // Log kept item stats in the manager.
  351.         logItem: function (action, unit, keptLine) {
  352.                 var i, val, code, desc,
  353.                         stringColor = "",
  354.                         color = -1,
  355.                         name = unit.fname.split("\n").reverse().join(" ").replace(/ÿc[0-9!"+<;.*]|^ /, "");
  356.  
  357.                 desc = unit.description.split("\n");
  358.  
  359.                 // Lines are normally in reverse. Add color tags if needed and reverse order.
  360.                 for (i = 0; i < desc.length; i += 1) {
  361.                         if (desc[i].match(/^ÿ/)) {
  362.                                 stringColor = desc[i].substring(0, 3);
  363.                         } else {
  364.                                 desc[i] = stringColor + desc[i];
  365.                         }
  366.                 }
  367.  
  368.                 desc = desc.reverse().join("\n");
  369.                 color = unit.getColor();
  370.                 desc += ("\nÿc0Item Level: " + unit.ilvl);
  371.  
  372.                 if (action === "Kept") {
  373.                         val = DataFile.getStats().lastArea;
  374.  
  375.                         if (val) {
  376.                                 desc += ("\nÿc0Area: " + val);
  377.                         }
  378.                 }
  379.  
  380.                 code = getBaseStat(0, unit.classid, 'normcode') || unit.code;
  381.                 code = code.replace(" ", "");
  382.  
  383.                 if ([10, 12, 58, 82, 83, 84].indexOf(unit.itemType) > -1) {
  384.                         code += (unit.gfx + 1);
  385.                 }
  386.  
  387.                 if (keptLine) {
  388.                         desc += ("\nÿc0Line: " + keptLine);
  389.                 }
  390.  
  391.                 D2Bot.printToItemLog(action + " " + name, desc, code, 0, color);
  392.         },
  393.  
  394.         // Change into werewolf or werebear
  395.         shapeShift: function (mode) { // 0 = werewolf, 1 = werebear
  396.                 if (arguments.length === 0 || mode < 0 || mode > 2) {
  397.                         throw new Error("Misc.shapeShift: Invalid parameter");
  398.                 }
  399.  
  400.                 var i, tick,
  401.                         state = mode === 0 ? 139 : 140,
  402.                         skill = mode === 0 ? 223 : 228;
  403.  
  404.                 for (i = 0; i < 3; i += 1) {
  405.                         Skill.cast(skill, 0);
  406.  
  407.                         tick = getTickCount();
  408.  
  409.                         while (getTickCount() - tick < 2000) {
  410.                                 if (me.getState(state)) {
  411.                                         return true;
  412.                                 }
  413.  
  414.                                 delay(10);
  415.                         }
  416.                 }
  417.  
  418.                 return false;
  419.         },
  420.  
  421.         // Change back to human shape
  422.         unShift: function () {
  423.                 var i, tick;
  424.  
  425.                 if (me.getState(139) || me.getState(140)) {
  426.                         for (i = 0; i < 3; i += 1) {
  427.                                 Skill.cast(me.getState(139) ? 223 : 228);
  428.  
  429.                                 tick = getTickCount();
  430.  
  431.                                 while (getTickCount() - tick < 2000) {
  432.                                         if (!me.getState(139) && !me.getState(140)) {
  433.                                                 return true;
  434.                                         }
  435.  
  436.                                         delay(10);
  437.                                 }
  438.                         }
  439.                 } else {
  440.                         return true;
  441.                 }
  442.  
  443.                 return false;
  444.         },
  445.  
  446.         // Teleport with slot II
  447.         teleSwitch: function () {
  448.                 this.oldSwitch = me.weaponswitch;
  449.  
  450.                 Precast.weaponSwitch();
  451.  
  452.                 return true;
  453.         },
  454.  
  455.         // Go to town when low on hp/mp or when out of potions. can be upgraded to check for curses etc.
  456.         townCheck: function () {
  457.                 var potion, check,
  458.                         needhp = true,
  459.                         needmp = true;
  460.  
  461.                 if (Config.TownCheck && !me.inTown) {
  462.                         if (Config.BeltColumn.indexOf("hp") > -1) {
  463.                                 potion = me.getItem(-1, 2); // belt item
  464.  
  465.                                 if (potion) {
  466.                                         do {
  467.                                                 if (potion.code.indexOf("hp") > -1) {
  468.                                                         needhp = false;
  469.  
  470.                                                         break;
  471.                                                 }
  472.                                         } while (potion.getNext());
  473.                                 }
  474.  
  475.                                 if (needhp) {
  476.                                         print("We need healing potions");
  477.  
  478.                                         check = true;
  479.                                 }
  480.                         }
  481.  
  482.                         if (Config.BeltColumn.indexOf("mp") > -1) {
  483.                                 potion = me.getItem(-1, 2); // belt item
  484.  
  485.                                 if (potion) {
  486.                                         do {
  487.                                                 if (potion.code.indexOf("mp") > -1) {
  488.                                                         needmp = false;
  489.  
  490.                                                         break;
  491.                                                 }
  492.                                         } while (potion.getNext());
  493.                                 }
  494.  
  495.                                 if (needmp) {
  496.                                         print("We need mana potions");
  497.  
  498.                                         check = true;
  499.                                 }
  500.                         }
  501.                 }
  502.  
  503.                 if (check) {
  504.                         Town.goToTown();
  505.                         Town.heal();
  506.                         Town.buyPotions();
  507.                         Town.reviveMerc();
  508.                         me.cancel();
  509.                         Town.move("portalspot");
  510.  
  511.                         if (!Pather.usePortal(null, me.name)) {
  512.                                 throw new Error("Misc.townCheck: Failed to use portal.");
  513.                         }
  514.  
  515.                         if (Config.PublicMode) {
  516.                                 Pather.makePortal();
  517.                         }
  518.  
  519.                         return true;
  520.                 }
  521.  
  522.                 return false;
  523.         }
  524. };
  525.  
  526. var Sort = {
  527.         // Sort units by comparing distance between the player
  528.         units: function (a, b) {
  529.                 return getDistance(me, a) - getDistance(me, b);
  530.         },
  531.  
  532.         // Sort preset units by comparing distance between the player (using preset x/y calculations)
  533.         presetUnits: function (a, b) {
  534.                 return getDistance(me, a.roomx * 5 + a.x, a.roomy * 5 + a.y) - getDistance(me, b.roomx * 5 + b.x, b.roomy * 5 + b.y);
  535.         },
  536.  
  537.         // Sort arrays of x,y coords by comparing distance between the player
  538.         points: function (a, b) {
  539.                 return getDistance(me, a[0], a[1]) - getDistance(me, b[0], b[1]);
  540.         }
  541. };
  542.  
  543. var Experience = {
  544.         totalExp: [0, 0, 500, 1500, 3750, 7875, 14175, 22680, 32886, 44396, 57715, 72144, 90180, 112725, 140906, 176132, 220165, 275207, 344008, 430010, 537513, 671891, 839864, 1049830, 1312287, 1640359, 2050449, 2563061, 3203826, 3902260, 4663553, 5493363, 6397855, 7383752, 8458379, 9629723, 10906488, 12298162, 13815086, 15468534, 17270791, 19235252, 21376515, 23710491, 26254525, 29027522, 32050088, 35344686, 38935798, 42850109, 47116709, 51767302, 56836449, 62361819, 68384473, 74949165, 82104680, 89904191, 98405658, 107672256, 117772849, 128782495, 140783010, 153863570, 168121381, 183662396, 200602101, 219066380, 239192444, 261129853, 285041630, 311105466, 339515048, 370481492, 404234916, 441026148, 481128591, 524840254, 572485967, 624419793, 681027665, 742730244, 809986056, 883294891, 963201521, 1050299747, 1145236814, 1248718217, 1361512946, 1484459201, 1618470619, 1764543065, 1923762030, 2097310703, 2286478756, 2492671933, 2717422497, 2962400612, 3229426756, 3520485254, 0, 0],
  545.         nextExp: [0, 500, 1000, 2250, 4125, 6300, 8505, 10206, 11510, 13319, 14429, 18036, 22545, 28181, 35226, 44033, 55042, 68801, 86002, 107503, 134378, 167973, 209966, 262457, 328072, 410090, 512612, 640765, 698434, 761293, 829810, 904492, 985897, 1074627, 1171344, 1276765, 1391674, 1516924, 1653448, 1802257, 1964461, 2141263, 2333976, 2544034, 2772997, 3022566, 3294598, 3591112, 3914311, 4266600, 4650593, 5069147, 5525370, 6022654, 6564692, 7155515, 7799511, 8501467, 9266598, 10100593, 11009646, 12000515, 13080560, 14257811, 15541015, 16939705, 18464279, 20126064, 21937409, 23911777, 26063836, 28409582, 30966444, 33753424, 36791232, 40102443, 43711663, 47645713, 51933826, 56607872, 61702579, 67255812, 73308835, 79906630, 87098226, 94937067, 103481403, 112794729, 122946255, 134011418, 146072446, 159218965, 173548673, 189168053, 206193177, 224750564, 244978115, 267026144, 291058498, 0, 0],
  546.  
  547.         // Percent progress into the current level. Format: xx.xx%
  548.         progress: function () {
  549.                 return me.getStat(12) === 99 ? 0 : (((me.getStat(13) - this.totalExp[me.getStat(12)]) / this.nextExp[me.getStat(12)]) * 100).toFixed(2);
  550.         },
  551.  
  552.         // Total experience gained in current run
  553.         gain: function () {
  554.                 return (me.getStat(13) - DataFile.getStats().experience);
  555.         },
  556.  
  557.         // Percent experience gained in current run
  558.         gainPercent: function () {
  559.                 return me.getStat(12) === 99 ? 0 : (this.gain() * 100 / this.nextExp[me.getStat(12)]).toFixed(6);
  560.         },
  561.  
  562.         // Runs until next level
  563.         runsToLevel: function () {
  564.                 return Math.round(((100 - this.progress()) / 100) * this.nextExp[me.getStat(12)] / this.gain());
  565.         },
  566.  
  567.         // Total runs needed for next level (not counting current progress)
  568.         totalRunsToLevel: function () {
  569.                 return Math.round(this.nextExp[me.getStat(12)] / this.gain());
  570.         },
  571.  
  572.         // Log to manager
  573.         log: function () {
  574.                 var string,
  575.                         gain = this.gain(),
  576.                         progress = this.progress(),
  577.                         runsToLevel = this.runsToLevel(),
  578.                         totalRunsToLevel = this.totalRunsToLevel();
  579.  
  580.                 string = "[Level: " + me.getStat(12) + ", " + progress + "%] [XP Gain: " + gain + "] [Lvl eta: " + runsToLevel + " games] [Time: " + timer(gameStart) + "]";
  581.  
  582.                 if (gain) {
  583.                         D2Bot.printToConsole(string + ";4");
  584.  
  585.                         if (me.getStat(12) > DataFile.getStats().level) {
  586.                                 D2Bot.printToConsole("Congrats! You gained a level. Current level:" + me.getStat(12) + ";5");
  587.                         }
  588.                 }
  589.         }
  590. };
  591.  
  592. var Packet = {
  593.         castSkill: function (hand, wX, wY) {
  594.                 hand = (hand === 0) ? 0x0c : 0x05;
  595.                 sendPacket(1, hand, 2, wX, 2, wY);
  596.         },
  597.         unitCast: function (hand, who) {
  598.                 hand = (hand === 0) ? 0x11 : 0x0a;
  599.                 sendPacket(1, hand, 4, who.type, 4, who.gid);
  600.         },
  601.         moveNPC: function (npc, dwX, dwY) {
  602.                 sendPacket(1, 0x59, 4, npc.type, 4, npc.gid, 4, dwX, 4, dwY);
  603.         },
  604.         teleWalk: function (wX, wY) {
  605.                 sendPacket(1, 0x5f, 2, wX, 2, wY);
  606.                 delay(me.ping + 5);
  607.                 sendPacket(1, 0x4b, 4, me.type, 4, me.gid);
  608.         },
  609.         flash: function (gid) {
  610.                 sendPacket(1, 0x4b, 4, 0, 4, gid);
  611.         },
  612.         changeStat: function (stat, value) {
  613.                 if (value > 0) {
  614.                         getPacket(1, 0x1d, 1, stat, 1, value);
  615.                 }
  616.         }
  617. };