]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/sect.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / update / sect.c
index 87bb4d15f26a69603629fdcd987cb17ceb6e3667..e4e8b43b92aa3640cfcbeb46ccfd0579857c87ef 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *     Steve McClure, 1996
  */
 
-#include <math.h>
+#include <config.h>
+
 #include "misc.h"
-#include "var.h"
 #include "sect.h"
 #include "nat.h"
 #include "item.h"
-#include "news.h"
 #include "file.h"
 #include "xy.h"
 #include "path.h"
 #include "product.h"
-#include "distribute.h"
 #include "optlist.h"
 #include "budg.h"
 #include "player.h"
 #include "lost.h"
 #include "gen.h"
 
-void
-dodeliver(struct sctstr *sp)
-{
-    register int i;
-    int thresh;
-    int dir;
-    int plague;
-    int n;
-
-    if (sp->sct_mobil <= 0)
-       return;
-    plague = sp->sct_pstage;
-    for (i = 1; i <= I_MAX; i++) {
-       if (sp->sct_del[i] == 0)
-           continue;
-       thresh = sp->sct_del[i] & ~0x7;
-       dir = sp->sct_del[i] & 0x7;
-       n = deliver(sp, &ichr[i], dir, thresh, sp->sct_item[i], plague);
-       if (n > 0) {
-           sp->sct_item[i] -= n;
-           if (sp->sct_mobil <= 0)
-               break;
-       }
-    }
-}
-
 /*
  * Increase sector efficiency if old type == new type.
  * decrease sector efficiency if old type != new type.
  * Return amount of work used.
  */
 static int
-upd_buildeff(struct natstr *np, register struct sctstr *sp, int *workp,
-            int *vec, int etu, int *desig, int sctwork, int *cost)
+upd_buildeff(struct natstr *np, struct sctstr *sp, int *workp,
+            short *vec, int etu, int *desig, int sctwork, int *cost)
 {
-    register int work_cost = 0;
-    int buildeff_work = (int)(*workp / 2);
+    int work_cost = 0;
+    int buildeff_work = *workp / 2;
     int n, hcms, lcms, neweff;
     u_char old_type = *desig;
 
@@ -114,20 +86,18 @@ upd_buildeff(struct natstr *np, register struct sctstr *sp, int *workp,
        }
        neweff = n;
        *cost += work_cost;
-       if (opt_BIG_CITY) {
-           if (!n && dchr[old_type].d_pkg == UPKG &&
-               dchr[*desig].d_pkg != UPKG) {
-               int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
-               if (vec[I_CIVIL] > maxpop)
-                   vec[I_CIVIL] = maxpop;
-               if (vec[I_UW] > maxpop)
-                   vec[I_UW] = maxpop;
-               *workp = (vec[I_CIVIL] * sctwork) / 100.0
-                   + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
-               *workp = roundavg((etu * (*workp)) / 100.0);
-
-               buildeff_work = min((int)(*workp / 2), buildeff_work);
-           }
+       if (!n && IS_BIG_CITY(old_type) &&
+           !IS_BIG_CITY(*desig)) {
+           int maxpop = max_population(np->nat_level[NAT_RLEV], *desig, n);
+           if (vec[I_CIVIL] > maxpop)
+               vec[I_CIVIL] = maxpop;
+           if (vec[I_UW] > maxpop)
+               vec[I_UW] = maxpop;
+           *workp = (vec[I_CIVIL] * sctwork) / 100.0
+               + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
+           *workp = roundavg((etu * (*workp)) / 100.0);
+
+           buildeff_work = MIN((int)(*workp / 2), buildeff_work);
        }
     }
     if (np->nat_priorities[*desig]) {
@@ -159,7 +129,7 @@ upd_buildeff(struct natstr *np, register struct sctstr *sp, int *workp,
            }
        }
     }
-    *workp = *workp / 2 + buildeff_work;
+    *workp = (*workp + 1) / 2 + buildeff_work;
 
     return neweff;
 }
