aboutsummaryrefslogtreecommitdiff
path: root/gst/info.c
blob: 0a7222d1397b8693b27236acfb0de79d3db46a1a (plain)
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
#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_cost (infos *info, info_unit *u) {
    // see design/notes.txt:implement cost function
    float sum = 0;
    int lc = u->levels[LEVEL_CHASSIS];
    info_chassis *chassis = info->chassis+u->chassis;
    float sumchassis = 0;
    sumchassis += powf(2, chassis->slot_weapon[lc])*20;
    sumchassis += powf(2, chassis->slot_armor[lc])*10;
    sumchassis += powf(2, chassis->slot_aug[lc])*5;
    sumchassis += chassis->weight_max[lc]/5;
    sumchassis += chassis->hp[lc]/20;
    sumchassis += chassis->speed[lc]*32;
    sum += sumchassis;
    if (u->battery != -1) {
        int lb = u->levels[LEVEL_BATTERY];
        info_battery *battery = info->batteries+u->battery;
        float sumbattery = 0;
        sumbattery += battery->capacity[lb];
        sum += sumbattery;
    }
    for(int i=0; i<8; i++) {
        int lw = u->levels[LEVEL_WEAPONS+i];
        int la = u->levels[LEVEL_ARMOR+i];
        if (u->weapons[i] != -1 && i<chassis->slot_weapon[lc]) {
            info_weapon *weapon = info->weapons+u->weapons[i];
            float sumweap = 0;
            float dam = info_unit_get_damage(info, u, i);
            float cool = info_unit_get_cooldown(info, u, i);
            float damtot = dam / cool;
            float aoe = info_unit_get_aoe(info, u, i);
            float knockback = info_unit_get_knockback(info, u, i);
            float stun = info_unit_get_stun(info, u, i);
            float mult = 1+aoe*5 + 10;
            damtot = damtot*mult;
            damtot += knockback * 20 + stun * 50;
            sumweap = damtot;
            sum += sumweap;
        }
    }
    for(int t=0; t<7; t++) {
        float armortot = info_unit_get_armor(info, u, t) * 3;
        sum += armortot;
    }
    
    return sum;
}
*/

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+=1) { 
        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); 
                i++;
            }
            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');
                i++;
            }
            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');
                i++;
            }
            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');
                i++;
            }
            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');
                i++;
            }
        } 
    }
}

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 ++;
            i += rt-1;
        }
    }
}

#define LOOP(x) for (int z=0; z<x; z++)

// weapon = 0
void stats_weapon_init (stats_weapon *weap) {
    LOOP(7) weap->damage[z] = 0;
    weap->cooldown = 0;
    weap->range = 0;
    weap->aoe = 0;
    weap->knockback = 0;
    weap->damage_battery = 0;
    weap->stun = 0;
    LOOP(7) weap->armor_reduce[z] = 0;
    weap->charge_per_shot = 0;
}

// frame = 0
void stats_frame_init (stats_frame *frame) {
    frame->hp = 0;
    frame->weight = 0;
    frame->weight_max = 0;
    frame->slot_weapon = 0;
    frame->slot_armor = 0;
    frame->slot_aug = 0;
    frame->speed = 0;
    frame->upkeep = 0;
    frame->capacity = 0;
    frame->recharge = 0;
    LOOP(7) frame->armor[z] = 0;
}

// stats = 0
void stats_comp_init (stats_comp *comp) {
    strcpy(comp->name, "-");
    strcpy(comp->description, "-");
    LOOP(MAXLEVEL) {
        stats_frame_init(comp->base +z);
        stats_frame_init(comp->perc +z);
        stats_weapon_init(comp->base_weapon +z);
        stats_weapon_init(comp->perc_weapon +z);
    }
}


#define PRINT_STRUCT(obj, text, attr, type) { \
    if (obj->attr != 0) {\
        sprintf(arr[i], "%s: %"#type, text, obj->attr); \
        i ++;\
    }\
}

