]> git.pond.sub.org Git - empserver/blob - src/lib/update/plague.c
1eafa6522f050644e34de99dea3740ec1c18f303
[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     int cvec[I_MAX + 1];
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     if (getvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR) <= 0)
66         memset(cvec, 0, sizeof(cvec));
67
68     if (cvec[C_PSTAGE] == 0) {
69         cvec[C_PSTAGE] = infect_people(np, vec, sp->sct_effic,
70                                        (int)sp->sct_mobil, sp);
71         cvec[C_PTIME] = 0;
72     } else {
73         n = plague_people(np, vec, cvec, etu);
74         switch (n) {
75         case PLG_DYING:
76             wu(0, sp->sct_own, "PLAGUE deaths reported in %s.\n",
77                ownxy(sp));
78             nreport(sp->sct_own, N_DIE_PLAGUE, 0, 1);
79             break;
80         case PLG_INFECT:
81             wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
82             break;
83         case PLG_INCUBATE:
84             /* Are we still incubating? */
85             if (n == cvec[C_PSTAGE]) {
86                 /* Yes. Will it turn "infectious" next time? */
87                 if (cvec[C_PTIME] <= etu) {
88                     /* Yes.  Report an outbreak. */
89                     wu(0, sp->sct_own,
90                        "Outbreak of PLAGUE in %s!\n", ownxy(sp));
91                     nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
92                 }
93             } else {
94                 /* It has already moved on to "infectious" */
95                 wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
96             }
97             break;
98         case PLG_EXPOSED:
99             /* Has the plague moved to "incubation" yet? */
100             if (n != cvec[C_PSTAGE]) {
101                 /* Yes. Will it turn "infectious" next time? */
102                 if (cvec[C_PTIME] <= etu) {
103                     /* Yes.  Report an outbreak. */
104                     wu(0, sp->sct_own,
105                        "Outbreak of PLAGUE in %s!\n", ownxy(sp));
106                     nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
107                 }
108             }
109             break;
110         default:
111             break;
112         }
113     }
114     if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
115         !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
116         makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
117         sp->sct_own = 0;
118         sp->sct_oldown = 0;
119     }
120     putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
121     putvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR);
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, register int *cvec,
164               int etus)
165 {
166     int stage;
167     double plg_num;
168     double plg_denom;
169     double pct_left;
170
171     if (opt_NO_PLAGUE)          /* no plague nothing to do */
172         return PLG_HEALTHY;
173     cvec[C_PTIME] -= etus;
174     stage = cvec[C_PSTAGE];
175     switch (stage) {
176     case PLG_DYING:
177         plg_num = 100.0 * etus;
178         plg_denom = (np->nat_level[NAT_RLEV] + 100.0) *
179             (vec[C_PTIME] + etus + 1.0);
180         pct_left = 1.0 - (double)(plg_num / plg_denom);
181         if (pct_left < 0.2)
182             pct_left = 0.2;
183         vec[I_CIVIL] = vec[I_CIVIL] * pct_left;
184         vec[I_MILIT] = vec[I_MILIT] * pct_left;
185         vec[I_UW] = vec[I_UW] * pct_left;
186         break;
187     case PLG_INFECT:
188     case PLG_INCUBATE:
189         break;
190     case PLG_EXPOSED:
191         cvec[C_PTIME] = 0;
192         break;
193     default:
194         /* bad */
195         cvec[C_PTIME] = 0;
196         break;
197     }
198     if (cvec[C_PTIME] <= 0) {
199         cvec[C_PSTAGE]--;
200         cvec[C_PTIME] = (etus / 2) + (random() % etus);
201     }
202     return stage;
203 }