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, int landno, 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;
75 extern long lnd_money[MAXNOC];
77 int lastx = 9999, lasty = 9999;
79 bp_enable_cachepath();
80 for (n = 0; NULL != (lp = getlandp(n)); n++) {
83 if (lp->lnd_own != natnum)
86 sp = getsectp(lp->lnd_x, lp->lnd_y);
87 if (sp->sct_type == SCT_SANCT)
89 if (lastx == 9999 || lasty == 9999) {
93 if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
95 bp_disable_cachepath();
97 bp_enable_cachepath();
99 np = getnatp(lp->lnd_own);
100 start_money = np->nat_money;
101 upd_land(lp, n, etus, np, bp, build);
102 lnd_money[lp->lnd_own] += np->nat_money - start_money;
103 if ((build && (np->nat_money != start_money)) || (!build))
105 if (player->simulation)
106 np->nat_money = start_money;
108 bp_disable_cachepath();
109 bp_clear_cachepath();
115 upd_land(register struct lndstr *lp, int landno, register int etus,
116 struct natstr *np, int *bp, int build)
117 /* build = 1, maintain = 0 */
119 extern int morale_base;
124 double techfact(int, double);
125 int min = morale_base - (int)np->nat_level[NAT_HLEV];
127 extern double money_land;
132 if (!player->simulation)
133 if (lp->lnd_retreat < min)
134 lp->lnd_retreat = min;
136 lcp = &lchr[(int)lp->lnd_type];
137 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
139 if (np->nat_priorities[PRI_LBUILD] == 0 || np->nat_money < 0)
141 if (lp->lnd_effic < LAND_MINEFF ||
142 !(landrepair(lp, vec, np, bp, etus))) {
143 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
150 if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
152 if (lcp->l_flags & L_ENGINEER)
154 /* cost = -(mult * etus * dmin(0.0, money_land * LND_COST(lcp->l_cost, lp->lnd_tech - lcp->l_tech)));*/
155 cost = -(mult * etus * dmin(0.0, money_land * lcp->l_cost));
156 if ((np->nat_priorities[PRI_LMAINT] == 0 ||
157 np->nat_money < cost) && !player->simulation) {
158 if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
160 "%s lost to lack of maintenance\n", prland(lp));
161 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
167 "%s lost %d%% to lack of maintenance\n",
168 prland(lp), lp->lnd_effic - eff);
171 np->nat_money -= cost;
173 /* Mil costs are now part of regular mil costs, not maint costs */
174 /* np->nat_money += (int) (money_mil * etus * lnd_getmil(lp));*/
176 /* Grab more stuff */
177 if ((opt_NOFOOD == 0) && !player->simulation)
178 resupply_commod(lp, I_FOOD);
180 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
181 if (!player->simulation) {
182 if ((n = feed_land(lp, vec, etus, &needed, 1)) > 0) {
183 wu(0, lp->lnd_own, "%d starved in %s%s\n",
185 (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
187 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
190 * do plague stuff. plague can't break out on land units,
191 * but it can still kill people on them.
193 getvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
194 if (cvec[C_PSTAGE] > 0) {
195 n = plague_people(np, vec, cvec, etus);
199 "PLAGUE deaths reported on %s\n", prland(lp));
200 nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
203 wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
206 /* Are we still incubating? */
207 if (n == cvec[C_PSTAGE]) {
208 /* Yes. Will it turn "infectious" next time? */
209 if (cvec[C_PTIME] <= etus) {
210 /* Yes. Report an outbreak. */
212 "Outbreak of PLAGUE on %s!\n", prland(lp));
213 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
216 /* It has already moved on to "infectious" */
218 "%s battling PLAGUE\n", prland(lp));
222 /* Has the plague moved to "incubation" yet? */
223 if (n != cvec[C_PSTAGE]) {
224 /* Yes. Will it turn "infectious" next time? */
225 if (cvec[C_PTIME] <= etus) {
226 /* Yes. Report an outbreak. */
228 "Outbreak of PLAGUE on %s!\n", prland(lp));
229 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
236 putvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
238 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
239 } /* end !player->simulation */
245 landrepair(register struct lndstr *land, int *vec, struct natstr *np,
248 extern int mil_dbl_pay;
249 extern int land_grow_scale;
255 int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
262 lp = &lchr[(int)land->lnd_type];
263 sp = getsectp(land->lnd_x, land->lnd_y);
266 getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
268 if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
271 if (land->lnd_effic == 100) {
272 /* land is ok; no repairs needed */
275 if (sp->sct_own != land->lnd_own)
278 if (!player->simulation)
279 avail = sp->sct_avail * 100;
281 avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
283 w_p_eff = 20 + (lp->l_lcm + 2 * lp->l_hcm);
284 delta = roundavg((double)avail / w_p_eff);
287 if (delta > etus * land_grow_scale)
288 delta = etus * land_grow_scale;
290 /* delta is the max amount we can grow */
292 left = 100 - land->lnd_effic;
296 leftp = ((float)left / 100.0);
298 memset(mvec, 0, sizeof(mvec));
299 mvec[I_LCM] = lcm_needed = ldround((double)(lp->l_lcm * leftp), 1);
300 mvec[I_HCM] = hcm_needed = ldround((double)(lp->l_hcm * leftp), 1);
302 mvec[I_GUN] = gun_needed = ldround((double)(lp->l_gun * leftp),1);
303 mvec[I_MILIT] = mil_needed = ldround((double)(lp->l_mil * leftp),1);
304 mvec[I_SHELL] = shell_needed = ldround((double)(lp->l_shell *leftp),1);
306 mvec[I_GUN] = gun_needed = 0;
307 mvec[I_MILIT] = mil_needed = 0;
308 mvec[I_SHELL] = shell_needed = 0;
310 get_materials(sp, bp, mvec, 0);
312 if (mvec[I_MILIT] >= mil_needed)
315 buildp = ((float)mvec[I_MILIT] / (float)lp->l_mil);
316 if (mvec[I_LCM] < lcm_needed)
317 buildp = MIN(buildp, ((float)mvec[I_LCM] / (float)lp->l_lcm));
318 if (mvec[I_HCM] < hcm_needed)
319 buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)lp->l_hcm));
320 if (mvec[I_GUN] < gun_needed)
321 buildp = MIN(buildp, ((float)mvec[I_GUN] / (float)lp->l_gun));
322 if (mvec[I_SHELL] < shell_needed)
323 buildp = MIN(buildp, ((float)mvec[I_SHELL] / (float)lp->l_shell));
325 build = ldround((double)(buildp * 100.0), 1);
327 memset(mvec, 0, sizeof(mvec));
328 mvec[I_LCM] = lcm_needed = roundavg((double)(lp->l_lcm * buildp));
329 mvec[I_HCM] = hcm_needed = roundavg((double)(lp->l_hcm * buildp));
331 mvec[I_GUN] = gun_needed = roundavg((double)(lp->l_gun * buildp));
332 mvec[I_MILIT] = mil_needed = roundavg((double)(lp->l_mil * buildp));
333 mvec[I_SHELL] = shell_needed = roundavg((double)(lp->l_shell *buildp));
335 mvec[I_GUN] = gun_needed = 0;
336 mvec[I_MILIT] = mil_needed = 0;
337 mvec[I_SHELL] = shell_needed = 0;
338 mil_dbl_pay += mil_needed;
340 get_materials(sp, bp, mvec, 1);
342 if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
345 avail -= build * w_p_eff;
346 if (!player->simulation) {
347 sp->sct_avail = avail / 100;
348 if (sp->sct_avail < 0)
351 pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
352 if (gt_bg_nmbr(bp, sp, I_MAX + 1) < 0)
353 pt_bg_nmbr(bp, sp, I_MAX + 1, 0);
357 logerror("land unit %d building %d ! \n", land->lnd_uid, build);
358 np->nat_money -= mult * lp->l_cost * build / 100.0;
359 if (!player->simulation) {
360 land->lnd_effic += (s_char)build;
368 * returns the number who starved, if any.
371 feed_land(struct lndstr *lp, register int *vec, int etus, int *needed,
374 extern double eatrate;
375 double food_eaten, ship_eaten;
377 int can_eat, need, svec[I_MAX + 1];
384 return 0; /* no food no work to be done */
386 lcp = &lchr[(int)lp->lnd_type];
388 food_eaten = (etus * eatrate) * total_mil(lp);
392 * If we're on a ship, and we don't have enough food,
393 * get some food off the carrying ship. (Don't starve
396 /* doit - Only try to take food off the ship during the update */
397 if ((food_eaten > vec[I_FOOD]) && (lp->lnd_ship >= 0) && doit) {
398 need = (int)food_eaten - vec[I_FOOD];
399 sp = getshipp(lp->lnd_ship);
400 getvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
401 ship_eaten = (etus * eatrate) *
402 (svec[I_CIVIL] + svec[I_MILIT] + svec[I_UW]);
403 if ((svec[I_FOOD] - need) > ship_eaten) {
405 svec[I_FOOD] -= need;
406 } else if ((svec[I_FOOD] - ship_eaten) > 0) {
407 vec[I_FOOD] += (svec[I_FOOD] - ship_eaten);
408 svec[I_FOOD] -= (svec[I_FOOD] - ship_eaten);
410 putvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
413 if (food_eaten > vec[I_FOOD]) {
414 *needed = food_eaten - vec[I_FOOD];
415 if (*needed < (food_eaten - vec[I_FOOD]))
417 can_eat = (vec[I_FOOD] / (etus * eatrate));
418 total_people = total_mil(lp);
419 /* only want to starve off at most 1/2 the populace. */
420 if (can_eat < (total_people / 2))
421 can_eat = total_people / 2;
423 people_left = (vec[I_FOOD] + 0.01) / (food_eaten + 0.01);
424 /* only want to starve off at most 1/2 the populace. */
425 if (people_left < 0.5)
427 /* lp->lnd_effic *= people_left;*/
428 starved = vec[I_MILIT] - (vec[I_MILIT] * people_left);
429 /* if (!player->simulation)
430 wu(0, lp->lnd_own, "%d mil starved on unit %s.\n",
433 vec[I_MILIT] -= starved;
436 vec[I_FOOD] -= (int)food_eaten;