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