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