-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathbattle.service.ts
313 lines (282 loc) · 9.96 KB
/
battle.service.ts
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
import { Injectable } from "@angular/core";
import { createWorker, ITypedWorker } from "typed-web-workers";
import { getUrl } from "./main.service";
import { EnemyManager } from "./model/enemy/enemyManager";
import { BattleRequest, ShipData } from "./workers/battleRequest";
import { Emitters } from "./emitters";
import { Enemy } from "./model/enemy/enemy";
declare let MyFromDecimal: (arg: any) => Decimal;
@Injectable({
providedIn: "root"
})
export class BattleService {
private static instance: BattleService;
battleWorker: ITypedWorker<BattleRequest, BattleResult>;
em: Emitters;
enemy: Enemy;
constructor() {
BattleService.instance = this;
const url = getUrl();
this.battleWorker = createWorker({
workerFunction: this.doBattle,
onMessage: this.onBattleEnd.bind(this),
onError: error => {},
importScripts: [
url + "break_infinity.min.js",
url + "assets/is.js"
// url + "assets/ship.js",
// url + "assets/battleResult.js",
// url + "assets/myUtility.js"
]
});
}
static getInstance(): BattleService {
return BattleService.instance;
}
doBattle(input: BattleRequest, cb: (_: BattleResult) => void): void {
// console.log(input);
const initial = Date.now();
const playerShips = new Array<Ship>();
const enemyShip = new Array<Ship>();
//#region Initialize Fleets
const fleets: Array<[Ship[], ShipData[]]> = [
[enemyShip, input.enemyFleet],
[playerShips, input.playerFleet]
];
fleets.forEach(toMake => {
const ships = toMake[0];
const design = toMake[1];
design.forEach(ds => {
const ship = new Ship();
ship.id = ds.id;
ship.class = ds.class;
ship.isDefense = ds.isDefense;
ship.armor = MyFromDecimal(ds.totalArmor);
ship.originalArmor = MyFromDecimal(ship.armor);
ship.shield = MyFromDecimal(ds.totalShield);
ship.originalShield = MyFromDecimal(ship.shield);
ship.explosionLevel = ds.explosionLevel / 100;
ship.armorReduction = MyFromDecimal(ds.armorReduction);
ship.shieldReduction = MyFromDecimal(ds.shieldReduction);
ship.shieldCharger = MyFromDecimal(ds.shieldCharger);
ds.modules.forEach(dl => {
if (MyFromDecimal(dl.computedDamage).gt(0)) {
ship.modules.push({
damage: MyFromDecimal(dl.computedDamage),
shieldPercent: dl.shieldPercent,
armorPercent: dl.armorPercent
});
}
});
const qta = MyFromDecimal(ds.quantity).toNumber();
for (let num = 0; num < qta; num++) {
const copy = ship.getCopy();
copy.fleetIndex = ships.push(copy) - 1;
}
});
});
// console.log("player ships: " + playerShips.length);
// console.log("enemy ships: " + enemyShip.length);
//#endregion
//#region Battle
// Up to 5 rounds
const battleFleets = [playerShips, enemyShip];
// for each round
// Ships are assumed to all attack at the same time
for (let round = 0; round < 5; round++) {
// for both player and enemy fleets
for (let num = 0; num < 2; num++) {
const ships = battleFleets[num];
const targets = battleFleets[(num + 1) % 2];
// Create a copy of targets so it's simpler to implement
const all = [];
const defenders = [];
const ground = [];
for (const target of targets) {
all.push(target);
if (target.class === "3") {
defenders.push(target);
}
if (target.isDefense) {
ground.push(target);
}
}
// console.log(defenders.length);
for (const ship of ships) {
let availableTargets = all;
// Bomber can only hit grounded target
if (ship.class === "2") {
// skip early if there no ground unit
if (ground.length === 0) continue;
availableTargets = ground;
} else if (
// 80% chance of hitting a defender
defenders.length > 0 &&
Math.random() < 0.8
) {
availableTargets = defenders;
}
if (availableTargets.length === 0) continue;
for (const weapon of ship.modules) {
const target =
availableTargets[
Math.floor(Math.random() * availableTargets.length)
];
// targeted dead ship, consider attack as missed
if (target.armor.lt(0)) continue;
let damageToDo = weapon.damage;
// Damage to shield
if (target.shield.gt(0)) {
const shieldPercent = weapon.shieldPercent / 100;
const maxShieldDamage = damageToDo
.times(shieldPercent)
.minus(target.shieldReduction)
.max(0);
// Skip if damage <0.1% shield
if (maxShieldDamage.lt(target.shield.div(1000))) continue;
target.shield = target.shield.minus(maxShieldDamage);
if (target.shield.gte(0)) continue;
damageToDo = target.shield
.minus(target.shieldReduction)
.abs()
.div(shieldPercent);
// console.log(damageToDo.toNumber());
}
// Damage to Armor
if (damageToDo.gt(0)) {
const maxArmorDamage = damageToDo
.times(weapon.armorPercent / 100)
.minus(target.armorReduction)
.max(0);
// Skip if damage < 0.1% armor
if (maxArmorDamage.lt(target.armor.div(1000))) continue;
target.armor = target.armor.minus(maxArmorDamage);
// Check explosion
// console.log(
// target.armor.div(target.originalArmor).toNumber() +
// " - " +
// target.explosionLevel
// );
if (
target.armor.gt(0) &&
target.armor.div(target.originalArmor).toNumber() <
target.explosionLevel
) {
const prob =
1 -
target.armor
.div(target.originalArmor.times(target.explosionLevel))
.toNumber();
// console.log(
// "Expl:" +
// target.armor.toNumber() +
// " " +
// target.originalArmor.toNumber() +
// " " +
// target.explosionLevel +
// " " +
// prob
// );
if (Math.random() < prob) {
// Explode
target.armor.fromNumber(-1);
}
}
// Remove dead ship
if (target.armor.lt(0)) {
target.free();
const last_target = targets.pop();
// Modify target array inplace without shifting every item (optimization)
if (target !== last_target) {
// Take the last target in array and put it where the target was.
last_target.fleetIndex = target.fleetIndex;
targets[target.fleetIndex] = last_target;
}
}
}
}
// If all target are dead, skip the rest of the ships in fleet.
if (targets.length === 0) {
// console.log("break");
break;
}
}
}
// If one of the fleet is dead
if (battleFleets[0].length < 1 || battleFleets[1].length < 1) break;
// Recharge shields
for (const ships of battleFleets) {
let totalCharge = ships.reduce(
(p, s) => p.plus(s.shieldCharger),
new Decimal(0)
);
// console.log("total charge: " + totalCharge.toNumber());
if (totalCharge.gt(0)) {
const sorted = ships
.filter(s => s.shield.gt(0))
.sort((a, b) =>
a.shield.div(a.originalShield).cmp(b.shield.div(b.originalShield))
);
for (const ship of sorted) {
const missing = ship.originalShield
.minus(ship.shield)
.min(totalCharge);
ship.shield = ship.shield.plus(missing);
totalCharge = totalCharge.minus(missing);
// console.log(totalCharge.toNumber());
if (totalCharge.lte(0)) break;
// console.log("charged: " + missing.toNumber());
}
}
}
// Regenerate shields
// playerShips
// .concat(enemyShip)
// .filter(s => s.shield.gt(0))
// .forEach(s => {
// s.shield = new Decimal(s.originalShield);
// });
}
//#endregion
//#region Return
const ret = new BattleResult();
if (enemyShip.length < 1) ret.result = "1";
const retArr: Array<[ShipData[], Ship[], Array<[string, Decimal]>]> = [
[input.playerFleet, playerShips, ret.playerLost],
[input.enemyFleet, enemyShip, ret.enemyLost]
];
retArr.forEach(arr => {
const fleetCount = {};
arr[0].forEach(fl => {
fleetCount[fl.id] = 0;
});
// Count and free the ships object
for (const ship of arr[1]) {
fleetCount[ship.id] += 1;
ship.free();
}
arr[0].forEach(fl => {
const alive = fleetCount[fl.id];
const qta = MyFromDecimal(fl.quantity);
if (qta.gt(alive)) {
arr[2].push([fl.id, qta.minus(alive)]);
}
});
});
const diff = Math.max(input.minTime * 1000 - Date.now() + initial, 5);
if (diff > 0) {
// console.log("wait:" + diff);
setTimeout(() => cb(ret), diff);
} else {
cb(ret);
}
//#endregion
}
onBattleEnd(result: BattleResult): void {
EnemyManager.getInstance().onBattleEnd(result);
if (this.em) {
this.em.updateEmitter.emit(2);
this.em.battleEndEmitter.emit(1);
}
}
}