-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBattleEngine.php
499 lines (435 loc) · 13.8 KB
/
BattleEngine.php
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
<?php
/*
* Copyright (C) 2020 Patryk Stefanski
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace BattleEngine;
use Exception;
use InvalidArgumentException;
class UnitAttributes
{
private float $weapons;
private float $shield;
private float $armor;
private array $rapidFire;
public function __construct(
float $weapons,
float $shield,
float $armor,
array $rapidFire
)
{
if ($weapons <= 0.0) {
throw new InvalidArgumentException('weapons must be greater than 0');
}
if ($shield <= 0.0) {
throw new InvalidArgumentException('shield must be greater than 0');
}
if ($armor <= 0.0) {
throw new InvalidArgumentException('armor must be greater than 0');
}
foreach ($rapidFire as $count) {
if ($count < 0 || $count > (1 << 32) - 1) {
throw new InvalidArgumentException('rapidFire elements must be between 0 and 2**32-1');
}
}
$this->weapons = $weapons;
$this->shield = $shield;
$this->armor = $armor;
$this->rapidFire = $rapidFire;
}
public function getWeapons(): float
{
return $this->weapons;
}
public function getShield(): float
{
return $this->shield;
}
public function getArmor(): float
{
return $this->armor;
}
public function getRapidFire(): array
{
return $this->rapidFire;
}
}
class Combatant
{
private int $weaponsTechnology;
private int $shieldingTechnology;
private int $armorTechnology;
private array $unitGroups;
public function __construct(
int $weaponsTechnology,
int $shieldingTechnology,
int $armorTechnology,
array $unitGroups
)
{
if ($weaponsTechnology < 0 || $weaponsTechnology > 255) {
throw new InvalidArgumentException('weaponsTechnology must be between 0 and 255');
}
if ($shieldingTechnology < 0 || $shieldingTechnology > 255) {
throw new InvalidArgumentException('shieldingTechnology must be between 0 and 255');
}
if ($armorTechnology < 0 || $armorTechnology > 255) {
throw new InvalidArgumentException('armorTechnology must be between 0 and 255');
}
foreach ($unitGroups as $count) {
if ($count < 0) {
throw new InvalidArgumentException('unitGroups elements must be at least 0');
}
}
$this->weaponsTechnology = $weaponsTechnology;
$this->shieldingTechnology = $shieldingTechnology;
$this->armorTechnology = $armorTechnology;
$this->unitGroups = $unitGroups;
}
public function getWeaponsTechnology(): int
{
return $this->weaponsTechnology;
}
public function getShieldingTechnology(): int
{
return $this->shieldingTechnology;
}
public function getArmorTechnology(): int
{
return $this->armorTechnology;
}
public function getUnitGroups(): array
{
return $this->unitGroups;
}
}
class UnitGroupStats
{
private int $timesFired;
private int $timesWasShot;
private int $shieldDamageDealt;
private int $hullDamageDealt;
private int $shieldDamageTaken;
private int $hullDamageTaken;
private int $numRemainingUnits;
public function __construct(
int $timesFired,
int $timesWasShot,
int $shieldDamageDealt,
int $hullDamageDealt,
int $shieldDamageTaken,
int $hullDamageTaken,
int $numRemainingUnits
)
{
$this->timesFired = $timesFired;
$this->timesWasShot = $timesWasShot;
$this->shieldDamageDealt = $shieldDamageDealt;
$this->hullDamageDealt = $hullDamageDealt;
$this->shieldDamageTaken = $shieldDamageTaken;
$this->hullDamageTaken = $hullDamageTaken;
$this->numRemainingUnits = $numRemainingUnits;
}
public function getTimesFired(): int
{
return $this->timesFired;
}
public function getTimesWasShot(): int
{
return $this->timesWasShot;
}
public function getShieldDamageDealt(): int
{
return $this->shieldDamageDealt;
}
public function getHullDamageDealt(): int
{
return $this->hullDamageDealt;
}
public function getShieldDamageTaken(): int
{
return $this->shieldDamageTaken;
}
public function getHullDamageTaken(): int
{
return $this->hullDamageTaken;
}
public function getNumRemainingUnits(): int
{
return $this->numRemainingUnits;
}
}
class CombatantOutcome
{
private array $roundsStats;
public function __construct(array $roundsStats)
{
$this->roundsStats = $roundsStats;
}
public function getRoundStats(int $roundNo): array
{
return $this->roundsStats[$roundNo];
}
public function getRoundsStats(): array
{
return $this->roundsStats;
}
}
class BattleOutcome
{
private int $numRounds;
private array $attackersOutcomes;
private array $defendersOutcomes;
public function __construct(
int $numRounds,
array $attackersOutcomes,
array $defendersOutcomes
)
{
$this->numRounds = $numRounds;
$this->attackersOutcomes = $attackersOutcomes;
$this->defendersOutcomes = $defendersOutcomes;
}
public function getNumRounds(): int
{
return $this->numRounds;
}
public function getAttackersOutcomes(): array
{
return $this->attackersOutcomes;
}
public function getDefendersOutcomes(): array
{
return $this->defendersOutcomes;
}
}
class Error extends Exception
{
}
class BattleEngine
{
private string $enginePath;
private array $unitsAttributes;
public function __construct(string $enginePath, array $unitsAttributes)
{
$this->enginePath = $enginePath;
$this->unitsAttributes = $unitsAttributes;
$this->assertValidUnitsAttributes();
}
private function assertValidUnitsAttributes()
{
$numKinds = count($this->unitsAttributes);
if ($numKinds === 0) {
throw new InvalidArgumentException('unitsAttributes cannot be empty');
}
for ($i = 0; $i < $numKinds; $i++) {
if (!array_key_exists($i, $this->unitsAttributes)) {
$message = sprintf('no unit with key %d found in unitsAttributes', $i);
throw new InvalidArgumentException($message);
}
$rapidFire = $this->unitsAttributes[$i]->getRapidFire();
foreach ($rapidFire as $kind => $count) {
if ($kind >= $numKinds) {
$message = sprintf(
'unit with key %d in rapidFire of unit with key %d does not exist in unitsAttributes',
$kind,
$i
);
throw new InvalidArgumentException($message);
}
}
}
}
private function makeStdinForUnitsAttributes(): string
{
$stdin = [];
$stdin[] = count($this->unitsAttributes);
$stdin[] = '';
foreach ($this->unitsAttributes as $attrs) {
$rapidFire = $attrs->getRapidFire();
$stdin[] = sprintf(
'%f %f %f %d',
$attrs->getWeapons(),
$attrs->getShield(),
$attrs->getArmor(),
count($rapidFire),
);
foreach ($rapidFire as $kind => $count) {
$stdin[] = sprintf('%d %d', $kind, $count);
}
$stdin[] = '';
}
return implode("\n", $stdin);
}
private function assertValidCombatants(string $name, array $combatants)
{
if (count($combatants) >= 256) {
throw new InvalidArgumentException('too many ' . $name);
}
foreach ($combatants as $i => $combatant) {
foreach ($combatant->getUnitGroups() as $kind => $count) {
if (!array_key_exists($kind, $this->unitsAttributes)) {
$message = sprintf(
'no unit with key %d found in unitsAttributes for %s at index %d',
$kind,
$name,
$i
);
throw new InvalidArgumentException($message);
}
}
}
}
private function makeStdinForCombatant(Combatant $combatant): string
{
$unitGroups = $combatant->getUnitGroups();
$stdin = [];
$stdin[] = sprintf(
'%d %d %d %d',
$combatant->getWeaponsTechnology(),
$combatant->getShieldingTechnology(),
$combatant->getArmorTechnology(),
count($unitGroups)
);
foreach ($unitGroups as $kind => $count) {
$stdin[] = sprintf('%d %d', $kind, $count);
}
return implode("\n", $stdin);
}
private function makeStdinForCombatants(
array $attackers,
array $defenders
): string
{
$stdin = [];
$stdin[] = sprintf('%d %d', count($attackers), count($defenders));
$stdin[] = '';
foreach ($attackers as $attacker) {
$stdin[] = $this->makeStdinForCombatant($attacker);
$stdin[] = '';
}
foreach ($defenders as $defender) {
$stdin[] = $this->makeStdinForCombatant($defender);
$stdin[] = '';
}
return implode("\n", $stdin);
}
private function parseCombatantOutcome(
int $numRounds,
array $data
): CombatantOutcome
{
$numKinds = count($this->unitsAttributes);
$index = 0;
$roundsStats = [];
for ($roundNo = 0; $roundNo < $numRounds; $roundNo++) {
$roundStats = [];
for ($kind = 0; $kind < $numKinds; $kind++) {
list(
$timesFired,
$timesWasShot,
$shieldDamageDealt,
$hullDamageDealt,
$shieldDamageTaken,
$hullDamageTaken,
$numRemainingUnits
) = array_slice($data, $index, 7);
$roundStats[] = new UnitGroupStats(
$timesFired,
$timesWasShot,
$shieldDamageDealt,
$hullDamageDealt,
$shieldDamageTaken,
$hullDamageTaken,
$numRemainingUnits
);
$index += 7;
}
$roundsStats[] = $roundStats;
}
return new CombatantOutcome($roundsStats);
}
public function simulate(
array $attackers,
array $defenders,
int $seed = 0,
int $numSimulations = 1
): array
{
$this->assertValidCombatants('attackers', $attackers);
$this->assertValidCombatants('defenders', $defenders);
if ($seed < 0) {
throw new InvalidArgumentException('seed must be at least 0');
}
if ($seed === 0) {
$seed = rand(1, 1000000000);
}
$cmd = $this->enginePath . ' ' . $seed . ' ' . $numSimulations;
$descriptorspec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
];
$cwd = null;
$env = [];
$p = proc_open($cmd, $descriptorspec, $pipes, $cwd, $env);
if (!is_resource($p)) {
throw new Error('opening process failed');
}
$attrsStdin = $this->makeStdinForUnitsAttributes();
$combatantsStdin = $this->makeStdinForCombatants(
$attackers,
$defenders
);
$stdin = $attrsStdin . "\n" . $combatantsStdin;
fwrite($pipes[0], $stdin);
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$returnCode = proc_close($p);
if ($returnCode !== 0) {
throw new Error($stderr);
}
$numKinds = count($this->unitsAttributes);
$numAttackers = count($attackers);
$numDefenders = count($defenders);
$result = array_map('intval', preg_split('/\s+/', $stdout));
$simulations = [];
$idx = 0;
for ($i = 0; $i < $numSimulations; $i++) {
$numRounds = $result[$idx];
$idx++;
$outcomeSize = $numRounds * $numKinds * 7;
$outcomes = [];
for ($j = 0; $j < $numAttackers + $numDefenders; $j++) {
$d = array_slice($result, $idx, $outcomeSize);
$idx += $outcomeSize;
$outcomes[] = $this->parseCombatantOutcome($numRounds, $d);
}
$attackersOutcomes = array_slice($outcomes, 0, $numAttackers);
$defendersOutcomes = array_slice($outcomes, $numAttackers);
$simulations[] = new BattleOutcome(
$numRounds,
$attackersOutcomes,
$defendersOutcomes
);
}
return $simulations;
}
}