]> git.pond.sub.org Git - empserver/blob - src/lib/update/plague.c
update: Fix revert to deity and "no civilians" corner cases
[empserver] / src / lib / update / plague.c
1 /*
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
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  *  plague.c: Plague related functions
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 1998-2000
31  *     Markus Armbruster, 2004-2016
32  */
33
34 #include <config.h>
35
36 #include "chance.h"
37 #include "item.h"
38 #include "lost.h"
39 #include "news.h"
40 #include "plague.h"
41 #include "update.h"
42
43 static int infect_people(struct natstr *, struct sctstr *);
44
45 void
46 do_plague(struct sctstr *sp, struct natstr *np, int etu)
47 {
48     int pstage, ptime;
49     int n;
50
51     if (opt_NO_PLAGUE)          /* no plague nothing to do */
52         return;
53
54     pstage = sp->sct_pstage;
55     ptime = sp->sct_ptime;
56
57     if (pstage == PLG_HEALTHY) {
58         pstage = infect_people(np, sp);
59         ptime = 0;
60     } else {
61         n = plague_people(np, sp->sct_item, &pstage, &ptime, etu);
62         switch (n) {
63         case PLG_DYING:
64             wu(0, sp->sct_own, "PLAGUE deaths reported in %s.\n",
65                ownxy(sp));
66             nreport(sp->sct_own, N_DIE_PLAGUE, 0, 1);
67             break;
68         case PLG_INFECT:
69             wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
70             break;
71         case PLG_INCUBATE:
72             /* Are we still incubating? */
73             if (n == pstage) {
74                 /* Yes. Will it turn "infectious" next time? */
75                 if (ptime <= etu) {
76                     /* Yes.  Report an outbreak. */
77                     wu(0, sp->sct_own,
78                        "Outbreak of PLAGUE in %s!\n", ownxy(sp));
79                     nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
80                 }
81             } else {
82                 /* It has already moved on to "infectious" */
83                 wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
84             }
85             break;
86         case PLG_EXPOSED:
87             /* Has the plague moved to "incubation" yet? */
88             if (n != pstage) {
89                 /* Yes. Will it turn "infectious" next time? */
90                 if (ptime <= etu) {
91                     /* Yes.  Report an outbreak. */
92                     wu(0, sp->sct_own,
93                        "Outbreak of PLAGUE in %s!\n", ownxy(sp));
94                     nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
95                 }
96             }
97             break;
98         default:
99             break;
100         }
101     }
102     sp->sct_pstage = pstage;
103     sp->sct_ptime = ptime;
104 }
105
106 /*ARGSUSED*/
107 static int
108 infect_people(struct natstr *np, struct sctstr *sp)
109 {
110     double pop, pop_space, bad_stuff, pollution, cleanup;
111     double plg_chance;
112
113     if (opt_NO_PLAGUE)          /* no plague nothing to do */
114         return PLG_HEALTHY;
115
116     if (np->nat_level[NAT_TLEV] <= 10.0)
117         return PLG_HEALTHY;
118
119     /*
120      * make plague where there was none before...
121      */
122     pop = sp->sct_item[I_CIVIL] + sp->sct_item[I_MILIT] + sp->sct_item[I_UW];
123     pop_space = max_pop(np->nat_level[NAT_RLEV], sp);
124     bad_stuff
125         = sp->sct_item[I_IRON] + sp->sct_item[I_OIL] + sp->sct_item[I_RAD] * 2;
126     pollution = bad_stuff / 10.0 + np->nat_level[NAT_TLEV] + 100.0;
127     cleanup = sp->sct_effic + sp->sct_mobil + 100 + np->nat_level[NAT_RLEV];
128     plg_chance = ((pop / pop_space) * (pollution / cleanup) - 1.0) * 0.01;
129     if (chance(plg_chance))
130         return PLG_EXPOSED;
131     return PLG_HEALTHY;
132 }
133
134 /*
135  * Given the fact that plague exists, kill off
136  * people if in plague state DYING.  Increment
137  * the plague time.  Return "current" plague
138  * stage.  No reports generated here anymore.
139  */
140 int
141 plague_people(struct natstr *np, short *vec,
142               int *pstage, int *ptime, int etus)
143 {
144     int stage;
145     double plg_num;
146     double plg_denom;
147     double pct_left;
148
149     if (opt_NO_PLAGUE)          /* no plague nothing to do */
150         return PLG_HEALTHY;
151     *ptime -= etus;
152     stage = *pstage;
153     switch (stage) {
154     case PLG_DYING:
155         plg_num = 100.0 * etus;
156         plg_denom = (np->nat_level[NAT_RLEV] + 100.0) *
157             (*ptime + etus + 1.0);
158         pct_left = 1.0 - plg_num / plg_denom;
159         if (pct_left < 0.2)
160             pct_left = 0.2;
161         vec[I_CIVIL] = vec[I_CIVIL] * pct_left;
162         vec[I_MILIT] = vec[I_MILIT] * pct_left;
163         vec[I_UW] = vec[I_UW] * pct_left;
164         break;
165     case PLG_INFECT:
166     case PLG_INCUBATE:
167         break;
168     case PLG_EXPOSED:
169         *ptime = 0;
170         break;
171     default:
172         /* bad */
173         logerror("plague_people: bad pstage %d", stage);
174         *pstage = PLG_HEALTHY;
175         *ptime = 0;
176         return PLG_HEALTHY;
177     }
178     if (*ptime <= 0) {
179         *pstage -= 1;
180         *ptime = etus / 2 + roll0(etus);
181     }
182     return stage;
183 }