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 if (!player->simulation)
118 np->nat_money -= enlisted * 3;
121 /* Fallout is calculated here. */
124 meltitems(int etus, int fallout, int own, short *vec,
125 int type, int x, int y, int uid)
130 for (n = I_NONE + 1; n <= I_MAX; n++) {
131 melt = roundavg(vec[n] * etus * (double)fallout
132 / (1000.0 * ichr[n].i_melt_denom));
135 if (melt > 5 && own) {
136 if (type == EF_SECTOR)
137 wu(0, own, "Lost %d %s to radiation in %s.\n",
138 melt, ichr[n].i_name,
140 else if (type == EF_LAND)
141 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
142 uid, melt, ichr[n].i_name,
144 else if (type == EF_SHIP)
145 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
146 uid, melt, ichr[n].i_name,
154 * Do fallout meltdown for sector @sp.
155 * @etus above 24 are treated as 24 to avoid *huge* kill offs in
159 do_fallout(struct sctstr *sp, int etus)
165 /* This check shouldn't be needed, but just in case. :) */
166 if (!sp->sct_fallout || !sp->sct_updated)
170 meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item,
171 EF_SECTOR, sp->sct_x, sp->sct_y, 0);
172 for (i = 0; NULL != (lp = getlandp(i)); i++) {
175 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
177 meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item,
178 EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid);
180 for (i = 0; NULL != (spp = getshipp(i)); i++) {
183 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
185 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
187 meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item,
188 EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid);
193 spread_fallout(struct sctstr *sp, int etus)
201 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
202 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
203 if (ap->sct_type == SCT_SANCT)
205 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
208 ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
213 decay_fallout(struct sctstr *sp, int etus)
219 decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
220 (double)etus * (double)sp->sct_fallout);
222 sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
226 * Produce for a specific nation
229 produce_sect(struct natstr *np, int etu, struct bp *bp)
231 struct budget *budget = &nat_budget[np->nat_cnum];
232 struct sctstr *sp, scratch_sect;
236 for (n = 0; NULL != (sp = getsectid(n)); n++) {
237 if (sp->sct_type == SCT_WATER)
239 if (sp->sct_own != np->nat_cnum)
241 if (sp->sct_updated != 0)
246 * When running the test suite, reseed PRNG for each sector
247 * with its UID, to keep results stable even when the number
248 * of PRNs consumed changes.
250 if (running_test_suite)
251 seed_prng(sp->sct_uid);
253 if (player->simulation) {
254 /* work on a copy, which will be discarded */
259 do_feed(sp, np, etu, 0);
261 if (sp->sct_off || np->nat_money < 0) {
263 bp_set_from_sect(bp, sp);
267 if (dchr[sp->sct_type].d_maint) {
268 cost = etu * dchr[sp->sct_type].d_maint;
269 budget->bm[BUDG_SCT_MAINT].count++;
270 budget->bm[BUDG_SCT_MAINT].money -= cost;
271 if (!player->simulation)
272 np->nat_money -= cost;
275 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
276 np->nat_money >= 0) {
277 cost = roundavg(buildeff(sp));
278 budget->bm[BUDG_SCT_BUILD].count++;
279 budget->bm[BUDG_SCT_BUILD].money -= cost;
280 if (!player->simulation)
281 np->nat_money -= cost;
284 if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
285 sp->sct_own == sp->sct_oldown) {
286 enlist(np, sp->sct_item, etu);
290 * now do the production (if sector effic >= 60%)
293 if (sp->sct_effic >= 60) {
294 if (np->nat_money >= 0 && dchr[sp->sct_type].d_prd >= 0)
298 bp_set_from_sect(bp, sp);