From e8bc1eab9a45b8893701e3acbd008fe259c53a4c Mon Sep 17 00:00:00 2001 From: jacopo grandi Date: Thu, 25 Feb 2021 16:29:19 +0100 Subject: before was messy, now is MACRO(MACRO(MACRO(MACRO(clean)))) --- gst/gst.c | 21 ++++++--- gst/gst.h | 4 +- gst/info.c | 151 ++++++++++++++++++++++++++++++++++++++++-------------------- gst/info.h | 12 ++++- gst/units.c | 53 +++++++++++++-------- gst/units.h | 13 ++++-- 6 files changed, 170 insertions(+), 84 deletions(-) (limited to 'gst') diff --git a/gst/gst.c b/gst/gst.c index 9ded823..456c1fc 100644 --- a/gst/gst.c +++ b/gst/gst.c @@ -39,7 +39,13 @@ void gst_lastpos (gamestate *gst) { } } -void gst_tobattle (gamestate *gst) { +void gst_compute_stats (gamestate *gst, infos *info) { + for (int i=0; iar.uslen; i++) { + stats_unit_compute(info, &gst->ar.us[i].info, gst->ustats +i); + } +} + +void gst_tobattle (gamestate *gst, infos *info) { if (gst->playernum == 1) { //info_load_army(gst->army_bp+1, "army"); gst->army_bp[1] = gst->army_bp[0]; @@ -70,6 +76,7 @@ void gst_tobattle (gamestate *gst) { gst->ar.us[gst->ar.uslen].owner = i; } } + gst_compute_stats(gst, info); gst_lastpos(gst); gst->starttime = FLT_MAX; gst->turn = 0; @@ -153,11 +160,11 @@ void gst_process (gamestate *gst, infos *info, fxs *fx, float t) { gst->turn ++; map *m; army *ar; gst_get_maparmy(gst, &m, &ar); - int move = army_move(info, ar, m); + int move = army_move(info, ar, m, gst->ustats); a_dmg dmgs[1024*8]; - int fire = army_fire(info, ar, m, dmgs); - army_upkeep(info, ar, m); + int fire = army_fire(info, ar, m, dmgs, gst->ustats); + army_upkeep(info, ar, m, gst->ustats); if (move == 0 && fire == 0) { gst->turn_until_finish--; } else { gst->turn_until_finish = 5; } @@ -194,7 +201,7 @@ void gst_render (SDL_Renderer *rend, SDL_Texture *txsprites, txtd *textd, amt = (amt / gst->turnspeed) + 1; if (amt > 1) amt = 1; if (amt < 0) amt = 0; // clamping away fuzzyness - // render enemies + // render units for (int i=0; iuslen; i++) { if (ar->us[i].hp <= 0) continue; float present_x = ar->us[i].pos[0]; @@ -210,8 +217,8 @@ void gst_render (SDL_Renderer *rend, SDL_Texture *txsprites, txtd *textd, SDL_Rect dstRect = { (int)x-posx, (int)y-posy, ts, ts }; SDL_RenderCopy(rend, txsprites, &srcRect, &dstRect); - float amt = ar->us[i].hp - / info_unit_get_health(info, &ar->us[i].info); + stats_unit base; stats_unit_compute(info, &ar->us[i].info, &base); + float amt = ar->us[i].hp / base.frame.hp; SDL_Rect hprect = { (int)x-posx, (int)y-posy+ts-5, ts*amt, 6 }; diff --git a/gst/gst.h b/gst/gst.h index ad0a685..6d9519e 100644 --- a/gst/gst.h +++ b/gst/gst.h @@ -19,6 +19,8 @@ typedef struct { map map_battle; army ar; float ar_lastpos[MAXUNITS][2]; + stats_unit ustats[MAXUNITS]; + int state; float cam[2]; float starttime; @@ -33,7 +35,7 @@ void gst_init (gamestate *gst); void gst_destroy (gamestate *gst); void gst_get_maparmy(gamestate *gst, map **m, army **ar); -void gst_tobattle (gamestate *gst); +void gst_tobattle (gamestate *gst, infos *info); void gst_toeditor (gamestate *gst); void gst_process (gamestate *gst, infos *info, fxs *fx, float t); diff --git a/gst/info.c b/gst/info.c index eb5738a..721863b 100644 --- a/gst/info.c +++ b/gst/info.c @@ -775,6 +775,7 @@ void info_parse_json (infos *info, char *json, char *obj) { 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; @@ -811,46 +812,83 @@ void stats_comp_init (stats_comp *comp) { } -#define PRINT_STRUCT(obj, attr, type) { \ +#define PRINT_STRUCT(obj, text, attr, type) { \ if (obj->attr != 0) {\ - printf(" "#attr": %"#type"\n", obj->attr); \ + sprintf(arr[i], "%s: %"#type, text, obj->attr); \ + i ++;\ }\ } -void stats_frame_printf (stats_frame *frame) { - PRINT_STRUCT(frame, hp, f); - PRINT_STRUCT(frame, weight, f); - PRINT_STRUCT(frame, weight_max, f); - PRINT_STRUCT(frame, slot_weapon, d); - PRINT_STRUCT(frame, slot_armor, d); - PRINT_STRUCT(frame, slot_aug, d); - PRINT_STRUCT(frame, speed, f); - PRINT_STRUCT(frame, upkeep, f); - PRINT_STRUCT(frame, capacity, f); - PRINT_STRUCT(frame, recharge, f); - LOOP(7) PRINT_STRUCT(frame, armor[z], f); -} - -void stats_weapon_printf (stats_weapon *weap) { - LOOP(7) PRINT_STRUCT(weap, damage[z], f); - PRINT_STRUCT(weap, cooldown, f); - PRINT_STRUCT(weap, aoe, f); - PRINT_STRUCT(weap, knockback, f); - PRINT_STRUCT(weap, damage_battery, f); - PRINT_STRUCT(weap, stun, f); - PRINT_STRUCT(weap, charge_per_shot, f); - LOOP(7) PRINT_STRUCT(weap, armor_reduce[z], f); -} - -void stats_comp_printf (stats_comp *comp) { +void printf_arr (char arr[][64], int n) { + for (int i=0; idamage_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); - stats_frame_printf(comp->base +z); - stats_frame_printf(comp->perc +z); - stats_weapon_printf(comp->base_weapon +z); - stats_weapon_printf(comp->perc_weapon +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); } } @@ -881,6 +919,7 @@ void stats_frame_sum (stats_frame *a, stats_frame *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; @@ -928,6 +967,7 @@ void stats_frame_mul (stats_frame *a, stats_frame *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); @@ -965,19 +1005,6 @@ void stats_unit_compute (infos *info, info_unit *u, stats_unit *base) { stats_unit_comp_sum(base, &perc, comp, lvl); }; - int wn = 0; - for (int i=0; i<8; i++) { - if (u->weapons[i] != -1) { - 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 ++; - } - } - for (int i=0; i<8; i++) { if (u->armor[i] != -1) { stats_comp *comp = &info->stats[STATS_ARMOR][u->armor[i]]; @@ -994,14 +1021,35 @@ void stats_unit_compute (infos *info, info_unit *u, stats_unit *base) { } } + int wn = 0; + for (int i=0; i<8; i++) { + if (u->weapons[i] != -1 && iframe.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); } -void stats_unit_printf (stats_unit *u) { - stats_frame_printf (&u->frame); - LOOP(8) stats_weapon_printf(u->weapon +z); +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); \ @@ -1072,6 +1120,7 @@ void stats_comp_parse (char *json, stats_comp *comp, jsmntok_t *t, int r, } 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'); @@ -1087,6 +1136,7 @@ void stats_comp_parse (char *json, stats_comp *comp, jsmntok_t *t, int r, } 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'); @@ -1118,7 +1168,7 @@ void info_stats_parse (infos *info, char *json, int stats_type) { info->statslen[stats_type] ++; i += rt-1; - //stats_comp_printf(comp); + //stats_comp_printf(info, comp); } } } @@ -1170,13 +1220,14 @@ void info_load (infos *info) { 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(&base); + stats_unit_printf(info, &base); } diff --git a/gst/info.h b/gst/info.h index e37397c..1e22ea6 100644 --- a/gst/info.h +++ b/gst/info.h @@ -24,6 +24,7 @@ typedef struct { float damage[7]; float cooldown; + float range; float aoe; float knockback; float damage_battery; @@ -152,8 +153,8 @@ typedef struct { info_brain brains[32]; int brainslen; - stats_comp *stats[5]; - int statslen[5]; + stats_comp *stats[6]; + int statslen[6]; } infos; void info_unit_init (info_unit *u); @@ -173,6 +174,13 @@ float info_unit_get_maxrange(infos *info, info_unit *u); float info_unit_get_armor(infos *info, info_unit *u, int d); float info_unit_get_cost(infos *info, info_unit *u); +int stats_frame_sprintf (infos *info, stats_frame *frame, char arr[][64]); +int stats_weapon_sprintf (infos *info, stats_weapon *weap, char arr[][64]); +void stats_unit_compute (infos *info, info_unit *u, stats_unit *base); + +float stats_compute_damage (stats_weapon *weapon, stats_frame *frame, + float *red); + void info_load (infos *info); void info_save_templates (infos *info, char *filename); diff --git a/gst/units.c b/gst/units.c index b699e26..785230e 100644 --- a/gst/units.c +++ b/gst/units.c @@ -31,14 +31,16 @@ void army_destory(army *ar) { void unit_init (infos *info, army *ar, map *m, int x, int y, info_unit *iu, int owner, unit *u) { + stats_unit base; stats_unit_compute(info, iu, &base); u->pos[0] = x*m->ts; u->pos[1] = y*m->ts; u->gridpos[0] = x; u->gridpos[1] = y; u->info = *iu; u->owner = owner; - u->hp = info_unit_get_health(info, iu); + u->hp = base.frame.hp; u->move_points = 0; - u->charge = info_unit_get_capacity(info, iu); + u->charge = base.frame.capacity; for (int i=0; i<8; u->cooldown[i] = 1, i++); + for (int i=0; i<7; u->reduced_armor[i] = 0, i++); } void army_spawn (army *ar, map *m, unit u) { @@ -102,7 +104,7 @@ void unit_search (infos *info, army *ar, map *m, unit *u, typedef struct { unit *u; int *dir; int done; } mcom; -int army_move_step (infos *info, army *ar, map *m) { +int army_move_step (infos *info, army *ar, map *m, stats_unit *ustats) { int dirs[4][2] = { {1, 0},{0, 1},{-1,0},{0,-1} }; mcom mcs[ar->uslen]; int mclen = 0; @@ -116,7 +118,11 @@ int army_move_step (infos *info, army *ar, map *m) { if (u->charge <= 0) continue; // search target inside max range unit *t[32]; - float maxrange = info_unit_get_maxrange(info, &u->info); + float maxrange = 0; + for (int w=0; wuslen; i++) { - ar->us[i].move_points += info_unit_get_speed(info, &ar->us[i].info); + ar->us[i].move_points += ustats[i].frame.speed; } - int iter = 0, finished = army_move_step(info, ar, m); + int iter = 0, finished = army_move_step(info, ar, m, ustats); for (; iter<5 && !finished; iter++) { - finished = army_move_step(info, ar, m); + finished = army_move_step(info, ar, m, ustats); } return iter; } -int army_fire (infos *info, army *ar, map *m, a_dmg dmgs[]) { +int army_fire (infos *info, army *ar, map *m, a_dmg dmgs[], + stats_unit *ustats) +{ for (int i=0; iuslen; i++) { unit *u = ar->us+i; int lw = u->info.levels[LEVEL_CHASSIS]; - for (int j=0; jchassis[u->info.chassis].slot_weapon[lw]; j++) { + for (int j=0; jcooldown[j] += 1; } } @@ -199,19 +207,26 @@ int army_fire (infos *info, army *ar, map *m, a_dmg dmgs[]) { if (u->hp <= 0) continue; if (u->charge <= 0) continue; int lw = u->info.levels[LEVEL_CHASSIS]; - for (int j=0; jchassis[u->info.chassis].slot_weapon[lw]; j++) { - if (u->info.weapons[j] == -1) continue; + for (int j=0; jcooldown[j] <= 0) continue; - float range = info_unit_get_range(info, &u->info, j); + float range = ustats[i].weapon[j].range; unit_search(info, ar, m, u, t, range); if (t[0]!=NULL) { dmgs[dmgslen].u = u; dmgs[dmgslen].t = t[0]; - dmgs[dmgslen].dam = info_unit_get_damage_target( - info, &u->info, j, &t[0]->info); + + int t_i = 0; + for (int k=0; kuslen; k++) { + if (ar->us+k == t[0]) { t_i = k; break; } + } + float dam = stats_compute_damage( + &ustats[i].weapon[j], + &ustats[t_i].frame, u->reduced_armor); + + dmgs[dmgslen].dam = dam; dmgslen++; - u->cooldown[j] -= info_unit_get_cooldown(info, &u->info, j); - u->charge -= info_unit_get_charge_per_shot(info, &u->info, j); + u->cooldown[j] -= ustats[i].weapon[j].cooldown; + u->charge -= ustats[i].weapon[j].charge_per_shot; if (u->charge < 0) u->charge = 0; } } @@ -225,12 +240,12 @@ int army_fire (infos *info, army *ar, map *m, a_dmg dmgs[]) { return dmgslen; } -void army_upkeep (infos *info, army *ar, map *m) { +void army_upkeep (infos *info, army *ar, map *m, stats_unit *ustats) { // battery drain or recharge for (int i=0; iuslen; i++) { unit *u = ar->us+i; if (u->hp <= 0) continue; - u->charge -= info_unit_get_upkeep(info, &u->info); + u->charge -= ustats[i].frame.upkeep; if (u->charge < 0) u->charge = 0; } } \ No newline at end of file diff --git a/gst/units.h b/gst/units.h index c7cc575..2b401e9 100644 --- a/gst/units.h +++ b/gst/units.h @@ -15,6 +15,8 @@ typedef struct unit_ { float move_points; float cooldown[8]; float charge; + float reduced_armor[7]; + float stun; } unit; typedef struct army_ { @@ -32,14 +34,15 @@ void unit_init (infos *info, army *ar, map *m, int x, int y, info_unit *iu, int owner, unit *u); void unit_remove (army *ar, map *m, unit *u); -void army_grid_init(army *ar); +void army_grid_init (army *ar); void army_init (army *ar, map *m); -void army_destory(army *ar); +void army_destory (army *ar); void army_spawn (army *ar, map *m, unit u); -int army_move (infos *info, army *ar, map *m); +int army_move (infos *info, army *ar, map *m, stats_unit *ustats); typedef struct { unit *u, *t; float dam; } a_dmg; -int army_fire (infos *info, army *ar, map *m, a_dmg dmgs[]); -void army_upkeep (infos *info, army *ar, map *m); +int army_fire (infos *info, army *ar, map *m, a_dmg dmgs[], + stats_unit *ustats); +void army_upkeep (infos *info, army *ar, map *m, stats_unit *ustats); #endif \ No newline at end of file -- cgit v1.2.3-54-g00ecf