]> 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 65ea12037c8bee0d410340efc77c3d9f13702d12..86cd78479e66b02fc13dc7fdc4d874558b9fc563 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
  *  Known contributors to this file:
  *     David Muir Sharnoff, 1987
  *     Steve McClure, 1997-2000
- *     Markus Armbruster, 2004-2010
+ *     Markus Armbruster, 2004-2016
  */
 
 #include <config.h>
 
+#include <math.h>
 #include "commands.h"
 #include "item.h"
 #include "optlist.h"
 #include "product.h"
+#include "update.h"
 
-static void prprod(coord, coord, int, double, double, int, char,
+static void prprod(struct sctstr *, double, double, char,
                   double, double, double, char[], int[], int[], int);
 
 int
@@ -63,39 +65,21 @@ 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 work;
     int totpop;
-    int act;                   /* actual production */
     double cost;
     int i;
-    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 */
+    double real, maxr;
+    double take, mtake;
     i_type it;
-    i_type vtype;
     unsigned char *resource;
     char cmnem[MAXPRCON];
     int cuse[MAXPRCON], cmax[MAXPRCON];
-    int lcms, hcms;
-    int civs;
-    int uws;
-    int bwork;
-    int twork;
-    int type;
-    int eff;
-    int maxpop;
-    int otype;
     char mnem;
 
     if (!snxtsct(&nstr, player->argp[1]))
@@ -106,90 +90,23 @@ prod(void)
     while (nxtsct(&nstr, &sect)) {
        if (!player->owner)
            continue;
-
-       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);
-
-       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)
            continue;
-       type = sect.sct_type;
-       eff = sect.sct_effic;
-       if (sect.sct_newtype != type) {
-           twork = (eff + 3) / 4;
-           if (twork > bwork) {
-               twork = bwork;
-           }
-           bwork -= twork;
-           eff -= twork * 4;
-           otype = type;
-           if (eff <= 0) {
-               type = sect.sct_newtype;
-               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,
-                               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) {
-               twork = bwork;
-           }
-           if (dchr[type].d_lcms > 0) {
-               lcms = sect.sct_item[I_LCM];
-               lcms /= dchr[type].d_lcms;
-               if (twork > lcms)
-                   twork = lcms;
-           }
-           if (dchr[type].d_hcms > 0) {
-               hcms = sect.sct_item[I_HCM];
-               hcms /= dchr[type].d_hcms;
-               if (twork > hcms)
-                   twork = hcms;
-           }
-           bwork -= twork;
-           eff += twork;
-       } else if (eff < 100) {
-           twork = 100 - eff;
-           if (twork > bwork) {
-               twork = bwork;
-           }
-           bwork -= twork;
-           eff += twork;
-       }
-       work = (work + 1) / 2 + bwork;
-       if (eff < 60)
-           continue;
 
-       p_e = eff / 100.0;
-       if (p_e > 1.0)
-           p_e = 1.0;
+       natp = getnatp(sect.sct_own);
+       do_feed(&sect, natp, etu_per_update, 1);
+       buildeff(&sect);
+       if (sect.sct_effic < 60)
+           continue;
 
-       if (type == SCT_ENLIST) {
+       if (sect.sct_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 = sect.sct_item[I_CIVIL] / 2 - sect.sct_item[I_MILIT];
            if (maxmil > 0) {
                enlisted = (etu_per_update
                            * (10 + sect.sct_item[I_MILIT])
@@ -199,71 +116,35 @@ prod(void)
            }
            if (enlisted < 0)
                enlisted = 0;
-           prprod(sect.sct_x, sect.sct_y, type, p_e, 1.0, work,
+           prprod(&sect, sect.sct_effic / 100.0, 1.0,
                   ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3,
                   "c\0\0", &enlisted, &enlisted, nsect++);
            continue;
        }
 
-       if (dchr[type].d_prd < 0)
+       if (dchr[sect.sct_type].d_prd < 0)
            continue;
-       unit_work = 0;
-       pp = &pchr[dchr[type].d_prd];
-       vtype = pp->p_type;
+       pp = &pchr[dchr[sect.sct_type].d_prd];
        if (pp->p_nrndx)
            resource = (unsigned char *)&sect + pp->p_nrndx;
        else
            resource = NULL;
-       natp = getnatp(sect.sct_own);
-       /*
-        * sect p_e  (inc improvements)
-        */
-       if (resource) {
-           unit_work++;
+
+       /* sector p.e. */
+       p_e = sect.sct_effic / 100.0;
+       if (resource)
            p_e *= *resource / 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];
-       }
-       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 (resource && pp->p_nrdep != 0) {
-           if (*resource * 100 < pp->p_nrdep * max)
-               max = *resource * 100 / pp->p_nrdep;
-       }
-       act = MIN(used, max);
 
-       real = (double)act * prodeff;
-       maxr = (double)max * prodeff;
+       prodeff = prod_eff(sect.sct_type, natp->nat_level[pp->p_nlndx]);
 
-       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;
@@ -288,18 +169,17 @@ 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
            mnem = 0;
-       prprod(sect.sct_x, sect.sct_y, type, p_e, prodeff, work,
-              mnem, real, maxr, cost,
+       prprod(&sect, p_e, prodeff, mnem, real, maxr, cost,
               cmnem, cuse, cmax, nsect++);
     }
     player->simulation = 0;
@@ -315,7 +195,7 @@ prod(void)
 }
 
 static void
-prprod(coord x, coord y, int type, double p_e, double prodeff, int work,
+prprod(struct sctstr *sp, double p_e, double prodeff,
        char mnem, double make, double max, double cost,
        char cmnem[], int cuse[], int cmax[], int nsect)
 {
@@ -326,8 +206,9 @@ prprod(coord x, coord y, int type, 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", x, y);
-    pr(" %c %3.0f%% %5d", dchr[type].d_mnem, p_e * 100.0, work);
+    prxy("%4d,%-4d", sp->sct_x, sp->sct_y);
+    pr(" %c %3.0f%% %5d",
+       dchr[sp->sct_type].d_mnem, p_e * 100.0, sp->sct_avail);
     if (mnem == '.')
        pr(" %5.2f", make);
     else