2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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
59 #define MIN(x,y) ((x) > (y) ? (y) : (x))
62 static int landrepair(register struct lndstr *, int *, struct natstr *,
64 static void upd_land(register struct lndstr *lp, register int etus,
65 struct natstr *np, int *bp, int build);
68 prod_land(int etus, int natnum, int *bp, int build)
69 /* build = 1, maintain = 0 */
71 register struct lndstr *lp;
76 int lastx = 9999, lasty = 9999;
78 bp_enable_cachepath();
79 for (n = 0; NULL != (lp = getlandp(n)); n++) {
82 if (lp->lnd_own != natnum)
85 sp = getsectp(lp->lnd_x, lp->lnd_y);
86 if (sp->sct_type == SCT_SANCT)
88 if (lastx == 9999 || lasty == 9999) {
92 if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
94 bp_disable_cachepath();
96 bp_enable_cachepath();
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))
104 if (player->simulation)
105 np->nat_money = start_money;
107 bp_disable_cachepath();
108 bp_clear_cachepath();
114 upd_land(register struct lndstr *lp, register int etus,
115 struct natstr *np, int *bp, int build)
116 /* build = 1, maintain = 0 */
119 u_short pstage, ptime;
122 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];
133 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
135 if (np->nat_priorities[PRI_LBUILD] == 0 || np->nat_money < 0)
137 if (lp->lnd_effic < LAND_MINEFF ||
138 !(landrepair(lp, vec, np, bp, etus))) {
139 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
146 if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
148 if (lcp->l_flags & L_ENGINEER)
150 /* cost = -(mult * etus * dmin(0.0, money_land * LND_COST(lcp->l_cost, lp->lnd_tech - lcp->l_tech)));*/
151 cost = -(mult * etus * dmin(0.0, money_land * lcp->l_cost));
152 if ((np->nat_priorities[PRI_LMAINT] == 0 ||
153 np->nat_money < cost) && !player->simulation) {
154 if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
156 "%s lost to lack of maintenance\n", prland(lp));
157 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
163 "%s lost %d%% to lack of maintenance\n",
164 prland(lp), lp->lnd_effic - eff);
167 np->nat_money -= cost;
169 /* Mil costs are now part of regular mil costs, not maint costs */
170 /* np->nat_money += (int) (money_mil * etus * lnd_getmil(lp));*/
172 /* Grab more stuff */
173 if ((opt_NOFOOD == 0) && !player->simulation)
174 resupply_commod(lp, I_FOOD);
176 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
177 if (!player->simulation) {
178 if ((n = feed_land(lp, vec, etus, &needed, 1)) > 0) {
179 wu(0, lp->lnd_own, "%d starved in %s%s\n",
181 (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
183 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
186 * do plague stuff. plague can't break out on land units,
187 * but it can still kill people on them.
189 pstage = lp->lnd_pstage;
190 ptime = lp->lnd_ptime;
191 if (pstage != PLG_HEALTHY) {
192 n = plague_people(np, vec, &pstage, &ptime, etus);
196 "PLAGUE deaths reported on %s\n", prland(lp));
197 nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
200 wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
203 /* Are we still incubating? */
205 /* Yes. Will it turn "infectious" next time? */
207 /* Yes. Report an outbreak. */
209 "Outbreak of PLAGUE on %s!\n", prland(lp));
210 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
213 /* It has already moved on to "infectious" */
215 "%s battling PLAGUE\n", prland(lp));
219 /* Has the plague moved to "incubation" yet? */
221 /* Yes. Will it turn "infectious" next time? */
223 /* Yes. Report an outbreak. */
225 "Outbreak of PLAGUE on %s!\n", prland(lp));
226 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
233 lp->lnd_pstage = pstage;
234 lp->lnd_ptime = ptime;
236 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
237 } /* end !player->simulation */
243 landrepair(register struct lndstr *land, int *vec, struct natstr *np,
251 int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
258 lp = &lchr[(int)land->lnd_type];
259 sp = getsectp(land->lnd_x, land->lnd_y);
262 getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
264 if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
267 if (land->lnd_effic == 100) {
268 /* land is ok; no repairs needed */
271 if (sp->sct_own != land->lnd_own)
274 if (!player->simulation)
275 avail = sp->sct_avail * 100;
277 avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
279 w_p_eff = 20 + (lp->l_lcm + 2 * lp->l_hcm);
280 delta = roundavg((double)avail / w_p_eff);
283 if (delta > etus * land_grow_scale)
284 delta = etus * land_grow_scale;
286 /* delta is the max amount we can grow */
288 left = 100 - land->lnd_effic;
292 leftp = ((float)left / 100.0);
294 memset(mvec, 0, sizeof(mvec));
295 mvec[I_LCM] = lcm_needed = ldround((double)(lp->l_lcm * leftp), 1);
296 mvec[I_HCM] = hcm_needed = ldround((double)(lp->l_hcm * leftp), 1);
298 mvec[I_GUN] = gun_needed = ldround((double)(lp->l_gun * leftp),1);
299 mvec[I_MILIT] = mil_needed = ldround((double)(lp->l_mil * leftp),1);
300 mvec[I_SHELL] = shell_needed = ldround((double)(lp->l_shell *leftp),1);
302 mvec[I_GUN] = gun_needed = 0;
303 mvec[I_MILIT] = mil_needed = 0;
304 mvec[I_SHELL] = shell_needed = 0;
306 get_materials(sp, bp, mvec, 0);
308 if (mvec[I_MILIT] >= mil_needed)
311 buildp = ((float)mvec[I_MILIT] / (float)lp->l_mil);
312 if (mvec[I_LCM] < lcm_needed)
313 buildp = MIN(buildp, ((float)mvec[I_LCM] / (float)lp->l_lcm));
314 if (mvec[I_HCM] < hcm_needed)
315 buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)lp->l_hcm));
316 if (mvec[I_GUN] < gun_needed)
317 buildp = MIN(buildp, ((float)mvec[I_GUN] / (float)lp->l_gun));
318 if (mvec[I_SHELL] < shell_needed)
319 buildp = MIN(buildp, ((float)mvec[I_SHELL] / (float)lp->l_shell));
321 build = ldround((double)(buildp * 100.0), 1);
323 memset(mvec, 0, sizeof(mvec));
324 mvec[I_LCM] = lcm_needed = roundavg((double)(lp->l_lcm * buildp));
325 mvec[I_HCM] = hcm_needed = roundavg((double)(lp->l_hcm * buildp));
327 mvec[I_GUN] = gun_needed = roundavg((double)(lp->l_gun * buildp));
328 mvec[I_MILIT] = mil_needed = roundavg((double)(lp->l_mil * buildp));
329 mvec[I_SHELL] = shell_needed = roundavg((double)(lp->l_shell *buildp));
331 mvec[I_GUN] = gun_needed = 0;
332 mvec[I_MILIT] = mil_needed = 0;
333 mvec[I_SHELL] = shell_needed = 0;
334 mil_dbl_pay += mil_needed;
336 get_materials(sp, bp, mvec, 1);
338 if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
341 avail -= build * w_p_eff;
342 if (!player->simulation) {
343 sp->sct_avail = avail / 100;
344 if (sp->sct_avail < 0)
347 pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
348 if (gt_bg_nmbr(bp, sp, I_MAX + 1) < 0)
349 pt_bg_nmbr(bp, sp, I_MAX + 1, 0);
353 logerror("land unit %d building %d ! \n", land->lnd_uid, build);
354 np->nat_money -= mult * lp->l_cost * build / 100.0;
355 if (!player->simulation) {
356 land->lnd_effic += (s_char)build;
364 * returns the number who starved, if any.
367 feed_land(struct lndstr *lp, register int *vec, int etus, int *needed,
370 double food_eaten, ship_eaten;
379 return 0; /* no food no work to be done */
381 lcp = &lchr[(int)lp->lnd_type];
383 food_eaten = (etus * eatrate) * total_mil(lp);
387 * If we're on a ship, and we don't have enough food,
388 * get some food off the carrying ship. (Don't starve
391 /* doit - Only try to take food off the ship during the update */
392 if ((food_eaten > vec[I_FOOD]) && (lp->lnd_ship >= 0) && doit) {
393 need = (int)food_eaten - vec[I_FOOD];
394 sp = getshipp(lp->lnd_ship);
395 ship_eaten = etus * eatrate * (sp->shp_item[I_CIVIL]
396 + sp->shp_item[I_MILIT]
397 + sp->shp_item[I_UW]);
398 if (sp->shp_item[I_FOOD] - need > ship_eaten) {
400 sp->shp_item[I_FOOD] -= need;
401 } else if (sp->shp_item[I_FOOD] - ship_eaten > 0) {
402 vec[I_FOOD] += sp->shp_item[I_FOOD] - ship_eaten;
403 sp->shp_item[I_FOOD] -= sp->shp_item[I_FOOD] - ship_eaten;
407 if (food_eaten > vec[I_FOOD]) {
408 *needed = food_eaten - vec[I_FOOD];
409 if (*needed < (food_eaten - vec[I_FOOD]))
411 can_eat = (vec[I_FOOD] / (etus * eatrate));
412 total_people = total_mil(lp);
413 /* only want to starve off at most 1/2 the populace. */
414 if (can_eat < (total_people / 2))
415 can_eat = total_people / 2;
417 people_left = (vec[I_FOOD] + 0.01) / (food_eaten + 0.01);
418 /* only want to starve off at most 1/2 the populace. */
419 if (people_left < 0.5)
421 /* lp->lnd_effic *= people_left;*/
422 starved = vec[I_MILIT] - (vec[I_MILIT] * people_left);
423 /* if (!player->simulation)
424 wu(0, lp->lnd_own, "%d mil starved on unit %s.\n",
427 vec[I_MILIT] -= starved;
430 vec[I_FOOD] -= (int)food_eaten;