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