]> git.pond.sub.org Git - empserver/blob - src/lib/update/plague.c
Remove superfluous parenthesis.
[empserver] / src / lib / update / plague.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  plague.c: Plague related functions
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1998-2000
32  */
33
34 #include <config.h>
35
36 #include "item.h"
37 #include "lost.h"
38 #include "news.h"
39 #include "plague.h"
40 #include "update.h"
41
42 static int infect_people(struct natstr *, struct sctstr *);
43
44 void
45 do_plague(struct sctstr *sp, struct natstr *np, int etu)
46 {
47     int pstage, ptime;
48     int n;
49
50     if (opt_NO_PLAGUE)          /* no plague nothing to do */
51         return;
52
53     pstage = sp->sct_pstage;
54     ptime = sp->sct_ptime;
55
56     if (pstage == PLG_HEALTHY) {
57         pstage = infect_people(np, sp);
58         ptime = 0;
59     } else {
60         n = plague_people(np, sp->sct_item, &pstage, &ptime, etu);
61         switch (n) {
62         case PLG_DYING:
63             wu(0, sp->sct_own, "PLAGUE deaths reported in %s.\n",
64                ownxy(sp));
65             nreport(sp->sct_own, N_DIE_PLAGUE, 0, 1);
66             break;
67         case PLG_INFECT:
68             wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
69             break;
70         case PLG_INCUBATE:
71             /* Are we still incubating? */
72             if (n == pstage) {
73                 /* Yes. Will it turn "infectious" next time? */
74                 if (ptime <= etu) {
75                     /* Yes.  Report an outbreak. */
76                     wu(0, sp->sct_own,
77                        "Outbreak of PLAGUE in %s!\n", ownxy(sp));
78                     nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
79                 }
80             } else {
81                 /* It has already moved on to "infectious" */
82                 wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
83             }
84             break;
85         case PLG_EXPOSED:
86             /* Has the plague moved to "incubation" yet? */
87             if (n != pstage) {
88                 /* Yes. Will it turn "infectious" next time? */
89                 if (ptime <= etu) {
90                     /* Yes.  Report an outbreak. */
91                     wu(0, sp->sct_own,
92                        "Outbreak of PLAGUE in %s!\n", ownxy(sp));
93                     nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
94                 }
95             }
96             break;
97         default:
98             break;
99         }
100     }
101     if (sp->sct_item[I_CIVIL] == 0 && sp->sct_item[I_MILIT] == 0
102         && !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
103         makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
104         sp->sct_own = 0;
105         sp->sct_oldown = 0;
106     }
107     sp->sct_pstage = pstage;
108     sp->sct_ptime = ptime;
109 }
110
111 /*ARGSUSED*/
112 static int
113 infect_people(struct natstr *np, struct sctstr *sp)
114 {
115     double pop, pop_space, bad_stuff, pollution, cleanup;
116     double plg_chance;
117
118     if (opt_NO_PLAGUE)          /* no plague nothing to do */
119         return PLG_HEALTHY;
120
121     if (np->nat_level[NAT_TLEV] <= 10.0)
122         return PLG_HEALTHY;
123
124     /*
125      * make plague where there was none before...
126      */
127     pop = sp->sct_item[I_CIVIL] + sp->sct_item[I_MILIT] + sp->sct_item[I_UW];
128     pop_space = max_pop(np->nat_level[NAT_RLEV], sp);
129     bad_stuff
130         = sp->sct_item[I_IRON] + sp->sct_item[I_OIL] + sp->sct_item[I_RAD] * 2;
131     pollution = bad_stuff / 10.0 + np->nat_level[NAT_TLEV] + 100.0;
132     cleanup = sp->sct_effic + sp->sct_mobil + 100 + np->nat_level[NAT_RLEV];
133     plg_chance = ((pop / pop_space) * (pollution / cleanup) - 1.0) * 0.01;
134     if (chance(plg_chance))
135         return PLG_EXPOSED;
136     return PLG_HEALTHY;
137 }
138
139 /*
140  * Given the fact that plague exists, kill off
141  * people if in plague state DYING.  Increment
142  * the plague time.  Return "current" plague
143  * stage.  No reports generated here anymore.
144  */
145 int
146 plague_people(struct natstr *np, short *vec,
147               int *pstage, int *ptime,
148               int etus)
149 {
150     int stage;
151     double plg_num;
152     double plg_denom;
153     double pct_left;
154
155     if (opt_NO_PLAGUE)          /* no plague nothing to do */
156         return PLG_HEALTHY;
157     *ptime -= etus;
158     stage = *pstage;
159     switch (stage) {
160     case PLG_DYING:
161         plg_num = 100.0 * etus;
162         plg_denom = (np->nat_level[NAT_RLEV] + 100.0) *
163             (*ptime + etus + 1.0);
164         pct_left = 1.0 - plg_num / plg_denom;
165         if (pct_left < 0.2)
166             pct_left = 0.2;
167         vec[I_CIVIL] = vec[I_CIVIL] * pct_left;
168         vec[I_MILIT] = vec[I_MILIT] * pct_left;
169         vec[I_UW] = vec[I_UW] * pct_left;
170         break;
171     case PLG_INFECT:
172     case PLG_INCUBATE:
173         break;
174     case PLG_EXPOSED:
175         *ptime = 0;
176         break;
177     default:
178         /* bad */
179         logerror("plague_people: bad pstage %d", stage);
180         *pstage = PLG_HEALTHY;
181         *ptime = 0;
182         return PLG_HEALTHY;
183     }
184     if (*ptime <= 0) {
185         *pstage -= 1;
186         *ptime = etus / 2 + random() % etus;
187     }
188     return stage;
189 }