From 70f69640e110f325b0c2b186ec10866a21ca6c9e Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 10 Jul 2016 21:15:15 +0200 Subject: [PATCH] budget: Fix for ship, plane, land unit building abroad 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 --- include/update.h | 4 +--- src/lib/commands/budg.c | 45 ++++++++++++++++++++-------------------- src/lib/update/main.c | 2 +- src/lib/update/prepare.c | 27 +++++++++++++++++------- src/lib/update/sect.c | 1 + tests/update/01-1 | 7 ------- tests/update/06-6 | 2 -- tests/update/journal.log | 20 ++++++++++-------- 8 files changed, 57 insertions(+), 51 deletions(-) diff --git a/include/update.h b/include/update.h index c7b61619..3bda2aca 100644 --- a/include/update.h +++ b/include/update.h @@ -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 *); diff --git a/src/lib/commands/budg.c b/src/lib/commands/budg.c index d9cecf2c..dfc0fa5b 100644 --- a/src/lib/commands/budg.c +++ b/src/lib/commands/budg.c @@ -35,7 +35,7 @@ #include -#include +#include #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; diff --git a/src/lib/update/main.c b/src/lib/update/main.c index bf81aad9..39cba643 100644 --- a/src/lib/update/main.c +++ b/src/lib/update/main.c @@ -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); diff --git a/src/lib/update/prepare.c b/src/lib/update/prepare.c index 23fe66ad..4a1998ea 100644 --- a/src/lib/update/prepare.c +++ b/src/lib/update/prepare.c @@ -44,10 +44,13 @@ #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; diff --git a/src/lib/update/sect.c b/src/lib/update/sect.c index 0fed6ca5..a0338b77 100644 --- a/src/lib/update/sect.c +++ b/src/lib/update/sect.c @@ -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; } diff --git a/tests/update/01-1 b/tests/update/01-1 index 55b5898c..6629e531 100644 --- a/tests/update/01-1 +++ b/tests/update/01-1 @@ -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 * diff --git a/tests/update/06-6 b/tests/update/06-6 index 9c9185ff..6845feb0 100644 --- a/tests/update/06-6 +++ b/tests/update/06-6 @@ -1,3 +1 @@ budget -| BUG: missing f1#93 -| BUG: missing inf#93 diff --git a/tests/update/journal.log b/tests/update/journal.log index 85c538bb..50e5ca9e 100644 --- a/tests/update/journal.log +++ b/tests/update/journal.log @@ -33,21 +33,21 @@ 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 @@ -389,16 +389,18 @@ 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