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