]> git.pond.sub.org Git - empserver/blob - src/lib/update/prepare.c
budget: Fix treasury tracking
[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 "file.h"
40 #include "item.h"
41 #include "land.h"
42 #include "nat.h"
43 #include "optlist.h"
44 #include "player.h"
45 #include "prototypes.h"
46 #include "ship.h"
47 #include "update.h"
48
49 void
50 prepare_sects(int etu)
51 {
52     struct sctstr *sp;
53     struct natstr *np;
54     int n;
55
56     memset(levels, 0, sizeof(levels));
57
58 /* Process all the fallout. */
59     if (opt_FALLOUT) {
60         if (!player->simulation) {
61             /* First, we determine which sectors to process fallout in */
62             for (n = 0; NULL != (sp = getsectid(n)); n++)
63                 sp->sct_updated = sp->sct_fallout != 0;
64             /* Next, we process the fallout there */
65             for (n = 0; NULL != (sp = getsectid(n)); n++)
66                 if (sp->sct_updated)
67                     do_fallout(sp, etu);
68             /* Next, we spread the fallout */
69             for (n = 0; NULL != (sp = getsectid(n)); n++)
70                 if (sp->sct_updated)
71                     spread_fallout(sp, etu);
72             /* Next, we decay the fallout */
73             for (n = 0; NULL != (sp = getsectid(n)); n++)
74                 if (sp->sct_fallout)
75                     decay_fallout(sp, etu);
76         }
77     }
78     for (n = 0; NULL != (sp = getsectid(n)); n++) {
79         sp->sct_updated = 0;
80
81         if (sp->sct_type == SCT_WATER)
82             continue;
83         if (getnatp(sp->sct_own)->nat_stat == STAT_SANCT)
84             continue;
85
86         /*
87          * When running the test suite, reseed PRNG for each sector
88          * with its UID, to keep results stable even when the number
89          * of PRNs consumed changes.
90          */
91         if (running_test_suite)
92             seed_prng(sp->sct_uid);
93
94         guerrilla(sp);
95         do_plague(sp, etu);
96         populace(sp, etu);
97         tax(sp, etu, &pops[sp->sct_own]);
98         if (sp->sct_type == SCT_BANK)
99             bank_income(sp, etu);
100     }
101     for (n = 0; NULL != (np = getnatp(n)); n++) {
102         upd_slmilcosts(etu, np->nat_cnum);
103         pay_reserve(np, etu);
104     }
105 }
106
107 void
108 tax(struct sctstr *sp, int etu, int *pop)
109 {
110     struct budget *budget = &nat_budget[sp->sct_own];
111     int civ_tax, uw_tax, mil_pay;
112
113     civ_tax = (int)(0.5 + sp->sct_item[I_CIVIL] * sp->sct_effic *
114                     etu * money_civ / 100);
115     /*
116      * captured civs only pay 1/4 taxes
117      */
118     if (sp->sct_own != sp->sct_oldown)
119         civ_tax /= 4;
120     budget->civ.count += sp->sct_item[I_CIVIL];
121     budget->civ.money += civ_tax;
122     budget->money += civ_tax;
123
124     uw_tax = (int)(0.5 + sp->sct_item[I_UW] * sp->sct_effic *
125                    etu * money_uw / 100);
126     budget->uw.count += sp->sct_item[I_UW];
127     budget->uw.money += uw_tax;
128     budget->money += uw_tax;
129
130     mil_pay = sp->sct_item[I_MILIT] * etu * money_mil;
131     budget->mil.count += sp->sct_item[I_MILIT];
132     budget->mil.money += mil_pay;
133     budget->money += mil_pay;
134
135     /*
136      * only non-captured civs add to census for nation
137      */
138     if (sp->sct_oldown == sp->sct_own)
139         *pop += sp->sct_item[I_CIVIL];
140 }
141
142 void
143 upd_slmilcosts(int etu, natid n)
144 {
145     struct shpstr *sp;
146     struct lndstr *lp;
147     int mil, i, mil_pay;
148
149     mil = 0;
150
151     for (i = 0; (sp = getshipp(i)); i++) {
152         if (!sp->shp_own || sp->shp_own != n)
153             continue;
154         mil += sp->shp_item[I_MILIT];
155     }
156
157     for (i = 0; (lp = getlandp(i)); i++) {
158         if (!lp->lnd_own || lp->lnd_own != n)
159             continue;
160         mil += lp->lnd_item[I_MILIT];
161     }
162
163     mil_pay = mil * etu * money_mil;
164     nat_budget[n].mil.count += mil;
165     nat_budget[n].mil.money += mil_pay;
166     nat_budget[n].money += mil_pay;
167 }
168
169 void
170 bank_income(struct sctstr *sp, int etu)
171 {
172     int inc;
173
174     inc = (int)(sp->sct_item[I_BAR] * etu * bankint * sp->sct_effic / 100);
175     nat_budget[sp->sct_own].bars.count += sp->sct_item[I_BAR];
176     nat_budget[sp->sct_own].bars.money += inc;
177     nat_budget[sp->sct_own].money += inc;
178 }
179
180 void
181 pay_reserve(struct natstr *np, int etu)
182 {
183     int pay = (int)(np->nat_reserve * money_res * etu);
184
185     nat_budget[np->nat_cnum].mil.money += pay;
186     nat_budget[np->nat_cnum].money += pay;
187 }