2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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 * sect.c: Do production for sectors
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2010
47 * Increase sector efficiency if old type == new type.
48 * decrease sector efficiency if old type != new type.
49 * Return amount of work used.
52 upd_buildeff(struct natstr *np, struct sctstr *sp, int *workp,
53 short *vec, int etu, int *desig, int sctwork, int *cost)
56 int buildeff_work = *workp / 2;
57 int n, hcms, lcms, neweff;
58 unsigned char old_type = *desig;
61 neweff = sp->sct_effic;
63 if (*desig != sp->sct_newtype) {
65 * Tear down existing sector.
66 * Easier to destroy than to build.
68 work_cost = (sp->sct_effic + 3) / 4;
69 if (work_cost > buildeff_work)
70 work_cost = buildeff_work;
71 buildeff_work -= work_cost;
72 n = sp->sct_effic - work_cost * 4;
75 *desig = sp->sct_newtype;
79 if (!n && IS_BIG_CITY(old_type) && !IS_BIG_CITY(*desig)) {
80 // FIXME use trunc_people() and total_work()
81 int maxpop = max_population(np->nat_level[NAT_RLEV], *desig, n);
82 if (vec[I_CIVIL] > maxpop)
83 vec[I_CIVIL] = maxpop;
84 if (vec[I_UW] > maxpop)
86 *workp = (vec[I_CIVIL] * sctwork) / 100.0
87 + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
88 *workp = roundavg((etu * *workp) / 100.0);
90 buildeff_work = MIN((int)(*workp / 2), buildeff_work);
93 if (*desig == sp->sct_newtype) {
94 work_cost = 100 - neweff;
95 if (work_cost > buildeff_work)
96 work_cost = buildeff_work;
98 if (dchr[*desig].d_lcms > 0) {
100 lcms /= dchr[*desig].d_lcms;
101 if (work_cost > lcms)
104 if (dchr[*desig].d_hcms > 0) {
106 hcms /= dchr[*desig].d_hcms;
107 if (work_cost > hcms)
112 *cost += work_cost * dchr[*desig].d_build;
113 buildeff_work -= work_cost;
115 if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
116 vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
117 vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
120 *workp = (*workp + 1) / 2 + buildeff_work;
126 * enlistment sectors are special; they require military
127 * to convert civ into mil in large numbers.
128 * Conversion will happen much more slowly without
129 * some mil initially.
132 enlist(short *vec, int etu, int *cost)
137 /* Need to check treaties here */
139 maxmil = vec[I_CIVIL] / 2 - vec[I_MILIT];
141 enlisted = etu * (10 + vec[I_MILIT]) * 0.05;
142 if (enlisted > maxmil)
144 vec[I_CIVIL] -= enlisted;
145 vec[I_MILIT] += enlisted;
147 *cost = enlisted * 3;
151 /* Fallout is calculated here. */
154 meltitems(int etus, int fallout, int own, short *vec,
155 int type, int x, int y, int uid)
160 for (n = I_NONE + 1; n <= I_MAX; n++) {
161 melt = roundavg(vec[n] * etus * (long)fallout
162 / (1000.0 * ichr[n].i_melt_denom));
165 if (melt > 5 && own) {
166 if (type == EF_SECTOR)
167 wu(0, own, "Lost %d %s to radiation in %s.\n",
168 melt, ichr[n].i_name,
170 else if (type == EF_LAND)
171 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
172 uid, melt, ichr[n].i_name,
174 else if (type == EF_SHIP)
175 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
176 uid, melt, ichr[n].i_name,
184 * Do fallout meltdown for sector SP.
185 * ETUS above 24 are treated as 24 to avoid *huge* kill offs in
189 do_fallout(struct sctstr *sp, int etus)
195 /* This check shouldn't be needed, but just in case. :) */
196 if (!sp->sct_fallout || !sp->sct_updated)
200 meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item,
201 EF_SECTOR, sp->sct_x, sp->sct_y, 0);
202 for (i = 0; NULL != (lp = getlandp(i)); i++) {
205 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
207 meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item,
208 EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid);
210 for (i = 0; NULL != (spp = getshipp(i)); i++) {
213 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
215 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
217 meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item,
218 EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid);
223 spread_fallout(struct sctstr *sp, int etus)
231 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
232 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
233 if (ap->sct_type == SCT_SANCT)
235 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
238 ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
243 decay_fallout(struct sctstr *sp, int etus)
249 decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
250 (double)etus * (double)sp->sct_fallout);
252 sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
256 * Produce for a specific nation
259 produce_sect(int natnum, int etu, struct bp *bp, long p_sect[][2])
263 short buf[I_MAX + 1];
265 int work, cost, ecost, pcost, sctwork;
266 int n, desig, neweff, amount;
268 for (n = 0; NULL != (sp = getsectid(n)); n++) {
269 if (sp->sct_type == SCT_WATER)
271 if (sp->sct_own != natnum)
273 if (sp->sct_updated != 0)
276 np = getnatp(natnum);
278 if (player->simulation) {
279 /* work on a copy, which will be discarded */
280 memcpy(buf, sp->sct_item, sizeof(buf));
285 /* If everybody is dead, the sector reverts to unowned.
286 * This is also checked at the end of the production in
287 * they all starved or were plagued off.
289 if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
290 !has_units(sp->sct_x, sp->sct_y, sp->sct_own, NULL)) {
291 if (!player->simulation) {
292 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
302 sctwork = do_feed(sp, np, vec, &work, etu);
303 bp_put_items(bp, sp, vec);
305 if (sp->sct_off || np->nat_money < 0)
308 neweff = sp->sct_effic;
310 pcost = cost = ecost = 0;
312 desig = sp->sct_type;
314 if (dchr[desig].d_maint) {
315 cost = etu * dchr[desig].d_maint;
316 p_sect[SCT_MAINT][0]++;
317 p_sect[SCT_MAINT][1] += cost;
318 if (!player->simulation)
319 np->nat_money -= cost;
322 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
323 np->nat_money >= 0) {
324 neweff = upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
326 bp_put_items(bp, sp, vec);
327 p_sect[SCT_EFFIC][0]++;
328 p_sect[SCT_EFFIC][1] += cost;
329 if (!player->simulation) {
330 np->nat_money -= cost;
331 sp->sct_type = desig;
332 sp->sct_effic = neweff;
336 if (desig == SCT_ENLIST && neweff >= 60 &&
337 sp->sct_own == sp->sct_oldown) {
338 p_sect[desig][0] += enlist(vec, etu, &ecost);
339 p_sect[desig][1] += ecost;
340 if (!player->simulation)
341 np->nat_money -= ecost;
342 bp_put_items(bp, sp, vec);
346 * now do the production (if sector effic >= 60%)
350 if (np->nat_money >= 0 && dchr[desig].d_prd >= 0)
351 work -= produce(np, sp, vec, work, desig, neweff,
353 bp_put_items(bp, sp, vec);
356 bp_put_avail(bp, sp, work);
357 p_sect[desig][0] += amount;
358 p_sect[desig][1] += pcost;
359 if (!player->simulation) {
360 sp->sct_avail = work;
361 np->nat_money -= pcost;