]> git.pond.sub.org Git - empserver/blob - src/lib/update/land.c
(feed_land): Remove unused local variables.
[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  *     Markus Armbruster, 2006
35  */
36
37 #include <config.h>
38
39 #include <math.h>
40 #include "misc.h"
41 #include "plague.h"
42 #include "sect.h"
43 #include "nat.h"
44 #include "land.h"
45 #include "ship.h"
46 #include "news.h"
47 #include "file.h"
48 #include "optlist.h"
49 #include "budg.h"
50 #include "player.h"
51 #include "update.h"
52 #include "lost.h"
53 #include "common.h"
54 #include "subs.h"
55 #include "common.h"
56 #include "gen.h"
57
58 int mil_dbl_pay;
59
60 static void landrepair(struct lndstr *, struct natstr *, int *, int);
61 static void upd_land(struct lndstr *, int, struct natstr *, int *, int);
62 static int feed_land(struct lndstr *, int);
63
64 int
65 prod_land(int etus, int natnum, int *bp, int build)
66                 /* build = 1, maintain = 0 */
67 {
68     struct lndstr *lp;
69     struct sctstr *sp;
70     struct natstr *np;
71     int n, k = 0;
72     int start_money;
73     int lastx = 9999, lasty = 9999;
74
75     bp_enable_cachepath();
76     for (n = 0; NULL != (lp = getlandp(n)); n++) {
77         if (lp->lnd_own == 0)
78             continue;
79         if (lp->lnd_own != natnum)
80             continue;
81         if (lp->lnd_effic < LAND_MINEFF) {
82             makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
83                      lp->lnd_x, lp->lnd_y);
84             lp->lnd_own = 0;
85             continue;
86         }
87
88         sp = getsectp(lp->lnd_x, lp->lnd_y);
89         if (sp->sct_type == SCT_SANCT)
90             continue;
91         if (lastx == 9999 || lasty == 9999) {
92             lastx = lp->lnd_x;
93             lasty = lp->lnd_y;
94         }
95         if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
96             /* Reset the cache */
97             bp_disable_cachepath();
98             bp_clear_cachepath();
99             bp_enable_cachepath();
100         }
101         np = getnatp(lp->lnd_own);
102         start_money = np->nat_money;
103         upd_land(lp, etus, np, bp, build);
104         lnd_money[lp->lnd_own] += np->nat_money - start_money;
105         if (!build || np->nat_money != start_money)
106             k++;
107         if (player->simulation)
108             np->nat_money = start_money;
109     }
110     bp_disable_cachepath();
111     bp_clear_cachepath();
112
113     return k;
114 }
115
116 static void
117 upd_land(struct lndstr *lp, int etus,
118          struct natstr *np, int *bp, int build)
119                /* build = 1, maintain = 0 */
120 {
121     struct lchrstr *lcp;
122     int pstage, ptime;
123     int n;
124     int min = morale_base - (int)np->nat_level[NAT_HLEV];
125     int mult;
126     int cost;
127     int eff;
128
129     if (!player->simulation)
130         if (lp->lnd_retreat < min)
131             lp->lnd_retreat = min;
132
133     lcp = &lchr[(int)lp->lnd_type];
134     if (build == 1) {
135         if (!lp->lnd_off && np->nat_money >= 0)
136             landrepair(lp, np, bp, etus);
137         if (!player->simulation)
138             lp->lnd_off = 0;
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 * MIN(0.0, money_land * lcp->l_cost));
146         if (np->nat_money < cost && !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,
151                          lp->lnd_x, 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         if (!player->simulation) {
164             /* feed */
165             if ((n = feed_land(lp, etus)) > 0) {
166                 wu(0, lp->lnd_own, "%d starved in %s%s\n",
167                    n, prland(lp),
168                    (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
169                 if (n > 10)
170                     nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
171             }
172             /*
173              * do plague stuff.  plague can't break out on land units,
174              * but it can still kill people on them.
175              */
176             pstage = lp->lnd_pstage;
177             ptime = lp->lnd_ptime;
178             if (pstage != PLG_HEALTHY) {
179                 n = plague_people(np, lp->lnd_item, &pstage, &ptime, etus);
180                 switch (n) {
181                 case PLG_DYING:
182                     wu(0, lp->lnd_own,
183                        "PLAGUE deaths reported on %s\n", prland(lp));
184                     nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
185                     break;
186                 case PLG_INFECT:
187                     wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
188                     break;
189                 case PLG_INCUBATE:
190                     /* Are we still incubating? */
191                     if (n == pstage) {
192                         /* Yes. Will it turn "infectious" next time? */
193                         if (ptime <= etus) {
194                             /* Yes.  Report an outbreak. */
195                             wu(0, lp->lnd_own,
196                                "Outbreak of PLAGUE on %s!\n", prland(lp));
197                             nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
198                         }
199                     } else {
200                         /* It has already moved on to "infectious" */
201                         wu(0, lp->lnd_own,
202                            "%s battling PLAGUE\n", prland(lp));
203                     }
204                     break;
205                 case PLG_EXPOSED:
206                     /* Has the plague moved to "incubation" yet? */
207                     if (n != pstage) {
208                         /* Yes. Will it turn "infectious" next time? */
209                         if (ptime <= etus) {
210                             /* Yes.  Report an outbreak. */
211                             wu(0, lp->lnd_own,
212                                "Outbreak of PLAGUE on %s!\n", prland(lp));
213                             nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
214                         }
215                     }
216                     break;
217                 default:
218                     break;
219                 }
220                 lp->lnd_pstage = pstage;
221                 lp->lnd_ptime = ptime;
222             }
223         }                       /* end !player->simulation */
224     }
225 }
226
227 /*ARGSUSED*/
228 static void
229 landrepair(struct lndstr *land, struct natstr *np, int *bp, int etus)
230 {
231     int delta;
232     struct sctstr *sp;
233     struct lchrstr *lp;
234     float leftp, buildp;
235     int left, build;
236     int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
237     int avail;
238     int w_p_eff;
239     int mult;
240     int mvec[I_MAX + 1];
241
242     lp = &lchr[(int)land->lnd_type];
243     sp = getsectp(land->lnd_x, land->lnd_y);
244     if (sp->sct_off)
245         return;
246     mult = 1;
247     if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
248         mult = 2;
249
250     if (land->lnd_effic == 100) {
251         /* land is ok; no repairs needed */
252         return;
253     }
254     if (sp->sct_own != land->lnd_own)
255         return;
256
257     if (!player->simulation)
258         avail = sp->sct_avail * 100;
259     else
260         avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
261
262     w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
263     delta = roundavg((double)avail / w_p_eff);
264     if (delta <= 0)
265         return;
266     if (delta > (int)((float)etus * land_grow_scale))
267         delta = (int)((float)etus * land_grow_scale);
268
269     /* delta is the max amount we can grow */
270
271     left = 100 - land->lnd_effic;
272     if (left > delta)
273         left = delta;
274
275     leftp = left / 100.0;
276
277     memset(mvec, 0, sizeof(mvec));
278     mvec[I_LCM] = lcm_needed = ldround(lp->l_lcm * leftp, 1);
279     mvec[I_HCM] = hcm_needed = ldround(lp->l_hcm * leftp, 1);
280 /*
281         mvec[I_GUN] = gun_needed = ldround(lp->l_gun * leftp, 1);
282         mvec[I_MILIT] = mil_needed = ldround(lp->l_mil * leftp, 1);
283         mvec[I_SHELL] = shell_needed = ldround(lp->l_shell *leftp, 1);
284  */
285     mvec[I_GUN] = gun_needed = 0;
286     mvec[I_MILIT] = mil_needed = 0;
287     mvec[I_SHELL] = shell_needed = 0;
288     get_materials(sp, bp, mvec, 0);
289
290     buildp = leftp;
291     if (mvec[I_MILIT] < mil_needed)
292         buildp = MIN(buildp, (float)mvec[I_MILIT] / (float)lp->l_mil);
293     if (mvec[I_LCM] < lcm_needed)
294         buildp = MIN(buildp, (float)mvec[I_LCM] / (float)lp->l_lcm);
295     if (mvec[I_HCM] < hcm_needed)
296         buildp = MIN(buildp, (float)mvec[I_HCM] / (float)lp->l_hcm);
297     if (mvec[I_GUN] < gun_needed)
298         buildp = MIN(buildp, (float)mvec[I_GUN] / (float)lp->l_gun);
299     if (mvec[I_SHELL] < shell_needed)
300         buildp = MIN(buildp, (float)mvec[I_SHELL] / (float)lp->l_shell);
301
302     build = ldround(buildp * 100.0, 1);
303     memset(mvec, 0, sizeof(mvec));
304     mvec[I_LCM] = roundavg(lp->l_lcm * buildp);
305     mvec[I_HCM] = roundavg(lp->l_hcm * buildp);
306 /*
307         mvec[I_GUN] = roundavg(lp->l_gun * buildp);
308         mvec[I_MILIT] = roundavg(lp->l_mil * buildp);
309         mvec[I_SHELL] = roundavg(lp->l_shell *buildp);
310  */
311     mvec[I_GUN] = 0;
312     mvec[I_MILIT] = 0;
313     mvec[I_SHELL] = 0;
314     mil_dbl_pay += mvec[I_MILIT];
315     get_materials(sp, bp, mvec, 1);
316
317     if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
318         build /= 3;
319
320     avail -= build * w_p_eff;
321     if (avail < 0)
322         avail = 0;
323     if (!player->simulation)
324         sp->sct_avail = avail / 100;
325     else
326         pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
327
328     if (build < 0)
329         logerror("land unit %d building %d ! \n", land->lnd_uid, build);
330     np->nat_money -= mult * lp->l_cost * build / 100.0;
331     if (!player->simulation) {
332         land->lnd_effic += (signed char)build;
333     }
334 }
335
336 /*
337  * returns the number who starved, if any.
338  */
339 static int
340 feed_land(struct lndstr *lp, int etus)
341 {
342     int needed;
343
344     if (opt_NOFOOD)
345         return 0;
346
347     needed = (int)ceil(food_needed(lp->lnd_item, etus));
348
349     /* scrounge */
350     if (needed > lp->lnd_item[I_FOOD])
351         resupply_commod(lp, I_FOOD);
352
353     return feed_people(lp->lnd_item, etus);
354 }