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