void printf_arr (char arr[][64], int n) {
    for (int i=0; i<n; i++) printf("%s\n", arr[i]);
}

int stats_frame_sprintf (infos *info, stats_frame *frame, char arr[][64]) {
    int i = 0;
    PRINT_STRUCT(frame, "hp", hp, .2f);
    PRINT_STRUCT(frame, "weight", weight, .0f);
    PRINT_STRUCT(frame, "weight max", weight_max, .0f);
    PRINT_STRUCT(frame, "weapon slots", slot_weapon, d);
    PRINT_STRUCT(frame, "armor slots", slot_armor, d);
    PRINT_STRUCT(frame, "augment slots", slot_aug, d);
    PRINT_STRUCT(frame, "speed", speed, .2f);
    PRINT_STRUCT(frame, "upkeep", upkeep, .2f);
    PRINT_STRUCT(frame, "capacity", capacity, .0f);
    PRINT_STRUCT(frame, "recharge", recharge, .1f);
    LOOP(7) {
        char str[64]; sprintf(str, "%s armor", info->damage_types[i]);
        PRINT_STRUCT(frame, str, armor[z], f);
    }
    return i;
}

int stats_weapon_sprintf (infos *info, stats_weapon *weap, char arr[][64]) {
    int i = 0;
    LOOP(7)  {
        char str[64]; sprintf(str, "%s damage", info->damage_types[i]);
        PRINT_STRUCT(weap, str, damage[z], .1f);
    }
    PRINT_STRUCT(weap, "cooldown", cooldown, .2f);
    PRINT_STRUCT(weap, "aoe", aoe, .0f);
    PRINT_STRUCT(weap, "range", range, .0f);
    PRINT_STRUCT(weap, "knockback", knockback, .0f);
    PRINT_STRUCT(weap, "drain charge", damage_battery, .1f);
    PRINT_STRUCT(weap, "stun", stun, .0f);
    PRINT_STRUCT(weap, "charge per shot", charge_per_shot, .2f);
    LOOP(7) {
        char str[64]; sprintf(str, "%s armor reduce", info->damage_types[i]);
        PRINT_STRUCT(weap, str, armor_reduce[z], .1f);
    }
    return i;
}

void stats_comp_printf (infos *info, stats_comp *comp) {
    printf("name: %s\n", comp->name);
    printf("description: %s\n", comp->description);
    char arr[32][64];
    LOOP(MAXLEVEL) {
        printf("  level %d ->\n", z);
        { int n = stats_frame_sprintf(info, comp->base +z, arr); 
            printf_arr(arr, n);
        }
        { int n = stats_frame_sprintf(info, comp->perc +z, arr); 
            printf_arr(arr, n); 
        }
        { int n = stats_weapon_sprintf(info, comp->base_weapon +z, arr);
            printf_arr(arr, n); 
        }
        { int n = stats_weapon_sprintf(info, comp->perc_weapon +z, arr);
            printf_arr(arr, n); 
        }
    }
}

void stats_unit_printf (infos *info, stats_unit *u) {
    char arr[32][64];
    { int n = stats_frame_sprintf (info, &u->frame, arr); printf_arr(arr, n); }
    LOOP(8) { 
        int n = stats_weapon_sprintf(info, u->weapon +z, arr); 
        printf_arr(arr, n); 
    }
}


// stats_unit = 0
void stats_unit_init (stats_unit *s) {
    stats_frame_init(&s->frame);
    LOOP(8) stats_weapon_init(&s->weapon[z]);
    s->weaponlen = 0;
}

// frame a += b
void stats_frame_sum (stats_frame *a, stats_frame *b) {
    a->hp += b->hp;
    a->weight += b->weight;
    a->weight_max += b->weight_max;
    a->slot_weapon += b->slot_weapon;
    a->slot_armor += b->slot_armor;
    a->slot_aug += b->slot_aug;
    a->speed += b->speed;
    a->upkeep += b->upkeep;
    a->capacity += b->capacity;
    a->recharge += b->recharge;
    LOOP(7) a->armor[z] += b->armor[z];
}

