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