]> git.pond.sub.org Git - empserver/commitdiff
budget: Fix for ship, plane, land unit building abroad
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 10 Jul 2016 19:15:15 +0000 (21:15 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 6 Aug 2017 18:08:30 +0000 (20:08 +0200)
The budget command simulates an update by running selected parts of
the update code.  It skips parts that depend on hidden information
such as guerrilla warfare.  For speed, it also skips parts it doesn't
need, such as distribution and foreign sectors, ships, planes and land
units.

Skipping foreign sectors is wrong when any of the player's ships,
planes or land units will be repaired in foreign sectors, because it
makes budget use old materials and work instead of new.

Skipping foreign ships, planes and land units is wrong when they
compete with the player's for materials and work.

The bug goes back to Chainsaw's option BUDGET.  See the previous
commit for more detailed historical notes.  The update test
demonstrates it in several variations.

Fix it with the sledgehammer: don't skip foreign sectors, ships,
planes and land units.  This makes budget almost twenty times slower
in my testing.  Probably tolerable on a reasonably beefy machine, but
we can do better; the next few commits will claw back most of the lost
performance.

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/prepare.c
src/lib/update/sect.c
tests/update/01-1
tests/update/06-6
tests/update/journal.log

index c7b6161954cff0e75edb1d3d6fd87f976b215b8a..3bda2aca64e2099906d3a3dd827cff2b87c5294e 100644 (file)
@@ -128,9 +128,7 @@ extern void prod_plane(int, int, struct bp *, int);
 extern void populace(struct sctstr *, int);
 extern int total_work(int, int, int, int, int, int);
 /* prepare.c */
-extern void prepare_sects(int);
-extern void tax(struct sctstr *, int);
-extern void bank_income(struct sctstr *, int);
+extern void prepare_sects(int, struct bp *);
 extern void pay_reserve(struct natstr *, int);
 /* produce.c */
 extern void produce(struct natstr *, struct sctstr *);
index d9cecf2c3d35697730e1fbb4534359582a968b75..dfc0fa5bd598563324781173660358bf86092ebf 100644 (file)
@@ -35,7 +35,7 @@
 
 #include <config.h>
 
-#include <ctype.h>
+#include <limits.h>
 #include "commands.h"
 #include "item.h"
 #include "optlist.h"
@@ -145,41 +145,42 @@ calc_all(void)
     struct budget *budget = &nat_budget[player->cnum];
     struct natstr *np;
     struct bp *bp;
-    int n;
-    struct sctstr *sp;
+    int i;
     int etu = etu_per_update;
 
     memset(nat_budget, 0, sizeof(nat_budget));
     np = getnatp(player->cnum);
+    /* Take care not to disclose others going broke: */
+    for (i = 0; i < MAXNOC; i++)
+       nat_budget[i].start_money = nat_budget[i].money = INT_MAX;
     budget->start_money = budget->money = np->nat_money;
     bp = bp_alloc();
 
-    for (n = 0; NULL != (sp = getsectid(n)); n++) {
-       bp_set_from_sect(bp, sp);
-       if (sp->sct_own == player->cnum) {
-           sp->sct_updated = 0;
-           tax(sp, etu);
-           if (sp->sct_type == SCT_BANK)
-               bank_income(sp, etu);
-       }
+    prepare_sects(etu, bp);
+    for (i = 0; i < MAXNOC; i++) {
+       prep_ships(etu, i);
+       prep_planes(etu, i);
+       prep_lands(etu, i);
+       pay_reserve(getnatp(i), etu);
     }
-    prep_ships(etu, player->cnum);
-    prep_planes(etu, player->cnum);
-    prep_lands(etu, player->cnum);
-    pay_reserve(np, etu);
 
     /* Maintain ships, planes and land units */
-    prod_ship(etu, player->cnum, bp, 0);
-    prod_plane(etu, player->cnum, bp, 0);
-    prod_land(etu, player->cnum, bp, 0);
+    for (i = 0; i < MAXNOC; i++) {
+       prod_ship(etu, i, bp, 0);
+       prod_plane(etu, i, bp, 0);
+       prod_land(etu, i, bp, 0);
+    }
 
     /* Produce */
-    produce_sect(np, etu, bp);
+    for (i = 0; i < MAXNOC; i++)
+       produce_sect(getnatp(i), etu, bp);
 
     /* Build ships, planes and land units */
-    prod_ship(etu, player->cnum, bp, 1);
-    prod_plane(etu, player->cnum, bp, 1);
-    prod_land(etu, player->cnum, bp, 1);
+    for (i = 0; i < MAXNOC; i++) {
+       prod_ship(etu, i, bp, 1);
+       prod_plane(etu, i, bp, 1);
+       prod_land(etu, i, bp, 1);
+    }
 
     if (CANT_HAPPEN(np->nat_money != budget->start_money))
        np->nat_money = budget->start_money;
index bf81aad960bad404b8ecac4bfc16f8643173da0d..39cba643d88476c98ff219284f95b9fe745fcb6e 100644 (file)
@@ -92,7 +92,7 @@ update_main(void)
     }
 
     logerror("preparing sectors...");
-    prepare_sects(etu);
+    prepare_sects(etu, NULL);
     logerror("done preparing sectors.");
     for (i = 0; i < MAXNOC; i++) {
        prep_ships(etu, i);
index 23fe66ad37a71a7ba89b3c3d916fda9d9f2461f0..4a1998ea0feea410a5ebfd41118cd1516c5d7777 100644 (file)
 #include "prototypes.h"
 #include "update.h"
 
+static void tax(struct sctstr *, int);
+static void bank_income(struct sctstr *, int);
+
 void
-prepare_sects(int etu)
+prepare_sects(int etu, struct bp *bp)
 {
-    struct sctstr *sp;
+    struct sctstr *sp, scratch_sect;
     int n;
 
 /* Process all the fallout. */
@@ -72,6 +75,7 @@ prepare_sects(int etu)
     }
 
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
+       bp_set_from_sect(bp, sp);
        sp->sct_updated = 0;
 
        if (sp->sct_type == SCT_WATER || sp->sct_type == SCT_SANCT)
@@ -85,16 +89,25 @@ prepare_sects(int etu)
        if (running_test_suite)
            seed_prng(sp->sct_uid);
 
-       guerrilla(sp);
-       do_plague(sp, etu);
-       populace(sp, etu);
+       if (player->simulation) {
+           /* work on a copy, which will be discarded */
+           scratch_sect = *sp;
+           sp = &scratch_sect;
+       }
+
+       if (!player->simulation) {
+           guerrilla(sp);
+           do_plague(sp, etu);
+           populace(sp, etu);
+       }
        tax(sp, etu);
        if (sp->sct_type == SCT_BANK)
            bank_income(sp, etu);
+       bp_set_from_sect(bp, sp);
     }
 }
 
