update: Push budget update into produce(), enlist()

produce() and enlist store the cost through a parameter and return the
amount.  Their caller produce_sect() then updates nat_budget[]
accordingly.  Move the nat_budget[] update into the callees.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2016-06-19 09:09:46 +02:00
parent a5314a59c4
commit 058268595f
3 changed files with 29 additions and 32 deletions

View file

@ -130,7 +130,7 @@ extern void upd_slmilcosts(int, natid);
extern void bank_income(struct sctstr *, int);
extern void pay_reserve(struct natstr *, int);
/* produce.c */
extern int produce(struct natstr *, struct sctstr *, int *);
extern void produce(struct natstr *, struct sctstr *);
extern int prod_materials_cost(struct pchrstr *, short[], int *);
extern int prod_resource_limit(struct pchrstr *, unsigned char *);
extern double prod_eff(int, float);

View file

@ -46,9 +46,10 @@ static char *levelnames[] = {
"Technology", "Research", "Education", "Happiness"
};
int
produce(struct natstr *np, struct sctstr *sp, int *cost)
void
produce(struct natstr *np, struct sctstr *sp)
{
struct budget *budget = &nat_budget[sp->sct_own];
struct pchrstr *product;
double p_e;
double prodeff;
@ -61,21 +62,21 @@ produce(struct natstr *np, struct sctstr *sp, int *cost)
int material_limit, res_limit;
int material_consume;
int val;
int cost;
if (dchr[sp->sct_type].d_prd < 0)
return 0;
return;
product = &pchr[dchr[sp->sct_type].d_prd];
item = product->p_type;
if (product->p_nrndx)
resource = (unsigned char *)sp + product->p_nrndx;
else
resource = NULL;
*cost = 0;
material_limit = prod_materials_cost(product, sp->sct_item,
&unit_work);
if (material_limit <= 0)
return 0;
return;
/* sector p.e. */
p_e = sp->sct_effic / 100.0;
@ -95,7 +96,7 @@ produce(struct natstr *np, struct sctstr *sp, int *cost)
if (material_consume > material_limit)
material_consume = material_limit;
if (material_consume == 0)
return 0;
return;
prodeff = prod_eff(sp->sct_type, np->nat_level[product->p_nlndx]);
if (prodeff <= 0.0) {
@ -103,7 +104,7 @@ produce(struct natstr *np, struct sctstr *sp, int *cost)
wu(0, sp->sct_own,
"%s level too low to produce in %s (need %d)\n",
levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
return 0;
return;
}
/*
* Adjust produced amount by commodity production ratio
@ -119,7 +120,7 @@ produce(struct natstr *np, struct sctstr *sp, int *cost)
} else {
actual = roundavg(output);
if (actual <= 0)
return 0;
return;
if (actual > 999) {
actual = 999;
material_consume = roundavg(actual / prodeff);
@ -151,22 +152,26 @@ produce(struct natstr *np, struct sctstr *sp, int *cost)
val = 0;
*resource = val;
}
*cost = product->p_cost * material_consume;
cost = product->p_cost * material_consume;
if (opt_TECH_POP) {
if (product->p_level == NAT_TLEV) {
if (tpops[sp->sct_own] > 50000)
*cost *= tpops[sp->sct_own] / 50000.0;
cost *= tpops[sp->sct_own] / 50000.0;
}
}
budget->prod[sp->sct_type].count += actual;
budget->prod[sp->sct_type].money -= cost;
if (!player->simulation)
np->nat_money -= cost;
if (CANT_HAPPEN(p_e <= 0.0))
return actual;
return;
work_used = roundavg(unit_work * material_consume / p_e);
if (CANT_HAPPEN(work_used > sp->sct_avail))
work_used = sp->sct_avail;
sp->sct_avail -= work_used;
return actual;
}
/*

View file

@ -96,8 +96,8 @@ buildeff(struct sctstr *sp)
* Conversion will happen much more slowly without
* some mil initially.
*/
static int
enlist(short *vec, int etu, int *cost)
static void
enlist(struct natstr *np, short *vec, int etu)
{
int maxmil;
int enlisted;
@ -111,8 +111,11 @@ enlist(short *vec, int etu, int *cost)
vec[I_CIVIL] -= enlisted;
vec[I_MILIT] += enlisted;
}
*cost = enlisted * 3;
return enlisted;
nat_budget[np->nat_cnum].prod[SCT_ENLIST].count += enlisted;
nat_budget[np->nat_cnum].prod[SCT_ENLIST].money -= enlisted * 3;
if (!player->simulation)
np->nat_money -= enlisted * 3;
}
/* Fallout is calculated here. */
@ -227,8 +230,8 @@ produce_sect(struct natstr *np, int etu, struct bp *bp)
{
struct budget *budget = &nat_budget[np->nat_cnum];
struct sctstr *sp, scratch_sect;
int cost, ecost, pcost;
int n, amount;
int cost;
int n;
for (n = 0; NULL != (sp = getsectid(n)); n++) {
if (sp->sct_type == SCT_WATER)
@ -261,9 +264,6 @@ produce_sect(struct natstr *np, int etu, struct bp *bp)
continue;
}
amount = 0;
pcost = cost = ecost = 0;
if (dchr[sp->sct_type].d_maint) {
cost = etu * dchr[sp->sct_type].d_maint;
budget->bm[BUDG_SCT_MAINT].count++;
@ -283,11 +283,7 @@ produce_sect(struct natstr *np, int etu, struct bp *bp)
if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
sp->sct_own == sp->sct_oldown) {
budget->prod[sp->sct_type].count
+= enlist(sp->sct_item, etu, &ecost);
budget->prod[sp->sct_type].money -= ecost;
if (!player->simulation)
np->nat_money -= ecost;
enlist(np, sp->sct_item, etu);
}
/*
@ -296,13 +292,9 @@ produce_sect(struct natstr *np, int etu, struct bp *bp)
if (sp->sct_effic >= 60) {
if (np->nat_money >= 0 && dchr[sp->sct_type].d_prd >= 0)
amount = produce(np, sp, &pcost);
produce(np, sp);
}
bp_set_from_sect(bp, sp);
budget->prod[sp->sct_type].count += amount;
budget->prod[sp->sct_type].money -= pcost;
if (!player->simulation)
np->nat_money -= pcost;
}
}