neweff prod work: Use update code instead of duplicating it

The code to build sectors got quadruplicated in Chainsaw.  We've since
fixed numerous inconsistencies, but still have four copies of the
code.  Thanks to the recent work on upd_buildeff(), we can now use it
to replace the other three copies.  Rename it back to to buildeff()
while there.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2016-06-04 10:55:44 +02:00
parent 41a2f7d1df
commit b7b99c95c2
5 changed files with 31 additions and 191 deletions

View file

@ -713,6 +713,7 @@ extern int update_removewants(void);
extern void revolt(struct sctstr *); extern void revolt(struct sctstr *);
extern void guerrilla(struct sctstr *); extern void guerrilla(struct sctstr *);
/* sect.c */ /* sect.c */
extern int buildeff(struct sctstr *);
extern void do_fallout(struct sctstr *, int); extern void do_fallout(struct sctstr *, int);
extern void spread_fallout(struct sctstr *, int); extern void spread_fallout(struct sctstr *, int);
extern void decay_fallout(struct sctstr *, int); extern void decay_fallout(struct sctstr *, int);

View file

@ -43,12 +43,7 @@ newe(void)
struct natstr *natp; struct natstr *natp;
struct sctstr sect; struct sctstr sect;
struct nstr_sect nstr; struct nstr_sect nstr;
double work, lcms, hcms;
int nsect; int nsect;
int bwork;
int twork;
int type;
int eff;
if (!snxtsct(&nstr, player->argp[1])) if (!snxtsct(&nstr, player->argp[1]))
return RET_SYN; return RET_SYN;
@ -60,70 +55,16 @@ newe(void)
continue; continue;
if (!sect.sct_off) { if (!sect.sct_off) {
natp = getnatp(sect.sct_own); natp = getnatp(sect.sct_own);
work = do_feed(&sect, natp, etu_per_update, 1); sect.sct_avail = do_feed(&sect, natp, etu_per_update, 1);
bwork = work / 2; buildeff(&sect);
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;
if (eff <= 0) {
type = sect.sct_newtype;
eff = 0;
}
twork = 100 - eff;
if (twork > bwork) {
twork = bwork;
}
if (dchr[type].d_lcms > 0) {
lcms = sect.sct_item[I_LCM];
lcms = (int)(lcms / dchr[type].d_lcms);
if (twork > lcms)
twork = lcms;
}
if (dchr[type].d_hcms > 0) {
hcms = sect.sct_item[I_HCM];
hcms = (int)(hcms / dchr[type].d_hcms);
if (twork > hcms)
twork = hcms;
}
eff += twork;
} else if (eff < 100) {
twork = 100 - eff;
if (twork > bwork) {
twork = bwork;
}
if (dchr[type].d_lcms > 0) {
lcms = sect.sct_item[I_LCM];
lcms = (int)(lcms / dchr[type].d_lcms);
if (twork > lcms)
twork = lcms;
}
if (dchr[type].d_hcms > 0) {
hcms = sect.sct_item[I_HCM];
hcms = (int)(hcms / dchr[type].d_hcms);
if (twork > hcms)
twork = hcms;
}
eff += twork;
}
} else {
eff = sect.sct_effic;
type = sect.sct_type;
} }
if (nsect++ == 0) { if (nsect++ == 0) {
pr("EFFICIENCY SIMULATION\n"); pr("EFFICIENCY SIMULATION\n");
pr(" sect des projected eff\n"); pr(" sect des projected eff\n");
} }
prxy("%4d,%-4d", nstr.x, nstr.y); prxy("%4d,%-4d", nstr.x, nstr.y);
pr(" %c", dchr[type].d_mnem); pr(" %c", dchr[sect.sct_type].d_mnem);
pr(" %3d%%\n", eff); pr(" %3d%%\n", sect.sct_effic);
} }
player->simulation = 0; player->simulation = 0;
if (nsect == 0) { if (nsect == 0) {

View file

@ -39,7 +39,7 @@
#include "optlist.h" #include "optlist.h"
#include "product.h" #include "product.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); double, double, double, char[], int[], int[], int);
int int
@ -70,7 +70,6 @@ prod(void)
double maxr; /* floating version of max */ double maxr; /* floating version of max */
double prodeff; double prodeff;
double real; /* floating pt version of act */ double real; /* floating pt version of act */
int work;
int totpop; int totpop;
int material_consume; /* actual production */ int material_consume; /* actual production */
double cost; double cost;
@ -88,11 +87,6 @@ prod(void)
unsigned char *resource; unsigned char *resource;
char cmnem[MAXPRCON]; char cmnem[MAXPRCON];
int cuse[MAXPRCON], cmax[MAXPRCON]; int cuse[MAXPRCON], cmax[MAXPRCON];
int lcms, hcms;
int bwork;
int twork;
int type;
int eff;
char mnem; char mnem;
if (!snxtsct(&nstr, player->argp[1])) if (!snxtsct(&nstr, player->argp[1]))
@ -107,53 +101,12 @@ prod(void)
continue; continue;
natp = getnatp(sect.sct_own); natp = getnatp(sect.sct_own);
work = do_feed(&sect, natp, etu_per_update, 1); sect.sct_avail = do_feed(&sect, natp, etu_per_update, 1);
bwork = work / 2; buildeff(&sect);
if (sect.sct_effic < 60)
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;
if (eff <= 0) {
type = sect.sct_newtype;
eff = 0;
}
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; continue;
if (type == SCT_ENLIST) { if (sect.sct_type == SCT_ENLIST) {
int maxmil; int maxmil;
int enlisted; int enlisted;
@ -170,15 +123,15 @@ prod(void)
} }
if (enlisted < 0) if (enlisted < 0)
enlisted = 0; enlisted = 0;
prprod(sect.sct_x, sect.sct_y, type, eff / 100.0, 1.0, work, prprod(&sect, sect.sct_effic / 100.0, 1.0,
ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3, ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3,
"c\0\0", &enlisted, &enlisted, nsect++); "c\0\0", &enlisted, &enlisted, nsect++);
continue; continue;
} }
if (dchr[type].d_prd < 0) if (dchr[sect.sct_type].d_prd < 0)
continue; continue;
pp = &pchr[dchr[type].d_prd]; pp = &pchr[dchr[sect.sct_type].d_prd];
vtype = pp->p_type; vtype = pp->p_type;
if (pp->p_nrndx) if (pp->p_nrndx)
resource = (unsigned char *)&sect + pp->p_nrndx; resource = (unsigned char *)&sect + pp->p_nrndx;
@ -188,7 +141,7 @@ prod(void)
mat_limit = prod_materials_cost(pp, sect.sct_item, &unit_work); mat_limit = prod_materials_cost(pp, sect.sct_item, &unit_work);
/* sector p.e. */ /* sector p.e. */
p_e = eff / 100.0; p_e = sect.sct_effic / 100.0;
if (resource) { if (resource) {
unit_work++; unit_work++;
p_e *= *resource / 100.0; p_e *= *resource / 100.0;
@ -196,7 +149,7 @@ prod(void)
if (unit_work == 0) if (unit_work == 0)
unit_work = 1; unit_work = 1;
worker_limit = work * p_e / (double)unit_work; worker_limit = sect.sct_avail * p_e / (double)unit_work;
res_limit = prod_resource_limit(pp, resource); res_limit = prod_resource_limit(pp, resource);
max_consume = res_limit; max_consume = res_limit;
@ -204,7 +157,7 @@ prod(void)
max_consume = (int)worker_limit; max_consume = (int)worker_limit;
material_consume = MIN(max_consume, mat_limit); material_consume = MIN(max_consume, mat_limit);
prodeff = prod_eff(type, natp->nat_level[pp->p_nlndx]); prodeff = prod_eff(sect.sct_type, natp->nat_level[pp->p_nlndx]);
real = (double)material_consume * prodeff; real = (double)material_consume * prodeff;
maxr = (double)max_consume * prodeff; maxr = (double)max_consume * prodeff;
@ -251,8 +204,7 @@ prod(void)
mnem = '.'; mnem = '.';
else else
mnem = 0; mnem = 0;
prprod(sect.sct_x, sect.sct_y, type, p_e, prodeff, work, prprod(&sect, p_e, prodeff, mnem, real, maxr, cost,
mnem, real, maxr, cost,
cmnem, cuse, cmax, nsect++); cmnem, cuse, cmax, nsect++);
} }
player->simulation = 0; player->simulation = 0;
@ -268,7 +220,7 @@ prod(void)
} }
static 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 mnem, double make, double max, double cost,
char cmnem[], int cuse[], int cmax[], int nsect) char cmnem[], int cuse[], int cmax[], int nsect)
{ {
@ -279,8 +231,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"); pr(" sect des eff avail make p.e. cost use1 use2 use3 max1 max2 max3 max\n");
} }
prxy("%4d,%-4d", x, y); prxy("%4d,%-4d", sp->sct_x, sp->sct_y);
pr(" %c %3.0f%% %5d", dchr[type].d_mnem, p_e * 100.0, work); pr(" %c %3.0f%% %5d",
dchr[sp->sct_type].d_mnem, p_e * 100.0, sp->sct_avail);
if (mnem == '.') if (mnem == '.')
pr(" %5.2f", make); pr(" %5.2f", make);
else else

