]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/prod.c
Clean up redundant code in prod()
[empserver] / src / lib / commands / prod.c
index bdec10e8bf2484d86011b422d9066b5bdb7ae327..882ff28cb79c84b96b7afe78241dc61761e16462 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-2013, 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.
  *
  *  ---
  *
  *  prod.c: Calculate production levels
- * 
+ *
  *  Known contributors to this file:
  *     David Muir Sharnoff, 1987
  *     Steve McClure, 1997-2000
+ *     Markus Armbruster, 2004-2010
  */
 
-#include "misc.h"
-#include "player.h"
-#include "var.h"
-#include "xy.h"
-#include "nsc.h"
-#include "sect.h"
-#include "product.h"
-#include "nat.h"
+#include <config.h>
+
+#include "commands.h"
 #include "item.h"
-#include "file.h"
 #include "optlist.h"
-#include "commands.h"
+#include "product.h"
+
+static void prprod(coord, coord, int, double, double, int, char,
+                  double, double, double, char[], int[], int[], int);
 
 int
-count_pop(register int n)
+count_pop(int n)
 {
-    register int i;
-    register int pop = 0;
+    int i;
+    int pop = 0;
     struct sctstr *sp;
 
     for (i = 0; NULL != (sp = getsectid(i)); i++) {
@@ -76,34 +73,30 @@ prod(void)
     int work;
     int totpop;
     int act;                   /* actual production */
-    int cost;
+    double cost;
     int i;
-    int max;                   /* production w/infinate materials */
+    int max;                   /* production w/infinite materials */
     int nsect;
     double take;
     double mtake;
     int there;
     int unit_work;             /* sum of component amounts */
     int used;                  /* production w/infinite workforce */
-    int wforce;
-    int it;
-    u_short *amount;           /* amount for component pointer */
-    u_char *comp;              /* component pointer */
-    u_char *endcomp;
-    u_char vtype;
-    s_char *resource;
-    int c;
-    s_char maxc[3][10];
-    s_char use[3][10];
+    i_type it;
+    i_type vtype;
+    unsigned char *resource;
+    char cmnem[MAXPRCON];
+    int cuse[MAXPRCON], cmax[MAXPRCON];
     int lcms, hcms;
-    int civs = 0;
-    int uws = 0;
+    int civs;
+    int uws;
     int bwork;
     int twork;
     int type;
     int eff;
     int maxpop;
-    u_char otype;
+    int otype;
+    char mnem;
 
     if (!snxtsct(&nstr, player->argp[1]))
        return RET_SYN;
@@ -114,26 +107,15 @@ prod(void)
        if (!player->owner)
            continue;
 
-       civs = min(9999, (int)((obrate * etu_per_update + 1.0)
-                              * sect.sct_item[I_CIVIL]));
-       uws = min(9999, (int)((uwbrate * etu_per_update + 1.0)
-                             * sect.sct_item[I_UW]));
-       if (opt_RES_POP) {
-           natp = getnatp(sect.sct_own);
-           maxpop = max_pop((float)natp->nat_level[NAT_RLEV], &sect);
-           civs = min(civs, maxpop);
-           uws = min(uws, maxpop);
-       } else {                /* now RES_POP */
-           civs = min(999, civs);
-           uws = min(999, uws);
-       }                       /* end RES_POP */
+       civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL];
+       uws = (1.0 + uwbrate * etu_per_update) * sect.sct_item[I_UW];
+       natp = getnatp(sect.sct_own);
+       maxpop = max_pop(natp->nat_level[NAT_RLEV], &sect);
 
-       /* This isn't quite right, since research might rise/fall */
-       /* during the update, but it's the best we can really do  */
-       wforce = (int)(((double)civs * sect.sct_work) / 100.0
-                      + uws
-                      + sect.sct_item[I_MILIT] * 2.0 / 5.0);
-       work = new_work(&sect, wforce * etu_per_update / 100);
+       work = new_work(&sect,
+                       total_work(sect.sct_work, etu_per_update,
+                                  civs, sect.sct_item[I_MILIT], uws,
+                                  maxpop));
        bwork = work / 2;
 
        if (sect.sct_off)
@@ -152,25 +134,14 @@ prod(void)
                type = sect.sct_newtype;
                eff = 0;
            }
