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