2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * land.c: Do production for land units
29 * Known contributors to this file:
31 * Thomas Ruschak, 1992
33 * Markus Armbruster, 2006-2011
47 static void landrepair(struct lndstr *, struct natstr *, struct bp *, int);
48 static void upd_land(struct lndstr *, int, struct natstr *, struct bp *, int);
49 static int feed_land(struct lndstr *, int);
52 prod_land(int etus, int natnum, struct bp *bp, int build)
53 /* build = 1, maintain = 0 */
61 for (n = 0; NULL != (lp = getlandp(n)); n++) {
64 if (lp->lnd_own != natnum)
66 if (lp->lnd_effic < LAND_MINEFF) {
67 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
68 lp->lnd_x, lp->lnd_y);
73 sp = getsectp(lp->lnd_x, lp->lnd_y);
74 if (sp->sct_type == SCT_SANCT)
76 np = getnatp(lp->lnd_own);
77 start_money = np->nat_money;
78 upd_land(lp, etus, np, bp, build);
79 lnd_money[lp->lnd_own] += np->nat_money - start_money;
80 if (!build || np->nat_money != start_money)
82 if (player->simulation)
83 np->nat_money = start_money;
90 upd_land(struct lndstr *lp, int etus,
91 struct natstr *np, struct bp *bp, int build)
92 /* build = 1, maintain = 0 */
96 int min = morale_base - (int)np->nat_level[NAT_HLEV];
97 int n, mult, cost, eff_lost;
99 if (!player->simulation)
100 if (lp->lnd_retreat < min)
101 lp->lnd_retreat = min;
103 lcp = &lchr[(int)lp->lnd_type];
105 if (!lp->lnd_off && np->nat_money >= 0)
106 landrepair(lp, np, bp, etus);
107 if (!player->simulation)
111 if (np->nat_level[NAT_TLEV] < lp->lnd_tech * 0.85)
113 if (lcp->l_flags & L_ENGINEER)
115 cost = -(mult * etus * MIN(0.0, money_land * lcp->l_cost));
116 if (np->nat_money < cost && !player->simulation) {
118 if (lp->lnd_effic - eff_lost < LAND_MINEFF)
119 eff_lost = lp->lnd_effic - LAND_MINEFF;
121 wu(0, lp->lnd_own, "%s lost %d%% to lack of maintenance\n",
122 prland(lp), eff_lost);
123 lp->lnd_effic -= eff_lost;
126 np->nat_money -= cost;
129 if (!player->simulation) {
131 if ((n = feed_land(lp, etus)) > 0) {
132 wu(0, lp->lnd_own, "%d starved in %s%s\n",
134 (lp->lnd_effic < LAND_MINEFF ? ", killing it" : ""));
136 nreport(lp->lnd_own, N_DIE_FAMINE, 0, 1);
139 * do plague stuff. plague can't break out on land units,
140 * but it can still kill people on them.
142 pstage = lp->lnd_pstage;
143 ptime = lp->lnd_ptime;
144 if (pstage != PLG_HEALTHY) {
145 n = plague_people(np, lp->lnd_item, &pstage, &ptime, etus);
149 "PLAGUE deaths reported on %s\n", prland(lp));
150 nreport(lp->lnd_own, N_DIE_PLAGUE, 0, 1);
153 wu(0, lp->lnd_own, "%s battling PLAGUE\n", prland(lp));
156 /* Are we still incubating? */
158 /* Yes. Will it turn "infectious" next time? */
160 /* Yes. Report an outbreak. */
162 "Outbreak of PLAGUE on %s!\n", prland(lp));
163 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
166 /* It has already moved on to "infectious" */
168 "%s battling PLAGUE\n", prland(lp));
172 /* Has the plague moved to "incubation" yet? */
174 /* Yes. Will it turn "infectious" next time? */
176 /* Yes. Report an outbreak. */
178 "Outbreak of PLAGUE on %s!\n", prland(lp));
179 nreport(lp->lnd_own, N_OUT_PLAGUE, 0, 1);
186 lp->lnd_pstage = pstage;
187 lp->lnd_ptime = ptime;
189 } /* end !player->simulation */
194 landrepair(struct lndstr *land, struct natstr *np, struct bp *bp, int etus)
205 lp = &lchr[(int)land->lnd_type];
206 sp = getsectp(land->lnd_x, land->lnd_y);
210 if (np->nat_level[NAT_TLEV] < land->lnd_tech * 0.85)
213 if (land->lnd_effic == 100) {
214 /* land is ok; no repairs needed */
217 if (relations_with(sp->sct_own, land->lnd_own) != ALLIED)
220 if (!player->simulation)
221 avail = sp->sct_avail * 100;
223 avail = bp_get_avail(bp, sp) * 100;
225 w_p_eff = LND_BLD_WORK(lp->l_lcm, lp->l_hcm);
226 delta = roundavg((double)avail / w_p_eff);
229 if (delta > (int)((float)etus * land_grow_scale))
230 delta = (int)((float)etus * land_grow_scale);
231 if (delta > 100 - land->lnd_effic)
232 delta = 100 - land->lnd_effic;
234 memset(mvec, 0, sizeof(mvec));
235 mvec[I_LCM] = lp->l_lcm;
236 mvec[I_HCM] = lp->l_hcm;
237 build = get_materials(sp, bp, mvec, delta);
239 if ((sp->sct_type != SCT_HEADQ) && (sp->sct_type != SCT_FORTR))
242 avail -= build * w_p_eff;
245 if (!player->simulation)
246 sp->sct_avail = avail / 100;
248 bp_put_avail(bp, sp, avail / 100);
251 logerror("land unit %d building %d ! \n", land->lnd_uid, build);
252 np->nat_money -= mult * lp->l_cost * build / 100.0;
253 if (!player->simulation) {
254 land->lnd_effic += (signed char)build;
259 * returns the number who starved, if any.
262 feed_land(struct lndstr *lp, int etus)
264 return feed_people(lp->lnd_item, etus);