-           if (opt_BIG_CITY) {
-               if (!eff && dchr[otype].d_pkg == UPKG &&
-                   dchr[type].d_pkg != UPKG) {
-                   if (opt_RES_POP) {
-                       natp = getnatp(sect.sct_own);
-                       civs = min(civs,
-                                  max_pop(natp->nat_level[NAT_RLEV], 0));
-                       uws = min(uws,
-                                 max_pop(natp->nat_level[NAT_RLEV], 0));
-                   } else {
-                       civs = min(9999, civs);
-                       uws = min(9999, uws);
-                   }
-                   wforce = (int)(((double)civs * sect.sct_work) / 100.0
-                                  + uws
-                                  + sect.sct_item[I_MILIT] * 2 / 5.0);
-                   work = etu_per_update * wforce / 100;
-                   bwork = min((int)(work / 2), bwork);
-               }
+           if (!eff && IS_BIG_CITY(otype) && !IS_BIG_CITY(type)) {
+               maxpop = max_population(natp->nat_level[NAT_RLEV],
+                                       type, eff);
+               work = new_work(&sect,
+                               total_work(sect.sct_work, etu_per_update,
+                                          civs, sect.sct_item[I_MILIT],
+                                          uws, maxpop));
+               bwork = MIN(work / 2, bwork);
            }
            twork = 100 - eff;
            if (twork > bwork) {
@@ -199,46 +170,68 @@ prod(void)
            eff += twork;
        }
        work = (work + 1) / 2 + bwork;
-       if (eff < 60 || (type != SCT_ENLIST && eff < 61))
+       if (eff < 60)
            continue;
 
        p_e = eff / 100.0;
        if (p_e > 1.0)
            p_e = 1.0;
 
-       if (dchr[type].d_prd == 0 && type != SCT_ENLIST)
+       if (type == SCT_ENLIST) {
+           int maxmil;
+           int enlisted;
+
+           if (sect.sct_own != sect.sct_oldown)
+               continue;
+           enlisted = 0;
+           maxmil = MIN(civs, maxpop) / 2 - sect.sct_item[I_MILIT];
+           if (maxmil > 0) {
+               enlisted = (etu_per_update
+                           * (10 + sect.sct_item[I_MILIT])
+                           * 0.05);
+               if (enlisted > maxmil)
+                   enlisted = maxmil;
+           }
+           if (enlisted < 0)
+               enlisted = 0;
+           prprod(sect.sct_x, sect.sct_y, type, p_e, 1.0, work,
+                  ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3,
+                  "c\0\0", &enlisted, &enlisted, nsect++);
+           continue;
+       }
+
+       if (dchr[type].d_prd < 0)
            continue;
        unit_work = 0;
        pp = &pchr[dchr[type].d_prd];
        vtype = pp->p_type;
-       natp = getnatp(sect.sct_own);
+       if (pp->p_nrndx)
+           resource = (unsigned char *)&sect + pp->p_nrndx;
+       else
+           resource = NULL;
        /*
         * sect p_e  (inc improvements)
         */
-       if (type == SCT_ENLIST && sect.sct_own != sect.sct_oldown)
-           continue;
-       if (type == SCT_ENLIST)
-           goto is_enlist;
-       if (pp->p_nrndx != 0) {
+       if (resource) {
            unit_work++;
-           resource = ((s_char *)&sect) + pp->p_nrndx;
-           p_e = (*resource * p_e) / 100.0;
+           p_e *= *resource / 100.0;
        }
        /*
         * production effic.
         */
-       prodeff = prod_eff(pp, natp->nat_level[pp->p_nlndx]);
+       prodeff = prod_eff(type, natp->nat_level[pp->p_nlndx]);
        /*
         * raw material limit
         */
        used = 9999;
-       amount = pp->p_vamt;
-       endcomp = pp->p_vtype + pp->p_nv;
-       for (comp = pp->p_vtype; comp < endcomp; comp++, amount++) {
-           if (*amount == 0)
+       for (i = 0; i < MAXPRCON; ++i) {
+           it = pp->p_ctype[i];
+           if (!pp->p_camt[i])
+               continue;
+           if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
                continue;
-           used = min(used, sect.sct_item[(int)*comp] / *amount);
-           unit_work += *amount;
+           used = MIN(used, sect.sct_item[it] / pp->p_camt[i]);
+           unit_work += pp->p_camt[i];
        }
        if (unit_work == 0)
            unit_work = 1;
@@ -247,17 +240,23 @@ prod(void)
         * workforce?
         */
        max = (int)(work * p_e / (double)unit_work + 0.5);
-       act = min(used, max);
+       if (resource && pp->p_nrdep != 0) {
+           if (*resource * 100 < pp->p_nrdep * max)
+               max = *resource * 100 / pp->p_nrdep;
+       }
+       act = MIN(used, max);
 
-       real = dmin(999.0, (double)act * prodeff);
-       maxr = dmin(999.0, (double)max * prodeff);
+       real = (double)act * prodeff;
+       maxr = (double)max * prodeff;
 
-       if (vtype != 0) {
+       if (vtype != I_NONE) {
+           real = MIN(999.0, real);
+           maxr = MIN(999.0, maxr);
            if (real < 0.0)
                real = 0.0;
            /* production backlog? */
-           there = min(ITEM_MAX, sect.sct_item[vtype]);
-           real = dmin(real, ITEM_MAX - there);
+           there = MIN(ITEM_MAX, sect.sct_item[vtype]);
+           real = MIN(real, ITEM_MAX - there);
        }
 
        if (prodeff != 0) {
@@ -266,119 +265,36 @@ prod(void)
        } else
            mtake = take = 0.0;
 
-       cost = (int)(take * (double)pp->p_cost);
+       cost = take * pp->p_cost;
        if (opt_TECH_POP) {
            if (pp->p_level == NAT_TLEV) {
                totpop = count_pop(sect.sct_own);
                if (totpop > 50000)
-                   cost = (int)((double)cost * (double)totpop / 50000.0);
+                   cost *= totpop / 50000.0;
            }
        }
 
-       comp = pp->p_vtype;
-       amount = pp->p_vamt;
-       i = 0;
-       while (comp < endcomp) {
-           it = unitem((int)*comp);
-           if (it > 0 && it <= I_MAX && ichr[it].i_name != 0)
-               c = ichr[it].i_name[0];
-           else
-               c = ' ';
-           (void)sprintf(use[i], " %3d%c",
-                         (int)((take * (double)(*amount)) + 0.5), c);
-           (void)sprintf(maxc[i], " %3d%c",
-                         (int)((mtake * (double)(*amount)) + 0.5), c);
-           ++comp;
-           ++amount;
-           ++i;
-       }
-       while (i < 3) {
-           strcpy(use[i], "     ");
-           strcpy(maxc[i], "     ");
-           ++i;
-       }
-
-      is_enlist:
-
-       if (nsect++ == 0) {
-           pr("PRODUCTION SIMULATION\n");
-           pr("   sect  des eff wkfc will make- p.e. cost  use1 use2 use3  max1 max2 max3  max\n");
-       }
-
-       prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
-       pr(" %c", dchr[type].d_mnem);
-       pr(" %3.0f%%", p_e * 100.0);
-
-       pr(" %4d", wforce);
-       if (vtype != 0) {
-           pr(" %4d", (int)(real + 0.5));
-       } else if (type != SCT_ENLIST) {
-           switch (pp->p_level) {
-           case NAT_TLEV:
-           case NAT_RLEV:
-               pr(" %1.2f", real);
-               break;
-           case NAT_ELEV:
-           case NAT_HLEV:
-               pr(" %4.0f", real);
-               break;
-           default:
-               pr("ERROR");
-               break;
-           }
-       } else {
-           int maxmil;
-           int enlisted;
-           int civs;
-
-           civs = min(999, (int)((obrate * etu_per_update + 1.0)
-                                 * sect.sct_item[I_CIVIL]));
-           natp = getnatp(sect.sct_own);
-           maxpop = max_pop((float)natp->nat_level[NAT_RLEV], &sect);
-           civs = min(civs, maxpop);
-           /* This isn't quite right, since research might
-              rise/fall during the update, but it's the best
-              we can really do  */
-           enlisted = 0;
-           maxmil = (civs / 2) - sect.sct_item[I_MILIT];
-           if (maxmil > 0) {
-               enlisted = (etu_per_update
-                           * (10 + sect.sct_item[I_MILIT])
-                           * 0.05);
-               if (enlisted > maxmil)
-                   enlisted = maxmil;
-           }
-           if (enlisted < 0)
-               enlisted = 0;
-           if (natp->nat_priorities[type] == 0) {
-               maxmil = 0;
-           }
-           pr(" %4d mil   1.00 $%-5d%3dc",
-              enlisted, enlisted * 3, enlisted);
-           pr("            %3dc           %4d\n",
-              enlisted, maxmil);
-           continue;
+       for (i = 0; i < MAXPRCON; ++i) {
+           cmnem[i] = cuse[i] = cmax[i] = 0;
+           if (!pp->p_camt[i])
+               continue;
+           it = pp->p_ctype[i];
+           if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
+               continue;
+           cmnem[i] = ichr[it].i_mnem;
+           cuse[i] = (int)(take * pp->p_camt[i] + 0.5);
+           cmax[i] = (int)(mtake * pp->p_camt[i] + 0.5);
        }
 
-       pr(" %-5.5s", pp->p_sname);
-       pr(" %.2f", prodeff);
-       pr(" $%-4d", cost);
-       for (i = 0; i < 3; i++) {
-           pr(use[i]);
-       }
-       pr(" ");
-       for (i = 0; i < 3; i++) {
-           pr(maxc[i]);
-       }
-       if (natp->nat_priorities[type] == 0) {
-           max = 0;
-           maxr = 0;
-       }
-       if (vtype != 0 || pp->p_level == NAT_ELEV
-           || pp->p_level == NAT_HLEV)
-           pr(" %4d\n", min(999, (int)(max * prodeff + 0.05)));
+       if (pp->p_type != I_NONE)
+           mnem = ichr[vtype].i_mnem;
+       else if (pp->p_level == NAT_TLEV || pp->p_level == NAT_RLEV)
+           mnem = '.';
        else
-           pr(" %1.2f\n", maxr);
+           mnem = 0;
+       prprod(sect.sct_x, sect.sct_y, type, p_e, prodeff, work,
+              mnem, real, maxr, cost,
+              cmnem, cuse, cmax, nsect++);
     }
     player->simulation = 0;
     if (nsect == 0) {
@@ -391,3 +307,41 @@ prod(void)
        pr("%d sector%s\n", nsect, splur(nsect));
     return RET_OK;
 }
+
+static void
+prprod(coord x, coord y, int type, double p_e, double prodeff, int work,
+       char mnem, double make, double max, double cost,
+       char cmnem[], int cuse[], int cmax[], int nsect)
+{
+    int i;
+
+    if (nsect == 0) {
+       pr("PRODUCTION SIMULATION\n");
+       pr("   sect  des eff avail  make p.e. cost   use1 use2 use3  max1 max2 max3   max\n");
+    }
+
+    prxy("%4d,%-4d", x, y);
+    pr(" %c %3.0f%% %5d", dchr[type].d_mnem, p_e * 100.0, work);
+    if (mnem == '.')
+       pr(" %5.2f", make);
+    else
+       pr(" %4.0f%c", make, mnem ? mnem : ' ');
+    pr(" %.2f $%-5.0f", prodeff, cost);
+    for (i = 0; i < 3; i++) {
+       if (i < MAXPRCON && cmnem[i])
+           pr("%4d%c", cuse[i], cmnem[i]);
+       else
+           pr("     ");
+    }
+    pr(" ");
+    for (i = 0; i < 3; i++) {
+       if (i < MAXPRCON && cmnem[i])
+           pr("%4d%c", cmax[i], cmnem[i]);
+       else
+           pr("     ");
+    }
+    if (mnem == '.')
+       pr(" %5.2f\n", max);
+    else
+       pr(" %5.0f\n", max);
+}