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