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