move lua context out of dungeons[]

and out of save files so restore doesn't need to clear stale data.
Behavior should be the same as before, except that when entering
the endgame branch and discarding the main dungeon and its other
branches, lua theme context is now discarded for those too.
This commit is contained in:
PatR
2020-05-05 18:06:00 -07:00
parent 19529ffb2f
commit a51e44e532
9 changed files with 57 additions and 44 deletions

View File

@@ -413,7 +413,7 @@ const struct instance_globals g_init = {
DUMMY, /* warnsyms */
/* dungeon.c */
UNDEFINED_VALUE, /* n_dgns */
0, /* n_dgns */
NULL, /* branches */
NULL, /* mapseenchn */
@@ -478,6 +478,7 @@ const struct instance_globals g_init = {
UNDEFINED_VALUE, /* mhitu_dieroll */
/* mklev.c */
UNDEFINED_VALUES, /* luathemes[] */
UNDEFINED_VALUE, /* vault_x */
UNDEFINED_VALUE, /* vault_y */
UNDEFINED_VALUE, /* made_branch */

View File

@@ -1420,6 +1420,8 @@ boolean at_stairs, falling, portal;
update_mlstmv(); /* current monsters are becoming inactive */
if (nhfp->structlevel)
bufon(nhfp->fd); /* use buffered output */
} else {
free_luathemes(TRUE);
}
save_mode = nhfp->mode;
nhfp->mode = cant_go_back ? FREEING : (WRITING | FREEING);

View File

@@ -144,23 +144,24 @@ boolean perform_write, free_data;
bwrite(nhfp->fd, (genericptr_t) &g.n_dgns, sizeof g.n_dgns);
bwrite(nhfp->fd, (genericptr_t) g.dungeons,
sizeof(dungeon) * (unsigned) g.n_dgns);
bwrite(nhfp->fd, (genericptr_t) &g.dungeon_topology, sizeof g.dungeon_topology);
bwrite(nhfp->fd, (genericptr_t) &g.dungeon_topology,
sizeof g.dungeon_topology);
bwrite(nhfp->fd, (genericptr_t) g.tune, sizeof tune);
}
for (count = 0, curr = g.branches; curr; curr = curr->next)
count++;
if (nhfp->structlevel)
bwrite(nhfp->fd, (genericptr_t) &count, sizeof(count));
bwrite(nhfp->fd, (genericptr_t) &count, sizeof count);
for (curr = g.branches; curr; curr = curr->next) {
if (nhfp->structlevel)
bwrite(nhfp->fd, (genericptr_t) curr, sizeof(branch));
bwrite(nhfp->fd, (genericptr_t) curr, sizeof *curr);
}
count = maxledgerno();
if (nhfp->structlevel) {
bwrite(nhfp->fd, (genericptr_t) &count, sizeof count);
bwrite(nhfp->fd, (genericptr_t) g.level_info,
(unsigned) count * sizeof(struct linfo));
(unsigned) count * sizeof (struct linfo));
bwrite(nhfp->fd, (genericptr_t) &g.inv_pos, sizeof g.inv_pos);
}
for (count = 0, curr_ms = g.mapseenchn; curr_ms;
@@ -168,7 +169,7 @@ boolean perform_write, free_data;
count++;
if (nhfp->structlevel)
bwrite(nhfp->fd, (genericptr_t) &count, sizeof(count));
bwrite(nhfp->fd, (genericptr_t) &count, sizeof count);
for (curr_ms = g.mapseenchn; curr_ms; curr_ms = curr_ms->next) {
save_mapseen(nhfp, curr_ms);
@@ -203,23 +204,22 @@ NHFILE *nhfp;
mapseen *curr_ms, *last_ms;
if (nhfp->structlevel) {
mread(nhfp->fd, (genericptr_t) &g.n_dgns, sizeof(g.n_dgns));
mread(nhfp->fd, (genericptr_t) g.dungeons, sizeof(dungeon) * (unsigned) g.n_dgns);
mread(nhfp->fd, (genericptr_t) &g.dungeon_topology, sizeof g.dungeon_topology);
mread(nhfp->fd, (genericptr_t) &g.n_dgns, sizeof g.n_dgns);
mread(nhfp->fd, (genericptr_t) g.dungeons,
sizeof (dungeon) * (unsigned) g.n_dgns);
mread(nhfp->fd, (genericptr_t) &g.dungeon_topology,
sizeof g.dungeon_topology);
mread(nhfp->fd, (genericptr_t) g.tune, sizeof tune);
}
last = g.branches = (branch *) 0;
for (i = 0; i < g.n_dgns; i++)
g.dungeons[i].themelua = (lua_State *) 0;
if (nhfp->structlevel)
mread(nhfp->fd, (genericptr_t) &count, sizeof(count));
mread(nhfp->fd, (genericptr_t) &count, sizeof count);
for (i = 0; i < count; i++) {
curr = (branch *) alloc(sizeof(branch));
curr = (branch *) alloc(sizeof *curr);
if (nhfp->structlevel)
mread(nhfp->fd, (genericptr_t) curr, sizeof(branch));
mread(nhfp->fd, (genericptr_t) curr, sizeof *curr);
curr->next = (branch *) 0;
if (last)
last->next = curr;
@@ -229,18 +229,18 @@ NHFILE *nhfp;
}
if (nhfp->structlevel)
mread(nhfp->fd, (genericptr_t) &count, sizeof(count));
mread(nhfp->fd, (genericptr_t) &count, sizeof count);
if (count >= MAXLINFO)
panic("level information count larger (%d) than allocated size",
count);
if (nhfp->structlevel)
mread(nhfp->fd, (genericptr_t) g.level_info,
(unsigned) count * sizeof(struct linfo));
(unsigned) count * sizeof (struct linfo));
if (nhfp->structlevel) {
mread(nhfp->fd, (genericptr_t) &g.inv_pos, sizeof g.inv_pos);
mread(nhfp->fd, (genericptr_t) &count, sizeof(count));
mread(nhfp->fd, (genericptr_t) &count, sizeof count);
}
last_ms = (mapseen *) 0;
@@ -1030,7 +1030,6 @@ init_dungeons()
Strcpy(g.dungeons[i].dname, dgn_name); /* FIXME: dname length */
Strcpy(g.dungeons[i].proto, dgn_protoname); /* FIXME: proto length */
Strcpy(g.dungeons[i].themerms, dgn_themerms); /* FIXME: length */
g.dungeons[i].themelua = (lua_State *) 0;
g.dungeons[i].boneid = *dgn_bonetag ? *dgn_bonetag : 0;
free((genericptr) dgn_fill);
/* free((genericptr) dgn_protoname); -- stored in pd.tmpdungeon[] */

View File

@@ -239,30 +239,46 @@ boolean special;
g.nsubroom++;
}
void
free_luathemes(keependgame)
boolean keependgame; /* False: exiting, True: discarding main dungeon */
{
int i;
for (i = 0; i < g.n_dgns; ++i) {
if (keependgame && i == astral_level.dnum)
continue;
if (g.luathemes[i]) {
lua_close((lua_State *) g.luathemes[i]);
g.luathemes[i] = (lua_State *) 0;
}
}
}
static void
makerooms()
{
boolean tried_vault = FALSE;
int themeroom_tries = 0;
boolean dothemes = (g.dungeons[u.uz.dnum].themelua != NULL);
char *fname = g.dungeons[u.uz.dnum].themerms;
char *fname;
lua_State *themes = (lua_State *) g.luathemes[u.uz.dnum];
if (*fname && !g.dungeons[u.uz.dnum].themelua) {
g.dungeons[u.uz.dnum].themelua = nhl_init();
if (g.dungeons[u.uz.dnum].themelua) {
if (!nhl_loadlua(g.dungeons[u.uz.dnum].themelua, fname)) {
if (!themes && *(fname = g.dungeons[u.uz.dnum].themerms)) {
if ((themes = nhl_init()) != 0) {
if (!nhl_loadlua(themes, fname)) {
/* loading lua failed, don't use themed rooms */
g.dungeons[u.uz.dnum].themerms[0] = '\0';
lua_close(g.dungeons[u.uz.dnum].themelua);
g.dungeons[u.uz.dnum].themelua = NULL;
dothemes = FALSE;
lua_close(themes);
themes = (lua_State *) 0;
} else {
dothemes = TRUE;
/* success; save state for this dungeon branch */
g.luathemes[u.uz.dnum] = (genericptr_t) themes;
}
}
if (!themes) /* don't try again when making next level */
*fname = '\0'; /* g.dungeons[u.uz.dnum].themerms */
}
if (dothemes) {
if (themes) {
create_des_coder();
}
@@ -277,12 +293,11 @@ makerooms()
g.rooms[g.nroom].hx = -1;
}
} else {
if (dothemes) {
if (themes) {
g.in_mk_themerooms = TRUE;
g.themeroom_failed = FALSE;
lua_getglobal(g.dungeons[u.uz.dnum].themelua,
"themerooms_generate");
lua_call(g.dungeons[u.uz.dnum].themelua, 0, 0);
lua_getglobal(themes, "themerooms_generate");
lua_call(themes, 0, 0);
g.in_mk_themerooms = FALSE;
if (g.themeroom_failed
&& ((themeroom_tries++ > 10)
@@ -294,7 +309,7 @@ makerooms()
}
}
}
if (dothemes) {
if (themes) {
wallification(1, 0, COLNO - 1, ROWNO - 1);
free(g.coder);
g.coder = NULL;

View File

@@ -1070,17 +1070,12 @@ free_dungeons()
{
#ifdef FREE_ALL_MEMORY
NHFILE tnhfp;
int i;
zero_nhfile(&tnhfp); /* also sets fd to -1 */
tnhfp.mode = FREEING;
savelevchn(&tnhfp);
save_dungeon(&tnhfp, FALSE, TRUE);
for (i = 0; i < g.n_dgns; i++)
if (g.dungeons[i].themelua) {
lua_close(g.dungeons[i].themelua);
g.dungeons[i].themelua = (lua_State *) 0;
}
free_luathemes(TRUE);
#endif
return;
}