]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/sect.c
config: Make work to build sectors configurable
[empserver] / src / lib / update / sect.c
index 91a6a54e26e34511b03bece4df603fa7085bd400..4cdb3503787ea6f710b012f6ec6bb00ffbeb3642 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2016, 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,
@@ -14,8 +14,7 @@
  *  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/>.
  *
  *  ---
  *
  *  ---
  *
  *  sect.c: Do production for sectors
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1996
+ *     Markus Armbruster, 2004-2016
  */
 
 #include <config.h>
 
-#include "misc.h"
-#include "sect.h"
-#include "nat.h"
+#include "budg.h"
+#include "chance.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 "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.
- * decrease sector efficiency if old type != new type.
- * Return amount of work used.
- */
-static int
-upd_buildeff(struct natstr *np, struct sctstr *sp, int *workp,
-            short *vec, int etu, int *desig, int sctwork, int *cost)
+int
+buildeff(struct sctstr *sp)
 {
-    int work_cost = 0;
-    int buildeff_work = *workp / 2;
-    int n, hcms, lcms, neweff;
-    unsigned char old_type = *desig;
+    int avail = sp->sct_avail / 2 * 100;
+    int cost;
+    int delta, build;
+    struct dchrstr *dcp;
 
-    *cost = 0;
-    neweff = sp->sct_effic;
+    cost = 0;
 
-    if (*desig != sp->sct_newtype) {
+    if (sp->sct_type != sp->sct_newtype) {
        /*
         * Tear down existing sector.
         * Easier to destroy than to build.
         */
-       work_cost = (sp->sct_effic + 3) / 4;
-       if (work_cost > buildeff_work)
-           work_cost = buildeff_work;
-       buildeff_work -= work_cost;
-       n = sp->sct_effic - work_cost * 4;
-       if (n <= 0) {
-           n = 0;
-           *desig = sp->sct_newtype;
-       }
-       neweff = n;
-       *cost += work_cost;
-       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);
+       dcp = &dchr[sp->sct_type];
+       build = 4 * avail / dcp->d_bwork;
+       if (build <= sp->sct_effic)
+           sp->sct_effic -= build;
+       else {
+           build = sp->sct_effic;
+           sp->sct_effic = 0;
+           sp->sct_type = sp->sct_newtype;
        }
+       avail -= (build + 3) / 4 * dcp->d_bwork;
+       cost += (build + 3) / 4;
     }
-    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;
-       }
 
-       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;
-       }
+    if (sp->sct_type == sp->sct_newtype) {
+       dcp = &dchr[sp->sct_type];
+       delta = avail / dcp->d_bwork;
+       if (delta > 100 - sp->sct_effic)
+           delta = 100 - sp->sct_effic;
+       build = get_materials(sp, dcp->d_mat, delta);
+       sp->sct_effic += build;
+       avail -= build * dcp->d_bwork;
+       cost += (build * dcp->d_cost + 99) / 100;
     }
-    *workp = (*workp + 1) / 2 + buildeff_work;
 
-    return neweff;
+    sp->sct_avail = (sp->sct_avail + 1) / 2 + avail / 100;
+    return cost;
 }
 
 /*
@@ -144,11 +99,10 @@ enlist(short *vec, int etu, int *cost)
     int maxmil;
     int enlisted;
 
-    /* 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;
@@ -161,14 +115,14 @@ enlist(short *vec, int etu, int *cost)
 /* Fallout is calculated here. */
 
 static void
-meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
-         int uid)
+meltitems(int etus, int fallout, int own, short *vec,
+         int type, int x, int y, int uid)
 {
     i_type n;
     int melt;
 
     for (n = I_NONE + 1; n <= I_MAX; n++) {
-       melt = roundavg(vec[n] * etus * (long)fallout
+       melt = roundavg(vec[n] * etus * (double)fallout
                        / (1000.0 * ichr[n].i_melt_denom));
        if (melt > vec[n])
            melt = vec[n];
@@ -191,8 +145,8 @@ meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
 }
 
 /*
- * Do fallout meltdown for sector SP.
- * ETUS above 24 are treated as 24 to avoid *huge* kill offs in
+ * Do fallout meltdown for sector @sp.
+ * @etus above 24 are treated as 24 to avoid *huge* kill offs in
  * large ETU games.
  */
 void
@@ -207,15 +161,15 @@ do_fallout(struct sctstr *sp, int etus)
        return;
     if (etus > 24)
        etus = 24;
-    meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item, EF_SECTOR,
-             sp->sct_x, sp->sct_y, 0);
+    meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item,
+             EF_SECTOR, sp->sct_x, sp->sct_y, 0);
     for (i = 0; NULL != (lp = getlandp(i)); i++) {
        if (!lp->lnd_own)
            continue;
        if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
            continue;
-       meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item, EF_LAND,
-                 lp->lnd_x, lp->lnd_y, lp->lnd_uid);
+       meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item,
+                 EF_LAND, lp->lnd_x, lp->lnd_y, lp->lnd_uid);
     }
     for (i = 0; NULL != (spp = getshipp(i)); i++) {
        if (!spp->shp_own)
@@ -224,8 +178,8 @@ do_fallout(struct sctstr *sp, int etus)
            continue;
        if (mchr[(int)spp->shp_type].m_flags & M_SUB)
            continue;
-       meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, EF_SHIP,
-                 spp->shp_x, spp->shp_y, spp->shp_uid);
+       meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item,
+                 EF_SHIP, spp->shp_x, spp->shp_y, spp->shp_uid);
     }
 }
 
