]> git.pond.sub.org Git - empserver/blob - src/lib/update/prepare.c
budget: Fix for ship, plane, land unit building abroad
[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 "nat.h"
42 #include "optlist.h"
43 #include "player.h"
44 #include "prototypes.h"
45 #include "update.h"
46
47 static void tax(struct sctstr *, int);
48 static void bank_income(struct sctstr *, int);
49
50 void
51 prepare_sects(int etu, struct bp *bp)
52 {
53     struct sctstr *sp, scratch_sect;
54     int n;
55
56 /* Process all the fallout. */
57     if (opt_FALLOUT) {
58         if (!player->simulation) {
59             /* First, we determine which sectors to process fallout in */
60             for (n = 0; NULL != (sp = getsectid(n)); n++)
61                 sp->sct_updated = sp->sct_fallout != 0;
62             /* Next, we process the fallout there */
63             for (n = 0; NULL != (sp = getsectid(n)); n++)
64                 if (sp->sct_updated && sp->sct_type != SCT_SANCT)
65                     do_fallout(sp, etu);
66             /* Next, we spread the fallout */
67             for (n = 0; NULL != (sp = getsectid(n)); n++)
68                 if (sp->sct_updated)
69                     spread_fallout(sp, etu);
70             /* Next, we decay the fallout */
71             for (n = 0; NULL != (sp = getsectid(n)); n++)
72                 if (sp->sct_fallout)
73                     decay_fallout(sp, etu);
74         }
75     }
76
77     for (n = 0; NULL != (sp = getsectid(n)); n++) {
78         bp_set_from_sect(bp, sp);
79         sp->sct_updated = 0;
80
81         if (sp->sct_type == SCT_WATER || sp->sct_type == SCT_SANCT)
82             continue;
83
84         /*
85          * When running the test suite, reseed PRNG for each sector
86          * with its UID, to keep results stable even when the number
87          * of PRNs consumed changes.
88          */
89         if (running_test_suite)
90             seed_prng(sp->sct_uid);
91
92         if (player->simulation) {
93             /* work on a copy, which will be discarded */
94             scratch_sect = *sp;
95             sp = &scratch_sect;
96         }
97
98         if (!player->simulation) {
99             guerrilla(sp);
100             do_plague(sp, etu);
101             populace(sp, etu);
102         }
103         tax(sp, etu);
104         if (sp->sct_type == SCT_BANK)
105             bank_income(sp, etu);
106         bp_set_from_sect(bp, sp);
107     }
108 }
109
110 static void
111 tax(struct sctstr *sp, int etu)
112 {
113     struct budget *budget = &nat_budget[sp->sct_own];
114     double civ_tax, uw_tax, mil_pay;
115
116     civ_tax = sp->sct_item[I_CIVIL] * etu * money_civ * sp->sct_effic / 100;
117     if (sp->sct_own == sp->sct_oldown)
118         budget->oldowned_civs += sp->sct_item[I_CIVIL];
119     else
120         civ_tax /= 4;           /* captured civs pay less */
121     budget->civ.count += sp->sct_item[I_CIVIL];
122     budget->civ.money += civ_tax;
123     budget->money += civ_tax;
124
125     uw_tax = sp->sct_item[I_UW] * etu * money_uw * sp->sct_effic / 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 static void
137 bank_income(struct sctstr *sp, int etu)
138 {
139     double income;
140
141     income = sp->sct_item[I_BAR] * etu * bankint * sp->sct_effic / 100;
142     nat_budget[sp->sct_own].bars.count += sp->sct_item[I_BAR];
143     nat_budget[sp->sct_own].bars.money += income;
144     nat_budget[sp->sct_own].money += income;
145 }
146
147 void
148 pay_reserve(struct natstr *np, int etu)
149 {
150     double pay = np->nat_reserve * money_res * etu;
151
152     nat_budget[np->nat_cnum].mil.money += pay;
153     nat_budget[np->nat_cnum].money += pay;
154 }