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