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