-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
111 lines (100 loc) · 3.27 KB
/
Copy pathfunctions.js
File metadata and controls
111 lines (100 loc) · 3.27 KB
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
// fully dynamic rotr bot functions
// written by paradox
// --- BATTLE FUNCTION ------------------------------------------------------ //
//
// Calculate a battle between two divisions.
//
// position key:
// 0: Regular/sea level hex
// 1: Hill hex
// 2: Mountain hex
// 3: Peak hex
// atkAdd and defAdd allow to add custom additive manpower modifiers. For
// example, the Freikorps' active ability adds 5% attack.
//
// -------------------------------------------------------------------------- //
function battle(atkStartTroops, defStartTroops, atkPos, defPos, atkAdd, defAdd, river) {
// modifiers
let atkMod = (river) ? -0.1 : 0 + atkAdd;
switch(atkPos) {
case 0: // regular
break;
case 1: // hill
atkMod += 0.1;
break;
case 2: // mountain
atkMod += 0.15;
break;
case 3: // peak
atkMod += 0.2;
break;
}
let defMod = defAdd;
switch(defPos) {
case 0: // regular
break;
case 1: // hill
defMod += 0.1;
break;
case 2: // mountain
defMod += 0.15;
break;
case 3: // peak
defMod += 0.2;
break;
}
const atkEffective = atkStartTroops * (1 + atkMod);
const defEffective = defStartTroops * (1 + defMod);
// beta coin flip; get a number between -defStartTroops and atkStartTroops.
// if negative, def wins. If positive, atk wins.
const coinflip = Math.floor(Math.random() * (atkEffective + defEffective));
const atkWins = coinflip > defEffective;
const loserTroops = (atkWins) ? defEffective : atkEffective;
const winnerTroops = (atkWins) ? atkEffective : defEffective;
let newLoserTroops = loserTroops * 0.9 - Math.round(Math.abs(atkStartTroops - defStartTroops) / 3);
return [atkWins, Math.round(newLoserTroops)];
}
// --- ANALYZE FUNCTION ----------------------------------------------------- //
//
// Get the chance that either side wins a battle. Unlike V1 Redux, this uses a
// uniform distribution, so it's very easy to calculate without simulations.
//
// Returns the chance for attackers to win.
//
// -------------------------------------------------------------------------- //
function analyze(atkStartTroops, defStartTroops, atkPos, defPos, atkAdd, defAdd, river) {
// this is copied from battle. hopefully i don't have to change it
// modifiers
let atkMod = (river) ? -0.1 : 0 + atkAdd;
switch(atkPos) {
case 0: // regular
break;
case 1: // hill
atkMod += 0.1;
break;
case 2: // mountain
atkMod += 0.15;
break;
case 3: // peak
atkMod += 0.2;
break;
}
let defMod = defAdd;
switch(defPos) {
case 0: // regular
break;
case 1: // hill
defMod += 0.1;
break;
case 2: // mountain
defMod += 0.15;
break;
case 3: // peak
defMod += 0.2;
break;
}
const atkEffective = atkStartTroops * (1 + atkMod);
const defEffective = defStartTroops * (1 + defMod);
return atkEffective / (atkEffective + defEffective);
}
module.exports = { battle, analyze };