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 void upd_land(register struct lndstr *lp, int landno, register int etus,
63 struct natstr *np, int *bp, int build);
66 prod_land(int etus, int natnum, int *bp, int build)
67 /* build = 1, maintain = 0 */
69 register struct lndstr *lp;
73 extern long lnd_money[MAXNOC];
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, n, 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(register struct lndstr *lp, int landno, register int etus,
114 struct natstr *np, int *bp, int build)
115 /* build = 1, maintain = 0 */
117 extern int morale_base;
122 double techfact(int, double);
123 int min = morale_base - (int)np->nat_level[NAT_HLEV];
125 extern double money_land;
130 if (!player->simulation)
131 if (lp->lnd_retreat < min)
132 lp->lnd_retreat = min;
134 lcp = &lchr[(int)lp->lnd_type];
135 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
137 if (np->nat_priorities[PRI_LBUILD] == 0 || np->nat_money < 0)
139 if (lp->lnd_effic < LAND_MINEFF ||
140 !(landrepair(lp, vec, np, bp, etus))) {
141 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
148 if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
150 if (lcp->l_flags & L_ENGINEER)
152 /* cost = -(mult * etus * dmin(0.0, money_land * LND_COST(lcp->l_cost, lp->lnd_tech - lcp->l_tech)));*/
153 cost = -(mult * etus * dmin(0.0, money_land * lcp->l_cost));
154 if ((np->nat_priorities[PRI_LMAINT] == 0 ||
155 np->nat_money < cost) && !player->simulation) {
156 if ((eff = lp->lnd_effic - etus / 5) < LAND_MINEFF) {
158 "%s lost to lack of maintenance\n", prland(lp));
159 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x,
165 "%s lost %d%% to lack of maintenance\n",
166 prland(lp), lp->lnd_effic - eff);
169 np->nat_money -= cost;
171 /* Mil costs are now part of regular mil costs, not maint costs */
172 /* np->nat_money += (int) (money_mil * etus * lnd_getmil(lp));*/
174 /* Grab more stuff */
175 if ((opt_NOFOOD == 0) && !player->simulation)
176 resupply_commod(lp, I_FOOD);
178 getvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
179 if (!player->simulation) {
180 if ((n = feed_land(lp, vec, etus, &needed, 1)) > 0) {
181 wu(0, lp->lnd_own, "%d starved in %s%s\n",
183 (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
185 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
188 * do plague stuff. plague can't break out on land units,
189 * but it can still kill people on them.
191 getvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
192 if (cvec[C_PSTAGE] > 0) {
193 n = plague_people(np, vec, cvec, etus);
197 "PLAGUE deaths reported on %s\n", prland(lp));
198 nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
201 wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
204 /* Are we still incubating? */
205 if (n == cvec[C_PSTAGE]) {
206 /* Yes. Will it turn "infectious" next time? */
207 if (cvec[C_PTIME] <= etus) {
208 /* Yes. Report an outbreak. */
210 "Outbreak of PLAGUE on %s!\n", prland(lp));
211 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
214 /* It has already moved on to "infectious" */
216 "%s battling PLAGUE\n", prland(lp));
220 /* Has the plague moved to "incubation" yet? */
221 if (n != cvec[C_PSTAGE]) {
222 /* Yes. Will it turn "infectious" next time? */
223 if (cvec[C_PTIME] <= etus) {
224 /* Yes. Report an outbreak. */
226 "Outbreak of PLAGUE on %s!\n", prland(lp));
227 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
234 putvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
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,
246 extern int mil_dbl_pay;
247 extern int land_grow_scale;
253 int mil_needed, lcm_needed, hcm_needed, gun_needed, shell_needed;
260 lp = &lchr[(int)land->lnd_type];
261 sp = getsectp(land->lnd_x, land->lnd_y);
264 getvec(VT_ITEM, svec, (s_char *)sp, EF_SECTOR);
266 if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
269 if (land->lnd_effic == 100) {
270 /* land is ok; no repairs needed */
273 if (sp->sct_own != land->lnd_own)
276 if (!player->simulation)
277 avail = sp->sct_avail * 100;
279 avail = gt_bg_nmbr(bp, sp, I_MAX + 1) * 100;
281 w_p_eff = 20 + (lp->l_lcm + 2 * lp->l_hcm);
282 delta = roundavg((double)avail / w_p_eff);
285 if (delta > etus * land_grow_scale)
286 delta = etus * land_grow_scale;
288 /* delta is the max amount we can grow */
290 left = 100 - land->lnd_effic;
294 leftp = ((float)left / 100.0);
296 bzero((s_char *)mvec, sizeof(mvec));
297 mvec[I_LCM] = lcm_needed = ldround((double)(lp->l_lcm * leftp), 1);
298 mvec[I_HCM] = hcm_needed = ldround((double)(lp->l_hcm * leftp), 1);
300 mvec[I_GUN] = gun_needed = ldround((double)(lp->l_gun * leftp),1);
301 mvec[I_MILIT] = mil_needed = ldround((double)(lp->l_mil * leftp),1);
302 mvec[I_SHELL] = shell_needed = ldround((double)(lp->l_shell *leftp),1);
304 mvec[I_GUN] = gun_needed = 0;
305 mvec[I_MILIT] = mil_needed = 0;
306 mvec[I_SHELL] = shell_needed = 0;
308 get_materials(sp, bp, mvec, 0);
310 if (mvec[I_MILIT] >= mil_needed)
313 buildp = ((float)mvec[I_MILIT] / (float)lp->l_mil);
314 if (mvec[I_LCM] < lcm_needed)
315 buildp = MIN(buildp, ((float)mvec[I_LCM] / (float)lp->l_lcm));
316 if (mvec[I_HCM] < hcm_needed)
317 buildp = MIN(buildp, ((float)mvec[I_HCM] / (float)lp->l_hcm));
318 if (mvec[I_GUN] < gun_needed)
319 buildp = MIN(buildp, ((float)mvec[I_GUN] / (float)lp->l_gun));
320 if (mvec[I_SHELL] < shell_needed)
321 buildp = MIN(buildp, ((float)mvec[I_SHELL] / (float)lp->l_shell));
323 build = ldround((double)(buildp * 100.0), 1);
325 bzero((s_char *)mvec, sizeof(mvec));
326 mvec[I_LCM] = lcm_needed = roundavg((double)(lp->l_lcm * buildp));
327 mvec[I_HCM] = hcm_needed = roundavg((double)(lp->l_hcm * buildp));
329 mvec[I_GUN] = gun_needed = roundavg((double)(lp->l_gun * buildp));
330 mvec[I_MILIT] = mil_needed = roundavg((double)(lp->l_mil * buildp));
331 mvec[I_SHELL] = shell_needed = roundavg((double)(lp->l_shell *buildp));
333 mvec[I_GUN] = gun_needed = 0;
334 mvec[I_MILIT] = mil_needed = 0;
335 mvec[I_SHELL] = shell_needed = 0;
336 mil_dbl_pay += mil_needed;
338 get_materials(sp, bp, mvec, 1);
340 if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
343 avail -= build * w_p_eff;
344 if (!player->simulation) {
345 sp->sct_avail = avail / 100;
346 if (sp->sct_avail < 0)
349 pt_bg_nmbr(bp, sp, I_MAX + 1, avail / 100);
350 if (gt_bg_nmbr(bp, sp, I_MAX + 1) < 0)
351 pt_bg_nmbr(bp, sp, I_MAX + 1, 0);
355 logerror("land unit %d building %d ! \n", land->lnd_uid, build);
356 np->nat_money -= mult * lp->l_cost * build / 100.0;
357 if (!player->simulation) {
358 land->lnd_effic += (s_char)build;
366 * returns the number who starved, if any.
369 feed_land(struct lndstr *lp, register int *vec, int etus, int *needed,
372 extern double eatrate;
373 double food_eaten, ship_eaten;
375 int can_eat, need, svec[I_MAX + 1];
382 return 0; /* no food no work to be done */
384 lcp = &lchr[(int)lp->lnd_type];
386 food_eaten = (etus * eatrate) * total_mil(lp);
390 * If we're on a ship, and we don't have enough food,
391 * get some food off the carrying ship. (Don't starve
394 /* doit - Only try to take food off the ship during the update */
395 if ((food_eaten > vec[I_FOOD]) && (lp->lnd_ship >= 0) && doit) {
396 need = (int)food_eaten - vec[I_FOOD];
397 sp = getshipp(lp->lnd_ship);
398 getvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
399 ship_eaten = (etus * eatrate) *
400 (svec[I_CIVIL] + svec[I_MILIT] + svec[I_UW]);
401 if ((svec[I_FOOD] - need) > ship_eaten) {
403 svec[I_FOOD] -= need;
404 } else if ((svec[I_FOOD] - ship_eaten) > 0) {
405 vec[I_FOOD] += (svec[I_FOOD] - ship_eaten);
406 svec[I_FOOD] -= (svec[I_FOOD] - ship_eaten);
408 putvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
411 if (food_eaten > vec[I_FOOD]) {
412 *needed = food_eaten - vec[I_FOOD];
413 if (*needed < (food_eaten - vec[I_FOOD]))
415 can_eat = (vec[I_FOOD] / (etus * eatrate));
416 total_people = total_mil(lp);
417 /* only want to starve off at most 1/2 the populace. */
418 if (can_eat < (total_people / 2))
419 can_eat = total_people / 2;
421 people_left = (vec[I_FOOD] + 0.01) / (food_eaten + 0.01);
422 /* only want to starve off at most 1/2 the populace. */
423 if (people_left < 0.5)
425 /* lp->lnd_effic *= people_left;*/
426 starved = vec[I_MILIT] - (vec[I_MILIT] * people_left);
427 /* if (!player->simulation)
428 wu(0, lp->lnd_own, "%d mil starved on unit %s.\n",
431 vec[I_MILIT] -= starved;
434 vec[I_FOOD] -= (int)food_eaten;