2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
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.
28 * land.c: Do production for land units
30 * Known contributors to this file:
32 * Thomas Ruschak, 1992
34 * Markus Armbruster, 2006
49 static void landrepair(struct lndstr *, struct natstr *, int *, int);
50 static void upd_land(struct lndstr *, int, struct natstr *, int *, int);
51 static int feed_land(struct lndstr *, int);
54 prod_land(int etus, int natnum, int *bp, int build)
55 /* build = 1, maintain = 0 */
62 int lastx = 9999, lasty = 9999;
64 bp_enable_cachepath();
65 for (n = 0; NULL != (lp = getlandp(n)); n++) {
68 if (lp->lnd_own != natnum)
70 if (lp->lnd_effic < LAND_MINEFF) {
71 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
72 lp->lnd_x, lp->lnd_y);
77 sp = getsectp(lp->lnd_x, lp->lnd_y);
78 if (sp->sct_type == SCT_SANCT)
80 if (lastx == 9999 || lasty == 9999) {
84 if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
86 bp_disable_cachepath();
88 bp_enable_cachepath();
90 np = getnatp(lp->lnd_own);
91 start_money = np->nat_money;
92 upd_land(lp, etus, np, bp, build);
93 lnd_money[lp->lnd_own] += np->nat_money - start_money;
94 if (!build || np->nat_money != start_money)
96 if (player->simulation)
97 np->nat_money = start_money;
99 bp_disable_cachepath();
100 bp_clear_cachepath();
106 upd_land(struct lndstr *lp, int etus,
107 struct natstr *np, int *bp, int build)
108 /* build = 1, maintain = 0 */
113 int min = morale_base - (int)np->nat_level[NAT_HLEV];
118 if (!player->simulation)
119 if (lp->lnd_retreat < min)
120 lp->lnd_retreat = min;
122 lcp = &lchr[(int)lp->lnd_type];
124 if (!lp->lnd_off && np->nat_money >= 0)
125 landrepair(lp, np, bp, etus);
126 if (!player->simulation)
130 if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
132 if (lcp->l_flags & L_ENGINEER)
134 cost = -(mult * etus * MIN(0.0, money_land * lcp->l_cost));
135 if (np->nat_money < cost && !player->simulation) {
136 if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
138 "%s lost to lack of maintenance\n", prland(lp));
139 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
140 lp->lnd_x, lp->lnd_y);
145 "%s lost %d%% to lack of maintenance\n",
146 prland(lp), lp->lnd_effic - eff);
149 np->nat_money -= cost;
152 if (!player->simulation) {
154 if ((n = feed_land(lp, etus)) > 0) {
155 wu(0, lp->lnd_own, "%d starved in %s%s\n",
157 (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
159 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
162 * do plague stuff. plague can't break out on land units,
163 * but it can still kill people on them.
165 pstage = lp->lnd_pstage;
166 ptime = lp->lnd_ptime;
167 if (pstage != PLG_HEALTHY) {
168 n = plague_people(np, lp->lnd_item, &pstage, &ptime, etus);
172 "PLAGUE deaths reported on %s\n", prland(lp));
173 nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
176 wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
179 /* Are we still incubating? */
181 /* Yes. Will it turn "infectious" next time? */
183 /* Yes. Report an outbreak. */
185 "Outbreak of PLAGUE on %s!\n", prland(lp));
186 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
189 /* It has already moved on to "infectious" */
191 "%s battling PLAGUE\n", prland(lp));
195 /* Has the plague moved to "incubation" yet? */
197 /* Yes. Will it turn "infectious" next time? */
199 /* Yes. Report an outbreak. */
201 "Outbreak of PLAGUE on %s!\n", prland(lp));
202 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
209 lp->lnd_pstage = pstage;
210 lp->lnd_ptime = ptime;
212 } /* end !player->simulation */
218 landrepair(struct lndstr *land, struct natstr *np, int *bp, int etus)
225 int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
231 lp = &lchr[(int)land->lnd_type];
232 sp = getsectp(land->lnd_x, land->lnd_y);
236 if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
239 if (land->lnd_effic == 100) {
240 /* land is ok; no repairs needed */
243 if ((sp->sct_own != land->lnd_own) &&
244 (getrel(getnatp(sp->sct_own), land->lnd_own) != ALLIED))
247 if (!player->simulation)
248 avail = sp->sct_avail * 100;
250 avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
252 w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
253 delta = roundavg((double)avail / w_p_eff);
256 if (delta > (int)((float)etus * land_grow_scale))
257 delta = (int)((float)etus * land_grow_scale);
259 /* delta is the max amount we can grow */
261 left = 100 - land->lnd_effic;
265 leftp = left / 100.0;
267 memset(mvec, 0, sizeof(mvec));
268 mvec[I_LCM] = lcm_needed = ldround(lp->l_lcm * leftp, 1);
269 mvec[I_HCM] = hcm_needed = ldround(lp->l_hcm * leftp, 1);
271 mvec[I_GUN] = gun_needed = ldround(lp->l_gun * leftp, 1);
272 mvec[I_MILIT] = mil_needed = ldround(lp->l_mil * leftp, 1);
273 mvec[I_SHELL] = shell_needed = ldround(lp->l_shell *leftp, 1);
275 mvec[I_GUN] = gun_needed = 0;
276 mvec[I_MILIT] = mil_needed = 0;
277 mvec[I_SHELL] = shell_needed = 0;
278 get_materials(sp, bp, mvec, 0);
281 if (mvec[I_MILIT] < mil_needed)
282 buildp = MIN(buildp, (float)mvec[I_MILIT] / (float)lp->l_mil);
283 if (mvec[I_LCM] < lcm_needed)
284 buildp = MIN(buildp, (float)mvec[I_LCM] / (float)lp->l_lcm);
285 if (mvec[I_HCM] < hcm_needed)
286 buildp = MIN(buildp, (float)mvec[I_HCM] / (float)lp->l_hcm);
287 if (mvec[I_GUN] < gun_needed)
288 buildp = MIN(buildp, (float)mvec[I_GUN] / (float)lp->l_gun);
289 if (mvec[I_SHELL] < shell_needed)
290 buildp = MIN(buildp, (float)mvec[I_SHELL] / (float)lp->l_shell);
292 build = ldround(buildp * 100.0, 1);
293 memset(mvec, 0, sizeof(mvec));
294 mvec[I_LCM] = roundavg(lp->l_lcm * buildp);
295 mvec[I_HCM] = roundavg(lp->l_hcm * buildp);
297 mvec[I_GUN] = roundavg(lp->l_gun * buildp);
298 mvec[I_MILIT] = roundavg(lp->l_mil * buildp);
299 mvec[I_SHELL] = roundavg(lp->l_shell *buildp);
304 get_materials(sp, bp, mvec, 1);
306 if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
309 avail -= build * w_p_eff;
312 if (!player->simulation)
313 sp->sct_avail = avail / 100;
315 pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
318 logerror("land unit %d building %d ! \n", land->lnd_uid, build);
319 np->nat_money -= mult * lp->l_cost * build / 100.0;
320 if (!player->simulation) {
321 land->lnd_effic += (signed char)build;
326 * returns the number who starved, if any.
329 feed_land(struct lndstr *lp, int etus)
336 needed = (int)ceil(food_needed(lp->lnd_item, etus));
339 if (needed > lp->lnd_item[I_FOOD])
340 resupply_commod(lp, I_FOOD);
342 return feed_people(lp->lnd_item, etus);