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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <units.h>
#include <info.h>
#include <vec.h>
#include <jsmn.h>
#include <jsonparse.h>
int type_damage_map (char strdmg[][32]) {
strcpy(strdmg[0], "pierce");
strcpy(strdmg[1], "laser");
strcpy(strdmg[2], "impact");
strcpy(strdmg[3], "fusion");
strcpy(strdmg[4], "explosive");
strcpy(strdmg[5], "emp");
strcpy(strdmg[6], "spread");
}
int damage_type_map (char *strdmg) {
if (strcmp(strdmg, "pierce") == 0) return 0;
if (strcmp(strdmg, "laser") == 0) return 1;
if (strcmp(strdmg, "impact") == 0) return 2;
if (strcmp(strdmg, "fusion") == 0) return 3;
if (strcmp(strdmg, "explosive") == 0) return 4;
if (strcmp(strdmg, "emp") == 0) return 5;
if (strcmp(strdmg, "spread") == 0) return 6;
printf("info: damage type unknown\n"); return -1;
}
void info_unit_init (info_unit *u) {
strcpy(u->name, "nameless");
u->chassis = -1;
u->battery = -1;
u->brain = -1;
for(int i=0; i<8; u->weapons[i] = -1, u->armor[i] = -1, i++);
for(int i=0; i<16; u->augs[i] = -1, i++);
for(int i=0; i<34; u->levels[i] = 0, i++);
}
float info_unit_get_capacity(infos *info, info_unit *u) {
float sum = 0;
int lc = u->levels[LEVEL_CHASSIS];
for(int i=0; i<16; i++) {
if (u->augs[i] != -1 && info->chassis[u->chassis].slot_aug[lc]) {
int lvl = u->levels[LEVEL_AUGS+i];
sum += info->augs[u->augs[i]].add_capacity[lvl];
}
}
int lvl = u->levels[LEVEL_BATTERY];
sum += info->batteries[u->battery].capacity[lvl];
return sum;
}
float info_unit_get_upkeep(infos *info, info_unit *u) {
float sum = 0;
int lc = u->levels[LEVEL_CHASSIS];
int lbr = u->levels[LEVEL_BRAIN];
for(int i=0; i<8; i++) {
if (u->weapons[i] != -1 && info->chassis[u->chassis].slot_weapon[lc]) {
int lvl = u->levels[LEVEL_WEAPONS+i];
sum += info->weapons[u->weapons[i]].upkeep[lvl];
}
if (u->armor[i] != -1 && info->chassis[u->chassis].slot_armor[lc]) {
int lvl = u->levels[LEVEL_ARMOR+i];
sum += info->armors[u->armor[i]].upkeep[lvl];
}
}
for(int i=0; i<16; i++) {
if (u->augs[i] != -1 && info->chassis[u->chassis].slot_aug[lc]) {
int lvl = u->levels[LEVEL_AUGS+i];
sum += info->augs[u->augs[i]].upkeep[lvl];
}
}
sum += info->chassis[u->chassis].upkeep[lc];
sum += info->brains[u->brain].upkeep[lbr];
return sum;
}
float info_unit_get_weight (infos *info, info_unit *u) {
float sum = 0;
int lc = u->levels[LEVEL_CHASSIS];
if (u->battery != -1) {
int lb = u->levels[LEVEL_BATTERY];
sum += info->batteries[u->battery].weight[lb];
}
for(int i=0; i<8; i++) {
if (u->weapons[i] != -1 && i<info->chassis[u->chassis].slot_weapon[lc]) {
int lvl = u->levels[LEVEL_WEAPONS+i];
sum += info->weapons[u->weapons[i]].weight[lvl];
}
if (u->armor[i] != -1 && i<info->chassis[u->chassis].slot_armor[lc]) {
int lvl = u->levels[LEVEL_ARMOR+i];
sum += info->armors[u->armor[i]].weight[lvl];
}
}
for(int i=0; i<16; i++) {
if (u->augs[i] != -1 && info->chassis[u->chassis].slot_aug) {
int lvl = u->levels[LEVEL_AUGS+i];
sum += info->augs[u->augs[i]].weight[lvl];
}
}
return sum;
}
float info_unit_get_dps (infos *info, info_unit *u) {
int lc = u->levels[LEVEL_CHASSIS];
float sum = 0;
for(int i=0; i<8; i++) {
if (u->weapons[i] != -1 && i<info->chassis[u->chassis].slot_weapon[lc]) {
info_weapon *w = info->weapons+u->weapons[i];
int lw = u->levels[LEVEL_WEAPONS+i];
float damage = info_unit_get_damage(info, u, i);
sum += damage/w->cooldown[lw];
}
}
return sum;
}
float info_unit_get_health(infos *info, info_unit *u) {
int lc = u->levels[LEVEL_CHASSIS];
float sum = 0;
for(int i=0; i<16; i++) {
if (u->augs[i] != -1 && info->chassis[u->chassis].slot_aug[lc]) {
int lvl = u->levels[LEVEL_AUGS+i];
sum += info->augs[u->augs[i]].add_hp[lvl];
}
}
sum += info->chassis[u->chassis].hp[lc];
return sum;
}
float info_unit_get_speed(infos *info, info_unit *u) {
int lc = u->levels[LEVEL_CHASSIS];
float sum = 0;
for(int i=0; i<16; i++) {
if (u->augs[i] != -1 && info->chassis[u->chassis].slot_aug[lc]) {
int lvl = u->levels[LEVEL_AUGS+i];
sum += info->augs[u->augs[i]].add_speed[lvl];
}
}
sum += info->chassis[u->chassis].speed[lc];
return sum;
}
float info_unit_get_damage (infos *info, info_unit *u, int w) {
int lc = u->levels[LEVEL_CHASSIS];
int damage_type = info->weapons[u->weapons[w]].damage_type;
float sum = 0;
for(int i=0; i<16; i++) {
if (u->augs[i] != -1 && info->chassis[u->chassis].slot_aug[lc]) {
int lvl = u->levels[LEVEL_AUGS+i];
sum += info->augs[u->augs[i]].add_damage[damage_type][lvl];
}
}
float mult = (1 + sum/100.0f);
if (mult < 0) mult = 0;
int lw = u->levels[LEVEL_WEAPONS+w];
float dam = info->weapons[u->weapons[w]].damage[lw] * mult;
return dam;
}
float info_unit_get_damage_target (infos *info, info_unit *u, int w,
info_unit *t)
{
// u shoots at t with w, which is lol
int damage_type = info->weapons[u->weapons[w]].damage_type;
float reduction_perc = info_unit_get_armor(info, t, damage_type);
float damage = info_unit_get_damage(info, u, w);
float mult = (1 - reduction_perc / 100.0f);
return damage * mult;
}
float info_unit_get_cooldown(infos *info, info_unit *u, int w) {
int lc = u->levels[LEVEL_CHASSIS];
float sum = 0;
for(int i=0; i<16; i++) {
if (u->augs[i] != -1 && info->chassis[u->chassis].slot_aug[lc]) {
int lvl = u->levels[LEVEL_AUGS+i];
sum += info->augs[u->augs[i]].add_cooldown[lvl];
}
}
int lw = u->levels[LEVEL_WEAPONS+w];
sum += info->weapons[u->weapons[w]].cooldown[lw];
return sum;
}
float info_unit_get_range(infos *info, info_unit *u, int w) {
int lc = u->levels[LEVEL_CHASSIS];
float sum = 0;
for(int i=0; i<16; i++) {
if (u->augs[i] != -1 && info->chassis[u->chassis].slot_aug[lc]) {
int lvl = u->levels[LEVEL_AUGS+i];
sum += info->augs[u->augs[i]].add_range[lvl];
}
}
int lw = u->levels[LEVEL_WEAPONS+w];
sum += info->weapons[u->weapons[w]].range[lw];
return sum;
}
float info_unit_get_charge_per_shot (infos *info, info_unit *u, int w) {
float sum = 0;
int lw = u->levels[LEVEL_WEAPONS+w];
sum += info->weapons[u->weapons[w]].charge_per_shot[lw];
return sum;
}
float info_unit_get_armor(infos *info, info_unit *u, int d) {
int lc = u->levels[LEVEL_CHASSIS];
float sum = 0;
for(int i=0; i<8; i++) {
if (u->armor[i] != -1 && i<info->chassis[u->chassis].slot_armor[lc]) {
int lvl = u->levels[LEVEL_ARMOR+i];
sum += info->armors[u->armor[i]].armor[d][lvl];
}
}
for(int i=0; i<16; i++) {
if (u->augs[i] != -1 && info->chassis[u->chassis].slot_aug[lc]) {
int lvl = u->levels[LEVEL_AUGS+i];
sum += info->augs[u->augs[i]].add_armor[d][lvl];
}
}
return sum;
}
void weapon_init (info_weapon *w) {
strcpy(w->name, "nameless");
for (int l=0; l<MAXLEVEL; l++) {
w->damage_type = 0;
w->weight[l] = 0;
w->cooldown[l] = 1;
w->damage[l] = 0;
w->range[l] = 0;
w->aoe[l] = 0;
w->knockback[l] = 0;
w->stun[l] = 0;
for (int i=0; i<7; i++) { w->reduce_armor[i][l]=0; }
w->upkeep[l] = 0;
w->charge_per_shot[l] = 0;
}
}
void chassis_init (info_chassis *c) {
strcpy(c->name, "nameless");
for (int l=0; l<MAXLEVEL; l++) {
c->slot_weapon[l] = 0;
c->slot_armor[l] = 0;
c->slot_aug[l] = 0;
c->weight_max[l] = 0;
c->hp[l] = 0;
c->upkeep[l] = 0;
}
}
void battery_init (info_battery *b) {
strcpy(b->name, "nameless");
for (int l=0; l<MAXLEVEL; l++) {
b->weight[l] = 0;
b->capacity[l] = 0;
b->recharge[l] = 0;
}
}
void armor_init (info_armor *a) {
strcpy(a->name, "nameless");
for (int l=0; l<MAXLEVEL; l++) {
a->weight[l] = 0;
for (int i=0; i<7; i++) { a->armor[i][l]=0; }
a->upkeep[l] = 0;
}
}
void aug_init (info_aug *a) {
strcpy(a->name, "nameless");
for (int l=0; l<MAXLEVEL; l++) {
a->weight[l] = 0;
for (int i=0; i<7; i++) { a->add_damage[i][l]=0; }
for (int i=0; i<7; i++) { a->add_armor[i][l]=0; }
a->add_range[l] = 0;
a->add_cooldown [l]= 0;
a->add_speed[l] = 0;
a->add_hp[l] = 0;
a->add_capacity[l] = 0;
a->upkeep[l] = 0;
}
}
void brain_init (info_brain *a) {
strcpy(a->name, "nameless");
for (int l=0; l<MAXLEVEL; l++) {
a->upkeep[l] = 0;
}
}
void info_unit_parse (char *json, info_unit *u, jsmntok_t *t, int r,
infos *info)
{
info_unit_init(u);
int obj_i = 0, dict_i = -1;
for (int i=0; i<r; i+=2) {
if (t[i].type == JSMN_STRING) {
char key[32]; substr_token(json, key, t+i);
if (strcmp(key, "name") == 0) {
substr_token(json, u->name, t+i+1);
}
if (strcmp(key, "chassis") == 0) {
char val[32]; substr_token(json, val, t+i+1);
u->chassis = atoi(val);
}
if (strcmp(key, "brain") == 0) {
char val[32]; substr_token(json, val, t+i+1);
u->brain = atoi(val);
}
if (strcmp(key, "battery") == 0) {
char val[32]; substr_token(json, val, t+i+1);
u->battery = atoi(val);
}
if (strcmp(key, "weapons") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, u->weapons, t+i+2, rt-1, 'c');
}
if (strcmp(key, "armor") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, u->armor, t+i+2, rt-1, 'c');
}
if (strcmp(key, "augs") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, u->augs, t+i+2, rt-1, 'c');
}
if (strcmp(key, "levels") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, u->levels, t+i+2, rt-1, 'c');
}
}
}
}
void info_weapon_parse (char *json, info_weapon *w, jsmntok_t *t, int r,
infos *info)
{
weapon_init(w);
int obj_i = 0, dict_i = -1;
for (int i=0; i<r; i+=2) {
if (t[i].type == JSMN_STRING) {
char key[32]; substr_token(json, key, t+i);
if (strcmp(key, "name") == 0) {
substr_token(json, w->name, t+i+1);
}
if (strcmp(key, "damage_type") == 0) {
char val[32]; substr_token(json, val, t+i+1);
w->damage_type = damage_type_map(val);
}
if (strcmp(key, "weight") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->weight, t+i+2, rt-1, 'f');
}
if (strcmp(key, "cooldown") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->cooldown, t+i+2, rt-1, 'f');
}
if (strcmp(key, "damage") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->damage, t+i+2, rt-1, 'f');
}
if (strcmp(key, "range") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->range, t+i+2, rt-1, 'f');
}
if (strcmp(key, "aoe") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->aoe, t+i+2, rt-1, 'f');
}
if (strcmp(key, "knockback") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->knockback, t+i+2, rt-1, 'i');
}
if (strcmp(key, "damage_battery") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->damage_battery, t+i+2, rt-1, 'f');
}
if (strcmp(key, "stun") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->stun, t+i+2, rt-1, 'i');
}
for (int h=0; h<7; h++) {
char strkey[32]; sprintf(strkey, "reduce_%s",
info->damage_types[h]);
if (strcmp(key, strkey) == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->reduce_armor[h], t+i+2, rt-1, 'f');
break;
}
}
if (strcmp(key, "upkeep") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, w->upkeep, t+i+2, rt-1, 'f');
}
}
}
}
void info_chassis_parse (char *json, info_chassis *c, jsmntok_t *t, int r,
infos *info)
{
chassis_init(c);
int obj_i = 0, dict_i = -1;
for (int i=0; i<r; i+=2) {
if (t[i].type == JSMN_STRING) {
char key[32]; substr_token(json, key, t+i);
if (strcmp(key, "name") == 0) {
substr_token(json, c->name, t+i+1);
}
if (strcmp(key, "slot_weapon") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, c->slot_weapon, t+i+2, rt-1, 'i');
}
if (strcmp(key, "slot_armor") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, c->slot_armor, t+i+2, rt-1, 'i');
}
if (strcmp(key, "slot_aug") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, c->slot_aug, t+i+2, rt-1, 'i');
}
if (strcmp(key, "weight_max") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, c->weight_max, t+i+2, rt-1, 'f');
}
if (strcmp(key, "hp") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, c->hp, t+i+2, rt-1, 'f');
}
if (strcmp(key, "speed") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, c->speed, t+i+2, rt-1, 'f');
}
if (strcmp(key, "upkeep") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, c->upkeep, t+i+2, rt-1, 'f');
}
}
}
}
void info_battery_parse (char *json, info_battery *b, jsmntok_t *t, int r,
infos *info)
{
battery_init(b);
int obj_i = 0, dict_i = -1;
for (int i=0; i<r; i+=2) {
if (t[i].type == JSMN_STRING) {
char key[32]; substr_token(json, key, t+i);
if (strcmp(key, "name") == 0) {
substr_token(json, b->name, t+i+1);
}
if (strcmp(key, "weight") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, b->weight, t+i+2, rt-1, 'f');
}
if (strcmp(key, "capacity") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, b->capacity, t+i+2, rt-1, 'f');
}
if (strcmp(key, "recharge") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, b->recharge, t+i+2, rt-1, 'i');
}
}
}
}
void info_armor_parse (char *json, info_armor *a, jsmntok_t *t, int r,
infos *info)
{
armor_init(a);
int obj_i = 0, dict_i = -1;
for (int i=0; i<r; i+=2) {
if (t[i].type == JSMN_STRING) {
char key[32]; substr_token(json, key, t+i);
if (strcmp(key, "name") == 0) {
substr_token(json, a->name, t+i+1);
}
if (strcmp(key, "weight") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->weight, t+i+2, rt-1, 'f');
}
for (int h=0; h<7; h++) {
if (strcmp(key, info->damage_types[h]) == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->armor[h], t+i+2, rt-1, 'f');
}
}
if (strcmp(key, "upkeep") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->upkeep, t+i+2, rt-1, 'f');
}
}
}
}
void info_aug_parse (char *json, info_aug *a, jsmntok_t *t, int r,
infos *info)
{
aug_init(a);
int obj_i = 0, dict_i = -1;
for (int i=0; i<r; i+=2) {
if (t[i].type == JSMN_STRING) {
char key[32]; substr_token(json, key, t+i);
if (strcmp(key, "name") == 0) {
substr_token(json, a->name, t+i+1);
}
if (strcmp(key, "weight") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->weight, t+i+2, rt-1, 'f');
}
for (int h=0; h<7; h++) {
char strkey[32]; sprintf(strkey, "damage_%s",
info->damage_types[h]);
if (strcmp(key, strkey) == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->add_damage[h], t+i+2, rt-1, 'f');
break;
}
}
for (int h=0; h<7; h++) {
char strkey[32]; sprintf(strkey, "armor_%s",
info->damage_types[h]);
if (strcmp(key, strkey) == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->add_armor[h], t+i+2, rt-1, 'f');
break;
}
}
if (strcmp(key, "add_range") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->add_range, t+i+2, rt-1, 'f');
}
if (strcmp(key, "add_cooldown") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->add_cooldown, t+i+2, rt-1, 'f');
}
if (strcmp(key, "add_hp") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->add_hp, t+i+2, rt-1, 'f');
}
if (strcmp(key, "add_capacity") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->add_capacity, t+i+2, rt-1, 'f');
}
if (strcmp(key, "upkeep") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->upkeep, t+i+2, rt-1, 'f');
}
}
}
}
void info_brain_parse (char *json, info_brain *a, jsmntok_t *t, int r,
infos *info)
{
brain_init(a);
int obj_i = 0, dict_i = -1;
for (int i=0; i<r; i+=2) {
if (t[i].type == JSMN_STRING) {
char key[32]; substr_token(json, key, t+i);
if (strcmp(key, "name") == 0) {
substr_token(json, a->name, t+i+1);
}
if (strcmp(key, "upkeep") == 0) {
int rt = json_parse_subtokens(json, t, r, i+1);
json_parse_array(json, a->upkeep, t+i+2, rt-1, 'f');
}
}
}
}
void info_parse_json (infos *info, char *json, char *obj) {
jsmn_parser p; jsmn_init(&p);
jsmntok_t t[MAXTOKENS];
int r = jsmn_parse(&p, json, strlen(json), t, MAXTOKENS);
int index = 0;
for (int i=1; i<r; i++) { // i=1: ignore outer []
if (t[i].type == JSMN_OBJECT) {
int rt = json_parse_subtokens(json, t, r, i);
if (strcmp(obj, "template") == 0) {
info_unit_parse(json,
info->templates+index, t+i+1, rt, info);
info->templateslen = index+1;
}
if (strcmp(obj, "weapon") == 0) {
info_weapon_parse(json,
info->weapons+index, t+i+1, rt, info);
info->weaponslen = index+1;
}
if (strcmp(obj, "chassis") == 0) {
info_chassis_parse(json,
info->chassis+index, t+i+1, rt, info);
info->chassislen = index+1;
}
if (strcmp(obj, "battery") == 0) {
info_battery_parse(json,
info->batteries+index, t+i+1, rt, info);
info->batterieslen = index+1;
}
if (strcmp(obj, "armor") == 0) {
info_armor_parse(json,
info->armors+index, t+i+1, rt, info);
info->armorslen = index+1;
}
if (strcmp(obj, "aug") == 0) {
info_aug_parse(json,
info->augs+index, t+i+1, rt, info);
info->augslen = index+1;
}
if (strcmp(obj, "brain") == 0) {
info_brain_parse(json,
info->brains+index, t+i+1, rt, info);
info->brainslen = index+1;
}
index ++;
i += rt-1;
}
}
}
int info_read_file (char *dst, char *name, int size) {
memset(dst, 0, size*sizeof(char));
FILE *f = fopen(name, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
fread(dst, 1, fsize, f);
fclose(f);
dst[fsize] = '\0';
return fsize;
}
void info_load (infos *info) {
int size = 1024*64;
char json[size];
type_damage_map(info->damage_types);
info->augslen = 0;
info_read_file(json, "content/weapons.txt", size);
info_parse_json(info, json, "weapon");
info_read_file(json, "content/chassis.txt", size);
info_parse_json(info, json, "chassis");
info_read_file(json, "content/batteries.txt", size);
info_parse_json(info, json, "battery");
info_read_file(json, "content/armor.txt", size);
info_parse_json(info, json, "armor");
info_read_file(json, "content/augments.txt", size);
info_parse_json(info, json, "aug");
info_read_file(json, "content/brains.txt", size);
info_parse_json(info, json, "brain");
info_read_file(json, "content/templates/default.txt", size);
info_parse_json(info, json, "template");
/*
for (int i=0; i<info->unitslen; i++) {
printf("name: %s\n", info->units[i].name);
printf(" hp: %f\n", info->units[i].hp);
printf(" damage: %f\n", info->units[i].damage);
printf(" range: %f\n", info->units[i].range);
}*/
/*
for (int i=0; i<info->weaponslen; i++) {
printf("name: %s\n", info->weapons[i].name);
printf(" damage_type: %d\n", info->weapons[i].damage_type);
printf(" weight: %f\n", info->weapons[i].weight);
printf(" cooldown: %f\n", info->weapons[i].cooldown);
printf(" damage: %f\n", info->weapons[i].damage);
printf(" range: %f\n", info->weapons[i].range);
printf(" aoe: %f\n", info->weapons[i].aoe);
printf(" knockback: %d\n", info->weapons[i].knockback);
printf(" damage_battery: %f\n", info->weapons[i].damage_battery);
printf(" stun: %d\n", info->weapons[i].stun);
float sum = 0; for (int j=0; j<7; j++) {
sum += info->weapons[i].reduce_armor[j];
}
if (sum > 0) {
printf(" red pierce: %f\n", info->weapons[i].reduce_armor[0]);
printf(" red laser: %f\n", info->weapons[i].reduce_armor[1]);
printf(" red impact: %f\n", info->weapons[i].reduce_armor[2]);
printf(" red fusion: %f\n", info->weapons[i].reduce_armor[3]);
printf(" red explosive: %f\n", info->weapons[i].reduce_armor[4]);
printf(" red emp: %f\n", info->weapons[i].reduce_armor[5]);
printf(" red spread: %f\n", info->weapons[i].reduce_armor[6]);
}
}*/
/*
for (int i=0; i<info->chassislen; i++) {
printf("name: %s\n", info->chassis[i].name);
printf(" slot_weapon: %d\n", info->chassis[i].slot_weapon);
printf(" slot_armor: %d\n", info->chassis[i].slot_armor);
printf(" slot_aug: %d\n", info->chassis[i].slot_aug);
printf(" weight_max: %f\n", info->chassis[i].weight_max);
printf(" hp: %f\n", info->chassis[i].hp);
}*/
/*
for (int i=0; i<info->batterieslen; i++) {
printf("name: %s\n", info->batteries[i].name);
printf(" weight: %f\n", info->batteries[i].weight);
printf(" capacity: %f\n", info->batteries[i].capacity);
printf(" recharge: %d\n", info->batteries[i].recharge);
}*/
/*
for (int i=0; i<info->armorslen; i++) {
printf("name: %s\n", info->armors[i].name);
printf(" weight: %f\n", info->armors[i].weight);
printf(" pierce: %f\n", info->armors[i].armor[0]);
printf(" laser: %f\n", info->armors[i].armor[1]);
printf(" impact: %f\n", info->armors[i].armor[2]);
printf(" fusion: %f\n", info->armors[i].armor[3]);
printf(" explosive: %f\n", info->armors[i].armor[4]);
printf(" emp: %f\n", info->armors[i].armor[5]);
printf(" spread: %f\n", info->armors[i].armor[6]);
}*/
}
void info_dump_json_templates (infos *info, char *str) {
int len = 0;
sprintf(str+len, "[ \n"); len = strlen(str);
for (int i=0; i<info->templateslen; i++) {
info_unit *u = info->templates+i;
sprintf(str+len, " { \n \"name\": \"%s\",\n", u->name);
len = strlen(str);
sprintf(str+len, " \"chassis\": \"%d\",\n", u->chassis);
len = strlen(str);
sprintf(str+len, " \"brain\": \"%d\",\n", u->brain);
len = strlen(str);
sprintf(str+len, " \"battery\": \"%d\",\n", u->battery);
len = strlen(str);
char w0[64]; json_dump_array(w0, u->weapons, 8, 'c');
sprintf(str+len, " \"weapons\": %s,\n", w0);
len = strlen(str);
char w1[64]; json_dump_array(w1, u->armor, 8, 'c');
sprintf(str+len, " \"armor\": %s,\n", w1);
len = strlen(str);
char w2[64]; json_dump_array(w2, u->augs, 8, 'c');
sprintf(str+len, " \"augs\": %s,\n", w2);
len = strlen(str);
char w3[128]; json_dump_array(w3, u->levels, 34, 'c');
sprintf(str+len, " \"levels\": %s\n", w3);
len = strlen(str);
char comma = ','; if(i==info->templateslen-1) comma = ' ';
sprintf(str+len, " }%c\n", comma);
len = strlen(str);
}
sprintf(str+len, "]"); len = strlen(str);
}
void info_save_templates (infos *info, char *filename) {
char src[1024*64];
info_dump_json_templates(info, src);
char pathname[64]; sprintf(pathname, "content/templates/%s.txt", filename);
FILE *f = fopen(pathname, "wb");
fwrite(src, 1, strlen(src)*sizeof(char), f);
fclose(f);
}
void info_template_add (infos *info, info_unit *temp) {
if (info->templateslen < MAXTEMPLATES) {
info->templates[info->templateslen] = *temp;
info->templateslen++;
}
}
void info_load_army(struct army_ *ar, char *filename) {
char buf[1024*64];
char pathname[64]; sprintf(pathname, "army/%s.txt", filename);
int len = info_read_file(buf, pathname, 1024*64);
memcpy(ar, buf, len-1);
ar->grid = NULL;
army_grid_init(ar);
}
void info_save_army(struct army_ *ar, char *filename) {
char pathname[64]; sprintf(pathname, "army/%s.txt", filename);
FILE *f = fopen(pathname, "wb");
fwrite(ar, 1, sizeof(army), f);
fclose(f);
}
void info_load_playername(char n[]) {
char buf[1024];
int len = info_read_file(buf, "player.txt", 1024);
memcpy(n, buf, sizeof(char)*len);
}
void info_save_playername(char n[]) {
FILE *f = fopen("player.txt", "wb");
fwrite(n, 1, strlen(n)*sizeof(char), f); fclose(f);
}
#include <plibsys.h>
int info_army_get_list (char l[][32]) {
int len = 0;
PDirEntry *entry;
PDir *dir = p_dir_new ("army/", NULL);
while ((entry = p_dir_get_next_entry (dir, NULL)) != NULL) {
if (entry->type == P_DIR_ENTRY_TYPE_FILE) {
strcpy(l[len], entry->name);
l[len][strlen(l[len])-4] = '\0'; // del .txt
len++;
}
}
return len;
}
|