]> git.pond.sub.org Git - empserver/blob - src/lib/update/sect.c
update: Push budget update into produce(), enlist()
[empserver] / src / lib / update / sect.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  *  sect.c: Do production for sectors
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1996
32  *     Markus Armbruster, 2004-2016
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "file.h"
39 #include "item.h"
40 #include "land.h"
41 #include "lost.h"
42 #include "nat.h"
43 #include "optlist.h"
44 #include "path.h"
45 #include "player.h"
46 #include "prototypes.h"
47 #include "ship.h"
48 #include "update.h"
49
50 double
51 buildeff(struct sctstr *sp)
52 {
53     int avail = sp->sct_avail / 2 * 100;
54     double cost;
55     int delta, build;
56     struct dchrstr *dcp;
57
58     cost = 0.0;
59
60     if (sp->sct_type != sp->sct_newtype) {
61         /*
62          * Tear down existing sector.
63          * Easier to destroy than to build.
64          */
65         dcp = &dchr[sp->sct_type];
66         build = 4 * avail / dcp->d_bwork;
67         if (build <= sp->sct_effic)
68             sp->sct_effic -= build;
69         else {
70             build = sp->sct_effic;
71             sp->sct_effic = 0;
72             sp->sct_type = sp->sct_newtype;
73         }
74         avail -= roundavg(build / 4.0 * dcp->d_bwork);
75         cost += build / 4.0;
76     }
77
78     if (sp->sct_type == sp->sct_newtype) {
79         dcp = &dchr[sp->sct_type];
80         delta = avail / dcp->d_bwork;
81         if (delta > 100 - sp->sct_effic)
82             delta = 100 - sp->sct_effic;
83         build = get_materials(sp, dcp->d_mat, delta);
84         sp->sct_effic += build;
85         avail -= build * dcp->d_bwork;
86         cost += build * dcp->d_cost / 100.0;
87     }
88
89     sp->sct_avail = (sp->sct_avail + 1) / 2 + avail / 100;
90     return cost;
91 }
92
93 /*
94  * enlistment sectors are special; they require military
95  * to convert civ into mil in large numbers.
96  * Conversion will happen much more slowly without
97  * some mil initially.
98  */
99 static void
100 enlist(struct natstr *np, short *vec, int etu)
101 {
102     int maxmil;
103     int enlisted;
104
105     enlisted = 0;
106     maxmil = vec[I_CIVIL] / 2 - vec[I_MILIT];
107     if (maxmil > 0) {
108         enlisted = etu * (10 + vec[I_MILIT]) * 0.05;
109         if (enlisted > maxmil)
110             enlisted = maxmil;
111         vec[I_CIVIL] -= enlisted;
112         vec[I_MILIT] += enlisted;
113     }
114
115     nat_budget[np->nat_cnum].prod[SCT_ENLIST].count += enlisted;
116     nat_budget[np->nat_cnum].prod[SCT_ENLIST].money -= enlisted * 3;
117     if (!player->simulation)
118         np->nat_money -= enlisted * 3;
119 }
120
121 /* Fallout is calculated here. */
122
123 static void
124 meltitems(int etus, int fallout, int own, short *vec,
125           int type, int x, int y, int uid)
126 {
127     i_type n;
128     int melt;
129
130     for (n = I_NONE + 1; n <= I_MAX; n++) {
131         melt = roundavg(vec[n] * etus * (double)fallout
132                         / (1000.0 * ichr[n].i_melt_denom));
133         if (melt > vec[n])
134             melt = vec[n];
135         if (melt > 5 && own) {
136             if (type == EF_SECTOR)
137                 wu(0, own, "Lost %d %s to radiation in %s.\n",
138                    melt, ichr[n].i_name,
139                    xyas(x, y, own));
140             else if (type == EF_LAND)
141                 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
142                    uid, melt, ichr[n].i_name,
143                    xyas(x, y, own));
144             else if (type == EF_SHIP)
145                 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
146                    uid, melt, ichr[n].i_name,
147                    xyas(x, y, own));
148         }
149         vec[n] -= melt;
150     }
151 }
152
153 /*
154  * Do fallout meltdown for sector @sp.
155  * @etus above 24 are treated as 24 to avoid *huge* kill offs in
156  * large ETU games.
157  */
158 void
159 do_fallout(struct sctstr *sp, int etus)
160 {
161     struct shpstr *spp;
162     struct lndstr *lp;
163     int i;
164
165 /* This check shouldn't be needed, but just in case. :) */
166     if (!sp->sct_fallout || !sp->sct_updated)
167         return;
168     if (etus > 24)
169         etus = 24;
170     meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item,
171               EF_SECTOR, sp->sct_x, sp->sct_y, 0);
172     for (i = 0; NULL != (lp = getlandp(i)); i++) {
173         if (!lp->lnd_own)
174             continue;
175         if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
176             continue;
177         meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item,
178                   EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid);
179     }
180     for (i = 0; NULL != (spp = getshipp(i)); i++) {
181         if (!spp->shp_own)
182             continue;
183         if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
184             continue;
185         if (mchr[(int)spp->shp_type].m_flags & M_SUB)
186             continue;
187         meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item,
188                   EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid);
189     }
190 }
191
192 void
193 spread_fallout(struct sctstr *sp, int etus)
194 {
195     struct sctstr *ap;
196     int n;
197     int inc;
198
199     if (etus > 24)
200         etus = 24;
201     for (n = DIR_FIRST; n <= DIR_LAST; n++) {
202         ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
203         if (ap->sct_type == SCT_SANCT)
204             continue;
205         inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
206         if (inc < 0)
207             inc = 0;
208         ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
209     }
210 }
211
212 void
213 decay_fallout(struct sctstr *sp, int etus)
214 {
215     int decay;
216
217     if (etus > 24)
218         etus = 24;
219     decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
220                      (double)etus * (double)sp->sct_fallout);
221
222     sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
223 }
224
225 /*
226  * Produce for a specific nation
227  */
228 void
229 produce_sect(struct natstr *np, int etu, struct bp *bp)
230 {
231     struct budget *budget = &nat_budget[np->nat_cnum];
232     struct sctstr *sp, scratch_sect;
233     int cost;
234     int n;
235
236     for (n = 0; NULL != (sp = getsectid(n)); n++) {
237         if (sp->sct_type == SCT_WATER)
238             continue;
239         if (sp->sct_own != np->nat_cnum)
240             continue;
241         if (sp->sct_updated != 0)
242             continue;
243         sp->sct_updated = 1;
244
245         /*
246          * When running the test suite, reseed PRNG for each sector
247          * with its UID, to keep results stable even when the number
248          * of PRNs consumed changes.
249          */
250         if (running_test_suite)
251             seed_prng(sp->sct_uid);
252
253         if (player->simulation) {
254             /* work on a copy, which will be discarded */
255             scratch_sect = *sp;
256             sp = &scratch_sect;
257         }
258
259         do_feed(sp, np, etu, 0);
260
261         if (sp->sct_off || np->nat_money < 0) {
262             sp->sct_avail = 0;
263             bp_set_from_sect(bp, sp);
264             continue;
265         }
266
267         if (dchr[sp->sct_type].d_maint) {
268             cost = etu * dchr[sp->sct_type].d_maint;
269             budget->bm[BUDG_SCT_MAINT].count++;
270             budget->bm[BUDG_SCT_MAINT].money -= cost;
271             if (!player->simulation)
272                 np->nat_money -= cost;
273         }
274
275         if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
276             np->nat_money >= 0) {
277             cost = roundavg(buildeff(sp));
278             budget->bm[BUDG_SCT_BUILD].count++;
279             budget->bm[BUDG_SCT_BUILD].money -= cost;
280             if (!player->simulation)
281                 np->nat_money -= cost;
282         }
283
284         if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
285             sp->sct_own == sp->sct_oldown) {
286             enlist(np, sp->sct_item, etu);
287         }
288
289         /*
290          * now do the production (if sector effic >= 60%)
291          */
292
293         if (sp->sct_effic >= 60) {
294             if (np->nat_money >= 0 && dchr[sp->sct_type].d_prd >= 0)
295                 produce(np, sp);
296         }
297
298         bp_set_from_sect(bp, sp);
299     }
300 }