/*
* 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
*
* ---
*
- * 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.
*
* ---
*
* Known contributors to this file:
* David Muir Sharnoff, 1987
* Steve McClure, 1997-2000
+ * Markus Armbruster, 2004-2006
*/
+#include <config.h>
+
#include "misc.h"
#include "player.h"
-#include "var.h"
#include "xy.h"
#include "nsc.h"
#include "sect.h"
#include "commands.h"
int
-count_pop(register int n)
+count_pop(int n)
{
- register int i;
- register int pop = 0;
+ int i;
+ int pop = 0;
struct sctstr *sp;
- int vec[I_MAX + 1];
for (i = 0; NULL != (sp = getsectid(i)); i++) {
if (sp->sct_own != n)
continue;
if (sp->sct_oldown != n)
continue;
- if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
- continue;
- pop += vec[I_CIVIL];
+ pop += sp->sct_item[I_CIVIL];
}
return pop;
}
struct sctstr sect;
struct nstr_sect nstr;
struct pchrstr *pp;
- double effic;
+ double p_e;
double maxr; /* floating version of max */
double prodeff;
double real; /* floating pt version of act */
- double work;
+ int work;
int totpop;
int act; /* actual production */
- int cost;
+ double cost;
int i;
- int max; /* production w/infinate materials */
- double maxtake;
+ int max; /* production w/infinite materials */
int nsect;
double take;
double mtake;
int there;
- int totcomp; /* sum of component amounts */
+ int unit_work; /* sum of component amounts */
int used; /* production w/infinite workforce */
- int wforce;
- int it;
- u_short *amount; /* amount for component pointer */
- u_char *comp; /* component pointer */
- u_char *endcomp;
- u_char vtype;
- s_char *resource;
- int c;
- s_char maxc[3][10];
- s_char use[3][10];
- int items[I_MAX + 1];
- int vec[I_MAX + 1], lcms, hcms;
+ i_type it;
+ i_type vtype;
+ unsigned char *resource;
+ char cmnem[MAXPRCON];
+ int cuse[MAXPRCON], cmax[MAXPRCON];
+ int lcms, hcms;
int civs = 0;
int uws = 0;
int bwork;
int type;
int eff;
int maxpop;
- u_char otype;
+ int otype;
if (!snxtsct(&nstr, player->argp[1]))
return RET_SYN;
while (nxtsct(&nstr, §)) {
if (!player->owner)
continue;
- getvec(VT_ITEM, items, (s_char *)§, EF_SECTOR);
- civs = min(9999, (int)((obrate * (double)etu_per_update + 1.0)
- * (double)items[I_CIVIL]));
- uws = min(9999, (int)((uwbrate * (double)etu_per_update + 1.0)
- * (double)items[I_UW]));
- if (opt_RES_POP) {
- natp = getnatp(sect.sct_own);
- maxpop = max_pop((float)natp->nat_level[NAT_RLEV], §);
- civs = min(civs, maxpop);
- uws = min(uws, maxpop);
- } else { /* now RES_POP */
- civs = min(999, civs);
- uws = min(999, uws);
- } /* end RES_POP */
+ 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], §);
/* This isn't quite right, since research might rise/fall */
/* during the update, but it's the best we can really do */
- wforce = (int)(((double)civs * (double)sect.sct_work)
- / 100.0 + (double)uws +
- (double)items[I_MILIT] * 2.0 / 5.0);
- work = (double)etu_per_update *(double)wforce / 100.0;
- if (opt_ROLLOVER_AVAIL) {
- if (sect.sct_type == sect.sct_newtype) {
- work += sect.sct_avail;
- }
- if (work > 999) work = 999;
- }
- bwork = (int)((double)work / 2.0);
+ work = new_work(§,
+ 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_newtype;
eff = 0;
}
- if (opt_BIG_CITY) {
- if (!eff && dchr[otype].d_pkg == UPKG &&
- dchr[type].d_pkg != UPKG) {
- if (opt_RES_POP) {
- natp = getnatp(sect.sct_own);
- civs =
- min(civs,
- max_pop(natp->nat_level[NAT_RLEV], 0));
- uws =
- min(uws,
- max_pop(natp->nat_level[NAT_RLEV], 0));
- } else {
- civs = min(9999, civs);
- uws = min(9999, uws);
- }
- wforce =
- (int)((civs * sect.sct_work) / 100.0 + uws +
- items[I_MILIT] * 2 / 5.0);
- work = etu_per_update * wforce / 100.0;
- bwork = min((int)(work / 2), bwork);
- }
+ 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(§,
+ 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;
}
- getvec(VT_ITEM, vec, (s_char *)§, EF_SECTOR);
if (dchr[type].d_lcms > 0) {
- lcms = vec[I_LCM];
+ lcms = sect.sct_item[I_LCM];
lcms /= dchr[type].d_lcms;
if (twork > lcms)
twork = lcms;
}
if (dchr[type].d_hcms > 0) {
- hcms = vec[I_HCM];
+ hcms = sect.sct_item[I_HCM];
hcms /= dchr[type].d_hcms;
if (twork > hcms)
twork = hcms;
bwork -= twork;
eff += twork;
}
- work = work / 2 + bwork;
- if (eff < 60 || (type != SCT_ENLIST && eff < 61))
+ work = (work + 1) / 2 + bwork;
+ if (eff < 60)
continue;
- effic = eff / 100.0;
- if (effic > 1.0)
- effic = 1.0;
+ p_e = eff / 100.0;
+ if (p_e > 1.0)
+ p_e = 1.0;
if (dchr[type].d_prd == 0 && type != SCT_ENLIST)
continue;
- totcomp = 0;
+ unit_work = 0;
pp = &pchr[dchr[type].d_prd];
vtype = pp->p_type;
natp = getnatp(sect.sct_own);
/*
- * sect effic (inc improvements)
+ * sect p_e (inc improvements)
*/
if (type == SCT_ENLIST && sect.sct_own != sect.sct_oldown)
continue;
if (type == SCT_ENLIST)
goto is_enlist;
if (pp->p_nrndx != 0) {
- totcomp++;
- resource = ((s_char *)§) + pp->p_nrndx;
- effic = (*resource * effic) / 100.0;
- if (pp->p_nrdep > 0) {
- maxtake = (*resource * 100.0) / pp->p_nrdep;
- if (effic > maxtake)
- effic = maxtake;
- }
+ unit_work++;
+ resource = (unsigned char *)§ + pp->p_nrndx;
+ p_e = (*resource * p_e) / 100.0;
}
/*
* production effic.
*/
- if (pp->p_nlndx >= 0) {
- prodeff = natp->nat_level[pp->p_nlndx] - pp->p_nlmin;
- if (prodeff < 0.0) {
- prodeff = 0.0;
- }
- prodeff = prodeff / (prodeff + pp->p_nllag);
- } else {
- prodeff = 1.0;
- }
- used = 999;
- comp = pp->p_vtype;
- endcomp = pp->p_vtype + pp->p_nv;
- amount = pp->p_vamt;
- while (comp < endcomp) {
- if (*amount == 0)
- totcomp++;
- else {
- used = min(used, (int)(getvar((int)*comp, (s_char *)§,
- EF_SECTOR) / *amount));
- totcomp += *amount;
- }
- ++comp;
- ++amount;
+ prodeff = prod_eff(pp, 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 (totcomp == 0)
- continue;
+ if (unit_work == 0)
+ unit_work = 1;
/*
* is production limited by resources or
* workforce?
*/
- max = (int)(work * effic / (double)totcomp) + 0.5;
- act = min(used, max);
- /*
- * some things are easier to make.. food,
- * pet, etc.
- */
- act = (int)(((double)pp->p_effic * 0.01 * (double)act) + 0.5);
- max = (int)(((double)pp->p_effic * 0.01 * (double)max) + 0.5);
+ max = (int)(work * p_e / (double)unit_work + 0.5);
+ if (pp->p_nrdep != 0 && vtype != I_NONE) {
+ if (*resource * 100 < pp->p_nrdep * max)
+ max = *resource * 100 / pp->p_nrdep;
+ }
+ act = MIN(used, max);
- real = (double)act *(double)prodeff;
- maxr = (double)max *(double)prodeff;
+ real = MIN(999.0, (double)act * prodeff);
+ maxr = MIN(999.0, (double)max * prodeff);
- if (vtype != 0) {
+ if (vtype != I_NONE) {
if (real < 0.0)
real = 0.0;
/* production backlog? */
- if ((there =
- getvar((int)vtype, (s_char *)§, EF_SECTOR)) >= 9999) {
- there = 9999;
- }
- act = min(act, (9999 - there));
- max = min(max, (9999 - there));
+ there = MIN(ITEM_MAX, sect.sct_item[vtype]);
+ real = MIN(real, ITEM_MAX - there);
}
if (prodeff != 0) {
} else
mtake = take = 0.0;
- if (take > 999.0)
- take = 999.0;
- if (mtake > 999.0)
- mtake = 999.0;
-
- take = (double)take / ((double)pp->p_effic * 0.01);
- mtake = (double)mtake / ((double)pp->p_effic * 0.01);
-
- cost = (int)(take * (double)pp->p_cost);
+ cost = take * pp->p_cost;
if (opt_TECH_POP) {
if (pp->p_level == NAT_TLEV) {
totpop = count_pop(sect.sct_own);
if (totpop > 50000)
- cost = (int)((double)cost * (double)totpop / 50000.0);
+ cost *= totpop / 50000.0;
}
}
- comp = pp->p_vtype;
- amount = pp->p_vamt;
- i = 0;
- while (comp < endcomp) {
- it = unitem((int)*comp);
- if (it > 0 && it <= I_MAX && ichr[it].i_name != 0)
- c = ichr[it].i_name[0];
- else
- c = ' ';
- (void)sprintf(use[i], " %3d%c",
- (int)((take * (double)(*amount)) + 0.5), c);
- (void)sprintf(maxc[i], " %3d%c",
- (int)((mtake * (double)(*amount)) + 0.5), c);
- ++comp;
- ++amount;
- ++i;
- }
- while (i < 3) {
- strcpy(use[i], " ");
- strcpy(maxc[i], " ");
- ++i;
+ for (i = 0; i < MAXPRCON; ++i) {
+ cmnem[i] = cuse[i] = cmax[i] = 0;
+ if (!pp->p_camt[i])
+ continue;
+ it = pp->p_ctype[i];
+ 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);
}
is_enlist:
if (nsect++ == 0) {
pr("PRODUCTION SIMULATION\n");
- pr(" sect des eff wkfc will 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", nstr.x, nstr.y, player->cnum);
pr(" %c", dchr[type].d_mnem);
- pr(" %3.0f%%", effic * 100.0);
+ pr(" %3.0f%%", p_e * 100.0);
+ pr(" %5d", work);
- pr(" %4d", wforce);
- if (vtype != 0) {
- pr(" %4d", (int)(real + 0.5));
+ if (vtype != I_NONE) {
+ pr(" %4d%c", (int)(real + 0.5), ichr[vtype].i_mnem);
} else if (type != SCT_ENLIST) {
switch (pp->p_level) {
case NAT_TLEV:
case NAT_RLEV:
- pr(" %1.2f", real);
+ pr(" %5.2f", real);
break;
case NAT_ELEV:
case NAT_HLEV:
- pr(" %4.0f", real);
+ pr(" %4.0f ", real);
break;
default:
- pr("ERROR");
+ CANT_REACH();
+ pr(" ??? ");
break;
}
} else {
int enlisted;
int civs;
- civs = min(999, (int)((obrate * (double)etu_per_update + 1.0)
- * (double)items[I_CIVIL]));
+ civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL];
natp = getnatp(sect.sct_own);
- maxpop = max_pop((float)natp->nat_level[NAT_RLEV], §);
- civs = min(civs, maxpop);
+ maxpop = max_pop(natp->nat_level[NAT_RLEV], §);
+ civs = MIN(civs, maxpop);
/* This isn't quite right, since research might
rise/fall during the update, but it's the best
we can really do */
enlisted = 0;
- maxmil = (civs / 2) - items[I_MILIT];
+ maxmil = (civs / 2) - sect.sct_item[I_MILIT];
if (maxmil > 0) {
- enlisted = (etu_per_update * (10 + items[I_MILIT]) * 0.05);
+ enlisted = (etu_per_update
+ * (10 + sect.sct_item[I_MILIT])
+ * 0.05);
if (enlisted > maxmil)
enlisted = maxmil;
}
if (enlisted < 0)
enlisted = 0;
- if (natp->nat_priorities[type] == 0) {
- maxmil = 0;
- }
- pr(" %4d mil 1.00 $%-5d%3dc",
- enlisted, enlisted * 3, enlisted);
- pr(" %3dc %4d\n",
- enlisted, maxmil, maxmil);
+ pr(" %4d%c 1.00 $%-5d%4dc %4dc %5d\n",
+ enlisted, ichr[I_MILIT].i_mnem, enlisted * 3,
+ enlisted, enlisted, maxmil);
continue;
}
- pr(" %-5.5s", pp->p_sname);
- prodeff = prodeff * (double)pp->p_effic * 0.01;
pr(" %.2f", prodeff);
- pr(" $%-4d", cost);
+ pr(" $%-5.0f", cost);
for (i = 0; i < 3; i++) {
- pr(use[i]);
+ if (i < MAXPRCON && cmnem[i])
+ pr("%4d%c", cuse[i], cmnem[i]);
+ else
+ pr(" ");
}
pr(" ");
for (i = 0; i < 3; i++) {
- pr(maxc[i]);
- }
- if (natp->nat_priorities[type] == 0) {
- max = 0;
- maxr = 0;
+ if (i < MAXPRCON && cmnem[i])
+ pr("%4d%c", cmax[i], cmnem[i]);
+ else
+ pr(" ");
}
- if (vtype != 0 || pp->p_level == NAT_ELEV
+ if (vtype != I_NONE || pp->p_level == NAT_ELEV
|| pp->p_level == NAT_HLEV)
- pr(" %4d\n", min(999, (int)(max * prodeff + 0.05)));
+ pr(" %5d\n", MIN(999, (int)(max * prodeff + 0.5)));
else
- pr(" %1.2f\n", maxr);
+ pr(" %5.2f\n", maxr);
}
player->simulation = 0;
if (nsect == 0) {