@@ -171,7 +141,7 @@ upd_buildeff(struct natstr *np, register struct sctstr *sp, int *workp,
  * some mil initially.
  */
 static int
-enlist(register int *vec, int etu, int *cost)
+enlist(short *vec, int etu, int *cost)
 {
     int maxmil;
     int enlisted;
@@ -196,26 +166,26 @@ static void
 meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
          int uid)
 {
-    int n;
+    i_type n;
     int melt;
 
-    for (n = 1; n <= I_MAX; n++) {
+    for (n = I_NONE + 1; n <= I_MAX; n++) {
        melt = roundavg(vec[n] * etus * (long)fallout
-                       / (1000.0 * melt_item_denom[n]));
+                       / (1000.0 * ichr[n].i_melt_denom));
        if (melt > vec[n])
            melt = vec[n];
        if (melt > 5 && own) {
            if (type == EF_SECTOR)
                wu(0, own, "Lost %d %s to radiation in %s.\n",
-                  melt < vec[n] ? melt : vec[n], ichr[n].i_name,
+                  melt, ichr[n].i_name,
                   xyas(x, y, own));
            else if (type == EF_LAND)
                wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
-                  uid, melt < vec[n] ? melt : vec[n], ichr[n].i_name,
+                  uid, melt, ichr[n].i_name,
                   xyas(x, y, own));
            else if (type == EF_SHIP)
                wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
-                  uid, melt < vec[n] ? melt : vec[n], ichr[n].i_name,
+                  uid, melt, ichr[n].i_name,
                   xyas(x, y, own));
        }
        vec[n] -= melt;
@@ -223,14 +193,12 @@ meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
 }
 
 /*
- * do_fallout - calculate fallout for sectors.
- *
- * This is etu based.  But, do limit HUGE kill offs in large ETU
- * games, the melting etus rate is limited to 24 etus.
+ * Do fallout meltdown for sector SP.
+ * ETUS above 24 are treated as 24 to avoid *huge* kill offs in
+ * large ETU games.
  */
-
 void
-do_fallout(register struct sctstr *sp, register int etus)
+do_fallout(struct sctstr *sp, int etus)
 {
     struct shpstr *spp;
     struct lndstr *lp;
@@ -261,10 +229,6 @@ do_fallout(register struct sctstr *sp, register int etus)
        meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, EF_SHIP,
                  spp->shp_x, spp->shp_y, spp->shp_uid);
     }
-#ifdef GODZILLA
-    if ((sp->sct_fallout > 20) && chance(100))
-       do_godzilla(sp);
-#endif /* GODZILLA */
 }
 
 void
@@ -272,7 +236,7 @@ spread_fallout(struct sctstr *sp, int etus)
 {
     struct sctstr *ap;
     int n;
-    register int inc;
+    int inc;
 
     if (etus > 24)
        etus = 24;
@@ -281,17 +245,9 @@ spread_fallout(struct sctstr *sp, int etus)
        if (ap->sct_type == SCT_SANCT)
            continue;
        inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
-#if 0
-       if (sp->sct_fallout) {
-           wu(0, 0, "Fallout from sector %d,%d to %d,%d is %d=%d*%e*%d\n",
-              sp->sct_x, sp->sct_y, sp->sct_x + diroff[n][0],
-              sp->sct_y + diroff[n][1], inc, etus,
-              fallout_spread, sp->sct_fallout);
-       }
-#endif
        if (inc < 0)
            inc = 0;
-       ap->sct_fallout = min(ap->sct_fallout + inc, FALLOUT_MAX);
+       ap->sct_fallout = MIN(ap->sct_fallout + inc, FALLOUT_MAX);
     }
 }
 
@@ -305,17 +261,9 @@ decay_fallout(struct sctstr *sp, int etus)
     decay = roundavg(((decay_per_etu + 6.0) * fallout_spread) *
                     (double)etus * (double)sp->sct_fallout);
 
