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