split g into multiple structures

The consolidation of global variables from scattered source
files into decl.c and declared in decl.h was begun in 3.7.0.
Their placement in common files was done for centralized
initialization and potential re-initialization during a
"play again" scenario.

It wasn't really necessary for all of them to be housed in a
single huge structure to meet the "play again" requirement,
and the single huge structure has been a little unwieldy when
it comes to maintenance.

Following this commit, instead of one single extremely large structure
named 'g' to house all of the relocated global variables, they
are distributed into several ga through gz.

To make things easy for the developer, each variable is placed
into the struct corresponding to the starting letter of the variable.
That way, no lookup is required in order to know which struct houses
a particular variable, it is a simple match to the starting letter
for all the centralized global variables.

A global variable named 'amulets', would be found in ga.
    ga.amulets
     ^ ^
A global varable named 'move', would be found in gm.
    gm.moves
     ^ ^
A global variable named 'val_for_n_or_more' would be found in gv.
    gv.val_for_n_or_more
     ^ ^
A global variable named 'youmonst' would be found in gy.
    gy.youmonst
     ^ ^
This commit is contained in:
nhmall
2022-11-29 21:53:21 -05:00
parent e64ed2859d
commit 02a48aa8cf
193 changed files with 10764 additions and 10148 deletions

View File

