Advertisement
DarkHorseDre

Untitled

Jun 24th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. *   @filename   ToolsThread.js
  3. *   @author     kolton
  4. *   @desc       several tools to help the player - potion use, chicken, Diablo clone stop, map reveal, quit with player
  5. */
  6.  
  7. js_strict(true);
  8.  
  9. include("json2.js");
  10. include("NTItemParser.dbl");
  11. include("OOG.js");
  12. include("AutoMule.js");
  13. include("Gambling.js");
  14. include("CraftingSystem.js");
  15. include("TorchSystem.js");
  16. include("MuleLogger.js");
  17. include("common/Attack.js");
  18. include("common/Cubing.js");
  19. include("common/CollMap.js");
  20. include("common/Config.js");
  21. include("common/Loader.js");
  22. include("common/Misc.js");
  23. include("common/Pickit.js");
  24. include("common/Pather.js");
  25. include("common/Precast.js");
  26. include("common/Prototypes.js");
  27. include("common/Runewords.js");
  28. include("common/Storage.js");
  29. include("common/Town.js");
  30.  
  31. function main() {
  32.     var i, mercHP, ironGolem, tick, merc,
  33.         debugInfo = {area: 0, currScript: "no entry"},
  34.         pingTimer = [],
  35.         quitFlag = false,
  36.         cloneWalked = false,
  37.         canQuit = true,
  38.         timerLastDrink = [];
  39.  
  40.     print("ÿc3Start ToolsThread script");
  41.     D2Bot.init();
  42.     Config.init(false);
  43.     Pickit.init(false);
  44.     Storage.Init();
  45.     CraftingSystem.buildLists();
  46.     Runewords.init();
  47.     Cubing.init();
  48.  
  49.     for (i = 0; i < 5; i += 1) {
  50.         timerLastDrink[i] = 0;
  51.     }
  52.  
  53.     // Reset core chicken
  54.     me.chickenhp = -1;
  55.     me.chickenmp = -1;
  56.  
  57.     // General functions
  58.     this.checkPing = function (print) {
  59.         // Quit after at least 5 seconds in game
  60.         if (getTickCount() - me.gamestarttime < 5000) {
  61.             return false;
  62.         }
  63.  
  64.         var i;
  65.  
  66.         for (i = 0; i < Config.PingQuit.length; i += 1) {
  67.             if (Config.PingQuit[i].Ping > 0) {
  68.                 if (me.ping >= Config.PingQuit[i].Ping) {
  69.                     me.overhead("High Ping");
  70.  
  71.                     if (pingTimer[i] === undefined || pingTimer[i] === 0) {
  72.                         pingTimer[i] = getTickCount();
  73.                     }
  74.  
  75.                     if (getTickCount() - pingTimer[i] >= Config.PingQuit[i].Duration * 1000) {
  76.                         if (print) {
  77.                             D2Bot.printToConsole("High ping (" + me.ping + "/" + Config.PingQuit[i].Ping + ") - leaving game.", 9);
  78.                         }
  79.  
  80.                         scriptBroadcast("pingquit");
  81.  
  82.                         return true;
  83.                     }
  84.                 } else {
  85.                     pingTimer[i] = 0;
  86.                 }
  87.             }
  88.         }
  89.  
  90.         return false;
  91.     };
  92.  
  93.     this.initQuitList = function () {
  94.         var i, string, obj,
  95.             temp = [];
  96.  
  97.         for (i = 0; i < Config.QuitList.length; i += 1) {
  98.             if (FileTools.exists("data/" + Config.QuitList[i] + ".json")) {
  99.                 string = Misc.fileAction("data/" + Config.QuitList[i] + ".json", 0);
  100.  
  101.                 if (string) {
  102.                     obj = JSON.parse(string);
  103.  
  104.                     if (obj && obj.hasOwnProperty("name")) {
  105.                         temp.push(obj.name);
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.  
  111.         Config.QuitList = temp.slice(0);
  112.     };
  113.  
  114.     this.getPotion = function (pottype, type) {
  115.         var i,
  116.             items = me.getItems();
  117.  
  118.         if (!items || items.length === 0) {
  119.             return false;
  120.         }
  121.  
  122.         // Get highest id = highest potion first
  123.         items.sort(function (a, b) {
  124.             return b.classid - a.classid;
  125.         });
  126.  
  127.         for (i = 0; i < items.length; i += 1) {
  128.             if (type < 3 && items[i].mode === 0 && items[i].location === 3 && items[i].itemType === pottype) {
  129.                 print("ÿc2Drinking potion from inventory.");
  130.  
  131.                 return copyUnit(items[i]);
  132.             }
  133.  
  134.             if (items[i].mode === 2 && items[i].itemType === pottype) {
  135.                 return copyUnit(items[i]);
  136.             }
  137.         }
  138.  
  139.         return false;
  140.     };
  141.  
  142.     this.togglePause = function () {
  143.         var i,  script,
  144.             scripts = ["default.dbj", "tools/townchicken.js", "tools/antihostile.js", "tools/party.js", "tools/rushthread.js"];
  145.  
  146.         for (i = 0; i < scripts.length; i += 1) {
  147.             script = getScript(scripts[i]);
  148.  
  149.             if (script) {
  150.                 if (script.running) {
  151.                     if (i === 0) { // default.dbj
  152.                         print("ÿc1Pausing.");
  153.                     }
  154.  
  155.                     // don't pause townchicken during clone walk
  156.                     if (scripts[i] !== "tools/townchicken.js" || !cloneWalked) {
  157.                         script.pause();
  158.                     }
  159.                 } else {
  160.                     if (i === 0) { // default.dbj
  161.                         print("ÿc2Resuming.");
  162.                     }
  163.  
  164.                     script.resume();
  165.                 }
  166.             }
  167.         }
  168.  
  169.         return true;
  170.     };
  171.  
  172.     this.stopDefault = function () {
  173.         var script = getScript("default.dbj");
  174.  
  175.         if (script && script.running) {
  176.             script.stop();
  177.         }
  178.  
  179.         return true;
  180.     };
  181.  
  182.     this.exit = function () {
  183.         this.stopDefault();
  184.         quit();
  185.     };
  186.  
  187.     this.drinkPotion = function (type) {
  188.         var pottype, potion,
  189.             tNow = getTickCount();
  190.  
  191.         switch (type) {
  192.         case 0:
  193.         case 1:
  194.             if ((timerLastDrink[type] && (tNow - timerLastDrink[type] < 1000)) || me.getState(type === 0 ? 100 : 106)) {
  195.                 return false;
  196.             }
  197.  
  198.             break;
  199.         case 2:
  200.             //dhd if (timerLastDrink[type] && (tNow - timerLastDrink[type] < 300)) { // small delay for juvs just to prevent using more at once
  201.             if (timerLastDrink[type] && (tNow - timerLastDrink[type] < 2000)) { // LONGER delay for juvs just to prevent using more at once, consider slow merc refresh
  202.                 return false;
  203.             }
  204.  
  205.             break;
  206.         case 4:
  207.             if (timerLastDrink[type] && (tNow - timerLastDrink[type] < 2000)) { // larger delay for juvs just to prevent using more at once, considering merc update rate
  208.                 return false;
  209.             }
  210.  
  211.             break;
  212.         default:
  213.             if (timerLastDrink[type] && (tNow - timerLastDrink[type] < 8000)) {
  214.                 return false;
  215.             }
  216.  
  217.             break;
  218.         }
  219.  
  220.         if (me.mode === 0 || me.mode === 17 || me.mode === 18) { // mode 18 - can't drink while leaping/whirling etc.
  221.             return false;
  222.         }
  223.  
  224.         switch (type) {
  225.         case 0:
  226.         case 3:
  227.             pottype = 76;
  228.  
  229.             break;
  230.         case 1:
  231.             pottype = 77;
  232.  
  233.             break;
  234.         default:
  235.             pottype = 78;
  236.  
  237.             break;
  238.         }
  239.  
  240.         potion = this.getPotion(pottype, type);
  241.  
  242.         if (potion) {
  243.             if (me.mode === 0 || me.mode === 17) {
  244.                 return false;
  245.             }
  246.  
  247.             if (type < 3) {
  248.                 potion.interact();
  249.             } else {
  250.                 try {
  251.                     clickItem(2, potion);
  252.                 } catch (e) {
  253.                     print("Couldn't give the potion to merc.");
  254.                 }
  255.             }
  256.  
  257.             timerLastDrink[type] = getTickCount();
  258.  
  259.             return true;
  260.         }
  261.  
  262.         return false;
  263.     };
  264.  
  265.     this.getNearestMonster = function () {
  266.         var gid, distance,
  267.             monster = getUnit(1),
  268.             range = 30;
  269.  
  270.         if (monster) {
  271.             do {
  272.                 if (monster.hp > 0 && Attack.checkMonster(monster) && !monster.getParent()) {
  273.                     distance = getDistance(me, monster);
  274.  
  275.                     if (distance < range) {
  276.                         range = distance;
  277.                         gid = monster.gid;
  278.                     }
  279.                 }
  280.             } while (monster.getNext());
  281.         }
  282.  
  283.         if (gid) {
  284.             monster = getUnit(1, -1, -1, gid);
  285.         } else {
  286.             monster = false;
  287.         }
  288.  
  289.         if (monster) {
  290.             return " to " + monster.name;
  291.         }
  292.  
  293.         return "";
  294.     };
  295.  
  296.     this.checkVipers = function () {
  297.         var owner,
  298.             monster = getUnit(1, 597);
  299.  
  300.         if (monster) {
  301.             do {
  302.                 if (monster.getState(96)) {
  303.                     owner = monster.getParent();
  304.  
  305.                     if (owner && owner.name !== me.name) {
  306.                         return true;
  307.                     }
  308.                 }
  309.             } while (monster.getNext());
  310.         }
  311.  
  312.         return false;
  313.     };
  314.  
  315.     this.getIronGolem = function () {
  316.         var owner,
  317.             golem = getUnit(1, 291);
  318.  
  319.         if (golem) {
  320.             do {
  321.                 owner = golem.getParent();
  322.  
  323.                 if (owner && owner.name === me.name) {
  324.                     return copyUnit(golem);
  325.                 }
  326.             } while (golem.getNext());
  327.         }
  328.  
  329.         return false;
  330.     };
  331.  
  332.     this.getNearestPreset = function () {
  333.         var i, unit, dist, id;
  334.  
  335.         unit = getPresetUnits(me.area);
  336.         dist = 99;
  337.  
  338.         for (i = 0; i < unit.length; i += 1) {
  339.             if (getDistance(me, unit[i].roomx * 5 + unit[i].x, unit[i].roomy * 5 + unit[i].y) < dist) {
  340.                 dist = getDistance(me, unit[i].roomx * 5 + unit[i].x, unit[i].roomy * 5 + unit[i].y);
  341.                 id = unit[i].type + " " + unit[i].id;
  342.             }
  343.         }
  344.  
  345.         return id || "";
  346.     };
  347.  
  348.     // Event functions
  349.     this.keyEvent = function (key) {
  350.         switch (key) {
  351.         case 19: // Pause/Break key
  352.             this.togglePause();
  353.  
  354.             break;
  355.         case 123: // F12 key
  356.             me.overhead("Revealing " + Pather.getAreaName(me.area));
  357.             revealLevel(true);
  358.  
  359.             break;
  360.         case 107: // Numpad +
  361.             showConsole();
  362.  
  363.             // me.getStat(105) will return real FCR from gear + Config.FCR from char cfg
  364.             var realFCR = me.getStat(105) - Config.FCR;
  365.             var realIAS = me.getStat(93) - Config.IAS;
  366.             var realFBR = me.getStat(102) - Config.FBR;
  367.             var realFHR = me.getStat(99) - Config.FHR;
  368.  
  369.             print("ÿc4MF: ÿc0" + me.getStat(80) + " ÿc4GF: ÿc0" + me.getStat(79) + " ÿc1FR: ÿc0" + me.getStat(39) +
  370.                 " ÿc3CR: ÿc0" + me.getStat(43) + " ÿc9LR: ÿc0" + me.getStat(41) + " ÿc2PR: ÿc0" + me.getStat(45) +
  371.                 "\n" +
  372.                 "FCR: " + realFCR + " IAS: " + realIAS + " FBR: " + realFBR +
  373.                 " FHR: " + realFHR + " FRW: " + me.getStat(96) +
  374.                 "\n" +
  375.                 "CB: " + me.getStat(136) + " DS: " + me.getStat(141) + " OW: " + me.getStat(135) +
  376.                 " ÿc1LL: ÿc0" + me.getStat(60) + " ÿc3ML: ÿc0" + me.getStat(62) +
  377.                 " DR: " + me.getStat(36) + "% + " + me.getStat(34) + " MDR: " + me.getStat(37) + "% + " + me.getStat(35) +
  378.                 "\n" +
  379.                 (me.getStat(153) > 0 ? "ÿc3Cannot be Frozenÿc1" : "" ));
  380.  
  381.             break;
  382.         case 101: // numpad 5
  383.             if (AutoMule.getInfo() && AutoMule.getInfo().hasOwnProperty("muleInfo")) {
  384.                 if (AutoMule.getMuleItems().length > 0) {
  385.                     print("ÿc2Mule triggered");
  386.                     scriptBroadcast("mule");
  387.                     this.exit();
  388.                 } else {
  389.                     me.overhead("No items to mule.");
  390.                 }
  391.             } else {
  392.                 me.overhead("Profile not enabled for muling.");
  393.             }
  394.  
  395.             break;
  396.         case 102: // Numpad 6
  397.             MuleLogger.logChar();
  398.             me.overhead("Logged char: " + me.name);
  399.  
  400.             break;
  401.         case 109: // Numpad -
  402.             Misc.spy(me.name);
  403.  
  404.             break;
  405.         case 110: // decimal point
  406.             say("/fps");
  407.  
  408.             break;
  409.         case 105: // numpad 9 - get nearest preset unit id
  410.             print(this.getNearestPreset());
  411.  
  412.             break;
  413.         case 106: // numpad * - precast
  414.             Precast.doPrecast(true);
  415.  
  416.             break;
  417.         }
  418.     };
  419.  
  420.     this.gameEvent = function (mode, param1, param2, name1, name2) {
  421.         switch (mode) {
  422.         case 0x00: // "%Name1(%Name2) dropped due to time out."
  423.         case 0x01: // "%Name1(%Name2) dropped due to errors."
  424.         case 0x03: // "%Name1(%Name2) left our world. Diablo's minions weaken."
  425.             if ((typeof Config.QuitList === "string" && Config.QuitList.toLowerCase() === "any") ||
  426.                     (Config.QuitList instanceof Array && Config.QuitList.indexOf(name1) > -1)) {
  427.                 print(name1 + (mode === 0 ? " timed out" : " left"));
  428.  
  429.                 quitFlag = true;
  430.             }
  431.  
  432.             if (Config.AntiHostile) {
  433.                 scriptBroadcast("remove " + name1);
  434.             }
  435.  
  436.             break;
  437.         case 0x06: // "%Name1 was Slain by %Name2"
  438.             if (Config.AntiHostile && param2 === 0x00 && name2 === me.name) {
  439.                 scriptBroadcast("mugshot " + name1);
  440.             }
  441.  
  442.             break;
  443.         case 0x07:
  444.             if (Config.AntiHostile && param2 === 0x03) { // "%Player has declared hostility towards you."
  445.                 scriptBroadcast("findHostiles");
  446.             }
  447.  
  448.             break;
  449.         case 0x11: // "%Param1 Stones of Jordan Sold to Merchants"
  450.             if (Config.DCloneQuit === 2) {
  451.                 D2Bot.printToConsole("SoJ sold in game. Leaving.");
  452.  
  453.                 quitFlag = true;
  454.  
  455.                 break;
  456.             }
  457.  
  458.             if (Config.SoJWaitTime && me.gametype === 1) { // only do this in expansion
  459.                 D2Bot.printToConsole(param1 + " Stones of Jordan Sold to Merchants on IP " + me.gameserverip.split(".")[3], 7);
  460.                 Messaging.sendToScript("default.dbj", "soj");
  461.             }
  462.  
  463.             break;
  464.         case 0x12: // "Diablo Walks the Earth"
  465.             if (Config.DCloneQuit > 0) {
  466.                 D2Bot.printToConsole("Diablo walked in game. Leaving.");
  467.  
  468.                 quitFlag = true;
  469.  
  470.                 break;
  471.             }
  472.  
  473.             if (Config.StopOnDClone && me.gametype === 1) { // only do this in expansion
  474.                 D2Bot.printToConsole("Diablo Walks the Earth", 7);
  475.  
  476.                 cloneWalked = true;
  477.  
  478.                 this.togglePause();
  479.                 Town.goToTown();
  480.                 showConsole();
  481.                 print("ÿc4Diablo Walks the Earth");
  482.  
  483.                 me.maxgametime = 0;
  484.  
  485.                 if (Config.KillDclone) {
  486.                     load("tools/clonekilla.js");
  487.                 }
  488.             }
  489.  
  490.             break;
  491.         }
  492.     };
  493.  
  494.     this.scriptEvent = function (msg) {
  495.         var obj;
  496.  
  497.         switch (msg) {
  498.         case "toggleQuitlist":
  499.             canQuit = !canQuit;
  500.  
  501.             break;
  502.         case "quit":
  503.             quitFlag = true;
  504.  
  505.             break;
  506.         default:
  507.             try {
  508.                 obj = JSON.parse(msg);
  509.             } catch (e) {
  510.                 return;
  511.             }
  512.  
  513.             if (obj) {
  514.                 if (obj.hasOwnProperty("currScript")) {
  515.                     debugInfo.currScript = obj.currScript;
  516.                 }
  517.  
  518.                 if (obj.hasOwnProperty("lastAction")) {
  519.                     debugInfo.lastAction = obj.lastAction;
  520.                 }
  521.  
  522.                 //D2Bot.store(JSON.stringify(debugInfo));
  523.                 DataFile.updateStats("debugInfo", JSON.stringify(debugInfo));
  524.             }
  525.  
  526.             break;
  527.         }
  528.     };
  529.  
  530.     // Cache variables to prevent a bug where d2bs loses the reference to Config object
  531.     Config = Misc.copy(Config);
  532.     tick = getTickCount();
  533.  
  534.     addEventListener("keyup", this.keyEvent);
  535.     addEventListener("gameevent", this.gameEvent);
  536.     addEventListener("scriptmsg", this.scriptEvent);
  537.     //addEventListener("gamepacket", Events.gamePacket);
  538.  
  539.     // Load Fastmod
  540.     Packet.changeStat(105, Config.FCR);
  541.     Packet.changeStat(99, Config.FHR);
  542.     Packet.changeStat(102, Config.FBR);
  543.     Packet.changeStat(93, Config.IAS);
  544.  
  545.     if (Config.QuitListMode > 0) {
  546.         this.initQuitList();
  547.     }
  548.  
  549.     // Start
  550.     while (true) {
  551.         try {
  552.             if (me.gameReady && !me.inTown) {
  553.                 if (Config.UseHP > 0 && me.hp < Math.floor(me.hpmax * Config.UseHP / 100)) {
  554.                     this.drinkPotion(0);
  555.                 }
  556.  
  557.                 if (Config.UseRejuvHP > 0 && me.hp < Math.floor(me.hpmax * Config.UseRejuvHP / 100)) {
  558.                     this.drinkPotion(2);
  559.                 }
  560.  
  561.                 if (Config.LifeChicken > 0 && me.hp <= Math.floor(me.hpmax * Config.LifeChicken / 100)) {
  562.                     D2Bot.printToConsole("Life Chicken (" + me.hp + "/" + me.hpmax + ")" + this.getNearestMonster() + " in " + Pather.getAreaName(me.area) + ". Ping: " + me.ping, 9);
  563.                     D2Bot.updateChickens();
  564.                     this.exit();
  565.  
  566.                     break;
  567.                 }
  568.  
  569.                 if (Config.UseMP > 0 && me.mp < Math.floor(me.mpmax * Config.UseMP / 100)) {
  570.                     this.drinkPotion(1);
  571.                 }
  572.  
  573.                 if (Config.UseRejuvMP > 0 && me.mp < Math.floor(me.mpmax * Config.UseRejuvMP / 100)) {
  574.                     this.drinkPotion(2);
  575.                 }
  576.  
  577.                 if (Config.ManaChicken > 0 && me.mp <= Math.floor(me.mpmax * Config.ManaChicken / 100)) {
  578.                     D2Bot.printToConsole("Mana Chicken: (" + me.mp + "/" + me.mpmax + ") in " + Pather.getAreaName(me.area), 9);
  579.                     D2Bot.updateChickens();
  580.                     this.exit();
  581.  
  582.                     break;
  583.                 }
  584.  
  585.                 if (Config.IronGolemChicken > 0 && me.classid === 2) {
  586.                     if (!ironGolem || copyUnit(ironGolem).x === undefined) {
  587.                         ironGolem = this.getIronGolem();
  588.                     }
  589.  
  590.                     if (ironGolem) {
  591.                         if (ironGolem.hp <= Math.floor(128 * Config.IronGolemChicken / 100)) { // ironGolem.hpmax is bugged with BO
  592.                             D2Bot.printToConsole("Irom Golem Chicken in " + Pather.getAreaName(me.area), 9);
  593.                             D2Bot.updateChickens();
  594.                             this.exit();
  595.  
  596.                             break;
  597.                         }
  598.                     }
  599.                 }
  600.  
  601.                 if (Config.UseMerc) {
  602.                     mercHP = getMercHP();
  603.                     merc = me.getMerc();
  604.  
  605.                     if (mercHP > 0 && merc && merc.mode !== 12) {
  606.                         if (mercHP < Config.MercChicken) {
  607.                             D2Bot.printToConsole("Merc Chicken in " + Pather.getAreaName(me.area), 9);
  608.                             D2Bot.updateChickens();
  609.                             this.exit();
  610.  
  611.                             break;
  612.                         }
  613.  
  614.                         if (mercHP < Config.UseMercHP) {
  615.                             this.drinkPotion(3);
  616.                         }
  617.  
  618.                         if (mercHP < Config.UseMercRejuv) {
  619.                             this.drinkPotion(4);
  620.                         }
  621.                     }
  622.                 }
  623.  
  624.                 if (Config.ViperCheck && getTickCount() - tick >= 250) {
  625.                     if (this.checkVipers()) {
  626.                         D2Bot.printToConsole("Revived Tomb Vipers found. Leaving game.", 9);
  627.  
  628.                         quitFlag = true;
  629.                     }
  630.  
  631.                     tick = getTickCount();
  632.                 }
  633.  
  634.                 if (this.checkPing(true)) {
  635.                     quitFlag = true;
  636.                 }
  637.             }
  638.         } catch (e) {
  639.             Misc.errorReport(e, "ToolsThread");
  640.  
  641.             quitFlag = true;
  642.         }
  643.  
  644.         if (quitFlag && canQuit) {
  645.             //dhd start edit - QUIT DELAY from zombie
  646.             var scratchNuts;
  647.             switch (Config.QuitDelay[0]) {
  648.                 case -1:
  649.                     break;
  650.                 case 0:
  651.                     scratchNuts = Math.floor((Math.random())*(Config.QuitDelay[2]-Config.QuitDelay[1]+1)+(Config.QuitDelay[1]));
  652.                     Town.goToTown();
  653.                     print("ÿc0(ÿc1Quit Delayÿc0) ÿc2Waiting for ÿc0" + scratchNuts + "ÿc2 seconds.");
  654.                     delay(scratchNuts*1000);
  655.                     break;
  656.                 default:
  657.                     scratchNuts = Config.QuitDelay[0];
  658.                     Town.goToTown();
  659.                     print("ÿc0(ÿc1Quit Delayÿc0) ÿc2Waiting for ÿc0" + scratchNuts + "ÿc2 seconds.");
  660.                     delay(scratchNuts*1000);
  661.             }
  662.             //continue after edit
  663.             print("ÿc8Run duration ÿc2" + ((getTickCount() - me.gamestarttime) / 1000));
  664.  
  665.             if (Config.LogExperience) {
  666.                 Experience.log();
  667.             }
  668.  
  669.             this.checkPing(false); // In case of quitlist triggering first
  670.             this.exit();
  671.  
  672.             break;
  673.         }
  674.  
  675.         if (debugInfo.area !== Pather.getAreaName(me.area)) {
  676.             debugInfo.area = Pather.getAreaName(me.area);
  677.  
  678.             //D2Bot.store(JSON.stringify(debugInfo));
  679.             DataFile.updateStats("debugInfo", JSON.stringify(debugInfo));
  680.         }
  681.  
  682.         delay(20);
  683.     }
  684.  
  685.     return true;
  686. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement