Factor prod_resource_limit() out of produce() and prod()

This commit is contained in:
Markus Armbruster 2013-05-05 17:09:00 +02:00
parent ec60098c36
commit ba2600e7db
3 changed files with 23 additions and 9 deletions

View file

@ -717,6 +717,7 @@ extern int bank_income(struct sctstr *, int);
extern int produce(struct natstr *, struct sctstr *, short *, int, int, extern int produce(struct natstr *, struct sctstr *, short *, int, int,
int, int *, int *); int, int *, int *);
extern int prod_materials_cost(struct pchrstr *, short[], 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); extern double prod_eff(int, float);
/* removewants.c */ /* removewants.c */
extern int update_removewants(void); extern int update_removewants(void);

View file

@ -82,6 +82,7 @@ prod(void)
int there; int there;
int unit_work; /* sum of component amounts */ int unit_work; /* sum of component amounts */
int used; /* production w/infinite workforce */ int used; /* production w/infinite workforce */
int res_limit;
i_type it; i_type it;
i_type vtype; i_type vtype;
unsigned char *resource; unsigned char *resource;
@ -227,10 +228,9 @@ prod(void)
* workforce? * workforce?
*/ */
max = (int)(work * p_e / (double)unit_work + 0.5); max = (int)(work * p_e / (double)unit_work + 0.5);
if (resource && pp->p_nrdep != 0) { res_limit = prod_resource_limit(pp, resource);
if (*resource * 100 < pp->p_nrdep * max) if (max > res_limit)
max = *resource * 100 / pp->p_nrdep; max = res_limit;
}
act = MIN(used, max); act = MIN(used, max);
real = (double)act * prodeff; real = (double)act * prodeff;

View file

@ -57,7 +57,7 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
int unit_work; int unit_work;
i_type item; i_type item;
int worker_limit; int worker_limit;
int material_limit; int material_limit, res_limit;
int material_consume; int material_consume;
int val; 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); worker_limit = roundavg(work * p_e / unit_work);
if (material_consume > worker_limit) if (material_consume > worker_limit)
material_consume = worker_limit; material_consume = worker_limit;
if (resource && product->p_nrdep != 0) { res_limit = prod_resource_limit(product, resource);
if (*resource * 100 < product->p_nrdep * material_consume) if (material_consume > res_limit)
material_consume = *resource * 100 / product->p_nrdep; material_consume = res_limit;
}
if (material_consume == 0) if (material_consume == 0)
return 0; return 0;
prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]); 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. * Return p.e. for sector type TYPE.
* Zero means level is too low for production. * Zero means level is too low for production.