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