]> git.pond.sub.org Git - empserver/blob - src/lib/update/sect.c
update: Treat sanctuaries more consistently
[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     nat_budget[np->nat_cnum].money -= enlisted * 3;
118 }
119
120 /* Fallout is calculated here. */
121
122 static void
123 meltitems(int etus, int fallout, int own, short *vec,
124           int type, int x, int y, int uid)
125 {
126     i_type n;
127     int melt;
128
129     for (n = I_NONE + 1; n <= I_MAX; n++) {
130         melt = roundavg(vec[n] * etus * (double)fallout
131                         / (1000.0 * ichr[n].i_melt_denom));
132         if (melt > vec[n])
133             melt = vec[n];
134         if (melt > 5 && own) {
135             if (type == EF_SECTOR)
136                 wu(0, own, "Lost %d %s to radiation in %s.\n",
137                    melt, ichr[n].i_name,
138                    xyas(x, y, own));
139             else if (type == EF_LAND)
140                 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
141                    uid, melt, ichr[n].i_name,
142                    xyas(x, y, own));
143             else if (type == EF_SHIP)
144                 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
145                    uid, melt, ichr[n].i_name,
146                    xyas(x, y, own));
147         }
148         vec[n] -= melt;
149     }
150 }
151
152 /*
153  * Do fallout meltdown for sector @sp.
154  * @etus above 24 are treated as 24 to avoid *huge* kill offs in
155  * large ETU games.
156  */
157 void
158 do_fallout(struct sctstr *sp, int etus)
159 {
160     struct shpstr *spp;
161     struct lndstr *lp;
162     int i;
163
164 /* This check shouldn't be needed, but just in case. :) */
165     if (!sp->sct_fallout || !sp->sct_updated)
166         return;
167     if (etus > 24)
168         etus = 24;
169     meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item,
170               EF_SECTOR, sp->sct_x, sp->sct_y, 0);
171     for (i = 0; NULL != (lp = getlandp(i)); i++) {
172         if (!lp->lnd_own)
173             continue;
174         if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
175             continue;
176         meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item,
177                   EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid);
178     }
179     for (i = 0; NULL != (spp = getshipp(i)); i++) {
180         if (!spp->shp_own)
181             continue;
182         if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
183             continue;
184         if (mchr[(int)spp->shp_type].m_flags & M_SUB)
185             continue;
186         meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item,
187                   EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid);
188     }
189 }
190
191 void
192 spread_fallout(struct sctstr *sp, int etus)
193 {
194     struct sctstr *ap;
195     int n;
196     int inc;
197
198     if (etus > 24)
199         etus = 24;
200     for (n = DIR_FIRST; n <= DIR_LAST; n++) {
201         ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
202         if (ap->sct_type == SCT_SANCT)
203             continue;
204         inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
205         if (inc < 0)
206             inc = 0;
207         ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
208     }
209 }
210
211 void
212 decay_fallout(struct sctstr *sp, int etus)
213 {
214     int decay;
215
216     if (etus > 24)
217         etus = 24;
218     decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
219                      (double)etus * (double)sp->sct_fallout);
220
221     sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
222 }
223
224 /*
225  * Produce for a specific nation
226  */
227 void
228 produce_sect(struct natstr *np, int etu, struct bp *bp)
229 {
230     struct budget *budget = &nat_budget[np->nat_cnum];
231     struct sctstr *sp, scratch_sect;
232     int n;
233     double cost;
234
235     for (n = 0; NULL != (sp = getsectid(n)); n++) {
236         if (sp->sct_type == SCT_WATER || sp->sct_type == SCT_SANCT)
237             continue;
238         if (sp->sct_own != np->nat_cnum)
239             continue;
240         if (sp->sct_updated != 0)
241             continue;
242         sp->sct_updated = 1;
243
244         /*
245          * When running the test suite, reseed PRNG for each sector
246          * with its UID, to keep results stable even when the number
247          * of PRNs consumed changes.
248          */
249         if (running_test_suite)
250             seed_prng(sp->sct_uid);
251
252         if (player->simulation) {
253             /* work on a copy, which will be discarded */
254             scratch_sect = *sp;
255             sp = &scratch_sect;
256         }
257
258         do_feed(sp, np, etu, 0);
259
260         if (dchr[sp->sct_type].d_maint) {
261             cost = etu * dchr[sp->sct_type].d_maint;
262             budget->bm[BUDG_SCT_MAINT].count++;
263             budget->bm[BUDG_SCT_MAINT].money -= cost;
264             budget->money -= cost;
265         }
266
267         if (sp->sct_off || budget->money < 0) {
268             sp->sct_avail = 0;
269             bp_set_from_sect(bp, sp);
270             continue;
271         }
272
273         if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
274             budget->money >= 0) {
275             cost = buildeff(sp);
276             budget->bm[BUDG_SCT_BUILD].count++;
277             budget->bm[BUDG_SCT_BUILD].money -= cost;
278             budget->money -= cost;
279         }
280
281         if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
282             sp->sct_own == sp->sct_oldown) {
283             enlist(np, sp->sct_item, etu);
284         }
285
286         /*
287          * now do the production (if sector effic >= 60%)
288          */
289
290         if (sp->sct_effic >= 60) {
291             if (budget->money >= 0 && dchr[sp->sct_type].d_prd >= 0)
292                 produce(np, sp);
293         }
294
295         bp_set_from_sect(bp, sp);
296     }
297 }