Advertisement
kolton

Untitled

Nov 30th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.83 KB | None | 0 0
  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.getPotion = function (pottype, type) {
  94. var i,
  95. items = me.getItems();
  96.  
  97. if (!items || items.length === 0) {
  98. return false;
  99. }
  100.  
  101. // Get highest id = highest potion first
  102. items.sort(function (a, b) {
  103. return b.classid - a.classid;
  104. });
  105.  
  106. for (i = 0; i < items.length; i += 1) {
  107. if (type < 3 && items[i].mode === 0 && items[i].location === 3 && items[i].itemType === pottype) {
  108. print("ÿc2Drinking potion from inventory.");
  109.  
  110. return copyUnit(items[i]);
  111. }
  112.  
  113. if (items[i].mode === 2 && items[i].itemType === pottype) {
  114. return copyUnit(items[i]);
  115. }
  116. }
  117.  
  118. return false;
  119. };
  120.  
  121. this.togglePause = function () {
  122. var i, script,
  123. scripts = ["default.dbj", "tools/townchicken.js", "tools/antihostile.js", "tools/party.js", "tools/flashthread.js", "tools/rushthread.js"];
  124.  
  125. for (i = 0; i < scripts.length; i += 1) {
  126. script = getScript(scripts[i]);
  127.  
  128. if (script) {
  129. if (script.running) {
  130. if (i === 0) { // default.dbj
  131. print("ÿc1Pausing.");
  132. }
  133.  
  134. // don't pause townchicken during clone walk
  135. if (scripts[i] !== "tools/townchicken.js" || !cloneWalked) {
  136. script.pause();
  137. }
  138. } else {
  139. if (i === 0) { // default.dbj
  140. print("ÿc2Resuming.");
  141. }
  142.  
  143. script.resume();
  144. }
  145. }
  146. }
  147.  
  148. return true;
  149. };
  150.  
  151. this.stopDefault = function () {
  152. var script = getScript("default.dbj");
  153.  
  154. if (script && script.running) {
  155. script.stop();
  156. }
  157.  
  158. return true;
  159. };
  160.  
  161. this.exit = function () {
  162. this.stopDefault();
  163. quit();
  164. };
  165.  
  166. this.drinkPotion = function (type) {
  167. var pottype, potion,
  168. tNow = getTickCount();
  169.  
  170. switch (type) {
  171. case 0:
  172. case 1:
  173. if ((timerLastDrink[type] && (tNow - timerLastDrink[type] < 1000)) || me.getState(type === 0 ? 100 : 106)) {
  174. return false;
  175. }
  176.  
  177. break;
  178. case 2:
  179. case 4:
  180. if (timerLastDrink[type] && (tNow - timerLastDrink[type] < 500)) { // small delay for juvs just to prevent using more at once
  181. return false;
  182. }
  183.  
  184. break;
  185. default:
  186. if (timerLastDrink[type] && (tNow - timerLastDrink[type] < 8000)) {
  187. return false;
  188. }
  189.  
  190. break;
  191. }
  192.  
  193. if (me.mode === 0 || me.mode === 17 || me.mode === 18) { // mode 18 - can't drink while leaping/whirling etc.
  194. return false;
  195. }
  196.  
  197. switch (type) {
  198. case 0:
  199. case 3:
  200. pottype = 76;
  201.  
  202. break;
  203. case 1:
  204. pottype = 77;
  205.  
  206. break;
  207. default:
  208. pottype = 78;
  209.  
  210. break;
  211. }
  212.  
  213. potion = this.getPotion(pottype, type);
  214.  
  215. if (potion) {
  216. if (me.mode === 0 || me.mode === 17) {
  217. return false;
  218. }
  219.  
  220. if (type < 3) {
  221. potion.interact();
  222. } else {
  223. try {
  224. clickItem(2, potion);
  225. } catch (e) {
  226. print("Couldn't give the potion to merc.");
  227. }
  228. }
  229.  
  230. timerLastDrink[type] = getTickCount();
  231.  
  232. return true;
  233. }
  234.  
  235. return false;
  236. };
  237.  
  238. this.getNearestMonster = function () {
  239. var gid, distance,
  240. monster = getUnit(1),
  241. range = 30;
  242.  
  243. if (monster) {
  244. do {
  245. if (monster.hp > 0 && Attack.checkMonster(monster) && !monster.getParent()) {
  246. distance = getDistance(me, monster);
  247.  
  248. if (distance < range) {
  249. range = distance;
  250. gid = monster.gid;
  251. }
  252. }
  253. } while (monster.getNext());
  254. }
  255.  
  256. if (gid) {
  257. monster = getUnit(1, -1, -1, gid);
  258. } else {
  259. monster = false;
  260. }
  261.  
  262. if (monster) {
  263. return " to " + monster.name;
  264. }
  265.  
  266. return "";
  267. };
  268.  
  269. this.checkVipers = function () {
  270. var monster = getUnit(1, 597);
  271.  
  272. if (monster) {
  273. do {
  274. if (monster.getState(96) && monster.getParent().name !== me.name) {
  275. return true;
  276. }
  277. } while (monster.getNext());
  278. }
  279.  
  280. return false;
  281. };
  282.  
  283. this.getIronGolem = function () {
  284. var owner,
  285. golem = getUnit(1, 291);
  286.  
  287. if (golem) {
  288. do {
  289. owner = golem.getParent();
  290.  
  291. if (owner && owner.name === me.name) {
  292. return copyUnit(golem);
  293. }
  294. } while (golem.getNext());
  295. }
  296.  
  297. return false;
  298. };
  299.  
  300. this.getNearestPreset = function () {
  301. var i, unit, dist, id;
  302.  
  303. unit = getPresetUnits(me.area);
  304. dist = 99;
  305.  
  306. for (i = 0; i < unit.length; i += 1) {
  307. if (getDistance(me, unit[i].roomx * 5 + unit[i].x, unit[i].roomy * 5 + unit[i].y) < dist) {
  308. dist = getDistance(me, unit[i].roomx * 5 + unit[i].x, unit[i].roomy * 5 + unit[i].y);
  309. id = unit[i].type + " " + unit[i].id;
  310. }
  311. }
  312.  
  313. return id || "";
  314. };
  315.  
  316. // Event functions
  317. this.keyEvent = function (key) {
  318. switch (key) {
  319. case 19: // Pause/Break key
  320. this.togglePause();
  321.  
  322. break;
  323. case 123: // F12 key
  324. me.overhead("Revealing " + Pather.getAreaName(me.area));
  325. revealLevel(true);
  326.  
  327. break;
  328. case 107: // Numpad +
  329. showConsole();
  330. print("ÿc4MF: ÿc0" + me.getStat(80) + " ÿc4GF: ÿc0" + me.getStat(79) + " ÿc1FR: ÿc0" + me.getStat(39) +
  331. " ÿc3CR: ÿc0" + me.getStat(43) + " ÿc9LR: ÿc0" + me.getStat(41) + " ÿc2PR: ÿc0" + me.getStat(45));
  332.  
  333. break;
  334. case 101: // numpad 5
  335. if (AutoMule.getInfo() && AutoMule.getInfo().hasOwnProperty("muleInfo")) {
  336. if (AutoMule.getMuleItems().length > 0) {
  337. print("ÿc2Mule triggered");
  338. scriptBroadcast("mule");
  339. this.exit();
  340. } else {
  341. me.overhead("No items to mule.");
  342. }
  343. } else {
  344. me.overhead("Profile not enabled for muling.");
  345. }
  346.  
  347. break;
  348. case 102:
  349. Misc.spy(me.name);
  350.  
  351. break;
  352. case 109: // Numpad -
  353. Misc.spy(me.name);
  354.  
  355. break;
  356. case 110: // decimal point
  357. say("/fps");
  358.  
  359. break;
  360. case 105: // numpad 9 - get nearest preset unit id
  361. print(this.getNearestPreset());
  362.  
  363. break;
  364. }
  365. };
  366.  
  367. this.gameEvent = function (mode, param1, param2, name1, name2) {
  368. switch (mode) {
  369. case 0x00: // "%Name1(%Name2) dropped due to time out."
  370. case 0x01: // "%Name1(%Name2) dropped due to errors."
  371. case 0x03: // "%Name1(%Name2) left our world. Diablo's minions weaken."
  372. if ((typeof Config.QuitList === "string" && Config.QuitList.toLowerCase() === "any") ||
  373. (Config.QuitList instanceof Array && Config.QuitList.indexOf(name1) > -1)) {
  374. print(name1 + (mode === 0 ? " timed out" : " left"));
  375.  
  376. quitFlag = true;
  377. }
  378.  
  379. if (Config.AntiHostile) {
  380. scriptBroadcast("remove " + name1);
  381. }
  382.  
  383. break;
  384. case 0x06: // "%Name1 was Slain by %Name2"
  385. if (Config.AntiHostile && param2 === 0x00 && name2 === me.name) {
  386. scriptBroadcast("mugshot " + name1);
  387. }
  388.  
  389. break;
  390. case 0x07:
  391. if (Config.AntiHostile && param2 === 0x03) { // "%Player has declared hostility towards you."
  392. scriptBroadcast("findHostiles");
  393. }
  394.  
  395. break;
  396. case 0x11: // "%Param1 Stones of Jordan Sold to Merchants"
  397. if (Config.DCloneQuit === 2) {
  398. D2Bot.printToConsole("SoJ sold in game. Leaving.");
  399.  
  400. quitFlag = true;
  401.  
  402. break;
  403. }
  404.  
  405. if (Config.SoJWaitTime && me.gametype === 1) { // only do this in expansion
  406. D2Bot.printToConsole(param1 + " Stones of Jordan Sold to Merchants on IP " + me.gameserverip.split(".")[3], 7);
  407. Messaging.sendToScript("default.dbj", "soj");
  408. }
  409.  
  410. break;
  411. case 0x12: // "Diablo Walks the Earth"
  412. if (Config.DCloneQuit > 0) {
  413. D2Bot.printToConsole("Diablo walked in game. Leaving.");
  414.  
  415. quitFlag = true;
  416.  
  417. break;
  418. }
  419.  
  420. if (Config.StopOnDClone && me.gametype === 1) { // only do this in expansion
  421. D2Bot.printToConsole("Diablo Walks the Earth", 7);
  422.  
  423. cloneWalked = true;
  424.  
  425. this.togglePause();
  426. Town.goToTown();
  427. showConsole();
  428. print("ÿc4Diablo Walks the Earth");
  429.  
  430. me.maxgametime = 0;
  431.  
  432. if (Config.KillDclone) {
  433. load("tools/clonekilla.js");
  434. }
  435. }
  436.  
  437. break;
  438. }
  439. };
  440.  
  441. this.scriptEvent = function (msg) {
  442. var obj;
  443.  
  444. switch (msg) {
  445. case "toggleQuitlist":
  446. canQuit = !canQuit;
  447.  
  448. break;
  449. case "quit":
  450. quitFlag = true;
  451.  
  452. break;
  453. default:
  454. try {
  455. obj = JSON.parse(msg);
  456. } catch (e) {
  457. return;
  458. }
  459.  
  460. if (obj) {
  461. if (obj.hasOwnProperty("currScript")) {
  462. debugInfo.currScript = obj.currScript;
  463. }
  464.  
  465. if (obj.hasOwnProperty("lastAction")) {
  466. debugInfo.lastAction = obj.lastAction;
  467. }
  468.  
  469. //D2Bot.store(JSON.stringify(debugInfo));
  470. DataFile.updateStats("debugInfo", JSON.stringify(debugInfo));
  471. }
  472.  
  473. break;
  474. }
  475. };
  476.  
  477. // Cache variables to prevent a bug where d2bs loses the reference to Config object
  478. Config = Misc.copy(Config);
  479. tick = getTickCount();
  480.  
  481. addEventListener("keyup", this.keyEvent);
  482. addEventListener("gameevent", this.gameEvent);
  483. addEventListener("scriptmsg", this.scriptEvent);
  484. //addEventListener("gamepacket", Events.gamePacket);
  485.  
  486. // Load Fastmod
  487. Packet.changeStat(105, Config.FCR);
  488. Packet.changeStat(99, Config.FHR);
  489. Packet.changeStat(102, Config.FBR);
  490. Packet.changeStat(93, Config.IAS);
  491.  
  492. // Start
  493. while (true) {
  494. try {
  495. if (me.gameReady && !me.inTown) {
  496. if (Config.UseHP > 0 && me.hp < Math.floor(me.hpmax * Config.UseHP / 100)) {
  497. this.drinkPotion(0);
  498. }
  499.  
  500. if (Config.UseRejuvHP > 0 && me.hp < Math.floor(me.hpmax * Config.UseRejuvHP / 100)) {
  501. this.drinkPotion(2);
  502. }
  503.  
  504. if (Config.LifeChicken > 0 && me.hp <= Math.floor(me.hpmax * Config.LifeChicken / 100)) {
  505. D2Bot.printToConsole("Life Chicken (" + me.hp + "/" + me.hpmax + ")" + this.getNearestMonster() + " in " + Pather.getAreaName(me.area) + ". Ping: " + me.ping, 9);
  506. D2Bot.updateChickens();
  507. this.exit();
  508.  
  509. break;
  510. }
  511.  
  512. if (Config.UseMP > 0 && me.mp < Math.floor(me.mpmax * Config.UseMP / 100)) {
  513. this.drinkPotion(1);
  514. }
  515.  
  516. if (Config.UseRejuvMP > 0 && me.mp < Math.floor(me.mpmax * Config.UseRejuvMP / 100)) {
  517. this.drinkPotion(2);
  518. }
  519.  
  520. if (Config.ManaChicken > 0 && me.mp <= Math.floor(me.mpmax * Config.ManaChicken / 100)) {
  521. D2Bot.printToConsole("Mana Chicken: (" + me.mp + "/" + me.mpmax + ") in " + Pather.getAreaName(me.area), 9);
  522. D2Bot.updateChickens();
  523. this.exit();
  524.  
  525. break;
  526. }
  527.  
  528. if (Config.IronGolemChicken > 0 && me.classid === 2) {
  529. if (!ironGolem || copyUnit(ironGolem).x === undefined) {
  530. ironGolem = this.getIronGolem();
  531. }
  532.  
  533. if (ironGolem) {
  534. if (ironGolem.hp <= Math.floor(128 * Config.IronGolemChicken / 100)) { // ironGolem.hpmax is bugged with BO
  535. D2Bot.printToConsole("Irom Golem Chicken in " + Pather.getAreaName(me.area), 9);
  536. D2Bot.updateChickens();
  537. this.exit();
  538.  
  539. break;
  540. }
  541. }
  542. }
  543.  
  544. if (Config.UseMerc) {
  545. mercHP = getMercHP();
  546. merc = me.getMerc();
  547.  
  548. if (mercHP > 0 && merc && merc.mode !== 12) {
  549. if (mercHP < Config.MercChicken) {
  550. D2Bot.printToConsole("Merc Chicken in " + Pather.getAreaName(me.area), 9);
  551. D2Bot.updateChickens();
  552. this.exit();
  553.  
  554. break;
  555. }
  556.  
  557. if (mercHP < Config.UseMercHP) {
  558. this.drinkPotion(3);
  559. }
  560.  
  561. if (mercHP < Config.UseMercRejuv) {
  562. this.drinkPotion(4);
  563. }
  564. }
  565. }
  566.  
  567. if (Config.ViperCheck && getTickCount() - tick >= 250) {
  568. if (this.checkVipers()) {
  569. D2Bot.printToConsole("Revived Tomb Vipers found. Leaving game.", 9);
  570.  
  571. quitFlag = true;
  572. }
  573.  
  574. tick = getTickCount();
  575. }
  576.  
  577. if (this.checkPing(true)) {
  578. quitFlag = true;
  579. }
  580. }
  581. } catch (e) {
  582. Misc.errorReport(e, "ToolsThread");
  583.  
  584. quitFlag = true;
  585. }
  586.  
  587. if (quitFlag && canQuit) {
  588. print("ÿc8Run duration ÿc2" + ((getTickCount() - me.gamestarttime) / 1000));
  589.  
  590. if (Config.LogExperience) {
  591. Experience.log();
  592. }
  593.  
  594. this.checkPing(false); // In case of quitlist triggering first
  595. this.exit();
  596.  
  597. break;
  598. }
  599.  
  600. if (debugInfo.area !== Pather.getAreaName(me.area)) {
  601. debugInfo.area = Pather.getAreaName(me.area);
  602.  
  603. //D2Bot.store(JSON.stringify(debugInfo));
  604. DataFile.updateStats("debugInfo", JSON.stringify(debugInfo));
  605. }
  606.  
  607. delay(20);
  608. }
  609.  
  610. return true;
  611. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement