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
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 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.
28 * land.c: Do production for land units
30 * Known contributors to this file:
32 * Thomas Ruschak, 1992
58 #define MIN(x,y) ((x) > (y) ? (y) : (x))
61 static int landrepair(struct lndstr *, struct natstr *,
63 static void upd_land(struct lndstr *lp, int etus,
64 struct natstr *np, int *bp, int build);
67 prod_land(int etus, int natnum, int *bp, int build)
68 /* build = 1, maintain = 0 */
75 int lastx = 9999, lasty = 9999;
77 bp_enable_cachepath();
78 for (n = 0; NULL != (lp = getlandp(n)); n++) {
81 if (lp->lnd_own != natnum)
84 sp = getsectp(lp->lnd_x, lp->lnd_y);
85 if (sp->sct_type == SCT_SANCT)
87 if (lastx == 9999 || lasty == 9999) {
91 if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
93 bp_disable_cachepath();
95 bp_enable_cachepath();
97 np = getnatp(lp->lnd_own);
98 start_money = np->nat_money;
99 upd_land(lp, etus, np, bp, build);
100 lnd_money[lp->lnd_own] += np->nat_money - start_money;
101 if ((build && (np->nat_money != start_money)) || (!build))
103 if (player->simulation)
104 np->nat_money = start_money;
106 bp_disable_cachepath();
107 bp_clear_cachepath();
113 upd_land(struct lndstr *lp, int etus,
114 struct natstr *np, int *bp, int build)
115 /* build = 1, maintain = 0 */
120 int min = morale_base - (int)np->nat_level[NAT_HLEV];
126 if (!player->simulation)
127 if (lp->lnd_retreat < min)
128 lp->lnd_retreat = min;
130 lcp = &lchr[(int)lp->lnd_type];
132 if (np->nat_priorities[PRI_LBUILD] == 0 || np->nat_money < 0)
134 if (lp->lnd_effic < LAND_MINEFF || !landrepair(lp, np, bp, etus)) {
135 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
142 if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
144 if (lcp->l_flags & L_ENGINEER)
146 /* cost = -(mult * etus * dmin(0.0, money_land * LND_COST(lcp->l_cost, lp->lnd_tech - lcp->l_tech)));*/
147 cost = -(mult * etus * dmin(0.0, money_land * lcp->l_cost));
148 if ((np->nat_priorities[PRI_LMAINT] == 0 || np->nat_money < cost)
149 && !player->simulation) {
150 if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
152 "%s lost to lack of maintenance\n", prland(lp));
153 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
159 "%s lost %d%% to lack of maintenance\n",
160 prland(lp), lp->lnd_effic - eff);
163 np->nat_money -= cost;
166 /* Grab more stuff */
167 if ((opt_NOFOOD == 0) && !player->simulation)
168 resupply_commod(lp, I_FOOD);
170 if (!player->simulation) {
171 if ((n = feed_land(lp, etus, &needed, 1)) > 0) {
172 wu(0, lp->lnd_own, "%d starved in %s%s\n",
174 (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
176 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
179 * do plague stuff. plague can't break out on land units,
180 * but it can still kill people on them.
182 pstage = lp->lnd_pstage;
183 ptime = lp->lnd_ptime;
184 if (pstage != PLG_HEALTHY) {
185 n = plague_people(np, lp->lnd_item, &pstage, &ptime, etus);
189 "PLAGUE deaths reported on %s\n", prland(lp));
190 nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
193 wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
196 /* Are we still incubating? */
198 /* Yes. Will it turn "infectious" next time? */
200 /* Yes. Report an outbreak. */
202 "Outbreak of PLAGUE on %s!\n", prland(lp));
203 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
206 /* It has already moved on to "infectious" */
208 "%s battling PLAGUE\n", prland(lp));
212 /* Has the plague moved to "incubation" yet? */
214 /* Yes. Will it turn "infectious" next time? */
216 /* Yes. Report an outbreak. */
218 "Outbreak of PLAGUE on %s!\n", prland(lp));
219 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
226 lp->lnd_pstage = pstage;
227 lp->lnd_ptime = ptime;
229 } /* end !player->simulation */
235 landrepair(struct lndstr *land, struct natstr *np,
243 int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
249 lp = &lchr[(int)land->lnd_type];
250 sp = getsectp(land->lnd_x, land->lnd_y);
254 if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
257 if (land->lnd_effic == 100) {
258 /* land is ok; no repairs needed */
261 if (sp->sct_own != land->lnd_own)
264 if (!player->simulation)
265 avail = sp->sct_avail * 100;
267 avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
269 w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
270 delta = roundavg((double)avail / w_p_eff);
273 if (delta > etus * land_grow_scale)
274 delta = etus * land_grow_scale;
276 /* delta is the max amount we can grow */
278 left = 100 - land->lnd_effic;
282 leftp = ((float)left / 100.0);
284 memset(mvec, 0, sizeof(mvec));
285 mvec[I_LCM] = lcm_needed = ldround((double)(lp->l_lcm * leftp), 1);
286 mvec[I_HCM] = hcm_needed = ldround((double)(lp->l_hcm * leftp), 1);
288 mvec[I_GUN] = gun_needed = ldround((double)(lp->l_gun * leftp),1);
289 mvec[I_MILIT] = mil_needed = ldround((double)(lp->l_mil * leftp),1);
290 mvec[I_SHELL] = shell_needed = ldround((double)(lp->l_shell *leftp),1);
292 mvec[I_GUN] = gun_needed = 0;
293 mvec[I_MILIT] = mil_needed = 0;
294 mvec[I_SHELL] = shell_needed = 0;
296 get_materials(sp, bp, mvec, 0);
298 if (mvec[I_MILIT] >= mil_needed)
301 buildp = ((float)mvec[I_MILIT] / (float)lp->l_mil);
302 if (mvec[I_LCM] < lcm_needed)
303 buildp = MIN(buildp, ((float)mvec[I_LCM] / (float)lp->l_lcm));
304 if (mvec[I_HCM] < hcm_needed)
305 buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)lp->l_hcm));
306 if (mvec[I_GUN] < gun_needed)
307 buildp = MIN(buildp, ((float)mvec[I_GUN] / (float)lp->l_gun));
308 if (mvec[I_SHELL] < shell_needed)
309 buildp = MIN(buildp, ((float)mvec[I_SHELL] / (float)lp->l_shell));
311 build = ldround((double)(buildp * 100.0), 1);
313 memset(mvec, 0, sizeof(mvec));
314 mvec[I_LCM] = lcm_needed = roundavg((double)(lp->l_lcm * buildp));
315 mvec[I_HCM] = hcm_needed = roundavg((double)(lp->l_hcm * buildp));
317 mvec[I_GUN] = gun_needed = roundavg((double)(lp->l_gun * buildp));
318 mvec[I_MILIT] = mil_needed = roundavg((double)(lp->l_mil * buildp));
319 mvec[I_SHELL] = shell_needed = roundavg((double)(lp->l_shell *buildp));
321 mvec[I_GUN] = gun_needed = 0;
322 mvec[I_MILIT] = mil_needed = 0;
323 mvec[I_SHELL] = shell_needed = 0;
324 mil_dbl_pay += mil_needed;
326 get_materials(sp, bp, mvec, 1);
328 if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
331 avail -= build * w_p_eff;
332 if (!player->simulation) {
333 sp->sct_avail = avail / 100;
334 if (sp->sct_avail < 0)
337 pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
338 if (gt_bg_nmbr(bp, sp, I_MAX + 1) < 0)
339 pt_bg_nmbr(bp, sp, I_MAX + 1, 0);
343 logerror("land unit %d building %d ! \n", land->lnd_uid, build);
344 np->nat_money -= mult * lp->l_cost * build / 100.0;
345 if (!player->simulation) {
346 land->lnd_effic += (s_char)build;
354 * returns the number who starved, if any.
357 feed_land(struct lndstr *lp, int etus, int *needed, int doit)
359 double food_eaten, ship_eaten;
368 return 0; /* no food no work to be done */
370 total_people = total_mil(lp);
371 food_eaten = etus * eatrate * total_people;
372 ifood_eaten = (int)food_eaten;
373 if (food_eaten - ifood_eaten > 0)
379 * If we're on a ship, and we don't have enough food,
380 * get some food off the carrying ship. (Don't starve
383 /* doit - Only try to take food off the ship during the update */
384 if (ifood_eaten > lp->lnd_item[I_FOOD] && lp->lnd_ship >= 0 && doit) {
385 need = ifood_eaten - lp->lnd_item[I_FOOD];
386 sp = getshipp(lp->lnd_ship);
387 ship_eaten = etus * eatrate * (sp->shp_item[I_CIVIL]
388 + sp->shp_item[I_MILIT]
389 + sp->shp_item[I_UW]);
390 if (sp->shp_item[I_FOOD] - need > ship_eaten) {
391 lp->lnd_item[I_FOOD] += need;
392 sp->shp_item[I_FOOD] -= need;
393 } else if (sp->shp_item[I_FOOD] - ship_eaten > 0) {
394 lp->lnd_item[I_FOOD] += sp->shp_item[I_FOOD] - ship_eaten;
395 sp->shp_item[I_FOOD] -= sp->shp_item[I_FOOD] - ship_eaten;
399 if (ifood_eaten > lp->lnd_item[I_FOOD]) {
400 *needed = ifood_eaten - lp->lnd_item[I_FOOD];
401 people_left = (lp->lnd_item[I_FOOD] + 0.01) / (food_eaten + 0.01);
402 /* only want to starve off at most 1/2 the populace. */
403 if (people_left < 0.5)
405 starved = total_people * (1 - people_left);
406 lp->lnd_item[I_MILIT] -= starved;
407 lp->lnd_item[I_FOOD] = 0;
409 lp->lnd_item[I_FOOD] -= (int)food_eaten;