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