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