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