-void
+static void
 tax(struct sctstr *sp, int etu)
 {
     struct budget *budget = &nat_budget[sp->sct_own];
@@ -120,7 +133,7 @@ tax(struct sctstr *sp, int etu)
     budget->money += mil_pay;
 }
 
-void
+static void
 bank_income(struct sctstr *sp, int etu)
 {
     double income;
index 0fed6ca5222a2372eb873aa995c5f75c8b09ce49..a0338b77ff22469abd1b0dce0a5b5169547cf6f8 100644 (file)
@@ -252,6 +252,7 @@ produce_sect(struct natstr *np, int etu, struct bp *bp)
        if (player->simulation) {
            /* work on a copy, which will be discarded */
            scratch_sect = *sp;
+           bp_to_sect(bp, &scratch_sect);
            sp = &scratch_sect;
        }
 
index 55b5898c8012d2c0266cadc0ab4a543156d027d8..6629e5313046eaee942df52fe706d9890e9cea91 100644 (file)
@@ -1,12 +1,5 @@
 budget
 | Note: f1#100 not actually built (plague kills off mil)
-| BUG: missing cs#91
-| BUG: mispredicts f1#75..78
-| BUG: missing f1#91
-| BUG: missing f1#92
-| BUG: f1#100 not actually built
-| BUG: missing inf#91
-| BUG: missing inf#92
 | TODO is it accurate?
 neweff * ?newd#-
 production *
index 9c9185ff6d6f267d5cc2acb9600fb9ff99eae485..6845feb0061d7ab6c594870201359be04b0ed8a4 100644 (file)
@@ -1,3 +1 @@
 budget
-| BUG: missing f1#93
-| BUG: missing inf#93
index 85c538bbcc49db25957029a56fd8095002deea53..50e5ca9e6f833430c21a0e1a01b1955cc71b2902 100644 (file)
     Play#1 output Play#1 1 bank                        41 bars                              410
     Play#1 output Play#1 1 refinery                    2580 petrol                          290
     Play#1 output Play#1 1 enlistment center           75 mil                               225
-    Play#1 output Play#1 1 Ship building                       10 ships                            1520
+    Play#1 output Play#1 1 Ship building                       11 ships                            1920
     Play#1 output Play#1 1 Ship maintenance                    32 ships                            2207
-    Play#1 output Play#1 1 Plane building                      13 planes                           1924
+    Play#1 output Play#1 1 Plane building                      15 planes                           3412
     Play#1 output Play#1 1 Plane maintenance                   21 planes                           1244
-    Play#1 output Play#1 1 Unit building                       6 units                             2300
+    Play#1 output Play#1 1 Unit building                       8 units                             2900
     Play#1 output Play#1 1 Unit maintenance                    16 units                             510
     Play#1 output Play#1 1 Sector building                     40 sectors                           774
     Play#1 output Play#1 1 Sector maintenance                  2 sectors                            120
     Play#1 output Play#1 1 Military payroll            1164 mil, 0 res                     5820
-    Play#1 output Play#1 1 Total expenses.....................................................25249
+    Play#1 output Play#1 1 Total expenses.....................................................27737
     Play#1 output Play#1 1 Income from taxes           26911 civs, 9007 uws              +12627
     Play#1 output Play#1 1 Total income......................................................+12627
     Play#1 output Play#1 1 Balance forward                                                        25000
-    Play#1 output Play#1 1 Estimated delta                                                       -12622
-    Play#1 output Play#1 1 Estimated new treasury.............................................12378
+    Play#1 output Play#1 1 Estimated delta                                                       -15110
+    Play#1 output Play#1 1 Estimated new treasury..............................................9890
     Play#1 output Play#1 6 0 99
     Play#1 input neweff * ?newd#-
     Play#1 command neweff
     Play#6 command budget
     Play#6 output Play#6 1 Sector Type                 Production                          Cost
     Play#6 output Play#6 1 Ship maintenance                    1 ship                               540
+    Play#6 output Play#6 1 Plane building                      1 plane                              120
     Play#6 output Play#6 1 Plane maintenance                   3 planes                             147
+    Play#6 output Play#6 1 Unit building                       1 unit                               150
     Play#6 output Play#6 1 Unit maintenance                    3 units                               90
     Play#6 output Play#6 1 Sector maintenance                  1 sector                              60
     Play#6 output Play#6 1 Military payroll            168 mil, 0 res                       840
-    Play#6 output Play#6 1 Total expenses......................................................1675
+    Play#6 output Play#6 1 Total expenses......................................................1945
     Play#6 output Play#6 1 Income from taxes           4000 civs, 0 uws                   +1749
     Play#6 output Play#6 1 Total income.......................................................+1749
     Play#6 output Play#6 1 Balance forward                                                        25000
-    Play#6 output Play#6 1 Estimated delta                                                          +74
-    Play#6 output Play#6 1 Estimated new treasury.............................................25074
+    Play#6 output Play#6 1 Estimated delta                                                         -196
+    Play#6 output Play#6 1 Estimated new treasury.............................................24804
     Play#6 output Play#6 6 0 99
     Play#6 input ctld
     Play#6 output Play#6 1 Bye-bye