Advertisement
kolton

Untitled

Jun 21st, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. *   @filename   AntiHostile.js
  3. *   @author     kolton
  4. *   @desc       handle hostile threats
  5. */
  6.  
  7. js_strict(true);
  8.  
  9. include("json2.js");
  10. include("NTItemParser.dbl");
  11. include("OOG.js");
  12. include("Gambling.js");
  13. include("common/Attack.js");
  14. include("common/Cubing.js");
  15. include("common/Config.js");
  16. include("common/CollMap.js");
  17. include("common/Loader.js");
  18. include("common/Misc.js");
  19. include("common/Pickit.js");
  20. include("common/Pather.js");
  21. include("common/Precast.js");
  22. include("common/Prototypes.js");
  23. include("common/Runewords.js");
  24. include("common/Storage.js");
  25. include("common/Town.js");
  26.  
  27. function main() {
  28.     // Variables and functions
  29.     var player, findTrigger, attackCount, prevPos, check, missile,
  30.         charClass = ["Amazon", "Sorceress", "Necromancer", "Paladin", "Barbarian", "Druid", "Assassin"],
  31.         hostiles = [];
  32.  
  33.     // AntiHostile gets game event info from ToolsThread
  34.     this.scriptEvent = function (msg) {
  35.         switch (msg.split(" ")[0]) {
  36.         case "findHostiles": // Scan for hostile players
  37.             findTrigger = true;
  38.  
  39.             break;
  40.         case "remove": // Remove a hostile player that left the game
  41.             if (hostiles.indexOf(msg.split(" ")[1]) > -1) {
  42.                 hostiles.splice(hostiles.indexOf(msg.split(" ")[1]), 1);
  43.             }
  44.  
  45.             break;
  46.         case "mugshot": // Take a screenshot and log the kill
  47.             D2Bot.printToConsole(msg.split(" ")[1] + " has been neutralized.", 4);
  48.             hideConsole();
  49.             delay(500);
  50.             takeScreenshot();
  51.  
  52.             break;
  53.         }
  54.     };
  55.  
  56.     // Find all hostile players and add their names to the 'hostiles' list
  57.     this.findHostiles = function () {
  58.         var party = getParty();
  59.  
  60.         if (party) {
  61.             do {
  62.                 if (party.name !== me.name && getPlayerFlag(me.gid, party.gid, 8) && hostiles.indexOf(party.name) === -1) {
  63.                     D2Bot.printToConsole(party.name + " (Level " + party.level + " " + charClass[party.classid] + ")" + " has declared hostility.", 8);
  64.                     hostiles.push(party.name);
  65.                 }
  66.             } while (party.getNext());
  67.         }
  68.  
  69.         return true;
  70.     };
  71.  
  72.     // Pause default so actions don't conflict
  73.     this.pause = function () {
  74.         var script = getScript("default.dbj");
  75.  
  76.         if (script && script.running) {
  77.             print("ÿc1Pausing.");
  78.             script.pause();
  79.         }
  80.     };
  81.  
  82.     // Resume default
  83.     this.resume = function () {
  84.         var script = getScript("default.dbj");
  85.  
  86.         if (script && !script.running) {
  87.             print("ÿc2Resuming.");
  88.             script.resume();
  89.         }
  90.     };
  91.  
  92.     // Initiate flashing sequence
  93.     this.startFlash = function (gid) {
  94.         var script = getScript("tools/FlashThread.js");
  95.  
  96.         if (script) {
  97.             script.send("flash " + gid);
  98.         }
  99.     };
  100.  
  101.     // Abort flashing sequence
  102.     this.stopFlash = function () {
  103.         var script = getScript("tools/FlashThread.js");
  104.  
  105.         if (script) {
  106.             script.send("unflash");
  107.         }
  108.     };
  109.  
  110.     // Find hostile player Units
  111.     this.findPlayer = function () {
  112.         var i, player;
  113.  
  114.         for (i = 0; i < hostiles.length; i += 1) {
  115.             player = getUnit(0, hostiles[i]);
  116.  
  117.             if (player) {
  118.                 do {
  119.                     if (player.mode !== 0 && player.mode !== 17 && getPlayerFlag(me.gid, player.gid, 8) && !player.inTown && !me.inTown) {
  120.                         return player;
  121.                     }
  122.                 } while (player.getNext());
  123.             }
  124.         }
  125.  
  126.         return false;
  127.     };
  128.  
  129.     // Find a missile type
  130.     this.findMissile = function (owner, id, range) {
  131.         if (range === undefined) {
  132.             range = 999;
  133.         }
  134.  
  135.         var missile = getUnit(3, id);
  136.  
  137.         if (!missile) {
  138.             return false;
  139.         }
  140.  
  141.         do {
  142.             if (missile.owner === owner.gid && getDistance(owner, missile) < range) {
  143.                 return missile;
  144.             }
  145.         } while (missile.getNext());
  146.  
  147.         return false;
  148.     };
  149.  
  150.     this.checkSummons = function (player) {
  151.         var unit,
  152.             name = player.name;
  153.  
  154.         unit = getUnit(1);
  155.  
  156.         if (unit) {
  157.             do {
  158.                 // Revives and spirit wolves
  159.                 if (unit.getParent() && unit.getParent().name === name && (unit.getState(96) || unit.classid === 420)) {
  160.                     return true;
  161.                 }
  162.             } while (unit.getNext());
  163.         }
  164.  
  165.         return false;
  166.     };
  167.  
  168.     // Init config and attacks
  169.     D2Bot.init();
  170.     Config.init();
  171.     Attack.init();
  172.     Storage.Init();
  173.  
  174.     // Load flash thread
  175.     if (Config.HostileAction > 1) {
  176.         load("tools/FlashThread.js");
  177.     }
  178.  
  179.     // Attack sequence adjustments - this only affects the AntiHostile thread
  180.     switch (me.classid) {
  181.     case 0: // Amazon - increase skill range
  182.         if ([24].indexOf(Config.AttackSkill[1]) > -1) {
  183.             ClassAttack.skillRange[1] = 40;
  184.             ClassAttack.skillRange[2] = 40;
  185.         }
  186.  
  187.         break;
  188.     case 1: // Sorceress - increase skill range
  189.         if ([47, 49, 51, 53, 56, 59].indexOf(Config.AttackSkill[1]) > -1) {
  190.             ClassAttack.skillRange[1] = 40;
  191.             ClassAttack.skillRange[2] = 40;
  192.         }
  193.  
  194.         break;
  195.     case 2: // Necromancer - increase skill range
  196.         if ([84, 93].indexOf(Config.AttackSkill[1]) > -1) {
  197.             ClassAttack.skillRange[1] = 40;
  198.             ClassAttack.skillRange[2] = 40;
  199.         }
  200.  
  201.         break;
  202.     case 6: // Assassin - use Mind Blast with trapsins
  203.         if (me.getSkill(273, 1) && [251, 256].indexOf(Config.AttackSkill[1]) > -1) {
  204.             Config.AttackSkill[1] = 273; // Mind Blast
  205.             ClassAttack.skillRange[1] = 40;
  206.             ClassAttack.trapRange = 40;
  207.         }
  208.  
  209.         break;
  210.     }
  211.  
  212.     // A simple but fast player dodge function
  213.     this.moveAway = function (unit, range) {
  214.         var i, coordx, coordy,
  215.             angle = Math.round(Math.atan2(me.y - unit.y, me.x - unit.x) * 180 / Math.PI),
  216.             angles = [0, 45, -45, 90, -90, 135, -135, 180];
  217.  
  218.         for (i = 0; i < angles.length; i += 1) {
  219.             // Avoid the position where the player actually tries to move to
  220.             coordx = Math.round((Math.cos((angle + angles[i]) * Math.PI / 180)) * range + unit.x); // unit.targetx
  221.             coordy = Math.round((Math.sin((angle + angles[i]) * Math.PI / 180)) * range + unit.y); // unit.targety
  222.  
  223.             if (Attack.validSpot(coordx, coordy)) {
  224.                 return Pather.moveTo(coordx, coordy);
  225.             }
  226.         }
  227.  
  228.         return false;
  229.     };
  230.  
  231.     addEventListener("scriptmsg", this.scriptEvent);
  232.     print("ÿc2Anti-Hostile thread loaded.");
  233.     this.findHostiles();
  234.  
  235.     // Main Loop
  236.     while (true) {
  237.         if (me.gameReady) {
  238.             // Scan for hostiles
  239.             if (findTrigger) {
  240.                 this.findHostiles();
  241.  
  242.                 findTrigger = false;
  243.             }
  244.  
  245.             if (hostiles.length > 0 && (Config.HostileAction === 0 || (Config.HostileAction === 1 && me.inTown))) {
  246.                 if (Config.TownOnHostile) {
  247.                     this.pause();
  248.                     Town.goToTown();
  249.  
  250.                     while (hostiles.length > 0) {
  251.                         delay(500);
  252.                     }
  253.  
  254.                     Pather.usePortal(null, me.name);
  255.                     this.resume();
  256.                 } else {
  257.                     quit();
  258.                 }
  259.  
  260.                 return;
  261.             }
  262.  
  263.             // Mode 3 - Spam entrance (still experimental)
  264.             if (Config.HostileAction === 3 && hostiles.length > 0 && me.area === 131) {
  265.                 switch (me.classid) {
  266.                 case 1: // Sorceress
  267.                     prevPos = {x: me.x, y: me.y};
  268.                     this.pause();
  269.                     Pather.moveTo(15103, 5247);
  270.  
  271.                     while (!this.findPlayer() && hostiles.length > 0) {
  272.                         if (!me.getState(121)) {
  273.                             Skill.cast(Config.AttackSkill[1], ClassAttack.skillHand[1], 15099, 5237);
  274.                         } else {
  275.                             if (Config.AttackSkill[2] > -1) {
  276.                                 Skill.cast(Config.AttackSkill[2], ClassAttack.skillHand[2], 15099, 5237);
  277.                             } else {
  278.                                 while (me.getState(121)) {
  279.                                     delay(40);
  280.                                 }
  281.                             }
  282.                         }
  283.                     }
  284.  
  285.                     break;
  286.                 case 5: // Druid
  287.                     // Don't bother if it's not a tornado druid
  288.                     if (Config.AttackSkill[1] !== 245) {
  289.                         break;
  290.                     }
  291.  
  292.                     prevPos = {x: me.x, y: me.y};
  293.                     this.pause();
  294.                     Pather.moveTo(15103, 5247);
  295.  
  296.                     while (!this.findPlayer() && hostiles.length > 0) {
  297.                         // Tornado path is a function of target x. Slight randomization will make sure it can't always miss
  298.                         Skill.cast(Config.AttackSkill[1], ClassAttack.skillHand[1], 15099 + rand(-2, 2), 5237);
  299.                     }
  300.  
  301.                     break;
  302.                 case 6: // Assassin
  303.                     prevPos = {x: me.x, y: me.y};
  304.                     this.pause();
  305.                     Pather.moveTo(15103, 5247);
  306.  
  307.                     while (!this.findPlayer() && hostiles.length > 0) {
  308.                         if (Config.UseTraps) {
  309.                             check = ClassAttack.checkTraps({x: 15099, y: 5242, classid: 544});
  310.  
  311.                             if (check) {
  312.                                 ClassAttack.placeTraps({x: 15099, y: 5242, classid: 544}, 5);
  313.                             }
  314.                         }
  315.  
  316.                         Skill.cast(Config.AttackSkill[1], ClassAttack.skillHand[1], 15099, 5237);
  317.  
  318.                         while (me.getState(121)) {
  319.                             delay(40);
  320.                         }
  321.                     }
  322.  
  323.                     break;
  324.                 }
  325.             }
  326.  
  327.             // Player left, return to old position
  328.             if (!hostiles.length && prevPos) {
  329.                 Pather.moveTo(prevPos.x, prevPos.y);
  330.                 this.resume();
  331.  
  332.                 // Reset position
  333.                 prevPos = false;
  334.             }
  335.  
  336.             player = this.findPlayer();
  337.  
  338.             if (player) {
  339.                 // Mode 1 - Quit if hostile player is nearby
  340.                 if (Config.HostileAction === 1) {
  341.                     if (Config.TownOnHostile) {
  342.                         this.pause();
  343.                         Town.goToTown();
  344.  
  345.                         while (hostiles.length > 0) {
  346.                             delay(500);
  347.                         }
  348.  
  349.                         Pather.usePortal(null, me.name);
  350.                         this.resume();
  351.                     } else {
  352.                         quit();
  353.                     }
  354.  
  355.                     return;
  356.                 }
  357.  
  358.                 // Kill the hostile player
  359.                 if (!prevPos) {
  360.                     prevPos = {x: me.x, y: me.y};
  361.                 }
  362.  
  363.                 this.pause();
  364.                 this.startFlash(player.gid); // might need to be expanded
  365.  
  366.                 Config.UseMerc = false; // Don't go revive the merc mid-fight
  367.                 attackCount = 0;
  368.  
  369.                 while (attackCount < 100) {
  370.                     if (!copyUnit(player).x || player.inTown || me.mode === 17) { // Invalidated Unit (out of getUnit range) or player in town
  371.                         break;
  372.                     }
  373.  
  374.                     ClassAttack.doAttack(player, false);
  375.  
  376.                     // Specific attack additions
  377.                     switch (me.classid) {
  378.                     case 1: // Sorceress
  379.                     case 2: // Necromancer
  380.                         // Dodge missiles - experimental
  381.                         missile = getUnit(3);
  382.  
  383.                         if (missile) {
  384.                             do {
  385.                                 if (getPlayerFlag(me.gid, missile.owner, 8) && (getDistance(me, missile) < 15 || (missile.targetx && getDistance(me, missile.targetx, missile.targety) < 15))) {
  386.                                     this.moveAway(missile, ClassAttack.skillRange[1]);
  387.  
  388.                                     break;
  389.                                 }
  390.                             } while (missile.getNext());
  391.                         }
  392.  
  393.                         // Move away if the player is too close or if he tries to move too close (telestomp)
  394.                         if (ClassAttack.skillRange[1] > 20 && (getDistance(me, player) < 30 || (player.targetx && getDistance(me, player.targetx, player.targety) < 15))) {
  395.                             this.moveAway(player, ClassAttack.skillRange[1]);
  396.                         }
  397.  
  398.                         break;
  399.                     case 3: // Paladin
  400.                         // Smite summoners
  401.                         if (Config.AttackSkill[1] === 112 && me.getSkill(97, 1)) {
  402.                             if ([2, 5].indexOf(player.classid) > -1 && getDistance(me, player) < 4 && this.checkSummons(player)) {
  403.                                 Skill.cast(97, 1, player);
  404.                             }
  405.                         }
  406.  
  407.                         break;
  408.                     }
  409.  
  410.                     attackCount += 1;
  411.  
  412.                     if (player.mode === 0 || player.mode === 17) {
  413.                         break;
  414.                     }
  415.                 }
  416.  
  417.                 Pather.moveTo(prevPos.x, prevPos.y);
  418.                 this.resume();
  419.                 this.stopFlash();
  420.             }
  421.         }
  422.  
  423.         delay(200);
  424.     }
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement