empserver/src/lib/update/prepare.c
Markus Armbruster 5a548c9901 update: Reorder sector production for speed
The update visits sectors in increasing order of country number.
Within a country, it visits in increasing order of sector number,
which is effectively top to bottom, left to right, starting with
absolute 0,0.

The order doesn't actually matter.  Before Chainsaw's option BUDGET,
the update simply visited the sectors in sector number order.  Go back
to that order, because it's faster.  For the update, it's a few
percent in my testing.  For budget, it's more than a third.

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

154 lines
4.3 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 "file.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++) {
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;
}