(pchrstr, MAXPRCON): Simplify variable-style storage of constituents.

Store only up to MAXPRCON constituents, not MAXCHRNV; code doesn't
fully support more than three anyway.  Remove member p_nv, use item
type I_NONE for unused slots.  Rename members p_vtype, p_vamt to
p_ctype, p_camt to avoid confusion with variable-style storage.
(pchr): Initializers adapted.
(nullify_objects, materials_cost, materials_charge, prod,
show_sect_capab, grind): Adapt, simplify where possible, protect
against bad item types in pchr[].
(MAXCHRNV): Unused, remove.
This commit is contained in:
Markus Armbruster 2004-08-17 15:19:46 +00:00
parent 15d4f89951
commit 4d154753b8
8 changed files with 103 additions and 101 deletions

View file

@ -147,8 +147,6 @@ extern char *getstring(char *prompt, char buf[]);
extern s_char *prbuf(s_char *format, ...) extern s_char *prbuf(s_char *format, ...)
ATTRIBUTE((format (printf, 1, 2))); ATTRIBUTE((format (printf, 1, 2)));
#define MAXCHRNV 12
#include "prototypes.h" /* must come at end, after defines and typedefs */ #include "prototypes.h" /* must come at end, after defines and typedefs */
#endif /* _MISC_H_ */ #endif /* _MISC_H_ */

View file

