]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/plague.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / update / plague.c
index 6f0f2454865fc684042ee79a4dd224d09e03be81..cdafc032c88bc24571f657c31f59c0c2fecb23b9 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  plague.c: Plague related functions
- * 
+ *
  *  Known contributors to this file:
  *     Steve McClure, 1998-2000
  */
 
-#include <math.h>
-#include "misc.h"
-#include "var.h"
-#include "sect.h"
-#include "nat.h"
+#include <config.h>
+
 #include "item.h"
+#include "lost.h"
 #include "news.h"
-#include "file.h"
-#include "xy.h"
-#include "optlist.h"
+#include "plague.h"
 #include "update.h"
-#include "common.h"
-#include "subs.h"
-#include "lost.h"
-#include "gen.h"
+
+static int infect_people(struct natstr *, struct sctstr *);
 
 void
 do_plague(struct sctstr *sp, struct natstr *np, int etu)
 {
-        int     vec[I_MAX+1];
-       int     cvec[I_MAX+1];
-       int     n;
+    int pstage, ptime;
+    int n;
 
-       if (opt_NO_PLAGUE)      /* no plague nothing to do */
-           return;
+    if (opt_NO_PLAGUE)         /* no plague nothing to do */
+       return;
 
-       if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
-         return;
-       if (getvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR) <= 0)
-         bzero((s_char *)cvec, sizeof(cvec));
+    pstage = sp->sct_pstage;
+    ptime = sp->sct_ptime;
 
-       if (cvec[C_PSTAGE] == 0) {
-               cvec[C_PSTAGE] = infect_people(np, vec, sp->sct_effic,
-                       (int)sp->sct_mobil, sp);
-               cvec[C_PTIME] = 0;
-       } else {
-               n = plague_people(np, vec, cvec, etu);
-               switch (n) {
-               case PLG_DYING:
-                       wu(0, sp->sct_own, "PLAGUE deaths reported in %s.\n",
-                               ownxy(sp));
-                       nreport(sp->sct_own, N_DIE_PLAGUE, 0, 1);
-                       break;
-               case PLG_INFECT:
-                       wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
-                       break;
-               case PLG_INCUBATE:
-                       /* Are we still incubating? */
-                       if (n == cvec[C_PSTAGE]) {
-                           /* Yes. Will it turn "infectious" next time? */
-                           if (cvec[C_PTIME] <= etu) {
-                               /* Yes.  Report an outbreak. */
-                               wu(0, sp->sct_own,
-                                       "Outbreak of PLAGUE in %s!\n",
-                                       ownxy(sp));
-                               nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
-                           }
-                       } else {
-                           /* It has already moved on to "infectious" */
-                           wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
-                       }
-                       break;
-               case PLG_EXPOSED:
-                       /* Has the plague moved to "incubation" yet? */
-                       if (n != cvec[C_PSTAGE]) {
-                           /* Yes. Will it turn "infectious" next time? */
-                           if (cvec[C_PTIME] <= etu) {
-                               /* Yes.  Report an outbreak. */
-                               wu(0, sp->sct_own,
-                                       "Outbreak of PLAGUE in %s!\n",
-                                       ownxy(sp));
-                               nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
-                           }
-                       }
-                       break;
-               default:
-                       break;
+    if (pstage == PLG_HEALTHY) {
+       pstage = infect_people(np, sp);
+       ptime = 0;
+    } else {
+       n = plague_people(np, sp->sct_item, &pstage, &ptime, etu);
+       switch (n) {
+       case PLG_DYING:
+           wu(0, sp->sct_own, "PLAGUE deaths reported in %s.\n",
+              ownxy(sp));
+           nreport(sp->sct_own, N_DIE_PLAGUE, 0, 1);
+           break;
+       case PLG_INFECT:
+           wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
+           break;
+       case PLG_INCUBATE:
+           /* Are we still incubating? */
+           if (n == pstage) {
+               /* Yes. Will it turn "infectious" next time? */
+               if (ptime <= etu) {
+                   /* Yes.  Report an outbreak. */
+                   wu(0, sp->sct_own,
+                      "Outbreak of PLAGUE in %s!\n", ownxy(sp));
+                   nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
                }
+           } else {
+               /* It has already moved on to "infectious" */
+               wu(0, sp->sct_own, "%s battling PLAGUE\n", ownxy(sp));
+           }
+           break;
+       case PLG_EXPOSED:
+           /* Has the plague moved to "incubation" yet? */
+           if (n != pstage) {
+               /* Yes. Will it turn "infectious" next time? */
+               if (ptime <= etu) {
+                   /* Yes.  Report an outbreak. */
+                   wu(0, sp->sct_own,
+                      "Outbreak of PLAGUE in %s!\n", ownxy(sp));
+                   nreport(sp->sct_own, N_OUT_PLAGUE, 0, 1);
+               }
+           }
+           break;
+       default:
+           break;
        }
-       if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
-               !has_units(sp->sct_x,sp->sct_y,sp->sct_own,0)) {
-               makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
-               sp->sct_own = 0;
-               sp->sct_oldown = 0;
-       }
-       putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
-       putvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR);
+    }
+    if (sp->sct_item[I_CIVIL] == 0 && sp->sct_item[I_MILIT] == 0
+       && !has_units(sp->sct_x, sp->sct_y, sp->sct_own, NULL)) {
+       makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
+       sp->sct_own = 0;
+       sp->sct_oldown = 0;
+    }
+    sp->sct_pstage = pstage;
+    sp->sct_ptime = ptime;
 }
 
 /*ARGSUSED*/
