empserver/src/lib/update/prepare.c
Markus Armbruster b6509b7309 budget: Avoid unnecessary work
Ship, plane and land unit repairs depend on and change the state of
the sector.  To predict repairs, we need to predict the state of the
sector before repairs.  The obvious way to do that is to simulate the
sector update and all ship, plane and land unit updates there in the
correct order.

Until recently, we simulated only own sectors, ships, planes and land
units.  Wrong when foreign sectors, ships, planes or land units are
involved.  The fix (commit 70f6964) makes budget simulate all
countries.  Correct, but does much more work than necessary.  With a
little effort, we can track what needs to be simulated.

Use the bp map for tracking.  We need to mark the player's sectors and
all sectors where he has ships, planes or land units.  Do the former
in bp_alloc(), and the latter in prep_ships(), prep_planes(),
prep_lands().

Skip sectors not so marked.  This requires delaying prepare_sects()
until after prep_ships(), prep_planes(), prep_lands().  Their order
doesn't actually matter: prep_ships() & friends only spend money, and
nothing in preparation depends on whether the country is still
solvent.

Skip ships, planes and land units in sectors not so marked.

This speeds up budget by around a third in my testing, more for small
countries.  Roughly 15% slower than before the fix for repairs abroad.
The update has to do a bit more work than before, but the performance
difference is lost in the noise.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
2017-08-06 20:09:06 +02:00

155 lines
4.4 KiB
C

/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure, Markus Armbruster
*
* Empire is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
* See files README, COPYING and CREDITS in the root of the source
* tree for related information and legal notices. It is expected
* that future projects/authors will amend these files as needed.
*
* ---
*
* prepare.c: Perform prelimiary updates of sectors
*
* Known contributors to this file:
* Dave Pare, 1986
* Thomas Ruschak, 1992
* Steve McClure, 1997
* Markus Armbruster, 2016
*/
#include <config.h>
#include "chance.h"
#include "item.h"
#include "nat.h"
#include "optlist.h"
#include "player.h"
#include "prototypes.h"
#include "update.h"
static void tax(struct sctstr *, int);
static void bank_income(struct sctstr *, int);
void
prepare_sects(int etu, struct bp *bp)
{
struct sctstr *sp, scratch_sect;
int n;
/* Process all the fallout. */
if (opt_FALLOUT) {
if (!player->simulation) {
/* First, we determine which sectors to process fallout in */
for (n = 0; NULL != (sp = getsectid(n)); n++)
sp->sct_updated = sp->sct_fallout != 0;
/* Next, we process the fallout there */
for (n = 0; NULL != (sp = getsectid(n)); n++)
if (sp->sct_updated && sp->sct_type != SCT_SANCT)
do_fallout(sp, etu);
/* Next, we spread the fallout */
for (n = 0; NULL != (sp = getsectid(n)); n++)
if (sp->sct_updated)
spread_fallout(sp, etu);
/* Next, we decay the fallout */
for (n = 0; NULL != (sp = getsectid(n)); n++) {
if (sp->sct_fallout)
decay_fallout(sp, etu);
sp->sct_updated = 0;
}
}
}
for (n = 0; NULL != (sp = getsectid(n)); n++) {
if (bp_skip_sect(bp, sp))
continue;
bp_set_from_sect(bp, sp);
if (sp->sct_type == SCT_WATER || sp->sct_type == SCT_SANCT)
continue;
/*
* When running the test suite, reseed PRNG for each sector
* with its UID, to keep results stable even when the number
* of PRNs consumed changes.
*/
if (running_test_suite)
seed_prng(sp->sct_uid);
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);
}
}
static void
tax(struct sctstr *sp, int etu)
{
struct budget *budget = &nat_budget[sp->sct_own];
double civ_tax, uw_tax, mil_pay;
civ_tax = sp->sct_item[I_CIVIL] * etu * money_civ * sp->sct_effic / 100;
if (sp->sct_own == sp->sct_oldown)
budget->oldowned_civs += sp->sct_item[I_CIVIL];
else
civ_tax /= 4; /* captured civs pay less */
budget->civ.count += sp->sct_item[I_CIVIL];
budget->civ.money += civ_tax;
budget->money += civ_tax;
uw_tax = sp->sct_item[I_UW] * etu * money_uw * sp->sct_effic / 100;
budget->uw.count += sp->sct_item[I_UW];
budget->uw.money += uw_tax;
budget->money += uw_tax;
mil_pay = sp->sct_item[I_MILIT] * etu * money_mil;
budget->mil.count += sp->sct_item[I_MILIT];
budget->mil.money += mil_pay;
budget->money += mil_pay;
}
static void
bank_income(struct sctstr *sp, int etu)
{
double income;
income = sp->sct_item[I_BAR] * etu * bankint * sp->sct_effic / 100;
nat_budget[sp->sct_own].bars.count += sp->sct_item[I_BAR];
nat_budget[sp->sct_own].bars.money += income;
nat_budget[sp->sct_own].money += income;
}
void
pay_reserve(struct natstr *np, int etu)
{
double pay = np->nat_reserve * money_res * etu;
nat_budget[np->nat_cnum].mil.money += pay;
nat_budget[np->nat_cnum].money += pay;
}