]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/produce.c
Update copyright notice
[empserver] / src / lib / update / produce.c
index f0bb792ad710bdc3398a663256bcfd7f9e5fd896..4c777bfd0fcaeee2b985bd131ba53d55158e91e0 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-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,
@@ -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/>.
  *
  *  ---
  *
  *  ---
  *
  *  produce.c: Produce goodies
- * 
+ *
  *  Known contributors to this file:
- *    
+ *     Markus Armbruster, 2004-2010
  */
 
 #include <config.h>
 
-#include "misc.h"
-#include "sect.h"
-#include "product.h"
-#include "nat.h"
-#include "xy.h"
+#include "budg.h"
 #include "player.h"
+#include "product.h"
 #include "update.h"
-#include "gen.h"
-#include "subs.h"
-#include "common.h"
-#include "optlist.h"
-#include "budg.h"
 
 static void materials_charge(struct pchrstr *, short *, int);
 static int materials_cost(struct pchrstr *, short *, int *);
@@ -60,7 +51,7 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
     struct pchrstr *product;
     double p_e;
     double prodeff;
-    u_char *resource;
+    unsigned char *resource;
     double output;
     int actual;
     int unit_work;
@@ -70,10 +61,14 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
     int material_consume;
     int val;
 
-    product = &pchr[dchr[desig].d_prd];
-    if (product == &pchr[0])
+    if (dchr[desig].d_prd < 0)
        return 0;
+    product = &pchr[dchr[desig].d_prd];
     item = product->p_type;
+    if (product->p_nrndx)
+       resource = (unsigned char *)sp + product->p_nrndx;
+    else
+       resource = NULL;
     *amount = 0;
     *cost = 0;
 
@@ -83,10 +78,9 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
      * calculate production efficiency.
      */
     p_e = neweff / 100.0;
-    if (product->p_nrndx != 0) {
+    if (resource) {
        unit_work++;
-       resource = (u_char *)sp + product->p_nrndx;
-       p_e = (*resource * p_e) / 100.0;
+       p_e *= *resource / 100.0;
     }
     /*
      * determine number that can be made with
@@ -98,9 +92,13 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
     worker_limit = roundavg(work * p_e / unit_work);
     if (material_consume > worker_limit)
        material_consume = worker_limit;
+    if (resource && product->p_nrdep != 0) {
+       if (*resource * 100 < product->p_nrdep * material_consume)
+           material_consume = *resource * 100 / product->p_nrdep;
+    }
     if (material_consume == 0)
        return 0;
-    prodeff = prod_eff(product, np->nat_level[product->p_nlndx]);
+    prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]);
     if (prodeff <= 0.0 && !player->simulation) {
        wu(0, sp->sct_own,
           "%s level too low to produce in %s (need %d)\n",
@@ -111,20 +109,17 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
      * Adjust produced amount by commodity production ratio
      */
     output = material_consume * prodeff;
-    actual = roundavg(output);
-    if (actual <= 0)
-       return 0;
     if (item == I_NONE) {
+       actual = ldround(output, 1);
        if (!player->simulation) {
            levels[sp->sct_own][product->p_level] += output;
-           wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
+           wu(0, sp->sct_own, "%s (%.2f) produced in %s\n",
               product->p_name, output, ownxy(sp));
        }
     } else {
-       if (product->p_nrdep != 0) {
-           if (*resource * 100 < product->p_nrdep * actual)
-               actual = *resource * 100 / product->p_nrdep;
-       }
+       actual = roundavg(output);
+       if (actual <= 0)
+           return 0;
        if (actual > 999) {
            material_consume = roundavg(999.0 * material_consume / actual);
            actual = 999;
@@ -134,20 +129,20 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
                                        * material_consume / actual);
            if (material_consume < 0)
                material_consume = 0;
-           vec[item] = ITEM_MAX;
+           actual = ITEM_MAX - vec[item];
            if (sp->sct_own && !player->simulation)
                wu(0, sp->sct_own,
                   "%s production backlog in %s\n",
                   product->p_name, ownxy(sp));
-       } else
-           vec[item] += actual;
+       }
+       vec[item] += actual;
     }
     /*
      * Reset produced amount by commodity production ratio
      */
     if (!player->simulation) {
        materials_charge(product, vec, material_consume);
-       if (product->p_nrdep != 0) {
+       if (resource && product->p_nrdep != 0) {
            /*
             * lower natural resource in sector depending on
             * amount produced
@@ -165,8 +160,7 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
     if (opt_TECH_POP) {
        if (product->p_level == NAT_TLEV) {
            if (tpops[sp->sct_own] > 50000)
-               *cost =
-                   (double)*cost * (double)tpops[sp->sct_own] / 50000.0;
+               *cost *= tpops[sp->sct_own] / 50000.0;
        }
     }
 
@@ -220,14 +214,19 @@ materials_charge(struct pchrstr *pp, short *vec, int count)
 }
 
 /*
- * Return level p.e. for product PP.
+ * Return level p.e. for sector type TYPE.
  * Zero means level is too low for production.
  * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
  */
 double
-prod_eff(struct pchrstr *pp, float level)
+prod_eff(int type, float level)
 {
     double level_p_e;
+    struct dchrstr *dp = &dchr[type];
+    struct pchrstr *pp = &pchr[dp->d_prd];
+
+    if (CANT_HAPPEN(dp->d_prd < 0))
+       return 0.0;
 
     if (pp->p_nlndx < 0)
        level_p_e = 1.0;
@@ -241,5 +240,5 @@ prod_eff(struct pchrstr *pp, float level)
        level_p_e = delta / (delta + pp->p_nllag);
     }
 
-    return level_p_e * pp->p_effic * 0.01;
+    return level_p_e * dp->d_peffic * 0.01;
 }