-int
-infect_people(struct natstr *np, register int *vec, u_int eff, int mobil, struct sctstr *sp)
+static int
+infect_people(struct natstr *np, struct sctstr *sp)
 {
-       double  plg_num;
-       double  plg_denom;
-       double  plg_chance;
-       double  civvies = 999.0;
+    double pop, pop_space, bad_stuff, pollution, cleanup;
+    double plg_chance;
 
-       if (opt_NO_PLAGUE)      /* no plague nothing to do */
-               return PLG_HEALTHY;
-
-       if (np->nat_level[NAT_TLEV] <= 10.0)
-               return PLG_HEALTHY;
-
-       if (opt_BIG_CITY && (sp->sct_type == SCT_CAPIT))
-               civvies = 9999.0;
+    if (opt_NO_PLAGUE)         /* no plague nothing to do */
+       return PLG_HEALTHY;
 
-       /*
-        * make plague where there was none before...
-        */
-       plg_num = ((vec[I_CIVIL] + vec[I_MILIT] + vec[I_UW]) / civvies) *
-               ((vec[I_IRON] + vec[I_OIL] + (vec[I_RAD] * 2)) / 10.0 +
-               np->nat_level[NAT_TLEV] + 100.0);
-       plg_denom = eff + mobil + 100 + np->nat_level[NAT_RLEV];
-       plg_chance = ((plg_num / plg_denom) - 1.0) * 0.01;
-       if (chance(plg_chance))
-               return PLG_EXPOSED;
+    if (np->nat_level[NAT_TLEV] <= 10.0)
        return PLG_HEALTHY;
+
+    /*
+     * make plague where there was none before...
+     */
+    pop = sp->sct_item[I_CIVIL] + sp->sct_item[I_MILIT] + sp->sct_item[I_UW];
+    pop_space = max_pop(np->nat_level[NAT_RLEV], sp);
+    bad_stuff
+       = sp->sct_item[I_IRON] + sp->sct_item[I_OIL] + sp->sct_item[I_RAD] * 2;
+    pollution = bad_stuff / 10.0 + np->nat_level[NAT_TLEV] + 100.0;
+    cleanup = sp->sct_effic + sp->sct_mobil + 100 + np->nat_level[NAT_RLEV];
+    plg_chance = ((pop / pop_space) * (pollution / cleanup) - 1.0) * 0.01;
+    if (chance(plg_chance))
+       return PLG_EXPOSED;
+    return PLG_HEALTHY;
 }
 
 /*
@@ -158,43 +142,46 @@ infect_people(struct natstr *np, register int *vec, u_int eff, int mobil, struct
  * stage.  No reports generated here anymore.
  */
 int
-plague_people(struct natstr *np, register int *vec, register int *cvec, int etus)
+plague_people(struct natstr *np, short *vec,
+             int *pstage, int *ptime, int etus)
 {
-       int     stage;
-       double  plg_num;
-       double  plg_denom;
-       double  pct_left;
+    int stage;
+    double plg_num;
+    double plg_denom;
+    double pct_left;
 
-       if (opt_NO_PLAGUE)      /* no plague nothing to do */
-           return PLG_HEALTHY;
-       cvec[C_PTIME] -= etus;
-       stage = cvec[C_PSTAGE];
-       switch (stage) {
-       case PLG_DYING:
-               plg_num = 100.0 * etus;
-               plg_denom = (np->nat_level[NAT_RLEV] + 100.0) *
-                       (vec[C_PTIME] + etus + 1.0);
-               pct_left = 1.0 - (double)(plg_num / plg_denom);
-               if (pct_left < 0.2)
-                       pct_left = 0.2;
-               vec[I_CIVIL] = vec[I_CIVIL] * pct_left;
-               vec[I_MILIT] = vec[I_MILIT] * pct_left;
-               vec[I_UW] = vec[I_UW] * pct_left;
-               break;
-       case PLG_INFECT:
-       case PLG_INCUBATE:
-               break;
-       case PLG_EXPOSED:
-               cvec[C_PTIME] = 0;
-               break;
-       default:
-               /* bad */
-               cvec[C_PTIME] = 0;
-               break;
-       }
-       if (cvec[C_PTIME] <= 0) {
-               cvec[C_PSTAGE]--;
-               cvec[C_PTIME] = (etus / 2) + (random() % etus);
-       }
-       return stage;
+    if (opt_NO_PLAGUE)         /* no plague nothing to do */
+       return PLG_HEALTHY;
+    *ptime -= etus;
+    stage = *pstage;
+    switch (stage) {
+    case PLG_DYING:
+       plg_num = 100.0 * etus;
+       plg_denom = (np->nat_level[NAT_RLEV] + 100.0) *
+           (*ptime + etus + 1.0);
+       pct_left = 1.0 - plg_num / plg_denom;
+       if (pct_left < 0.2)
+           pct_left = 0.2;
+       vec[I_CIVIL] = vec[I_CIVIL] * pct_left;
+       vec[I_MILIT] = vec[I_MILIT] * pct_left;
+       vec[I_UW] = vec[I_UW] * pct_left;
+       break;
+    case PLG_INFECT:
+    case PLG_INCUBATE:
+       break;
+    case PLG_EXPOSED:
+       *ptime = 0;
+       break;
+    default:
+       /* bad */
+       logerror("plague_people: bad pstage %d", stage);
+       *pstage = PLG_HEALTHY;
+       *ptime = 0;
+       return PLG_HEALTHY;
+    }
+    if (*ptime <= 0) {
+       *pstage -= 1;
+       *ptime = etus / 2 + random() % etus;
+    }
+    return stage;
 }