-#if 0
-    if (decay || sp->sct_fallout)
-       wu(0, 0, "Fallout decay in %d,%d is %d from %d\n", sp->sct_x,
-          sp->sct_y, decay, sp->sct_fallout);
-#endif
-
     sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
 }
 
-#define SHOULD_PRODUCE(sp,t)   (((sp->sct_type == t) || (t == -1)) ? 1 : 0)
-
 /*
  * Produce only a set sector type for a specific nation
  * (or all, if sector_type == -1)
@@ -325,9 +273,10 @@ void
 produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
             int sector_type)
 {
-    register struct sctstr *sp;
-    register struct natstr *np;
-    int vec[I_MAX + 1];
+    struct sctstr *sp;
+    struct natstr *np;
+    short buf[I_MAX + 1];
+    short *vec;
     int work, cost, ecost, pcost, sctwork;
     int n, desig, maxpop, neweff, amount;
 
@@ -338,7 +287,7 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
            continue;
        if (sp->sct_updated != 0)
            continue;
-       if (!SHOULD_PRODUCE(sp, sector_type))
+       if (sp->sct_type != sector_type && sector_type != -1)
            continue;
 
        if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
@@ -346,8 +295,13 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
            p_sect[SCT_CAPIT][1] += etu;
        }
 
-       if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
-           continue;
+       if (player->simulation) {
+           /* work on a copy, which will be discarded */
+           memcpy(buf, sp->sct_item, sizeof(buf));
+           vec = buf;
+       } else
+           vec = sp->sct_item;
+
        /* If everybody is dead, the sector reverts to unowned. 
           * This is also checked at the end of the production in
           * they all starved or were plagued off.
@@ -375,10 +329,8 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
        bp_clear_cachepath();
 
        if (sp->sct_off || np->nat_money < 0) {
-           if (!player->simulation) {
-               putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
+           if (!player->simulation)
                sp->sct_off = 0;
-           }
            continue;
        }
        if ((np->nat_priorities[sp->sct_type] == 0) &&
@@ -386,7 +338,6 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
            ((pchr[dchr[sp->sct_type].d_prd].p_cost != 0) ||
             (sp->sct_type == SCT_ENLIST))) {
            if (!player->simulation) {
-               putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
                logerror("Skipping %s production for country %s\n",
                         dchr[sp->sct_type].d_name, np->nat_cnam);
            }
@@ -401,28 +352,14 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
 
        if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
            np->nat_money > 0) {
-           neweff =
-               upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
-                            &cost);
+           neweff = upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
+                                 &cost);
            pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
            pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
            p_sect[SCT_EFFIC][0]++;
            p_sect[SCT_EFFIC][1] += cost;
            if (!player->simulation) {
                np->nat_money -= cost;
-               /* No longer tear down infrastructure
-                  if (sp->sct_type != desig) {
-                  sp->sct_road = 0;
-                  sp->sct_defense = 0;
-                  } else if (neweff < sp->sct_effic) {
-                  sp->sct_road -= (sp->sct_road * (sp->sct_effic - neweff) / 100.0);
-                  sp->sct_defense -= (sp->sct_defense * (sp->sct_effic - neweff) / 100.0);
-                  if (sp->sct_road < 0)
-                  sp->sct_road = 0;
-                  if (sp->sct_defense < 0)
-                  sp->sct_defense = 0;
-                  }
-                */
                sp->sct_type = desig;
                sp->sct_effic = neweff;
                if (!opt_DEFENSE_INFRA)
@@ -434,7 +371,6 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
            ((pchr[dchr[desig].d_prd].p_cost != 0) ||
             (desig == SCT_ENLIST))) {
            if (!player->simulation) {
-               putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
                logerror("Skipping %s production for country %s\n",
                         dchr[sp->sct_type].d_name, np->nat_cnam);
            }
@@ -468,7 +404,6 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
                vec[I_CIVIL] = maxpop;
            if (vec[I_UW] > maxpop)
                vec[I_UW] = maxpop;
-           putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
            sp->sct_avail = work;
            np->nat_money -= pcost;
        }