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