-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaoe2_fightsim.js
executable file
·116 lines (96 loc) · 3.77 KB
/
aoe2_fightsim.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
AOE2_HitsCalc = function()
{
this.FightOver = false;
this.unit1 = null;
this.unit2 = null;
// compares two units and prints the number of hits to beat and lose
Fight = function(obj1, obj2, callback)
{
this.FightOver = false;
this.unit1 = (JSON.parse(JSON.stringify(obj1)));
this.unit2 = (JSON.parse(JSON.stringify(obj2)));
this.unit1.ar = this.unit1.ar.split('/');
this.unit2.ar = this.unit2.ar.split('/');
callback("<span style='color: blue;'>Battle: "+this.unit1.name+" ("+this.unit1.hp+"HP) vs "+this.unit2.name+" ("+this.unit2.hp+" HP)</span>");
//a.hp -= (Math.abs(b.at - a.mdef) + Math.abs(b.patt - a.pdef));
this.unit1.hits = 0;
this.unit2.hits = 0;
this.PerformAttack(this.unit1, this.unit2, callback);
this.PerformAttack(this.unit2, this.unit1, callback);
}
PerformAttack = function(obj1, obj2, callback)
{
if(obj1.fr !== undefined && obj1.fr != "-" && obj1.fr > 0)
{
this.Timeout = setTimeout(function(){
if(!this.FightOver && obj1.hp > 0)
{
obj1.hits++;
var at = 0;
var atbonus = 0;
var defbonus = 0;
var def = 0;
at = (obj1.at != "-") ? obj1.at : 0;
if(!obj1.t.includes("pierce")) def = obj2.ar[0];
else def = obj2.ar[1];
// atk bonus
if(obj1.extra !== undefined && obj1.extra["attack bonus"] !== undefined){
var atbonusarr = [], temp = obj1.extra["attack bonus"].replace(" against").split(", ");
for(var i in temp){
var spl = temp[i].split(" "), spl2 = spl[1].split("/");
for(var i2 in spl2) atbonusarr[spl2[i2].substr(0, 4)] = spl[0];
}
//
var temp = obj2.t.split(" ");
for(var i in temp)
if(atbonusarr[temp[i].substr(0, 4)] != undefined){
atbonus = atbonusarr[temp[i].substr(0, 4)].substr(1);
break;}
}
// /atk bonus
// def bonus
if(obj1.extra !== undefined && obj1.extra["armor bonus"] !== undefined){
var defbonusarr = [], temp = obj1.extra["armor bonus"].replace(" against").split(", ");
for(var i in temp){
var spl = temp[i].split(" "), spl2 = spl[1].split("/");
for(var i2 in spl2) defbonusarr[spl2[i2].substr(0, 4)] = spl[0];
}
//
var temp = obj2.t.split(" ");
for(var i in temp)
if(defbonusarr[temp[i].substr(0, 4)] != undefined){
defbonus = defbonusarr[temp[i].substr(0, 4)].substr(1);
break;}
}
// /def bonus
at = parseFloat(at);
atbonus = parseFloat(atbonus);
def = parseFloat(def);
defbonus = parseFloat(defbonus);
if(obj1.at != "-" && obj1.at != 0 && at <= 0) at = 1;
var totaldmg = (at + atbonus) - (def + defbonus);
if(totaldmg <= 0) totaldmg = 1;
obj2.hp -= totaldmg;
var str = obj1.name + " attacks " + obj2.name + " (" + obj2.hp + " HP) causing total " + totaldmg+" damage";
if(atbonus > 0) str += " (" + atbonus + " bonus dmg)";
if(def > 0 || defbonus) str += " (-"+(def + defbonus)+" def)";
callback(str);
if(obj2.hp <= 0)
{
this.Finished(callback);
this.FightOver = true;
}
this.PerformAttack(obj1, obj2, callback);
}
}, obj1.fr * 100);
}
}
Finished = function(callback){
clearTimeout(this.Timeout);
if(this.unit1.hp > this.unit2.hp)
callback("<span style='color: green;'>"+this.unit1.name + " ("+this.unit1.hp+" HP) wins against " + this.unit2.name + " ("+this.unit2.hp+" HP) after "+this.unit1.hits+" hits</span>");
if(this.unit1.hp < this.unit2.hp)
callback("<span style='color: red;'>"+this.unit1.name + " ("+this.unit1.hp+" HP) loses against " + this.unit2.name + " ("+this.unit2.hp+" HP) after "+this.unit2.hits+" hits</span>");
}
return this;
}