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