// weapon a += b
void stats_weapon_sum (stats_weapon *a, stats_weapon *b) {
    LOOP(7) a->damage[z] += b->damage[z];
    a->cooldown += b->cooldown;
    a->range += b->range;
    a->aoe += b->aoe;
    a->knockback += b->knockback;
    a->damage_battery += b->damage_battery;
    a->stun += b->stun;
    LOOP(7) a->armor_reduce[z] += b->armor_reduce[z];
    a->charge_per_shot += b->charge_per_shot;
}

// stats_unit a += b
void stats_unit_sum (stats_unit *a, stats_unit *b) {
    stats_frame_sum(&a->frame, &b->frame);
    LOOP(8) stats_weapon_sum(&a->weapon[z], &b->weapon[z]);
}

// base, perc += comp.base, comp.perc
void stats_unit_comp_sum (stats_unit *base, stats_unit *perc, 
    stats_comp *comp, int lvl) 
{
    stats_frame_sum (&base->frame, &comp->base[lvl]);
    stats_frame_sum (&perc->frame, &comp->perc[lvl]);
    for (int i=0; i<8; i++) {
        stats_weapon_sum (&base->weapon[i], &comp->base_weapon[lvl]);
        stats_weapon_sum (&perc->weapon[i], &comp->perc_weapon[lvl]);
    }
}

#define PERC_NORM(x) x*0.01+1

// frame a *= b
void stats_frame_mul (stats_frame *a, stats_frame *b) {
    a->hp *= PERC_NORM(b->hp);
    a->weight *= PERC_NORM(b->weight);
    a->weight_max *= PERC_NORM(b->weight_max);
    a->slot_weapon *= PERC_NORM(b->slot_weapon);
    a->slot_armor *= PERC_NORM(b->slot_armor);
    a->slot_aug *= PERC_NORM(b->slot_aug);
    a->speed *= PERC_NORM(b->speed);
    a->upkeep *= PERC_NORM(b->upkeep);
    a->capacity *= PERC_NORM(b->capacity);
    a->recharge *= PERC_NORM(b->recharge);
    LOOP(7) a->armor[z] *= PERC_NORM(b->armor[z]);
}

// weapon a *= b
void stats_weapon_mul (stats_weapon *a, stats_weapon *b) {
    LOOP(7) a->damage[z] *= PERC_NORM(b->damage[z]);
    a->cooldown *= PERC_NORM(b->cooldown);
    a->range *= PERC_NORM(b->range);
    a->aoe *= PERC_NORM(b->aoe);
    a->knockback *= PERC_NORM(b->knockback);
    a->damage_battery *= PERC_NORM(b->damage_battery);
    a->stun *= PERC_NORM(b->stun);
    LOOP(7) a->armor_reduce[z] *= PERC_NORM(b->armor_reduce[z]);
    a->charge_per_shot * PERC_NORM(b->charge_per_shot);
}

void stats_unit_apply_perc (stats_unit *base, stats_unit *perc) {
    stats_frame_mul (&base->frame, &perc->frame);
    for (int i=0; i<8; i++) {
        stats_weapon_mul (&base->weapon[i], &perc->weapon[i]);
    }
}