@@ -111,9 +111,9 @@ l_nhcore_init(void)
/* Sample sbi for getting resource usage information. */
nhl_sandbox_info sbi = {NHL_SB_SAFE|NHL_SB_REPORT2, 10000000, 10000000, 0};
#endif
if ((g.luacore = nhl_init(&sbi)) != 0) {
if (!nhl_loadlua(g.luacore, "nhcore.lua")) {
g.luacore = (lua_State *) 0;
if ((gl.luacore = nhl_init(&sbi)) != 0) {
if (!nhl_loadlua(gl.luacore, "nhcore.lua")) {
gl.luacore = (lua_State *) 0;
} else {
int i;
@@ -127,9 +127,9 @@ l_nhcore_init(void)
void
l_nhcore_done(void)
{
if (g.luacore) {
nhl_done(g.luacore);
g.luacore = 0;
if (gl.luacore) {
nhl_done(gl.luacore);
gl.luacore = 0;
}
end_luapat();
}
@@ -140,24 +140,24 @@ l_nhcore_call(int callidx)
int ltyp;
if (callidx < 0 || callidx >= NUM_NHCORE_CALLS
|| !g.luacore || !nhcore_call_available[callidx])
|| !gl.luacore || !nhcore_call_available[callidx])
return;
lua_getglobal(g.luacore, "nhcore");
if (!lua_istable(g.luacore, -1)) {
lua_getglobal(gl.luacore, "nhcore");
if (!lua_istable(gl.luacore, -1)) {
/*impossible("nhcore is not a lua table");*/
nhl_done(g.luacore);
g.luacore = 0;
nhl_done(gl.luacore);
gl.luacore = 0;
return;
}
lua_getfield(g.luacore, -1, nhcore_call_names[callidx]);
ltyp = lua_type(g.luacore, -1);
lua_getfield(gl.luacore, -1, nhcore_call_names[callidx]);
ltyp = lua_type(gl.luacore, -1);
if (ltyp == LUA_TFUNCTION) {
lua_remove(g.luacore, -2); /* nhcore_call_names[callidx] */
lua_remove(g.luacore, -2); /* nhcore */
if (nhl_pcall(g.luacore, 0, 1)) {
impossible("Lua error: %s", lua_tostring(g.luacore, -1));
lua_remove(gl.luacore, -2); /* nhcore_call_names[callidx] */
lua_remove(gl.luacore, -2); /* nhcore */
if (nhl_pcall(gl.luacore, 0, 1)) {
impossible("Lua error: %s", lua_tostring(gl.luacore, -1));
}
} else {
/*impossible("nhcore.%s is not a lua function",
@@ -1021,8 +1021,8 @@ nhl_dnum_name(lua_State *L)
if (argc == 1) {
lua_Integer dnum = luaL_checkinteger(L, 1);
if (dnum >= 0 && dnum < g.n_dgns)
lua_pushstring(L, g.dungeons[dnum].dname);
if (dnum >= 0 && dnum < gn.n_dgns)
lua_pushstring(L, gd.dungeons[dnum].dname);
else
lua_pushstring(L, "");
} else
@@ -1034,7 +1034,7 @@ nhl_dnum_name(lua_State *L)
static int
nhl_stairways(lua_State *L)
{
stairway *tmp = g.stairs;
stairway *tmp = gs.stairs;
int i = 1; /* lua arrays should start at 1 */
lua_newtable(L);
@@ -1101,7 +1101,7 @@ nhl_pushkey(lua_State *L)
return 0;
}
/* do a turn of moveloop, or until g.multi is done if param is true. */
/* do a turn of moveloop, or until gm.multi is done if param is true. */
/* nh.doturn(); nh.doturn(true); */
static int
nhl_doturn(lua_State *L)
@@ -1114,7 +1114,7 @@ nhl_doturn(lua_State *L)
do {
moveloop_core();
} while (domulti && g.multi);
} while (domulti && gm.multi);
return 0;
}
@@ -1421,13 +1421,13 @@ nhl_meta_u_index(lua_State *L)
}
if (!strcmp(tkey, "inventory")) {
nhl_push_obj(L, g.invent);
nhl_push_obj(L, gi.invent);
return 1;
} else if (!strcmp(tkey, "role")) {
lua_pushstring(L, g.urole.name.m);
lua_pushstring(L, gu.urole.name.m);
return 1;
} else if (!strcmp(tkey, "moves")) {
lua_pushinteger(L, g.moves);
lua_pushinteger(L, gm.moves);
return 1;
} else if (!strcmp(tkey, "uhave_amulet")) {
lua_pushinteger(L, u.uhave.amulet);
@@ -1455,8 +1455,8 @@ RESTORE_WARNING_UNREACHABLE_CODE
static int
nhl_u_clear_inventory(lua_State *L UNUSED)
{
while (g.invent)
useupall(g.invent);
while (gi.invent)
useupall(gi.invent);
return 0;
}
@@ -1770,7 +1770,7 @@ get_lua_version(void)
{
nhl_sandbox_info sbi = {NHL_SB_VERSION, 0, 0, 0};
if (g.lua_ver[0] == 0) {
if (gl.lua_ver[0] == 0) {
lua_State *L = nhl_init(&sbi);
if (L) {
@@ -1797,22 +1797,22 @@ get_lua_version(void)
vs = LUA_VERSION, len = strlen(vs);
#endif
}
if (vs && len < sizeof g.lua_ver) {
if (vs && len < sizeof gl.lua_ver) {
if (!strncmpi(vs, "Lua", 3)) {
vs += 3;
if (*vs == '-' || *vs == ' ')
vs += 1;
}
Strcpy(g.lua_ver, vs);
Strcpy(gl.lua_ver, vs);
}
}
nhl_done(L);
#ifdef LUA_COPYRIGHT
if (sizeof LUA_COPYRIGHT <= sizeof g.lua_copyright)
Strcpy(g.lua_copyright, LUA_COPYRIGHT);
if (sizeof LUA_COPYRIGHT <= sizeof gl.lua_copyright)
Strcpy(gl.lua_copyright, LUA_COPYRIGHT);
#endif
}
return (const char *) g.lua_ver;
return (const char *) gl.lua_ver;
}
RESTORE_WARNING_CONDEXPR_IS_CONSTANT
@@ -1995,7 +1995,7 @@ start_luapat(void)
/* load a pattern matching function */
rv = luaL_loadstring(luapat,
"function matches(s,p) return not not string.match(s,p) end");
"function matches(s,p) return not not stringm.match(s,p) end");
if (rv != LUA_OK) {
panic("start_luapat: %d",rv);
}
@@ -2346,17 +2346,17 @@ nhl_warn(
const char *msg_fragment,
int to_be_continued) /* 0: last fragment; 1: more to come */
{
size_t fraglen, buflen = strlen(g.lua_warnbuf);
size_t fraglen, buflen = strlen(gl.lua_warnbuf);
if (msg_fragment && buflen < sizeof g.lua_warnbuf - 1) {
if (msg_fragment && buflen < sizeof gl.lua_warnbuf - 1) {
fraglen = strlen(msg_fragment);
if (buflen + fraglen > sizeof g.lua_warnbuf - 1)
fraglen = sizeof g.lua_warnbuf - 1 - buflen;
(void) strncat(g.lua_warnbuf, msg_fragment, fraglen);
if (buflen + fraglen > sizeof gl.lua_warnbuf - 1)
fraglen = sizeof gl.lua_warnbuf - 1 - buflen;
(void) strncat(gl.lua_warnbuf, msg_fragment, fraglen);
}
if (!to_be_continued) {
paniclog("[lua]", g.lua_warnbuf);
g.lua_warnbuf[0] = '\0';
paniclog("[lua]", gl.lua_warnbuf);
gl.lua_warnbuf[0] = '\0';
}
}