2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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-2016
48 buildeff(struct sctstr *sp)
50 int avail = sp->sct_avail / 2 * 100;
57 if (sp->sct_type != sp->sct_newtype) {
59 * Tear down existing sector.
60 * Easier to destroy than to build.
62 dcp = &dchr[sp->sct_type];
63 build = 4 * avail / dcp->d_bwork;
64 if (build <= sp->sct_effic)
65 sp->sct_effic -= build;
67 build = sp->sct_effic;
69 sp->sct_type = sp->sct_newtype;
71 avail -= (build + 3) / 4 * dcp->d_bwork;
72 cost += (build + 3) / 4;
75 if (sp->sct_type == sp->sct_newtype) {
76 dcp = &dchr[sp->sct_type];
77 delta = avail / dcp->d_bwork;
78 if (delta > 100 - sp->sct_effic)
79 delta = 100 - sp->sct_effic;
80 build = get_materials(sp, dcp->d_mat, delta);
81 sp->sct_effic += build;
82 avail -= build * dcp->d_bwork;
83 cost += (build * dcp->d_cost + 99) / 100;
86 sp->sct_avail = (sp->sct_avail + 1) / 2 + avail / 100;
91 * enlistment sectors are special; they require military
92 * to convert civ into mil in large numbers.
93 * Conversion will happen much more slowly without
97 enlist(short *vec, int etu, int *cost)
103 maxmil = vec[I_CIVIL] / 2 - vec[I_MILIT];
105 enlisted = etu * (10 + vec[I_MILIT]) * 0.05;
106 if (enlisted > maxmil)
108 vec[I_CIVIL] -= enlisted;
109 vec[I_MILIT] += enlisted;
111 *cost = enlisted * 3;
115 /* Fallout is calculated here. */
118 meltitems(int etus, int fallout, int own, short *vec,
119 int type, int x, int y, int uid)
124 for (n = I_NONE + 1; n <= I_MAX; n++) {
125 melt = roundavg(vec[n] * etus * (double)fallout
126 / (1000.0 * ichr[n].i_melt_denom));
129 if (melt > 5 && own) {
130 if (type == EF_SECTOR)
131 wu(0, own, "Lost %d %s to radiation in %s.\n",
132 melt, ichr[n].i_name,
134 else if (type == EF_LAND)
135 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
136 uid, melt, ichr[n].i_name,
138 else if (type == EF_SHIP)
139 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
140 uid, melt, ichr[n].i_name,
148 * Do fallout meltdown for sector @sp.
149 * @etus above 24 are treated as 24 to avoid *huge* kill offs in
153 do_fallout(struct sctstr *sp, int etus)
159 /* This check shouldn't be needed, but just in case. :) */
160 if (!sp->sct_fallout || !sp->sct_updated)
164 meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item,
165 EF_SECTOR, sp->sct_x, sp->sct_y, 0);
166 for (i = 0; NULL != (lp = getlandp(i)); i++) {
169 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
171 meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item,
172 EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid);
174 for (i = 0; NULL != (spp = getshipp(i)); i++) {
177 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
179 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
181 meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item,
182 EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid);
187 spread_fallout(struct sctstr *sp, int etus)
195 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
196 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
197 if (ap->sct_type == SCT_SANCT)
199 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
202 ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
207 decay_fallout(struct sctstr *sp, int etus)
213 decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
214 (double)etus * (double)sp->sct_fallout);
216 sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
220 * Produce for a specific nation
223 produce_sect(struct natstr *np, int etu, struct bp *bp, int p_sect[][2])
225 struct sctstr *sp, scratch_sect;
226 int work, cost, ecost, pcost;
229 for (n = 0; NULL != (sp = getsectid(n)); n++) {
230 if (sp->sct_type == SCT_WATER)
232 if (sp->sct_own != np->nat_cnum)
234 if (sp->sct_updated != 0)
239 * When running the test suite, reseed PRNG for each sector
240 * with its UID, to keep results stable even when the number
241 * of PRNs consumed changes.
243 if (running_test_suite)
244 seed_prng(sp->sct_uid);
246 if (player->simulation) {
247 /* work on a copy, which will be discarded */
252 work = do_feed(sp, np, etu, 0);
254 if (sp->sct_off || np->nat_money < 0) {
255 bp_set_from_sect(bp, sp);
259 sp->sct_avail = work;
261 pcost = cost = ecost = 0;
263 if (dchr[sp->sct_type].d_maint) {
264 cost = etu * dchr[sp->sct_type].d_maint;
265 p_sect[SCT_MAINT][0]++;
266 p_sect[SCT_MAINT][1] += cost;
267 if (!player->simulation)
268 np->nat_money -= cost;
271 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
272 np->nat_money >= 0) {
274 p_sect[SCT_EFFIC][0]++;
275 p_sect[SCT_EFFIC][1] += cost;
276 if (!player->simulation)
277 np->nat_money -= cost;
280 if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
281 sp->sct_own == sp->sct_oldown) {
282 p_sect[sp->sct_type][0] += enlist(sp->sct_item, etu, &ecost);
283 p_sect[sp->sct_type][1] += ecost;
284 if (!player->simulation)
285 np->nat_money -= ecost;
289 * now do the production (if sector effic >= 60%)
292 if (sp->sct_effic >= 60) {
293 if (np->nat_money >= 0 && dchr[sp->sct_type].d_prd >= 0)
294 amount = produce(np, sp, &pcost);
297 bp_set_from_sect(bp, sp);
298 p_sect[sp->sct_type][0] += amount;
299 p_sect[sp->sct_type][1] += pcost;
300 if (!player->simulation)
301 np->nat_money -= pcost;