2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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:
33 * Markus Armbruster, 2004-2009
49 * Increase sector efficiency if old type == new type.
50 * decrease sector efficiency if old type != new type.
51 * Return amount of work used.
54 upd_buildeff(struct natstr *np, struct sctstr *sp, int *workp,
55 short *vec, int etu, int *desig, int sctwork, int *cost)
58 int buildeff_work = *workp / 2;
59 int n, hcms, lcms, neweff;
60 unsigned char old_type = *desig;
63 neweff = sp->sct_effic;
65 if (*desig != sp->sct_newtype) {
67 * Tear down existing sector.
68 * Easier to destroy than to build.
70 work_cost = (sp->sct_effic + 3) / 4;
71 if (work_cost > buildeff_work)
72 work_cost = buildeff_work;
73 buildeff_work -= work_cost;
74 n = sp->sct_effic - work_cost * 4;
77 *desig = sp->sct_newtype;
81 if (!n && IS_BIG_CITY(old_type) &&
82 !IS_BIG_CITY(*desig)) {
83 // FIXME use trunc_people() and total_work()
84 int maxpop = max_population(np->nat_level[NAT_RLEV], *desig, n);
85 if (vec[I_CIVIL] > maxpop)
86 vec[I_CIVIL] = maxpop;
87 if (vec[I_UW] > maxpop)
89 *workp = (vec[I_CIVIL] * sctwork) / 100.0
90 + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
91 *workp = roundavg((etu * *workp) / 100.0);
93 buildeff_work = MIN((int)(*workp / 2), buildeff_work);
96 if (*desig == sp->sct_newtype) {
97 work_cost = 100 - neweff;
98 if (work_cost > buildeff_work)
99 work_cost = buildeff_work;
101 if (dchr[*desig].d_lcms > 0) {
103 lcms /= dchr[*desig].d_lcms;
104 if (work_cost > lcms)
107 if (dchr[*desig].d_hcms > 0) {
109 hcms /= dchr[*desig].d_hcms;
110 if (work_cost > hcms)
115 *cost += work_cost * dchr[*desig].d_build;
116 buildeff_work -= work_cost;
118 if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
119 vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
120 vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
123 *workp = (*workp + 1) / 2 + buildeff_work;
129 * enlistment sectors are special; they require military
130 * to convert civ into mil in large numbers.
131 * Conversion will happen much more slowly without
132 * some mil initially.
135 enlist(short *vec, int etu, int *cost)
140 /* Need to check treaties here */
142 maxmil = vec[I_CIVIL] / 2 - vec[I_MILIT];
144 enlisted = etu * (10 + vec[I_MILIT]) * 0.05;
145 if (enlisted > maxmil)
147 vec[I_CIVIL] -= enlisted;
148 vec[I_MILIT] += enlisted;
150 *cost = enlisted * 3;
154 /* Fallout is calculated here. */
157 meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
163 for (n = I_NONE + 1; n <= I_MAX; n++) {
164 melt = roundavg(vec[n] * etus * (long)fallout
165 / (1000.0 * ichr[n].i_melt_denom));
168 if (melt > 5 && own) {
169 if (type == EF_SECTOR)
170 wu(0, own, "Lost %d %s to radiation in %s.\n",
171 melt, ichr[n].i_name,
173 else if (type == EF_LAND)
174 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
175 uid, melt, ichr[n].i_name,
177 else if (type == EF_SHIP)
178 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
179 uid, melt, ichr[n].i_name,
187 * Do fallout meltdown for sector SP.
188 * ETUS above 24 are treated as 24 to avoid *huge* kill offs in
192 do_fallout(struct sctstr *sp, int etus)
198 /* This check shouldn't be needed, but just in case. :) */
199 if (!sp->sct_fallout || !sp->sct_updated)
203 meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item, EF_SECTOR,
204 sp->sct_x, sp->sct_y, 0);
205 for (i = 0; NULL != (lp = getlandp(i)); i++) {
208 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
210 meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item, EF_LAND,
211 lp->lnd_x, lp->lnd_y, lp->lnd_uid);
213 for (i = 0; NULL != (spp = getshipp(i)); i++) {
216 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
218 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
220 meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, EF_SHIP,
221 spp->shp_x, spp->shp_y, spp->shp_uid);
226 spread_fallout(struct sctstr *sp, int etus)
234 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
235 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
236 if (ap->sct_type == SCT_SANCT)
238 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
241 ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
246 decay_fallout(struct sctstr *sp, int etus)
252 decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
253 (double)etus * (double)sp->sct_fallout);
255 sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
259 * Produce for a specific nation
262 produce_sect(int natnum, int etu, struct bp *bp, long p_sect[][2])
266 short buf[I_MAX + 1];
268 int work, cost, ecost, pcost, sctwork;
269 int n, desig, neweff, amount;
271 for (n = 0; NULL != (sp = getsectid(n)); n++) {
272 if (sp->sct_type == SCT_WATER)
274 if (sp->sct_own != natnum)
276 if (sp->sct_updated != 0)
279 np = getnatp(natnum);
281 if (player->simulation) {
282 /* work on a copy, which will be discarded */
283 memcpy(buf, sp->sct_item, sizeof(buf));
288 /* If everybody is dead, the sector reverts to unowned.
289 * This is also checked at the end of the production in
290 * they all starved or were plagued off.
292 if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
293 !has_units(sp->sct_x, sp->sct_y, sp->sct_own, NULL)) {
294 if (!player->simulation) {
295 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
305 sctwork = do_feed(sp, np, vec, &work, etu);
306 bp_put_items(bp, sp, vec);
308 if (sp->sct_off || np->nat_money < 0)
311 neweff = sp->sct_effic;
313 pcost = cost = ecost = 0;
315 desig = sp->sct_type;
317 if (dchr[desig].d_maint) {
318 cost = etu * dchr[desig].d_maint;
319 p_sect[SCT_MAINT][0]++;
320 p_sect[SCT_MAINT][1] += cost;
321 if (!player->simulation)
322 np->nat_money -= cost;
325 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
326 np->nat_money >= 0) {
327 neweff = upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
329 bp_put_items(bp, sp, vec);
330 p_sect[SCT_EFFIC][0]++;
331 p_sect[SCT_EFFIC][1] += cost;
332 if (!player->simulation) {
333 np->nat_money -= cost;
334 sp->sct_type = desig;
335 sp->sct_effic = neweff;
339 if (desig == SCT_ENLIST && neweff >= 60 &&
340 sp->sct_own == sp->sct_oldown) {
341 p_sect[desig][0] += enlist(vec, etu, &ecost);
342 p_sect[desig][1] += ecost;
343 if (!player->simulation)
344 np->nat_money -= ecost;
345 bp_put_items(bp, sp, vec);
349 * now do the production (if sector effic >= 60%)
353 if (np->nat_money >= 0 && dchr[desig].d_prd >= 0)
354 work -= produce(np, sp, vec, work, desig, neweff,
356 bp_put_items(bp, sp, vec);
359 bp_put_avail(bp, sp, work);
360 p_sect[desig][0] += amount;
361 p_sect[desig][1] += pcost;
362 if (!player->simulation) {
363 sp->sct_avail = work;
364 np->nat_money -= pcost;