]> git.pond.sub.org Git - empserver/blob - src/lib/update/sect.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / update / sect.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <config.h>
36
37 #include "misc.h"
38 #include "sect.h"
39 #include "nat.h"
40 #include "item.h"
41 #include "file.h"
42 #include "xy.h"
43 #include "path.h"
44 #include "product.h"
45 #include "optlist.h"
46 #include "budg.h"
47 #include "player.h"
48 #include "land.h"
49 #include "ship.h"
50 #include "update.h"
51 #include "subs.h"
52 #include "common.h"
53 #include "lost.h"
54 #include "gen.h"
55
56 /*
57  * Increase sector efficiency if old type == new type.
58  * decrease sector efficiency if old type != new type.
59  * Return amount of work used.
60  */
61 static int
62 upd_buildeff(struct natstr *np, struct sctstr *sp, int *workp,
63              short *vec, int etu, int *desig, int sctwork, int *cost)
64 {
65     int work_cost = 0;
66     int buildeff_work = *workp / 2;
67     int n, hcms, lcms, neweff;
68     u_char old_type = *desig;
69
70     *cost = 0;
71     neweff = sp->sct_effic;
72
73     if (*desig != sp->sct_newtype) {
74         /*
75          * Tear down existing sector.
76          * Easier to destroy than to build.
77          */
78         work_cost = (sp->sct_effic + 3) / 4;
79         if (work_cost > buildeff_work)
80             work_cost = buildeff_work;
81         buildeff_work -= work_cost;
82         n = sp->sct_effic - work_cost * 4;
83         if (n <= 0) {
84             n = 0;
85             *desig = sp->sct_newtype;
86         }
87         neweff = n;
88         *cost += work_cost;
89         if (!n && IS_BIG_CITY(old_type) &&
90             !IS_BIG_CITY(*desig)) {
91             int maxpop = max_population(np->nat_level[NAT_RLEV], *desig, n);
92             if (vec[I_CIVIL] > maxpop)
93                 vec[I_CIVIL] = maxpop;
94             if (vec[I_UW] > maxpop)
95                 vec[I_UW] = maxpop;
96             *workp = (vec[I_CIVIL] * sctwork) / 100.0
97                 + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
98             *workp = roundavg((etu * (*workp)) / 100.0);
99
100             buildeff_work = MIN((int)(*workp / 2), buildeff_work);
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 * ichr[n].i_melt_denom));
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, 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, 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, ichr[n].i_name,
189                    xyas(x, y, own));
190         }
191         vec[n] -= melt;
192     }
193 }
194
195 /*
196  * Do fallout meltdown for sector SP.
197  * ETUS above 24 are treated as 24 to avoid *huge* kill offs in
198  * large ETU games.
199  */
200 void
201 do_fallout(struct sctstr *sp, int etus)
202 {
203     struct shpstr *spp;
204     struct lndstr *lp;
205     int i;
206
207 /* This check shouldn't be needed, but just in case. :) */
208     if (!sp->sct_fallout || !sp->sct_updated)
209         return;
210     if (etus > 24)
211         etus = 24;
212     meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item, EF_SECTOR,
213               sp->sct_x, sp->sct_y, 0);
214     for (i = 0; NULL != (lp = getlandp(i)); i++) {
215         if (!lp->lnd_own)
216             continue;
217         if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
218             continue;
219         meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item, EF_LAND,
220                   lp->lnd_x, lp->lnd_y, lp->lnd_uid);
221     }
222     for (i = 0; NULL != (spp = getshipp(i)); i++) {
223         if (!spp->shp_own)
224             continue;
225         if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
226             continue;
227         if (mchr[(int)spp->shp_type].m_flags & M_SUB)
228             continue;
229         meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, EF_SHIP,
230                   spp->shp_x, spp->shp_y, spp->shp_uid);
231     }
232 }
233
234 void
235 spread_fallout(struct sctstr *sp, int etus)
236 {
237     struct sctstr *ap;
238     int n;
239     int inc;
240
241     if (etus > 24)
242         etus = 24;
243     for (n = DIR_FIRST; n <= DIR_LAST; n++) {
244         ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
245         if (ap->sct_type == SCT_SANCT)
246             continue;
247         inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
248         if (inc < 0)
249             inc = 0;
250         ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
251     }
252 }
253
254 void
255 decay_fallout(struct sctstr *sp, int etus)
256 {
257     int decay;
258
259     if (etus > 24)
260         etus = 24;
261     decay = roundavg(((decay_per_etu + 6.0) * fallout_spread) *
262                      (double)etus * (double)sp->sct_fallout);
263
264     sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
265 }
266
267 /*
268  * Produce only a set sector type for a specific nation
269  * (or all, if sector_type == -1)
270  *
271  */
272 void
273 produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
274              int sector_type)
275 {
276     struct sctstr *sp;
277     struct natstr *np;
278     short buf[I_MAX + 1];
279     short *vec;
280     int work, cost, ecost, pcost, sctwork;
281     int n, desig, maxpop, neweff, amount;
282
283     for (n = 0; NULL != (sp = getsectid(n)); n++) {
284         if (sp->sct_type == SCT_WATER)
285             continue;
286         if (sp->sct_own != natnum)
287             continue;
288         if (sp->sct_updated != 0)
289             continue;
290         if (sp->sct_type != sector_type && sector_type != -1)
291             continue;
292
293         if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
294             p_sect[SCT_CAPIT][0]++;
295             p_sect[SCT_CAPIT][1] += etu;
296         }
297
298         if (player->simulation) {
299             /* work on a copy, which will be discarded */
300             memcpy(buf, sp->sct_item, sizeof(buf));
301             vec = buf;
302         } else
303             vec = sp->sct_item;
304
305         /* If everybody is dead, the sector reverts to unowned. 
306            * This is also checked at the end of the production in
307            * they all starved or were plagued off.
308          */
309         if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
310             !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
311             makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
312             sp->sct_own = 0;
313             sp->sct_oldown = 0;
314             continue;
315         }
316
317         sp->sct_updated = 1;
318         work = 0;
319
320         np = getnatp(natnum);
321
322         /* do_feed trys to supply.  So, we need to enable cacheing
323            here */
324         bp_enable_cachepath();
325
326         sctwork = do_feed(sp, np, vec, &work, bp, etu);
327
328         bp_disable_cachepath();
329         bp_clear_cachepath();
330
331         if (sp->sct_off || np->nat_money < 0) {
332             if (!player->simulation)
333                 sp->sct_off = 0;
334             continue;
335         }
336         if ((np->nat_priorities[sp->sct_type] == 0) &&
337             (sp->sct_type == sp->sct_newtype) &&
338             ((pchr[dchr[sp->sct_type].d_prd].p_cost != 0) ||
339              (sp->sct_type == SCT_ENLIST))) {
340             if (!player->simulation) {
341                 logerror("Skipping %s production for country %s\n",
342                          dchr[sp->sct_type].d_name, np->nat_cnam);
343             }
344             continue;
345         }
346
347         neweff = sp->sct_effic;
348         amount = 0;
349         pcost = cost = ecost = 0;
350
351         desig = sp->sct_type;
352
353         if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
354             np->nat_money > 0) {
355             neweff = upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
356                                   &cost);
357             pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
358             pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
359             p_sect[SCT_EFFIC][0]++;
360             p_sect[SCT_EFFIC][1] += cost;
361             if (!player->simulation) {
362                 np->nat_money -= cost;
363                 sp->sct_type = desig;
364                 sp->sct_effic = neweff;
365                 if (!opt_DEFENSE_INFRA)
366                     sp->sct_defense = sp->sct_effic;
367             }
368         }
369
370         if ((np->nat_priorities[desig] == 0) &&
371             ((pchr[dchr[desig].d_prd].p_cost != 0) ||
372              (desig == SCT_ENLIST))) {
373             if (!player->simulation) {
374                 logerror("Skipping %s production for country %s\n",
375                          dchr[sp->sct_type].d_name, np->nat_cnam);
376             }
377             continue;
378         }
379
380         if (desig == SCT_ENLIST && neweff >= 60 &&
381             sp->sct_own == sp->sct_oldown) {
382             p_sect[desig][0] += enlist(vec, etu, &ecost);
383             p_sect[desig][1] += ecost;
384             if (!player->simulation)
385                 np->nat_money -= ecost;
386         }
387
388         /*
389          * now do the production (if sector effic >= 60%)
390          */
391
392         if (neweff >= 60) {
393             if (np->nat_money > 0 && dchr[desig].d_prd)
394                 work -=
395                     produce(np, sp, vec, work, desig, neweff, &pcost, &amount);
396         }
397
398         pt_bg_nmbr(bp, sp, I_MAX + 1, work);
399         p_sect[desig][0] += amount;
400         p_sect[desig][1] += pcost;
401         if (!player->simulation) {
402             maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
403             if (vec[I_CIVIL] > maxpop)
404                 vec[I_CIVIL] = maxpop;
405             if (vec[I_UW] > maxpop)
406                 vec[I_UW] = maxpop;
407             sp->sct_avail = work;
408             np->nat_money -= pcost;
409         }
410     }
411 }