From a988df656b4d37dfa2472a4cde390185cfcea8b5 Mon Sep 17 00:00:00 2001 From: jacopo grandi Date: Tue, 2 Mar 2021 18:26:39 +0100 Subject: test foundation and functional reimplementation of cost function --- gst/fxs.c | 1 + gst/gst.c | 46 +++++++++++-------- gst/gst.h | 3 ++ gst/info.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++------------ gst/info.h | 5 +- gst/units.c | 1 + 6 files changed, 155 insertions(+), 51 deletions(-) (limited to 'gst') diff --git a/gst/fxs.c b/gst/fxs.c index 90f56b2..25ddf18 100644 --- a/gst/fxs.c +++ b/gst/fxs.c @@ -12,6 +12,7 @@ void fx_init (fxs *fx) { } void fx_add_bullet (fxs *fx, bullet *b) { + if (fx->bulletslen >= 1024-1) return; fx->bullets[fx->bulletslen] = *b; fx->bulletslen++; } diff --git a/gst/gst.c b/gst/gst.c index 77ca46d..d3d4ee8 100644 --- a/gst/gst.c +++ b/gst/gst.c @@ -76,6 +76,7 @@ void gst_tobattle (gamestate *gst, infos *info) { gst->ar.us[gst->ar.uslen].owner = i; } } + gst_compute_stats(gst, info); gst_lastpos(gst); gst->starttime = FLT_MAX; @@ -139,7 +140,9 @@ void gst_spawn_bullets (gamestate *gst, fxs *fx, a_dmg dmgs[], int dmgslen, int gst_check_victory (gamestate *gst) { int counts[gst->playernum], max=-1, imax = -1; - for (int i=0; iar.uslen; i++) { + for (int i=0; iar.uslen; i++) { + if (gst->ar.us[i].hp <= 0) continue; + if (gst->ar.us[i].charge <= 0) continue; counts[gst->ar.us[i].owner] ++; if (counts[gst->ar.us[i].owner] > max) { imax = gst->ar.us[i].owner; @@ -149,29 +152,32 @@ int gst_check_victory (gamestate *gst) { return imax; } +void gst_next_turn (gamestate *gst, infos *info, fxs *fx, float t) { + gst_lastpos(gst); + gst->coveredtime += gst->turnspeed; + gst->turn ++; + map *m; army *ar; + gst_get_maparmy(gst, &m, &ar); + int move = army_move(info, ar, m, gst->ustats); + + a_dmg dmgs[1024*8]; + 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; } + if (gst->turn_until_finish <= 0) { + gst->over = 1; + } + gst_spawn_bullets(gst, fx, dmgs, fire, t); +} + void gst_process (gamestate *gst, infos *info, fxs *fx, float t) { if (gst->state == 1) { if (gst->starttime > t) gst->starttime = t; float t_elapsed = t-gst->starttime; - if (t_elapsed > gst->coveredtime) { - // next turn - gst_lastpos(gst); - gst->coveredtime += gst->turnspeed; - gst->turn ++; - map *m; army *ar; - gst_get_maparmy(gst, &m, &ar); - int move = army_move(info, ar, m, gst->ustats); - - a_dmg dmgs[1024*8]; - 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; } - if (gst->turn_until_finish <= 0) { - gst->over = 1; - } - gst_spawn_bullets(gst, fx, dmgs, fire, t); + if (t_elapsed >= gst->coveredtime) { + gst_next_turn(gst, info, fx, t); } } } diff --git a/gst/gst.h b/gst/gst.h index 3178278..c7ba752 100644 --- a/gst/gst.h +++ b/gst/gst.h @@ -39,6 +39,9 @@ void gst_get_maparmy(gamestate *gst, map **m, army **ar); void gst_tobattle (gamestate *gst, infos *info); void gst_toeditor (gamestate *gst); +void gst_next_turn (gamestate *gst, infos *info, fxs *fx, float t); +int gst_check_victory (gamestate *gst); + void gst_process (gamestate *gst, infos *info, fxs *fx, float t); void gst_render (SDL_Renderer *rend, SDL_Texture *txsprites, txtd *textd, gamestate *gst, infos *info, float t); diff --git a/gst/info.c b/gst/info.c index a1ad3b2..74b2d19 100644 --- a/gst/info.c +++ b/gst/info.c @@ -31,7 +31,6 @@ int damage_type_map (char *strdmg) { printf("info: damage type unknown\n"); return -1; } - void info_unit_init (info_unit *u) { strcpy(u->name, "nameless"); u->chassis = -1; @@ -334,43 +333,117 @@ void stats_unit_comp_sum (stats_unit *base, stats_unit *perc, } } -#define PERC_NORM(x) x*0.01+1 +float f_perc_norm (float x) { return 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]); + 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_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) { + 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; +} + +void stats_unit_mul (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]); } } +// map +void stats_frame_map (stats_frame *a, float (*f)(float)) { + a->hp = f(a->hp); + a->weight = f(a->weight); + a->weight_max = f(a->weight_max); + a->slot_weapon = f(a->slot_weapon); + a->slot_armor = f(a->slot_armor); + a->slot_aug = f(a->slot_aug); + a->speed = f(a->speed); + a->upkeep = f(a->upkeep); + a->capacity = f(a->capacity); + a->recharge = f(a->recharge); + LOOP(7) a->armor[z] = f(a->armor[z]); +} + +void stats_weapon_map (stats_weapon *a, float (*f)(float)) { + LOOP(7) a->damage[z] = f(a->damage[z]); + a->cooldown = f(a->cooldown); + a->range = f(a->range); + a->aoe = f(a->aoe); + a->knockback = f(a->knockback); + a->damage_battery = f(a->damage_battery); + a->stun = f(a->stun); + LOOP(7) a->armor_reduce[z] = f(a->armor_reduce[z]); + a->charge_per_shot = f(a->charge_per_shot); +} + +void stats_unit_map (stats_unit *base, float (*f)(float)) { + stats_frame_map (&base->frame, f); + for (int i=0; i<8; i++) { + stats_weapon_map (&base->weapon[i], f); + } +} + +// fold +float stats_frame_fold (stats_frame *frame, float (*f)(float, float)) { + float v = 0; + v = f(v, frame->hp); + v = f(v, frame->weight); + v = f(v, frame->weight_max); + v = f(v, frame->slot_weapon); + v = f(v, frame->slot_armor); + v = f(v, frame->slot_aug); + v = f(v, frame->speed); + v = f(v, frame->upkeep); + v = f(v, frame->capacity); + v = f(v, frame->recharge); + LOOP(7) v = f(v, frame->armor[z]); + return v; +} + +float stats_weapon_fold (stats_weapon *weapon, float (*f)(float, float)) { + float v = 0; + LOOP(7) v = f(v, weapon->damage[z]); + v = f(v, weapon->cooldown); + v = f(v, weapon->range); + v = f(v, weapon->aoe); + v = f(v, weapon->knockback); + v = f(v, weapon->damage_battery); + v = f(v, weapon->stun); + LOOP(7) v = f(v, weapon->armor_reduce[z]); + v = f(v, weapon->charge_per_shot); + return v; +} + +float stats_unit_fold (stats_unit *base, float (*f)(float, float)) { + float v = 0; + v = f(v, stats_frame_fold (&base->frame, f)); + for (int i=0; i<8; i++) { + v = f(v, stats_weapon_fold (&base->weapon[i], f)); + } + return v; +} + // 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); @@ -423,7 +496,8 @@ void stats_unit_compute (infos *info, info_unit *u, stats_unit *base) { } base->weaponlen = wn; - stats_unit_apply_perc(base, &perc); + stats_unit_map(&perc, f_perc_norm); + stats_unit_mul(base, &perc); } float stats_compute_damage (stats_weapon *weapon, stats_frame *frame, @@ -437,6 +511,23 @@ float stats_compute_damage (stats_weapon *weapon, stats_frame *frame, return damage; } +float f_add1 (float x) { return x+1; } + +void cost_weights_init (stats_unit *w) { + stats_unit_init(w); + stats_unit_map(w, f_add1); +} + + +float f_sum (float a, float b) { return a+b; } + +float stats_compute_cost (stats_unit *w, stats_unit *base) { + stats_unit costed = *base; + stats_unit_mul(&costed, w); + float cost = stats_unit_fold(&costed, f_sum); + return cost; +} + #define MATCH(frame, attr, type) {\ if (strcmp(key, #frame"_"#attr) == 0) {\ @@ -462,7 +553,6 @@ float stats_compute_damage (stats_weapon *weapon, stats_frame *frame, }\ } -/* REMOVE AFTER THIS */ #include void stats_comp_parse (char *json, stats_comp *comp, jsmntok_t *t, int r, infos *info) @@ -555,8 +645,6 @@ void info_stats_parse (infos *info, char *json, int stats_type) { stats_comp_parse(json, comp, t+i+1, rt, info); info->statslen[stats_type] ++; i += rt-1; - - //stats_comp_printf(info, comp); } } } @@ -577,6 +665,7 @@ void info_load (infos *info) { int size = 1024*64; char json[size]; type_damage_map(info->damage_types); + cost_weights_init(&info->cost_weights); info->templateslen = 0; info_read_file(json, "content/templates/default.txt", size); @@ -596,14 +685,15 @@ 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(info, &base); + */ } diff --git a/gst/info.h b/gst/info.h index ce4d3fd..e0d784a 100644 --- a/gst/info.h +++ b/gst/info.h @@ -80,6 +80,8 @@ typedef struct { typedef struct { char damage_types[7][32]; + stats_unit cost_weights; + info_unit templates[MAXTEMPLATES]; int templateslen; @@ -89,13 +91,14 @@ typedef struct { void info_unit_init (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); + +float stats_compute_cost (stats_unit *w, stats_unit *base); void info_load (infos *info); diff --git a/gst/units.c b/gst/units.c index 78b28ab..0297266 100644 --- a/gst/units.c +++ b/gst/units.c @@ -17,6 +17,7 @@ void army_grid_init(army *ar) { } void army_init (army *ar, map *m) { + ar->grid = NULL; ar->uslen = 0; ar->sx = m->sx; ar->sy = m->sy; strcpy(ar->name, "-"); -- cgit v1.2.3-54-g00ecf