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