From 02e55b0647eb5c631e7d7669a13fd0d47ec26c15 Mon Sep 17 00:00:00 2001 From: jacopo grandi Date: Thu, 4 Mar 2021 12:40:52 +0100 Subject: morning fixes and bruteforce unit generation --- gst/generate.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 gst/generate.c (limited to 'gst/generate.c') diff --git a/gst/generate.c b/gst/generate.c new file mode 100644 index 0000000..1d8a340 --- /dev/null +++ b/gst/generate.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +#include "generate.h" + +void generate_init () { + srand(time(NULL)); +} + +float calc_unit_cost (infos *info, info_unit *u) { + stats_unit base; + stats_unit_compute(info, u, &base); + return stats_compute_cost(&info->cost_weights, &base); +} + +int accept_cond (infos *info, info_unit *u, float cost_max) { + stats_unit base; + stats_unit_compute(info, u, &base); + float cost = stats_compute_cost(&info->cost_weights, &base); + if (base.frame.weight > base.frame.weight_max) return 0; + if (cost > cost_max) return 0; + return 1; +} + +// generates randomly a valid unit +void gen_unit_attempt (infos *info, info_unit *u, float cost_max) { + info_unit_init(u); + stats_unit base; + u->chassis = rand() % info->statslen[STATS_CHASSIS]; + u->battery = rand() % info->statslen[STATS_BATTERY]; + u->brain = rand() % info->statslen[STATS_BRAIN]; + stats_unit_compute(info, u, &base); + for (int i=0; iweapons[i] = rand() % info->statslen[STATS_WEAPONS]; + if (!accept_cond(info, u, cost_max)) { u->weapons[i] = -1; return; } + } + for (int i=0; iarmor[i] = rand() % info->statslen[STATS_ARMOR]; + if (!accept_cond(info, u, cost_max)) { u->armor[i] = -1; return; } + } + for (int i=0; iaugs[i] = rand() % info->statslen[STATS_AUGS]; + if (!accept_cond(info, u, cost_max)) { u->augs[i] = -1; return; } + } +} + +// selects the max cost generated unit +int generate_unit (infos *info, info_unit *u, float cost_max) { + info_unit cand = *u; + info_unit candmax; info_unit_init(&candmax); + float cost; + for (int i=0; i calc_unit_cost(info, &candmax)) { + candmax = cand; + printf("found: %f\n", calc_unit_cost(info, &cand)); + } + } + } + if (candmax.chassis != -1) { + *u = candmax; + return 0; + } + else return 1; +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf