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