]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/prod.c
Update copyright notice
[empserver] / src / lib / commands / prod.c
index 5f0c9a1a6d9d8b76a6e2a9cf0efcb025b7320ba0..6e6e2795e6a7268b54169f874808b738a5fc1ebd 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-2015, 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/>.
  *
  *  ---
  *
  *  ---
  *
  *  prod.c: Calculate production levels
- * 
+ *
  *  Known contributors to this file:
  *     David Muir Sharnoff, 1987
  *     Steve McClure, 1997-2000
- *     Markus Armbruster, 2004-2006
+ *     Markus Armbruster, 2004-2013
  */
 
 #include <config.h>
 
-#include "misc.h"
-#include "player.h"
-#include "xy.h"
-#include "nsc.h"
-#include "sect.h"
-#include "product.h"
-#include "nat.h"
+#include "commands.h"
 #include "item.h"
-#include "file.h"
 #include "optlist.h"
-#include "commands.h"
+#include "product.h"
 
-static void prprod(struct sctstr *, double, double, int, char,
+static void prprod(coord, coord, int, double, double, int, char,
                   double, double, double, char[], int[], int[], int);
 
 int
@@ -80,16 +72,17 @@ prod(void)
     double real;               /* floating pt version of act */
     int work;
     int totpop;
-    int act;                   /* actual production */
+    int material_consume;      /* actual production */
     double cost;
     int i;
-    int max;                   /* production w/infinite materials */
+    int max_consume;           /* 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 mat_limit, res_limit;
+    double worker_limit;
     i_type it;
     i_type vtype;
     unsigned char *resource;
@@ -143,7 +136,6 @@ prod(void)
                eff = 0;
            }
            if (!eff && IS_BIG_CITY(otype) && !IS_BIG_CITY(type)) {
-               natp = getnatp(sect.sct_own);
                maxpop = max_population(natp->nat_level[NAT_RLEV],
                                        type, eff);
                work = new_work(&sect,
@@ -182,22 +174,14 @@ prod(void)
        if (eff < 60)
            continue;
 
-       p_e = eff / 100.0;
-       if (p_e > 1.0)
-           p_e = 1.0;
-
        if (type == SCT_ENLIST) {
            int maxmil;
            int enlisted;
 
            if (sect.sct_own != sect.sct_oldown)
                continue;
-           civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL];
-           natp = getnatp(sect.sct_own);
-           maxpop = max_pop(natp->nat_level[NAT_RLEV], &sect);
-           civs = MIN(civs, maxpop);
            enlisted = 0;
-           maxmil = (civs / 2) - sect.sct_item[I_MILIT];
+           maxmil = MIN(civs, maxpop) / 2 - sect.sct_item[I_MILIT];
            if (maxmil > 0) {
                enlisted = (etu_per_update
                            * (10 + sect.sct_item[I_MILIT])
@@ -207,60 +191,47 @@ prod(void)
            }
            if (enlisted < 0)
                enlisted = 0;
-           prprod(&sect, p_e, 1.0, work,
+           prprod(sect.sct_x, sect.sct_y, type, eff / 100.0, 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)
+       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);
-       /*
-        * sect p_e  (inc improvements)
-        */
-       if (pp->p_nrndx != 0) {
-           unit_work++;
+       if (pp->p_nrndx)
            resource = (unsigned char *)&sect + pp->p_nrndx;
-           p_e = (*resource * p_e) / 100.0;
-       }
-       /*
-        * production effic.
-        */
-       prodeff = prod_eff(type, natp->nat_level[pp->p_nlndx]);
-       /*
-        * raw material limit
-        */
-       used = 9999;
-       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[it] / pp->p_camt[i]);
-           unit_work += pp->p_camt[i];
+       else
+           resource = NULL;
+
+       mat_limit = prod_materials_cost(pp, sect.sct_item, &unit_work);
+
+       /* sector p.e. */
+       p_e = eff / 100.0;
+       if (resource) {
+           unit_work++;
+           p_e *= *resource / 100.0;
        }
        if (unit_work == 0)
            unit_work = 1;
-       /*
-        * is production limited by resources or
-        * workforce?
-        */
-       max = (int)(work * p_e / (double)unit_work + 0.5);
-       if (pp->p_nrdep != 0 && vtype != I_NONE) {
-           if (*resource * 100 < pp->p_nrdep * max)
-               max = *resource * 100 / pp->p_nrdep;
-       }
-       act = MIN(used, max);
 
-       real = MIN(999.0, (double)act * prodeff);
-       maxr = MIN(999.0, (double)max * prodeff);
+       worker_limit = work * p_e / (double)unit_work;
+       res_limit = prod_resource_limit(pp, resource);
+
+       max_consume = res_limit;
+       if (max_consume > worker_limit)
+           max_consume = (int)worker_limit;
+       material_consume = MIN(max_consume, mat_limit);
+
+       prodeff = prod_eff(type, natp->nat_level[pp->p_nlndx]);
+       real = (double)material_consume * prodeff;
+       maxr = (double)max_consume * prodeff;
 
        if (vtype != I_NONE) {
+           real = MIN(999.0, real);
+           maxr = MIN(999.0, maxr);
            if (real < 0.0)
                real = 0.0;
            /* production backlog? */
@@ -301,7 +272,7 @@ prod(void)
            mnem = '.';
        else
            mnem = 0;
-       prprod(&sect, p_e, prodeff, work,
+       prprod(sect.sct_x, sect.sct_y, type, p_e, prodeff, work,
               mnem, real, maxr, cost,
               cmnem, cuse, cmax, nsect++);
     }
@@ -317,8 +288,8 @@ prod(void)
     return RET_OK;
 }
 
-void
-prprod(struct sctstr *sp, double p_e, double prodeff, int work,
+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)
 {
@@ -329,8 +300,8 @@ prprod(struct sctstr *sp, double p_e, double prodeff, int work,
        pr("   sect  des eff avail  make p.e. cost   use1 use2 use3  max1 max2 max3   max\n");
     }
 
-    prxy("%4d,%-4d", sp->sct_x, sp->sct_y, player->cnum);
-    pr(" %c %3.0f%% %5d", dchr[sp->sct_type].d_mnem, p_e * 100.0, work);
+    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