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
34 * Markus Armbruster, 2006
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);
65 prod_land(int etus, int natnum, int *bp, int build)
66 /* build = 1, maintain = 0 */
73 int lastx = 9999, lasty = 9999;
75 bp_enable_cachepath();
76 for (n = 0; NULL != (lp = getlandp(n)); n++) {
79 if (lp->lnd_own != natnum)
81 if (lp->lnd_effic < LAND_MINEFF) {
82 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
83 lp->lnd_x, lp->lnd_y);
88 sp = getsectp(lp->lnd_x, lp->lnd_y);
89 if (sp->sct_type == SCT_SANCT)
91 if (lastx == 9999 || lasty == 9999) {
95 if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
97 bp_disable_cachepath();
99 bp_enable_cachepath();
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)
107 if (player->simulation)
108 np->nat_money = start_money;
110 bp_disable_cachepath();
111 bp_clear_cachepath();
117 upd_land(struct lndstr *lp, int etus,
118 struct natstr *np, int *bp, int build)
119 /* build = 1, maintain = 0 */
124 int min = morale_base - (int)np->nat_level[NAT_HLEV];
129 if (!player->simulation)
130 if (lp->lnd_retreat < min)
131 lp->lnd_retreat = min;
133 lcp = &lchr[(int)lp->lnd_type];
135 if (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_money < cost && !player->simulation) {
145 if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
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);
154 "%s lost %d%% to lack of maintenance\n",
155 prland(lp), lp->lnd_effic - eff);
158 np->nat_money -= cost;
161 if (!player->simulation) {
163 if ((n = feed_land(lp, etus)) > 0) {
164 wu(0, lp->lnd_own, "%d starved in %s%s\n",
166 (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
168 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
171 * do plague stuff. plague can't break out on land units,
172 * but it can still kill people on them.
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);
181 "PLAGUE deaths reported on %s\n", prland(lp));
182 nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
185 wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
188 /* Are we still incubating? */
190 /* Yes. Will it turn "infectious" next time? */
192 /* Yes. Report an outbreak. */
194 "Outbreak of PLAGUE on %s!\n", prland(lp));
195 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
198 /* It has already moved on to "infectious" */
200 "%s battling PLAGUE\n", prland(lp));
204 /* Has the plague moved to "incubation" yet? */
206 /* Yes. Will it turn "infectious" next time? */
208 /* Yes. Report an outbreak. */
210 "Outbreak of PLAGUE on %s!\n", prland(lp));
211 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
218 lp->lnd_pstage = pstage;
219 lp->lnd_ptime = ptime;
221 } /* end !player->simulation */
227 landrepair(struct lndstr *land, struct natstr *np, int *bp, int etus)
234 int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
240 lp = &lchr[(int)land->lnd_type];
241 sp = getsectp(land->lnd_x, land->lnd_y);
245 if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
248 if (land->lnd_effic == 100) {
249 /* land is ok; no repairs needed */
252 if (sp->sct_own != land->lnd_own)
255 if (!player->simulation)
256 avail = sp->sct_avail * 100;
258 avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
260 w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
261 delta = roundavg((double)avail / w_p_eff);
264 if (delta > (int)((float)etus * land_grow_scale))
265 delta = (int)((float)etus * land_grow_scale);
267 /* delta is the max amount we can grow */
269 left = 100 - land->lnd_effic;
273 leftp = left / 100.0;
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);
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);
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);
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);
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);
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);
312 mil_dbl_pay += mvec[I_MILIT];
313 get_materials(sp, bp, mvec, 1);
315 if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
318 avail -= build * w_p_eff;
321 if (!player->simulation)
322 sp->sct_avail = avail / 100;
324 pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
327 logerror("land unit %d building %d ! \n", land->lnd_uid, build);
328 np->nat_money -= mult * lp->l_cost * build / 100.0;
329 if (!player->simulation) {
330 land->lnd_effic += (signed char)build;
335 * returns the number who starved, if any.
338 feed_land(struct lndstr *lp, int etus)
340 int needed, give, take;
346 needed = (int)ceil(food_needed(lp->lnd_item, etus));
349 if (needed > lp->lnd_item[I_FOOD])
350 resupply_commod(lp, I_FOOD);
352 return feed_people(lp->lnd_item, etus);