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