]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/prod.c
production: Use update code instead of duplicating it
[empserver] / src / lib / commands / prod.c
index 21ab4e0d9d50ad49a11806e4f4edbba26664bb46..86cd78479e66b02fc13dc7fdc4d874558b9fc563 100644 (file)
 
 #include <config.h>
 
+#include <math.h>
 #include "commands.h"
 #include "item.h"
 #include "optlist.h"
 #include "product.h"
+#include "update.h"
 
 static void prprod(struct sctstr *, double, double, char,
                   double, double, double, char[], int[], int[], int);
@@ -63,27 +65,18 @@ int
 prod(void)
 {
     struct natstr *natp;
-    struct sctstr sect;
+    struct sctstr sect, scratch_sect;
     struct nstr_sect nstr;
     struct pchrstr *pp;
     double p_e;
-    double maxr;               /* floating version of max */
     double prodeff;
-    double real;               /* floating pt version of act */
     int totpop;
-    int material_consume;      /* actual production */
     double cost;
     int i;
-    int max_consume;           /* production w/infinite materials */
     int nsect;
-    double take;
-    double mtake;
-    int there;
-    int unit_work;             /* sum of component amounts */
-    int mat_limit, res_limit;
-    double worker_limit;
+    double real, maxr;
+    double take, mtake;
     i_type it;
-    i_type vtype;
     unsigned char *resource;
     char cmnem[MAXPRCON];
     int cuse[MAXPRCON], cmax[MAXPRCON];
@@ -132,44 +125,26 @@ prod(void)
        if (dchr[sect.sct_type].d_prd < 0)
            continue;
        pp = &pchr[dchr[sect.sct_type].d_prd];
-       vtype = pp->p_type;
        if (pp->p_nrndx)
            resource = (unsigned char *)&sect + pp->p_nrndx;
        else
            resource = NULL;
 
-       mat_limit = prod_materials_cost(pp, sect.sct_item, &unit_work);
-
        /* sector p.e. */
        p_e = sect.sct_effic / 100.0;
-       if (resource) {
-           unit_work++;
+       if (resource)
            p_e *= *resource / 100.0;
-       }
-       if (unit_work == 0)
-           unit_work = 1;
-
-       worker_limit = sect.sct_avail * 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(sect.sct_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? */
-           there = MIN(ITEM_MAX, sect.sct_item[vtype]);
-           real = MIN(real, ITEM_MAX - there);
-       }
+       scratch_sect = sect;
+       real = prod_output(&scratch_sect, prodeff);
+
+       scratch_sect = sect;
+       for (i = 0; i < MAXPRCON; ++i)
+           scratch_sect.sct_item[pp->p_ctype[i]] = ITEM_MAX;
+       scratch_sect.sct_item[pp->p_type] = 0;
+       maxr = prod_output(&scratch_sect, prodeff);
 
        if (prodeff != 0) {
            take = real / prodeff;
@@ -194,12 +169,12 @@ prod(void)
            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);
+           cuse[i] = (int)ceil(take * pp->p_camt[i]);
+           cmax[i] = (int)ceil(mtake * pp->p_camt[i]);
        }
 
        if (pp->p_type != I_NONE)
-           mnem = ichr[vtype].i_mnem;
+           mnem = ichr[pp->p_type].i_mnem;
        else if (pp->p_level == NAT_TLEV || pp->p_level == NAT_RLEV)
            mnem = '.';
        else