-
Notifications
You must be signed in to change notification settings - Fork 7
/
hist_record.c
759 lines (700 loc) · 15.4 KB
/
hist_record.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
harris - a strategy game
Copyright (C) 2012-2015 Edward Cree
licensed under GPLv3+ - see top of harris.c for details
hist_record: parse history events from a game event log (for stats purposes)
*/
#include "hist_record.h"
#include <string.h>
#include "bits.h"
#include "date.h"
#include "saving.h"
/* Get next word from line, if there is one */
#define WORD() (word = strtok(NULL, " "))
/* Get next word from line, or complain we didn't get one */
#define WORD_OR_ERR(_what) if (!WORD()) { \
fprintf(stderr, "Expected " _what ", got EOL\n"); \
return 1; \
}
/* <ct-ev> ::= CT <mark:int> # older saves may be missing <mark>, in which case assume 0 */
int parse_creat(struct creat_record *rec)
{
char *word;
if (WORD())
{
if (sscanf(word, "%u", &rec->mark) != 1)
{
fprintf(stderr, "bad navaid '%s'\n", word);
return 2;
}
}
else
{
rec->mark = 0;
}
return 0;
}
/* <nav-ev> ::= NA <navaid:int> # installation of a navaid (or, for fighters, radar) */
int parse_nav(struct nav_record *rec)
{
char *word;
WORD_OR_ERR("navaid");
if (sscanf(word, "%u", &rec->navaid) != 1)
{
fprintf(stderr, "bad navaid '%s'\n", word);
return 2;
}
return 0;
}
/* <raid-ev>::= RA <targ-id:int> <ra-crew>
* <ra-crew>::= <cm-uid> [<ra-crew>]
*/
int parse_raid(struct raid_record *rec)
{
char *word;
WORD_OR_ERR("targ-id");
if (sscanf(word, "%u", &rec->targ) != 1)
{
fprintf(stderr, "bad targ-id '%s'\n", word);
return 2;
}
for (rec->ncrew = 0; rec->ncrew <= MAX_CREW; rec->ncrew++)
{
word = strtok(NULL, " ");
if (!word)
break;
if (gcmid(word, &rec->crew[rec->ncrew]))
{
fprintf(stderr, "bad C/M ID '%s'\n", word);
return 2;
}
}
return 0;
}
/* <hit-ev> ::= HI <targ-id:int> <bmb:int> # bmb is either lb of bombs, lb of mines, or # of leaflets. */
int parse_hit(struct hit_record *rec)
{
char *word;
WORD_OR_ERR("targ-id");
if (sscanf(word, "%u", &rec->targ) != 1)
{
fprintf(stderr, "bad targ-id '%s'\n", word);
return 2;
}
WORD_OR_ERR("bmb");
if (sscanf(word, "%u", &rec->bmb) != 1)
{
fprintf(stderr, "bad bmb '%s'\n", word);
return 2;
}
return 0;
}
/* <dmg-ev> ::= DM <delta-dmg:float> <current-dmg:float> (AC <from:ac-uid> | FK <flak-id:int> | TF <targ-d:int>) */
int parse_dmg(struct dmg_record *rec)
{
char *word;
WORD_OR_ERR("delta-dmg");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->delta) != 1)
{
fprintf(stderr, "bad delta-dmg '%s'\n", word);
return 2;
}
WORD_OR_ERR("current-dmg");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->current) != 1)
{
fprintf(stderr, "bad current-dmg '%s'\n", word);
return 2;
}
WORD_OR_ERR("source type");
if (!strcmp(word, "AC"))
{
bool ftr = container_of(rec, struct ac_record, dmg)->fighter;
rec->src.ds = ftr ? DS_BOMBER : DS_FIGHTER;
WORD_OR_ERR("source ID");
if (gacid(word, &rec->src.idx))
{
fprintf(stderr, "bad damage source index '%s'\n", word);
return 2;
}
return 0;
}
else if (!strcmp(word, "FK"))
{
rec->src.ds = DS_FLAK;
}
else if (!strcmp(word, "TF"))
{
rec->src.ds = DS_TFLK;
}
else
{
fprintf(stderr, "bad damage source type '%s'\n", word);
return 2;
}
WORD_OR_ERR("source index");
if (sscanf(word, "%u", &rec->src.idx) != 1)
{
fprintf(stderr, "bad damage source index '%s'\n", word);
return 2;
}
return 0;
}
/* <fail-ev>::= FA <failstate:int> # is really a bool */
int parse_fail(struct fail_record *rec)
{
char *word;
unsigned int fs;
WORD_OR_ERR("failstate");
if (sscanf(word, "%u", &fs) != 1)
{
fprintf(stderr, "bad failstate '%s'\n", word);
return 2;
}
if (fs > 1)
fprintf(stderr, "Warning: weird failstate %u (expect 0 or 1)\n", fs);
rec->state = !!fs;
return 0;
}
/* <crsh-ev>::= CR
* <obs-ev> ::= OB
*/
/* <data> ::= A <ac-uid> <b-or-f><type:int> <a-event> | ...
* <ac-uid> ::= <hex>*8 # randomly generated token. P(match) = 2^-32 ~= 1/4.2e9
* <b-or-f> ::= B | F
* <cclass> ::= P | N | B | E | W | G
* <a-event>::= <ct-ev> | <nav-ev> | <pff-ev> | <raid-ev> | <hit-ev> | <dmg-ev> | <fail-ev> | <crsh-ev> | <obs-ev>
*/
int parse_ac(struct ac_record *rec)
{
char *word;
WORD_OR_ERR("A/C ID");
if (gacid(word, &rec->id))
{
fprintf(stderr, "bad A/C ID '%s'\n", word);
return 2;
}
WORD_OR_ERR("type");
if (word[0] == 'B')
{
rec->fighter = false;
}
else if (word[0] == 'F')
{
rec->fighter = true;
}
else
{
fprintf(stderr, "bad <b-or-f> '%s'\n", word);
return 2;
}
if (sscanf(word+1, "%u", &rec->ac_type) != 1)
{
fprintf(stderr, "bad A/C type '%s'\n", word+1);
return 2;
}
WORD_OR_ERR("A-event class");
if (!strcmp(word, "CT"))
{
rec->type = AE_CT;
return parse_creat(&rec->creat);
}
if (!strcmp(word, "NA"))
{
rec->type = AE_NAV;
return parse_nav(&rec->nav);
}
if (!strcmp(word, "PF"))
{
rec->type = AE_PFF;
return 0;
}
if (!strcmp(word, "RA"))
{
rec->type = AE_RAID;
return parse_raid(&rec->raid);
}
if (!strcmp(word, "HI"))
{
rec->type = AE_HIT;
return parse_hit(&rec->hit);
}
if (!strcmp(word, "DM"))
{
rec->type = AE_DMG;
return parse_dmg(&rec->dmg);
}
if (!strcmp(word, "FA"))
{
rec->type = AE_FAIL;
return parse_fail(&rec->fail);
}
if (!strcmp(word, "CR"))
{
rec->type = AE_CROB;
rec->crob.ty = CROB_CR;
return 0;
}
if (!strcmp(word, "SC"))
{
rec->type = AE_CROB;
rec->crob.ty = CROB_SC;
return 0;
}
if (!strcmp(word, "OB"))
{
rec->type = AE_CROB;
rec->crob.ty = CROB_OB;
return 0;
}
fprintf(stderr, "Unrecognised A-event class '%s'\n", word);
return 2;
}
/* <t-dmg> ::= DM <delta-dmg:float> <current-dmg:float> */
int parse_tdm(struct tdm_record *rec)
{
char *word;
WORD_OR_ERR("delta-dmg");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->delta) != 1)
{
fprintf(stderr, "bad delta-dmg '%s'\n", word);
return 2;
}
WORD_OR_ERR("current-dmg");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->current) != 1)
{
fprintf(stderr, "bad current-dmg '%s'\n", word);
return 2;
}
return 0;
}
/* <t-flk> ::= FK <delta-flk:float> <current-flk:float> */
int parse_tfk(struct tfk_record *rec)
{
char *word;
WORD_OR_ERR("delta-flk");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->delta) != 1)
{
fprintf(stderr, "bad delta-flk '%s'\n", word);
return 2;
}
WORD_OR_ERR("current-flk");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->current) != 1)
{
fprintf(stderr, "bad current-flk '%s'\n", word);
return 2;
}
return 0;
}
/* <data> ::= ... | T <targ-id:int> <t-event>
* <t-event>::= <t-dmg> | <t-flk> | <t-ship>
*/
int parse_targ(struct targ_record *rec)
{
char *word;
WORD_OR_ERR("targ-id");
if (sscanf(word, "%u", &rec->id) != 1)
{
fprintf(stderr, "bad targ-id '%s'\n", word);
return 2;
}
WORD_OR_ERR("T-event class");
if (!strcmp(word, "DM"))
{
rec->type = TE_DMG;
return parse_tdm(&rec->tdm);
}
if (!strcmp(word, "FK"))
{
rec->type = TE_FLAK;
return parse_tfk(&rec->tfk);
}
if (!strcmp(word, "SH"))
{
rec->type = TE_SHIP;
return 0;
}
fprintf(stderr, "Unrecognised T-event class '%s'\n", word);
return 2;
}
/* <cash-ev>::= CA <delta-cash:int> <current-cash:int> # Only for Budget payments, as CT costs are known */
int parse_cash(struct cash_record *rec)
{
char *word;
WORD_OR_ERR("delta-cash");
if (sscanf(word, "%d", &rec->delta) != 1)
{
fprintf(stderr, "bad delta-cash '%s'\n", word);
return 2;
}
WORD_OR_ERR("current-cash");
if (sscanf(word, "%d", &rec->current) != 1)
{
fprintf(stderr, "bad current-cash '%s'\n", word);
return 2;
}
return 0;
}
/* <conf-ev>::= CO <confid:float> */
int parse_confid(struct confid_record *rec)
{
char *word;
WORD_OR_ERR("confid");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->confid) != 1)
{
fprintf(stderr, "bad confid '%s'\n", word);
return 2;
}
return 0;
}
/* <mora-ev>::= MO <morale:float> */
int parse_morale(struct morale_record *rec)
{
char *word;
WORD_OR_ERR("morale");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->morale) != 1)
{
fprintf(stderr, "bad morale '%s'\n", word);
return 2;
}
return 0;
}
/* <gprd-ev>::= GP <iclass:int> <gprod:float> <dprod:float> # dprod is _not_ the difference from the previous gprod! */
int parse_gprod(struct gprod_record *rec)
{
char *word;
WORD_OR_ERR("iclass");
if (sscanf(word, "%u", &rec->iclass) != 1)
{
fprintf(stderr, "bad iclass '%s'\n", word);
return 2;
}
if (rec->iclass < 0 || rec->iclass >= ICLASS_MIXED)
{
fprintf(stderr, "bad iclass '%d', expected 0 to %d\n", rec->iclass, ICLASS_MIXED - 1);
return 2;
}
WORD_OR_ERR("gprod");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->gprod) != 1)
{
fprintf(stderr, "bad gprod '%s'\n", word);
return 2;
}
WORD_OR_ERR("dprod");
if (sscanf(word, FLOAT, CAST_FLOAT_PTR &rec->dprod) != 1)
{
fprintf(stderr, "bad dprod '%s'\n", word);
return 2;
}
return 0;
}
/* <tpri-ev>::= TP <tclass:int> <ignore:int> # really a bool */
int parse_tprio(struct tprio_record *rec)
{
char *word;
int ignore;
WORD_OR_ERR("tclass");
if (sscanf(word, "%u", &rec->tclass) != 1)
{
fprintf(stderr, "bad tclass '%s'\n", word);
return 2;
}
if (rec->tclass < 0 || rec->tclass > T_CLASSES)
{
fprintf(stderr, "bad tclass '%d', expected 0 to %d\n", rec->tclass, T_CLASSES);
return 2;
}
WORD_OR_ERR("ignore");
if (sscanf(word, "%u", &ignore) != 1)
{
fprintf(stderr, "bad ignore '%s'\n", word);
return 2;
}
rec->ignore = (ignore != 0);
return 0;
}
/* <ipri-ev>::= IP <iclass:int> <ignore:int> # really a bool */
int parse_iprio(struct iprio_record *rec)
{
char *word;
int ignore;
WORD_OR_ERR("iclass");
if (sscanf(word, "%u", &rec->iclass) != 1)
{
fprintf(stderr, "bad iclass '%s'\n", word);
return 2;
}
if (rec->iclass < 0 || rec->iclass > I_CLASSES)
{
fprintf(stderr, "bad iclass '%d', expected 0 to %d\n", rec->iclass, I_CLASSES);
return 2;
}
WORD_OR_ERR("ignore");
if (sscanf(word, "%u", &ignore) != 1)
{
fprintf(stderr, "bad ignore '%s'\n", word);
return 2;
}
rec->ignore = (ignore != 0);
return 0;
}
/* <prop-ev>::= PG <event:int> */
int parse_prop(struct prop_record *rec)
{
char *word;
WORD_OR_ERR("event");
if (sscanf(word, "%u", &rec->event) != 1)
{
fprintf(stderr, "bad event '%s'\n", word);
return 2;
}
if (rec->event < 0 || rec->event >= NEVENTS)
{
fprintf(stderr, "bad event '%d', expected 0 to %d\n", rec->event, NEVENTS);
return 2;
}
return 0;
}
/* <misc-ev>::= <cash-ev> | <conf-ev> | <mora-ev> | <gprd-ev> | <prio-ev> | <prop-ev>
* <prio-ev>::= <tpri-ev> | <ipri-ev>
*/
int parse_misc(struct misc_record *rec)
{
char *word;
WORD_OR_ERR("M-event class");
if (!strcmp(word, "CA"))
{
rec->type = ME_CASH;
return parse_cash(&rec->cash);
}
if (!strcmp(word, "CO"))
{
rec->type = ME_CONFID;
return parse_confid(&rec->confid);
}
if (!strcmp(word, "MO"))
{
rec->type = ME_MORALE;
return parse_morale(&rec->morale);
}
if (!strcmp(word, "GP"))
{
rec->type = ME_GPROD;
return parse_gprod(&rec->gprod);
}
if (!strcmp(word, "TP"))
{
rec->type = ME_TPRIO;
return parse_tprio(&rec->tprio);
}
if (!strcmp(word, "IP"))
{
rec->type = ME_IPRIO;
return parse_iprio(&rec->iprio);
}
if (!strcmp(word, "PG"))
{
rec->type = ME_PROP;
return parse_prop(&rec->prop);
}
fprintf(stderr, "Unrecognised M-event class '%s'\n", word);
return 2;
}
/* <gen-ev> ::= GE <lrate:int> # generation of a new (student) crewman */
int parse_gen(struct gen_record *rec)
{
char *word;
WORD_OR_ERR("lrate");
if (sscanf(word, "%u", &rec->lrate) != 1)
{
fprintf(stderr, "bad lrate '%s'\n", word);
return 2;
}
return 0;
}
/* <ski-ev> ::= SK <skill:int> # reached a new integer-part of skill */
int parse_skill(struct skill_record *rec)
{
char *word;
WORD_OR_ERR("skill");
if (sscanf(word, "%u", &rec->skill) != 1)
{
fprintf(stderr, "bad skill '%s'\n", word);
return 2;
}
return 0;
}
/* <csta-ev>::= ST <cstatus> # status change (see also EX)
* <cstatus>::= C | S | I # never have E as that's an <escp-ev> rather than a <csta-ev>
*/
int parse_csta(struct csta_record *rec)
{
char *word;
WORD_OR_ERR("cstatus");
if (strlen(word) != 1)
{
fprintf(stderr, "bad cstatus '%s'\n", word);
return 2;
}
switch (*word)
{
case 'C':
rec->status = CSTATUS_CREWMAN;
return 0;
case 'S':
rec->status = CSTATUS_STUDENT;
return 0;
case 'I':
rec->status = CSTATUS_INSTRUC;
return 0;
default:
fprintf(stderr, "bad cstatus '%s'\n", word);
return 2;
}
}
/* <op-ev> ::= OP <tops:int> # completed an op (*not* generated daily for instructors!) */
int parse_op(struct op_record *rec)
{
char *word;
WORD_OR_ERR("tops");
if (sscanf(word, "%u", &rec->tops) != 1)
{
fprintf(stderr, "bad tops '%s'\n", word);
return 2;
}
return 0;
}
/* <escp-ev>::= EX <rtime:int> # number of days until return */
int parse_esc(struct esc_record *rec)
{
char *word;
WORD_OR_ERR("rtime");
if (sscanf(word, "%d", &rec->rtime) != 1)
{
fprintf(stderr, "bad rtime '%s'\n", word);
return 2;
}
return 0;
}
/* <data> ::= ... | C <cm-uid> <cclass> <crew-ev>
* <crew-ev>::= <gen-ev> | <ski-ev> | <csta-ev> | <op-ev> | <deth-ev> | <escp-ev>
*/
int parse_crewev(struct crew_record *rec)
{
char *word;
WORD_OR_ERR("C/M ID");
if (gcmid(word, &rec->id))
{
fprintf(stderr, "bad C/M ID '%s'\n", word);
return 2;
}
WORD_OR_ERR("cclass");
if (strlen(word) != 1)
{
fprintf(stderr, "bad cclass '%s'\n", word);
return 2;
}
if ((rec->cclass = lookup_crew_letter(*word)) == CCLASS_NONE)
{
fprintf(stderr, "bad cclass '%s'\n", word);
return 2;
}
WORD_OR_ERR("C-event class");
if (!strcmp(word, "GE"))
{
rec->type = CE_GEN;
return parse_gen(&rec->gen);
}
if (!strcmp(word, "SK"))
{
rec->type = CE_SKILL;
return parse_skill(&rec->skill);
}
if (!strcmp(word, "ST"))
{
rec->type = CE_STATUS;
return parse_csta(&rec->csta);
}
if (!strcmp(word, "OP"))
{
rec->type = CE_OP;
return parse_op(&rec->op);
}
if (!strcmp(word, "DE"))
{
rec->type = CE_DEATH;
rec->death.pw = false;
return 0;
}
if (!strcmp(word, "PW"))
{
rec->type = CE_DEATH;
rec->death.pw = true;
return 0;
}
if (!strcmp(word, "EX"))
{
rec->type = CE_ESC;
return parse_esc(&rec->esc);
}
fprintf(stderr, "Unrecognised C-event class '%s'\n", word);
return 2;
}
// <init-ev>::= INIT <filename:str> # Never generated, but should appear in all startpoints (*.sav.in)
int parse_init(char **rec)
{
char *word;
WORD_OR_ERR("INIT");
if (strcmp(word, "INIT"))
{
fprintf(stderr, "Expected INIT, got '%s'\n", word);
return 2;
}
*rec = strtok(NULL, "");
return 0;
}
/* <record> ::= <date> <time> <data>
* <data> ::= A <ac-uid> <b-or-f><type:int> <a-event> | T <targ-id:int> <t-event> | I <init-ev> | M <misc-ev> | C <cm-uid> <cclass> <crew-ev>
* <date> ::= <day>-<month>-<year> # as save file; times < 12:00 are saved under the previous day
* <time> ::= <hh>:<mm> # raids cannot begin before 18:00; post-raid is 11:00 thru 11:59
*/
int parse_event(struct hist_record *rec, char *line)
{
char *copy = strdup(line);
char *word;
if (!(word = strtok(copy, " ")))
{
fprintf(stderr, "Expected date, got EOL\n");
return 1;
}
rec->date = readdate(word, (date){0, 0, 0});
WORD_OR_ERR("time");
rec->time = readtime(word, (harris_time){0, 0});
WORD_OR_ERR("event class");
if (!strcmp(word, "I"))
{
rec->type = HT_INIT;
return parse_init(&rec->init);
}
if (!strcmp(word, "A"))
{
rec->type = HT_AC;
return parse_ac(&rec->ac);
}
if (!strcmp(word, "T"))
{
rec->type = HT_TARG;
return parse_targ(&rec->targ);
}
if (!strcmp(word, "M"))
{
rec->type = HT_MISC;
return parse_misc(&rec->misc);
}
if (!strcmp(word, "C"))
{
rec->type = HT_CREW;
return parse_crewev(&rec->crew);
}
fprintf(stderr, "Unrecognised event class '%s'\n", word);
return 2;
}