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