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