From 2fea2a7cb897f2beb08a6fd02cf70eca79236bf3 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 17 Jul 2016 08:44:55 +0200 Subject: [PATCH] update: Collect fallout code in fallout.c Signed-off-by: Markus Armbruster --- include/update.h | 5 +- src/lib/update/fallout.c | 178 +++++++++++++++++++++++++++++++++++++++ src/lib/update/prepare.c | 24 +----- src/lib/update/sect.c | 108 ------------------------ 4 files changed, 182 insertions(+), 133 deletions(-) create mode 100644 src/lib/update/fallout.c diff --git a/include/update.h b/include/update.h index b4befb2a..fbfff23d 100644 --- a/include/update.h +++ b/include/update.h @@ -93,6 +93,8 @@ extern void bp_to_sect(struct bp *, struct sctstr *); extern void dodeliver(struct sctstr *); /* distribute.c */ extern int dodistribute(struct sctstr *, int, double); +/* fallout.c */ +extern void fallout(int); /* finish.c */ extern void finish_sects(int); /* human.c */ @@ -147,9 +149,6 @@ extern void revolt(struct sctstr *); extern void guerrilla(struct sctstr *); /* sect.c */ extern double buildeff(struct sctstr *); -extern void do_fallout(struct sctstr *, int); -extern void spread_fallout(struct sctstr *, int); -extern void decay_fallout(struct sctstr *, int); extern void produce_sect(int, struct bp *); /* ship.c */ extern void prep_ships(int, struct bp *); diff --git a/src/lib/update/fallout.c b/src/lib/update/fallout.c new file mode 100644 index 00000000..aa5cb5c6 --- /dev/null +++ b/src/lib/update/fallout.c @@ -0,0 +1,178 @@ +/* + * Empire - A multi-player, client/server Internet based war game. + * Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak, + * Ken Stevens, Steve McClure, Markus Armbruster + * + * Empire is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * --- + * + * See files README, COPYING and CREDITS in the root of the source + * tree for related information and legal notices. It is expected + * that future projects/authors will amend these files as needed. + * + * --- + * + * fallout.c: Nuclear fallout + * + * Known contributors to this file: + * Dave Pare, 1986 + * Steve McClure, 1997 + * Markus Armbruster, 2004-2016 + */ + +#include + +#include "chance.h" +#include "optlist.h" +#include "land.h" +#include "ship.h" +#include "path.h" +#include "prototypes.h" +#include "update.h" + +static void do_fallout(struct sctstr *, int); +static void meltitems(int, int, int, short *, int, int, int, int); +static void spread_fallout(struct sctstr *, int); +static void decay_fallout(struct sctstr *, int); + +void +fallout(int etu) +{ + int n; + struct sctstr *sp; + + if (!opt_FALLOUT) + return; + + /* First, we determine which sectors to process fallout in */ + for (n = 0; NULL != (sp = getsectid(n)); n++) + sp->sct_updated = sp->sct_fallout != 0; + /* Next, we process the fallout there */ + for (n = 0; NULL != (sp = getsectid(n)); n++) + if (sp->sct_updated && sp->sct_type != SCT_SANCT) + do_fallout(sp, etu); + /* Next, we spread the fallout */ + for (n = 0; NULL != (sp = getsectid(n)); n++) + if (sp->sct_updated) + spread_fallout(sp, etu); + /* Next, we decay the fallout */ + for (n = 0; NULL != (sp = getsectid(n)); n++) { + if (sp->sct_fallout) + decay_fallout(sp, etu); + sp->sct_updated = 0; + } +} + +/* + * Do fallout meltdown for sector @sp. + * @etus above 24 are treated as 24 to avoid *huge* kill offs in + * large ETU games. + */ +static void +do_fallout(struct sctstr *sp, int etus) +{ + struct shpstr *spp; + struct lndstr *lp; + int i; + +/* This check shouldn't be needed, but just in case. :) */ + if (!sp->sct_fallout || !sp->sct_updated) + return; + if (etus > 24) + etus = 24; + meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item, + EF_SECTOR, sp->sct_x, sp->sct_y, 0); + for (i = 0; NULL != (lp = getlandp(i)); i++) { + if (!lp->lnd_own) + continue; + if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y) + continue; + meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item, + EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid); + } + for (i = 0; NULL != (spp = getshipp(i)); i++) { + if (!spp->shp_own) + continue; + if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y) + continue; + if (mchr[(int)spp->shp_type].m_flags & M_SUB) + continue; + meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, + EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid); + } +} + +static void +meltitems(int etus, int fallout, int own, short *vec, + int type, int x, int y, int uid) +{ + i_type n; + int melt; + + for (n = I_NONE + 1; n <= I_MAX; n++) { + melt = roundavg(vec[n] * etus * (double)fallout + / (1000.0 * ichr[n].i_melt_denom)); + if (melt > vec[n]) + melt = vec[n]; + if (melt > 5 && own) { + if (type == EF_SECTOR) + wu(0, own, "Lost %d %s to radiation in %s.\n", + melt, ichr[n].i_name, + xyas(x, y, own)); + else if (type == EF_LAND) + wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n", + uid, melt, ichr[n].i_name, + xyas(x, y, own)); + else if (type == EF_SHIP) + wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n", + uid, melt, ichr[n].i_name, + xyas(x, y, own)); + } + vec[n] -= melt; + } +} + +static void +spread_fallout(struct sctstr *sp, int etus) +{ + struct sctstr *ap; + int n; + int inc; + + if (etus > 24) + etus = 24; + for (n = DIR_FIRST; n <= DIR_LAST; n++) { + ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]); + if (ap->sct_type == SCT_SANCT) + continue; + inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1; + if (inc < 0) + inc = 0; + ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX); + } +} + +static void +decay_fallout(struct sctstr *sp, int etus) +{ + int decay; + + if (etus > 24) + etus = 24; + decay = roundavg((decay_per_etu + 6.0) * fallout_spread * + (double)etus * (double)sp->sct_fallout); + + sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0; +} diff --git a/src/lib/update/prepare.c b/src/lib/update/prepare.c index f966a651..f405abbb 100644 --- a/src/lib/update/prepare.c +++ b/src/lib/update/prepare.c @@ -52,28 +52,8 @@ prepare_sects(int etu, struct bp *bp) struct sctstr *sp, scratch_sect; int n; -/* Process all the fallout. */ - if (opt_FALLOUT) { - if (!player->simulation) { - /* First, we determine which sectors to process fallout in */ - for (n = 0; NULL != (sp = getsectid(n)); n++) - sp->sct_updated = sp->sct_fallout != 0; - /* Next, we process the fallout there */ - for (n = 0; NULL != (sp = getsectid(n)); n++) - if (sp->sct_updated && sp->sct_type != SCT_SANCT) - do_fallout(sp, etu); - /* Next, we spread the fallout */ - for (n = 0; NULL != (sp = getsectid(n)); n++) - if (sp->sct_updated) - spread_fallout(sp, etu); - /* Next, we decay the fallout */ - for (n = 0; NULL != (sp = getsectid(n)); n++) { - if (sp->sct_fallout) - decay_fallout(sp, etu); - sp->sct_updated = 0; - } - } - } + if (!player->simulation) + fallout(etu); for (n = 0; NULL != (sp = getsectid(n)); n++) { if (bp_skip_sect(bp, sp)) diff --git a/src/lib/update/sect.c b/src/lib/update/sect.c index de26026f..d5117449 100644 --- a/src/lib/update/sect.c +++ b/src/lib/update/sect.c @@ -36,14 +36,10 @@ #include "chance.h" #include "item.h" -#include "land.h" -#include "lost.h" #include "nat.h" #include "optlist.h" -#include "path.h" #include "player.h" #include "prototypes.h" -#include "ship.h" #include "update.h" double @@ -116,110 +112,6 @@ enlist(struct natstr *np, short *vec, int etu) nat_budget[np->nat_cnum].money -= enlisted * 3; } -/* Fallout is calculated here. */ - -static void -meltitems(int etus, int fallout, int own, short *vec, - int type, int x, int y, int uid) -{ - i_type n; - int melt; - - for (n = I_NONE + 1; n <= I_MAX; n++) { - melt = roundavg(vec[n] * etus * (double)fallout - / (1000.0 * ichr[n].i_melt_denom)); - if (melt > vec[n]) - melt = vec[n]; - if (melt > 5 && own) { - if (type == EF_SECTOR) - wu(0, own, "Lost %d %s to radiation in %s.\n", - melt, ichr[n].i_name, - xyas(x, y, own)); - else if (type == EF_LAND) - wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n", - uid, melt, ichr[n].i_name, - xyas(x, y, own)); - else if (type == EF_SHIP) - wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n", - uid, melt, ichr[n].i_name, - xyas(x, y, own)); - } - vec[n] -= melt; - } -} - -/* - * Do fallout meltdown for sector @sp. - * @etus above 24 are treated as 24 to avoid *huge* kill offs in - * large ETU games. - */ -void -do_fallout(struct sctstr *sp, int etus) -{ - struct shpstr *spp; - struct lndstr *lp; - int i; - -/* This check shouldn't be needed, but just in case. :) */ - if (!sp->sct_fallout || !sp->sct_updated) - return; - if (etus > 24) - etus = 24; - meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item, - EF_SECTOR, sp->sct_x, sp->sct_y, 0); - for (i = 0; NULL != (lp = getlandp(i)); i++) { - if (!lp->lnd_own) - continue; - if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y) - continue; - meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item, - EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid); - } - for (i = 0; NULL != (spp = getshipp(i)); i++) { - if (!spp->shp_own) - continue; - if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y) - continue; - if (mchr[(int)spp->shp_type].m_flags & M_SUB) - continue; - meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, - EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid); - } -} - -void -spread_fallout(struct sctstr *sp, int etus) -{ - struct sctstr *ap; - int n; - int inc; - - if (etus > 24) - etus = 24; - for (n = DIR_FIRST; n <= DIR_LAST; n++) { - ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]); - if (ap->sct_type == SCT_SANCT) - continue; - inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1; - if (inc < 0) - inc = 0; - ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX); - } -} - -void -decay_fallout(struct sctstr *sp, int etus) -{ - int decay; - - if (etus > 24) - etus = 24; - decay = roundavg((decay_per_etu + 6.0) * fallout_spread * - (double)etus * (double)sp->sct_fallout); - - sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0; -} - /* * Produce for a specific nation */