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