]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/sect.c
Update copyright notice
[empserver] / src / lib / update / sect.c
index ad1dd41ef79fa2ba1a0ded8f5c78aca204b53d8b..acaf5fac71452f6619c103962f69315eae8559d2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, 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 "misc.h"
-#include "sect.h"
-#include "nat.h"
+#include <config.h>
+
+#include "budg.h"
 #include "item.h"
-#include "file.h"
-#include "xy.h"
+#include "land.h"
+#include "lost.h"
 #include "path.h"
-#include "product.h"
-#include "optlist.h"
-#include "budg.h"
 #include "player.h"
-#include "land.h"
+#include "product.h"
 #include "ship.h"
 #include "update.h"
-#include "subs.h"
-#include "common.h"
-#include "lost.h"
-#include "gen.h"
 
 /*
  * Increase sector efficiency if old type == new type.
@@ -63,7 +56,7 @@ upd_buildeff(struct natstr *np, struct sctstr *sp, int *workp,
     int work_cost = 0;
     int buildeff_work = *workp / 2;
     int n, hcms, lcms, neweff;
-    u_char old_type = *desig;
+    unsigned char old_type = *desig;
 
     *cost = 0;
     neweff = sp->sct_effic;
@@ -84,49 +77,46 @@ upd_buildeff(struct natstr *np, 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)) {
+           // FIXME use trunc_people() and total_work()
+           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]) {
-       if (*desig == sp->sct_newtype) {
-           work_cost = 100 - neweff;
-           if (work_cost > buildeff_work)
-               work_cost = buildeff_work;
-
-           if (dchr[*desig].d_lcms > 0) {
-               lcms = vec[I_LCM];
-               lcms /= dchr[*desig].d_lcms;
-               if (work_cost > lcms)
-                   work_cost = lcms;
-           }
-           if (dchr[*desig].d_hcms > 0) {
-               hcms = vec[I_HCM];
-               hcms /= dchr[*desig].d_hcms;
-               if (work_cost > hcms)
-                   work_cost = hcms;
-           }
+    if (*desig == sp->sct_newtype) {
+       work_cost = 100 - neweff;
+       if (work_cost > buildeff_work)
+           work_cost = buildeff_work;
 
-           neweff += work_cost;
-           *cost += work_cost * dchr[*desig].d_build;
-           buildeff_work -= work_cost;
+       if (dchr[*desig].d_lcms > 0) {
+           lcms = vec[I_LCM];
+           lcms /= dchr[*desig].d_lcms;
+           if (work_cost > lcms)
+               work_cost = lcms;
+       }
+       if (dchr[*desig].d_hcms > 0) {
+           hcms = vec[I_HCM];
+           hcms /= dchr[*desig].d_hcms;
+           if (work_cost > hcms)
+               work_cost = hcms;
+       }
 
-           if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
-               vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
-               vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
-           }
+       neweff += work_cost;
+       *cost += work_cost * dchr[*desig].d_build;
+       buildeff_work -= work_cost;
+
+       if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
+           vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
+           vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
        }
     }
     *workp = (*workp + 1) / 2 + buildeff_work;
@@ -148,9 +138,9 @@ enlist(short *vec, int etu, int *cost)
 
     /* Need to check treaties here */
     enlisted = 0;
-    maxmil = (vec[I_CIVIL] / 2) - vec[I_MILIT];
+    maxmil = vec[I_CIVIL] / 2 - vec[I_MILIT];
     if (maxmil > 0) {
-       enlisted = (etu * (10 + vec[I_MILIT]) * 0.05);
+       enlisted = etu * (10 + vec[I_MILIT]) * 0.05;
        if (enlisted > maxmil)
            enlisted = maxmil;
        vec[I_CIVIL] -= enlisted;
@@ -171,21 +161,21 @@ meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
 
     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;
@@ -193,12 +183,10 @@ 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(struct sctstr *sp, int etus)
 {
@@ -231,10 +219,6 @@ do_fallout(struct sctstr *sp, 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
@@ -251,17 +235,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);
     }
 }
 
@@ -272,33 +248,24 @@ decay_fallout(struct sctstr *sp, int etus)
 
     if (etus > 24)
        etus = 24;
-    decay = roundavg(((decay_per_etu + 6.0) * fallout_spread) *
+    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;
 }
 
 /*
- * Produce only a set sector type for a specific nation
- * (or all, if sector_type == -1)
- *
+ * Produce for a specific nation
  */
 void
-produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
-            int sector_type)
+produce_sect(int natnum, int etu, struct bp *bp, long p_sect[][2])
 {
     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;
+    int n, desig, neweff, amount;
 
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
        if (sp->sct_type == SCT_WATER)
@@ -307,12 +274,14 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
            continue;
        if (sp->sct_updated != 0)
            continue;
-       if (sp->sct_type != sector_type && sector_type != -1)
-           continue;
+
+       np = getnatp(natnum);
 
        if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
            p_sect[SCT_CAPIT][0]++;
            p_sect[SCT_CAPIT][1] += etu;
+           if (!player->simulation)
+               np->nat_money -= etu;
        }
 
        if (player->simulation) {
@@ -323,46 +292,34 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
            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.
+        * This is also checked at the end of the production in
+        * they all starved or were plagued off.
         */
        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;
+           if (!player->simulation) {
+               makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
+               sp->sct_own = 0;
+               sp->sct_oldown = 0;
+           }
            continue;
        }
 
        sp->sct_updated = 1;
        work = 0;
 
-       np = getnatp(natnum);
-
        /* do_feed trys to supply.  So, we need to enable cacheing
           here */
        bp_enable_cachepath();
 
-       sctwork = do_feed(sp, np, vec, &work, bp, etu);
+       sctwork = do_feed(sp, np, vec, &work, etu);
+       bp_put_items(bp, sp, vec);
 
        bp_disable_cachepath();
        bp_clear_cachepath();
 
-       if (sp->sct_off || np->nat_money < 0) {
-           if (!player->simulation)
-               sp->sct_off = 0;
-           continue;
-       }
-       if ((np->nat_priorities[sp->sct_type] == 0) &&
-           (sp->sct_type == sp->sct_newtype) &&
-           ((pchr[dchr[sp->sct_type].d_prd].p_cost != 0) ||
-            (sp->sct_type == SCT_ENLIST))) {
-           if (!player->simulation) {
-               logerror("Skipping %s production for country %s\n",
-                        dchr[sp->sct_type].d_name, np->nat_cnam);
-           }
+       if (sp->sct_off || np->nat_money < 0)
            continue;
-       }
 
        neweff = sp->sct_effic;
        amount = 0;
@@ -371,38 +328,26 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
        desig = sp->sct_type;
 
        if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
-           np->nat_money > 0) {
+           np->nat_money >= 0) {
            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]);
+           bp_put_items(bp, sp, vec);
            p_sect[SCT_EFFIC][0]++;
            p_sect[SCT_EFFIC][1] += cost;
            if (!player->simulation) {
                np->nat_money -= cost;
                sp->sct_type = desig;
                sp->sct_effic = neweff;
-               if (!opt_DEFENSE_INFRA)
-                   sp->sct_defense = sp->sct_effic;
            }
        }
 
-       if ((np->nat_priorities[desig] == 0) &&
-           ((pchr[dchr[desig].d_prd].p_cost != 0) ||
-            (desig == SCT_ENLIST))) {
-           if (!player->simulation) {
-               logerror("Skipping %s production for country %s\n",
-                        dchr[sp->sct_type].d_name, np->nat_cnam);
-           }
-           continue;
-       }
-
        if (desig == SCT_ENLIST && neweff >= 60 &&
            sp->sct_own == sp->sct_oldown) {
            p_sect[desig][0] += enlist(vec, etu, &ecost);
            p_sect[desig][1] += ecost;
            if (!player->simulation)
                np->nat_money -= ecost;
+           bp_put_items(bp, sp, vec);
        }
 
        /*
@@ -410,20 +355,16 @@ produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
         */
 
        if (neweff >= 60) {
-           if (np->nat_money > 0 && dchr[desig].d_prd)
-               work -=
-                   produce(np, sp, vec, work, desig, neweff, &pcost, &amount);
+           if (np->nat_money >= 0 && dchr[desig].d_prd >= 0)
+               work -= produce(np, sp, vec, work, desig, neweff,
+                               &pcost, &amount);
+           bp_put_items(bp, sp, vec);
        }
 
-       pt_bg_nmbr(bp, sp, I_MAX + 1, work);
+       bp_put_avail(bp, sp, work);
        p_sect[desig][0] += amount;
        p_sect[desig][1] += pcost;
        if (!player->simulation) {
-           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;
            sp->sct_avail = work;
            np->nat_money -= pcost;
        }