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