// compute all necessary components stats of u into base
void stats_unit_compute (infos *info, info_unit *u, stats_unit *base) {
    stats_unit_init(base);
    stats_unit perc; stats_unit_init(&perc);    
    {
        stats_comp *comp = &info->stats[STATS_CHASSIS][u->chassis];
        int lvl = u->levels[LEVEL_CHASSIS];
        stats_unit_comp_sum(base, &perc, comp, lvl);
    };
    
    {
        stats_comp *comp = &info->stats[STATS_BRAIN][u->brain];
        int lvl = u->levels[LEVEL_BRAIN];
        stats_unit_comp_sum(base, &perc, comp, lvl);
    };
    
    {
        stats_comp *comp = &info->stats[STATS_BATTERY][u->battery];
        int lvl = u->levels[LEVEL_BATTERY];
        stats_unit_comp_sum(base, &perc, comp, lvl);
    };
    
    for (int i=0; i<8; i++) {
        if (u->armor[i] != -1) {
            stats_comp *comp = &info->stats[STATS_ARMOR][u->armor[i]];
            int lvl = u->levels[LEVEL_ARMOR+i];
            stats_unit_comp_sum(base, &perc, comp, lvl);
        }
    }
    
    for (int i=0; i<16; i++) {
        if (u->augs[i] != -1) {
            stats_comp *comp = &info->stats[STATS_AUGS][u->augs[i]];
            int lvl = u->levels[LEVEL_AUGS+i];
            stats_unit_comp_sum(base, &perc, comp, lvl);
        }
    }
    
    int wn = 0;
    for (int i=0; i<8; i++) {
        if (u->weapons[i] != -1 && i<base->frame.slot_weapon) {
            stats_comp *comp = &info->stats[STATS_WEAPONS][u->weapons[i]];
            int lvl = u->levels[LEVEL_WEAPONS+i];
            stats_frame_sum (&base->frame, &comp->base[lvl]);
            stats_frame_sum (&perc.frame, &comp->perc[lvl]);
            stats_weapon_sum (&base->weapon[wn], &comp->base_weapon[lvl]);
            stats_weapon_sum (&perc.weapon[wn], &comp->perc_weapon[lvl]);
            wn ++;
        }
    }
    base->weaponlen = wn;
    
    stats_unit_apply_perc(base, &perc);
}

float stats_compute_damage (stats_weapon *weapon, stats_frame *frame, 
    float *red) 
{
    /* TODO: red */
    float damage = 0;
    for (int i=0; i<7; i++) {
        damage += weapon->damage[i] * (1- frame->armor[i]*0.01);
    }
    return damage;
}


#define MATCH(frame, attr, type) {\
    if (strcmp(key, #frame"_"#attr) == 0) {\
        int rt = json_parse_subtokens(json, t, r, i+1); \
        if (type == 'f') {\
            float v[3]; json_parse_array(json, v, t+i+2, rt-1, type);\
            LOOP(MAXLEVEL) comp->frame[z].attr = v[z];\
        } if (type == 'i') {\
            int v[3]; json_parse_array(json, v, t+i+2, rt-1, type);\
            LOOP(MAXLEVEL) comp->frame[z].attr = v[z];\
        }\
        i += rt;\
    }\
} 

#define MATCH_ARRAY(frame, attr, attr2, type, num) {\
    char str[64]; sprintf(str, "%s%s", #frame"_"#attr"_", attr2);\
    if (strcmp(key, str) == 0) {\
        int rt = json_parse_subtokens(json, t, r, i+1); \
        float v[3]; json_parse_array(json, v, t+i+2, rt-1, type);\
        LOOP(MAXLEVEL) comp->frame[z].attr[num] = v[z];\
        i += rt;\
    }\
} 

/* REMOVE AFTER THIS */ #include <conio.h>

