From 6cd3d55c4e734e821f819f264b78f5c3187693ee Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 24 Feb 2008 11:07:18 +0100 Subject: [PATCH] Initialize ef_type and uid automatically in ef_extend() New struct emptypedstr to avoid depending on empobj.h there. Remove now superfluous manual initializations elsewhere. This doesn't fix any missing initializations. --- include/empobj.h | 17 +++++++++++------ include/file.h | 7 ++++++- include/lost.h | 2 +- include/news.h | 2 +- src/lib/common/file.c | 12 +++++++++--- src/lib/subs/land.c | 5 ----- src/lib/subs/nuke.c | 5 ----- src/lib/subs/plane.c | 5 ----- src/lib/subs/ship.c | 5 ----- 9 files changed, 28 insertions(+), 32 deletions(-) diff --git a/include/empobj.h b/include/empobj.h index 8300211fe..514154a95 100644 --- a/include/empobj.h +++ b/include/empobj.h @@ -51,18 +51,23 @@ #include "types.h" struct empobj { - short ef_type; /* is always valid */ + /* + * initial part must match struct emptypedstr + * valid if EFF_TYPED is set in table's flags + */ + short ef_type; short uid; - natid own; /* is valid if EFF_OWNER is set in table def. */ - coord x; /* is valid if EFF_XY is set in table def. */ - coord y; /* is valid if EFF_XY is set in table def. */ - signed char type; /* is valid for sectors and units */ + /* end of part matching struct emptypedstr */ + natid own; /* valid if EFF_OWNER is in table's flags */ + coord x; /* valid if EFF_XY is in table's flags */ + coord y; /* valid if EFF_XY is in table's flags */ + signed char type; /* valid for sectors and units */ /* remaining are valid for units */ signed char effic; signed char mobil; unsigned char off; short tech; - char group; /* is valid if EFF_GROUP is set in table def. */ + char group; /* valid if EFF_GROUP is in table's flags */ coord opx, opy; short mission; short radius; diff --git a/include/file.h b/include/file.h index 640adbc9e..43a67f733 100644 --- a/include/file.h +++ b/include/file.h @@ -65,13 +65,18 @@ struct empfile { int (*prewrite)(int, void *); /* called before write, unless null */ }; +struct emptypedstr { + short ef_type; + short uid; +}; + /* * Flag bits for struct empfile member flags * Immutable flags are properties of the table and thus cannot change. * The remaining flags record how the table is being used. */ /* Immutable flags, fixed at compile-time */ -/* Table entries' addresses can't be safely cast to struct emptyped *. */ +/* Dereferencing entry address cast to struct emptypedstr * is safe */ #define EFF_TYPED bit(0) /* * EFF_XY / EFF_OWNER / EFF_GROUP assert that coordinates / owner / diff --git a/include/lost.h b/include/lost.h index a377c6ec1..179c7b9d3 100644 --- a/include/lost.h +++ b/include/lost.h @@ -40,7 +40,7 @@ struct loststr { /* initial part must match struct empobj */ short ef_type; - int lost_uid; /* lost ID (assigned, otherwise unused) */ + int lost_uid; natid lost_owner; /* Who lost it */ /* end of part matching struct empobj */ short lost_type; /* Type of thing (ship, plane, nuke, land, sector) */ diff --git a/include/news.h b/include/news.h index cc182a910..5985d26a1 100644 --- a/include/news.h +++ b/include/news.h @@ -47,7 +47,7 @@ struct nwsstr { /* initial part must match struct empobj */ short ef_type; - short nws_uid; /* unused */ + short nws_uid; /* end of part matching struct empobj */ natid nws_ano; /* "actor" country # */ signed char nws_vrb; /* action (verb) */ diff --git a/src/lib/common/file.c b/src/lib/common/file.c index 090b60622..2a3f26896 100644 --- a/src/lib/common/file.c +++ b/src/lib/common/file.c @@ -475,11 +475,17 @@ static void do_blank(struct empfile *ep, void *buf, int id, int count) { int i; + struct emptypedstr *elt; memset(buf, 0, count * ep->size); - if (ep->init) { - for (i = 0; i < count; i++) - ep->init(id + i, (char *)buf + i * ep->size); + for (i = 0; i < count; i++) { + elt = (struct emptypedstr *)((char *)buf + i * ep->size); + if (ep->flags & EFF_TYPED) { + elt->ef_type = ep->uid; + elt->uid = id + i; + } + if (ep->init) + ep->init(id + i, (void *)elt); } } diff --git a/src/lib/subs/land.c b/src/lib/subs/land.c index 5a2414777..2f5d68700 100644 --- a/src/lib/subs/land.c +++ b/src/lib/subs/land.c @@ -165,11 +165,6 @@ lnd_prewrite(int n, void *ptr) void lnd_init(int n, void *ptr) { - struct lndstr *lp = ptr; - - lp->ef_type = EF_LAND; - lp->lnd_uid = n; - lp->lnd_own = 0; } char * diff --git a/src/lib/subs/nuke.c b/src/lib/subs/nuke.c index c895e69c1..8d8c44f02 100644 --- a/src/lib/subs/nuke.c +++ b/src/lib/subs/nuke.c @@ -101,11 +101,6 @@ nuk_prewrite(int n, void *ptr) void nuk_init(int n, void *ptr) { - struct nukstr *np = ptr; - - np->ef_type = EF_NUKE; - np->nuk_uid = n; - np->nuk_own = 0; } int diff --git a/src/lib/subs/plane.c b/src/lib/subs/plane.c index 0cddce20b..a5b5e0e7b 100644 --- a/src/lib/subs/plane.c +++ b/src/lib/subs/plane.c @@ -144,11 +144,6 @@ pln_prewrite(int n, void *ptr) void pln_init(int n, void *ptr) { - struct plnstr *pp = ptr; - - pp->ef_type = EF_PLANE; - pp->pln_uid = n; - pp->pln_own = 0; } char * diff --git a/src/lib/subs/ship.c b/src/lib/subs/ship.c index dcdf68143..3653663cc 100644 --- a/src/lib/subs/ship.c +++ b/src/lib/subs/ship.c @@ -112,11 +112,6 @@ shp_prewrite(int n, void *ptr) void shp_init(int n, void *ptr) { - struct shpstr *sp = ptr; - - sp->ef_type = EF_SHIP; - sp->shp_uid = n; - sp->shp_own = 0; } char * -- 2.43.0