]> git.pond.sub.org Git - empserver/blob - src/lib/update/prepare.c
Include "file.h" where it's needed
[empserver] / src / lib / update / prepare.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  prepare.c: Perform prelimiary updates of sectors
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Thomas Ruschak, 1992
32  *     Steve McClure, 1997
33  *     Markus Armbruster, 2016
34  */
35
36 #include <config.h>
37
38 #include "chance.h"
39 #include "item.h"
40 #include "nat.h"
41 #include "optlist.h"
42 #include "player.h"
43 #include "prototypes.h"
44 #include "update.h"
45
46 static void tax(struct sctstr *, int);
47 static void bank_income(struct sctstr *, int);
48
49 void
50 prepare_sects(int etu, struct bp *bp)
51 {
52     struct sctstr *sp, scratch_sect;
53     int n;
54
55 /* Process all the fallout. */
56     if (opt_FALLOUT) {
57         if (!player->simulation) {
58             /* First, we determine which sectors to process fallout in */
59             for (n = 0; NULL != (sp = getsectid(n)); n++)
60                 sp->sct_updated = sp->sct_fallout != 0;
61             /* Next, we process the fallout there */
62             for (n = 0; NULL != (sp = getsectid(n)); n++)
63                 if (sp->sct_updated && sp->sct_type != SCT_SANCT)
64                     do_fallout(sp, etu);
65             /* Next, we spread the fallout */
66             for (n = 0; NULL != (sp = getsectid(n)); n++)
67                 if (sp->sct_updated)
68                     spread_fallout(sp, etu);
69             /* Next, we decay the fallout */
70             for (n = 0; NULL != (sp = getsectid(n)); n++) {
71                 if (sp->sct_fallout)
72                     decay_fallout(sp, etu);
73                 sp->sct_updated = 0;
74             }
75         }
76     }
77
78     for (n = 0; NULL != (sp = getsectid(n)); n++) {
79         bp_set_from_sect(bp, sp);
80         if (sp->sct_type == SCT_WATER || sp->sct_type == SCT_SANCT)
81             continue;
82
83         /*
84          * When running the test suite, reseed PRNG for each sector
85          * with its UID, to keep results stable even when the number
86          * of PRNs consumed changes.
87          */
88         if (running_test_suite)
89             seed_prng(sp->sct_uid);
90
91         if (player->simulation) {
92             /* work on a copy, which will be discarded */
93             scratch_sect = *sp;
94             sp = &scratch_sect;
95         }
96
97         if (!player->simulation) {
98             guerrilla(sp);
99             do_plague(sp, etu);
100             populace(sp, etu);
101         }
102         tax(sp, etu);
103         if (sp->sct_type == SCT_BANK)
104             bank_income(sp, etu);
105         bp_set_from_sect(bp, sp);
106     }
107 }
108
109 static void
110 tax(struct sctstr *sp, int etu)
111 {
112     struct budget *budget = &nat_budget[sp->sct_own];
113     double civ_tax, uw_tax, mil_pay;
114
115     civ_tax = sp->sct_item[I_CIVIL] * etu * money_civ * sp->sct_effic / 100;
116     if (sp->sct_own == sp->sct_oldown)
117         budget->oldowned_civs += sp->sct_item[I_CIVIL];
118     else
119         civ_tax /= 4;           /* captured civs pay less */
120     budget->civ.count += sp->sct_item[I_CIVIL];
121     budget->civ.money += civ_tax;
122     budget->money += civ_tax;
123
124     uw_tax = sp->sct_item[I_UW] * etu * money_uw * sp->sct_effic / 100;
125     budget->uw.count += sp->sct_item[I_UW];
126     budget->uw.money += uw_tax;
127     budget->money += uw_tax;
128
129     mil_pay = sp->sct_item[I_MILIT] * etu * money_mil;
130     budget->mil.count += sp->sct_item[I_MILIT];
131     budget->mil.money += mil_pay;
132     budget->money += mil_pay;
133 }
134
135 static void
136 bank_income(struct sctstr *sp, int etu)
137 {
138     double income;
139
140     income = sp->sct_item[I_BAR] * etu * bankint * sp->sct_effic / 100;
141     nat_budget[sp->sct_own].bars.count += sp->sct_item[I_BAR];
142     nat_budget[sp->sct_own].bars.money += income;
143     nat_budget[sp->sct_own].money += income;
144 }
145
146 void
147 pay_reserve(struct natstr *np, int etu)
148 {
149     double pay = np->nat_reserve * money_res * etu;
150
151     nat_budget[np->nat_cnum].mil.money += pay;
152     nat_budget[np->nat_cnum].money += pay;
153 }