-
Notifications
You must be signed in to change notification settings - Fork 2
/
BattleEngine.c
601 lines (485 loc) · 18.5 KB
/
BattleEngine.c
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
/*
* 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/>.
*/
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined _MSC_VER && _MSC_VER >= 1900
#define restrict __restrict
#elif defined _MSC_VER
#define restrict
#endif
/* Lehmer RNG. */
#define RANDOM_MULTIPLIER 48271UL
#define RANDOM_MODULUS 2147483647UL
#define RANDOM_MAX (RANDOM_MODULUS - 1)
#define RANDOM_NEXT(r) ((uint32_t)((uint64_t)(r)*RANDOM_MULTIPLIER % RANDOM_MODULUS))
#define MAX_ROUNDS 6
struct unit_attributes {
float weapons;
float shield;
float armor;
uint32_t *rapid_fire;
};
struct units_attributes {
uint8_t num_kinds;
uint32_t *rapid_fire;
struct unit_attributes attributes[];
};
/* Unit group = units with the same kind. */
struct unit_group_stats {
uint64_t times_fired;
uint64_t times_was_shot;
uint64_t shield_damage_dealt;
uint64_t hull_damage_dealt;
uint64_t shield_damage_taken;
uint64_t hull_damage_taken;
uint64_t num_remaining_units;
};
struct combatant {
struct unit_group_stats *stats;
uint8_t weapons_technology;
uint8_t shielding_technology;
uint8_t armor_technology;
uint64_t *unit_groups;
};
struct unit {
float shield;
float hull;
uint8_t kind;
uint8_t combatant_id;
};
struct party {
struct combatant *combatants;
struct unit *units;
uint64_t num_alive;
};
const uint64_t MAX_UNITS = UINT64_MAX / sizeof(struct unit);
static struct units_attributes *load_units_attributes(FILE *file) {
int n;
uint8_t num_kinds;
n = fscanf(file, "%" SCNu8, &num_kinds);
if (n != 1) {
fputs("Parsing units attributes failed, cannot scan num_kinds\n", stderr);
goto fail;
}
if (num_kinds == 0) {
fputs("Parsing units attributes failed, num_kinds must be greater than 0\n", stderr);
goto fail;
}
struct units_attributes *units_attributes =
malloc(sizeof(*units_attributes) + num_kinds * sizeof(*units_attributes->attributes));
if (units_attributes == NULL) {
fputs("Parsing units attributes failed, allocation of attributes failed\n", stderr);
goto fail;
}
uint32_t *rapid_fire = calloc((size_t)num_kinds * (size_t)num_kinds, sizeof(*rapid_fire));
if (rapid_fire == NULL) {
fputs("Parsing units attributes failed, allocation of rapid fire failed\n", stderr);
goto fail_units_attributes;
}
units_attributes->num_kinds = num_kinds;
units_attributes->rapid_fire = rapid_fire;
for (uint8_t kind = 0; kind < num_kinds; kind++) {
struct unit_attributes *attr = &units_attributes->attributes[kind];
attr->rapid_fire = rapid_fire + (size_t)kind * (size_t)num_kinds;
uint8_t num_rapid_fire;
n = fscanf(file, "%f%f%f%" SCNu8, &attr->weapons, &attr->shield, &attr->armor, &num_rapid_fire);
if (n != 4) {
fprintf(stderr, "Parsing units attributes failed, cannot scan kind #%" PRIu8 "\n", kind);
goto fail_rapid_fire;
}
for (uint32_t i = 0; i < num_rapid_fire; i++) {
uint8_t target_kind;
uint32_t rf;
n = fscanf(file, "%" SCNu8 "%" SCNu32, &target_kind, &rf);
if (n != 2) {
fprintf(stderr,
"Parsing units attributes failed, cannot scan rapid fire "
"#%" PRIu32 " for kind #%" PRIu8 "\n",
i, kind);
goto fail_rapid_fire;
}
if (target_kind >= num_kinds) {
fprintf(stderr,
"Parsing units attributes failed, rapid fire #%" PRIu32 " is "
"invalid for kind #%" PRIu8 "\n",
i, kind);
goto fail_rapid_fire;
}
attr->rapid_fire[target_kind] = rf;
}
}
return units_attributes;
fail_rapid_fire:
free(rapid_fire);
fail_units_attributes:
free(units_attributes);
fail:
return NULL;
}
static void cleanup_units_attributes(struct units_attributes *units_attributes) {
free(units_attributes->rapid_fire);
free(units_attributes);
}
static size_t calc_combatants_alloc_size(const struct units_attributes *restrict units_attributes,
uint32_t num_combatants) {
const uint8_t num_kinds = units_attributes->num_kinds;
return num_combatants * sizeof(struct combatant) + num_combatants * num_kinds * sizeof(uint64_t) +
num_combatants * MAX_ROUNDS * num_kinds * sizeof(struct unit_group_stats);
}
static struct combatant *load_combatants(FILE *restrict file, const struct units_attributes *restrict units_attributes,
uint32_t num_combatants) {
const uint8_t num_kinds = units_attributes->num_kinds;
int n;
assert(num_combatants > 0 && num_combatants <= 2 * 256);
assert(num_kinds > 0);
size_t total_size = calc_combatants_alloc_size(units_attributes, num_combatants);
struct combatant *combatants = calloc(total_size, 1);
if (combatants == NULL) {
fputs("Loading combatants failed, allocation of combatants failed\n", stderr);
goto fail;
}
uint64_t *unit_groups = (uint64_t *)&combatants[num_combatants];
struct unit_group_stats *stats = (struct unit_group_stats *)&unit_groups[num_combatants * num_kinds];
for (uint32_t i = 0; i < num_combatants; i++, unit_groups += num_kinds, stats += MAX_ROUNDS * num_kinds) {
struct combatant *c = &combatants[i];
c->unit_groups = unit_groups;
c->stats = stats;
uint8_t num_unit_groups;
n = fscanf(file, "%" SCNu8 "%" SCNu8 "%" SCNu8 "%" SCNu8, &c->weapons_technology, &c->shielding_technology,
&c->armor_technology, &num_unit_groups);
if (n != 4) {
fprintf(stderr, "Loading combatants failed, cannot scan combatant #%" PRIu32 "\n", i);
goto fail_combatants;
}
for (uint8_t j = 0; j < num_unit_groups; j++) {
uint8_t kind;
uint64_t num_units;
n = fscanf(file, "%" SCNu8 "%" SCNu64, &kind, &num_units);
if (n != 2) {
fprintf(stderr,
"Loading combatants failed, cannot scan unit group #%" PRIu8 " "
"for combatant #%" PRIu32 "\n",
j, i);
goto fail_combatants;
}
if (kind >= num_kinds) {
fprintf(stderr,
"Loading combatants failed, unit group #%" PRIu8 " is invalid "
"for combatant #%" PRIu32 "\n",
j, i);
goto fail_combatants;
}
unit_groups[kind] = num_units;
}
}
return combatants;
fail_combatants:
free(combatants);
fail:
return NULL;
}
static struct party *create_party(const struct units_attributes *restrict units_attributes,
struct combatant *combatants, uint32_t num_combatants) {
const uint8_t num_kinds = units_attributes->num_kinds;
assert(num_combatants <= 256);
struct party *party = malloc(sizeof(*party));
if (party == NULL) {
fputs("Allocating memory for a party failed\n", stderr);
goto fail;
}
uint64_t total_units = 0;
for (uint32_t i = 0; i < num_combatants; i++) {
for (uint8_t kind = 0; kind < num_kinds; kind++) {
uint64_t num_units = combatants[i].unit_groups[kind];
if (num_units > MAX_UNITS - total_units) {
fputs("Too many units\n", stderr);
goto fail_party;
}
total_units += num_units;
}
}
assert(total_units <= SIZE_MAX / sizeof(struct unit));
struct unit *units = malloc(total_units * sizeof(*units));
if (units == NULL) {
fputs("Allocating memory for party units failed\n", stderr);
goto fail_party;
}
party->combatants = combatants;
party->units = units;
party->num_alive = total_units;
for (uint32_t i = 0; i < num_combatants; i++) {
const struct combatant *combatant = &combatants[i];
for (uint8_t kind = 0; kind < num_kinds; kind++) {
float max_hull = 0.1f * units_attributes->attributes[kind].armor * (1.0f + 0.1f * combatant->armor_technology);
for (uint32_t j = 0; j < combatant->unit_groups[kind]; j++) {
struct unit *unit = units++;
unit->hull = max_hull;
unit->kind = kind;
unit->combatant_id = (uint8_t)i;
}
}
}
return party;
fail_party:
free(party);
fail:
return NULL;
}
static void restore_shields(const struct units_attributes *restrict units_attributes, struct party *restrict party) {
const struct combatant *combatants = party->combatants;
struct unit *units = party->units;
uint64_t num_alive = party->num_alive;
for (uint64_t i = 0; i < num_alive; i++) {
struct unit *unit = &units[i];
unit->shield = units_attributes->attributes[unit->kind].shield *
(1.0f + 0.1f * combatants[unit->combatant_id].shielding_technology);
}
}
static void fire(const struct units_attributes *restrict units_attributes, struct party *restrict attackers_party,
struct party *restrict defenders_party, uint32_t round, uint32_t *restrict random) {
const uint8_t num_kinds = units_attributes->num_kinds;
uint32_t r = *random;
struct combatant *attackers = attackers_party->combatants;
struct unit *shooters = attackers_party->units;
uint64_t num_shooters = attackers_party->num_alive;
struct combatant *defenders = defenders_party->combatants;
struct unit *targets = defenders_party->units;
uint64_t num_targets = defenders_party->num_alive;
for (uint64_t i = 0; i < num_shooters; i++) {
const struct unit *shooter = &shooters[i];
uint8_t shooter_kind = shooter->kind;
const struct unit_attributes *shooter_attrs = &units_attributes->attributes[shooter_kind];
struct combatant *attacker = &attackers[shooter->combatant_id];
struct unit_group_stats *shooter_stats = &attacker->stats[round * num_kinds + shooter_kind];
float damage = shooter_attrs->weapons * (1.0f + 0.1f * attackers[shooter->combatant_id].weapons_technology);
uint32_t rapid_fire;
do {
r = RANDOM_NEXT(r);
struct unit *target = &targets[r % num_targets];
uint8_t target_kind = target->kind;
const struct unit_attributes *target_attrs = &units_attributes->attributes[target_kind];
struct combatant *defender = &defenders[target->combatant_id];
struct unit_group_stats *target_stats = &defender->stats[round * num_kinds + target_kind];
shooter_stats->times_fired++;
target_stats->times_was_shot++;
if (target->hull != 0.0f) {
float hull = target->hull;
float hull_damage = damage - target->shield;
if (hull_damage < 0.0f) {
float max_shield = target_attrs->shield * (1.0f + 0.1f * defender->shielding_technology);
float shield_damage = 0.01f * floorf(100.0f * damage / max_shield) * max_shield;
target->shield -= shield_damage;
shooter_stats->shield_damage_dealt += (uint64_t)shield_damage;
target_stats->shield_damage_taken += (uint64_t)shield_damage;
} else {
shooter_stats->shield_damage_dealt += (uint64_t)target->shield;
target_stats->shield_damage_taken += (uint64_t)target->shield;
target->shield = 0.0f;
if (hull_damage > hull) {
hull_damage = hull;
}
hull -= hull_damage;
shooter_stats->hull_damage_dealt += (uint64_t)hull_damage;
target_stats->hull_damage_taken += (uint64_t)hull_damage;
}
if (hull != 0.0f) {
float max_hull = 0.1f * target_attrs->armor * (1.0f + 0.1f * defender->armor_technology);
if (hull < 0.7f * max_hull) {
r = RANDOM_NEXT(r);
if (hull < (1.0f / (float)RANDOM_MAX) * (float)r * max_hull) {
hull = 0.0f;
}
}
}
target->hull = hull;
}
rapid_fire = shooter_attrs->rapid_fire[target_kind];
} while (rapid_fire != 0 && (r = RANDOM_NEXT(r)) % rapid_fire != 0);
}
*random = r;
}
static void update_units(const struct units_attributes *restrict units_attributes,
struct combatant *restrict combatants, struct party *restrict party, uint32_t round) {
const uint8_t num_kinds = units_attributes->num_kinds;
struct unit *units = party->units;
uint64_t num_alive = party->num_alive, n = 0;
for (uint64_t i = 0; i < num_alive; i++) {
struct unit *unit = &units[i];
if (unit->hull != 0.0f) {
units[n++] = *unit;
struct combatant *combatant = &combatants[unit->combatant_id];
combatant->stats[round * num_kinds + unit->kind].num_remaining_units++;
}
}
party->num_alive = n;
}
static void update_combatants(const struct units_attributes *restrict units_attributes,
struct combatant *restrict combatants, uint32_t num_combatants,
struct party *restrict party) {
const uint8_t num_kinds = units_attributes->num_kinds;
for (uint32_t i = 0; i < num_combatants; i++) {
struct combatant *combatant = &combatants[i];
memset(combatant->unit_groups, 0, num_kinds * sizeof(*combatant->unit_groups));
}
for (uint64_t i = 0; i < party->num_alive; i++) {
const struct unit *unit = &party->units[i];
combatants[unit->combatant_id].unit_groups[unit->kind]++;
}
}
static bool fight(const struct units_attributes *restrict units_attributes, struct combatant *restrict attackers,
uint32_t num_attackers, struct combatant *restrict defenders, uint32_t num_defenders,
uint32_t *restrict num_rounds, uint32_t *restrict random) {
bool ret = false;
struct party *attackers_party = create_party(units_attributes, attackers, num_attackers);
if (attackers_party == NULL) {
goto out;
}
struct party *defenders_party = create_party(units_attributes, defenders, num_defenders);
if (defenders_party == NULL) {
goto out_attackers_party;
}
uint32_t round = 0;
while (round < MAX_ROUNDS && attackers_party->num_alive > 0 && defenders_party->num_alive > 0) {
restore_shields(units_attributes, attackers_party);
restore_shields(units_attributes, defenders_party);
fire(units_attributes, attackers_party, defenders_party, round, random);
fire(units_attributes, defenders_party, attackers_party, round, random);
update_units(units_attributes, attackers, attackers_party, round);
update_units(units_attributes, defenders, defenders_party, round);
round++;
}
*num_rounds = round;
update_combatants(units_attributes, attackers, num_attackers, attackers_party);
update_combatants(units_attributes, defenders, num_defenders, defenders_party);
ret = true;
free(defenders_party->units);
free(defenders_party);
out_attackers_party:
free(attackers_party->units);
free(attackers_party);
out:
return ret;
}
static void dump_stats(FILE *restrict file, const struct combatant *restrict combatants, uint32_t num_combatants,
uint32_t num_rounds, uint8_t num_kinds) {
for (uint32_t i = 0; i < num_combatants; i++) {
const struct combatant *combatant = &combatants[i];
for (uint32_t round = 0; round < num_rounds; round++) {
const struct unit_group_stats *stats = &combatant->stats[round * num_kinds];
for (uint8_t kind = 0; kind < num_kinds; kind++) {
const struct unit_group_stats *s = &stats[kind];
fprintf(file, "%" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
s->times_fired, s->times_was_shot, s->shield_damage_dealt, s->hull_damage_dealt, s->shield_damage_taken,
s->hull_damage_taken, s->num_remaining_units);
}
fputc('\n', file);
}
}
}
static int simulate(uint32_t seed, uint32_t num_simulations) {
int n, ret = 1;
if (num_simulations == 0) {
return 0;
}
struct units_attributes *units_attributes = load_units_attributes(stdin);
if (units_attributes == NULL) {
goto out;
}
uint32_t num_attackers, num_defenders;
n = fscanf(stdin, "%" SCNu32 "%" SCNu32, &num_attackers, &num_defenders);
if (n != 2) {
fputs("Scanning the number of combatants failed\n", stderr);
goto out_units_attributes;
}
if (num_attackers == 0 || num_defenders == 0) {
puts("0");
ret = 0;
goto out_units_attributes;
}
if (num_attackers > 256) {
fputs("The number of attackers cannot be greater than 256\n", stderr);
goto out_units_attributes;
}
if (num_defenders > 256) {
fputs("The number of defenders cannot be greater than 256\n", stderr);
goto out_units_attributes;
}
uint32_t num_combatants = num_attackers + num_defenders;
struct combatant *combatants = load_combatants(stdin, units_attributes, num_combatants);
if (combatants == NULL) {
goto out_units_attributes;
}
struct combatant *combatants_copy = NULL;
size_t combatants_size = 0;
if (num_simulations > 1) {
combatants_size = calc_combatants_alloc_size(units_attributes, num_combatants);
combatants_copy = malloc(combatants_size);
if (combatants_copy == NULL) {
goto out_combatants;
}
memcpy(combatants_copy, combatants, combatants_size);
}
struct combatant *attackers = combatants;
struct combatant *defenders = &combatants[num_attackers];
for (uint32_t n = 0; n < num_simulations; n++) {
if (n != 0) {
memcpy(combatants, combatants_copy, combatants_size);
}
uint32_t num_rounds = 0;
fight(units_attributes, attackers, num_attackers, defenders, num_defenders, &num_rounds, &seed);
printf("%" PRIu32 "\n\n", num_rounds);
dump_stats(stdout, combatants, num_attackers + num_defenders, num_rounds, units_attributes->num_kinds);
}
ret = 0;
free(combatants_copy);
out_combatants:
free(combatants);
out_units_attributes:
cleanup_units_attributes(units_attributes);
out:
return ret;
}
int main(int argc, char *argv[]) {
int n;
if (argc != 3) {
fprintf(stderr, "Usage: %s <SEED> <NUM_SIMULATIONS>\n", argv[0]);
return 1;
}
uint32_t seed;
n = sscanf(argv[1], "%" SCNu32, &seed);
if (n != 1) {
fputs("Scanning seed failed\n", stderr);
return 1;
}
if (seed == 0) {
fputs("Seed cannot be 0\n", stderr);
return 1;
}
uint32_t num_simulations;
n = sscanf(argv[2], "%" SCNu32, &num_simulations);
if (n != 1) {
fputs("Scanning num_simulations failed\n", stderr);
return 1;
}
return simulate(seed, num_simulations);
}