@ -34,10 +34,15 @@
#ifndef _PRODUCT_H_ #ifndef _PRODUCT_H_
#define _PRODUCT_H_ #define _PRODUCT_H_
/*
* Maximum number of product constituents.
* Beware, some output formats rely on MAXPRCON <= 3!
*/
enum { MAXPRCON = 3 };
struct pchrstr { struct pchrstr {
u_char p_nv; /* number of constituents */ u_char p_ctype[MAXPRCON]; /* constituent types */
u_char p_vtype[MAXCHRNV]; /* constituent types */ u_short p_camt[MAXPRCON]; /* constituent amounts */
u_short p_vamt[MAXCHRNV]; /* constituent amounts */
int p_type; /* vtype if product is a variable */ int p_type; /* vtype if product is a variable */
int p_level; /* index (NAT_?LEV) if product is not a var */ int p_level; /* index (NAT_?LEV) if product is not a var */
int p_cost; /* dollars / product unit */ int p_cost; /* dollars / product unit */

View file

@ -68,23 +68,36 @@ grin(void)
if (sect.sct_effic < 60 || sect.sct_own != player->cnum) if (sect.sct_effic < 60 || sect.sct_own != player->cnum)
continue; continue;
n = sect.sct_item[I_BAR] >= qty ? qty : sect.sct_item[I_BAR]; n = sect.sct_item[I_BAR] >= qty ? qty : sect.sct_item[I_BAR];
avail = n * 5.0; /* work limit */
avail = n * 5;
if (avail > sect.sct_avail) { if (avail > sect.sct_avail) {
n = sect.sct_avail / 5; n = sect.sct_avail / 5;
avail = sect.sct_avail; avail = sect.sct_avail;
} }
for (i = 0; i < pchr[P_BAR].p_nv; i++) { /* space limit */
for (i = 0; i < MAXPRCON; i++) {
if (!pchr[P_BAR].p_camt[i])
continue;
if (CANT_HAPPEN(pchr[P_BAR].p_ctype[i] <= I_NONE ||
pchr[P_BAR].p_ctype[i] > I_MAX))
continue;
n = min(n, n = min(n,
(double)(ITEM_MAX - sect.sct_item[pchr[P_BAR].p_vtype[i]]) (double)(ITEM_MAX - sect.sct_item[pchr[P_BAR].p_ctype[i]])
/ (pchr[P_BAR].p_vamt[i] * grind_eff)); / (pchr[P_BAR].p_camt[i] * grind_eff));
} }
if (n > 0) { if (n > 0) {
pr("%d bars ground up in %s\n", n, pr("%d bars ground up in %s\n", n,
xyas(sect.sct_x, sect.sct_y, player->cnum)); xyas(sect.sct_x, sect.sct_y, player->cnum));
sect.sct_item[I_BAR] -= n; sect.sct_item[I_BAR] -= n;
for (i = 0; i < pchr[P_BAR].p_nv; i++) { for (i = 0; i < MAXPRCON; i++) {
sect.sct_item[pchr[P_BAR].p_vtype[i]] if (!pchr[P_BAR].p_camt[i])
+= n * pchr[P_BAR].p_vamt[i] * grind_eff; continue;
if (CANT_HAPPEN(pchr[P_BAR].p_ctype[i] <= I_NONE ||
pchr[P_BAR].p_ctype[i] > I_MAX))
continue;
sect.sct_item[pchr[P_BAR].p_ctype[i]]
+= n * pchr[P_BAR].p_camt[i] * grind_eff;
} }
sect.sct_avail -= avail; sect.sct_avail -= avail;
putsect(&sect); putsect(&sect);

View file

@ -77,8 +77,8 @@ prod(void)
int totpop; int totpop;
int act; /* actual production */ int act; /* actual production */
int cost; int cost;
int i; int i, j;
int max; /* production w/infinate materials */ int max; /* production w/infinite materials */
int nsect; int nsect;
double take; double take;
double mtake; double mtake;
@ -87,14 +87,10 @@ prod(void)
int used; /* production w/infinite workforce */ int used; /* production w/infinite workforce */
int wforce; int wforce;
int it; int it;
u_short *amount; /* amount for component pointer */
u_char *comp; /* component pointer */
u_char *endcomp;
u_char vtype; u_char vtype;
s_char *resource; s_char *resource;
int c; s_char maxc[MAXPRCON][10];
s_char maxc[3][10]; s_char use[MAXPRCON][10];
s_char use[3][10];
int lcms, hcms; int lcms, hcms;
int civs = 0; int civs = 0;
int uws = 0; int uws = 0;
@ -232,13 +228,14 @@ prod(void)
* raw material limit * raw material limit
*/ */
used = 9999; used = 9999;
amount = pp->p_vamt; for (j = 0; j < MAXPRCON; ++j) {
endcomp = pp->p_vtype + pp->p_nv; it = pp->p_ctype[j];
for (comp = pp->p_vtype; comp < endcomp; comp++, amount++) { if (!pp->p_camt[j])
if (*amount == 0)
continue; continue;
used = min(used, sect.sct_item[(int)*comp] / *amount); if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
unit_work += *amount; continue;
used = min(used, sect.sct_item[it] / pp->p_camt[j]);
unit_work += pp->p_camt[j];
} }
if (unit_work == 0) if (unit_work == 0)
unit_work = 1; unit_work = 1;
@ -275,22 +272,20 @@ prod(void)
} }
} }
comp = pp->p_vtype;
amount = pp->p_vamt;
i = 0; i = 0;
while (comp < endcomp) { for (j = 0; j < MAXPRCON; ++j) {
it = unitem((int)*comp); it = pp->p_ctype[j];
if (it > 0 && it <= I_MAX && ichr[it].i_name != 0) if (it > I_NONE && it <= I_MAX && ichr[it].i_name != 0) {
c = ichr[it].i_name[0]; if (CANT_HAPPEN(i >= 3))
else break;
c = ' '; sprintf(use[i], " %3d%c",
(void)sprintf(use[i], " %3d%c", (int)((take * (double)pp->p_camt[j]) + 0.5),
(int)((take * (double)(*amount)) + 0.5), c); ichr[it].i_name[0]);
(void)sprintf(maxc[i], " %3d%c", sprintf(maxc[i], " %3d%c",
(int)((mtake * (double)(*amount)) + 0.5), c); (int)((mtake * (double)pp->p_camt[j]) + 0.5),
++comp; ichr[it].i_name[0]);
++amount; ++i;
++i; }
} }
while (i < 3) { while (i < 3) {
strcpy(use[i], " "); strcpy(use[i], " ");

View file

@ -41,58 +41,58 @@
struct pchrstr pchr[] = { struct pchrstr pchr[] = {
/* level cost nrndx nrdep nlndx nlmin nllag effic sname name */ /* level cost nrndx nrdep nlndx nlmin nllag effic sname name */
{0, {0}, {0}, {{I_NONE, I_NONE, I_NONE}, {0, 0, 0},
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"unused", "",}, "unused", "",},
{2, {V_LCM, V_HCM}, {2, 1}, {{I_LCM, I_HCM, I_NONE}, {2, 1, 0},
V_SHELL, -1, 3, 0, 0, NAT_TLEV, 20, 10, 100, V_SHELL, -1, 3, 0, 0, NAT_TLEV, 20, 10, 100,
"shells", "shells",}, "shells", "shells",},
{3, {V_OIL, V_LCM, V_HCM}, {1, 5, 10}, {{I_OIL, I_LCM, I_HCM}, {1, 5, 10},
V_GUN, -1, 30, 0, 0, NAT_TLEV, 20, 10, 100, V_GUN, -1, 30, 0, 0, NAT_TLEV, 20, 10, 100,
"guns", "guns",}, "guns", "guns",},
{1, {V_OIL}, {1}, {{I_OIL, I_NONE, I_NONE}, {1, 0, 0},
V_PETROL, -1, 1, 0, 0, NAT_TLEV, 20, 10, 1000, V_PETROL, -1, 1, 0, 0, NAT_TLEV, 20, 10, 1000,
"petrol", "petrol",}, "petrol", "petrol",},
{0, {0}, {0}, {{I_NONE, I_NONE, I_NONE}, {0, 0, 0},
V_IRON, -1, 0, offsetof(struct sctstr, sct_min), 0, -1, 0, 0, 100, V_IRON, -1, 0, offsetof(struct sctstr, sct_min), 0, -1, 0, 0, 100,
"iron ore", "iron",}, "iron ore", "iron",},
{0, {0}, {0}, {{I_NONE, I_NONE, I_NONE}, {0, 0, 0},
V_DUST, -1, 0, offsetof(struct sctstr, sct_gmin), 20, -1, 0, 0, 100, V_DUST, -1, 0, offsetof(struct sctstr, sct_gmin), 20, -1, 0, 0, 100,
"gold dust", "dust",}, "gold dust", "dust",},
{1, {V_DUST}, {5}, {{I_DUST, I_NONE, I_NONE}, {5, 0, 0},
V_BAR, -1, 10, 0, 0, -1, 0, 0, 100, V_BAR, -1, 10, 0, 0, -1, 0, 0, 100,
"gold bars", "bars",}, "gold bars", "bars",},
{0, {0}, {0}, {{I_NONE, I_NONE, I_NONE}, {0, 0, 0},
V_FOOD, -1, 0, offsetof(struct sctstr, sct_fertil), 0, NAT_TLEV, -10, 10, 900, V_FOOD, -1, 0, offsetof(struct sctstr, sct_fertil), 0, NAT_TLEV, -10, 10, 900,
"food", "food",}, "food", "food",},
{0, {0}, {0}, {{I_NONE, I_NONE, I_NONE}, {0, 0, 0},
V_OIL, -1, 0, offsetof(struct sctstr, sct_oil), 10, NAT_TLEV, -10, 10, 100, V_OIL, -1, 0, offsetof(struct sctstr, sct_oil), 10, NAT_TLEV, -10, 10, 100,
"oil", "oil",}, "oil", "oil",},
{1, {V_IRON}, {1}, {{I_IRON, I_NONE, I_NONE}, {1, 0, 0},
V_LCM, -1, 0, 0, 0, NAT_TLEV, -10, 10, 100, V_LCM, -1, 0, 0, 0, NAT_TLEV, -10, 10, 100,
"light construction materials", "lcm",}, "light construction materials", "lcm",},
{1, {V_IRON}, {2}, {{I_IRON, I_NONE, I_NONE}, {2, 0, 0},
V_HCM, -1, 0, 0, 0, NAT_TLEV, -10, 10, 100, V_HCM, -1, 0, 0, 0, NAT_TLEV, -10, 10, 100,
"heavy construction materials", "hcm",}, "heavy construction materials", "hcm",},
{3, {V_DUST, V_OIL, V_LCM}, {1, 5, 10}, {{I_DUST, I_OIL, I_LCM}, {1, 5, 10},
0, NAT_TLEV, 300, 0, 0, NAT_ELEV, 5, 10, 100, 0, NAT_TLEV, 300, 0, 0, NAT_ELEV, 5, 10, 100,
"technological breakthroughs", "tech",}, "technological breakthroughs", "tech",},
{3, {V_DUST, V_OIL, V_LCM}, {1, 5, 10}, {{I_DUST, I_OIL, I_LCM}, {1, 5, 10},
0, NAT_RLEV, 90, 0, 0, NAT_ELEV, 5, 10, 100, 0, NAT_RLEV, 90, 0, 0, NAT_ELEV, 5, 10, 100,
"medical discoveries", "medical",}, "medical discoveries", "medical",},
{1, {V_LCM}, {1}, {{I_LCM, I_NONE, I_NONE}, {1, 0, 0},
0, NAT_ELEV, 9, 0, 0, -1, 0, 0, 100, 0, NAT_ELEV, 9, 0, 0, -1, 0, 0, 100,
"a class of graduates", "edu",}, "a class of graduates", "edu",},
{1, {V_LCM}, {1}, {{I_LCM, I_NONE, I_NONE}, {1, 0, 0},
0, NAT_HLEV, 9, 0, 0, -1, 0, 0, 100, 0, NAT_HLEV, 9, 0, 0, -1, 0, 0, 100,
"happy strollers", "happy",}, "happy strollers", "happy",},
{0, {0}, {0}, {{I_NONE, I_NONE, I_NONE}, {0, 0, 0},
V_RAD, -1, 2, offsetof(struct sctstr, sct_uran), 35, NAT_TLEV, 40, 10, 100, V_RAD, -1, 2, offsetof(struct sctstr, sct_uran), 35, NAT_TLEV, 40, 10, 100,
"radioactive materials", "rad",}, "radioactive materials", "rad",},
{0, {0}, {0}, {{I_NONE, I_NONE, I_NONE}, {0, 0, 0},
V_DUST, -1, 0, offsetof(struct sctstr, sct_gmin), 20, -1, 0, 0, 75, V_DUST, -1, 0, offsetof(struct sctstr, sct_gmin), 20, -1, 0, 0, 75,
"gold dust", "dust",}, "gold dust", "dust",},
{0, {0}, {0}, {{I_NONE, I_NONE, I_NONE}, {0, 0, 0},
0, 0, 0, 0, 0, 0, 0, 0, 0, "", "",} 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "",}
}; };

View file

@ -701,12 +701,14 @@ show_sect_capab(int foo)
pr("%c %-23s %-7s ", dchr[x].d_mnem, dchr[x].d_name, pr("%c %-23s %-7s ", dchr[x].d_mnem, dchr[x].d_name,
pchr[j].p_sname); pchr[j].p_sname);
/*for(i=0;i<MAXCHRNV;i++) */ CANT_HAPPEN(MAXPRCON > 3); /* output has only three columns */
/* XXX currently no more than 3 items actually used */
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
if ((i < pchr[j].p_nv) && (pchr[j].p_vamt[i] > 0)) { if (i < MAXPRCON
pr("%2d %c ", pchr[j].p_vamt[i], && pchr[j].p_camt[i]
ichr[pchr[j].p_vtype[i] & (~VT_ITEM)].i_name[0]); && pchr[j].p_ctype[i] > I_NONE
&& pchr[j].p_ctype[i] <= I_MAX) {
pr("%2d %c ", pchr[j].p_camt[i],
ichr[pchr[j].p_ctype[i] & (~VT_ITEM)].i_name[0]);
} else { } else {
pr(" "); pr(" ");
} }

View file

@ -176,53 +176,42 @@ produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
} }
static int static int
materials_cost(struct pchrstr *product, short *vec, int *costp) materials_cost(struct pchrstr *pp, short *vec, int *costp)
{ {
register u_char *vp; int count;
register u_short *ap; int cost;
register int count; int i, n;
register int cost;
register int n;
register u_char *endp;
count = 9999; count = 9999;
cost = 0; cost = 0;
ap = product->p_vamt; for (i = 0; i < MAXPRCON; ++i) {
endp = product->p_vtype + product->p_nv; if (!pp->p_camt[i])
for (vp = product->p_vtype; vp < endp; vp++, ap++) {
if (!*ap)
continue; continue;
n = vec[*vp & ~VT_ITEM] / *ap; if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
continue;
n = vec[pp->p_ctype[i]] / pp->p_camt[i];
if (n < count) if (n < count)
count = n; count = n;
cost += *ap; cost += pp->p_camt[i];
} }
*costp = cost; *costp = cost;
return count; return count;
} }
static void static void
materials_charge(struct pchrstr *product, short *vec, int count) materials_charge(struct pchrstr *pp, short *vec, int count)
{ {
register u_char *vp; int i, item, n;
register u_short *ap;
register u_char *endp;
register int item;
register int n;
ap = product->p_vamt; for (i = 0; i < MAXPRCON; ++i) {
endp = product->p_vtype + product->p_nv; item = pp->p_ctype[i];
for (vp = product->p_vtype; vp < endp; vp++, ap++) { if (!pp->p_camt[i])
item = *vp & ~VT_ITEM;
if (item < 0 || item > I_MAX) {
logerror("materials_charge: bad item %d", item);
continue; continue;
} if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
if ((n = vec[item] - *ap * count) < 0) { continue;
logerror("materials_charge: %d > %d item #%d", n = vec[item] - pp->p_camt[i] * count;
n, vec[item], item); if (CANT_HAPPEN(n < 0))
n = 0; n = 0;
}
vec[item] = n; vec[item] = n;
} }
} }

View file

@ -439,13 +439,13 @@ nullify_objects(void)
dchr[i].d_lcms = 0; dchr[i].d_lcms = 0;
} }
for (i = 0; i < prd_maxno; i++) { for (i = 0; i < prd_maxno; i++) {
for (j = 0; j < pchr[i].p_nv; j++) { for (j = 0; j < MAXPRCON; j++) {
if (opt_NO_HCMS && pchr[i].p_vtype[j] == V_HCM) if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
pchr[i].p_vamt[j] = 0; pchr[i].p_camt[j] = 0;
if (opt_NO_LCMS && pchr[i].p_vtype[j] == V_LCM) if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
pchr[i].p_vamt[j] = 0; pchr[i].p_camt[j] = 0;
if (opt_NO_OIL && pchr[i].p_vtype[j] == V_OIL) if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
pchr[i].p_vamt[j] = 0; pchr[i].p_camt[j] = 0;
} }
} }
} }