]> git.pond.sub.org Git - empserver/blob - src/lib/update/sect.c
Cleanup #includes of (mostly a long time) unused header files.
[empserver] / src / lib / update / sect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
36 #include "sect.h"
37 #include "nat.h"
38 #include "item.h"
39 #include "file.h"
40 #include "xy.h"
41 #include "path.h"
42 #include "product.h"
43 #include "optlist.h"
44 #include "budg.h"
45 #include "player.h"
46 #include "land.h"
47 #include "ship.h"
48 #include "update.h"
49 #include "subs.h"
50 #include "common.h"
51 #include "lost.h"
52 #include "gen.h"
53
54 /*
55  * Increase sector efficiency if old type == new type.
56  * decrease sector efficiency if old type != new type.
57  * Return amount of work used.
58  */
59 static int
60 upd_buildeff(struct natstr *np, struct sctstr *sp, int *workp,
61              short *vec, int etu, int *desig, int sctwork, int *cost)
62 {
63     int work_cost = 0;
64     int buildeff_work = *workp / 2;
65     int n, hcms, lcms, neweff;
66     u_char old_type = *desig;
67
68     *cost = 0;
69     neweff = sp->sct_effic;
70
71     if (*desig != sp->sct_newtype) {
72         /*
73          * Tear down existing sector.
74          * Easier to destroy than to build.
75          */
76         work_cost = (sp->sct_effic + 3) / 4;
77         if (work_cost > buildeff_work)
78             work_cost = buildeff_work;
79         buildeff_work -= work_cost;
80         n = sp->sct_effic - work_cost * 4;
81         if (n <= 0) {
82             n = 0;
83             *desig = sp->sct_newtype;
84         }
85         neweff = n;
86         *cost += work_cost;
87         if (opt_BIG_CITY) {
88             if (!n && dchr[old_type].d_pkg == UPKG &&
89                 dchr[*desig].d_pkg != UPKG) {
90                 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
91                 if (vec[I_CIVIL] > maxpop)
92                     vec[I_CIVIL] = maxpop;
93                 if (vec[I_UW] > maxpop)
94                     vec[I_UW] = maxpop;
95                 *workp = (vec[I_CIVIL] * sctwork) / 100.0
96                     + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
97                 *workp = roundavg((etu * (*workp)) / 100.0);
98
99                 buildeff_work = min((int)(*workp / 2), buildeff_work);
100             }
101         }
102     }
103     if (np->nat_priorities[*desig]) {
104         if (*desig == sp->sct_newtype) {
105             work_cost = 100 - neweff;
106             if (work_cost > buildeff_work)
107                 work_cost = buildeff_work;
108
109             if (dchr[*desig].d_lcms > 0) {
110                 lcms = vec[I_LCM];
111                 lcms /= dchr[*desig].d_lcms;
112                 if (work_cost > lcms)
113                     work_cost = lcms;
114             }
115             if (dchr[*desig].d_hcms > 0) {
116                 hcms = vec[I_HCM];
117                 hcms /= dchr[*desig].d_hcms;
118                 if (work_cost > hcms)
119                     work_cost = hcms;
120             }
121
122             neweff += work_cost;
123             *cost += work_cost * dchr[*desig].d_build;
124             buildeff_work -= work_cost;
125
126             if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
127                 vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
128                 vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
129             }
130         }
131     }
132     *workp = (*workp + 1) / 2 + buildeff_work;
133
134     return neweff;
135 }
136
137 /*
138  * enlistment sectors are special; they require military
139  * to convert civ into mil in large numbers.
140  * Conversion will happen much more slowly without
141  * some mil initially.
142  */
143 static int
144 enlist(short *vec, int etu, int *cost)
145 {
146     int maxmil;
147     int enlisted;
148
149     /* Need to check treaties here */
150     enlisted = 0;
151     maxmil = (vec[I_CIVIL] / 2) - vec[I_MILIT];
152     if (maxmil > 0) {
153         enlisted = (etu * (10 + vec[I_MILIT]) * 0.05);
154         if (enlisted > maxmil)
155             enlisted = maxmil;
156         vec[I_CIVIL] -= enlisted;
157         vec[I_MILIT] += enlisted;
158     }
159     *cost = enlisted * 3;
160     return enlisted;
161 }
162
163 /* Fallout is calculated here. */
164
165 static void
166 meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
167           int uid)
168 {
169     i_type n;
170     int melt;
171
172     for (n = I_NONE + 1; n <= I_MAX; n++) {
173         melt = roundavg(vec[n] * etus * (long)fallout
174                         / (1000.0 * melt_item_denom[n]));
175         if (melt > vec[n])
176             melt = vec[n];
177         if (melt > 5 && own) {
178             if (type == EF_SECTOR)
179                 wu(0, own, "Lost %d %s to radiation in %s.\n",
180                    melt < vec[n] ? melt : vec[n], ichr[n].i_name,
181                    xyas(x, y, own));
182             else if (type == EF_LAND)
183                 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
184                    uid, melt < vec[n] ? melt : vec[n], ichr[n].i_name,
185                    xyas(x, y, own));
186             else if (type == EF_SHIP)
187                 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
188                    uid, melt < vec[n] ? melt : vec[n], ichr[n].i_name,
189                    xyas(x, y, own));
190         }
191         vec[n] -= melt;
192     }
193 }
194
195 /*
196  * do_fallout - calculate fallout for sectors.
197  *
198  * This is etu based.  But, do limit HUGE kill offs in large ETU
199  * games, the melting etus rate is limited to 24 etus.
200  */
201
202 void
203 do_fallout(struct sctstr *sp, int etus)
204 {
205     struct shpstr *spp;
206     struct lndstr *lp;
207     int i;
208
209 /* This check shouldn't be needed, but just in case. :) */
210     if (!sp->sct_fallout || !sp->sct_updated)
211         return;
212     if (etus > 24)
213         etus = 24;
214     meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item, EF_SECTOR,
215               sp->sct_x, sp->sct_y, 0);
216     for (i = 0; NULL != (lp = getlandp(i)); i++) {
217         if (!lp->lnd_own)
218             continue;
219         if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
220             continue;
221         meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item, EF_LAND,
222                   lp->lnd_x, lp->lnd_y, lp->lnd_uid);
223     }
224     for (i = 0; NULL != (spp = getshipp(i)); i++) {
225         if (!spp->shp_own)
226             continue;
227         if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
228             continue;
229         if (mchr[(int)spp->shp_type].m_flags & M_SUB)
230             continue;
231         meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, EF_SHIP,
232                   spp->shp_x, spp->shp_y, spp->shp_uid);
233     }
234 #ifdef  GODZILLA
235     if ((sp->sct_fallout > 20) && chance(100))
236         do_godzilla(sp);
237 #endif /* GODZILLA */
238 }
239
240 void
241 spread_fallout(struct sctstr *sp, int etus)
242 {
243     struct sctstr *ap;
244     int n;
245     int inc;
246
247     if (etus > 24)
248         etus = 24;
249     for (n = DIR_FIRST; n <= DIR_LAST; n++) {
250         ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
251         if (ap->sct_type == SCT_SANCT)
252             continue;
253         inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
254 #if 0
255         if (sp->sct_fallout) {
256             wu(0, 0, "Fallout from sector %d,%d to %d,%d is %d=%d*%e*%d\n",
257                sp->sct_x, sp->sct_y, sp->sct_x + diroff[n][0],
258                sp->sct_y + diroff[n][1], inc, etus,
259                fallout_spread, sp->sct_fallout);
260         }
261 #endif
262         if (inc < 0)
263             inc = 0;
264         ap->sct_fallout = min(ap->sct_fallout + inc, FALLOUT_MAX);
265     }
266 }
267
268 void
269 decay_fallout(struct sctstr *sp, int etus)
270 {
271     int decay;
272
273     if (etus > 24)
274         etus = 24;
275     decay = roundavg(((decay_per_etu + 6.0) * fallout_spread) *
276                      (double)etus * (double)sp->sct_fallout);
277
278 #if 0
279     if (decay || sp->sct_fallout)
280         wu(0, 0, "Fallout decay in %d,%d is %d from %d\n", sp->sct_x,
281            sp->sct_y, decay, sp->sct_fallout);
282 #endif
283
284     sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
285 }
286
287 /*
288  * Produce only a set sector type for a specific nation
289  * (or all, if sector_type == -1)
290  *
291  */
292 void
293 produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
294              int sector_type)
295 {
296     struct sctstr *sp;
297     struct natstr *np;
298     short buf[I_MAX + 1];
299     short *vec;
300     int work, cost, ecost, pcost, sctwork;
301     int n, desig, maxpop, neweff, amount;
302
303     for (n = 0; NULL != (sp = getsectid(n)); n++) {
304         if (sp->sct_type == SCT_WATER)
305             continue;
306         if (sp->sct_own != natnum)
307             continue;
308         if (sp->sct_updated != 0)
309             continue;
310         if (sp->sct_type != sector_type && sector_type != -1)
311             continue;
312
313         if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
314             p_sect[SCT_CAPIT][0]++;
315             p_sect[SCT_CAPIT][1] += etu;
316         }
317
318         if (player->simulation) {
319             /* work on a copy, which will be discarded */
320             memcpy(buf, sp->sct_item, sizeof(buf));
321             vec = buf;
322         } else
323             vec = sp->sct_item;
324
325         /* If everybody is dead, the sector reverts to unowned. 
326            * This is also checked at the end of the production in
327            * they all starved or were plagued off.
328          */
329         if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
330             !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
331             makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
332             sp->sct_own = 0;
333             sp->sct_oldown = 0;
334             continue;
335         }
336
337         sp->sct_updated = 1;
338         work = 0;
339
340         np = getnatp(natnum);
341
342         /* do_feed trys to supply.  So, we need to enable cacheing
343            here */
344         bp_enable_cachepath();
345
346         sctwork = do_feed(sp, np, vec, &work, bp, etu);
347
348         bp_disable_cachepath();
349         bp_clear_cachepath();
350
351         if (sp->sct_off || np->nat_money < 0) {
352             if (!player->simulation)
353                 sp->sct_off = 0;
354             continue;
355         }
356         if ((np->nat_priorities[sp->sct_type] == 0) &&
357             (sp->sct_type == sp->sct_newtype) &&
358             ((pchr[dchr[sp->sct_type].d_prd].p_cost != 0) ||
359              (sp->sct_type == SCT_ENLIST))) {
360             if (!player->simulation) {
361                 logerror("Skipping %s production for country %s\n",
362                          dchr[sp->sct_type].d_name, np->nat_cnam);
363             }
364             continue;
365         }
366
367         neweff = sp->sct_effic;
368         amount = 0;
369         pcost = cost = ecost = 0;
370
371         desig = sp->sct_type;
372
373         if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
374             np->nat_money > 0) {
375             neweff = upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
376                                   &cost);
377             pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
378             pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
379             p_sect[SCT_EFFIC][0]++;
380             p_sect[SCT_EFFIC][1] += cost;
381             if (!player->simulation) {
382                 np->nat_money -= cost;
383                 sp->sct_type = desig;
384                 sp->sct_effic = neweff;
385                 if (!opt_DEFENSE_INFRA)
386                     sp->sct_defense = sp->sct_effic;
387             }
388         }
389
390         if ((np->nat_priorities[desig] == 0) &&
391             ((pchr[dchr[desig].d_prd].p_cost != 0) ||
392              (desig == SCT_ENLIST))) {
393             if (!player->simulation) {
394                 logerror("Skipping %s production for country %s\n",
395                          dchr[sp->sct_type].d_name, np->nat_cnam);
396             }
397             continue;
398         }
399
400         if (desig == SCT_ENLIST && neweff >= 60 &&
401             sp->sct_own == sp->sct_oldown) {
402             p_sect[desig][0] += enlist(vec, etu, &ecost);
403             p_sect[desig][1] += ecost;
404             if (!player->simulation)
405                 np->nat_money -= ecost;
406         }
407
408         /*
409          * now do the production (if sector effic >= 60%)
410          */
411
412         if (neweff >= 60) {
413             if (np->nat_money > 0 && dchr[desig].d_prd)
414                 work -=
415                     produce(np, sp, vec, work, desig, neweff, &pcost, &amount);
416         }
417
418         pt_bg_nmbr(bp, sp, I_MAX + 1, work);
419         p_sect[desig][0] += amount;
420         p_sect[desig][1] += pcost;
421         if (!player->simulation) {
422             maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
423             if (vec[I_CIVIL] > maxpop)
424                 vec[I_CIVIL] = maxpop;
425             if (vec[I_UW] > maxpop)
426                 vec[I_UW] = maxpop;
427             sp->sct_avail = work;
428             np->nat_money -= pcost;
429         }
430     }
431 }