void stats_comp_parse (char *json, stats_comp *comp, jsmntok_t *t, int r, 
    infos *info) 
{
    stats_comp_init(comp);
    int obj_i = 0, dict_i = -1; 
    for (int i=0; i<r; i+=1) { 
        if (t[i].type == JSMN_STRING) { 
            char key[32]; substr_token(json, key, t+i); 
            if (strcmp(key, "name") == 0) {
                substr_token(json, comp->name, t+i+1); i++;
            }
            if (strcmp(key, "description") == 0) {
                substr_token(json, comp->description, t+i+1); i++;
            }
            
            MATCH(base, hp, 'f');
            MATCH(base, weight, 'f');
            MATCH(base, weight_max, 'f');
            MATCH(base, slot_weapon, 'i');
            MATCH(base, slot_armor, 'i');
            MATCH(base, slot_aug, 'i');
            MATCH(base, speed, 'f');
            MATCH(base, upkeep, 'f');
            MATCH(base, capacity, 'f');
            MATCH(base, recharge, 'f');
            for (int j=0;j<7; j++) {
                MATCH_ARRAY(base, armor, info->damage_types[j], 'f', j);
            }
            
            MATCH(perc, hp, 'f');
            MATCH(perc, weight, 'f');
            MATCH(perc, weight_max, 'f');
            MATCH(perc, slot_weapon, 'i');
            MATCH(perc, slot_armor, 'i');
            MATCH(perc, slot_aug, 'i');
            MATCH(perc, speed, 'f');
            MATCH(perc, upkeep, 'f');
            MATCH(perc, capacity, 'f');
            MATCH(perc, recharge, 'f');
            for (int j=0;j<7; j++) {
                MATCH_ARRAY(perc, armor, info->damage_types[j], 'f', j);
            }
            
            MATCH(base_weapon, cooldown, 'f');
            MATCH(base_weapon, range, 'f');
            MATCH(base_weapon, aoe, 'f');
            MATCH(base_weapon, knockback, 'f');
            MATCH(base_weapon, damage_battery, 'f');
            MATCH(base_weapon, stun, 'f');
            MATCH(base_weapon, charge_per_shot, 'f');
            for (int j=0;j<7; j++) {
                MATCH_ARRAY(base_weapon, damage, 
                    info->damage_types[j], 'f', j);
            }
            for (int j=0;j<7; j++) {
                MATCH_ARRAY(base_weapon, armor_reduce, 
                    info->damage_types[j], 'f', j);
            }
            
            MATCH(perc_weapon, cooldown, 'f');
            MATCH(perc_weapon, range, 'f');
            MATCH(perc_weapon, aoe, 'f');
            MATCH(perc_weapon, knockback, 'f');
            MATCH(perc_weapon, damage_battery, 'f');
            MATCH(perc_weapon, stun, 'f');
            MATCH(perc_weapon, charge_per_shot, 'f');
            for (int j=0;j<7; j++) {
                MATCH_ARRAY(perc_weapon, damage, 
                    info->damage_types[j], 'f', j);
            }
            for (int j=0;j<7; j++) {
                MATCH_ARRAY(perc_weapon, armor_reduce, 
                    info->damage_types[j], 'f', j);
            }
        }
    }
}

void info_stats_parse (infos *info, char *json, int stats_type) { 
    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);
            stats_comp *comp = info->stats[stats_type]
                +info->statslen[stats_type];
            stats_comp_parse(json, comp, t+i+1, rt, info);
            info->statslen[stats_type] ++;
            i += rt-1;
            
            //stats_comp_printf(info, comp);
        }
    } 
}

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_read_file(json, "content/templates/default.txt", size);
    info_parse_json(info, json, "template");
    
    char files[6][32] = { 
        "content/chassis.txt",
        "content/brains.txt", 
        "content/batteries.txt", 
        "content/weapons.txt", 
        "content/armor.txt", 
        "content/augments.txt" 
    };
    for (int i=0; i<6; i++) {
        info->stats[i] = (stats_comp*)malloc(64*sizeof(stats_comp));
        if (info->stats[i] != NULL) {
            info->statslen[i] = 0;
            info_read_file(json, files[i], size);
            info_stats_parse(info, json, i);
            printf("info->statslen[%d]: %d\n", i, info->statslen[i]);
        } else { printf("error: out of memory in allocating for stats"); }
    }
    
    info_unit *u = info->templates+0;
    stats_unit base;
    stats_unit_compute(info, u, &base);
    stats_unit_printf(info, &base);
}


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, 35, '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) {
    strcpy(ar->name, 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) {
    strcpy(ar->name, 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;
}

void info_army_rename (char oldn[], char newn[]) {
    char pathnameold[64]; sprintf(pathnameold, "army/%s.txt", oldn);
    char pathnamenew[64]; sprintf(pathnamenew, "army/%s.txt", newn);
    rename(pathnameold, pathnamenew);
}

void info_army_remove (char name[]) {
    char pathname[64]; sprintf(pathname, "army/%s.txt", name);
    remove(pathname);
}