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