2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * sect.c: Do production for sectors
30 * Known contributors to this file:
57 * Increase sector efficiency if old type == new type.
58 * decrease sector efficiency if old type != new type.
59 * Return amount of work used.
62 upd_buildeff(struct natstr *np, struct sctstr *sp, int *workp,
63 short *vec, int etu, int *desig, int sctwork, int *cost)
66 int buildeff_work = *workp / 2;
67 int n, hcms, lcms, neweff;
68 unsigned char old_type = *desig;
71 neweff = sp->sct_effic;
73 if (*desig != sp->sct_newtype) {
75 * Tear down existing sector.
76 * Easier to destroy than to build.
78 work_cost = (sp->sct_effic + 3) / 4;
79 if (work_cost > buildeff_work)
80 work_cost = buildeff_work;
81 buildeff_work -= work_cost;
82 n = sp->sct_effic - work_cost * 4;
85 *desig = sp->sct_newtype;
89 if (!n && IS_BIG_CITY(old_type) &&
90 !IS_BIG_CITY(*desig)) {
91 int maxpop = max_population(np->nat_level[NAT_RLEV], *desig, n);
92 if (vec[I_CIVIL] > maxpop)
93 vec[I_CIVIL] = maxpop;
94 if (vec[I_UW] > maxpop)
96 *workp = (vec[I_CIVIL] * sctwork) / 100.0
97 + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
98 *workp = roundavg((etu * *workp) / 100.0);
100 buildeff_work = MIN((int)(*workp / 2), buildeff_work);
103 if (*desig == sp->sct_newtype) {
104 work_cost = 100 - neweff;
105 if (work_cost > buildeff_work)
106 work_cost = buildeff_work;
108 if (dchr[*desig].d_lcms > 0) {
110 lcms /= dchr[*desig].d_lcms;
111 if (work_cost > lcms)
114 if (dchr[*desig].d_hcms > 0) {
116 hcms /= dchr[*desig].d_hcms;
117 if (work_cost > hcms)
122 *cost += work_cost * dchr[*desig].d_build;
123 buildeff_work -= work_cost;
125 if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
126 vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
127 vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
130 *workp = (*workp + 1) / 2 + buildeff_work;
136 * enlistment sectors are special; they require military
137 * to convert civ into mil in large numbers.
138 * Conversion will happen much more slowly without
139 * some mil initially.
142 enlist(short *vec, int etu, int *cost)
147 /* Need to check treaties here */
149 maxmil = (vec[I_CIVIL] / 2) - vec[I_MILIT];
151 enlisted = (etu * (10 + vec[I_MILIT]) * 0.05);
152 if (enlisted > maxmil)
154 vec[I_CIVIL] -= enlisted;
155 vec[I_MILIT] += enlisted;
157 *cost = enlisted * 3;
161 /* Fallout is calculated here. */
164 meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
170 for (n = I_NONE + 1; n <= I_MAX; n++) {
171 melt = roundavg(vec[n] * etus * (long)fallout
172 / (1000.0 * ichr[n].i_melt_denom));
175 if (melt > 5 && own) {
176 if (type == EF_SECTOR)
177 wu(0, own, "Lost %d %s to radiation in %s.\n",
178 melt, ichr[n].i_name,
180 else if (type == EF_LAND)
181 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
182 uid, melt, ichr[n].i_name,
184 else if (type == EF_SHIP)
185 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
186 uid, melt, ichr[n].i_name,
194 * Do fallout meltdown for sector SP.
195 * ETUS above 24 are treated as 24 to avoid *huge* kill offs in
199 do_fallout(struct sctstr *sp, int etus)
205 /* This check shouldn't be needed, but just in case. :) */
206 if (!sp->sct_fallout || !sp->sct_updated)
210 meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item, EF_SECTOR,
211 sp->sct_x, sp->sct_y, 0);
212 for (i = 0; NULL != (lp = getlandp(i)); i++) {
215 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
217 meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item, EF_LAND,
218 lp->lnd_x, lp->lnd_y, lp->lnd_uid);
220 for (i = 0; NULL != (spp = getshipp(i)); i++) {
223 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
225 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
227 meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, EF_SHIP,
228 spp->shp_x, spp->shp_y, spp->shp_uid);
233 spread_fallout(struct sctstr *sp, int etus)
241 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
242 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
243 if (ap->sct_type == SCT_SANCT)
245 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
248 ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
253 decay_fallout(struct sctstr *sp, int etus)
259 decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
260 (double)etus * (double)sp->sct_fallout);
262 sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
266 * Produce only a set sector type for a specific nation
269 produce_sect(int natnum, int etu, int *bp, long p_sect[][2])
273 short buf[I_MAX + 1];
275 int work, cost, ecost, pcost, sctwork;
276 int n, desig, maxpop, neweff, amount;
278 for (n = 0; NULL != (sp = getsectid(n)); n++) {
279 if (sp->sct_type == SCT_WATER)
281 if (sp->sct_own != natnum)
283 if (sp->sct_updated != 0)
286 if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
287 p_sect[SCT_CAPIT][0]++;
288 p_sect[SCT_CAPIT][1] += etu;
291 if (player->simulation) {
292 /* work on a copy, which will be discarded */
293 memcpy(buf, sp->sct_item, sizeof(buf));
298 /* If everybody is dead, the sector reverts to unowned.
299 * This is also checked at the end of the production in
300 * they all starved or were plagued off.
302 if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
303 !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
304 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
313 np = getnatp(natnum);
315 /* do_feed trys to supply. So, we need to enable cacheing
317 bp_enable_cachepath();
319 sctwork = do_feed(sp, np, vec, &work, bp, etu);
321 bp_disable_cachepath();
322 bp_clear_cachepath();
324 if (sp->sct_off || np->nat_money < 0) {
325 if (!player->simulation)
330 neweff = sp->sct_effic;
332 pcost = cost = ecost = 0;
334 desig = sp->sct_type;
336 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
338 neweff = upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
340 pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
341 pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
342 p_sect[SCT_EFFIC][0]++;
343 p_sect[SCT_EFFIC][1] += cost;
344 if (!player->simulation) {
345 np->nat_money -= cost;
346 sp->sct_type = desig;
347 sp->sct_effic = neweff;
351 if (desig == SCT_ENLIST && neweff >= 60 &&
352 sp->sct_own == sp->sct_oldown) {
353 p_sect[desig][0] += enlist(vec, etu, &ecost);
354 p_sect[desig][1] += ecost;
355 if (!player->simulation)
356 np->nat_money -= ecost;
360 * now do the production (if sector effic >= 60%)
364 if (np->nat_money > 0 && dchr[desig].d_prd)
365 work -= produce(np, sp, vec, work, desig, neweff,
369 pt_bg_nmbr(bp, sp, I_MAX + 1, work);
370 p_sect[desig][0] += amount;
371 p_sect[desig][1] += pcost;
372 if (!player->simulation) {
373 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
374 if (vec[I_CIVIL] > maxpop)
375 vec[I_CIVIL] = maxpop;
376 if (vec[I_UW] > maxpop)
378 sp->sct_avail = work;
379 np->nat_money -= pcost;