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
46 #include "prototypes.h"
51 buildeff(struct sctstr *sp)
53 int avail = sp->sct_avail / 2 * 100;
60 if (sp->sct_type != sp->sct_newtype) {
62 * Tear down existing sector.
63 * Easier to destroy than to build.
65 dcp = &dchr[sp->sct_type];
66 build = 4 * avail / dcp->d_bwork;
67 if (build <= sp->sct_effic)
68 sp->sct_effic -= build;
70 build = sp->sct_effic;
72 sp->sct_type = sp->sct_newtype;
74 avail -= roundavg(build / 4.0 * dcp->d_bwork);
78 if (sp->sct_type == sp->sct_newtype) {
79 dcp = &dchr[sp->sct_type];
80 delta = avail / dcp->d_bwork;
81 if (delta > 100 - sp->sct_effic)
82 delta = 100 - sp->sct_effic;
83 build = get_materials(sp, dcp->d_mat, delta);
84 sp->sct_effic += build;
85 avail -= build * dcp->d_bwork;
86 cost += build * dcp->d_cost / 100.0;
89 sp->sct_avail = (sp->sct_avail + 1) / 2 + avail / 100;
94 * enlistment sectors are special; they require military
95 * to convert civ into mil in large numbers.
96 * Conversion will happen much more slowly without
100 enlist(struct natstr *np, short *vec, int etu)
106 maxmil = vec[I_CIVIL] / 2 - vec[I_MILIT];
108 enlisted = etu * (10 + vec[I_MILIT]) * 0.05;
109 if (enlisted > maxmil)
111 vec[I_CIVIL] -= enlisted;
112 vec[I_MILIT] += enlisted;
115 nat_budget[np->nat_cnum].prod[SCT_ENLIST].count += enlisted;
116 nat_budget[np->nat_cnum].prod[SCT_ENLIST].money -= enlisted * 3;
117 nat_budget[np->nat_cnum].money -= enlisted * 3;
120 /* Fallout is calculated here. */
123 meltitems(int etus, int fallout, int own, short *vec,
124 int type, int x, int y, int uid)
129 for (n = I_NONE + 1; n <= I_MAX; n++) {
130 melt = roundavg(vec[n] * etus * (double)fallout
131 / (1000.0 * ichr[n].i_melt_denom));
134 if (melt > 5 && own) {
135 if (type == EF_SECTOR)
136 wu(0, own, "Lost %d %s to radiation in %s.\n",
137 melt, ichr[n].i_name,
139 else if (type == EF_LAND)
140 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
141 uid, melt, ichr[n].i_name,
143 else if (type == EF_SHIP)
144 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
145 uid, melt, ichr[n].i_name,
153 * Do fallout meltdown for sector @sp.
154 * @etus above 24 are treated as 24 to avoid *huge* kill offs in
158 do_fallout(struct sctstr *sp, int etus)
164 /* This check shouldn't be needed, but just in case. :) */
165 if (!sp->sct_fallout || !sp->sct_updated)
169 meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item,
170 EF_SECTOR, sp->sct_x, sp->sct_y, 0);
171 for (i = 0; NULL != (lp = getlandp(i)); i++) {
174 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
176 meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item,
177 EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid);
179 for (i = 0; NULL != (spp = getshipp(i)); i++) {
182 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
184 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
186 meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item,
187 EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid);
192 spread_fallout(struct sctstr *sp, int etus)
200 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
201 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
202 if (ap->sct_type == SCT_SANCT)
204 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
207 ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
212 decay_fallout(struct sctstr *sp, int etus)
218 decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
219 (double)etus * (double)sp->sct_fallout);
221 sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
225 * Produce for a specific nation
228 produce_sect(struct natstr *np, int etu, struct bp *bp)
230 struct budget *budget = &nat_budget[np->nat_cnum];
231 struct sctstr *sp, scratch_sect;
235 for (n = 0; NULL != (sp = getsectid(n)); n++) {
236 if (sp->sct_type == SCT_WATER || sp->sct_type == SCT_SANCT)
238 if (sp->sct_own != np->nat_cnum)
240 if (sp->sct_updated != 0)
245 * When running the test suite, reseed PRNG for each sector
246 * with its UID, to keep results stable even when the number
247 * of PRNs consumed changes.
249 if (running_test_suite)
250 seed_prng(sp->sct_uid);
252 if (player->simulation) {
253 /* work on a copy, which will be discarded */
258 do_feed(sp, np, etu, 0);
260 if (dchr[sp->sct_type].d_maint) {
261 cost = etu * dchr[sp->sct_type].d_maint;
262 budget->bm[BUDG_SCT_MAINT].count++;
263 budget->bm[BUDG_SCT_MAINT].money -= cost;
264 budget->money -= cost;
267 if (sp->sct_off || budget->money < 0) {
269 bp_set_from_sect(bp, sp);
273 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
274 budget->money >= 0) {
276 budget->bm[BUDG_SCT_BUILD].count++;
277 budget->bm[BUDG_SCT_BUILD].money -= cost;
278 budget->money -= cost;
281 if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
282 sp->sct_own == sp->sct_oldown) {
283 enlist(np, sp->sct_item, etu);
287 * now do the production (if sector effic >= 60%)
290 if (sp->sct_effic >= 60) {
291 if (budget->money >= 0 && dchr[sp->sct_type].d_prd >= 0)
295 bp_set_from_sect(bp, sp);