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, struct natstr *np, int *bp, int build);
65 prod_land(int etus, int natnum, int *bp, int build)
66 /* build = 1, maintain = 0 */
68 register struct lndstr *lp;
72 extern long lnd_money[MAXNOC];
74 int lastx = 9999, lasty = 9999;
76 bp_enable_cachepath();
77 for (n=0; NULL != (lp = getlandp(n)); n++) {
80 if (lp->lnd_own != natnum)
83 sp = getsectp(lp->lnd_x,lp->lnd_y);
84 if (sp->sct_type == SCT_SANCT)
86 if (lastx == 9999 || lasty == 9999) {
90 if (lastx != lp->lnd_x || lasty != lp->lnd_y) {
92 bp_disable_cachepath();
94 bp_enable_cachepath();
96 np = getnatp(lp->lnd_own);
97 start_money = np->nat_money;
98 upd_land(lp, n, etus, np, bp, build);
99 lnd_money[lp->lnd_own] += np->nat_money - start_money;
100 if ((build && (np->nat_money != start_money)) || (!build))
102 if (player->simulation)
103 np->nat_money = start_money;
105 bp_disable_cachepath();
106 bp_clear_cachepath();
112 upd_land(register struct lndstr *lp, int landno, register int etus, struct natstr *np, int *bp, int build)
113 /* build = 1, maintain = 0 */
115 extern int morale_base;
120 double techfact(int, double);
121 int min = morale_base-(int)np->nat_level[NAT_HLEV];
123 extern double money_land;
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 ||
138 if (lp->lnd_effic < LAND_MINEFF ||
139 !(landrepair(lp,vec,np,bp,etus))){
140 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
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",
158 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
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 ?
182 ", killing it" : ""));
184 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
187 * do plague stuff. plague can't break out on land units,
188 * but it can still kill people on them.
190 getvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
191 if (cvec[C_PSTAGE] > 0) {
192 n = plague_people(np, vec, cvec, etus);
196 "PLAGUE deaths reported on %s\n",
198 nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
201 wu(0, lp->lnd_own, "%s battling PLAGUE\n",
205 /* Are we still incubating? */
206 if (n == cvec[C_PSTAGE]) {
207 /* Yes. Will it turn "infectious" next time? */
208 if (cvec[C_PTIME] <= etus) {
209 /* Yes. Report an outbreak. */
211 "Outbreak of PLAGUE on %s!\n",
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",
230 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
237 putvec(VT_COND, cvec, (s_char *)lp, EF_LAND);
239 putvec(VT_ITEM, vec, (s_char *)lp, EF_LAND);
240 } /* end !player->simulation */
246 landrepair(register struct lndstr *land, int *vec, struct natstr *np, int *bp, int etus)
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 bzero((s_char *)mvec, 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 bzero((s_char *)mvec, 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, int doit)
373 extern double eatrate;
374 double food_eaten, ship_eaten;
376 int can_eat, need, svec[I_MAX+1];
382 if (opt_NOFOOD) 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;
405 else if ((svec[I_FOOD]-ship_eaten) > 0){
406 vec[I_FOOD] += (svec[I_FOOD] - ship_eaten);
407 svec[I_FOOD] -= (svec[I_FOOD] - ship_eaten);
409 putvec(VT_ITEM, svec, (s_char *)sp, EF_SHIP);
412 if (food_eaten > vec[I_FOOD]) {
413 *needed = food_eaten - vec[I_FOOD];
414 if (*needed < (food_eaten - vec[I_FOOD]))
416 can_eat = (vec[I_FOOD] / (etus * eatrate));
417 total_people = total_mil(lp);
418 /* only want to starve off at most 1/2 the populace. */
419 if (can_eat < (total_people/2))
420 can_eat = total_people/2;
422 people_left = (vec[I_FOOD] + 0.01) / (food_eaten + 0.01);
423 /* only want to starve off at most 1/2 the populace. */
424 if (people_left < 0.5)
426 /* lp->lnd_effic *= people_left;*/
427 starved = vec[I_MILIT] - (vec[I_MILIT] * people_left);
428 /* if (!player->simulation)
429 wu(0, lp->lnd_own, "%d mil starved on unit %s.\n",
432 vec[I_MILIT] -= starved;
435 vec[I_FOOD] -= (int)food_eaten;