]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/produce.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / update / produce.c
index 0d459431f90fc787a0a18cab0b43cc458a63a768..bb53300377c024014a9ba93db2f2d11ae92aa907 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2006, 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.
  *
  *  ---
  *
  *    
  */
 
+#include <config.h>
+
 #include "misc.h"
-#include "var.h"
 #include "sect.h"
 #include "product.h"
 #include "nat.h"
-#include "file.h"
 #include "xy.h"
 #include "player.h"
 #include "update.h"
@@ -56,16 +56,14 @@ int
 produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
        int desig, int neweff, int *cost, int *amount)
 {
-    register struct pchrstr *product;
-    int vtype;
+    struct pchrstr *product;
     double p_e;
-    double level_p_e;
-    s_char *resource;
-    int output;
+    double prodeff;
+    u_char *resource;
+    double output;
     int actual;
     int unit_work;
-    double depend;
-    int item;
+    i_type item;
     int worker_limit;
     int material_limit;
     int material_consume;
@@ -74,8 +72,7 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
     product = &pchr[dchr[desig].d_prd];
     if (product == &pchr[0])
        return 0;
-    vtype = product->p_type;
-    item = vtype & ~VT_ITEM;
+    item = product->p_type;
     *amount = 0;
     *cost = 0;
 
@@ -87,14 +84,8 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
     p_e = neweff / 100.0;
     if (product->p_nrndx != 0) {
        unit_work++;
-       resource = ((s_char *)sp) + product->p_nrndx;
+       resource = (u_char *)sp + product->p_nrndx;
        p_e = (*resource * p_e) / 100.0;
-       if (product->p_nrdep > 0) {
-           /* XXX this looks way wrong */
-           depend = (*resource * 100.0) / product->p_nrdep;
-           if (p_e > depend)
-               p_e = depend;
-       }
     }
     /*
      * determine number that can be made with
@@ -108,44 +99,42 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
        material_consume = worker_limit;
     if (material_consume == 0)
        return 0;
-    level_p_e = 1.0;
-    if (product->p_nlndx >= 0) {
-       level_p_e = np->nat_level[product->p_nlndx] - product->p_nlmin;
-       if ((level_p_e < 0.0) && (!player->simulation)) {
-           wu(0, sp->sct_own,
-              "%s level too low to produce in %s (need %d)\n",
-              levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
-           return 0;
-       }
-       level_p_e = level_p_e / (level_p_e + product->p_nllag);
+    prodeff = prod_eff(product, 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",
+          levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
+       return 0;
     }
     /*
      * Adjust produced amount by commodity production ratio
      */
-    output = roundavg(product->p_effic * 0.01 * material_consume);
-    if ((vtype == 0) && (!player->simulation)) {
-       levels[sp->sct_own][product->p_level] += output * level_p_e;
-       wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
-          product->p_name, output * level_p_e, ownxy(sp));
+    output = material_consume * prodeff;
+    actual = roundavg(output);
+    if (actual <= 0)
+       return 0;
+    if (item == I_NONE) {
+       if (!player->simulation) {
+           levels[sp->sct_own][product->p_level] += output;
+           wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
+              product->p_name, output, ownxy(sp));
+       }
     } else {
-       if ((actual = roundavg(level_p_e * output)) <= 0)
-           return 0;
        if (product->p_nrdep != 0) {
            if (*resource * 100 < product->p_nrdep * actual)
                actual = *resource * 100 / product->p_nrdep;
        }
        if (actual > 999) {
+           material_consume = roundavg(999.0 * material_consume / actual);
            actual = 999;
-           material_consume = (int)(actual / (product->p_effic * 0.01));
        }
        if (vec[item] + actual > ITEM_MAX) {
-           material_consume =
-               roundavg((ITEM_MAX - vec[item]) * material_consume / actual);
+           material_consume = roundavg((double)(ITEM_MAX - vec[item])
+                                       * material_consume / actual);
            if (material_consume < 0)
                material_consume = 0;
            vec[item] = ITEM_MAX;
-           if (( /* vtype != V_FOOD && */ sp->sct_own) &&
-               (!player->simulation))
+           if (sp->sct_own && !player->simulation)
                wu(0, sp->sct_own,
                   "%s production backlog in %s\n",
                   product->p_name, ownxy(sp));
@@ -180,61 +169,76 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
        }
     }
 
-    /* The min() here is to take care of integer rounding errors */
+    /* The MIN() here is to take care of integer rounding errors */
     if (p_e > 0.0) {
-       return min(work, (int)(unit_work * material_consume / p_e));
+       return MIN(work, (int)(unit_work * material_consume / p_e));
     }
     return 0;
 }
 
 static int
-materials_cost(struct pchrstr *product, short *vec, int *costp)
+materials_cost(struct pchrstr *pp, short *vec, int *costp)
 {
-    register u_char *vp;
-    register u_short *ap;
-    register int count;
-    register int cost;
-    register int n;
-    register u_char *endp;
+    int count;
+    int cost;
+    int i, n;
 
     count = 9999;
     cost = 0;
-    ap = product->p_vamt;
-    endp = product->p_vtype + product->p_nv;
-    for (vp = product->p_vtype; vp < endp; vp++, ap++) {
-       if (!*ap)
+    for (i = 0; i < MAXPRCON; ++i) {
+       if (!pp->p_camt[i])
+           continue;
+       if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
            continue;
-       n = vec[*vp & ~VT_ITEM] / *ap;
+       n = vec[pp->p_ctype[i]] / pp->p_camt[i];
        if (n < count)
            count = n;
-       cost += *ap;
+       cost += pp->p_camt[i];
     }
     *costp = cost;
     return count;
 }
 
 static void
-materials_charge(struct pchrstr *product, short *vec, int count)
+materials_charge(struct pchrstr *pp, short *vec, int count)
 {
-    register u_char *vp;
-    register u_short *ap;
-    register u_char *endp;
-    register int item;
-    register int n;
-
-    ap = product->p_vamt;
-    endp = product->p_vtype + product->p_nv;
-    for (vp = product->p_vtype; vp < endp; vp++, ap++) {
-       item = *vp & ~VT_ITEM;
-       if (item < 0 || item > I_MAX) {
-           logerror("materials_charge: bad item %d", item);
+    int i, n;
+    i_type item;
+
+    for (i = 0; i < MAXPRCON; ++i) {
+       item = pp->p_ctype[i];
+       if (!pp->p_camt[i])
            continue;
-       }
-       if ((n = vec[item] - *ap * count) < 0) {
-           logerror("materials_charge: %d > %d item #%d",
-                    n, vec[item], item);
+       if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
+           continue;
+       n = vec[item] - pp->p_camt[i] * count;
+       if (CANT_HAPPEN(n < 0))
            n = 0;
-       }
        vec[item] = n;
     }
 }
+
+/*
+ * Return level p.e. for product PP.
+ * 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)
+{
+    double level_p_e;
+
+    if (pp->p_nlndx < 0)
+       level_p_e = 1.0;
+    else {
+       double delta = (double)level - (double)pp->p_nlmin;
+
+       if (delta < 0.0)
+           return 0.0;
+       if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
+           return 0.0;
+       level_p_e = delta / (delta + pp->p_nllag);
+    }
+
+    return level_p_e * pp->p_effic * 0.01;
+}