-
Notifications
You must be signed in to change notification settings - Fork 156
/
Attack.js
1604 lines (1292 loc) · 47.9 KB
/
Attack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @filename Attack.js
* @author kolton, theBGuy
* @desc handle player attacks
*
*/
const Attack = {
infinity: false,
auradin: false,
monsterObjects: [
sdk.monsters.Turret1, sdk.monsters.Turret2, sdk.monsters.Turret3, sdk.monsters.MummyGenerator,
sdk.monsters.GargoyleTrap, sdk.monsters.LightningSpire, sdk.monsters.FireTower,
sdk.monsters.BarricadeDoor1, sdk.monsters.BarricadeDoor2, sdk.monsters.BarricadeWall1, sdk.monsters.BarricadeWall2,
sdk.monsters.CatapultS, sdk.monsters.CatapultE, sdk.monsters.CatapultSiege, sdk.monsters.CatapultW,
sdk.monsters.BarricadeTower, sdk.monsters.PrisonDoor, sdk.monsters.DiablosBoneCage, sdk.monsters.Hut,
],
Result: {
FAILED: 0,
SUCCESS: 1,
CANTATTACK: 2, // need to fix the ambiguity between this result and Failed
NEEDMANA: 3
},
// Initialize attacks
init: function () {
if (Config.Wereform) {
include("common/Attacks/wereform.js");
} else if (Config.CustomClassAttack && FileTools.exists("libs/common/Attacks/" + Config.CustomClassAttack + ".js")) {
console.log("Loading custom attack file");
include("common/Attacks/" + Config.CustomClassAttack + ".js");
} else {
include("common/Attacks/" + sdk.player.class.nameOf(me.classid) + ".js");
}
if (Config.AttackSkill[1] < 0 || Config.AttackSkill[3] < 0) {
showConsole();
console.warn("ÿc1Bad attack config. Don't expect your bot to attack.");
}
this.getPrimarySlot();
Skill.init();
if (me.expansion) {
Precast.checkCTA();
this.checkInfinity();
this.checkAuradin();
}
},
// check if slot has items
checkSlot: function (slot = me.weaponswitch) {
let item = me.getItem(-1, sdk.items.mode.Equipped);
if (item) {
do {
if (me.weaponswitch !== slot) {
if (item.bodylocation === sdk.body.RightArmSecondary || item.bodylocation === sdk.body.LeftArmSecondary) {
return true;
}
} else {
if (item.isOnMain) {
return true;
}
}
} while (item.getNext());
}
return false;
},
getPrimarySlot: function () {
// determine primary slot if not set
if (Config.PrimarySlot === -1) {
if (me.classic) {
Config.PrimarySlot = sdk.player.slot.Main;
} else {
// Always start on main-hand
me.weaponswitch !== sdk.player.slot.Main && me.switchWeapons(sdk.player.slot.Main);
// have cta
if ((Precast.haveCTA > -1) || Precast.checkCTA()) {
// have item on non-cta slot - set non-cta slot as primary
if (this.checkSlot(Precast.haveCTA ^ 1)) {
Config.PrimarySlot = Precast.haveCTA ^ 1;
} else {
// other slot is empty - set cta as primary slot
Config.PrimarySlot = Precast.haveCTA;
}
} else if (!this.checkSlot(sdk.player.slot.Main) && this.checkSlot(sdk.player.slot.Secondary)) {
// only slot II has items
Config.PrimarySlot = sdk.player.slot.Secondary;
} else {
// both slots have items, both are empty, or only slot I has items
Config.PrimarySlot = sdk.player.slot.Main;
}
}
}
return Config.PrimarySlot;
},
getCustomAttack: function (unit) {
// Check if unit got invalidated
if (!unit || !unit.name || !copyUnit(unit).x) return false;
for (let i in Config.CustomAttack) {
if (Config.CustomAttack.hasOwnProperty(i)) {
// if it contains numbers but is a string, convert to an int
if (i.match(/\d+/g)) {
i = parseInt(i, 10);
}
switch (typeof i) {
case "string":
if (unit.name.toLowerCase() === i.toLowerCase()) {
return Config.CustomAttack[i];
}
break;
case "number":
if (unit.classid === i) {
return Config.CustomAttack[i];
}
}
}
}
return false;
},
// Get items with charges - isn't used anywhere
getCharges: function () {
!Skill.charges && (Skill.charges = []);
let item = me.getItem(-1, sdk.items.mode.Equipped);
if (item) {
do {
let stats = item.getStat(-2);
if (stats.hasOwnProperty(sdk.stats.ChargedSkill)) {
if (stats[sdk.stats.ChargedSkill] instanceof Array) {
for (let i = 0; i < stats[sdk.stats.ChargedSkill].length; i += 1) {
if (stats[sdk.stats.ChargedSkill][i] !== undefined) {
Skill.charges.push({
unit: copyUnit(item),
gid: item.gid,
skill: stats[sdk.stats.ChargedSkill][i].skill,
level: stats[sdk.stats.ChargedSkill][i].level,
charges: stats[sdk.stats.ChargedSkill][i].charges,
maxcharges: stats[sdk.stats.ChargedSkill][i].maxcharges
});
}
}
} else {
Skill.charges.push({
unit: copyUnit(item),
gid: item.gid,
skill: stats[sdk.stats.ChargedSkill].skill,
level: stats[sdk.stats.ChargedSkill].level,
charges: stats[sdk.stats.ChargedSkill].charges,
maxcharges: stats[sdk.stats.ChargedSkill].maxcharges
});
}
}
} while (item.getNext());
}
return true;
},
// Check if player or his merc are using Infinity, and adjust resistance checks based on that
checkInfinity: function () {
if (me.classic) return false;
let merc;
// check if we have a merc and they aren't dead
Config.UseMerc && !me.mercrevivecost && (merc = Misc.poll(() => me.getMerc(), 1000, 100));
// Check merc infinity
!!merc && (this.infinity = merc.checkItem({name: sdk.locale.items.Infinity}).have);
// Check player infinity - only check if merc doesn't have
!this.infinity && (this.infinity = me.checkItem({name: sdk.locale.items.Infinity, equipped: true}).have);
return this.infinity;
},
checkAuradin: function () {
// Check player Dragon, Dream, HoJ, or Ice
this.auradin = me.haveSome([
{name: sdk.locale.items.Dragon, equipped: true}, {name: sdk.locale.items.Dream, equipped: true},
{name: sdk.locale.items.HandofJustice, equipped: true}, {name: sdk.locale.items.Ice, equipped: true},
]);
return this.auradin;
},
// just check if we can telestomp
canTeleStomp: function (unit) {
if (!unit || !unit.attackable) return false;
return Config.TeleStomp && Config.UseMerc && Pather.canTeleport() && Attack.checkResist(unit, "physical") && !!me.getMerc() && Attack.validSpot(unit.x, unit.y);
},
// Kill a monster based on its classId, can pass a unit as well
kill: function (classId) {
if (!classId || Config.AttackSkill[1] < 0) return false;
let target = (typeof classId === "object" ? classId : Misc.poll(() => Game.getMonster(classId), 2000, 100));
if (!target) {
console.warn("Attack.kill: Target not found");
return Attack.clear(10);
}
const findTarget = function (gid, loc) {
let path = getPath(me.area, me.x, me.y, loc.x, loc.y, 1, 5);
if (!path) return false;
if (path.some(function (node) {
Pather.walkTo(node.x, node.y);
return Game.getMonster(-1, -1, gid);
})) {
return Game.getMonster(-1, -1, gid);
} else {
return false;
}
};
const who = (!!target.name ? target.name : classId);
const gid = target.gid;
let retry = 0;
let errorInfo = "";
let attackCount = 0;
let lastLoc = {x: me.x, y: me.y};
let tick = getTickCount();
console.log("ÿc7Kill ÿc0:: " + who);
Config.MFLeader && Pather.makePortal() && say("kill " + classId);
while (attackCount < Config.MaxAttackCount && target.attackable && !this.skipCheck(target)) {
Misc.townCheck();
// Check if unit got invalidated, happens if necro raises a skeleton from the boss's corpse.
if (!target || !copyUnit(target).x) {
target = Game.getMonster(-1, -1, gid);
!target && (target = findTarget(gid, lastLoc));
if (!target) {
console.warn("ÿc1Failed to kill " + who + " (couldn't relocate unit)");
break;
}
}
// todo - dodge boss missiles
Config.Dodge && me.hpPercent <= Config.DodgeHP && this.deploy(target, Config.DodgeRange, 5, 9);
Config.MFSwitchPercent && target.hpPercent < Config.MFSwitchPercent && me.switchWeapons(this.getPrimarySlot() ^ 1);
if (attackCount > 0 && attackCount % 15 === 0 && Skill.getRange(Config.AttackSkill[1]) < 4) {
Packet.flash(me.gid);
}
let result = ClassAttack.doAttack(target, attackCount % 15 === 0);
if (result === this.Result.FAILED) {
if (retry++ > 3) {
errorInfo = " (doAttack failed)";
break;
}
Packet.flash(me.gid);
} else if (result === this.Result.CANTATTACK) {
errorInfo = " (No valid attack skills)";
break;
} else if (result === this.Result.NEEDMANA) {
continue;
} else {
retry = 0;
}
lastLoc = {x: me.x, y: me.y};
attackCount++;
}
attackCount === Config.MaxAttackCount && (errorInfo = " (attackCount exceeded: " + attackCount + ")");
Config.MFSwitchPercent && me.switchWeapons(this.getPrimarySlot());
ClassAttack.afterAttack();
Pickit.pickItems();
if (!!target && target.attackable) {
console.warn("ÿc1Failed to kill ÿc0" + who + errorInfo);
} else {
console.log("ÿc7Killed ÿc0:: " + who + "ÿc0 - ÿc7Duration: ÿc0" + Time.format(getTickCount() - tick));
}
return (!target || !copyUnit(target).x || target.dead || !target.attackable);
},
hurt: function (classId, percent) {
if (!classId || !percent) return false;
let target = (typeof classId === "object" ? classid : Misc.poll(() => Game.getMonster(classId), 2000, 100));
if (!target) {
console.warn("Attack.hurt: Target not found");
return false;
}
let retry = 0, attackCount = 0;
let tick = getTickCount();
const who = (!!target.name ? target.name : classId);
while (attackCount < Config.MaxAttackCount && target.attackable && !Attack.skipCheck(target)) {
let result = ClassAttack.doAttack(target, attackCount % 15 === 0);
if (result === this.Result.FAILED) {
if (retry++ > 3) {
break;
}
Packet.flash(me.gid);
} else if (result === this.Result.CANTATTACK) {
break;
} else if (result === this.Result.NEEDMANA) {
continue;
} else {
retry = 0;
}
if (!copyUnit(target).x) {
return true;
}
attackCount += 1;
if (target.hpPercent <= percent) {
console.log("ÿc7Hurt ÿc0:: " + who + "ÿc7HpPercent: ÿc0" + target.hpPercent + "ÿc0 - ÿc7Duration: ÿc0" + Time.format(getTickCount() - tick));
break;
}
}
return true;
},
getScarinessLevel: function (unit) {
// todo - define summonertype prototype
let scariness = 0;
const ids = [
sdk.monsters.FallenShaman, sdk.monsters.CarverShaman, sdk.monsters.CarverShaman2, sdk.monsters.DevilkinShaman, sdk.monsters.DevilkinShaman2,
sdk.monsters.DarkShaman1, sdk.monsters.DarkShaman2, sdk.monsters.WarpedShaman, sdk.monsters.HollowOne, sdk.monsters.Guardian1,
sdk.monsters.Guardian2, sdk.monsters.Unraveler1, sdk.monsters.Unraveler2, sdk.monsters.Ancient1, sdk.monsters.Ancient2, sdk.monsters.Ancient3,
sdk.monsters.BaalSubjectMummy, sdk.monsters.RatManShaman, sdk.monsters.FetishShaman, sdk.monsters.FlayerShaman1, sdk.monsters.FlayerShaman2, sdk.monsters.SoulKillerShaman1,
sdk.monsters.SoulKillerShaman2, sdk.monsters.StygianDollShaman1, sdk.monsters.StygianDollShaman2, sdk.monsters.FleshSpawner1, sdk.monsters.FleshSpawner2,
sdk.monsters.StygianHag, sdk.monsters.Grotesque1, sdk.monsters.Grotesque2
];
// Only handling monsters for now
if (!unit || unit.type !== sdk.unittype.Monster) return undefined;
// Minion
(unit.isMinion) && (scariness += 1);
// Champion
(unit.isChampion) && (scariness += 2);
// Boss
(unit.isUnique) && (scariness += 4);
// Summoner or the like
ids.includes(unit.classid) && (scariness += 8);
return scariness;
},
// Clear monsters in a section based on range and spectype or clear monsters around a boss monster
// probably going to change to passing an object
clear: function (range, spectype, bossId, sortfunc, pickit = true) {
while (!me.gameReady) {
delay(40);
}
if (Config.AttackSkill[1] < 0 || Config.AttackSkill[3] < 0) return false;
range === undefined && (range = 25);
spectype === undefined && (spectype = 0);
bossId === undefined && (bossId = false);
sortfunc === undefined && (sortfunc = false);
!sortfunc && (sortfunc = this.sortMonsters);
if (typeof (range) !== "number") throw new Error("Attack.clear: range must be a number.");
let i, boss, orgx, orgy, start, skillCheck;
let gidAttack = [];
let tick = getTickCount();
let [killedBoss, logged] = [false, false];
let [retry, attackCount] = [0, 0];
if (bossId) {
boss = Misc.poll(function () {
switch (true) {
case typeof bossId === "object":
return bossId;
case ((typeof bossId === "number" && bossId > 999)):
return Game.getMonster(-1, -1, bossId);
default:
return Game.getMonster(bossId);
}
}, 2000, 100);
if (!boss) {
console.warn("Attack.clear: " + bossId + " not found");
return Attack.clear(10);
}
({orgx, orgy} = {orgx: boss.x, orgy: boss.y});
Config.MFLeader && !!bossId && Pather.makePortal() && say("clear " + bossId);
} else {
({orgx, orgy} = {orgx: me.x, orgy: me.y});
}
let monsterList = [];
let target = Game.getMonster();
if (target) {
do {
if ((!spectype || (target.spectype & spectype)) && target.attackable && !this.skipCheck(target)) {
// Speed optimization - don't go through monster list until there's at least one within clear range
if (!start && getDistance(target, orgx, orgy) <= range && (Pather.canTeleport() || !checkCollision(me, target, sdk.collision.WallOrRanged))) {
start = true;
}
monsterList.push(copyUnit(target));
}
} while (target.getNext());
}
while (start && monsterList.length > 0 && attackCount < Config.MaxAttackCount) {
if (me.dead) return false;
boss && (({orgx, orgy} = {orgx: boss.x, orgy: boss.y}));
monsterList.sort(sortfunc);
target = copyUnit(monsterList[0]);
if (target.x !== undefined && (getDistance(target, orgx, orgy) <= range || (this.getScarinessLevel(target) > 7 && target.distance <= range))
&& target.attackable) {
Config.Dodge && me.hpPercent <= Config.DodgeHP && this.deploy(target, Config.DodgeRange, 5, 9);
Misc.townCheck(true);
tick = getTickCount();
if (!logged && boss && boss.gid === target.gid) {
logged = true;
console.log("ÿc7Clear ÿc0:: " + (!!target.name ? target.name : bossId));
}
//me.overhead("attacking " + target.name + " spectype " + target.spectype + " id " + target.classid);
let result = ClassAttack.doAttack(target, attackCount % 15 === 0);
if (result) {
retry = 0;
if (result === this.Result.CANTATTACK) {
monsterList.shift();
continue;
} else if (result === this.Result.NEEDMANA) {
continue;
}
for (i = 0; i < gidAttack.length; i += 1) {
if (gidAttack[i].gid === target.gid) {
break;
}
}
if (i === gidAttack.length) {
gidAttack.push({gid: target.gid, attacks: 0, name: target.name});
}
gidAttack[i].attacks += 1;
attackCount += 1;
let isSpecial = target.isSpecial;
let secAttack = me.barbarian ? (isSpecial ? 2 : 4) : 5;
if (Config.AttackSkill[secAttack] > -1 && (!Attack.checkResist(target, Config.AttackSkill[isSpecial ? 1 : 3])
|| (me.paladin && Config.AttackSkill[isSpecial ? 1 : 3] === sdk.skills.BlessedHammer && !ClassAttack.getHammerPosition(target)))) {
skillCheck = Config.AttackSkill[secAttack];
} else {
skillCheck = Config.AttackSkill[isSpecial ? 1 : 3];
}
// Desync/bad position handler
switch (skillCheck) {
case sdk.skills.BlessedHammer:
// Tele in random direction with Blessed Hammer
if (gidAttack[i].attacks > 0 && gidAttack[i].attacks % (isSpecial ? 4 : 2) === 0) {
let coord = CollMap.getRandCoordinate(me.x, -1, 1, me.y, -1, 1, 5);
Pather.moveTo(coord.x, coord.y);
}
break;
default:
// Flash with melee skills
if (gidAttack[i].attacks > 0 && gidAttack[i].attacks % (isSpecial ? 15 : 5) === 0 && Skill.getRange(skillCheck) < 4) {
Packet.flash(me.gid);
}
break;
}
// Skip non-unique monsters after 15 attacks, except in Throne of Destruction
if (!me.inArea(sdk.areas.ThroneofDestruction) && !isSpecial && gidAttack[i].attacks > 15) {
print("ÿc1Skipping " + target.name + " " + target.gid + " " + gidAttack[i].attacks);
monsterList.shift();
}
if (target.dead || Config.FastPick) {
if (boss && boss.gid === target.gid && target.dead) {
killedBoss = true;
console.log("ÿc7Cleared ÿc0:: " + (!!target.name ? target.name : bossId) + "ÿc0 - ÿc7Duration: ÿc0" + Time.format(getTickCount() - tick));
}
Pickit.fastPick();
}
} else {
if (retry++ > 3) {
monsterList.shift();
retry = 0;
}
Packet.flash(me.gid);
}
} else {
monsterList.shift();
}
}
if (attackCount > 0) {
ClassAttack.afterAttack(pickit);
this.openChests(range, orgx, orgy);
pickit && Pickit.pickItems();
} else {
Precast.doPrecast(false); // we didn't attack anything but check if we need to precast. TODO: better method of keeping track of precast skills
}
if (boss && !killedBoss) {
// check if boss corpse is around
if (boss.dead) {
console.log("ÿc7Cleared ÿc0:: " + (!!boss.name ? boss.name : bossId) + "ÿc0 - ÿc7Duration: ÿc0" + Time.format(getTickCount() - tick));
} else {
console.log("ÿc7Clear ÿc0:: ÿc1Failed to clear ÿc0:: " + (!!boss.name ? boss.name : bossId));
}
}
return true;
},
clearClassids: function (...ids) {
let monster = Game.getMonster();
if (monster) {
let list = [];
do {
if (ids.includes(monster.classid) && monster.attackable) {
list.push(copyUnit(monster));
}
} while (monster.getNext());
Attack.clearList(list);
}
return true;
},
// Filter monsters based on classId, spectype and range
getMob: function (classid, spectype, range, center) {
let monsterList = [];
let monster = Game.getMonster();
range === undefined && (range = 25);
!center && (center = me);
switch (typeof classid) {
case "number":
case "string":
monster = Game.getMonster(classid);
if (monster) {
do {
if (getDistance(center.x, center.y, monster.x, monster.y) <= range
&& (!spectype || (monster.spectype & spectype)) && monster.attackable) {
monsterList.push(copyUnit(monster));
}
} while (monster.getNext());
}
break;
case "object":
monster = Game.getMonster();
if (monster) {
do {
if (classid.includes(monster.classid) && getDistance(center.x, center.y, monster.x, monster.y) <= range
&& (!spectype || (monster.spectype & spectype)) && monster.attackable) {
monsterList.push(copyUnit(monster));
}
} while (monster.getNext());
}
break;
}
return monsterList;
},
// Clear an already formed array of monstas
clearList: function (mainArg, sortFunc, refresh) {
let i, target, monsterList;
let retry = 0;
let gidAttack = [];
let attackCount = 0;
switch (typeof mainArg) {
case "function":
monsterList = mainArg.call();
break;
case "object":
monsterList = mainArg.slice(0);
break;
case "boolean": // false from Attack.getMob()
return false;
default:
throw new Error("clearList: Invalid argument");
}
!sortFunc && (sortFunc = this.sortMonsters);
while (monsterList.length > 0 && attackCount < Config.MaxAttackCount) {
if (refresh && attackCount > 0 && attackCount % refresh === 0) {
monsterList = mainArg.call();
}
if (me.dead) return false;
monsterList.sort(sortFunc);
target = copyUnit(monsterList[0]);
if (target.x !== undefined && target.attackable) {
Config.Dodge && me.hpPercent <= Config.DodgeHP && this.deploy(target, Config.DodgeRange, 5, 9);
Misc.townCheck(true);
//me.overhead("attacking " + target.name + " spectype " + target.spectype + " id " + target.classid);
let result = ClassAttack.doAttack(target, attackCount % 15 === 0);
if (result) {
retry = 0;
if (result === this.Result.CANTATTACK) {
monsterList.shift();
continue;
} else if (result === this.Result.NEEDMANA) {
continue;
}
for (i = 0; i < gidAttack.length; i += 1) {
if (gidAttack[i].gid === target.gid) {
break;
}
}
if (i === gidAttack.length) {
gidAttack.push({gid: target.gid, attacks: 0});
}
gidAttack[i].attacks += 1;
let isSpecial = target.isSpecial;
// Desync/bad position handler
switch (Config.AttackSkill[isSpecial ? 1 : 3]) {
case sdk.skills.BlessedHammer:
// Tele in random direction with Blessed Hammer
if (gidAttack[i].attacks > 0 && gidAttack[i].attacks % (isSpecial ? 5 : 15) === 0) {
let coord = CollMap.getRandCoordinate(me.x, -1, 1, me.y, -1, 1, 4);
Pather.moveTo(coord.x, coord.y);
}
break;
default:
// Flash with melee skills
if (gidAttack[i].attacks > 0 && gidAttack[i].attacks % (isSpecial ? 5 : 15) === 0 && Skill.getRange(Config.AttackSkill[isSpecial ? 1 : 3]) < 4) {
Packet.flash(me.gid);
}
break;
}
// Skip non-unique monsters after 15 attacks, except in Throne of Destruction
if (!me.inArea(sdk.areas.ThroneofDestruction) && !isSpecial && gidAttack[i].attacks > 15) {
console.log("ÿc1Skipping " + target.name + " " + target.gid + " " + gidAttack[i].attacks);
monsterList.shift();
}
attackCount += 1;
if (target.dead || Config.FastPick) {
Pickit.fastPick();
}
} else {
if (retry++ > 3) {
monsterList.shift();
retry = 0;
}
Packet.flash(me.gid);
}
} else {
monsterList.shift();
}
}
if (attackCount > 0) {
ClassAttack.afterAttack(true);
this.openChests(Config.OpenChests.Range);
Pickit.pickItems();
} else {
Precast.doPrecast(false); // we didn't attack anything but check if we need to precast. TODO: better method of keeping track of precast skills
}
return true;
},
securePosition: function (x, y, range = 15, timer = 3000, skipBlocked = true, special = false) {
let tick;
(typeof x !== "number" || typeof y !== "number") && ({x, y} = me);
skipBlocked === true && (skipBlocked = sdk.collision.Ranged);
while (true) {
[x, y].distance > 5 && Pather.moveTo(x, y);
let monster = Game.getMonster();
let monList = [];
if (monster) {
do {
if (getDistance(monster, x, y) <= range && monster.attackable && this.canAttack(monster)
&& (!skipBlocked || !checkCollision(me, monster, skipBlocked))
&& (Pather.canTeleport() || !checkCollision(me, monster, sdk.collision.BlockWall))) {
monList.push(copyUnit(monster));
}
} while (monster.getNext());
}
if (!monList.length) {
!tick && (tick = getTickCount());
// only return if it's been safe long enough
if (getTickCount() - tick >= timer) {
return;
}
} else {
this.clearList(monList);
// reset the timer when there's monsters in range
tick && (tick = false);
}
if (special) {
if (me.paladin && Skill.canUse(sdk.skills.Redemption) && Skill.setSkill(sdk.skills.Redemption, sdk.skills.hand.Right)) {
delay(1000);
}
}
delay(100);
}
},
// Draw lines around a room on minimap
markRoom: function (room, color) {
let arr = [];
arr.push(new Line(room.x * 5, room.y * 5, room.x * 5, room.y * 5 + room.ysize, color, true));
arr.push(new Line(room.x * 5, room.y * 5, room.x * 5 + room.xsize, room.y * 5, color, true));
arr.push(new Line(room.x * 5 + room.xsize, room.y * 5, room.x * 5 + room.xsize, room.y * 5 + room.ysize, color, true));
arr.push(new Line(room.x * 5, room.y * 5 + room.ysize, room.x * 5 + room.xsize, room.y * 5 + room.ysize, color, true));
},
countUniques: function () {
!this.uniques && (this.uniques = 0);
!this.ignoredGids && (this.ignoredGids = []);
let monster = Game.getMonster();
if (monster) {
do {
if ((monster.isSuperUnique) && this.ignoredGids.indexOf(monster.gid) === -1) {
this.uniques += 1;
this.ignoredGids.push(monster.gid);
}
} while (monster.getNext());
}
},
storeStatistics: function (area) {
!FileTools.exists("statistics.json") && Misc.fileAction("statistics.json", 1, "{}");
let obj = JSON.parse(Misc.fileAction("statistics.json", 0));
if (obj) {
if (obj[area] === undefined) {
obj[area] = {
runs: 0,
averageUniques: 0
};
}
obj[area].averageUniques = ((obj[area].averageUniques * obj[area].runs + this.uniques) / (obj[area].runs + 1)).toFixed(4);
obj[area].runs += 1;
Misc.fileAction("statistics.json", 1, JSON.stringify(obj));
}
this.uniques = 0;
this.ignoredGids = [];
},
// Clear an entire area based on monster spectype
clearLevel: function (spectype = 0) {
function RoomSort(a, b) {
return getDistance(myRoom[0], myRoom[1], a[0], a[1]) - getDistance(myRoom[0], myRoom[1], b[0], b[1]);
}
let room = getRoom();
if (!room) return false;
console.time("clearLevel");
console.info(true, Pather.getAreaName(me.area));
let myRoom, previousArea;
let rooms = [];
const currentArea = getArea().id;
const breakClearLevelCheck = !!(Loader.scriptName() === "MFHelper" && Config.MFHelper.BreakClearLevel && Config.Leader !== "");
do {
rooms.push([room.x * 5 + room.xsize / 2, room.y * 5 + room.ysize / 2]);
} while (room.getNext());
if (Config.MFLeader && rooms.length > 0) {
Pather.makePortal();
// tombs exception
if (me.area >= sdk.areas.TalRashasTomb1 && me.area <= sdk.areas.TalRashasTomb7) {
say("clearlevel " + me.area);
} else {
say("clearlevel " + Pather.getAreaName(currentArea));
}
}
while (rooms.length > 0) {
// get the first room + initialize myRoom var
!myRoom && (room = getRoom(me.x, me.y));
if (breakClearLevelCheck) {
let leader = Misc.findPlayer(Config.Leader);
if (leader && leader.area !== me.area && !leader.inTown) {
me.overhead("break the clearing in " + getArea().name);
return true;
}
}
if (room) {
// use previous room to calculate distance
if (room instanceof Array) {
myRoom = [room[0], room[1]];
} else {
// create a new room to calculate distance (first room, done only once)
myRoom = [room.x * 5 + room.xsize / 2, room.y * 5 + room.ysize / 2];
}
}
rooms.sort(RoomSort);
room = rooms.shift();
let result = Pather.getNearestWalkable(room[0], room[1], 18, 3);
if (result) {
Pather.moveTo(result[0], result[1], 3, spectype);
previousArea = result;
if (!this.clear(40, spectype)) {
break;
}
} else if (currentArea !== getArea().id) {
// Make sure bot does not get stuck in different area.
Pather.moveTo(previousArea[0], previousArea[1], 3, spectype);
}
}
//this.storeStatistics(Pather.getAreaName(me.area));
console.info(false, Pather.getAreaName(currentArea), "clearLevel");
return true;
},
// Sort monsters based on distance, spectype and classId (summoners are attacked first)
// Think this needs a collison check included for non tele chars, might prevent choosing closer mob that is actually behind a wall vs the one we pass trying to get behind the wall
sortMonsters: function (unitA, unitB) {
// No special sorting for were-form
if (Config.Wereform) return getDistance(me, unitA) - getDistance(me, unitB);
// sort main bosses first
// Andy
if (me.inArea(sdk.areas.CatacombsLvl4)) {
if (unitA.distance < 5 && unitA.classid === sdk.monsters.Andariel && !checkCollision(me, unitA, sdk.collision.Ranged)) return -1;
}
// Meph
if (me.inArea(sdk.areas.DuranceofHateLvl3)) {
if (unitA.distance < 5 && unitA.classid === sdk.monsters.Mephisto && !checkCollision(me, unitA, sdk.collision.Ranged)) return -1;
}
// Baal
if (me.inArea(sdk.areas.WorldstoneChamber)) {
if (unitA.classid === sdk.monsters.Baal) return -1;
}
// Barb optimization
if (me.barbarian) {
if (!Attack.checkResist(unitA, Attack.getSkillElement(Config.AttackSkill[(unitA.isSpecial) ? 1 : 3]))) {
return 1;
}
if (!Attack.checkResist(unitB, Attack.getSkillElement(Config.AttackSkill[(unitB.isSpecial) ? 1 : 3]))) {
return -1;
}
}
// Put monsters under Attract curse at the end of the list - They are helping us
if (unitA.getState(sdk.states.Attract)) return 1;
if (unitB.getState(sdk.states.Attract)) return -1;
const ids = [
sdk.monsters.OblivionKnight1, sdk.monsters.OblivionKnight2, sdk.monsters.OblivionKnight3, sdk.monsters.FallenShaman, sdk.monsters.CarverShaman, sdk.monsters.CarverShaman2,
sdk.monsters.DevilkinShaman, sdk.monsters.DevilkinShaman2, sdk.monsters.DarkShaman1, sdk.monsters.DarkShaman2, sdk.monsters.WarpedShaman, sdk.monsters.HollowOne, sdk.monsters.Guardian1,
sdk.monsters.Guardian2, sdk.monsters.Unraveler1, sdk.monsters.Unraveler2, sdk.monsters.Ancient1, sdk.monsters.BaalSubjectMummy, sdk.monsters.BloodRaven, sdk.monsters.RatManShaman,
sdk.monsters.FetishShaman, sdk.monsters.FlayerShaman1, sdk.monsters.FlayerShaman2, sdk.monsters.SoulKillerShaman1, sdk.monsters.SoulKillerShaman2, sdk.monsters.StygianDollShaman1,
sdk.monsters.StygianDollShaman2, sdk.monsters.FleshSpawner1, sdk.monsters.FleshSpawner2, sdk.monsters.StygianHag, sdk.monsters.Grotesque1, sdk.monsters.Ancient2, sdk.monsters.Ancient3,
sdk.monsters.Grotesque2
];
if (!me.inArea(sdk.areas.ClawViperTempleLvl2) && ids.includes(unitA.classid) && ids.includes(unitB.classid)) {
// Kill "scary" uniques first (like Bishibosh)
if ((unitA.isUnique) && (unitB.isUnique)) return getDistance(me, unitA) - getDistance(me, unitB);
if (unitA.isUnique) return -1;
if (unitB.isUnique) return 1;
return getDistance(me, unitA) - getDistance(me, unitB);
}
if (ids.includes(unitA.classid)) return -1;
if (ids.includes(unitB.classid)) return 1;
if (Config.BossPriority) {
if ((unitA.isSuperUnique) && (unitB.isSuperUnique)) return getDistance(me, unitA) - getDistance(me, unitB);
if (unitA.isSuperUnique) return -1;
if (unitB.isSuperUnique) return 1;
}
return getDistance(me, unitA) - getDistance(me, unitB);
},
// Check if a set of coords is valid/accessable
// re-work this for more info
// casting skills can go over non-floors - excluding bliz/meteor - not sure if any others
// physical skills can't, need to exclude monster objects though
// splash skills can go through some objects, however some objects are cast blockers
validSpot: function (x, y, skill = -1, unitid = 0) {
// Just in case
if (!me.area || !x || !y) return false;
// for now this just returns true and we leave getting into position to the actual class attack files
if (Skill.missileSkills.includes(skill)
|| ([sdk.skills.Blizzard, sdk.skills.Meteor].includes(skill) && unitid > 0 && !getBaseStat("monstats", unitid, "flying"))) {
return true;
}
let result;
let nonFloorAreas = [sdk.areas.ArcaneSanctuary, sdk.areas.RiverofFlame, sdk.areas.ChaosSanctuary, sdk.areas.Abaddon, sdk.areas.PitofAcheron, sdk.areas.InfernalPit];
// Treat thrown errors as invalid spot
try {
result = getCollision(me.area, x, y);
} catch (e) {
return false;
}
if (result === undefined) return false;
switch (true) {
case Skill.needFloor.includes(skill) && nonFloorAreas.includes(me.area):