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