]> git.pond.sub.org Git - empserver/blob - src/lib/update/land.c
Declare all configuration variables in optlist.h. Include that
[empserver] / src / lib / update / land.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  *  land.c: Do production for land units
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak, 1992
33  *     Steve McClure, 1996
34  */
35
36 #include "misc.h"
37 #include "var.h"
38 #include "sect.h"
39 #include "nat.h"
40 #include "land.h"
41 #include "ship.h"
42 #include "var.h"
43 #include "news.h"
44 #include "file.h"
45 #include "product.h"
46 #include "optlist.h"
47 #include "budg.h"
48 #include "player.h"
49 #include "update.h"
50 #include "lost.h"
51 #include "common.h"
52 #include "subs.h"
53 #include "common.h"
54 #include "gen.h"
55
56 int mil_dbl_pay;
57
58 #ifndef MIN
59 #define MIN(x,y)       ((x) > (y) ? (y) : (x))
60 #endif
61
62 static int landrepair(register struct lndstr *, int *, struct natstr *,
63                       int *, int);
64 static void upd_land(register struct lndstr *lp, int landno, register int etus,
65                      struct natstr *np, int *bp, int build);
66
67 int
68 prod_land(int etus, int natnum, int *bp, int build)
69                 /* build = 1, maintain = 0 */
70 {
71     register struct lndstr *lp;
72     struct sctstr *sp;
73     struct natstr *np;
74     int n, k = 0;
75     extern long lnd_money[MAXNOC];
76     int start_money;
77     int lastx = 9999, lasty = 9999;
78
79     bp_enable_cachepath();
80     for (n = 0; NULL != (lp = getlandp(n)); n++) {
81         if (lp->lnd_own == 0)
82             continue;
83         if (lp->lnd_own != natnum)
84             continue;
85
86         sp = getsectp(lp->lnd_x, lp->lnd_y);
87         if (sp->sct_type == SCT_SANCT)
88             continue;
89         if (lastx == 9999 || lasty == 9999) {
90             lastx = lp->lnd_x;
91             lasty = lp->lnd_y;
92         }
93         if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
94             /* Reset the cache */
95             bp_disable_cachepath();
96             bp_clear_cachepath();
97             bp_enable_cachepath();
98         }
99         np = getnatp(lp->lnd_own);
100         start_money = np->nat_money;
101         upd_land(lp, n, etus, np, bp, build);
102         lnd_money[lp->lnd_own] += np->nat_money - start_money;
103         if ((build && (np->nat_money != start_money)) || (!build))
104             k++;
105         if (player->simulation)
106             np->nat_money = start_money;
107     }
108     bp_disable_cachepath();
109     bp_clear_cachepath();
110
111     return k;
112 }
113
114 static void
115 upd_land(register struct lndstr *lp, int landno, register int etus,
116          struct natstr *np, int *bp, int build)
117                /* build = 1, maintain = 0 */
118 {
119     struct lchrstr *lcp;
120     int vec[I_MAX + 1];
121     int cvec[I_MAX + 1];
122     int n;
123     int min = morale_base - (int)np->nat_level[NAT_HLEV];
124     int mult;
125     int needed;
126     int cost;
127     int eff;
128
129     if (!player->simulation)
130         if (lp->lnd_retreat < min)
131             lp->lnd_retreat = min;
132
133     lcp = &lchr[(int)lp->lnd_type];
134     getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
135     if (build == 1) {
136         if (np->nat_priorities[PRI_LBUILD] == 0 || np->nat_money < 0)
137             return;
138         if (lp->lnd_effic < LAND_MINEFF ||
139             !(landrepair(lp, vec, np, bp, etus))) {
140             makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
141                      lp->lnd_y);
142             lp->lnd_own = 0;
143             return;
144         }
145     } else {
146         mult = 1;
147         if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
148             mult = 2;
149         if (lcp->l_flags & L_ENGINEER)
150             mult *= 3;
151 /*              cost = -(mult * etus * dmin(0.0, money_land * LND_COST(lcp->l_cost, lp->lnd_tech - lcp->l_tech)));*/
152         cost = -(mult * etus * dmin(0.0, money_land * lcp->l_cost));
153         if ((np->nat_priorities[PRI_LMAINT] == 0 ||
154              np->nat_money < cost) && !player->simulation) {
155             if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
156                 wu(0, lp->lnd_own,
157                    "%s lost to lack of maintenance\n", prland(lp));
158                 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
159                          lp->lnd_y);
160                 lp->lnd_own = 0;
161                 return;
162             }
163             wu(0, lp->lnd_own,
164                "%s lost %d%% to lack of maintenance\n",
165                prland(lp), lp->lnd_effic - eff);
166             lp->lnd_effic = eff;
167         } else {
168             np->nat_money -= cost;
169         }
170 /* Mil costs are now part of regular mil costs, not maint costs */
171 /*              np->nat_money += (int) (money_mil * etus * lnd_getmil(lp));*/
172
173         /* Grab more stuff */
174         if ((opt_NOFOOD == 0) && !player->simulation)
175             resupply_commod(lp, I_FOOD);
176
177         getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
178         if (!player->simulation) {
179             if ((n = feed_land(lp, vec, etus, &needed, 1)) > 0) {
180                 wu(0, lp->lnd_own, "%d starved in %s%s\n",
181                    n, prland(lp),
182                    (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
183                 if (n > 10)
184                     nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
185             }
186             /*
187              * do plague stuff.  plague can't break out on land units,
188              * but it can still kill people on them.
189              */
190             getvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
191             if (cvec[C_PSTAGE] > 0) {
192                 n = plague_people(np, vec, cvec, etus);
193                 switch (n) {
194                 case PLG_DYING:
195                     wu(0, lp->lnd_own,
196                        "PLAGUE deaths reported on %s\n", prland(lp));
197                     nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
198                     break;
199                 case PLG_INFECT:
200                     wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
201                     break;
202                 case PLG_INCUBATE:
203                     /* Are we still incubating? */
204                     if (n == cvec[C_PSTAGE]) {
205                         /* Yes. Will it turn "infectious" next time? */
206                         if (cvec[C_PTIME] <= etus) {
207                             /* Yes.  Report an outbreak. */
208                             wu(0, lp->lnd_own,
209                                "Outbreak of PLAGUE on %s!\n", prland(lp));
210                             nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
211                         }
212                     } else {
213                         /* It has already moved on to "infectious" */
214                         wu(0, lp->lnd_own,
215                            "%s battling PLAGUE\n", prland(lp));
216                     }
217                     break;
218                 case PLG_EXPOSED:
219                     /* Has the plague moved to "incubation" yet? */
220                     if (n != cvec[C_PSTAGE]) {
221                         /* Yes. Will it turn "infectious" next time? */
222                         if (cvec[C_PTIME] <= etus) {
223                             /* Yes.  Report an outbreak. */
224                             wu(0, lp->lnd_own,
225                                "Outbreak of PLAGUE on %s!\n", prland(lp));
226                             nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
227                         }
228                     }
229                     break;
230                 default:
231                     break;
232                 }
233                 putvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
234             }
235             putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
236         }                       /* end !player->simulation */
237     }
238 }
239
240 /*ARGSUSED*/
241 static int
242 landrepair(register struct lndstr *land, int *vec, struct natstr *np,
243            int *bp, int etus)
244 {
245     register int delta;
246     struct sctstr *sp;
247     struct lchrstr *lp;
248     float leftp, buildp;
249     int left, build;
250     int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
251     int avail;
252     int w_p_eff;
253     int mult;
254     int svec[I_MAX + 1];
255     int mvec[I_MAX + 1];
256
257     lp = &lchr[(int)land->lnd_type];
258     sp = getsectp(land->lnd_x, land->lnd_y);
259     if (sp->sct_off)
260         return 1;
261     getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
262     mult = 1;
263     if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
264         mult = 2;
265
266     if (land->lnd_effic == 100) {
267         /* land is ok; no repairs needed */
268         return 1;
269     }
270     if (sp->sct_own != land->lnd_own)
271         return 1;
272
273     if (!player->simulation)
274         avail = sp->sct_avail * 100;
275     else
276         avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
277
278     w_p_eff = 20 + (lp->l_lcm + 2 * lp->l_hcm);
279     delta = roundavg((double)avail / w_p_eff);
280     if (delta <= 0)
281         return 1;
282     if (delta > etus * land_grow_scale)
283         delta = etus * land_grow_scale;
284
285     /* delta is the max amount we can grow */
286
287     left = 100 - land->lnd_effic;
288     if (left > delta)
289         left = delta;
290
291     leftp = ((float)left / 100.0);
292
293     memset(mvec, 0, sizeof(mvec));
294     mvec[I_LCM] = lcm_needed = ldround((double)(lp->l_lcm * leftp), 1);
295     mvec[I_HCM] = hcm_needed = ldround((double)(lp->l_hcm * leftp), 1);
296 /*
297         mvec[I_GUN] = gun_needed = ldround((double)(lp->l_gun * leftp),1);
298         mvec[I_MILIT] = mil_needed = ldround((double)(lp->l_mil * leftp),1);
299         mvec[I_SHELL] = shell_needed = ldround((double)(lp->l_shell *leftp),1);
300  */
301     mvec[I_GUN] = gun_needed = 0;
302     mvec[I_MILIT] = mil_needed = 0;
303     mvec[I_SHELL] = shell_needed = 0;
304
305     get_materials(sp, bp, mvec, 0);
306
307     if (mvec[I_MILIT] >= mil_needed)
308         buildp = leftp;
309     else
310         buildp = ((float)mvec[I_MILIT] / (float)lp->l_mil);
311     if (mvec[I_LCM] < lcm_needed)
312         buildp = MIN(buildp, ((float)mvec[I_LCM] / (float)lp->l_lcm));
313     if (mvec[I_HCM] < hcm_needed)
314         buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)lp->l_hcm));
315     if (mvec[I_GUN] < gun_needed)
316         buildp = MIN(buildp, ((float)mvec[I_GUN] / (float)lp->l_gun));
317     if (mvec[I_SHELL] < shell_needed)
318         buildp = MIN(buildp, ((float)mvec[I_SHELL] / (float)lp->l_shell));
319
320     build = ldround((double)(buildp * 100.0), 1);
321
322     memset(mvec, 0, sizeof(mvec));
323     mvec[I_LCM] = lcm_needed = roundavg((double)(lp->l_lcm * buildp));
324     mvec[I_HCM] = hcm_needed = roundavg((double)(lp->l_hcm * buildp));
325 /*
326         mvec[I_GUN] = gun_needed = roundavg((double)(lp->l_gun * buildp));
327         mvec[I_MILIT] = mil_needed = roundavg((double)(lp->l_mil * buildp));
328         mvec[I_SHELL] = shell_needed = roundavg((double)(lp->l_shell *buildp));
329  */
330     mvec[I_GUN] = gun_needed = 0;
331     mvec[I_MILIT] = mil_needed = 0;
332     mvec[I_SHELL] = shell_needed = 0;
333     mil_dbl_pay += mil_needed;
334
335     get_materials(sp, bp, mvec, 1);
336
337     if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
338         build /= 3;
339
340     avail -= build * w_p_eff;
341     if (!player->simulation) {
342         sp->sct_avail = avail / 100;
343         if (sp->sct_avail < 0)
344             sp->sct_avail = 0;
345     } else {
346         pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
347         if (gt_bg_nmbr(bp, sp, I_MAX + 1) < 0)
348             pt_bg_nmbr(bp, sp, I_MAX + 1, 0);
349     }
350
351     if (build < 0)
352         logerror("land unit %d building %d ! \n", land->lnd_uid, build);
353     np->nat_money -= mult * lp->l_cost * build / 100.0;
354     if (!player->simulation) {
355         land->lnd_effic += (s_char)build;
356
357         putsect(sp);
358     }
359     return 1;
360 }
361
362 /*
363  * returns the number who starved, if any.
364  */
365 int
366 feed_land(struct lndstr *lp, register int *vec, int etus, int *needed,
367           int doit)
368 {
369     double food_eaten, ship_eaten;
370     double people_left;
371     int can_eat, need, svec[I_MAX + 1];
372     int total_people;
373     int starved;
374     struct lchrstr *lcp;
375     struct shpstr *sp;
376
377     if (opt_NOFOOD)
378         return 0;               /* no food no work to be done */
379
380     lcp = &lchr[(int)lp->lnd_type];
381
382     food_eaten = (etus * eatrate) * total_mil(lp);
383     starved = 0;
384     *needed = 0;
385     /*
386      * If we're on a ship, and we don't have enough food,
387      * get some food off the carrying ship. (Don't starve
388      * the ship, tho...
389      */
390 /* doit - Only try to take food off the ship during the update */
391     if ((food_eaten > vec[I_FOOD]) && (lp->lnd_ship >= 0) && doit) {
392         need = (int)food_eaten - vec[I_FOOD];
393         sp = getshipp(lp->lnd_ship);
394         getvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
395         ship_eaten = (etus * eatrate) *
396             (svec[I_CIVIL] + svec[I_MILIT] + svec[I_UW]);
397         if ((svec[I_FOOD] - need) > ship_eaten) {
398             vec[I_FOOD] += need;
399             svec[I_FOOD] -= need;
400         } else if ((svec[I_FOOD] - ship_eaten) > 0) {
401             vec[I_FOOD] += (svec[I_FOOD] - ship_eaten);
402             svec[I_FOOD] -= (svec[I_FOOD] - ship_eaten);
403         }
404         putvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
405     }
406
407     if (food_eaten > vec[I_FOOD]) {
408         *needed = food_eaten - vec[I_FOOD];
409         if (*needed < (food_eaten - vec[I_FOOD]))
410             (*needed)++;
411         can_eat = (vec[I_FOOD] / (etus * eatrate));
412         total_people = total_mil(lp);
413         /* only want to starve off at most 1/2 the populace. */
414         if (can_eat < (total_people / 2))
415             can_eat = total_people / 2;
416
417         people_left = (vec[I_FOOD] + 0.01) / (food_eaten + 0.01);
418         /* only want to starve off at most 1/2 the populace. */
419         if (people_left < 0.5)
420             people_left = 0.5;
421 /*              lp->lnd_effic *= people_left;*/
422         starved = vec[I_MILIT] - (vec[I_MILIT] * people_left);
423 /*              if (!player->simulation)
424                         wu(0, lp->lnd_own, "%d mil starved on unit %s.\n",
425                            starved,
426                            prland(lp));*/
427         vec[I_MILIT] -= starved;
428         vec[I_FOOD] = 0;
429     } else {
430         vec[I_FOOD] -= (int)food_eaten;
431     }
432     return starved;
433 }