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