]> git.pond.sub.org Git - empserver/commitdiff
budget: Track sector expenses in nat_budget[]
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 18 Jun 2016 13:33:05 +0000 (15:33 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 6 Aug 2017 18:00:00 +0000 (20:00 +0200)
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>
include/update.h
src/lib/commands/budg.c
src/lib/update/main.c
src/lib/update/sect.c

index 77a2305d2d2706b7006a3ba6eddb3f9b2dafaeb7..8a2c2a4adc94cefd35b57f01569d880c35e7d735 100644 (file)
 #define IMPORT 0
 #define EXPORT 1
 
-#define SCT_EFFIC (SCT_TYPE_MAX + 1)
-#define SCT_MAINT (SCT_TYPE_MAX + 2)
-#define SCT_BUDG_MAX SCT_MAINT
+enum {
+    BUDG_SCT_BUILD,
+    BUDG_SCT_MAINT,
+    BUDG_BLD_MAX = BUDG_SCT_MAINT
+};
 
 struct budg_item {
     int money;                 /* money delta */
@@ -50,6 +52,10 @@ struct budg_item {
 
 /* A nation's budget for an update */
 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 */
     struct budg_item mil;
 };
@@ -135,7 +141,7 @@ extern double buildeff(struct sctstr *);
 extern void do_fallout(struct sctstr *, int);
 extern void spread_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 */
 extern int prod_ship(int, int, struct bp *, int);
 
index af6957e18338706f813b15f53142c6439ffd7fa4..ddb1bd1bf720ca437cc554f1fcb18edf0a04a01d 100644 (file)
@@ -42,7 +42,7 @@
 #include "product.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 *ships, int *sbuild, int *nsbuild, int *smaint,
                     int *units, int *lbuild, int *nlbuild, int *lmaint,
@@ -53,7 +53,6 @@ int
 budg(void)
 {
     int i;
-    int p_sect[SCT_BUDG_MAX+1][2];
     int taxes, Ncivs, Nuws, bars, Nbars;
     int ships, sbuild, nsbuild, smaint;
     int units, lbuild, nlbuild, lmaint;
@@ -67,8 +66,7 @@ budg(void)
     np = getnatp(player->cnum);
 
     player->simulation = 1;
-    budget = calc_all(p_sect,
-                     &taxes, &Ncivs, &Nuws, &bars, &Nbars,
+    budget = calc_all(&taxes, &Ncivs, &Nuws, &bars, &Nbars,
                      &ships, &sbuild, &nsbuild, &smaint,
                      &units, &lbuild, &nlbuild, &lmaint,
                      &planes, &pbuild, &npbuild, &pmaint);
@@ -78,17 +76,18 @@ budg(void)
     expenses = 0;
     pr("Sector Type\t\t\tProduction\t\t\t    Cost\n");
     for (i = 0; i <= SCT_TYPE_MAX; i++) {
-       if (!p_sect[i][1])
+       if (!budget->prod[i].money)
            continue;
        pr("%-17s\t\t", dchr[i].d_name);
        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)
-           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
            pr("\t\t");
-       pr("\t\t%8d\n", p_sect[i][1]);
-       expenses += p_sect[i][1];
+       pr("\t\t%8d\n", -budget->prod[i].money);
+       expenses -= budget->prod[i].money;
     }
 
     if (sbuild) {
@@ -126,19 +125,21 @@ budg(void)
        expenses += -lmaint;
     }
 
-    if (p_sect[SCT_EFFIC][1]) {
-       sprintf(buf, "%d sector%s",
-               p_sect[SCT_EFFIC][0], splur(p_sect[SCT_EFFIC][0]));
+    if (budget->bm[BUDG_SCT_BUILD].money) {
+       snprintf(buf, sizeof(buf), "%d sector%s",
+                budget->bm[BUDG_SCT_BUILD].count,
+                splur(budget->bm[BUDG_SCT_BUILD].count));
        pr("Sector building\t\t\t%-16s\t\t%8d\n",
-          buf, p_sect[SCT_EFFIC][1]);
-       expenses += p_sect[SCT_EFFIC][1];
+          buf, -budget->bm[BUDG_SCT_BUILD].money);
+       expenses -= budget->bm[BUDG_SCT_BUILD].money;
     }
-    if (p_sect[SCT_MAINT][0]) {
-       sprintf(buf, "%d sector%s",
-               p_sect[SCT_MAINT][0], splur(p_sect[SCT_MAINT][0]));
+    if (budget->bm[BUDG_SCT_MAINT].count) {
+       snprintf(buf, sizeof(buf), "%d sector%s",
+                budget->bm[BUDG_SCT_MAINT].count,
+                splur(budget->bm[BUDG_SCT_MAINT].count));
        pr("Sector maintenance\t\t%-16s\t\t%8d\n",
-          buf, p_sect[SCT_MAINT][1]);
-       expenses += p_sect[SCT_MAINT][1];
+          buf, -budget->bm[BUDG_SCT_MAINT].money);
+       expenses -= budget->bm[BUDG_SCT_MAINT].money;
     }
     if (budget->mil.money) {
        snprintf(buf, sizeof(buf), "%d mil, %d res",
@@ -172,8 +173,7 @@ budg(void)
 }
 
 static struct budget *
-calc_all(int p_sect[][2],
-        int *taxes, int *Ncivs, int *Nuws, int *bars, int *Nbars,
+calc_all(int *taxes, int *Ncivs, int *Nuws, int *bars, int *Nbars,
         int *ships, int *sbuild, int *nsbuild, int *smaint,
         int *units, int *lbuild, int *nlbuild, int *lmaint,
         int *planes, int *pbuild, int *npbuild, int *pmaint)
@@ -187,7 +187,6 @@ calc_all(int p_sect[][2],
     int etu = etu_per_update;
 
     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;
     *ships = *sbuild = *nsbuild = *smaint = 0;
     *units = *lbuild = *nlbuild = *lmaint = 0;
@@ -229,7 +228,7 @@ calc_all(int p_sect[][2],
     *lmaint = lnd_money[player->cnum];
 
     /* Produce */
-    produce_sect(np, etu, bp, p_sect);
+    produce_sect(np, etu, bp);
 
     /* Build ships */
     sea_money[player->cnum] = 0;
index ceff5c0a5c31b205968a257b283acc41c9a874b6..5975f1da183b75a1be7236eed84ddd3f0a0195ef 100644 (file)
@@ -105,9 +105,6 @@ update_main(void)
     logerror("done preparing sectors.");
     logerror("producing for countries...");
     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)))
            continue;
        if (np->nat_stat == STAT_SANCT) {
@@ -120,7 +117,7 @@ update_main(void)
        prod_land(etu, i, NULL, 0);
 
        /* produce all sects */
-       produce_sect(np, etu, NULL, p_sect);
+       produce_sect(np, etu, NULL);
 
        /* build units */
        prod_ship(etu, i, NULL, 1);
index ac0a818e9e736848fbb9b7e70fd24c29c952ba82..e36eb8c2b87287a20e5c7bcce0d5fcb75552363d 100644 (file)
@@ -223,8 +223,9 @@ decay_fallout(struct sctstr *sp, int etus)
  * Produce for a specific nation
  */
 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;
     int cost, ecost, pcost;
     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) {
            cost = etu * dchr[sp->sct_type].d_maint;
-           p_sect[SCT_MAINT][0]++;
-           p_sect[SCT_MAINT][1] += cost;
+           budget->bm[BUDG_SCT_MAINT].count++;
+           budget->bm[BUDG_SCT_MAINT].money -= cost;
            if (!player->simulation)
                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) &&
            np->nat_money >= 0) {
            cost = roundavg(buildeff(sp));
-           p_sect[SCT_EFFIC][0]++;
-           p_sect[SCT_EFFIC][1] += cost;
+           budget->bm[BUDG_SCT_BUILD].count++;
+           budget->bm[BUDG_SCT_BUILD].money -= cost;
            if (!player->simulation)
                np->nat_money -= cost;
        }
 
        if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
            sp->sct_own == sp->sct_oldown) {
-           p_sect[sp->sct_type][0] += enlist(sp->sct_item, etu, &ecost);
-           p_sect[sp->sct_type][1] += ecost;
+           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;
        }
@@ -298,8 +300,8 @@ produce_sect(struct natstr *np, int etu, struct bp *bp, int p_sect[][2])
        }
 
        bp_set_from_sect(bp, sp);
-       p_sect[sp->sct_type][0] += amount;
-       p_sect[sp->sct_type][1] += pcost;
+       budget->prod[sp->sct_type].count += amount;
+       budget->prod[sp->sct_type].money -= pcost;
        if (!player->simulation)
            np->nat_money -= pcost;
     }