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