View file

@ -27,7 +27,7 @@
* work.c: Implementation of the work command * work.c: Implementation of the work command
* *
* Known contributors to this file: * Known contributors to this file:
* Markus Armbruster, 2009-2010 * Markus Armbruster, 2009-2016
*/ */
#include <config.h> #include <config.h>
@ -37,8 +37,6 @@
#include "land.h" #include "land.h"
#include "optlist.h" #include "optlist.h"
static int buildeff(struct sctstr *, int, double *);
int int
work(void) work(void)
{ {
@ -46,7 +44,7 @@ work(void)
struct nstr_item ni; struct nstr_item ni;
struct sctstr sect; struct sctstr sect;
struct lndstr land; struct lndstr land;
int work_amt, eff_amt, w; int work_amt, eff_amt, w, sect_avail;
char *p; char *p;
char buf[1024]; char buf[1024];
double cost; double cost;
@ -85,8 +83,11 @@ work(void)
prland(&land)); prland(&land));
continue; continue;
} }
cost = 0.0; sect_avail = sect.sct_avail;
w = buildeff(&sect, w, &cost); sect.sct_avail = w;
cost = buildeff(&sect);
w = sect.sct_avail - sect_avail;
sect.sct_avail = sect_avail;
if (w == 0) { if (w == 0) {
pr("%s can't change efficiency in %s\n", pr("%s can't change efficiency in %s\n",
prland(&land), xyas(land.lnd_x, land.lnd_y, player->cnum)); prland(&land), xyas(land.lnd_x, land.lnd_y, player->cnum));
@ -119,59 +120,3 @@ work(void)
pr("%d unit%s\n", nunits, splur(nunits)); pr("%d unit%s\n", nunits, splur(nunits));
return RET_OK; return RET_OK;
} }
static int
buildeff(struct sctstr *sp, int work, double *money)
{
int work_cost;
int n, hcms, lcms;
int effdone = 0;
if (sp->sct_type != sp->sct_newtype) {
/*
* Tear down existing sector.
* Easier to destroy than to build.
*/
work_cost = (sp->sct_effic + 3) / 4;
if (work_cost > work)
work_cost = work;
n = sp->sct_effic - work_cost * 4;
if (n <= 0) {
n = 0;
sp->sct_type = sp->sct_newtype;
}
sp->sct_effic = n;
work -= work_cost;
*money += work_cost;
effdone += work_cost;
}
if (sp->sct_type == sp->sct_newtype) {
work_cost = 100 - sp->sct_effic;
if (work_cost > work)
work_cost = work;
if (dchr[sp->sct_type].d_lcms > 0) {
lcms = sp->sct_item[I_LCM];
lcms /= dchr[sp->sct_type].d_lcms;
if (work_cost > lcms)
work_cost = lcms;
}
if (dchr[sp->sct_type].d_hcms > 0) {
hcms = sp->sct_item[I_HCM];
hcms /= dchr[sp->sct_type].d_hcms;
if (work_cost > hcms)
work_cost = hcms;
}
sp->sct_effic += work_cost;
*money += work_cost * dchr[sp->sct_type].d_build;
if ((dchr[sp->sct_type].d_lcms > 0) ||
(dchr[sp->sct_type].d_hcms > 0)) {
sp->sct_item[I_LCM] -= work_cost * dchr[sp->sct_type].d_lcms;
sp->sct_item[I_HCM] -= work_cost * dchr[sp->sct_type].d_hcms;
}
effdone += work_cost;
}
return effdone;
}

View file

@ -44,8 +44,8 @@
#include "ship.h" #include "ship.h"
#include "update.h" #include "update.h"
static int int
upd_buildeff(struct sctstr *sp) buildeff(struct sctstr *sp)
{ {
int work_cost = 0; int work_cost = 0;
int avail = sp->sct_avail; int avail = sp->sct_avail;
@ -304,7 +304,7 @@ produce_sect(struct natstr *np, int etu, struct bp *bp, int p_sect[][2])
if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) && if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
np->nat_money >= 0) { np->nat_money >= 0) {
cost = upd_buildeff(sp); cost = buildeff(sp);
bp_put_items(bp, sp); bp_put_items(bp, sp);
p_sect[SCT_EFFIC][0]++; p_sect[SCT_EFFIC][0]++;
p_sect[SCT_EFFIC][1] += cost; p_sect[SCT_EFFIC][1] += cost;