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