@@ -266,94 +220,67 @@ decay_fallout(struct sctstr *sp, int etus)
  * Produce for a specific nation
  */
 void
-produce_sect(int natnum, int etu, int *bp, long p_sect[][2])
+produce_sect(struct natstr *np, int etu, struct bp *bp, int 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;
+    struct sctstr *sp, scratch_sect;
+    int work, cost, ecost, pcost;
+    int n, amount;
 
     for (n = 0; NULL != (sp = getsectid(n)); n++) {
        if (sp->sct_type == SCT_WATER)
            continue;
-       if (sp->sct_own != natnum)
+       if (sp->sct_own != np->nat_cnum)
            continue;
        if (sp->sct_updated != 0)
            continue;
+       sp->sct_updated = 1;
 
-       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;
-       }
+       /*
+        * When running the test suite, reseed PRNG for each sector
+        * with its UID, to keep results stable even when the number
+        * of PRNs consumed changes.
+        */
+       if (running_test_suite)
+           seed_prng(sp->sct_uid);
 
        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.
-        */
-       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;
-           continue;
+           scratch_sect = *sp;
+           sp = &scratch_sect;
        }
 
-       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);
-
-       bp_disable_cachepath();
-       bp_clear_cachepath();
+       work = do_feed(sp, np, etu, 0);
 
        if (sp->sct_off || np->nat_money < 0) {
-           if (!player->simulation)
-               sp->sct_off = 0;
+           bp_set_from_sect(bp, sp);
            continue;
        }
 
-       neweff = sp->sct_effic;
+       sp->sct_avail = work;
        amount = 0;
        pcost = cost = ecost = 0;
 
-       desig = sp->sct_type;
+       if (dchr[sp->sct_type].d_maint) {
+           cost = etu * dchr[sp->sct_type].d_maint;
+           p_sect[SCT_MAINT][0]++;
+           p_sect[SCT_MAINT][1] += cost;
+           if (!player->simulation)
+               np->nat_money -= cost;
+       }
 
        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);
-           pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
-           pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
+           np->nat_money >= 0) {
+           cost = buildeff(sp);
            p_sect[SCT_EFFIC][0]++;
            p_sect[SCT_EFFIC][1] += cost;
-           if (!player->simulation) {
+           if (!player->simulation)
                np->nat_money -= cost;
-               sp->sct_type = desig;
-               sp->sct_effic = neweff;
-           }
        }
 
-       if (desig == SCT_ENLIST && neweff >= 60 &&
+       if (sp->sct_type == SCT_ENLIST && sp->sct_effic >= 60 &&
            sp->sct_own == sp->sct_oldown) {
-           p_sect[desig][0] += enlist(vec, etu, &ecost);
-           p_sect[desig][1] += ecost;
+           p_sect[sp->sct_type][0] += enlist(sp->sct_item, etu, &ecost);
+           p_sect[sp->sct_type][1] += ecost;
            if (!player->simulation)
                np->nat_money -= ecost;
        }
@@ -362,23 +289,15 @@ produce_sect(int natnum, int etu, int *bp, long p_sect[][2])
         * now do the production (if sector effic >= 60%)
         */
 
-       if (neweff >= 60) {
-           if (np->nat_money > 0 && dchr[desig].d_prd >= 0)
-               work -= produce(np, sp, vec, work, desig, neweff,
-                               &pcost, &amount);
+       if (sp->sct_effic >= 60) {
+           if (np->nat_money >= 0 && dchr[sp->sct_type].d_prd >= 0)
+               amount = produce(np, sp, &pcost);
        }
 
-       pt_bg_nmbr(bp, sp, I_MAX + 1, 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;
+       bp_set_from_sect(bp, sp);
+       p_sect[sp->sct_type][0] += amount;
+       p_sect[sp->sct_type][1] += pcost;
+       if (!player->simulation)
            np->nat_money -= pcost;
-       }
     }
 }