budget: Track sector expenses in nat_budget[]

The update summarizes sector production, building and maintenance for
budget in a two-dimensional array int p_sect[SCT_BUDG_MAX+1][2].  All
references into this array use literals as second subscript.  Bzzzt,
wrong data type.

Add two one-dimensional arrays to nat_budget[], one for production,
and one for building and maintenance.  p_sect[i] becomes
nat_budget[cnum].prod[i] for production, and .bm[j] for building and
maintenance.  p_sect[i][0] becomes .count, and -p_sect[i][1] becomes
.money.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2016-06-18 15:33:05 +02:00
parent bb495cac60
commit 16f9a393c4
4 changed files with 44 additions and 40 deletions

View file

@ -39,9 +39,11 @@
#define IMPORT 0 #define IMPORT 0
#define EXPORT 1 #define EXPORT 1
#define SCT_EFFIC (SCT_TYPE_MAX + 1) enum {
#define SCT_MAINT (SCT_TYPE_MAX + 2) BUDG_SCT_BUILD,
#define SCT_BUDG_MAX SCT_MAINT BUDG_SCT_MAINT,
BUDG_BLD_MAX = BUDG_SCT_MAINT
};
struct budg_item { struct budg_item {
int money; /* money delta */ int money; /* money delta */
@ -50,6 +52,10 @@ struct budg_item {
/* A nation's budget for an update */ /* A nation's budget for an update */
struct budget { struct budget {
/* production by sector type */
struct budg_item prod[SCT_TYPE_MAX + 1];
/* building and maintenance */
struct budg_item bm[BUDG_BLD_MAX + 1];
/* military payroll */ /* military payroll */
struct budg_item mil; struct budg_item mil;
}; };
@ -135,7 +141,7 @@ extern double buildeff(struct sctstr *);
extern void do_fallout(struct sctstr *, int); extern void do_fallout(struct sctstr *, int);
extern void spread_fallout(struct sctstr *, int); extern void spread_fallout(struct sctstr *, int);
extern void decay_fallout(struct sctstr *, int); extern void decay_fallout(struct sctstr *, int);
extern void produce_sect(struct natstr *, int, struct bp *, int[][2]); extern void produce_sect(struct natstr *, int, struct bp *);
/* ship.c */ /* ship.c */
extern int prod_ship(int, int, struct bp *, int); extern int prod_ship(int, int, struct bp *, int);

View file

@ -42,7 +42,7 @@
#include "product.h" #include "product.h"
#include "update.h" #include "update.h"
static struct budget *calc_all(int (*p_sect)[2], int *taxes, int *Ncivs, static struct budget *calc_all(int *taxes, int *Ncivs,
int *Nuws, int *bars, int *Nbars, int *Nuws, int *bars, int *Nbars,
int *ships, int *sbuild, int *nsbuild, int *smaint, int *ships, int *sbuild, int *nsbuild, int *smaint,
int *units, int *lbuild, int *nlbuild, int *lmaint, int *units, int *lbuild, int *nlbuild, int *lmaint,
@ -53,7 +53,6 @@ int
budg(void) budg(void)
{ {
int i; int i;
int p_sect[SCT_BUDG_MAX+1][2];
int taxes, Ncivs, Nuws, bars, Nbars; int taxes, Ncivs, Nuws, bars, Nbars;
int ships, sbuild, nsbuild, smaint; int ships, sbuild, nsbuild, smaint;
int units, lbuild, nlbuild, lmaint; int units, lbuild, nlbuild, lmaint;
@ -67,8 +66,7 @@ budg(void)
np = getnatp(player->cnum); np = getnatp(player->cnum);
player->simulation = 1; player->simulation = 1;
budget = calc_all(p_sect, budget = calc_all(&taxes, &Ncivs, &Nuws, &bars, &Nbars,
&taxes, &Ncivs, &Nuws, &bars, &Nbars,
&ships, &sbuild, &nsbuild, &smaint, &ships, &sbuild, &nsbuild, &smaint,
&units, &lbuild, &nlbuild, &lmaint, &units, &lbuild, &nlbuild, &lmaint,
&planes, &pbuild, &npbuild, &pmaint); &planes, &pbuild, &npbuild, &pmaint);
@ -78,17 +76,18 @@ budg(void)
expenses = 0; expenses = 0;
pr("Sector Type\t\t\tProduction\t\t\t Cost\n"); pr("Sector Type\t\t\tProduction\t\t\t Cost\n");
for (i = 0; i <= SCT_TYPE_MAX; i++) { for (i = 0; i <= SCT_TYPE_MAX; i++) {
if (!p_sect[i][1]) if (!budget->prod[i].money)
continue; continue;
pr("%-17s\t\t", dchr[i].d_name); pr("%-17s\t\t", dchr[i].d_name);
if (i == SCT_ENLIST) if (i == SCT_ENLIST)
pr("%d mil \t", p_sect[i][0]); pr("%d mil \t", budget->prod[i].count);
else if (dchr[i].d_prd >= 0) else if (dchr[i].d_prd >= 0)
pr("%d %-7s\t", p_sect[i][0], pchr[dchr[i].d_prd].p_sname); pr("%d %-7s\t", budget->prod[i].count,
pchr[dchr[i].d_prd].p_sname);
else else
pr("\t\t"); pr("\t\t");
pr("\t\t%8d\n", p_sect[i][1]); pr("\t\t%8d\n", -budget->prod[i].money);
expenses += p_sect[i][1]; expenses -= budget->prod[i].money;
} }
if (sbuild) { if (sbuild) {
@ -126,19 +125,21 @@ budg(void)
expenses += -lmaint; expenses += -lmaint;
} }
if (p_sect[SCT_EFFIC][1]) { if (budget->bm[BUDG_SCT_BUILD].money) {
sprintf(buf, "%d sector%s", snprintf(buf, sizeof(buf), "%d sector%s",
p_sect[SCT_EFFIC][0], splur(p_sect[SCT_EFFIC][0])); budget->bm[BUDG_SCT_BUILD].count,
splur(budget->bm[BUDG_SCT_BUILD].count));
pr("Sector building\t\t\t%-16s\t\t%8d\n", pr("Sector building\t\t\t%-16s\t\t%8d\n",
buf, p_sect[SCT_EFFIC][1]); buf, -budget->bm[BUDG_SCT_BUILD].money);
expenses += p_sect[SCT_EFFIC][1]; expenses -= budget->bm[BUDG_SCT_BUILD].money;
} }
if (p_sect[SCT_MAINT][0]) { if (budget->bm[BUDG_SCT_MAINT].count) {
sprintf(buf, "%d sector%s", snprintf(buf, sizeof(buf), "%d sector%s",
p_sect[SCT_MAINT][0], splur(p_sect[SCT_MAINT][0])); budget->bm[BUDG_SCT_MAINT].count,
splur(budget->bm[BUDG_SCT_MAINT].count));
pr("Sector maintenance\t\t%-16s\t\t%8d\n", pr("Sector maintenance\t\t%-16s\t\t%8d\n",
buf, p_sect[SCT_MAINT][1]); buf, -budget->bm[BUDG_SCT_MAINT].money);
expenses += p_sect[SCT_MAINT][1]; expenses -= budget->bm[BUDG_SCT_MAINT].money;
} }
if (budget->mil.money) { if (budget->mil.money) {
snprintf(buf, sizeof(buf), "%d mil, %d res", snprintf(buf, sizeof(buf), "%d mil, %d res",
@ -172,8 +173,7 @@ budg(void)
} }
static struct budget * static struct budget *
calc_all(int p_sect[][2], calc_all(int *taxes, int *Ncivs, int *Nuws, int *bars, int *Nbars,
int *taxes, int *Ncivs, int *Nuws, int *bars, int *Nbars,
int *ships, int *sbuild, int *nsbuild, int *smaint, int *ships, int *sbuild, int *nsbuild, int *smaint,
int *units, int *lbuild, int *nlbuild, int *lmaint, int *units, int *lbuild, int *nlbuild, int *lmaint,
int *planes, int *pbuild, int *npbuild, int *pmaint) int *planes, int *pbuild, int *npbuild, int *pmaint)
@ -187,7 +187,6 @@ calc_all(int p_sect[][2],
int etu = etu_per_update; int etu = etu_per_update;
memset(nat_budget, 0, sizeof(nat_budget)); memset(nat_budget, 0, sizeof(nat_budget));
memset(p_sect, 0, sizeof(**p_sect) * (SCT_BUDG_MAX+1) * 2);
*taxes = *Ncivs = *Nuws = *bars = *Nbars = 0; *taxes = *Ncivs = *Nuws = *bars = *Nbars = 0;
*ships = *sbuild = *nsbuild = *smaint = 0; *ships = *sbuild = *nsbuild = *smaint = 0;
*units = *lbuild = *nlbuild = *lmaint = 0; *units = *lbuild = *nlbuild = *lmaint = 0;
@ -229,7 +228,7 @@ calc_all(int p_sect[][2],
*lmaint = lnd_money[player->cnum]; *lmaint = lnd_money[player->cnum];
/* Produce */ /* Produce */
produce_sect(np, etu, bp, p_sect); produce_sect(np, etu, bp);
/* Build ships */ /* Build ships */
sea_money[player->cnum] = 0; sea_money[player->cnum] = 0;

View file

@ -105,9 +105,6 @@ update_main(void)
logerror("done preparing sectors."); logerror("done preparing sectors.");
logerror("producing for countries..."); logerror("producing for countries...");
for (i = 0; i < MAXNOC; i++) { for (i = 0; i < MAXNOC; i++) {
int p_sect[SCT_BUDG_MAX+1][2];
memset(p_sect, 0, sizeof(p_sect));
if (!(np = getnatp(i))) if (!(np = getnatp(i)))
continue; continue;
if (np->nat_stat == STAT_SANCT) { if (np->nat_stat == STAT_SANCT) {
@ -120,7 +117,7 @@ update_main(void)
prod_land(etu, i, NULL, 0); prod_land(etu, i, NULL, 0);
/* produce all sects */ /* produce all sects */
produce_sect(np, etu, NULL, p_sect); produce_sect(np, etu, NULL);
/* build units */ /* build units */
prod_ship(etu, i, NULL, 1); prod_ship(etu, i, NULL, 1);

View file

@ -223,8 +223,9 @@ decay_fallout(struct sctstr *sp, int etus)
* Produce for a specific nation * Produce for a specific nation
*/ */
void void
produce_sect(struct natstr *np, int etu, struct bp *bp, int p_sect[][2]) produce_sect(struct natstr *np, int etu, struct bp *bp)
{ {
struct budget *budget = &nat_budget[np->nat_cnum];
struct sctstr *sp, scratch_sect; struct sctstr *sp, scratch_sect;
int cost, ecost, pcost; int cost, ecost, pcost;
int n, amount; int n, amount;
@ -265,8 +266,8 @@ produce_sect(struct natstr *np, int etu, struct bp *bp, int p_sect[][2])
if (dchr[sp->sct_type].d_maint) { if (dchr[sp->sct_type].d_maint) {
cost = etu * dchr[sp->sct_type].d_maint; cost = etu * dchr[sp->sct_type].d_maint;
p_sect[SCT_MAINT][0]++; budget->bm[BUDG_SCT_MAINT].count++;
p_sect[SCT_MAINT][1] += cost; budget->bm[BUDG_SCT_MAINT].money -= cost;
if (!player->simulation) if (!player->simulation)
np->nat_money -= cost; np->nat_money -= cost;
} }
@ -274,16 +275,17 @@ produce_sect(struct natstr *np, int etu, struct bp *bp, int p_sect[][2])
if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) && if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
np->nat_money >= 0) { np->nat_money >= 0) {
cost = roundavg(buildeff(sp)); cost = roundavg(buildeff(sp));
p_sect[SCT_EFFIC][0]++; budget->bm[BUDG_SCT_BUILD].count++;
p_sect[SCT_EFFIC][1] += cost; budget->bm[BUDG_SCT_BUILD].money -= cost;
if (!player->simulation) if (!player->simulation)
np->nat_money -= cost; np->nat_money -= cost;
} }
if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 && if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
sp->sct_own == sp->sct_oldown) { sp->sct_own == sp->sct_oldown) {
p_sect[sp->sct_type][0] += enlist(sp->sct_item, etu, &ecost); budget->prod[sp->sct_type].count
p_sect[sp->sct_type][1] += ecost; += enlist(sp->sct_item, etu, &ecost);
budget->prod[sp->sct_type].money -= ecost;
if (!player->simulation) if (!player->simulation)
np->nat_money -= ecost; np->nat_money -= ecost;
} }
@ -298,8 +300,8 @@ produce_sect(struct natstr *np, int etu, struct bp *bp, int p_sect[][2])
} }
bp_set_from_sect(bp, sp); bp_set_from_sect(bp, sp);
p_sect[sp->sct_type][0] += amount; budget->prod[sp->sct_type].count += amount;
p_sect[sp->sct_type][1] += pcost; budget->prod[sp->sct_type].money -= pcost;
if (!player->simulation) if (!player->simulation)
np->nat_money -= pcost; np->nat_money -= pcost;
} }