]> git.pond.sub.org Git - empserver/blob - src/lib/update/fallout.c
Update copyright notice
[empserver] / src / lib / update / fallout.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  fallout.c: Nuclear fallout
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1997
32  *     Markus Armbruster, 2004-2016
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "optlist.h"
39 #include "land.h"
40 #include "ship.h"
41 #include "path.h"
42 #include "prototypes.h"
43 #include "update.h"
44
45 static void do_fallout(struct sctstr *, int);
46 static void meltitems(int, int, int, short *, int, int, int, int);
47 static void spread_fallout(struct sctstr *, int);
48 static void decay_fallout(struct sctstr *, int);
49
50 void
51 fallout(int etu)
52 {
53     int n;
54     struct sctstr *sp;
55
56     if (!opt_FALLOUT)
57         return;
58
59     /* First, we determine which sectors to process fallout in */
60     for (n = 0; NULL != (sp = getsectid(n)); n++)
61         sp->sct_updated = sp->sct_fallout != 0;
62     /* Next, we process the fallout there */
63     for (n = 0; NULL != (sp = getsectid(n)); n++)
64         if (sp->sct_updated && sp->sct_type != SCT_SANCT)
65             do_fallout(sp, etu);
66     /* Next, we spread the fallout */
67     for (n = 0; NULL != (sp = getsectid(n)); n++)
68         if (sp->sct_updated)
69             spread_fallout(sp, etu);
70     /* Next, we decay the fallout */
71     for (n = 0; NULL != (sp = getsectid(n)); n++) {
72         if (sp->sct_fallout)
73             decay_fallout(sp, etu);
74         sp->sct_updated = 0;
75     }
76 }
77
78 /*
79  * Do fallout meltdown for sector @sp.
80  * @etus above 24 are treated as 24 to avoid *huge* kill offs in
81  * large ETU games.
82  */
83 static void
84 do_fallout(struct sctstr *sp, int etus)
85 {
86     struct shpstr *spp;
87     struct lndstr *lp;
88     int i;
89
90 /* This check shouldn't be needed, but just in case. :) */
91     if (!sp->sct_fallout || !sp->sct_updated)
92         return;
93     if (etus > 24)
94         etus = 24;
95     meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item,
96               EF_SECTOR, sp->sct_x, sp->sct_y, 0);
97     for (i = 0; NULL != (lp = getlandp(i)); i++) {
98         if (!lp->lnd_own)
99             continue;
100         if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
101             continue;
102         meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item,
103                   EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid);
104     }
105     for (i = 0; NULL != (spp = getshipp(i)); i++) {
106         if (!spp->shp_own)
107             continue;
108         if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
109             continue;
110         if (mchr[(int)spp->shp_type].m_flags & M_SUB)
111             continue;
112         meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item,
113                   EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid);
114     }
115 }
116
117 static void
118 meltitems(int etus, int fallout, int own, short *vec,
119           int type, int x, int y, int uid)
120 {
121     i_type n;
122     int melt;
123
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));
127         if (melt > vec[n])
128             melt = vec[n];
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,
133                    xyas(x, y, own));
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,
137                    xyas(x, y, own));
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,
141                    xyas(x, y, own));
142         }
143         vec[n] -= melt;
144     }
145 }
146
147 static void
148 spread_fallout(struct sctstr *sp, int etus)
149 {
150     struct sctstr *ap;
151     int n;
152     int inc;
153
154     if (etus > 24)
155         etus = 24;
156     for (n = DIR_FIRST; n <= DIR_LAST; n++) {
157         ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
158         if (ap->sct_type == SCT_SANCT)
159             continue;
160         inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
161         if (inc < 0)
162             inc = 0;
163         ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
164     }
165 }
166
167 static void
168 decay_fallout(struct sctstr *sp, int etus)
169 {
170     int decay;
171
172     if (etus > 24)
173         etus = 24;
174     decay = roundavg((decay_per_etu + 6.0) * fallout_spread *
175                      (double)etus * (double)sp->sct_fallout);
176
177     sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
178 }