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
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
59 static void landrepair(struct lndstr *, struct natstr *, int *, int);
60 static void upd_land(struct lndstr *, int, struct natstr *, int *, int);
61 static int feed_land(struct lndstr *, int);
64 prod_land(int etus, int natnum, int *bp, int build)
65 /* build = 1, maintain = 0 */
72 int lastx = 9999, lasty = 9999;
74 bp_enable_cachepath();
75 for (n = 0; NULL != (lp = getlandp(n)); n++) {
78 if (lp->lnd_own != natnum)
80 if (lp->lnd_effic < LAND_MINEFF) {
81 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
82 lp->lnd_x, lp->lnd_y);
87 sp = getsectp(lp->lnd_x, lp->lnd_y);
88 if (sp->sct_type == SCT_SANCT)
90 if (lastx == 9999 || lasty == 9999) {
94 if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
96 bp_disable_cachepath();
98 bp_enable_cachepath();
100 np = getnatp(lp->lnd_own);
101 start_money = np->nat_money;
102 upd_land(lp, etus, np, bp, build);
103 lnd_money[lp->lnd_own] += np->nat_money - start_money;
104 if (!build || np->nat_money != start_money)
106 if (player->simulation)
107 np->nat_money = start_money;
109 bp_disable_cachepath();
110 bp_clear_cachepath();
116 upd_land(struct lndstr *lp, int etus,
117 struct natstr *np, int *bp, int build)
118 /* build = 1, maintain = 0 */
123 int min = morale_base - (int)np->nat_level[NAT_HLEV];
128 if (!player->simulation)
129 if (lp->lnd_retreat < min)
130 lp->lnd_retreat = min;
132 lcp = &lchr[(int)lp->lnd_type];
134 if (np->nat_priorities[PRI_LBUILD] == 0 || np->nat_money < 0)
136 landrepair(lp, np, bp, etus);
139 if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
141 if (lcp->l_flags & L_ENGINEER)
143 cost = -(mult * etus * MIN(0.0, money_land * lcp->l_cost));
144 if ((np->nat_priorities[PRI_LMAINT] == 0 || np->nat_money < cost)
145 && !player->simulation) {
146 if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
148 "%s lost to lack of maintenance\n", prland(lp));
149 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
150 lp->lnd_x, lp->lnd_y);
155 "%s lost %d%% to lack of maintenance\n",
156 prland(lp), lp->lnd_effic - eff);
159 np->nat_money -= cost;
162 if (!player->simulation) {
164 if ((n = feed_land(lp, etus)) > 0) {
165 wu(0, lp->lnd_own, "%d starved in %s%s\n",
167 (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
169 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
172 * do plague stuff. plague can't break out on land units,
173 * but it can still kill people on them.
175 pstage = lp->lnd_pstage;
176 ptime = lp->lnd_ptime;
177 if (pstage != PLG_HEALTHY) {
178 n = plague_people(np, lp->lnd_item, &pstage, &ptime, etus);
182 "PLAGUE deaths reported on %s\n", prland(lp));
183 nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
186 wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
189 /* Are we still incubating? */
191 /* Yes. Will it turn "infectious" next time? */
193 /* Yes. Report an outbreak. */
195 "Outbreak of PLAGUE on %s!\n", prland(lp));
196 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
199 /* It has already moved on to "infectious" */
201 "%s battling PLAGUE\n", prland(lp));
205 /* Has the plague moved to "incubation" yet? */
207 /* Yes. Will it turn "infectious" next time? */
209 /* Yes. Report an outbreak. */
211 "Outbreak of PLAGUE on %s!\n", prland(lp));
212 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
219 lp->lnd_pstage = pstage;
220 lp->lnd_ptime = ptime;
222 } /* end !player->simulation */
228 landrepair(struct lndstr *land, struct natstr *np, int *bp, int etus)
235 int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
241 lp = &lchr[(int)land->lnd_type];
242 sp = getsectp(land->lnd_x, land->lnd_y);
246 if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
249 if (land->lnd_effic == 100) {
250 /* land is ok; no repairs needed */
253 if (sp->sct_own != land->lnd_own)
256 if (!player->simulation)
257 avail = sp->sct_avail * 100;
259 avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
261 w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
262 delta = roundavg((double)avail / w_p_eff);
265 if (delta > (int)((float)etus * land_grow_scale))
266 delta = (int)((float)etus * land_grow_scale);
268 /* delta is the max amount we can grow */
270 left = 100 - land->lnd_effic;
274 leftp = left / 100.0;
276 memset(mvec, 0, sizeof(mvec));
277 mvec[I_LCM] = lcm_needed = ldround(lp->l_lcm * leftp, 1);
278 mvec[I_HCM] = hcm_needed = ldround(lp->l_hcm * leftp, 1);
280 mvec[I_GUN] = gun_needed = ldround(lp->l_gun * leftp, 1);
281 mvec[I_MILIT] = mil_needed = ldround(lp->l_mil * leftp, 1);
282 mvec[I_SHELL] = shell_needed = ldround(lp->l_shell *leftp, 1);
284 mvec[I_GUN] = gun_needed = 0;
285 mvec[I_MILIT] = mil_needed = 0;
286 mvec[I_SHELL] = shell_needed = 0;
287 get_materials(sp, bp, mvec, 0);
290 if (mvec[I_MILIT] < mil_needed)
291 buildp = MIN(buildp, (float)mvec[I_MILIT] / (float)lp->l_mil);
292 if (mvec[I_LCM] < lcm_needed)
293 buildp = MIN(buildp, (float)mvec[I_LCM] / (float)lp->l_lcm);
294 if (mvec[I_HCM] < hcm_needed)
295 buildp = MIN(buildp, (float)mvec[I_HCM] / (float)lp->l_hcm);
296 if (mvec[I_GUN] < gun_needed)
297 buildp = MIN(buildp, (float)mvec[I_GUN] / (float)lp->l_gun);
298 if (mvec[I_SHELL] < shell_needed)
299 buildp = MIN(buildp, (float)mvec[I_SHELL] / (float)lp->l_shell);
301 build = ldround(buildp * 100.0, 1);
302 memset(mvec, 0, sizeof(mvec));
303 mvec[I_LCM] = roundavg(lp->l_lcm * buildp);
304 mvec[I_HCM] = roundavg(lp->l_hcm * buildp);
306 mvec[I_GUN] = roundavg(lp->l_gun * buildp);
307 mvec[I_MILIT] = roundavg(lp->l_mil * buildp);
308 mvec[I_SHELL] = roundavg(lp->l_shell *buildp);
313 mil_dbl_pay += mvec[I_MILIT];
314 get_materials(sp, bp, mvec, 1);
316 if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
319 avail -= build * w_p_eff;
320 if (!player->simulation) {
321 sp->sct_avail = avail / 100;
322 if (sp->sct_avail < 0)
325 pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
326 if (gt_bg_nmbr(bp, sp, I_MAX + 1) < 0)
327 pt_bg_nmbr(bp, sp, I_MAX + 1, 0);
331 logerror("land unit %d building %d ! \n", land->lnd_uid, build);
332 np->nat_money -= mult * lp->l_cost * build / 100.0;
333 if (!player->simulation) {
334 land->lnd_effic += (signed char)build;
339 * returns the number who starved, if any.
342 feed_land(struct lndstr *lp, int etus)
344 int needed, give, take;
350 needed = (int)ceil(food_needed(lp->lnd_item, etus));
353 if (needed > lp->lnd_item[I_FOOD])
354 resupply_commod(lp, I_FOOD);
356 return feed_people(lp->lnd_item, etus);