aboutsummaryrefslogtreecommitdiff
path: root/gst/generate.c
diff options
context:
space:
mode:
authorjacopo grandi <jak.sk8@hotmail.it>2021-03-05 16:54:26 +0100
committerjacopo grandi <jak.sk8@hotmail.it>2021-03-05 16:54:26 +0100
commitd1af4269c4130d11d77f61f1a2ac8f3e249f72f2 (patch)
tree05618430db0951262bd22e7bbe77b663c3c0346f /gst/generate.c
parent02e55b0647eb5c631e7d7669a13fd0d47ec26c15 (diff)
unit generator in hud, minor fix in movement, % in view and cost weights
Diffstat (limited to 'gst/generate.c')
-rw-r--r--gst/generate.c63
1 files changed, 50 insertions, 13 deletions
diff --git a/gst/generate.c b/gst/generate.c
index 1d8a340..3339ad7 100644
--- a/gst/generate.c
+++ b/gst/generate.c
@@ -10,6 +10,7 @@ void generate_init () {
}
float calc_unit_cost (infos *info, info_unit *u) {
+ if (u->chassis == -1) return 0;
stats_unit base;
stats_unit_compute(info, u, &base);
return stats_compute_cost(&info->cost_weights, &base);
@@ -24,6 +25,35 @@ int accept_cond (infos *info, info_unit *u, float cost_max) {
return 1;
}
+int gen_add_comp (infos *info, stats_unit *base, info_unit *u) {
+ int sel = rand() % 3;
+ if (sel == 0) {
+ for (int i=0; i<base->frame.slot_weapon; i++) {
+ if (u->weapons[i] == -1) {
+ u->weapons[i] = rand() % info->statslen[STATS_WEAPONS];
+ break;
+ }
+ }
+ }
+ else if (sel == 1) {
+ for (int i=0; i<base->frame.slot_armor; i++) {
+ if (u->armor[i] == -1) {
+ u->armor[i] = rand() % info->statslen[STATS_ARMOR];
+ break;
+ }
+ }
+ }
+ else if (sel == 2) {
+ for (int i=0; i<base->frame.slot_aug; i++) {
+ if (u->augs[i] == -1) {
+ u->augs[i] = rand() % info->statslen[STATS_AUGS];
+ break;
+ }
+ }
+ }
+ return sel;
+}
+
// generates randomly a valid unit
void gen_unit_attempt (infos *info, info_unit *u, float cost_max) {
info_unit_init(u);
@@ -32,27 +62,33 @@ void gen_unit_attempt (infos *info, info_unit *u, float cost_max) {
u->battery = rand() % info->statslen[STATS_BATTERY];
u->brain = rand() % info->statslen[STATS_BRAIN];
stats_unit_compute(info, u, &base);
- for (int i=0; i<base.frame.slot_weapon; i++) {
- u->weapons[i] = rand() % info->statslen[STATS_WEAPONS];
- if (!accept_cond(info, u, cost_max)) { u->weapons[i] = -1; return; }
- }
- for (int i=0; i<base.frame.slot_armor; i++) {
- u->armor[i] = rand() % info->statslen[STATS_ARMOR];
- if (!accept_cond(info, u, cost_max)) { u->armor[i] = -1; return; }
- }
- for (int i=0; i<base.frame.slot_aug; i++) {
- u->augs[i] = rand() % info->statslen[STATS_AUGS];
- if (!accept_cond(info, u, cost_max)) { u->augs[i] = -1; return; }
+ for (int i=0; i<8*8*16; i++) {
+ int sel = gen_add_comp(info, &base, u);
+ if (sel == 0) {
+ if (!accept_cond(info, u, cost_max)) {
+ u->weapons[i] = -1; return;
+ }
+ }
+ if (sel == 1) {
+ if (!accept_cond(info, u, cost_max)) {
+ u->armor[i] = -1; return;
+ }
+ }
+ if (sel == 2) {
+ 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 cand;
info_unit candmax; info_unit_init(&candmax);
float cost;
for (int i=0; i<GENERATE_UNIT_MAX_ATTEMPTS; i++) {
- cand = *u;
+ info_unit_init(&cand);
gen_unit_attempt(info, &cand, cost_max);
if (accept_cond(info, &cand, cost_max)) {
if (calc_unit_cost(info, &cand)
@@ -63,6 +99,7 @@ int generate_unit (infos *info, info_unit *u, float cost_max) {
}
}
if (candmax.chassis != -1) {
+ printf("i generated!\n");
*u = candmax;
return 0;
}