Sectors need space for items, deliveries and distribution thresholds.

To save space, the ancients invented `variables': a collection of
key-value pairs, missing means zero value, space for `enough' keys.
This complicates the code, as assigning to a `variable' can fail for
lack of space.  Over time, `enough' increased, and for quite some time
now `variables' have been *wasting* space.  This changeset replaces
them, except in struct mchrstr, struct lchrstr and struct pchrstr,
where they are read-only, and will be replaced later.  It is only a
first step; further cleanup is required.  To simplify and minimize
this necessarily huge changeset, the new item[] arrays have an unused
slot 0, and the old variable types V_CIVIL, ... are still defined, but
must have the same values as the item types I_CIVIL, ...
This commit is contained in:
Markus Armbruster 2004-03-03 16:54:22 +00:00
parent ba86513b01
commit eccc5cb7d7
86 changed files with 853 additions and 1226 deletions

View file

@ -97,7 +97,6 @@ knockdown(struct sctstr *sp, struct emp_qelem *list)
struct lndstr land;
struct plnstr plane;
struct nstr_item ni;
int mines;
struct natstr *np;
if (sp->sct_type == SCT_BTOWER)
@ -161,12 +160,10 @@ knockdown(struct sctstr *sp, struct emp_qelem *list)
plane.pln_effic = 0;
putplane(plane.pln_uid, &plane);
}
/*
* save only the mines; zero the rest of the
* commodities.
*/
mines = getvar(V_MINE, (caddr_t)sp, EF_SECTOR);
sp->sct_nv = 0;
if (mines > 0)
(void)putvar(V_MINE, mines, (caddr_t)sp, EF_SECTOR);
memset(sp->sct_item, 0, sizeof(sp->sct_item));
memset(sp->sct_del, 0, sizeof(sp->sct_del));
memset(sp->sct_dist, 0, sizeof(sp->sct_dist));
sp->sct_pstage = PLG_HEALTHY;
sp->sct_ptime = 0;
sp->sct_che = 0;
}

View file

@ -49,10 +49,24 @@
#include "gen.h"
#include "subs.h"
void
item_damage(int pct, u_short *item)
{
int i, lose;
for (i = 1; i <= I_MAX; ++i) {
if (opt_SUPER_BARS && i == I_BAR)
continue;
lose = roundavg((double)item[i] * pct * 0.01);
if (i == I_CIVIL || i == I_MILIT || i == I_UW)
lose = ldround(people_damage * lose, 1);
item[i] = item[i] >= lose ? item[i] - lose : 0;
}
}
void
ship_damage(struct shpstr *sp, int dam)
{
if (dam <= 0)
return;
if (dam > 100)
@ -65,8 +79,7 @@ ship_damage(struct shpstr *sp, int dam)
sp->shp_mobil = damage((int)sp->shp_mobil, dam);
if (opt_FUEL && sp->shp_fuel)
sp->shp_fuel = damage((int)sp->shp_fuel, dam);
sp->shp_nv = vl_damage(dam, sp->shp_vtype, sp->shp_vamt,
(int)sp->shp_nv);
item_damage(dam, sp->shp_item);
}
void
@ -93,8 +106,7 @@ land_damage(struct lndstr *lp, int dam)
lp->lnd_mobil = damage((int)lp->lnd_mobil, dam);
if (opt_FUEL && lp->lnd_fuel)
lp->lnd_fuel = damage((int)lp->lnd_fuel, dam);
lp->lnd_nv = vl_damage(dam, lp->lnd_vtype, lp->lnd_vamt,
(int)lp->lnd_nv);
item_damage(dam, lp->lnd_item);
}
}

View file

@ -423,9 +423,8 @@ ef_mtime(int type)
return fdate(empfile[type].fd);
}
int
ef_vars(int type, register s_char *sp, u_char **nvp, u_char **vp,
u_short **ap)
u_short *
ef_items(int type, void *sp)
{
register struct empfile *ef;
@ -433,11 +432,8 @@ ef_vars(int type, register s_char *sp, u_char **nvp, u_char **vp,
return 0;
ef = &empfile[type];
if ((ef->flags & EFF_COM) == 0)
return -1;
*nvp = (u_char *)(sp + ef->varoffs[0]);
*vp = (u_char *)(sp + ef->varoffs[1]);
*ap = (u_short *)(sp + ef->varoffs[2]);
return ef->maxvars;
return 0;
return (u_short *)((char *)sp + ef->itemoffs);
}
int

View file

@ -36,81 +36,36 @@
#include "file.h"
#include "common.h"
int
getvar(int vtype, s_char *sp, int ptype)
{
u_char *vtypep;
u_short *vamtp;
u_char *nvp;
int amt;
if (ef_vars(ptype, sp, &nvp, &vtypep, &vamtp) < 0) {
logerror("getvar: ptype %d has no vars", ptype);
return 0;
}
amt = vl_find(vtype, vtypep, vamtp, (int)*nvp);
if (amt < 0) {
logerror("getvar: vl_find returns %d, vtype %d", amt, vtype);
return 0;
}
return amt;
}
int
getvec(int class, int *vec, s_char *sp, int ptype)
{
u_char *vtypep;
u_short *vamtp;
u_char *nvp;
int nv;
u_short *src = ef_items(ptype, sp);
int i;
if (ef_vars(ptype, sp, &nvp, &vtypep, &vamtp) < 0) {
if (!src || class != VT_ITEM) {
logerror("getvec: ptype %d has no vars", ptype);
return 0;
}
nv = vl_getvec(vtypep, vamtp, (int)*nvp, class, vec);
if (nv < 0) {
logerror("vl_getvec: returns %d, ptype %d\n", nv, ptype);
return 0;
}
return nv;
}
int
putvar(int vtype, int amt, s_char *sp, int ptype)
{
u_char *vtypep;
u_short *vamtp;
u_char *nvp;
int maxv;
for (i = 0; i <= I_MAX; ++i)
vec[i] = src[i];
if (vtype < 0 || vtype > V_MAX) {
logerror("putvar: bad vtype %d\n", vtype);
return 0;
}
if ((maxv = ef_vars(ptype, sp, &nvp, &vtypep, &vamtp)) < 0) {
logerror("putvar: ptype %d has no vars", ptype);
return 0;
}
if (amt < 0)
amt = 0;
return vl_set(vtype, (u_int)amt, vtypep, vamtp, nvp, maxv);
return I_MAX;
}
int
putvec(int class, int *vec, s_char *sp, int ptype)
{
u_char *vtypep;
u_short *vamtp;
u_char *nvp;
int maxv, x;
u_short *dst = ef_items(ptype, sp);
int i;
if ((maxv = ef_vars(ptype, sp, &nvp, &vtypep, &vamtp)) < 0) {
if (!dst || class != VT_ITEM) {
logerror("putvec: ptype %d has no vars", ptype);
return 0;
}
for (x = 0; x < I_MAX; x++)
if (vec[x] < 0)
vec[x] = 0;
return vl_setvec(vtypep, vamtp, nvp, maxv, class, vec);
for (i = 0; i <= I_MAX; ++i)
dst[i] = vec[i];
return I_MAX;
}

View file

@ -72,8 +72,7 @@ sect_damage(struct sctstr *sp, int dam, struct emp_qelem *list)
if (sp->sct_mobil > 0)
sp->sct_mobil = damage((int)sp->sct_mobil, dam);
sp->sct_nv = vl_damage(dam,
sp->sct_vtype, sp->sct_vamt, (int)sp->sct_nv);
item_damage(dam, sp->sct_item);
if (opt_EASY_BRIDGES == 0) {
if (sp->sct_effic < 20 && sp->sct_type == SCT_BHEAD)
bridgefall(sp, list);

View file

@ -41,7 +41,9 @@
#include "common.h"
#include "gen.h"
#if 0
static int freeslot(u_char *vec, register u_char *end);
#endif
int
vl_find(register int vtype, u_char *typevec, u_short *amtvec, int nelem)
@ -62,6 +64,7 @@ vl_find(register int vtype, u_char *typevec, u_short *amtvec, int nelem)
return 0;
}
#if 0
int
vl_set(register int vtype, u_int amt, u_char *typevec, u_short *amtvec,
u_char *nvp, int max)
@ -283,3 +286,4 @@ freeslot(u_char *vec, register u_char *end)
}
return -1;
}
#endif