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