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)
51 int avail = sp->sct_avail;
52 int buildeff_work = avail / 2;
53 int cost, n, neweff, desig;
56 neweff = sp->sct_effic;
58 if (sp->sct_type != sp->sct_newtype) {
60 * Tear down existing sector.
61 * Easier to destroy than to build.
63 work_cost = (sp->sct_effic + 3) / 4;
64 if (work_cost > buildeff_work)
65 work_cost = buildeff_work;
66 buildeff_work -= work_cost;
67 n = sp->sct_effic - work_cost * 4;
70 sp->sct_type = sp->sct_newtype;
77 if (desig == sp->sct_newtype) {
78 work_cost = 100 - neweff;
79 if (work_cost > buildeff_work)
80 work_cost = buildeff_work;
81 work_cost = get_materials(sp, dchr[sp->sct_type].d_mat, work_cost);
84 cost += (work_cost * dchr[desig].d_cost + 99) / 100;
85 buildeff_work -= work_cost;
88 sp->sct_effic = neweff;
89 sp->sct_avail = (avail + 1) / 2 + buildeff_work;
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(short *vec, int etu, int *cost)
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;
114 *cost = enlisted * 3;
118 /* Fallout is calculated here. */
121 meltitems(int etus, int fallout, int own, short *vec,
122 int type, int x, int y, int uid)
127 for (n = I_NONE + 1; n <= I_MAX; n++) {
128 melt = roundavg(vec[n] * etus * (double)fallout
129 / (1000.0 * ichr[n].i_melt_denom));
132 if (melt > 5 && own) {
133 if (type == EF_SECTOR)
134 wu(0, own, "Lost %d %s to radiation in %s.\n",
135 melt, ichr[n].i_name,
137 else if (type == EF_LAND)
138 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
139 uid, melt, ichr[n].i_name,
141 else if (type == EF_SHIP)
142 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
143 uid, melt, ichr[n].i_name,
151 * Do fallout meltdown for sector @sp.
152 * @etus above 24 are treated as 24 to avoid *huge* kill offs in
156 do_fallout(struct sctstr *sp, int etus)
162 /* This check shouldn't be needed, but just in case. :) */
163 if (!sp->sct_fallout || !sp->sct_updated)
167 meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item,
168 EF_SECTOR, sp->sct_x, sp->sct_y, 0);
169 for (i = 0; NULL != (lp = getlandp(i)); i++) {
172 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
174 meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item,
175 EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid);
177 for (i = 0; NULL != (spp = getshipp(i)); i++) {
180 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
182 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
184 meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item,
185 EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid);
190 spread_fallout(struct sctstr *sp, int etus)
198 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
199 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
200 if (ap->sct_type == SCT_SANCT)
202 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
205 ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
210 decay_fallout(struct sctstr *sp, int etus)
216 decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
217 (double)etus * (double)sp->sct_fallout);
219 sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
223 * Produce for a specific nation
226 produce_sect(struct natstr *np, int etu, struct bp *bp, int p_sect[][2])
228 struct sctstr *sp, scratch_sect;
229 int work, cost, ecost, pcost;
232 for (n = 0; NULL != (sp = getsectid(n)); n++) {
233 if (sp->sct_type == SCT_WATER)
235 if (sp->sct_own != np->nat_cnum)
237 if (sp->sct_updated != 0)
242 * When running the test suite, reseed PRNG for each sector
243 * with its UID, to keep results stable even when the number
244 * of PRNs consumed changes.
246 if (running_test_suite)
247 seed_prng(sp->sct_uid);
249 if (player->simulation) {
250 /* work on a copy, which will be discarded */
255 work = do_feed(sp, np, etu, 0);
257 if (sp->sct_off || np->nat_money < 0) {
258 bp_set_from_sect(bp, sp);
262 sp->sct_avail = work;
264 pcost = cost = ecost = 0;
266 if (dchr[sp->sct_type].d_maint) {
267 cost = etu * dchr[sp->sct_type].d_maint;
268 p_sect[SCT_MAINT][0]++;
269 p_sect[SCT_MAINT][1] += cost;
270 if (!player->simulation)
271 np->nat_money -= cost;
274 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
275 np->nat_money >= 0) {
277 p_sect[SCT_EFFIC][0]++;
278 p_sect[SCT_EFFIC][1] += cost;
279 if (!player->simulation)
280 np->nat_money -= cost;
283 if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
284 sp->sct_own == sp->sct_oldown) {
285 p_sect[sp->sct_type][0] += enlist(sp->sct_item, etu, &ecost);
286 p_sect[sp->sct_type][1] += ecost;
287 if (!player->simulation)
288 np->nat_money -= ecost;
292 * now do the production (if sector effic >= 60%)
295 if (sp->sct_effic >= 60) {
296 if (np->nat_money >= 0 && dchr[sp->sct_type].d_prd >= 0)
297 amount = produce(np, sp, &pcost);
300 bp_set_from_sect(bp, sp);
301 p_sect[sp->sct_type][0] += amount;
302 p_sect[sp->sct_type][1] += pcost;
303 if (!player->simulation)
304 np->nat_money -= pcost;