From ba2600e7db0beec65baf12da4d6790b3096664e7 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 5 May 2013 17:09:00 +0200 Subject: [PATCH] Factor prod_resource_limit() out of produce() and prod() --- include/prototypes.h | 1 + src/lib/commands/prod.c | 8 ++++---- src/lib/update/produce.c | 23 ++++++++++++++++++----- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/prototypes.h b/include/prototypes.h index 63d0a8d9..db4ab682 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -717,6 +717,7 @@ extern int bank_income(struct sctstr *, int); extern int produce(struct natstr *, struct sctstr *, short *, int, int, int, int *, int *); extern int prod_materials_cost(struct pchrstr *, short[], int *); +extern int prod_resource_limit(struct pchrstr *, unsigned char *); extern double prod_eff(int, float); /* removewants.c */ extern int update_removewants(void); diff --git a/src/lib/commands/prod.c b/src/lib/commands/prod.c index 504b7fe2..9f7cc8b8 100644 --- a/src/lib/commands/prod.c +++ b/src/lib/commands/prod.c @@ -82,6 +82,7 @@ prod(void) int there; int unit_work; /* sum of component amounts */ int used; /* production w/infinite workforce */ + int res_limit; i_type it; i_type vtype; unsigned char *resource; @@ -227,10 +228,9 @@ prod(void) * workforce? */ max = (int)(work * p_e / (double)unit_work + 0.5); - if (resource && pp->p_nrdep != 0) { - if (*resource * 100 < pp->p_nrdep * max) - max = *resource * 100 / pp->p_nrdep; - } + res_limit = prod_resource_limit(pp, resource); + if (max > res_limit) + max = res_limit; act = MIN(used, max); real = (double)act * prodeff; diff --git a/src/lib/update/produce.c b/src/lib/update/produce.c index e136d271..2085c36e 100644 --- a/src/lib/update/produce.c +++ b/src/lib/update/produce.c @@ -57,7 +57,7 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work, int unit_work; i_type item; int worker_limit; - int material_limit; + int material_limit, res_limit; int material_consume; int val; @@ -93,10 +93,9 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work, worker_limit = roundavg(work * p_e / unit_work); if (material_consume > worker_limit) material_consume = worker_limit; - if (resource && product->p_nrdep != 0) { - if (*resource * 100 < product->p_nrdep * material_consume) - material_consume = *resource * 100 / product->p_nrdep; - } + res_limit = prod_resource_limit(product, resource); + if (material_consume > res_limit) + material_consume = res_limit; if (material_consume == 0) return 0; prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]); @@ -217,6 +216,20 @@ materials_charge(struct pchrstr *pp, short *vec, int count) } } +/* + * Return how much of product PP can be made from its resource. + * If PP depletes a resource, RESOURCE must point to its value. + */ +int +prod_resource_limit(struct pchrstr *pp, unsigned char *resource) +{ + if (CANT_HAPPEN(pp->p_nrndx && !resource)) + return 0; + if (resource && pp->p_nrdep != 0) + return *resource * 100 / pp->p_nrdep; + return ITEM_MAX; +} + /* * Return p.e. for sector type TYPE. * Zero means level is too low for production.