From 247626dfe552ece836bd404e491b74a646c3e467 Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 10:15:43 -0800 Subject: [PATCH 01/32] Removed unnecessary use of static for nocreate values. If multiple games are played without exiting the process, on the second new player creation the nocreate values will already be set based on the first player creation. --- src/u_init.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/u_init.c b/src/u_init.c index 5b5c5113f..71da5a84a 100644 --- a/src/u_init.c +++ b/src/u_init.c @@ -974,15 +974,16 @@ register struct trobj *trop; struct obj *obj; int otyp, i; - while (trop->trclass) { + NEARDATA short nocreate = STRANGE_OBJECT; + NEARDATA short nocreate2 = STRANGE_OBJECT; + NEARDATA short nocreate3 = STRANGE_OBJECT; + NEARDATA short nocreate4 = STRANGE_OBJECT; + + while (trop->trclass) { otyp = (int) trop->trotyp; if (otyp != UNDEF_TYP) { obj = mksobj(otyp, TRUE, FALSE); } else { /* UNDEF_TYP */ - static NEARDATA short nocreate = STRANGE_OBJECT; - static NEARDATA short nocreate2 = STRANGE_OBJECT; - static NEARDATA short nocreate3 = STRANGE_OBJECT; - static NEARDATA short nocreate4 = STRANGE_OBJECT; /* * For random objects, do not create certain overly powerful * items: wand of wishing, ring of levitation, or the From 250ca464bc2aa5ec565b73c2cbaf9308c8592a70 Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 13:01:58 -0800 Subject: [PATCH 02/32] Modified how we initialize save file info. There should be no net change other then improving code maintenance. --- include/global.h | 27 +++++++++++++++++++++++++++ src/decl.c | 42 ++---------------------------------------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/include/global.h b/include/global.h index 154c49ebe..d57352611 100644 --- a/include/global.h +++ b/include/global.h @@ -294,15 +294,42 @@ struct savefile_info { unsigned long sfi3; /* thirdparty */ }; #ifdef NHSTDC +#define SFI_ZERO (0UL) #define SFI1_EXTERNALCOMP (1UL) #define SFI1_RLECOMP (1UL << 1) #define SFI1_ZEROCOMP (1UL << 2) #else +#define SFI_ZERO (0L) #define SFI1_EXTERNALCOMP (1L) #define SFI1_RLECOMP (1L << 1) #define SFI1_ZEROCOMP (1L << 2) #endif +#if defined(COMPRESS) || defined(ZLIB_COMP) +#define SFI1_DEFAULT_EXTERNALCOMP SFI1_EXTERNALCOMP +#else +#define SFI1_DEFAULT_EXTERNALCOMP SFI_ZERO +#endif + +#if defined(ZEROCOMP) +#define SFI1_DEFAULT_ZEROCOMP SFI1_EXTERNALCOMP +#else +#define SFI1_DEFAULT_ZEROCOMP SFI_ZERO +#endif + +#if defined(RLECOMP) +#define SFI1_DEFAULT_RLECOMP SFI1_RLECOMP +#else +#define SFI1_DEFAULT_RLECOMP SFI_ZERO +#endif + +#define SFI1_DEFAULT (SFI1_DEFAULT_EXTERNALCOMP | \ + SFI1_DEFAULT_ZEROCOMP | \ + SFI1_DEFAULT_RLECOMP) +#define SFI2_DEFAULT SFI_ZERO +#define SFI3_DEFAULT SFI_ZERO + + /* * Configurable internal parameters. * diff --git a/src/decl.c b/src/decl.c index ffd9ef974..15c28d2a1 100644 --- a/src/decl.c +++ b/src/decl.c @@ -285,49 +285,11 @@ char *fqn_prefix_names[PREFIX_COUNT] = { #endif NEARDATA struct savefile_info sfcap = { -#ifdef NHSTDC - 0x00000000UL -#else - 0x00000000L -#endif -#if defined(COMPRESS) || defined(ZLIB_COMP) - | SFI1_EXTERNALCOMP -#endif -#if defined(ZEROCOMP) - | SFI1_ZEROCOMP -#endif -#if defined(RLECOMP) - | SFI1_RLECOMP -#endif - , -#ifdef NHSTDC - 0x00000000UL, 0x00000000UL -#else - 0x00000000L, 0x00000000L -#endif + SFI1_DEFAULT, SFI2_DEFAULT, SFI3_DEFAULT }; NEARDATA struct savefile_info sfrestinfo, sfsaveinfo = { -#ifdef NHSTDC - 0x00000000UL -#else - 0x00000000L -#endif -#if defined(COMPRESS) || defined(ZLIB_COMP) - | SFI1_EXTERNALCOMP -#endif -#if defined(ZEROCOMP) - | SFI1_ZEROCOMP -#endif -#if defined(RLECOMP) - | SFI1_RLECOMP -#endif - , -#ifdef NHSTDC - 0x00000000UL, 0x00000000UL -#else - 0x00000000L, 0x00000000L -#endif + SFI1_DEFAULT, SFI2_DEFAULT, SFI3_DEFAULT }; struct plinemsg_type *plinemsg_types = (struct plinemsg_type *) 0; From c1dbae9f1fcdb3ff850107a0152c88b6a8f0cb07 Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 13:05:44 -0800 Subject: [PATCH 03/32] Added PLAYAGAIN experimental support definition. --- include/config.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/config.h b/include/config.h index 4f242679a..761262c4b 100644 --- a/include/config.h +++ b/include/config.h @@ -552,6 +552,11 @@ typedef unsigned char uchar; #endif +/* PLAYAGAIN is support for allowing the game shell to stay open after the player + saves or dies. This requires that the game engine can be re-entered to start + another game. This support is primarily about ensuring that game engine + state is cleaned up properly to allow the game engine to be re-initialized. */ +/* #define PLAYAGAIN */ /* End of Section 4 */ From d5c792a87cce18f1a985a8387a9b759e62ed50ce Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 14:21:31 -0800 Subject: [PATCH 04/32] Revert "Modified how we initialize save file info." This reverts commit 250ca464bc2aa5ec565b73c2cbaf9308c8592a70. --- include/global.h | 27 --------------------------- src/decl.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/include/global.h b/include/global.h index d57352611..154c49ebe 100644 --- a/include/global.h +++ b/include/global.h @@ -294,42 +294,15 @@ struct savefile_info { unsigned long sfi3; /* thirdparty */ }; #ifdef NHSTDC -#define SFI_ZERO (0UL) #define SFI1_EXTERNALCOMP (1UL) #define SFI1_RLECOMP (1UL << 1) #define SFI1_ZEROCOMP (1UL << 2) #else -#define SFI_ZERO (0L) #define SFI1_EXTERNALCOMP (1L) #define SFI1_RLECOMP (1L << 1) #define SFI1_ZEROCOMP (1L << 2) #endif -#if defined(COMPRESS) || defined(ZLIB_COMP) -#define SFI1_DEFAULT_EXTERNALCOMP SFI1_EXTERNALCOMP -#else -#define SFI1_DEFAULT_EXTERNALCOMP SFI_ZERO -#endif - -#if defined(ZEROCOMP) -#define SFI1_DEFAULT_ZEROCOMP SFI1_EXTERNALCOMP -#else -#define SFI1_DEFAULT_ZEROCOMP SFI_ZERO -#endif - -#if defined(RLECOMP) -#define SFI1_DEFAULT_RLECOMP SFI1_RLECOMP -#else -#define SFI1_DEFAULT_RLECOMP SFI_ZERO -#endif - -#define SFI1_DEFAULT (SFI1_DEFAULT_EXTERNALCOMP | \ - SFI1_DEFAULT_ZEROCOMP | \ - SFI1_DEFAULT_RLECOMP) -#define SFI2_DEFAULT SFI_ZERO -#define SFI3_DEFAULT SFI_ZERO - - /* * Configurable internal parameters. * diff --git a/src/decl.c b/src/decl.c index 15c28d2a1..ffd9ef974 100644 --- a/src/decl.c +++ b/src/decl.c @@ -285,11 +285,49 @@ char *fqn_prefix_names[PREFIX_COUNT] = { #endif NEARDATA struct savefile_info sfcap = { - SFI1_DEFAULT, SFI2_DEFAULT, SFI3_DEFAULT +#ifdef NHSTDC + 0x00000000UL +#else + 0x00000000L +#endif +#if defined(COMPRESS) || defined(ZLIB_COMP) + | SFI1_EXTERNALCOMP +#endif +#if defined(ZEROCOMP) + | SFI1_ZEROCOMP +#endif +#if defined(RLECOMP) + | SFI1_RLECOMP +#endif + , +#ifdef NHSTDC + 0x00000000UL, 0x00000000UL +#else + 0x00000000L, 0x00000000L +#endif }; NEARDATA struct savefile_info sfrestinfo, sfsaveinfo = { - SFI1_DEFAULT, SFI2_DEFAULT, SFI3_DEFAULT +#ifdef NHSTDC + 0x00000000UL +#else + 0x00000000L +#endif +#if defined(COMPRESS) || defined(ZLIB_COMP) + | SFI1_EXTERNALCOMP +#endif +#if defined(ZEROCOMP) + | SFI1_ZEROCOMP +#endif +#if defined(RLECOMP) + | SFI1_RLECOMP +#endif + , +#ifdef NHSTDC + 0x00000000UL, 0x00000000UL +#else + 0x00000000L, 0x00000000L +#endif }; struct plinemsg_type *plinemsg_types = (struct plinemsg_type *) 0; From 95208cff1dcf9df9e56f45ab280bf66a02a9cc6c Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 15:03:00 -0800 Subject: [PATCH 05/32] Added nhassert() support to tile2bmp. --- win/share/tile2bmp.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/win/share/tile2bmp.c b/win/share/tile2bmp.c index ca2c7dd73..6722bbc26 100644 --- a/win/share/tile2bmp.c +++ b/win/share/tile2bmp.c @@ -360,3 +360,10 @@ pixel (*pixels)[TILE_X]; } } } + +/* nhassert_failed is called when an nhassert's condition is false */ +void nhassert_failed(const char * exp, const char * file, int line) +{ + Fprintf(stderr, "NHASSERT(%s) in '%s' at line %d\n", exp, file, line); + exit(EXIT_FAILURE); +} From 3e8d98dee5403fd6610a15b2bcacd08a856d7984 Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 15:03:26 -0800 Subject: [PATCH 06/32] Added nhassert() support. --- util/lev_main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/util/lev_main.c b/util/lev_main.c index ecf7a444c..729549791 100644 --- a/util/lev_main.c +++ b/util/lev_main.c @@ -1637,4 +1637,11 @@ short ospeed; #endif #endif /* STRICT_REF_DEF */ +/* nhassert_failed is called when an nhassert's condition is false */ +void nhassert_failed(const char * exp, const char * file, int line) +{ + fprintf(stderr, "NHASSERT(%s) in '%s' at line %d\n", exp, file, line); + exit(EXIT_FAILURE); +} + /*lev_main.c*/ From 6737fee07d9cdc7ac88bf003c32f97a5b7a07a6a Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 15:20:17 -0800 Subject: [PATCH 07/32] Added decl_early_init() which is called when PLAYAGAIN is supported. decl_early_init() is called when we are starting a game. On first start, it validates that global state is in the expected state. When called on subsequent starts, it initializes global state to expected state. --- include/config.h | 11 +- include/extern.h | 3 + src/decl.c | 259 ++++++++++++++++++++++++++++++++++++++++++++ sys/share/pcmain.c | 4 + sys/winnt/stubs.c | 3 + win/win32/winhack.c | 3 + 6 files changed, 279 insertions(+), 4 deletions(-) diff --git a/include/config.h b/include/config.h index 761262c4b..d4f67e1ac 100644 --- a/include/config.h +++ b/include/config.h @@ -552,10 +552,13 @@ typedef unsigned char uchar; #endif -/* PLAYAGAIN is support for allowing the game shell to stay open after the player - saves or dies. This requires that the game engine can be re-entered to start - another game. This support is primarily about ensuring that game engine - state is cleaned up properly to allow the game engine to be re-initialized. */ +/* PLAYAGAIN support for allowing the game shell to stay open after the player + * saves or dies. This requires that the game engine can be re-entered to + * start another game. + * + * This support does not include supporting playing another game when + * a panic has occured due to undetermined state the engine is left in after a + * panic */ /* #define PLAYAGAIN */ /* End of Section 4 */ diff --git a/include/extern.h b/include/extern.h index 89910b0bb..d3a251b83 100644 --- a/include/extern.h +++ b/include/extern.h @@ -251,6 +251,9 @@ E void FDECL(destroy_drawbridge, (int, int)); /* ### decl.c ### */ E void NDECL(decl_init); +#ifdef PLAYAGAIN +E void NDECL(decl_early_init); +#endif /* ### detect.c ### */ diff --git a/src/decl.c b/src/decl.c index ffd9ef974..6c26b4422 100644 --- a/src/decl.c +++ b/src/decl.c @@ -284,6 +284,31 @@ char *fqn_prefix_names[PREFIX_COUNT] = { }; #endif +#ifdef PLAYAGAIN +const struct savefile_info default_sfinfo = { +#ifdef NHSTDC + 0x00000000UL +#else + 0x00000000L +#endif +#if defined(COMPRESS) || defined(ZLIB_COMP) + | SFI1_EXTERNALCOMP +#endif +#if defined(ZEROCOMP) + | SFI1_ZEROCOMP +#endif +#if defined(RLECOMP) + | SFI1_RLECOMP +#endif + , +#ifdef NHSTDC + 0x00000000UL, 0x00000000UL +#else + 0x00000000L, 0x00000000L +#endif +}; +#endif + NEARDATA struct savefile_info sfcap = { #ifdef NHSTDC 0x00000000UL @@ -346,4 +371,238 @@ decl_init() return; } +#ifdef PLAYAGAIN + +static boolean s_firstStart = TRUE; + +static boolean +decl_is_block_zero(p, n) +unsigned char * p; +int n; +{ + static unsigned char zeroblock[512] = { 0 }; + unsigned char * sentinel = p + n; + while (p < sentinel) { + int c = (n < sizeof(zeroblock) ? n : sizeof(zeroblock)); + if (memcmp(p, zeroblock, c) != 0) return FALSE; + p += c; + } + return TRUE; +} + +static void +decl_zero_block(p, n) +unsigned char * p; +int n; +{ + nhassert(!s_firstStart || decl_is_block_zero(p, n)); + memset(p, 0, n); +} + +#define ZEROARRAY(x) decl_zero_block((void *) &x[0], 0, sizeof(x)) +#define ZEROARRAYN(x,n) decl_zero_block((void *) &x[0], 0, sizeof(x[0])*(n)) +#define ZERO(x) decl_zero_block((void *) &x, 0, sizeof(x)) +#define ZEROPTR(x) { nhassert(x == NULL); x = NULL; } +#define ZEROPTRNOCHECK(x) { x = NULL; } + +/* decl_early_init() is called when we are starting a game. On first + * start, it validates that global state is in the expected state. + * When called on subsequent starts, it initializes global state to + * expected state. + * + * In the case that of global pointers, on subsequent starts it will + * panic if it finds a non-NULL pointer with the assumption that a + * pointer has leaked. */ + +void +decl_early_init() +{ + hackpid = 0; +#if defined(UNIX) || defined(VMS) + locknum = 0; +#endif +#ifdef DEF_PAGER + catmore = 0; +#endif + + ZEROARRAY(bases); + + multi = 0; + multi_reason = NULL; + nroom = 0; + nsubroom = 0; + occtime = 0; + + x_maze_max = (COLNO - 1) & ~1; + y_maze_max = (ROWNO - 1) & ~1; + + otg_temp = 0; + + ZERO(dungeon_topology); + ZERO(quest_status); + + warn_obj_cnt = 0; + ZEROARRAYN(smeq, MAXNROFROOMS + 1); + doorindex = 0; + save_cm = NULL; + + ZERO(killer); + done_money = 0; + nomovemsg = NULL; + ZEROARRAY(plname); + ZEROARRAY(pl_character); + pl_race = '\0'; + + ZEROARRAY(pl_fruit); + ffruit = NULL; + + ZEROARRAY(tune); + + occtxt = NULL; + + yn_number = 0; + +#if defined(MICRO) || defined(WIN32) + ZEROARRAYN(hackdir, PATHLEN); +#ifdef MICRO + ZEROARRAYN(levels, PATHLEN); +#endif /* MICRO */ +#endif /* MICRO || WIN32 */ + +#ifdef MFLOPPY + ZEROARRAYN(permbones, PATHLEN); + ramdisk = FALSE; + saveprompt = TRUE; +#endif + + ZEROARRAY(level_info); + + ZERO(program_state); + + tbx = 0; + tby = 0; + + ZERO(m_shot); + + ZEROARRAYN(dungeons, MAXDUNGEON); + ZEROPTR(sp_levchn); + ZERO(upstair); + ZERO(dnstair); + ZERO(upladder); + ZERO(dnladder); + ZERO(sstairs); + ZERO(updest); + ZERO(dndest); + ZERO(inv_pos); + + defer_see_monsters = FALSE; + in_mklev = FALSE; + stoned = FALSE; + unweapon = FALSE; + mrg_to_wielded = FALSE; + + in_steed_dismounting = FALSE; + + ZERO(bhitpos); + ZEROARRAY(doors); + + ZEROARRAY(rooms); + subrooms = &rooms[MAXNROFROOMS + 1]; + upstairs_room = NULL; + dnstairs_room = NULL; + sstairs_room = NULL; + + ZERO(level); + ZEROPTR(ftrap); + ZERO(youmonst); + ZERO(context); + ZERO(flags); +#ifdef SYSFLAGS + ZERO(sysflags); +#endif + ZERO(iflags); + ZERO(u); + ZERO(ubirthday); + ZERO(urealtime); + + ZEROARRAY(lastseentyp); + + ZEROPTR(invent); + ZEROPTRNOCHECK(uwep); + ZEROPTRNOCHECK(uarm); + ZEROPTRNOCHECK(uswapwep); + ZEROPTRNOCHECK(uquiver); + ZEROPTRNOCHECK(uarmu); + ZEROPTRNOCHECK(uskin); + ZEROPTRNOCHECK(uarmc); + ZEROPTRNOCHECK(uarmh); + ZEROPTRNOCHECK(uarms); + ZEROPTRNOCHECK(uarmg); + ZEROPTRNOCHECK(uarmf); + ZEROPTRNOCHECK(uamul); + ZEROPTRNOCHECK(uright); + ZEROPTRNOCHECK(uleft); + ZEROPTRNOCHECK(ublindf); + ZEROPTRNOCHECK(uchain); + ZEROPTRNOCHECK(uball); + + ZEROPTR(current_wand); + ZEROPTR(thrownobj); + ZEROPTR(kickedobj); + + ZEROARRAYN(spl_book, MAXSPELL + 1); + + moves = 1; + monstermoves = 1; + + wailmsg = 0L; + + ZEROPTR(migrating_objs); + ZEROPTR(billobjs); + + ZERO(zeroobj); + ZERO(zeromonst); + ZERO(zeroany); + + ZEROARRAYN(dogname, PL_PSIZ); + ZEROARRAYN(catname, PL_PSIZ); + ZEROARRAYN(horsename, PL_PSIZ); + preferred_pet = 0; + ZEROPTR(mydogs); + ZEROPTR(migrating_mons); + + ZEROARRAY(mvitals); + + ZEROPTR(menu_colorings); + + vision_full_recalc = 0; + viz_array = NULL; + + WIN_MESSAGE = WIN_ERR; +#ifndef STATUS_VIA_WINDOWPORT + WIN_STATUS = WIN_ERR; +#endif + WIN_MAP = WIN_ERR; + WIN_INVEN = WIN_ERR; + + ZEROARRAYN(toplines, TBUFSZ); + ZERO(tc_gbl_data); + ZEROARRAYN(fqn_prefix, PREFIX_COUNT); + + sfcap = default_sfinfo; + sfrestinfo = default_sfinfo; + sfsaveinfo = default_sfinfo; + + ZEROPTR(plinemsg_types); + +#ifdef PANICTRACE + ARGV0 = NULL; +#endif + + nhUse_dummy = 0; + + s_firstStart = FALSE; +} +#endif + /*decl.c*/ diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index 93674ab34..9ffbeca51 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -97,7 +97,11 @@ char *argv[]; nethack_enter(argc, argv); +#ifdef PLAYAGAIN + decl_early_init(); +#endif sys_early_init(); + #if defined(WIN32) && defined(TTY_GRAPHICS) Strcpy(default_window_sys, "tty"); #else diff --git a/sys/winnt/stubs.c b/sys/winnt/stubs.c index bb5ad82e4..ff3cf5919 100644 --- a/sys/winnt/stubs.c +++ b/sys/winnt/stubs.c @@ -37,6 +37,9 @@ char *argv[]; { boolean resuming; +#ifdef PLAYAGAIN + decl_early_init(); +#endif sys_early_init(); Strcpy(default_window_sys, "tty"); resuming = pcmain(argc, argv); diff --git a/win/win32/winhack.c b/win/win32/winhack.c index 9dcbb4e4b..6db66261e 100644 --- a/win/win32/winhack.c +++ b/win/win32/winhack.c @@ -97,6 +97,9 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, win10_init(); +#ifdef PLAYAGAIN + decl_early_init(); +#endif sys_early_init(); /* init applicatio structure */ From 79e0a11c15ba03af2dc57359e7b0b42b4767576a Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 15:40:31 -0800 Subject: [PATCH 08/32] Previous encumberance state moved to decl.c. --- include/decl.h | 3 +++ src/decl.c | 5 +++++ src/pickup.c | 1 - 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/decl.h b/include/decl.h index 992e86ad5..7fcde0c6c 100644 --- a/include/decl.h +++ b/include/decl.h @@ -437,6 +437,9 @@ struct early_opt { boolean valallowed; }; +/* encumbrance */ +E int oldcap; + #undef E #endif /* DECL_H */ diff --git a/src/decl.c b/src/decl.c index 6c26b4422..c3397a048 100644 --- a/src/decl.c +++ b/src/decl.c @@ -214,6 +214,9 @@ NEARDATA struct monst *migrating_mons = (struct monst *) 0; NEARDATA struct mvitals mvitals[NUMMONS]; +/* originally from pickup.c */ +int oldcap = 0; /* encumbrance */ + NEARDATA struct c_color_names c_color_names = { "black", "amber", "golden", "light blue", "red", "green", "silver", "blue", "purple", "white", "orange" @@ -525,6 +528,8 @@ decl_early_init() ZERO(ubirthday); ZERO(urealtime); + ZERO(oldcap); + ZEROARRAY(lastseentyp); ZEROPTR(invent); diff --git a/src/pickup.c b/src/pickup.c index d68def38d..b32640dbd 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -1556,7 +1556,6 @@ struct obj *otmp; int encumber_msg() { - static int oldcap = UNENCUMBERED; int newcap = near_capacity(); if (oldcap < newcap) { From 48c89acef440216c7bec2090b8097f56dcd288e7 Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 15:49:43 -0800 Subject: [PATCH 09/32] Moved petname_used to decl.c --- include/decl.h | 3 +++ src/decl.c | 4 +++- src/dog.c | 1 - 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/decl.h b/include/decl.h index 7fcde0c6c..5e211f053 100644 --- a/include/decl.h +++ b/include/decl.h @@ -192,6 +192,8 @@ E NEARDATA char dogname[]; E NEARDATA char catname[]; E NEARDATA char horsename[]; E char preferred_pet; +E int petname_used; + E const char *occtxt; /* defined when occupation != NULL */ E const char *nomovemsg; E char lock[]; @@ -440,6 +442,7 @@ struct early_opt { /* encumbrance */ E int oldcap; + #undef E #endif /* DECL_H */ diff --git a/src/decl.c b/src/decl.c index c3397a048..10f67f7de 100644 --- a/src/decl.c +++ b/src/decl.c @@ -207,6 +207,7 @@ NEARDATA char dogname[PL_PSIZ] = DUMMY; NEARDATA char catname[PL_PSIZ] = DUMMY; NEARDATA char horsename[PL_PSIZ] = DUMMY; char preferred_pet; /* '\0', 'c', 'd', 'n' (none) */ +int petname_used = 0; /* monsters that went down/up together with @ */ NEARDATA struct monst *mydogs = (struct monst *) 0; /* monsters that are moving to another dungeon level */ @@ -572,7 +573,8 @@ decl_early_init() ZEROARRAYN(dogname, PL_PSIZ); ZEROARRAYN(catname, PL_PSIZ); ZEROARRAYN(horsename, PL_PSIZ); - preferred_pet = 0; + ZERO(preferred_pet); + ZERO(petname_used); ZEROPTR(mydogs); ZEROPTR(migrating_mons); diff --git a/src/dog.c b/src/dog.c index 0d967195e..7c1b62e8b 100644 --- a/src/dog.c +++ b/src/dog.c @@ -160,7 +160,6 @@ makedog() register struct obj *otmp; const char *petname; int pettype; - static int petname_used = 0; if (preferred_pet == 'n') return ((struct monst *) 0); From 0ebe905ad171194bf80544ff6d8ac5eb2bf44506 Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 20:47:22 -0800 Subject: [PATCH 10/32] Initial check in of icontext. --- include/decl.h | 16 ++++++++++++++-- src/decl.c | 19 ++++++++++++++----- src/pickup.c | 6 +++--- sys/share/pcmain.c | 4 +--- sys/winnt/stubs.c | 4 +--- win/win32/winhack.c | 4 +--- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/include/decl.h b/include/decl.h index 5e211f053..2cc7f965c 100644 --- a/include/decl.h +++ b/include/decl.h @@ -439,8 +439,20 @@ struct early_opt { boolean valallowed; }; -/* encumbrance */ -E int oldcap; +/* instance_context holds per game instance data that does not need to be + * persisted upon game exit. This game instance data is one of the first + * things initialized during the initialization of the game engine. + * It is initialized with icontext_initial_state found in decl.c */ + +#define PLAYAGAIN + +struct instance_context { + int oldcap; /* encumberance - pickup.c */ +}; + +E struct instance_context icontext; + +E void icontext_init(); #undef E diff --git a/src/decl.c b/src/decl.c index 10f67f7de..edfeb3a60 100644 --- a/src/decl.c +++ b/src/decl.c @@ -215,9 +215,6 @@ NEARDATA struct monst *migrating_mons = (struct monst *) 0; NEARDATA struct mvitals mvitals[NUMMONS]; -/* originally from pickup.c */ -int oldcap = 0; /* encumbrance */ - NEARDATA struct c_color_names c_color_names = { "black", "amber", "golden", "light blue", "red", "green", "silver", "blue", "purple", "white", "orange" @@ -529,8 +526,6 @@ decl_early_init() ZERO(ubirthday); ZERO(urealtime); - ZERO(oldcap); - ZEROARRAY(lastseentyp); ZEROPTR(invent); @@ -612,4 +607,18 @@ decl_early_init() } #endif +const struct instance_context icontext_initial_state = { + 0, /* oldcap - last encumberance in pickup.c */ +}; + +struct instance_context icontext; + +void +icontext_init() +{ + icontext = icontext_initial_state; + + decl_early_init(); +}; + /*decl.c*/ diff --git a/src/pickup.c b/src/pickup.c index b32640dbd..83d5eb273 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -1558,7 +1558,7 @@ encumber_msg() { int newcap = near_capacity(); - if (oldcap < newcap) { + if (icontext.oldcap < newcap) { switch (newcap) { case 1: Your("movements are slowed slightly because of your load."); @@ -1576,7 +1576,7 @@ encumber_msg() break; } context.botl = 1; - } else if (oldcap > newcap) { + } else if (icontext.oldcap > newcap) { switch (newcap) { case 0: Your("movements are now unencumbered."); @@ -1595,7 +1595,7 @@ encumber_msg() context.botl = 1; } - oldcap = newcap; + icontext.oldcap = newcap; return newcap; } diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index 9ffbeca51..932ec971e 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -97,9 +97,7 @@ char *argv[]; nethack_enter(argc, argv); -#ifdef PLAYAGAIN - decl_early_init(); -#endif + icontext_init(); sys_early_init(); #if defined(WIN32) && defined(TTY_GRAPHICS) diff --git a/sys/winnt/stubs.c b/sys/winnt/stubs.c index ff3cf5919..9e16c6dce 100644 --- a/sys/winnt/stubs.c +++ b/sys/winnt/stubs.c @@ -37,9 +37,7 @@ char *argv[]; { boolean resuming; -#ifdef PLAYAGAIN - decl_early_init(); -#endif + icontext_init(); sys_early_init(); Strcpy(default_window_sys, "tty"); resuming = pcmain(argc, argv); diff --git a/win/win32/winhack.c b/win/win32/winhack.c index 6db66261e..ef48d6b63 100644 --- a/win/win32/winhack.c +++ b/win/win32/winhack.c @@ -97,9 +97,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, win10_init(); -#ifdef PLAYAGAIN - decl_early_init(); -#endif + icontext_init(); sys_early_init(); /* init applicatio structure */ From 455d2769e084c368912a86c3d30b745b3e165ab3 Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 13:01:58 -0800 Subject: [PATCH 11/32] Initial check-in of icontext work. --- include/config.h | 8 ++ include/decl.h | 18 +++ include/extern.h | 3 + src/decl.c | 275 +++++++++++++++++++++++++++++++++++++++++++ src/dog.c | 1 - src/pickup.c | 7 +- sys/share/pcmain.c | 2 + sys/winnt/stubs.c | 1 + util/lev_main.c | 7 ++ win/share/tile2bmp.c | 7 ++ win/win32/winhack.c | 1 + 11 files changed, 325 insertions(+), 5 deletions(-) diff --git a/include/config.h b/include/config.h index 4f242679a..d4f67e1ac 100644 --- a/include/config.h +++ b/include/config.h @@ -552,6 +552,14 @@ typedef unsigned char uchar; #endif +/* PLAYAGAIN support for allowing the game shell to stay open after the player + * saves or dies. This requires that the game engine can be re-entered to + * start another game. + * + * This support does not include supporting playing another game when + * a panic has occured due to undetermined state the engine is left in after a + * panic */ +/* #define PLAYAGAIN */ /* End of Section 4 */ diff --git a/include/decl.h b/include/decl.h index 992e86ad5..2cc7f965c 100644 --- a/include/decl.h +++ b/include/decl.h @@ -192,6 +192,8 @@ E NEARDATA char dogname[]; E NEARDATA char catname[]; E NEARDATA char horsename[]; E char preferred_pet; +E int petname_used; + E const char *occtxt; /* defined when occupation != NULL */ E const char *nomovemsg; E char lock[]; @@ -437,6 +439,22 @@ struct early_opt { boolean valallowed; }; +/* instance_context holds per game instance data that does not need to be + * persisted upon game exit. This game instance data is one of the first + * things initialized during the initialization of the game engine. + * It is initialized with icontext_initial_state found in decl.c */ + +#define PLAYAGAIN + +struct instance_context { + int oldcap; /* encumberance - pickup.c */ +}; + +E struct instance_context icontext; + +E void icontext_init(); + + #undef E #endif /* DECL_H */ diff --git a/include/extern.h b/include/extern.h index 89910b0bb..d3a251b83 100644 --- a/include/extern.h +++ b/include/extern.h @@ -251,6 +251,9 @@ E void FDECL(destroy_drawbridge, (int, int)); /* ### decl.c ### */ E void NDECL(decl_init); +#ifdef PLAYAGAIN +E void NDECL(decl_early_init); +#endif /* ### detect.c ### */ diff --git a/src/decl.c b/src/decl.c index ffd9ef974..edfeb3a60 100644 --- a/src/decl.c +++ b/src/decl.c @@ -207,6 +207,7 @@ NEARDATA char dogname[PL_PSIZ] = DUMMY; NEARDATA char catname[PL_PSIZ] = DUMMY; NEARDATA char horsename[PL_PSIZ] = DUMMY; char preferred_pet; /* '\0', 'c', 'd', 'n' (none) */ +int petname_used = 0; /* monsters that went down/up together with @ */ NEARDATA struct monst *mydogs = (struct monst *) 0; /* monsters that are moving to another dungeon level */ @@ -284,6 +285,31 @@ char *fqn_prefix_names[PREFIX_COUNT] = { }; #endif +#ifdef PLAYAGAIN +const struct savefile_info default_sfinfo = { +#ifdef NHSTDC + 0x00000000UL +#else + 0x00000000L +#endif +#if defined(COMPRESS) || defined(ZLIB_COMP) + | SFI1_EXTERNALCOMP +#endif +#if defined(ZEROCOMP) + | SFI1_ZEROCOMP +#endif +#if defined(RLECOMP) + | SFI1_RLECOMP +#endif + , +#ifdef NHSTDC + 0x00000000UL, 0x00000000UL +#else + 0x00000000L, 0x00000000L +#endif +}; +#endif + NEARDATA struct savefile_info sfcap = { #ifdef NHSTDC 0x00000000UL @@ -346,4 +372,253 @@ decl_init() return; } +#ifdef PLAYAGAIN + +static boolean s_firstStart = TRUE; + +static boolean +decl_is_block_zero(p, n) +unsigned char * p; +int n; +{ + static unsigned char zeroblock[512] = { 0 }; + unsigned char * sentinel = p + n; + while (p < sentinel) { + int c = (n < sizeof(zeroblock) ? n : sizeof(zeroblock)); + if (memcmp(p, zeroblock, c) != 0) return FALSE; + p += c; + } + return TRUE; +} + +static void +decl_zero_block(p, n) +unsigned char * p; +int n; +{ + nhassert(!s_firstStart || decl_is_block_zero(p, n)); + memset(p, 0, n); +} + +#define ZEROARRAY(x) decl_zero_block((void *) &x[0], 0, sizeof(x)) +#define ZEROARRAYN(x,n) decl_zero_block((void *) &x[0], 0, sizeof(x[0])*(n)) +#define ZERO(x) decl_zero_block((void *) &x, 0, sizeof(x)) +#define ZEROPTR(x) { nhassert(x == NULL); x = NULL; } +#define ZEROPTRNOCHECK(x) { x = NULL; } + +/* decl_early_init() is called when we are starting a game. On first + * start, it validates that global state is in the expected state. + * When called on subsequent starts, it initializes global state to + * expected state. + * + * In the case that of global pointers, on subsequent starts it will + * panic if it finds a non-NULL pointer with the assumption that a + * pointer has leaked. */ + +void +decl_early_init() +{ + hackpid = 0; +#if defined(UNIX) || defined(VMS) + locknum = 0; +#endif +#ifdef DEF_PAGER + catmore = 0; +#endif + + ZEROARRAY(bases); + + multi = 0; + multi_reason = NULL; + nroom = 0; + nsubroom = 0; + occtime = 0; + + x_maze_max = (COLNO - 1) & ~1; + y_maze_max = (ROWNO - 1) & ~1; + + otg_temp = 0; + + ZERO(dungeon_topology); + ZERO(quest_status); + + warn_obj_cnt = 0; + ZEROARRAYN(smeq, MAXNROFROOMS + 1); + doorindex = 0; + save_cm = NULL; + + ZERO(killer); + done_money = 0; + nomovemsg = NULL; + ZEROARRAY(plname); + ZEROARRAY(pl_character); + pl_race = '\0'; + + ZEROARRAY(pl_fruit); + ffruit = NULL; + + ZEROARRAY(tune); + + occtxt = NULL; + + yn_number = 0; + +#if defined(MICRO) || defined(WIN32) + ZEROARRAYN(hackdir, PATHLEN); +#ifdef MICRO + ZEROARRAYN(levels, PATHLEN); +#endif /* MICRO */ +#endif /* MICRO || WIN32 */ + +#ifdef MFLOPPY + ZEROARRAYN(permbones, PATHLEN); + ramdisk = FALSE; + saveprompt = TRUE; +#endif + + ZEROARRAY(level_info); + + ZERO(program_state); + + tbx = 0; + tby = 0; + + ZERO(m_shot); + + ZEROARRAYN(dungeons, MAXDUNGEON); + ZEROPTR(sp_levchn); + ZERO(upstair); + ZERO(dnstair); + ZERO(upladder); + ZERO(dnladder); + ZERO(sstairs); + ZERO(updest); + ZERO(dndest); + ZERO(inv_pos); + + defer_see_monsters = FALSE; + in_mklev = FALSE; + stoned = FALSE; + unweapon = FALSE; + mrg_to_wielded = FALSE; + + in_steed_dismounting = FALSE; + + ZERO(bhitpos); + ZEROARRAY(doors); + + ZEROARRAY(rooms); + subrooms = &rooms[MAXNROFROOMS + 1]; + upstairs_room = NULL; + dnstairs_room = NULL; + sstairs_room = NULL; + + ZERO(level); + ZEROPTR(ftrap); + ZERO(youmonst); + ZERO(context); + ZERO(flags); +#ifdef SYSFLAGS + ZERO(sysflags); +#endif + ZERO(iflags); + ZERO(u); + ZERO(ubirthday); + ZERO(urealtime); + + ZEROARRAY(lastseentyp); + + ZEROPTR(invent); + ZEROPTRNOCHECK(uwep); + ZEROPTRNOCHECK(uarm); + ZEROPTRNOCHECK(uswapwep); + ZEROPTRNOCHECK(uquiver); + ZEROPTRNOCHECK(uarmu); + ZEROPTRNOCHECK(uskin); + ZEROPTRNOCHECK(uarmc); + ZEROPTRNOCHECK(uarmh); + ZEROPTRNOCHECK(uarms); + ZEROPTRNOCHECK(uarmg); + ZEROPTRNOCHECK(uarmf); + ZEROPTRNOCHECK(uamul); + ZEROPTRNOCHECK(uright); + ZEROPTRNOCHECK(uleft); + ZEROPTRNOCHECK(ublindf); + ZEROPTRNOCHECK(uchain); + ZEROPTRNOCHECK(uball); + + ZEROPTR(current_wand); + ZEROPTR(thrownobj); + ZEROPTR(kickedobj); + + ZEROARRAYN(spl_book, MAXSPELL + 1); + + moves = 1; + monstermoves = 1; + + wailmsg = 0L; + + ZEROPTR(migrating_objs); + ZEROPTR(billobjs); + + ZERO(zeroobj); + ZERO(zeromonst); + ZERO(zeroany); + + ZEROARRAYN(dogname, PL_PSIZ); + ZEROARRAYN(catname, PL_PSIZ); + ZEROARRAYN(horsename, PL_PSIZ); + ZERO(preferred_pet); + ZERO(petname_used); + ZEROPTR(mydogs); + ZEROPTR(migrating_mons); + + ZEROARRAY(mvitals); + + ZEROPTR(menu_colorings); + + vision_full_recalc = 0; + viz_array = NULL; + + WIN_MESSAGE = WIN_ERR; +#ifndef STATUS_VIA_WINDOWPORT + WIN_STATUS = WIN_ERR; +#endif + WIN_MAP = WIN_ERR; + WIN_INVEN = WIN_ERR; + + ZEROARRAYN(toplines, TBUFSZ); + ZERO(tc_gbl_data); + ZEROARRAYN(fqn_prefix, PREFIX_COUNT); + + sfcap = default_sfinfo; + sfrestinfo = default_sfinfo; + sfsaveinfo = default_sfinfo; + + ZEROPTR(plinemsg_types); + +#ifdef PANICTRACE + ARGV0 = NULL; +#endif + + nhUse_dummy = 0; + + s_firstStart = FALSE; +} +#endif + +const struct instance_context icontext_initial_state = { + 0, /* oldcap - last encumberance in pickup.c */ +}; + +struct instance_context icontext; + +void +icontext_init() +{ + icontext = icontext_initial_state; + + decl_early_init(); +}; + /*decl.c*/ diff --git a/src/dog.c b/src/dog.c index 0d967195e..7c1b62e8b 100644 --- a/src/dog.c +++ b/src/dog.c @@ -160,7 +160,6 @@ makedog() register struct obj *otmp; const char *petname; int pettype; - static int petname_used = 0; if (preferred_pet == 'n') return ((struct monst *) 0); diff --git a/src/pickup.c b/src/pickup.c index d68def38d..83d5eb273 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -1556,10 +1556,9 @@ struct obj *otmp; int encumber_msg() { - static int oldcap = UNENCUMBERED; int newcap = near_capacity(); - if (oldcap < newcap) { + if (icontext.oldcap < newcap) { switch (newcap) { case 1: Your("movements are slowed slightly because of your load."); @@ -1577,7 +1576,7 @@ encumber_msg() break; } context.botl = 1; - } else if (oldcap > newcap) { + } else if (icontext.oldcap > newcap) { switch (newcap) { case 0: Your("movements are now unencumbered."); @@ -1596,7 +1595,7 @@ encumber_msg() context.botl = 1; } - oldcap = newcap; + icontext.oldcap = newcap; return newcap; } diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index 93674ab34..932ec971e 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -97,7 +97,9 @@ char *argv[]; nethack_enter(argc, argv); + icontext_init(); sys_early_init(); + #if defined(WIN32) && defined(TTY_GRAPHICS) Strcpy(default_window_sys, "tty"); #else diff --git a/sys/winnt/stubs.c b/sys/winnt/stubs.c index bb5ad82e4..9e16c6dce 100644 --- a/sys/winnt/stubs.c +++ b/sys/winnt/stubs.c @@ -37,6 +37,7 @@ char *argv[]; { boolean resuming; + icontext_init(); sys_early_init(); Strcpy(default_window_sys, "tty"); resuming = pcmain(argc, argv); diff --git a/util/lev_main.c b/util/lev_main.c index ecf7a444c..729549791 100644 --- a/util/lev_main.c +++ b/util/lev_main.c @@ -1637,4 +1637,11 @@ short ospeed; #endif #endif /* STRICT_REF_DEF */ +/* nhassert_failed is called when an nhassert's condition is false */ +void nhassert_failed(const char * exp, const char * file, int line) +{ + fprintf(stderr, "NHASSERT(%s) in '%s' at line %d\n", exp, file, line); + exit(EXIT_FAILURE); +} + /*lev_main.c*/ diff --git a/win/share/tile2bmp.c b/win/share/tile2bmp.c index ca2c7dd73..6722bbc26 100644 --- a/win/share/tile2bmp.c +++ b/win/share/tile2bmp.c @@ -360,3 +360,10 @@ pixel (*pixels)[TILE_X]; } } } + +/* nhassert_failed is called when an nhassert's condition is false */ +void nhassert_failed(const char * exp, const char * file, int line) +{ + Fprintf(stderr, "NHASSERT(%s) in '%s' at line %d\n", exp, file, line); + exit(EXIT_FAILURE); +} diff --git a/win/win32/winhack.c b/win/win32/winhack.c index 9dcbb4e4b..ef48d6b63 100644 --- a/win/win32/winhack.c +++ b/win/win32/winhack.c @@ -97,6 +97,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, win10_init(); + icontext_init(); sys_early_init(); /* init applicatio structure */ From f0f0dea993054bc00169959f07f06c13b68b37cd Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 21:40:15 -0800 Subject: [PATCH 12/32] check. --- include/config.h | 9 -- include/decl.h | 10 +- include/extern.h | 3 - src/apply.c | 17 +-- src/artifact.c | 35 +++--- src/botl.c | 5 +- src/decl.c | 296 ++--------------------------------------------- src/dog.c | 2 +- src/restore.c | 2 +- 9 files changed, 44 insertions(+), 335 deletions(-) diff --git a/include/config.h b/include/config.h index d4f67e1ac..e0bcdb3d8 100644 --- a/include/config.h +++ b/include/config.h @@ -552,15 +552,6 @@ typedef unsigned char uchar; #endif -/* PLAYAGAIN support for allowing the game shell to stay open after the player - * saves or dies. This requires that the game engine can be re-entered to - * start another game. - * - * This support does not include supporting playing another game when - * a panic has occured due to undetermined state the engine is left in after a - * panic */ -/* #define PLAYAGAIN */ - /* End of Section 4 */ #ifdef TTY_TILES_ESCCODES diff --git a/include/decl.h b/include/decl.h index 2cc7f965c..4716f158c 100644 --- a/include/decl.h +++ b/include/decl.h @@ -192,7 +192,6 @@ E NEARDATA char dogname[]; E NEARDATA char catname[]; E NEARDATA char horsename[]; E char preferred_pet; -E int petname_used; E const char *occtxt; /* defined when occupation != NULL */ E const char *nomovemsg; @@ -444,10 +443,15 @@ struct early_opt { * things initialized during the initialization of the game engine. * It is initialized with icontext_initial_state found in decl.c */ -#define PLAYAGAIN - struct instance_context { int oldcap; /* encumberance - pickup.c */ + int petname_used; /* user preferred pet name has been used - dog.c */ + int jumping_is_magic; /* current jump result of magic - apply.c */ + int polearm_range_min; /* apply.c */ + int polearm_range_max; /* apply.c */ + int spec_dbon_applies; /* coordinate effects from spec_dbon() with + * messages in artifact_hit() - artifact.c */ + int mrank_sz; /* loaded by max_rank_sz - botl.c */ }; E struct instance_context icontext; diff --git a/include/extern.h b/include/extern.h index d3a251b83..89910b0bb 100644 --- a/include/extern.h +++ b/include/extern.h @@ -251,9 +251,6 @@ E void FDECL(destroy_drawbridge, (int, int)); /* ### decl.c ### */ E void NDECL(decl_init); -#ifdef PLAYAGAIN -E void NDECL(decl_early_init); -#endif /* ### detect.c ### */ diff --git a/src/apply.c b/src/apply.c index d202acc77..b4c0270df 100644 --- a/src/apply.c +++ b/src/apply.c @@ -1600,15 +1600,13 @@ boolean showmsg; return TRUE; } -static int jumping_is_magic; - STATIC_OVL boolean get_valid_jump_position(x,y) int x,y; { return (isok(x, y) && (ACCESSIBLE(levl[x][y].typ) || Passes_walls) - && is_valid_jump_pos(x, y, jumping_is_magic, FALSE)); + && is_valid_jump_pos(x, y, icontext.jumping_is_magic, FALSE)); } void @@ -1722,7 +1720,7 @@ int magic; /* 0=Physical, otherwise skill level */ pline("Where do you want to jump?"); cc.x = u.ux; cc.y = u.uy; - jumping_is_magic = magic; + icontext.jumping_is_magic = magic; getpos_sethilite(display_jump_positions, get_valid_jump_position); if (getpos(&cc, TRUE, "the desired position") < 0) return 0; /* user pressed ESC */ @@ -2914,16 +2912,13 @@ int min_range, max_range; return TRUE; } -static int polearm_range_min = -1; -static int polearm_range_max = -1; - STATIC_OVL boolean get_valid_polearm_position(x, y) int x, y; { return (isok(x, y) && ACCESSIBLE(levl[x][y].typ) - && distu(x, y) >= polearm_range_min - && distu(x, y) <= polearm_range_max); + && distu(x, y) >= icontext.polearm_range_min + && distu(x, y) <= icontext.polearm_range_max); } void @@ -2995,8 +2990,8 @@ struct obj *obj; else max_range = 8; /* (P_SKILL(typ) >= P_EXPERT) */ - polearm_range_min = min_range; - polearm_range_max = max_range; + icontext.polearm_range_min = min_range; + icontext.polearm_range_max = max_range; /* Prompt for a location */ pline(where_to_hit); diff --git a/src/artifact.c b/src/artifact.c index 87cbf3b61..a5d090200 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -40,9 +40,6 @@ STATIC_DCL int FDECL(count_surround_traps, (int, int)); of hit points that will fit in a 15 bit integer. */ #define FATAL_DAMAGE_MODIFIER 200 -/* coordinate effects from spec_dbon() with messages in artifact_hit() */ -STATIC_OVL int spec_dbon_applies = 0; - /* flags including which artifacts have already been created */ static boolean artiexist[1 + NROFARTIFACTS + 1]; /* and a discovery list for them (no dummy first entry here) */ @@ -847,15 +844,15 @@ int tmp; if (!weap || (weap->attk.adtyp == AD_PHYS /* check for `NO_ATTK' */ && weap->attk.damn == 0 && weap->attk.damd == 0)) - spec_dbon_applies = FALSE; + icontext.spec_dbon_applies = FALSE; else if (otmp->oartifact == ART_GRIMTOOTH) /* Grimtooth has SPFX settings to warn against elves but we want its damage bonus to apply to all targets, so bypass spec_applies() */ - spec_dbon_applies = TRUE; + icontext.spec_dbon_applies = TRUE; else - spec_dbon_applies = spec_applies(weap, mon); + icontext.spec_dbon_applies = spec_applies(weap, mon); - if (spec_dbon_applies) + if (icontext.spec_dbon_applies) return weap->attk.damd ? rnd((int) weap->attk.damd) : max(tmp, 1); return 0; } @@ -979,14 +976,14 @@ char *hittee; /* target's name: "you" or mon_nam(mdef) */ scare_dieroll /= (1 << (mb->spe / 3)); /* if target successfully resisted the artifact damage bonus, reduce overall likelihood of the assorted special effects */ - if (!spec_dbon_applies) + if (!icontext.spec_dbon_applies) dieroll += 1; /* might stun even when attempting a more severe effect, but in that case it will only happen if the other effect fails; extra damage will apply regardless; 3.4.1: sometimes might just probe even when it hasn't been enchanted */ - do_stun = (max(mb->spe, 0) < rn2(spec_dbon_applies ? 11 : 7)); + do_stun = (max(mb->spe, 0) < rn2(icontext.spec_dbon_applies ? 11 : 7)); /* the special effects also boost physical damage; increments are generally cumulative, but since the stun effect is based on a @@ -1185,12 +1182,12 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_FIRE, otmp)) { if (realizes_damage) pline_The("fiery blade %s %s%c", - !spec_dbon_applies + !icontext.spec_dbon_applies ? "hits" : (mdef->data == &mons[PM_WATER_ELEMENTAL]) ? "vaporizes part of" : "burns", - hittee, !spec_dbon_applies ? '.' : '!'); + hittee, !icontext.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_FIRE); if (!rn2(4)) @@ -1204,8 +1201,8 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_COLD, otmp)) { if (realizes_damage) pline_The("ice-cold blade %s %s%c", - !spec_dbon_applies ? "hits" : "freezes", hittee, - !spec_dbon_applies ? '.' : '!'); + !icontext.spec_dbon_applies ? "hits" : "freezes", hittee, + !icontext.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_COLD); return realizes_damage; @@ -1213,9 +1210,9 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_ELEC, otmp)) { if (realizes_damage) pline_The("massive hammer hits%s %s%c", - !spec_dbon_applies ? "" : "! Lightning strikes", - hittee, !spec_dbon_applies ? '.' : '!'); - if (spec_dbon_applies) + !icontext.spec_dbon_applies ? "" : "! Lightning strikes", + hittee, !icontext.spec_dbon_applies ? '.' : '!'); + if (icontext.spec_dbon_applies) wake_nearto(mdef->mx, mdef->my, 4 * 4); if (!rn2(5)) (void) destroy_mitem(mdef, RING_CLASS, AD_ELEC); @@ -1226,10 +1223,10 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_MAGM, otmp)) { if (realizes_damage) pline_The("imaginary widget hits%s %s%c", - !spec_dbon_applies + !icontext.spec_dbon_applies ? "" : "! A hail of magic missiles strikes", - hittee, !spec_dbon_applies ? '.' : '!'); + hittee, !icontext.spec_dbon_applies ? '.' : '!'); return realizes_damage; } @@ -1238,7 +1235,7 @@ int dieroll; /* needed for Magicbane and vorpal blades */ return Mb_hit(magr, mdef, otmp, dmgptr, dieroll, vis, hittee); } - if (!spec_dbon_applies) { + if (!icontext.spec_dbon_applies) { /* since damage bonus didn't apply, nothing more to do; no further attacks have side-effects on inventory */ return FALSE; diff --git a/src/botl.c b/src/botl.c index 4af749105..9256051b3 100644 --- a/src/botl.c +++ b/src/botl.c @@ -11,7 +11,6 @@ extern const char *hu_stat[]; /* defined in eat.c */ const char *const enc_stat[] = { "", "Burdened", "Stressed", "Strained", "Overtaxed", "Overloaded" }; -STATIC_OVL NEARDATA int mrank_sz = 0; /* loaded by max_rank_sz (from u_init) */ STATIC_DCL const char *NDECL(rank); #ifdef STATUS_HILITES STATIC_DCL void NDECL(bot_via_windowport); @@ -65,7 +64,7 @@ do_statusline1() Strcpy(nb = eos(nb), rank()); Sprintf(nb = eos(nb), " "); - i = mrank_sz + 15; + i = icontext.mrank_sz + 15; j = (int) ((nb + 2) - newbot1); /* strlen(newbot1) but less computation */ if ((i - j) > 0) Sprintf(nb = eos(nb), "%*s", i - j, " "); /* pad with spaces */ @@ -346,7 +345,7 @@ max_rank_sz() if (urole.rank[i].f && (r = strlen(urole.rank[i].f)) > maxr) maxr = r; } - mrank_sz = maxr; + icontext.mrank_sz = maxr; return; } diff --git a/src/decl.c b/src/decl.c index edfeb3a60..2a259efaa 100644 --- a/src/decl.c +++ b/src/decl.c @@ -207,7 +207,6 @@ NEARDATA char dogname[PL_PSIZ] = DUMMY; NEARDATA char catname[PL_PSIZ] = DUMMY; NEARDATA char horsename[PL_PSIZ] = DUMMY; char preferred_pet; /* '\0', 'c', 'd', 'n' (none) */ -int petname_used = 0; /* monsters that went down/up together with @ */ NEARDATA struct monst *mydogs = (struct monst *) 0; /* monsters that are moving to another dungeon level */ @@ -285,7 +284,6 @@ char *fqn_prefix_names[PREFIX_COUNT] = { }; #endif -#ifdef PLAYAGAIN const struct savefile_info default_sfinfo = { #ifdef NHSTDC 0x00000000UL @@ -308,53 +306,8 @@ const struct savefile_info default_sfinfo = { 0x00000000L, 0x00000000L #endif }; -#endif -NEARDATA struct savefile_info sfcap = { -#ifdef NHSTDC - 0x00000000UL -#else - 0x00000000L -#endif -#if defined(COMPRESS) || defined(ZLIB_COMP) - | SFI1_EXTERNALCOMP -#endif -#if defined(ZEROCOMP) - | SFI1_ZEROCOMP -#endif -#if defined(RLECOMP) - | SFI1_RLECOMP -#endif - , -#ifdef NHSTDC - 0x00000000UL, 0x00000000UL -#else - 0x00000000L, 0x00000000L -#endif -}; - -NEARDATA struct savefile_info sfrestinfo, sfsaveinfo = { -#ifdef NHSTDC - 0x00000000UL -#else - 0x00000000L -#endif -#if defined(COMPRESS) || defined(ZLIB_COMP) - | SFI1_EXTERNALCOMP -#endif -#if defined(ZEROCOMP) - | SFI1_ZEROCOMP -#endif -#if defined(RLECOMP) - | SFI1_RLECOMP -#endif - , -#ifdef NHSTDC - 0x00000000UL, 0x00000000UL -#else - 0x00000000L, 0x00000000L -#endif -}; +NEARDATA struct savefile_info sfcap, sfrestinfo, sfsaveinfo; struct plinemsg_type *plinemsg_types = (struct plinemsg_type *) 0; @@ -372,243 +325,14 @@ decl_init() return; } -#ifdef PLAYAGAIN - -static boolean s_firstStart = TRUE; - -static boolean -decl_is_block_zero(p, n) -unsigned char * p; -int n; -{ - static unsigned char zeroblock[512] = { 0 }; - unsigned char * sentinel = p + n; - while (p < sentinel) { - int c = (n < sizeof(zeroblock) ? n : sizeof(zeroblock)); - if (memcmp(p, zeroblock, c) != 0) return FALSE; - p += c; - } - return TRUE; -} - -static void -decl_zero_block(p, n) -unsigned char * p; -int n; -{ - nhassert(!s_firstStart || decl_is_block_zero(p, n)); - memset(p, 0, n); -} - -#define ZEROARRAY(x) decl_zero_block((void *) &x[0], 0, sizeof(x)) -#define ZEROARRAYN(x,n) decl_zero_block((void *) &x[0], 0, sizeof(x[0])*(n)) -#define ZERO(x) decl_zero_block((void *) &x, 0, sizeof(x)) -#define ZEROPTR(x) { nhassert(x == NULL); x = NULL; } -#define ZEROPTRNOCHECK(x) { x = NULL; } - -/* decl_early_init() is called when we are starting a game. On first - * start, it validates that global state is in the expected state. - * When called on subsequent starts, it initializes global state to - * expected state. - * - * In the case that of global pointers, on subsequent starts it will - * panic if it finds a non-NULL pointer with the assumption that a - * pointer has leaked. */ - -void -decl_early_init() -{ - hackpid = 0; -#if defined(UNIX) || defined(VMS) - locknum = 0; -#endif -#ifdef DEF_PAGER - catmore = 0; -#endif - - ZEROARRAY(bases); - - multi = 0; - multi_reason = NULL; - nroom = 0; - nsubroom = 0; - occtime = 0; - - x_maze_max = (COLNO - 1) & ~1; - y_maze_max = (ROWNO - 1) & ~1; - - otg_temp = 0; - - ZERO(dungeon_topology); - ZERO(quest_status); - - warn_obj_cnt = 0; - ZEROARRAYN(smeq, MAXNROFROOMS + 1); - doorindex = 0; - save_cm = NULL; - - ZERO(killer); - done_money = 0; - nomovemsg = NULL; - ZEROARRAY(plname); - ZEROARRAY(pl_character); - pl_race = '\0'; - - ZEROARRAY(pl_fruit); - ffruit = NULL; - - ZEROARRAY(tune); - - occtxt = NULL; - - yn_number = 0; - -#if defined(MICRO) || defined(WIN32) - ZEROARRAYN(hackdir, PATHLEN); -#ifdef MICRO - ZEROARRAYN(levels, PATHLEN); -#endif /* MICRO */ -#endif /* MICRO || WIN32 */ - -#ifdef MFLOPPY - ZEROARRAYN(permbones, PATHLEN); - ramdisk = FALSE; - saveprompt = TRUE; -#endif - - ZEROARRAY(level_info); - - ZERO(program_state); - - tbx = 0; - tby = 0; - - ZERO(m_shot); - - ZEROARRAYN(dungeons, MAXDUNGEON); - ZEROPTR(sp_levchn); - ZERO(upstair); - ZERO(dnstair); - ZERO(upladder); - ZERO(dnladder); - ZERO(sstairs); - ZERO(updest); - ZERO(dndest); - ZERO(inv_pos); - - defer_see_monsters = FALSE; - in_mklev = FALSE; - stoned = FALSE; - unweapon = FALSE; - mrg_to_wielded = FALSE; - - in_steed_dismounting = FALSE; - - ZERO(bhitpos); - ZEROARRAY(doors); - - ZEROARRAY(rooms); - subrooms = &rooms[MAXNROFROOMS + 1]; - upstairs_room = NULL; - dnstairs_room = NULL; - sstairs_room = NULL; - - ZERO(level); - ZEROPTR(ftrap); - ZERO(youmonst); - ZERO(context); - ZERO(flags); -#ifdef SYSFLAGS - ZERO(sysflags); -#endif - ZERO(iflags); - ZERO(u); - ZERO(ubirthday); - ZERO(urealtime); - - ZEROARRAY(lastseentyp); - - ZEROPTR(invent); - ZEROPTRNOCHECK(uwep); - ZEROPTRNOCHECK(uarm); - ZEROPTRNOCHECK(uswapwep); - ZEROPTRNOCHECK(uquiver); - ZEROPTRNOCHECK(uarmu); - ZEROPTRNOCHECK(uskin); - ZEROPTRNOCHECK(uarmc); - ZEROPTRNOCHECK(uarmh); - ZEROPTRNOCHECK(uarms); - ZEROPTRNOCHECK(uarmg); - ZEROPTRNOCHECK(uarmf); - ZEROPTRNOCHECK(uamul); - ZEROPTRNOCHECK(uright); - ZEROPTRNOCHECK(uleft); - ZEROPTRNOCHECK(ublindf); - ZEROPTRNOCHECK(uchain); - ZEROPTRNOCHECK(uball); - - ZEROPTR(current_wand); - ZEROPTR(thrownobj); - ZEROPTR(kickedobj); - - ZEROARRAYN(spl_book, MAXSPELL + 1); - - moves = 1; - monstermoves = 1; - - wailmsg = 0L; - - ZEROPTR(migrating_objs); - ZEROPTR(billobjs); - - ZERO(zeroobj); - ZERO(zeromonst); - ZERO(zeroany); - - ZEROARRAYN(dogname, PL_PSIZ); - ZEROARRAYN(catname, PL_PSIZ); - ZEROARRAYN(horsename, PL_PSIZ); - ZERO(preferred_pet); - ZERO(petname_used); - ZEROPTR(mydogs); - ZEROPTR(migrating_mons); - - ZEROARRAY(mvitals); - - ZEROPTR(menu_colorings); - - vision_full_recalc = 0; - viz_array = NULL; - - WIN_MESSAGE = WIN_ERR; -#ifndef STATUS_VIA_WINDOWPORT - WIN_STATUS = WIN_ERR; -#endif - WIN_MAP = WIN_ERR; - WIN_INVEN = WIN_ERR; - - ZEROARRAYN(toplines, TBUFSZ); - ZERO(tc_gbl_data); - ZEROARRAYN(fqn_prefix, PREFIX_COUNT); - - sfcap = default_sfinfo; - sfrestinfo = default_sfinfo; - sfsaveinfo = default_sfinfo; - - ZEROPTR(plinemsg_types); - -#ifdef PANICTRACE - ARGV0 = NULL; -#endif - - nhUse_dummy = 0; - - s_firstStart = FALSE; -} -#endif - const struct instance_context icontext_initial_state = { - 0, /* oldcap - last encumberance in pickup.c */ + 0, /* oldcap - last encumberance in pickup.c */ + 0, /* petname_used - dog.c */ + 0, /* jumping_is_magic - apply.c */ + -1, /* polearm_range_min - apply.c */ + -1, /* polearm_range_max - apply.c */ + 0, /* spec_dbon_applies - artifact.c */ + 0, /* mrank_sz - botl.c */ }; struct instance_context icontext; @@ -618,7 +342,9 @@ icontext_init() { icontext = icontext_initial_state; - decl_early_init(); + sfcap = default_sfinfo; + sfrestinfo = default_sfinfo; + sfsaveinfo = default_sfinfo; }; /*decl.c*/ diff --git a/src/dog.c b/src/dog.c index 7c1b62e8b..79e6b781f 100644 --- a/src/dog.c +++ b/src/dog.c @@ -197,7 +197,7 @@ makedog() put_saddle_on_mon(otmp, mtmp); } - if (!petname_used++ && *petname) + if (!icontext.petname_used++ && *petname) mtmp = christen_monst(mtmp, petname); initedog(mtmp); diff --git a/src/restore.c b/src/restore.c index f26379967..557cb3219 100644 --- a/src/restore.c +++ b/src/restore.c @@ -899,7 +899,7 @@ register int fd; #ifdef MFLOPPY gameDiskPrompt(); #endif - max_rank_sz(); /* to recompute mrank_sz (botl.c) */ + max_rank_sz(); /* to recompute icontext.mrank_sz (botl.c) */ /* take care of iron ball & chain */ for (otmp = fobj; otmp; otmp = otmp->nobj) if (otmp->owornmask) From f8ab311e3d84e24d8f6c10972c2f37e0647fa7ca Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 21:55:03 -0800 Subject: [PATCH 13/32] Fixed nocreate. --- include/decl.h | 4 ++++ src/decl.c | 4 ++++ src/u_init.c | 21 ++++++++------------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/include/decl.h b/include/decl.h index 4716f158c..49bd34d2e 100644 --- a/include/decl.h +++ b/include/decl.h @@ -452,6 +452,10 @@ struct instance_context { int spec_dbon_applies; /* coordinate effects from spec_dbon() with * messages in artifact_hit() - artifact.c */ int mrank_sz; /* loaded by max_rank_sz - botl.c */ + short nocreate; /* ini_inv() - u_init.c = STRANGE_OBJECT */ + short nocreate2; /* ini_inv() - u_init.c = STRANGE_OBJECT */ + short nocreate3; /* ini_inv() - u_init.c = STRANGE_OBJECT */ + short nocreate4; /* ini_inv() - u_init.c = STRANGE_OBJECT */ }; E struct instance_context icontext; diff --git a/src/decl.c b/src/decl.c index 2a259efaa..2f52fc41f 100644 --- a/src/decl.c +++ b/src/decl.c @@ -333,6 +333,10 @@ const struct instance_context icontext_initial_state = { -1, /* polearm_range_max - apply.c */ 0, /* spec_dbon_applies - artifact.c */ 0, /* mrank_sz - botl.c */ + STRANGE_OBJECT, /* nocreate - ini_inv() in u_init.c */ + STRANGE_OBJECT, /* nocreate2 - ini_inv() in u_init.c */ + STRANGE_OBJECT, /* nocreate3 - ini_inv() in u_init.c */ + STRANGE_OBJECT, /* nocreate4 - ini_inv() in u_init.c */ }; struct instance_context icontext; diff --git a/src/u_init.c b/src/u_init.c index 71da5a84a..9d38214ed 100644 --- a/src/u_init.c +++ b/src/u_init.c @@ -974,11 +974,6 @@ register struct trobj *trop; struct obj *obj; int otyp, i; - NEARDATA short nocreate = STRANGE_OBJECT; - NEARDATA short nocreate2 = STRANGE_OBJECT; - NEARDATA short nocreate3 = STRANGE_OBJECT; - NEARDATA short nocreate4 = STRANGE_OBJECT; - while (trop->trclass) { otyp = (int) trop->trotyp; if (otyp != UNDEF_TYP) { @@ -996,9 +991,9 @@ register struct trobj *trop; */ obj = mkobj(trop->trclass, FALSE); otyp = obj->otyp; - while (otyp == WAN_WISHING || otyp == nocreate - || otyp == nocreate2 || otyp == nocreate3 - || otyp == nocreate4 || otyp == RIN_LEVITATION + while (otyp == WAN_WISHING || otyp == icontext.nocreate + || otyp == icontext.nocreate2 || otyp == icontext.nocreate3 + || otyp == icontext.nocreate4 || otyp == RIN_LEVITATION /* 'useless' items */ || otyp == POT_HALLUCINATION || otyp == POT_ACID @@ -1040,16 +1035,16 @@ register struct trobj *trop; case WAN_POLYMORPH: case RIN_POLYMORPH: case POT_POLYMORPH: - nocreate = RIN_POLYMORPH_CONTROL; + icontext.nocreate = RIN_POLYMORPH_CONTROL; break; case RIN_POLYMORPH_CONTROL: - nocreate = RIN_POLYMORPH; - nocreate2 = SPE_POLYMORPH; - nocreate3 = POT_POLYMORPH; + icontext.nocreate = RIN_POLYMORPH; + icontext.nocreate2 = SPE_POLYMORPH; + icontext.nocreate3 = POT_POLYMORPH; } /* Don't have 2 of the same ring or spellbook */ if (obj->oclass == RING_CLASS || obj->oclass == SPBOOK_CLASS) - nocreate4 = otyp; + icontext.nocreate4 = otyp; } if (urace.malenum != PM_HUMAN) { From f305d1ab6c1fe0bdabe8c106f701311942a1074c Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 21:40:15 -0800 Subject: [PATCH 14/32] Fixed nocreate. --- include/config.h | 9 -- include/decl.h | 14 ++- include/extern.h | 3 - src/apply.c | 17 +-- src/artifact.c | 35 +++--- src/botl.c | 5 +- src/decl.c | 300 +++-------------------------------------------- src/dog.c | 2 +- src/restore.c | 2 +- src/u_init.c | 21 ++-- 10 files changed, 60 insertions(+), 348 deletions(-) diff --git a/include/config.h b/include/config.h index d4f67e1ac..e0bcdb3d8 100644 --- a/include/config.h +++ b/include/config.h @@ -552,15 +552,6 @@ typedef unsigned char uchar; #endif -/* PLAYAGAIN support for allowing the game shell to stay open after the player - * saves or dies. This requires that the game engine can be re-entered to - * start another game. - * - * This support does not include supporting playing another game when - * a panic has occured due to undetermined state the engine is left in after a - * panic */ -/* #define PLAYAGAIN */ - /* End of Section 4 */ #ifdef TTY_TILES_ESCCODES diff --git a/include/decl.h b/include/decl.h index 2cc7f965c..49bd34d2e 100644 --- a/include/decl.h +++ b/include/decl.h @@ -192,7 +192,6 @@ E NEARDATA char dogname[]; E NEARDATA char catname[]; E NEARDATA char horsename[]; E char preferred_pet; -E int petname_used; E const char *occtxt; /* defined when occupation != NULL */ E const char *nomovemsg; @@ -444,10 +443,19 @@ struct early_opt { * things initialized during the initialization of the game engine. * It is initialized with icontext_initial_state found in decl.c */ -#define PLAYAGAIN - struct instance_context { int oldcap; /* encumberance - pickup.c */ + int petname_used; /* user preferred pet name has been used - dog.c */ + int jumping_is_magic; /* current jump result of magic - apply.c */ + int polearm_range_min; /* apply.c */ + int polearm_range_max; /* apply.c */ + int spec_dbon_applies; /* coordinate effects from spec_dbon() with + * messages in artifact_hit() - artifact.c */ + int mrank_sz; /* loaded by max_rank_sz - botl.c */ + short nocreate; /* ini_inv() - u_init.c = STRANGE_OBJECT */ + short nocreate2; /* ini_inv() - u_init.c = STRANGE_OBJECT */ + short nocreate3; /* ini_inv() - u_init.c = STRANGE_OBJECT */ + short nocreate4; /* ini_inv() - u_init.c = STRANGE_OBJECT */ }; E struct instance_context icontext; diff --git a/include/extern.h b/include/extern.h index d3a251b83..89910b0bb 100644 --- a/include/extern.h +++ b/include/extern.h @@ -251,9 +251,6 @@ E void FDECL(destroy_drawbridge, (int, int)); /* ### decl.c ### */ E void NDECL(decl_init); -#ifdef PLAYAGAIN -E void NDECL(decl_early_init); -#endif /* ### detect.c ### */ diff --git a/src/apply.c b/src/apply.c index d202acc77..b4c0270df 100644 --- a/src/apply.c +++ b/src/apply.c @@ -1600,15 +1600,13 @@ boolean showmsg; return TRUE; } -static int jumping_is_magic; - STATIC_OVL boolean get_valid_jump_position(x,y) int x,y; { return (isok(x, y) && (ACCESSIBLE(levl[x][y].typ) || Passes_walls) - && is_valid_jump_pos(x, y, jumping_is_magic, FALSE)); + && is_valid_jump_pos(x, y, icontext.jumping_is_magic, FALSE)); } void @@ -1722,7 +1720,7 @@ int magic; /* 0=Physical, otherwise skill level */ pline("Where do you want to jump?"); cc.x = u.ux; cc.y = u.uy; - jumping_is_magic = magic; + icontext.jumping_is_magic = magic; getpos_sethilite(display_jump_positions, get_valid_jump_position); if (getpos(&cc, TRUE, "the desired position") < 0) return 0; /* user pressed ESC */ @@ -2914,16 +2912,13 @@ int min_range, max_range; return TRUE; } -static int polearm_range_min = -1; -static int polearm_range_max = -1; - STATIC_OVL boolean get_valid_polearm_position(x, y) int x, y; { return (isok(x, y) && ACCESSIBLE(levl[x][y].typ) - && distu(x, y) >= polearm_range_min - && distu(x, y) <= polearm_range_max); + && distu(x, y) >= icontext.polearm_range_min + && distu(x, y) <= icontext.polearm_range_max); } void @@ -2995,8 +2990,8 @@ struct obj *obj; else max_range = 8; /* (P_SKILL(typ) >= P_EXPERT) */ - polearm_range_min = min_range; - polearm_range_max = max_range; + icontext.polearm_range_min = min_range; + icontext.polearm_range_max = max_range; /* Prompt for a location */ pline(where_to_hit); diff --git a/src/artifact.c b/src/artifact.c index 87cbf3b61..a5d090200 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -40,9 +40,6 @@ STATIC_DCL int FDECL(count_surround_traps, (int, int)); of hit points that will fit in a 15 bit integer. */ #define FATAL_DAMAGE_MODIFIER 200 -/* coordinate effects from spec_dbon() with messages in artifact_hit() */ -STATIC_OVL int spec_dbon_applies = 0; - /* flags including which artifacts have already been created */ static boolean artiexist[1 + NROFARTIFACTS + 1]; /* and a discovery list for them (no dummy first entry here) */ @@ -847,15 +844,15 @@ int tmp; if (!weap || (weap->attk.adtyp == AD_PHYS /* check for `NO_ATTK' */ && weap->attk.damn == 0 && weap->attk.damd == 0)) - spec_dbon_applies = FALSE; + icontext.spec_dbon_applies = FALSE; else if (otmp->oartifact == ART_GRIMTOOTH) /* Grimtooth has SPFX settings to warn against elves but we want its damage bonus to apply to all targets, so bypass spec_applies() */ - spec_dbon_applies = TRUE; + icontext.spec_dbon_applies = TRUE; else - spec_dbon_applies = spec_applies(weap, mon); + icontext.spec_dbon_applies = spec_applies(weap, mon); - if (spec_dbon_applies) + if (icontext.spec_dbon_applies) return weap->attk.damd ? rnd((int) weap->attk.damd) : max(tmp, 1); return 0; } @@ -979,14 +976,14 @@ char *hittee; /* target's name: "you" or mon_nam(mdef) */ scare_dieroll /= (1 << (mb->spe / 3)); /* if target successfully resisted the artifact damage bonus, reduce overall likelihood of the assorted special effects */ - if (!spec_dbon_applies) + if (!icontext.spec_dbon_applies) dieroll += 1; /* might stun even when attempting a more severe effect, but in that case it will only happen if the other effect fails; extra damage will apply regardless; 3.4.1: sometimes might just probe even when it hasn't been enchanted */ - do_stun = (max(mb->spe, 0) < rn2(spec_dbon_applies ? 11 : 7)); + do_stun = (max(mb->spe, 0) < rn2(icontext.spec_dbon_applies ? 11 : 7)); /* the special effects also boost physical damage; increments are generally cumulative, but since the stun effect is based on a @@ -1185,12 +1182,12 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_FIRE, otmp)) { if (realizes_damage) pline_The("fiery blade %s %s%c", - !spec_dbon_applies + !icontext.spec_dbon_applies ? "hits" : (mdef->data == &mons[PM_WATER_ELEMENTAL]) ? "vaporizes part of" : "burns", - hittee, !spec_dbon_applies ? '.' : '!'); + hittee, !icontext.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_FIRE); if (!rn2(4)) @@ -1204,8 +1201,8 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_COLD, otmp)) { if (realizes_damage) pline_The("ice-cold blade %s %s%c", - !spec_dbon_applies ? "hits" : "freezes", hittee, - !spec_dbon_applies ? '.' : '!'); + !icontext.spec_dbon_applies ? "hits" : "freezes", hittee, + !icontext.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_COLD); return realizes_damage; @@ -1213,9 +1210,9 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_ELEC, otmp)) { if (realizes_damage) pline_The("massive hammer hits%s %s%c", - !spec_dbon_applies ? "" : "! Lightning strikes", - hittee, !spec_dbon_applies ? '.' : '!'); - if (spec_dbon_applies) + !icontext.spec_dbon_applies ? "" : "! Lightning strikes", + hittee, !icontext.spec_dbon_applies ? '.' : '!'); + if (icontext.spec_dbon_applies) wake_nearto(mdef->mx, mdef->my, 4 * 4); if (!rn2(5)) (void) destroy_mitem(mdef, RING_CLASS, AD_ELEC); @@ -1226,10 +1223,10 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_MAGM, otmp)) { if (realizes_damage) pline_The("imaginary widget hits%s %s%c", - !spec_dbon_applies + !icontext.spec_dbon_applies ? "" : "! A hail of magic missiles strikes", - hittee, !spec_dbon_applies ? '.' : '!'); + hittee, !icontext.spec_dbon_applies ? '.' : '!'); return realizes_damage; } @@ -1238,7 +1235,7 @@ int dieroll; /* needed for Magicbane and vorpal blades */ return Mb_hit(magr, mdef, otmp, dmgptr, dieroll, vis, hittee); } - if (!spec_dbon_applies) { + if (!icontext.spec_dbon_applies) { /* since damage bonus didn't apply, nothing more to do; no further attacks have side-effects on inventory */ return FALSE; diff --git a/src/botl.c b/src/botl.c index 4af749105..9256051b3 100644 --- a/src/botl.c +++ b/src/botl.c @@ -11,7 +11,6 @@ extern const char *hu_stat[]; /* defined in eat.c */ const char *const enc_stat[] = { "", "Burdened", "Stressed", "Strained", "Overtaxed", "Overloaded" }; -STATIC_OVL NEARDATA int mrank_sz = 0; /* loaded by max_rank_sz (from u_init) */ STATIC_DCL const char *NDECL(rank); #ifdef STATUS_HILITES STATIC_DCL void NDECL(bot_via_windowport); @@ -65,7 +64,7 @@ do_statusline1() Strcpy(nb = eos(nb), rank()); Sprintf(nb = eos(nb), " "); - i = mrank_sz + 15; + i = icontext.mrank_sz + 15; j = (int) ((nb + 2) - newbot1); /* strlen(newbot1) but less computation */ if ((i - j) > 0) Sprintf(nb = eos(nb), "%*s", i - j, " "); /* pad with spaces */ @@ -346,7 +345,7 @@ max_rank_sz() if (urole.rank[i].f && (r = strlen(urole.rank[i].f)) > maxr) maxr = r; } - mrank_sz = maxr; + icontext.mrank_sz = maxr; return; } diff --git a/src/decl.c b/src/decl.c index edfeb3a60..2f52fc41f 100644 --- a/src/decl.c +++ b/src/decl.c @@ -207,7 +207,6 @@ NEARDATA char dogname[PL_PSIZ] = DUMMY; NEARDATA char catname[PL_PSIZ] = DUMMY; NEARDATA char horsename[PL_PSIZ] = DUMMY; char preferred_pet; /* '\0', 'c', 'd', 'n' (none) */ -int petname_used = 0; /* monsters that went down/up together with @ */ NEARDATA struct monst *mydogs = (struct monst *) 0; /* monsters that are moving to another dungeon level */ @@ -285,7 +284,6 @@ char *fqn_prefix_names[PREFIX_COUNT] = { }; #endif -#ifdef PLAYAGAIN const struct savefile_info default_sfinfo = { #ifdef NHSTDC 0x00000000UL @@ -308,53 +306,8 @@ const struct savefile_info default_sfinfo = { 0x00000000L, 0x00000000L #endif }; -#endif -NEARDATA struct savefile_info sfcap = { -#ifdef NHSTDC - 0x00000000UL -#else - 0x00000000L -#endif -#if defined(COMPRESS) || defined(ZLIB_COMP) - | SFI1_EXTERNALCOMP -#endif -#if defined(ZEROCOMP) - | SFI1_ZEROCOMP -#endif -#if defined(RLECOMP) - | SFI1_RLECOMP -#endif - , -#ifdef NHSTDC - 0x00000000UL, 0x00000000UL -#else - 0x00000000L, 0x00000000L -#endif -}; - -NEARDATA struct savefile_info sfrestinfo, sfsaveinfo = { -#ifdef NHSTDC - 0x00000000UL -#else - 0x00000000L -#endif -#if defined(COMPRESS) || defined(ZLIB_COMP) - | SFI1_EXTERNALCOMP -#endif -#if defined(ZEROCOMP) - | SFI1_ZEROCOMP -#endif -#if defined(RLECOMP) - | SFI1_RLECOMP -#endif - , -#ifdef NHSTDC - 0x00000000UL, 0x00000000UL -#else - 0x00000000L, 0x00000000L -#endif -}; +NEARDATA struct savefile_info sfcap, sfrestinfo, sfsaveinfo; struct plinemsg_type *plinemsg_types = (struct plinemsg_type *) 0; @@ -372,243 +325,18 @@ decl_init() return; } -#ifdef PLAYAGAIN - -static boolean s_firstStart = TRUE; - -static boolean -decl_is_block_zero(p, n) -unsigned char * p; -int n; -{ - static unsigned char zeroblock[512] = { 0 }; - unsigned char * sentinel = p + n; - while (p < sentinel) { - int c = (n < sizeof(zeroblock) ? n : sizeof(zeroblock)); - if (memcmp(p, zeroblock, c) != 0) return FALSE; - p += c; - } - return TRUE; -} - -static void -decl_zero_block(p, n) -unsigned char * p; -int n; -{ - nhassert(!s_firstStart || decl_is_block_zero(p, n)); - memset(p, 0, n); -} - -#define ZEROARRAY(x) decl_zero_block((void *) &x[0], 0, sizeof(x)) -#define ZEROARRAYN(x,n) decl_zero_block((void *) &x[0], 0, sizeof(x[0])*(n)) -#define ZERO(x) decl_zero_block((void *) &x, 0, sizeof(x)) -#define ZEROPTR(x) { nhassert(x == NULL); x = NULL; } -#define ZEROPTRNOCHECK(x) { x = NULL; } - -/* decl_early_init() is called when we are starting a game. On first - * start, it validates that global state is in the expected state. - * When called on subsequent starts, it initializes global state to - * expected state. - * - * In the case that of global pointers, on subsequent starts it will - * panic if it finds a non-NULL pointer with the assumption that a - * pointer has leaked. */ - -void -decl_early_init() -{ - hackpid = 0; -#if defined(UNIX) || defined(VMS) - locknum = 0; -#endif -#ifdef DEF_PAGER - catmore = 0; -#endif - - ZEROARRAY(bases); - - multi = 0; - multi_reason = NULL; - nroom = 0; - nsubroom = 0; - occtime = 0; - - x_maze_max = (COLNO - 1) & ~1; - y_maze_max = (ROWNO - 1) & ~1; - - otg_temp = 0; - - ZERO(dungeon_topology); - ZERO(quest_status); - - warn_obj_cnt = 0; - ZEROARRAYN(smeq, MAXNROFROOMS + 1); - doorindex = 0; - save_cm = NULL; - - ZERO(killer); - done_money = 0; - nomovemsg = NULL; - ZEROARRAY(plname); - ZEROARRAY(pl_character); - pl_race = '\0'; - - ZEROARRAY(pl_fruit); - ffruit = NULL; - - ZEROARRAY(tune); - - occtxt = NULL; - - yn_number = 0; - -#if defined(MICRO) || defined(WIN32) - ZEROARRAYN(hackdir, PATHLEN); -#ifdef MICRO - ZEROARRAYN(levels, PATHLEN); -#endif /* MICRO */ -#endif /* MICRO || WIN32 */ - -#ifdef MFLOPPY - ZEROARRAYN(permbones, PATHLEN); - ramdisk = FALSE; - saveprompt = TRUE; -#endif - - ZEROARRAY(level_info); - - ZERO(program_state); - - tbx = 0; - tby = 0; - - ZERO(m_shot); - - ZEROARRAYN(dungeons, MAXDUNGEON); - ZEROPTR(sp_levchn); - ZERO(upstair); - ZERO(dnstair); - ZERO(upladder); - ZERO(dnladder); - ZERO(sstairs); - ZERO(updest); - ZERO(dndest); - ZERO(inv_pos); - - defer_see_monsters = FALSE; - in_mklev = FALSE; - stoned = FALSE; - unweapon = FALSE; - mrg_to_wielded = FALSE; - - in_steed_dismounting = FALSE; - - ZERO(bhitpos); - ZEROARRAY(doors); - - ZEROARRAY(rooms); - subrooms = &rooms[MAXNROFROOMS + 1]; - upstairs_room = NULL; - dnstairs_room = NULL; - sstairs_room = NULL; - - ZERO(level); - ZEROPTR(ftrap); - ZERO(youmonst); - ZERO(context); - ZERO(flags); -#ifdef SYSFLAGS - ZERO(sysflags); -#endif - ZERO(iflags); - ZERO(u); - ZERO(ubirthday); - ZERO(urealtime); - - ZEROARRAY(lastseentyp); - - ZEROPTR(invent); - ZEROPTRNOCHECK(uwep); - ZEROPTRNOCHECK(uarm); - ZEROPTRNOCHECK(uswapwep); - ZEROPTRNOCHECK(uquiver); - ZEROPTRNOCHECK(uarmu); - ZEROPTRNOCHECK(uskin); - ZEROPTRNOCHECK(uarmc); - ZEROPTRNOCHECK(uarmh); - ZEROPTRNOCHECK(uarms); - ZEROPTRNOCHECK(uarmg); - ZEROPTRNOCHECK(uarmf); - ZEROPTRNOCHECK(uamul); - ZEROPTRNOCHECK(uright); - ZEROPTRNOCHECK(uleft); - ZEROPTRNOCHECK(ublindf); - ZEROPTRNOCHECK(uchain); - ZEROPTRNOCHECK(uball); - - ZEROPTR(current_wand); - ZEROPTR(thrownobj); - ZEROPTR(kickedobj); - - ZEROARRAYN(spl_book, MAXSPELL + 1); - - moves = 1; - monstermoves = 1; - - wailmsg = 0L; - - ZEROPTR(migrating_objs); - ZEROPTR(billobjs); - - ZERO(zeroobj); - ZERO(zeromonst); - ZERO(zeroany); - - ZEROARRAYN(dogname, PL_PSIZ); - ZEROARRAYN(catname, PL_PSIZ); - ZEROARRAYN(horsename, PL_PSIZ); - ZERO(preferred_pet); - ZERO(petname_used); - ZEROPTR(mydogs); - ZEROPTR(migrating_mons); - - ZEROARRAY(mvitals); - - ZEROPTR(menu_colorings); - - vision_full_recalc = 0; - viz_array = NULL; - - WIN_MESSAGE = WIN_ERR; -#ifndef STATUS_VIA_WINDOWPORT - WIN_STATUS = WIN_ERR; -#endif - WIN_MAP = WIN_ERR; - WIN_INVEN = WIN_ERR; - - ZEROARRAYN(toplines, TBUFSZ); - ZERO(tc_gbl_data); - ZEROARRAYN(fqn_prefix, PREFIX_COUNT); - - sfcap = default_sfinfo; - sfrestinfo = default_sfinfo; - sfsaveinfo = default_sfinfo; - - ZEROPTR(plinemsg_types); - -#ifdef PANICTRACE - ARGV0 = NULL; -#endif - - nhUse_dummy = 0; - - s_firstStart = FALSE; -} -#endif - const struct instance_context icontext_initial_state = { - 0, /* oldcap - last encumberance in pickup.c */ + 0, /* oldcap - last encumberance in pickup.c */ + 0, /* petname_used - dog.c */ + 0, /* jumping_is_magic - apply.c */ + -1, /* polearm_range_min - apply.c */ + -1, /* polearm_range_max - apply.c */ + 0, /* spec_dbon_applies - artifact.c */ + 0, /* mrank_sz - botl.c */ + STRANGE_OBJECT, /* nocreate - ini_inv() in u_init.c */ + STRANGE_OBJECT, /* nocreate2 - ini_inv() in u_init.c */ + STRANGE_OBJECT, /* nocreate3 - ini_inv() in u_init.c */ + STRANGE_OBJECT, /* nocreate4 - ini_inv() in u_init.c */ }; struct instance_context icontext; @@ -618,7 +346,9 @@ icontext_init() { icontext = icontext_initial_state; - decl_early_init(); + sfcap = default_sfinfo; + sfrestinfo = default_sfinfo; + sfsaveinfo = default_sfinfo; }; /*decl.c*/ diff --git a/src/dog.c b/src/dog.c index 7c1b62e8b..79e6b781f 100644 --- a/src/dog.c +++ b/src/dog.c @@ -197,7 +197,7 @@ makedog() put_saddle_on_mon(otmp, mtmp); } - if (!petname_used++ && *petname) + if (!icontext.petname_used++ && *petname) mtmp = christen_monst(mtmp, petname); initedog(mtmp); diff --git a/src/restore.c b/src/restore.c index f26379967..557cb3219 100644 --- a/src/restore.c +++ b/src/restore.c @@ -899,7 +899,7 @@ register int fd; #ifdef MFLOPPY gameDiskPrompt(); #endif - max_rank_sz(); /* to recompute mrank_sz (botl.c) */ + max_rank_sz(); /* to recompute icontext.mrank_sz (botl.c) */ /* take care of iron ball & chain */ for (otmp = fobj; otmp; otmp = otmp->nobj) if (otmp->owornmask) diff --git a/src/u_init.c b/src/u_init.c index 71da5a84a..9d38214ed 100644 --- a/src/u_init.c +++ b/src/u_init.c @@ -974,11 +974,6 @@ register struct trobj *trop; struct obj *obj; int otyp, i; - NEARDATA short nocreate = STRANGE_OBJECT; - NEARDATA short nocreate2 = STRANGE_OBJECT; - NEARDATA short nocreate3 = STRANGE_OBJECT; - NEARDATA short nocreate4 = STRANGE_OBJECT; - while (trop->trclass) { otyp = (int) trop->trotyp; if (otyp != UNDEF_TYP) { @@ -996,9 +991,9 @@ register struct trobj *trop; */ obj = mkobj(trop->trclass, FALSE); otyp = obj->otyp; - while (otyp == WAN_WISHING || otyp == nocreate - || otyp == nocreate2 || otyp == nocreate3 - || otyp == nocreate4 || otyp == RIN_LEVITATION + while (otyp == WAN_WISHING || otyp == icontext.nocreate + || otyp == icontext.nocreate2 || otyp == icontext.nocreate3 + || otyp == icontext.nocreate4 || otyp == RIN_LEVITATION /* 'useless' items */ || otyp == POT_HALLUCINATION || otyp == POT_ACID @@ -1040,16 +1035,16 @@ register struct trobj *trop; case WAN_POLYMORPH: case RIN_POLYMORPH: case POT_POLYMORPH: - nocreate = RIN_POLYMORPH_CONTROL; + icontext.nocreate = RIN_POLYMORPH_CONTROL; break; case RIN_POLYMORPH_CONTROL: - nocreate = RIN_POLYMORPH; - nocreate2 = SPE_POLYMORPH; - nocreate3 = POT_POLYMORPH; + icontext.nocreate = RIN_POLYMORPH; + icontext.nocreate2 = SPE_POLYMORPH; + icontext.nocreate3 = POT_POLYMORPH; } /* Don't have 2 of the same ring or spellbook */ if (obj->oclass == RING_CLASS || obj->oclass == SPBOOK_CLASS) - nocreate4 = otyp; + icontext.nocreate4 = otyp; } if (urace.malenum != PM_HUMAN) { From 8111624d0e7179ec783c96ef299eeee82b1fa0d9 Mon Sep 17 00:00:00 2001 From: Bart House Date: Thu, 22 Nov 2018 10:15:43 -0800 Subject: [PATCH 15/32] Initial check in of icontext work. --- include/config.h | 1 - include/decl.h | 26 ++++++++++++++++++++++ src/apply.c | 17 ++++++--------- src/artifact.c | 35 ++++++++++++++---------------- src/botl.c | 5 ++--- src/decl.c | 51 ++++++++++++++++++++++++-------------------- src/dog.c | 3 +-- src/pickup.c | 7 +++--- src/restore.c | 2 +- src/u_init.c | 22 ++++++++----------- sys/share/pcmain.c | 2 ++ sys/winnt/stubs.c | 1 + util/lev_main.c | 7 ++++++ win/share/tile2bmp.c | 7 ++++++ win/win32/winhack.c | 1 + 15 files changed, 110 insertions(+), 77 deletions(-) diff --git a/include/config.h b/include/config.h index 4f242679a..e0bcdb3d8 100644 --- a/include/config.h +++ b/include/config.h @@ -552,7 +552,6 @@ typedef unsigned char uchar; #endif - /* End of Section 4 */ #ifdef TTY_TILES_ESCCODES diff --git a/include/decl.h b/include/decl.h index 992e86ad5..49bd34d2e 100644 --- a/include/decl.h +++ b/include/decl.h @@ -192,6 +192,7 @@ E NEARDATA char dogname[]; E NEARDATA char catname[]; E NEARDATA char horsename[]; E char preferred_pet; + E const char *occtxt; /* defined when occupation != NULL */ E const char *nomovemsg; E char lock[]; @@ -437,6 +438,31 @@ struct early_opt { boolean valallowed; }; +/* instance_context holds per game instance data that does not need to be + * persisted upon game exit. This game instance data is one of the first + * things initialized during the initialization of the game engine. + * It is initialized with icontext_initial_state found in decl.c */ + +struct instance_context { + int oldcap; /* encumberance - pickup.c */ + int petname_used; /* user preferred pet name has been used - dog.c */ + int jumping_is_magic; /* current jump result of magic - apply.c */ + int polearm_range_min; /* apply.c */ + int polearm_range_max; /* apply.c */ + int spec_dbon_applies; /* coordinate effects from spec_dbon() with + * messages in artifact_hit() - artifact.c */ + int mrank_sz; /* loaded by max_rank_sz - botl.c */ + short nocreate; /* ini_inv() - u_init.c = STRANGE_OBJECT */ + short nocreate2; /* ini_inv() - u_init.c = STRANGE_OBJECT */ + short nocreate3; /* ini_inv() - u_init.c = STRANGE_OBJECT */ + short nocreate4; /* ini_inv() - u_init.c = STRANGE_OBJECT */ +}; + +E struct instance_context icontext; + +E void icontext_init(); + + #undef E #endif /* DECL_H */ diff --git a/src/apply.c b/src/apply.c index d202acc77..b4c0270df 100644 --- a/src/apply.c +++ b/src/apply.c @@ -1600,15 +1600,13 @@ boolean showmsg; return TRUE; } -static int jumping_is_magic; - STATIC_OVL boolean get_valid_jump_position(x,y) int x,y; { return (isok(x, y) && (ACCESSIBLE(levl[x][y].typ) || Passes_walls) - && is_valid_jump_pos(x, y, jumping_is_magic, FALSE)); + && is_valid_jump_pos(x, y, icontext.jumping_is_magic, FALSE)); } void @@ -1722,7 +1720,7 @@ int magic; /* 0=Physical, otherwise skill level */ pline("Where do you want to jump?"); cc.x = u.ux; cc.y = u.uy; - jumping_is_magic = magic; + icontext.jumping_is_magic = magic; getpos_sethilite(display_jump_positions, get_valid_jump_position); if (getpos(&cc, TRUE, "the desired position") < 0) return 0; /* user pressed ESC */ @@ -2914,16 +2912,13 @@ int min_range, max_range; return TRUE; } -static int polearm_range_min = -1; -static int polearm_range_max = -1; - STATIC_OVL boolean get_valid_polearm_position(x, y) int x, y; { return (isok(x, y) && ACCESSIBLE(levl[x][y].typ) - && distu(x, y) >= polearm_range_min - && distu(x, y) <= polearm_range_max); + && distu(x, y) >= icontext.polearm_range_min + && distu(x, y) <= icontext.polearm_range_max); } void @@ -2995,8 +2990,8 @@ struct obj *obj; else max_range = 8; /* (P_SKILL(typ) >= P_EXPERT) */ - polearm_range_min = min_range; - polearm_range_max = max_range; + icontext.polearm_range_min = min_range; + icontext.polearm_range_max = max_range; /* Prompt for a location */ pline(where_to_hit); diff --git a/src/artifact.c b/src/artifact.c index 87cbf3b61..a5d090200 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -40,9 +40,6 @@ STATIC_DCL int FDECL(count_surround_traps, (int, int)); of hit points that will fit in a 15 bit integer. */ #define FATAL_DAMAGE_MODIFIER 200 -/* coordinate effects from spec_dbon() with messages in artifact_hit() */ -STATIC_OVL int spec_dbon_applies = 0; - /* flags including which artifacts have already been created */ static boolean artiexist[1 + NROFARTIFACTS + 1]; /* and a discovery list for them (no dummy first entry here) */ @@ -847,15 +844,15 @@ int tmp; if (!weap || (weap->attk.adtyp == AD_PHYS /* check for `NO_ATTK' */ && weap->attk.damn == 0 && weap->attk.damd == 0)) - spec_dbon_applies = FALSE; + icontext.spec_dbon_applies = FALSE; else if (otmp->oartifact == ART_GRIMTOOTH) /* Grimtooth has SPFX settings to warn against elves but we want its damage bonus to apply to all targets, so bypass spec_applies() */ - spec_dbon_applies = TRUE; + icontext.spec_dbon_applies = TRUE; else - spec_dbon_applies = spec_applies(weap, mon); + icontext.spec_dbon_applies = spec_applies(weap, mon); - if (spec_dbon_applies) + if (icontext.spec_dbon_applies) return weap->attk.damd ? rnd((int) weap->attk.damd) : max(tmp, 1); return 0; } @@ -979,14 +976,14 @@ char *hittee; /* target's name: "you" or mon_nam(mdef) */ scare_dieroll /= (1 << (mb->spe / 3)); /* if target successfully resisted the artifact damage bonus, reduce overall likelihood of the assorted special effects */ - if (!spec_dbon_applies) + if (!icontext.spec_dbon_applies) dieroll += 1; /* might stun even when attempting a more severe effect, but in that case it will only happen if the other effect fails; extra damage will apply regardless; 3.4.1: sometimes might just probe even when it hasn't been enchanted */ - do_stun = (max(mb->spe, 0) < rn2(spec_dbon_applies ? 11 : 7)); + do_stun = (max(mb->spe, 0) < rn2(icontext.spec_dbon_applies ? 11 : 7)); /* the special effects also boost physical damage; increments are generally cumulative, but since the stun effect is based on a @@ -1185,12 +1182,12 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_FIRE, otmp)) { if (realizes_damage) pline_The("fiery blade %s %s%c", - !spec_dbon_applies + !icontext.spec_dbon_applies ? "hits" : (mdef->data == &mons[PM_WATER_ELEMENTAL]) ? "vaporizes part of" : "burns", - hittee, !spec_dbon_applies ? '.' : '!'); + hittee, !icontext.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_FIRE); if (!rn2(4)) @@ -1204,8 +1201,8 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_COLD, otmp)) { if (realizes_damage) pline_The("ice-cold blade %s %s%c", - !spec_dbon_applies ? "hits" : "freezes", hittee, - !spec_dbon_applies ? '.' : '!'); + !icontext.spec_dbon_applies ? "hits" : "freezes", hittee, + !icontext.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_COLD); return realizes_damage; @@ -1213,9 +1210,9 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_ELEC, otmp)) { if (realizes_damage) pline_The("massive hammer hits%s %s%c", - !spec_dbon_applies ? "" : "! Lightning strikes", - hittee, !spec_dbon_applies ? '.' : '!'); - if (spec_dbon_applies) + !icontext.spec_dbon_applies ? "" : "! Lightning strikes", + hittee, !icontext.spec_dbon_applies ? '.' : '!'); + if (icontext.spec_dbon_applies) wake_nearto(mdef->mx, mdef->my, 4 * 4); if (!rn2(5)) (void) destroy_mitem(mdef, RING_CLASS, AD_ELEC); @@ -1226,10 +1223,10 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_MAGM, otmp)) { if (realizes_damage) pline_The("imaginary widget hits%s %s%c", - !spec_dbon_applies + !icontext.spec_dbon_applies ? "" : "! A hail of magic missiles strikes", - hittee, !spec_dbon_applies ? '.' : '!'); + hittee, !icontext.spec_dbon_applies ? '.' : '!'); return realizes_damage; } @@ -1238,7 +1235,7 @@ int dieroll; /* needed for Magicbane and vorpal blades */ return Mb_hit(magr, mdef, otmp, dmgptr, dieroll, vis, hittee); } - if (!spec_dbon_applies) { + if (!icontext.spec_dbon_applies) { /* since damage bonus didn't apply, nothing more to do; no further attacks have side-effects on inventory */ return FALSE; diff --git a/src/botl.c b/src/botl.c index 4af749105..9256051b3 100644 --- a/src/botl.c +++ b/src/botl.c @@ -11,7 +11,6 @@ extern const char *hu_stat[]; /* defined in eat.c */ const char *const enc_stat[] = { "", "Burdened", "Stressed", "Strained", "Overtaxed", "Overloaded" }; -STATIC_OVL NEARDATA int mrank_sz = 0; /* loaded by max_rank_sz (from u_init) */ STATIC_DCL const char *NDECL(rank); #ifdef STATUS_HILITES STATIC_DCL void NDECL(bot_via_windowport); @@ -65,7 +64,7 @@ do_statusline1() Strcpy(nb = eos(nb), rank()); Sprintf(nb = eos(nb), " "); - i = mrank_sz + 15; + i = icontext.mrank_sz + 15; j = (int) ((nb + 2) - newbot1); /* strlen(newbot1) but less computation */ if ((i - j) > 0) Sprintf(nb = eos(nb), "%*s", i - j, " "); /* pad with spaces */ @@ -346,7 +345,7 @@ max_rank_sz() if (urole.rank[i].f && (r = strlen(urole.rank[i].f)) > maxr) maxr = r; } - mrank_sz = maxr; + icontext.mrank_sz = maxr; return; } diff --git a/src/decl.c b/src/decl.c index ffd9ef974..2f52fc41f 100644 --- a/src/decl.c +++ b/src/decl.c @@ -284,7 +284,7 @@ char *fqn_prefix_names[PREFIX_COUNT] = { }; #endif -NEARDATA struct savefile_info sfcap = { +const struct savefile_info default_sfinfo = { #ifdef NHSTDC 0x00000000UL #else @@ -307,28 +307,7 @@ NEARDATA struct savefile_info sfcap = { #endif }; -NEARDATA struct savefile_info sfrestinfo, sfsaveinfo = { -#ifdef NHSTDC - 0x00000000UL -#else - 0x00000000L -#endif -#if defined(COMPRESS) || defined(ZLIB_COMP) - | SFI1_EXTERNALCOMP -#endif -#if defined(ZEROCOMP) - | SFI1_ZEROCOMP -#endif -#if defined(RLECOMP) - | SFI1_RLECOMP -#endif - , -#ifdef NHSTDC - 0x00000000UL, 0x00000000UL -#else - 0x00000000L, 0x00000000L -#endif -}; +NEARDATA struct savefile_info sfcap, sfrestinfo, sfsaveinfo; struct plinemsg_type *plinemsg_types = (struct plinemsg_type *) 0; @@ -346,4 +325,30 @@ decl_init() return; } +const struct instance_context icontext_initial_state = { + 0, /* oldcap - last encumberance in pickup.c */ + 0, /* petname_used - dog.c */ + 0, /* jumping_is_magic - apply.c */ + -1, /* polearm_range_min - apply.c */ + -1, /* polearm_range_max - apply.c */ + 0, /* spec_dbon_applies - artifact.c */ + 0, /* mrank_sz - botl.c */ + STRANGE_OBJECT, /* nocreate - ini_inv() in u_init.c */ + STRANGE_OBJECT, /* nocreate2 - ini_inv() in u_init.c */ + STRANGE_OBJECT, /* nocreate3 - ini_inv() in u_init.c */ + STRANGE_OBJECT, /* nocreate4 - ini_inv() in u_init.c */ +}; + +struct instance_context icontext; + +void +icontext_init() +{ + icontext = icontext_initial_state; + + sfcap = default_sfinfo; + sfrestinfo = default_sfinfo; + sfsaveinfo = default_sfinfo; +}; + /*decl.c*/ diff --git a/src/dog.c b/src/dog.c index 0d967195e..79e6b781f 100644 --- a/src/dog.c +++ b/src/dog.c @@ -160,7 +160,6 @@ makedog() register struct obj *otmp; const char *petname; int pettype; - static int petname_used = 0; if (preferred_pet == 'n') return ((struct monst *) 0); @@ -198,7 +197,7 @@ makedog() put_saddle_on_mon(otmp, mtmp); } - if (!petname_used++ && *petname) + if (!icontext.petname_used++ && *petname) mtmp = christen_monst(mtmp, petname); initedog(mtmp); diff --git a/src/pickup.c b/src/pickup.c index d68def38d..83d5eb273 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -1556,10 +1556,9 @@ struct obj *otmp; int encumber_msg() { - static int oldcap = UNENCUMBERED; int newcap = near_capacity(); - if (oldcap < newcap) { + if (icontext.oldcap < newcap) { switch (newcap) { case 1: Your("movements are slowed slightly because of your load."); @@ -1577,7 +1576,7 @@ encumber_msg() break; } context.botl = 1; - } else if (oldcap > newcap) { + } else if (icontext.oldcap > newcap) { switch (newcap) { case 0: Your("movements are now unencumbered."); @@ -1596,7 +1595,7 @@ encumber_msg() context.botl = 1; } - oldcap = newcap; + icontext.oldcap = newcap; return newcap; } diff --git a/src/restore.c b/src/restore.c index f26379967..557cb3219 100644 --- a/src/restore.c +++ b/src/restore.c @@ -899,7 +899,7 @@ register int fd; #ifdef MFLOPPY gameDiskPrompt(); #endif - max_rank_sz(); /* to recompute mrank_sz (botl.c) */ + max_rank_sz(); /* to recompute icontext.mrank_sz (botl.c) */ /* take care of iron ball & chain */ for (otmp = fobj; otmp; otmp = otmp->nobj) if (otmp->owornmask) diff --git a/src/u_init.c b/src/u_init.c index 5b5c5113f..9d38214ed 100644 --- a/src/u_init.c +++ b/src/u_init.c @@ -974,15 +974,11 @@ register struct trobj *trop; struct obj *obj; int otyp, i; - while (trop->trclass) { + while (trop->trclass) { otyp = (int) trop->trotyp; if (otyp != UNDEF_TYP) { obj = mksobj(otyp, TRUE, FALSE); } else { /* UNDEF_TYP */ - static NEARDATA short nocreate = STRANGE_OBJECT; - static NEARDATA short nocreate2 = STRANGE_OBJECT; - static NEARDATA short nocreate3 = STRANGE_OBJECT; - static NEARDATA short nocreate4 = STRANGE_OBJECT; /* * For random objects, do not create certain overly powerful * items: wand of wishing, ring of levitation, or the @@ -995,9 +991,9 @@ register struct trobj *trop; */ obj = mkobj(trop->trclass, FALSE); otyp = obj->otyp; - while (otyp == WAN_WISHING || otyp == nocreate - || otyp == nocreate2 || otyp == nocreate3 - || otyp == nocreate4 || otyp == RIN_LEVITATION + while (otyp == WAN_WISHING || otyp == icontext.nocreate + || otyp == icontext.nocreate2 || otyp == icontext.nocreate3 + || otyp == icontext.nocreate4 || otyp == RIN_LEVITATION /* 'useless' items */ || otyp == POT_HALLUCINATION || otyp == POT_ACID @@ -1039,16 +1035,16 @@ register struct trobj *trop; case WAN_POLYMORPH: case RIN_POLYMORPH: case POT_POLYMORPH: - nocreate = RIN_POLYMORPH_CONTROL; + icontext.nocreate = RIN_POLYMORPH_CONTROL; break; case RIN_POLYMORPH_CONTROL: - nocreate = RIN_POLYMORPH; - nocreate2 = SPE_POLYMORPH; - nocreate3 = POT_POLYMORPH; + icontext.nocreate = RIN_POLYMORPH; + icontext.nocreate2 = SPE_POLYMORPH; + icontext.nocreate3 = POT_POLYMORPH; } /* Don't have 2 of the same ring or spellbook */ if (obj->oclass == RING_CLASS || obj->oclass == SPBOOK_CLASS) - nocreate4 = otyp; + icontext.nocreate4 = otyp; } if (urace.malenum != PM_HUMAN) { diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index 93674ab34..932ec971e 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -97,7 +97,9 @@ char *argv[]; nethack_enter(argc, argv); + icontext_init(); sys_early_init(); + #if defined(WIN32) && defined(TTY_GRAPHICS) Strcpy(default_window_sys, "tty"); #else diff --git a/sys/winnt/stubs.c b/sys/winnt/stubs.c index bb5ad82e4..9e16c6dce 100644 --- a/sys/winnt/stubs.c +++ b/sys/winnt/stubs.c @@ -37,6 +37,7 @@ char *argv[]; { boolean resuming; + icontext_init(); sys_early_init(); Strcpy(default_window_sys, "tty"); resuming = pcmain(argc, argv); diff --git a/util/lev_main.c b/util/lev_main.c index ecf7a444c..729549791 100644 --- a/util/lev_main.c +++ b/util/lev_main.c @@ -1637,4 +1637,11 @@ short ospeed; #endif #endif /* STRICT_REF_DEF */ +/* nhassert_failed is called when an nhassert's condition is false */ +void nhassert_failed(const char * exp, const char * file, int line) +{ + fprintf(stderr, "NHASSERT(%s) in '%s' at line %d\n", exp, file, line); + exit(EXIT_FAILURE); +} + /*lev_main.c*/ diff --git a/win/share/tile2bmp.c b/win/share/tile2bmp.c index ca2c7dd73..6722bbc26 100644 --- a/win/share/tile2bmp.c +++ b/win/share/tile2bmp.c @@ -360,3 +360,10 @@ pixel (*pixels)[TILE_X]; } } } + +/* nhassert_failed is called when an nhassert's condition is false */ +void nhassert_failed(const char * exp, const char * file, int line) +{ + Fprintf(stderr, "NHASSERT(%s) in '%s' at line %d\n", exp, file, line); + exit(EXIT_FAILURE); +} diff --git a/win/win32/winhack.c b/win/win32/winhack.c index 9dcbb4e4b..ef48d6b63 100644 --- a/win/win32/winhack.c +++ b/win/win32/winhack.c @@ -97,6 +97,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, win10_init(); + icontext_init(); sys_early_init(); /* init applicatio structure */ From 53791ab1de659897f838a5743320712360770e1c Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 04:49:51 -0800 Subject: [PATCH 16/32] Instance variable work check-point. --- include/config.h | 1 + include/decl.h | 60 +++++++++++++++++++++++++++++++-------------- src/apply.c | 12 ++++----- src/artifact.c | 32 ++++++++++++------------ src/botl.c | 4 +-- src/decl.c | 55 +++++++++++++++++++++++++++++------------ src/dog.c | 2 +- src/muse.c | 18 ++++++-------- src/pickup.c | 6 ++--- src/restore.c | 2 +- src/trap.c | 25 +++++++++---------- src/u_init.c | 16 ++++++------ src/uhitm.c | 11 +++------ src/weapon.c | 36 +++++++++++++-------------- src/zap.c | 26 ++++++++------------ sys/share/pcmain.c | 2 +- win/win32/winhack.c | 2 +- 17 files changed, 171 insertions(+), 139 deletions(-) diff --git a/include/config.h b/include/config.h index e0bcdb3d8..4f242679a 100644 --- a/include/config.h +++ b/include/config.h @@ -552,6 +552,7 @@ typedef unsigned char uchar; #endif + /* End of Section 4 */ #ifdef TTY_TILES_ESCCODES diff --git a/include/decl.h b/include/decl.h index 49bd34d2e..e0597efe3 100644 --- a/include/decl.h +++ b/include/decl.h @@ -438,29 +438,51 @@ struct early_opt { boolean valallowed; }; -/* instance_context holds per game instance data that does not need to be - * persisted upon game exit. This game instance data is one of the first - * things initialized during the initialization of the game engine. - * It is initialized with icontext_initial_state found in decl.c */ +/* instance_variables holds engine state that does not need to be + * persisted upon game exit. The initialization state is well defined + * an set in decl.c during early early engine initialization. + * + * unlike instance_flags, variables can be of any type. */ -struct instance_context { - int oldcap; /* encumberance - pickup.c */ - int petname_used; /* user preferred pet name has been used - dog.c */ - int jumping_is_magic; /* current jump result of magic - apply.c */ - int polearm_range_min; /* apply.c */ - int polearm_range_max; /* apply.c */ - int spec_dbon_applies; /* coordinate effects from spec_dbon() with - * messages in artifact_hit() - artifact.c */ - int mrank_sz; /* loaded by max_rank_sz - botl.c */ - short nocreate; /* ini_inv() - u_init.c = STRANGE_OBJECT */ - short nocreate2; /* ini_inv() - u_init.c = STRANGE_OBJECT */ - short nocreate3; /* ini_inv() - u_init.c = STRANGE_OBJECT */ - short nocreate4; /* ini_inv() - u_init.c = STRANGE_OBJECT */ +struct instance_variables { + /* apply.c */ + int jumping_is_magic; /* current jump result of magic */ + int polearm_range_min; + int polearm_range_max; + /* artifcat.c */ + int spec_dbon_applies; /* coordinate effects from spec_dbon() with + messages in artifact_hit() */ + /* botl.c */ + int mrank_sz; /* loaded by max_rank_sz */ + /* dog.c */ + int petname_used; /* user preferred pet name has been used */ + /* muse.c */ + boolean m_using; /* kludge to use mondided instead of killed */ + /* pickup.c */ + int oldcap; /* last encumberance */ + /* trap.c */ + int force_mintrap; /* mintrap() should take a flags argument, but for time + being we use this */ + /* u_init.c */ + short nocreate; + short nocreate2; + short nocreate3; + short nocreate4; + /* uhitm.c */ + boolean override_confirmation; /* Used to flag attacks caused by + Stormbringer's maliciousness. */ + /* weapon.c */ + struct obj *propellor; + /* zap.c */ + int poly_zapped; + boolean obj_zapped; + + unsigned long magic; /* validate that structure layout is preserved */ }; -E struct instance_context icontext; +E struct instance_variables iv; -E void icontext_init(); +E void instance_variable_init(); #undef E diff --git a/src/apply.c b/src/apply.c index b4c0270df..710891283 100644 --- a/src/apply.c +++ b/src/apply.c @@ -1606,7 +1606,7 @@ int x,y; { return (isok(x, y) && (ACCESSIBLE(levl[x][y].typ) || Passes_walls) - && is_valid_jump_pos(x, y, icontext.jumping_is_magic, FALSE)); + && is_valid_jump_pos(x, y, iv.jumping_is_magic, FALSE)); } void @@ -1720,7 +1720,7 @@ int magic; /* 0=Physical, otherwise skill level */ pline("Where do you want to jump?"); cc.x = u.ux; cc.y = u.uy; - icontext.jumping_is_magic = magic; + iv.jumping_is_magic = magic; getpos_sethilite(display_jump_positions, get_valid_jump_position); if (getpos(&cc, TRUE, "the desired position") < 0) return 0; /* user pressed ESC */ @@ -2917,8 +2917,8 @@ get_valid_polearm_position(x, y) int x, y; { return (isok(x, y) && ACCESSIBLE(levl[x][y].typ) - && distu(x, y) >= icontext.polearm_range_min - && distu(x, y) <= icontext.polearm_range_max); + && distu(x, y) >= iv.polearm_range_min + && distu(x, y) <= iv.polearm_range_max); } void @@ -2990,8 +2990,8 @@ struct obj *obj; else max_range = 8; /* (P_SKILL(typ) >= P_EXPERT) */ - icontext.polearm_range_min = min_range; - icontext.polearm_range_max = max_range; + iv.polearm_range_min = min_range; + iv.polearm_range_max = max_range; /* Prompt for a location */ pline(where_to_hit); diff --git a/src/artifact.c b/src/artifact.c index a5d090200..2bb4f5078 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -844,15 +844,15 @@ int tmp; if (!weap || (weap->attk.adtyp == AD_PHYS /* check for `NO_ATTK' */ && weap->attk.damn == 0 && weap->attk.damd == 0)) - icontext.spec_dbon_applies = FALSE; + iv.spec_dbon_applies = FALSE; else if (otmp->oartifact == ART_GRIMTOOTH) /* Grimtooth has SPFX settings to warn against elves but we want its damage bonus to apply to all targets, so bypass spec_applies() */ - icontext.spec_dbon_applies = TRUE; + iv.spec_dbon_applies = TRUE; else - icontext.spec_dbon_applies = spec_applies(weap, mon); + iv.spec_dbon_applies = spec_applies(weap, mon); - if (icontext.spec_dbon_applies) + if (iv.spec_dbon_applies) return weap->attk.damd ? rnd((int) weap->attk.damd) : max(tmp, 1); return 0; } @@ -976,14 +976,14 @@ char *hittee; /* target's name: "you" or mon_nam(mdef) */ scare_dieroll /= (1 << (mb->spe / 3)); /* if target successfully resisted the artifact damage bonus, reduce overall likelihood of the assorted special effects */ - if (!icontext.spec_dbon_applies) + if (!iv.spec_dbon_applies) dieroll += 1; /* might stun even when attempting a more severe effect, but in that case it will only happen if the other effect fails; extra damage will apply regardless; 3.4.1: sometimes might just probe even when it hasn't been enchanted */ - do_stun = (max(mb->spe, 0) < rn2(icontext.spec_dbon_applies ? 11 : 7)); + do_stun = (max(mb->spe, 0) < rn2(iv.spec_dbon_applies ? 11 : 7)); /* the special effects also boost physical damage; increments are generally cumulative, but since the stun effect is based on a @@ -1182,12 +1182,12 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_FIRE, otmp)) { if (realizes_damage) pline_The("fiery blade %s %s%c", - !icontext.spec_dbon_applies + !iv.spec_dbon_applies ? "hits" : (mdef->data == &mons[PM_WATER_ELEMENTAL]) ? "vaporizes part of" : "burns", - hittee, !icontext.spec_dbon_applies ? '.' : '!'); + hittee, !iv.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_FIRE); if (!rn2(4)) @@ -1201,8 +1201,8 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_COLD, otmp)) { if (realizes_damage) pline_The("ice-cold blade %s %s%c", - !icontext.spec_dbon_applies ? "hits" : "freezes", hittee, - !icontext.spec_dbon_applies ? '.' : '!'); + !iv.spec_dbon_applies ? "hits" : "freezes", hittee, + !iv.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_COLD); return realizes_damage; @@ -1210,9 +1210,9 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_ELEC, otmp)) { if (realizes_damage) pline_The("massive hammer hits%s %s%c", - !icontext.spec_dbon_applies ? "" : "! Lightning strikes", - hittee, !icontext.spec_dbon_applies ? '.' : '!'); - if (icontext.spec_dbon_applies) + !iv.spec_dbon_applies ? "" : "! Lightning strikes", + hittee, !iv.spec_dbon_applies ? '.' : '!'); + if (iv.spec_dbon_applies) wake_nearto(mdef->mx, mdef->my, 4 * 4); if (!rn2(5)) (void) destroy_mitem(mdef, RING_CLASS, AD_ELEC); @@ -1223,10 +1223,10 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_MAGM, otmp)) { if (realizes_damage) pline_The("imaginary widget hits%s %s%c", - !icontext.spec_dbon_applies + !iv.spec_dbon_applies ? "" : "! A hail of magic missiles strikes", - hittee, !icontext.spec_dbon_applies ? '.' : '!'); + hittee, !iv.spec_dbon_applies ? '.' : '!'); return realizes_damage; } @@ -1235,7 +1235,7 @@ int dieroll; /* needed for Magicbane and vorpal blades */ return Mb_hit(magr, mdef, otmp, dmgptr, dieroll, vis, hittee); } - if (!icontext.spec_dbon_applies) { + if (!iv.spec_dbon_applies) { /* since damage bonus didn't apply, nothing more to do; no further attacks have side-effects on inventory */ return FALSE; diff --git a/src/botl.c b/src/botl.c index 9256051b3..76b8b6425 100644 --- a/src/botl.c +++ b/src/botl.c @@ -64,7 +64,7 @@ do_statusline1() Strcpy(nb = eos(nb), rank()); Sprintf(nb = eos(nb), " "); - i = icontext.mrank_sz + 15; + i = iv.mrank_sz + 15; j = (int) ((nb + 2) - newbot1); /* strlen(newbot1) but less computation */ if ((i - j) > 0) Sprintf(nb = eos(nb), "%*s", i - j, " "); /* pad with spaces */ @@ -345,7 +345,7 @@ max_rank_sz() if (urole.rank[i].f && (r = strlen(urole.rank[i].f)) > maxr) maxr = r; } - icontext.mrank_sz = maxr; + iv.mrank_sz = maxr; return; } diff --git a/src/decl.c b/src/decl.c index 2f52fc41f..a6c651965 100644 --- a/src/decl.c +++ b/src/decl.c @@ -325,26 +325,51 @@ decl_init() return; } -const struct instance_context icontext_initial_state = { - 0, /* oldcap - last encumberance in pickup.c */ - 0, /* petname_used - dog.c */ - 0, /* jumping_is_magic - apply.c */ - -1, /* polearm_range_min - apply.c */ - -1, /* polearm_range_max - apply.c */ - 0, /* spec_dbon_applies - artifact.c */ - 0, /* mrank_sz - botl.c */ - STRANGE_OBJECT, /* nocreate - ini_inv() in u_init.c */ - STRANGE_OBJECT, /* nocreate2 - ini_inv() in u_init.c */ - STRANGE_OBJECT, /* nocreate3 - ini_inv() in u_init.c */ - STRANGE_OBJECT, /* nocreate4 - ini_inv() in u_init.c */ +#define UNDEFINED { 0 } /* move to hack.h if we are keeping */ +#define UNDEFINED_PTR NULL /* move to hack.h if we are keeping */ +#define IVMAGIC 0xdeadbeef + +const struct instance_variables iv_init = { + /* apply.c */ + 0, /* jumping_is_magic */ + -1, /* polearm_range_min */ + -1, /* polearm_range_max */ + /* artifact.c */ + 0, /* spec_dbon_applies */ + /* botl.c */ + 0, /* mrank_sz */ + /* dog.c */ + 0, /* petname_used */ + /* mused.c */ + FALSE, /* m_using */ + /* pickup.c */ + 0, /* oldcap */ + /* trap.c */ + 0, /* force_mintrap */ + /* u_init.c */ + STRANGE_OBJECT, /* nocreate */ + STRANGE_OBJECT, /* nocreate2 */ + STRANGE_OBJECT, /* nocreate3 */ + STRANGE_OBJECT, /* nocreate4 */ + /* uhitm.c */ + UNDEFINED, /* override_confirmation */ + /* weapon.c */ + UNDEFINED_PTR, /* propellor */ + /* zap.c */ + UNDEFINED, /* poly_zap */ + UNDEFINED, /* obj_zapped */ + + IVMAGIC /* used to validate that structure layout has been preserved */ }; -struct instance_context icontext; +struct instance_variables iv; void -icontext_init() +instance_variable_init() { - icontext = icontext_initial_state; + iv = iv_init; + + nhassert(iv_init.magic == IVMAGIC); sfcap = default_sfinfo; sfrestinfo = default_sfinfo; diff --git a/src/dog.c b/src/dog.c index 79e6b781f..a5dadde70 100644 --- a/src/dog.c +++ b/src/dog.c @@ -197,7 +197,7 @@ makedog() put_saddle_on_mon(otmp, mtmp); } - if (!icontext.petname_used++ && *petname) + if (!iv.petname_used++ && *petname) mtmp = christen_monst(mtmp, petname); initedog(mtmp); diff --git a/src/muse.c b/src/muse.c index 94b0350ad..493850a1f 100644 --- a/src/muse.c +++ b/src/muse.c @@ -8,8 +8,6 @@ #include "hack.h" -boolean m_using = FALSE; - /* Let monsters use magic items. Arbitrary assumptions: Monsters only use * scrolls when they can see, monsters know when wands have 0 charges, * monsters cannot recognize if items are cursed are not, monsters which @@ -670,12 +668,12 @@ struct monst *mtmp; zap_oseen = oseen; mzapmsg(mtmp, otmp, FALSE); otmp->spe--; - m_using = TRUE; + iv.m_using = TRUE; mbhit(mtmp, rn1(8, 6), mbhitm, bhito, otmp); /* monster learns that teleportation isn't useful here */ if (level.flags.noteleport) mtmp->mtrapseen |= (1 << (TELEP_TRAP - 1)); - m_using = FALSE; + iv.m_using = FALSE; return 2; case MUSE_SCR_TELEPORTATION: { int obj_is_cursed = otmp->cursed; @@ -1399,11 +1397,11 @@ struct monst *mtmp; otmp->spe--; if (oseen) makeknown(otmp->otyp); - m_using = TRUE; + iv.m_using = TRUE; buzz((int) (-30 - (otmp->otyp - WAN_MAGIC_MISSILE)), (otmp->otyp == WAN_MAGIC_MISSILE) ? 2 : 6, mtmp->mx, mtmp->my, sgn(mtmp->mux - mtmp->mx), sgn(mtmp->muy - mtmp->my)); - m_using = FALSE; + iv.m_using = FALSE; return (DEADMONSTER(mtmp)) ? 1 : 2; case MUSE_FIRE_HORN: case MUSE_FROST_HORN: @@ -1413,20 +1411,20 @@ struct monst *mtmp; } else You_hear("a horn being played."); otmp->spe--; - m_using = TRUE; + iv.m_using = TRUE; buzz(-30 - ((otmp->otyp == FROST_HORN) ? AD_COLD - 1 : AD_FIRE - 1), rn1(6, 6), mtmp->mx, mtmp->my, sgn(mtmp->mux - mtmp->mx), sgn(mtmp->muy - mtmp->my)); - m_using = FALSE; + iv.m_using = FALSE; return (DEADMONSTER(mtmp)) ? 1 : 2; case MUSE_WAN_TELEPORTATION: case MUSE_WAN_STRIKING: zap_oseen = oseen; mzapmsg(mtmp, otmp, FALSE); otmp->spe--; - m_using = TRUE; + iv.m_using = TRUE; mbhit(mtmp, rn1(8, 6), mbhitm, bhito, otmp); - m_using = FALSE; + iv.m_using = FALSE; return 2; case MUSE_SCR_EARTH: { /* TODO: handle steeds */ diff --git a/src/pickup.c b/src/pickup.c index 83d5eb273..42bc59256 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -1558,7 +1558,7 @@ encumber_msg() { int newcap = near_capacity(); - if (icontext.oldcap < newcap) { + if (iv.oldcap < newcap) { switch (newcap) { case 1: Your("movements are slowed slightly because of your load."); @@ -1576,7 +1576,7 @@ encumber_msg() break; } context.botl = 1; - } else if (icontext.oldcap > newcap) { + } else if (iv.oldcap > newcap) { switch (newcap) { case 0: Your("movements are now unencumbered."); @@ -1595,7 +1595,7 @@ encumber_msg() context.botl = 1; } - icontext.oldcap = newcap; + iv.oldcap = newcap; return newcap; } diff --git a/src/restore.c b/src/restore.c index 557cb3219..35992d8e6 100644 --- a/src/restore.c +++ b/src/restore.c @@ -899,7 +899,7 @@ register int fd; #ifdef MFLOPPY gameDiskPrompt(); #endif - max_rank_sz(); /* to recompute icontext.mrank_sz (botl.c) */ + max_rank_sz(); /* to recompute iv.mrank_sz (botl.c) */ /* take care of iron ball & chain */ for (otmp = fobj; otmp; otmp = otmp->nobj) if (otmp->owornmask) diff --git a/src/trap.c b/src/trap.c index 330fa73e2..c6ba82ea0 100644 --- a/src/trap.c +++ b/src/trap.c @@ -39,9 +39,6 @@ STATIC_DCL boolean FDECL(thitm, (int, struct monst *, struct obj *, int, BOOLEAN_P)); STATIC_DCL void NDECL(maybe_finish_sokoban); -/* mintrap() should take a flags argument, but for time being we use this */ -STATIC_VAR int force_mintrap = 0; - STATIC_VAR const char *const a_your[2] = { "a", "your" }; STATIC_VAR const char *const A_Your[2] = { "A", "Your" }; STATIC_VAR const char tower_of_flame[] = "tower of flame"; @@ -2163,7 +2160,7 @@ register struct monst *mtmp; } else { register int tt = trap->ttyp; boolean in_sight, tear_web, see_it, - inescapable = force_mintrap || ((tt == HOLE || tt == PIT) + inescapable = iv.force_mintrap || ((tt == HOLE || tt == PIT) && Sokoban && !trap->madeby_u); const char *fallverb; @@ -2279,7 +2276,7 @@ register struct monst *mtmp; || mptr == &mons[PM_BUGBEAR]) You_hear("the roaring of an angry bear!"); } - } else if (force_mintrap) { + } else if (iv.force_mintrap) { if (in_sight) { pline("%s evades %s bear trap!", Monnam(mtmp), a_your[trap->madeby_u]); @@ -2428,7 +2425,7 @@ register struct monst *mtmp; if (is_flyer(mptr) || is_floater(mptr) || (mtmp->wormno && count_wsegs(mtmp) > 5) || is_clinger(mptr)) { - if (force_mintrap && !Sokoban) { + if (iv.force_mintrap && !Sokoban) { /* openfallingtrap; not inescapable here */ if (in_sight) { seetrap(trap); @@ -2465,7 +2462,7 @@ register struct monst *mtmp; if (is_flyer(mptr) || is_floater(mptr) || mptr == &mons[PM_WUMPUS] || (mtmp->wormno && count_wsegs(mtmp) > 5) || mptr->msize >= MZ_HUGE) { - if (force_mintrap && !Sokoban) { + if (iv.force_mintrap && !Sokoban) { /* openfallingtrap; not inescapable here */ if (in_sight) { seetrap(trap); @@ -2554,7 +2551,7 @@ register struct monst *mtmp; a_your[trap->madeby_u]); deltrap(trap); newsym(mtmp->mx, mtmp->my); - } else if (force_mintrap && !mtmp->mtrapped) { + } else if (iv.force_mintrap && !mtmp->mtrapped) { if (in_sight) { pline("%s avoids %s spider web!", Monnam(mtmp), a_your[trap->madeby_u]); @@ -4689,9 +4686,9 @@ boolean *noticed; /* set to true iff hero notices the effect; */ /* dotrap calls mintrap when mounted hero encounters a web */ if (u.usteed) dotrapflags |= NOWEBMSG; - ++force_mintrap; + ++iv.force_mintrap; dotrap(t, dotrapflags); - --force_mintrap; + --iv.force_mintrap; result = (u.utrap != 0); } else { if (mon->mtrapped) @@ -4699,9 +4696,9 @@ boolean *noticed; /* set to true iff hero notices the effect; */ /* you notice it if you see the trap close/tremble/whatever or if you sense the monster who becomes trapped */ *noticed = cansee(t->tx, t->ty) || canspotmon(mon); - ++force_mintrap; + ++iv.force_mintrap; result = (mintrap(mon) != 0); - --force_mintrap; + --iv.force_mintrap; } return result; } @@ -4742,9 +4739,9 @@ boolean *noticed; /* set to true iff hero notices the effect; */ *noticed = cansee(t->tx, t->ty) || canspotmon(mon); /* monster will be angered; mintrap doesn't handle that */ wakeup(mon, TRUE); - ++force_mintrap; + ++iv.force_mintrap; result = (mintrap(mon) != 0); - --force_mintrap; + --iv.force_mintrap; /* mon might now be on the migrating monsters list */ } return result; diff --git a/src/u_init.c b/src/u_init.c index 9d38214ed..a522ac0ad 100644 --- a/src/u_init.c +++ b/src/u_init.c @@ -991,9 +991,9 @@ register struct trobj *trop; */ obj = mkobj(trop->trclass, FALSE); otyp = obj->otyp; - while (otyp == WAN_WISHING || otyp == icontext.nocreate - || otyp == icontext.nocreate2 || otyp == icontext.nocreate3 - || otyp == icontext.nocreate4 || otyp == RIN_LEVITATION + while (otyp == WAN_WISHING || otyp == iv.nocreate + || otyp == iv.nocreate2 || otyp == iv.nocreate3 + || otyp == iv.nocreate4 || otyp == RIN_LEVITATION /* 'useless' items */ || otyp == POT_HALLUCINATION || otyp == POT_ACID @@ -1035,16 +1035,16 @@ register struct trobj *trop; case WAN_POLYMORPH: case RIN_POLYMORPH: case POT_POLYMORPH: - icontext.nocreate = RIN_POLYMORPH_CONTROL; + iv.nocreate = RIN_POLYMORPH_CONTROL; break; case RIN_POLYMORPH_CONTROL: - icontext.nocreate = RIN_POLYMORPH; - icontext.nocreate2 = SPE_POLYMORPH; - icontext.nocreate3 = POT_POLYMORPH; + iv.nocreate = RIN_POLYMORPH; + iv.nocreate2 = SPE_POLYMORPH; + iv.nocreate3 = POT_POLYMORPH; } /* Don't have 2 of the same ring or spellbook */ if (obj->oclass == RING_CLASS || obj->oclass == SPBOOK_CLASS) - icontext.nocreate4 = otyp; + iv.nocreate4 = otyp; } if (urace.malenum != PM_HUMAN) { diff --git a/src/uhitm.c b/src/uhitm.c index a14218543..4328b00ec 100644 --- a/src/uhitm.c +++ b/src/uhitm.c @@ -26,9 +26,6 @@ STATIC_DCL boolean FDECL(shade_aware, (struct obj *)); extern boolean notonhead; /* for long worms */ -/* Used to flag attacks caused by Stormbringer's maliciousness. */ -static boolean override_confirmation = FALSE; - #define PROJECTILE(obj) ((obj) && is_ammo(obj)) void @@ -201,7 +198,7 @@ struct obj *wep; /* uwep for attack(), null for kick_monster() */ && !Stunned) { /* Intelligent chaotic weapons (Stormbringer) want blood */ if (wep && wep->oartifact == ART_STORMBRINGER) { - override_confirmation = TRUE; + iv.override_confirmation = TRUE; return FALSE; } if (canspotmon(mtmp)) { @@ -370,7 +367,7 @@ register struct monst *mtmp; /* possibly set in attack_checks; examined in known_hitum, called via hitum or hmonas below */ - override_confirmation = FALSE; + iv.override_confirmation = FALSE; /* attack_checks() used to use directly, now it uses bhitpos instead; it might map an invisible monster there */ bhitpos.x = u.ux + u.dx; @@ -451,7 +448,7 @@ int dieroll; { register boolean malive = TRUE; - if (override_confirmation) { + if (iv.override_confirmation) { /* this may need to be generalized if weapons other than Stormbringer acquire similar anti-social behavior... */ if (flags.verbose) @@ -604,7 +601,7 @@ struct attack *uattk; /* second attack for two-weapon combat; won't occur if Stormbringer overrode confirmation (assumes Stormbringer is primary weapon) or if the monster was killed or knocked to different location */ - if (u.twoweap && !override_confirmation && malive && m_at(x, y) == mon) { + if (u.twoweap && !iv.override_confirmation && malive && m_at(x, y) == mon) { tmp = find_roll_to_hit(mon, uattk->aatyp, uswapwep, &attknum, &armorpenalty); dieroll = rnd(20); diff --git a/src/weapon.c b/src/weapon.c index a590366bb..2bdd2e549 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -387,8 +387,6 @@ static NEARDATA const int pwep[] = { HALBERD, BARDICHE, SPETUM, BEC_DE_CORBIN, FAUCHARD, PARTISAN, LANCE }; -static struct obj *propellor; - /* select a ranged weapon for the monster */ struct obj * select_rwep(mtmp) @@ -401,7 +399,7 @@ register struct monst *mtmp; char mlet = mtmp->data->mlet; - propellor = &zeroobj; + iv.propellor = &zeroobj; Oselect(EGG); /* cockatrice egg */ if (mlet == S_KOP) /* pies are first choice for Kops */ Oselect(CREAM_PIE); @@ -433,7 +431,7 @@ register struct monst *mtmp; || !mon_hates_silver(mtmp))) { if ((otmp = oselect(mtmp, pwep[i])) != 0 && (otmp == mwep || !mweponly)) { - propellor = otmp; /* force the monster to wield it */ + iv.propellor = otmp; /* force the monster to wield it */ return otmp; } } @@ -454,41 +452,41 @@ register struct monst *mtmp; for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj) if (otmp->oclass == GEM_CLASS && (otmp->otyp != LOADSTONE || !otmp->cursed)) { - propellor = m_carrying(mtmp, SLING); + iv.propellor = m_carrying(mtmp, SLING); return otmp; } } /* KMH -- This belongs here so darts will work */ - propellor = &zeroobj; + iv.propellor = &zeroobj; prop = (objects[rwep[i]]).oc_skill; if (prop < 0) { switch (-prop) { case P_BOW: - propellor = (oselect(mtmp, YUMI)); - if (!propellor) - propellor = (oselect(mtmp, ELVEN_BOW)); - if (!propellor) - propellor = (oselect(mtmp, BOW)); - if (!propellor) - propellor = (oselect(mtmp, ORCISH_BOW)); + iv.propellor = (oselect(mtmp, YUMI)); + if (!iv.propellor) + iv.propellor = (oselect(mtmp, ELVEN_BOW)); + if (!iv.propellor) + iv.propellor = (oselect(mtmp, BOW)); + if (!iv.propellor) + iv.propellor = (oselect(mtmp, ORCISH_BOW)); break; case P_SLING: - propellor = (oselect(mtmp, SLING)); + iv.propellor = (oselect(mtmp, SLING)); break; case P_CROSSBOW: - propellor = (oselect(mtmp, CROSSBOW)); + iv.propellor = (oselect(mtmp, CROSSBOW)); } - if ((otmp = MON_WEP(mtmp)) && mwelded(otmp) && otmp != propellor + if ((otmp = MON_WEP(mtmp)) && mwelded(otmp) && otmp != iv.propellor && mtmp->weapon_check == NO_WEAPON_WANTED) - propellor = 0; + iv.propellor = 0; } /* propellor = obj, propellor to use * propellor = &zeroobj, doesn't need a propellor * propellor = 0, needed one and didn't have one */ - if (propellor != 0) { + if (iv.propellor != 0) { /* Note: cannot use m_carrying for loadstones, since it will * always select the first object of a type, and maybe the * monster is carrying two but only the first is unthrowable. @@ -635,7 +633,7 @@ register struct monst *mon; break; case NEED_RANGED_WEAPON: (void) select_rwep(mon); - obj = propellor; + obj = iv.propellor; break; case NEED_PICK_AXE: obj = m_carrying(mon, PICK_AXE); diff --git a/src/zap.c b/src/zap.c index d53468fe3..f2c6f47dd 100644 --- a/src/zap.c +++ b/src/zap.c @@ -12,14 +12,8 @@ */ #define MAGIC_COOKIE 1000 -static NEARDATA boolean obj_zapped; -static NEARDATA int poly_zapped; - extern boolean notonhead; /* for long worms */ -/* kludge to use mondied instead of killed */ -extern boolean m_using; - STATIC_DCL void FDECL(polyuse, (struct obj *, int, int)); STATIC_DCL void FDECL(create_polymon, (struct obj *, int)); STATIC_DCL int FDECL(stone_to_flesh_obj, (struct obj *)); @@ -1366,13 +1360,13 @@ struct obj *obj; if (obj->otyp == SCR_MAIL) return; #endif - obj_zapped = TRUE; + iv.obj_zapped = TRUE; - if (poly_zapped < 0) { + if (iv.poly_zapped < 0) { /* some may metamorphosize */ for (i = obj->quan; i; i--) if (!rn2(Luck + 45)) { - poly_zapped = objects[obj->otyp].oc_material; + iv.poly_zapped = objects[obj->otyp].oc_material; break; } } @@ -2071,7 +2065,7 @@ schar zz; learnwand(obj); } - poly_zapped = -1; + iv.poly_zapped = -1; for (otmp = level.objects[tx][ty]; otmp; otmp = next_obj) { next_obj = otmp->nexthere; /* for zap downwards, don't hit object poly'd hero is hiding under */ @@ -2081,8 +2075,8 @@ schar zz; hitanything += (*fhito)(otmp, obj); } - if (poly_zapped >= 0) - create_polymon(level.objects[tx][ty], poly_zapped); + if (iv.poly_zapped >= 0) + create_polymon(level.objects[tx][ty], iv.poly_zapped); return hitanything; } @@ -2956,16 +2950,16 @@ struct obj *obj; /* wand or spell */ void zapsetup() { - obj_zapped = FALSE; + iv.obj_zapped = FALSE; } void zapwrapup() { /* if do_osshock() set obj_zapped while polying, give a message now */ - if (obj_zapped) + if (iv.obj_zapped) You_feel("shuddering vibrations."); - obj_zapped = FALSE; + iv.obj_zapped = FALSE; } /* called for various wand and spell effects - M. Stephenson */ @@ -5041,7 +5035,7 @@ int damage, tell; if (damage) { mtmp->mhp -= damage; if (DEADMONSTER(mtmp)) { - if (m_using) + if (iv.m_using) monkilled(mtmp, "", AD_RBRE); else killed(mtmp); diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index 932ec971e..bd7ee0a74 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -97,7 +97,7 @@ char *argv[]; nethack_enter(argc, argv); - icontext_init(); + instance_variable_init(); sys_early_init(); #if defined(WIN32) && defined(TTY_GRAPHICS) diff --git a/win/win32/winhack.c b/win/win32/winhack.c index ef48d6b63..466044ec2 100644 --- a/win/win32/winhack.c +++ b/win/win32/winhack.c @@ -97,7 +97,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, win10_init(); - icontext_init(); + instance_variable_init(); sys_early_init(); /* init applicatio structure */ From a188bc5d40da6e1dcf7b5a874f66654c84196ed5 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 05:13:06 -0800 Subject: [PATCH 17/32] Instance variable work. --- include/decl.h | 5 +++++ src/decl.c | 5 +++++ src/save.c | 24 ++++++++++-------------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/decl.h b/include/decl.h index e0597efe3..e8a8dfab5 100644 --- a/include/decl.h +++ b/include/decl.h @@ -460,6 +460,11 @@ struct instance_variables { boolean m_using; /* kludge to use mondided instead of killed */ /* pickup.c */ int oldcap; /* last encumberance */ + /* save.c */ + boolean havestate; + unsigned ustuck_id; /* need to preserve during save */ + unsigned usteed_id; /* need to preserve during save */ + /* trap.c */ int force_mintrap; /* mintrap() should take a flags argument, but for time being we use this */ diff --git a/src/decl.c b/src/decl.c index a6c651965..c517edaa5 100644 --- a/src/decl.c +++ b/src/decl.c @@ -344,6 +344,10 @@ const struct instance_variables iv_init = { FALSE, /* m_using */ /* pickup.c */ 0, /* oldcap */ + /* save.c */ + TRUE, /* havestate*/ + 0, /* ustuck_id */ + 0, /* usteed_id */ /* trap.c */ 0, /* force_mintrap */ /* u_init.c */ @@ -370,6 +374,7 @@ instance_variable_init() iv = iv_init; nhassert(iv_init.magic == IVMAGIC); + nhassert(iv.havestate == TRUE); sfcap = default_sfinfo; sfrestinfo = default_sfinfo; diff --git a/src/save.c b/src/save.c index bca03c49e..de2c99b74 100644 --- a/src/save.c +++ b/src/save.c @@ -71,9 +71,6 @@ static struct save_procs { #define HUP #endif -/* need to preserve these during save to avoid accessing freed memory */ -static unsigned ustuck_id = 0, usteed_id = 0; - int dosave() { @@ -213,8 +210,8 @@ dosave0() store_version(fd); store_savefileinfo(fd); store_plname_in_file(fd); - ustuck_id = (u.ustuck ? u.ustuck->m_id : 0); - usteed_id = (u.usteed ? u.usteed->m_id : 0); + iv.ustuck_id = (u.ustuck ? u.ustuck->m_id : 0); + iv.usteed_id = (u.usteed ? u.usteed->m_id : 0); savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE); savegamestate(fd, WRITE_SAVE | FREE_SAVE); @@ -336,10 +333,10 @@ register int fd, mode; sizeof(struct spell) * (MAXSPELL + 1)); save_artifacts(fd); save_oracles(fd, mode); - if (ustuck_id) - bwrite(fd, (genericptr_t) &ustuck_id, sizeof ustuck_id); - if (usteed_id) - bwrite(fd, (genericptr_t) &usteed_id, sizeof usteed_id); + if (iv.ustuck_id) + bwrite(fd, (genericptr_t) &iv.ustuck_id, sizeof iv.ustuck_id); + if (iv.usteed_id) + bwrite(fd, (genericptr_t) &iv.usteed_id, sizeof iv.usteed_id); bwrite(fd, (genericptr_t) pl_character, sizeof pl_character); bwrite(fd, (genericptr_t) pl_fruit, sizeof pl_fruit); savefruitchn(fd, mode); @@ -369,7 +366,6 @@ void savestateinlock() { int fd, hpid; - static boolean havestate = TRUE; char whynot[BUFSZ]; /* When checkpointing is on, the full state needs to be written @@ -383,7 +379,7 @@ savestateinlock() * noop pid rewriting will take place on the first "checkpoint" after * the game is started or restored, if checkpointing is off. */ - if (flags.ins_chkpt || havestate) { + if (flags.ins_chkpt || iv.havestate) { /* save the rest of the current game state in the lock file, * following the original int pid, the current level number, * and the current savefile name, which should not be subject @@ -421,13 +417,13 @@ savestateinlock() store_savefileinfo(fd); store_plname_in_file(fd); - ustuck_id = (u.ustuck ? u.ustuck->m_id : 0); - usteed_id = (u.usteed ? u.usteed->m_id : 0); + iv.ustuck_id = (u.ustuck ? u.ustuck->m_id : 0); + iv.usteed_id = (u.usteed ? u.usteed->m_id : 0); savegamestate(fd, WRITE_SAVE); } bclose(fd); } - havestate = flags.ins_chkpt; + iv.havestate = flags.ins_chkpt; } #endif From dfc8f071f6769b961882bb20fe25810b29c50eaf Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 09:27:10 -0800 Subject: [PATCH 18/32] Renamed instance_variables to global_variables and iv to g. --- include/decl.h | 10 +++++----- include/rm.h | 2 +- src/apply.c | 12 ++++++------ src/artifact.c | 32 ++++++++++++++++---------------- src/botl.c | 4 ++-- src/decl.c | 12 ++++++------ src/dog.c | 2 +- src/eat.c | 2 +- src/mon.c | 4 ++-- src/muse.c | 16 ++++++++-------- src/pickup.c | 6 +++--- src/restore.c | 2 +- src/save.c | 20 ++++++++++---------- src/trap.c | 22 +++++++++++----------- src/u_init.c | 16 ++++++++-------- src/uhitm.c | 8 ++++---- src/weapon.c | 34 +++++++++++++++++----------------- src/zap.c | 20 ++++++++++---------- sys/share/pcmain.c | 2 +- sys/share/random.c | 2 +- win/win32/winhack.c | 2 +- 21 files changed, 115 insertions(+), 115 deletions(-) diff --git a/include/decl.h b/include/decl.h index e8a8dfab5..a097dcc17 100644 --- a/include/decl.h +++ b/include/decl.h @@ -438,13 +438,13 @@ struct early_opt { boolean valallowed; }; -/* instance_variables holds engine state that does not need to be +/* instance_globals holds engine state that does not need to be * persisted upon game exit. The initialization state is well defined * an set in decl.c during early early engine initialization. * - * unlike instance_flags, variables can be of any type. */ + * unlike instance_flags, values in the structure can be of any type. */ -struct instance_variables { +struct instance_globals { /* apply.c */ int jumping_is_magic; /* current jump result of magic */ int polearm_range_min; @@ -485,9 +485,9 @@ struct instance_variables { unsigned long magic; /* validate that structure layout is preserved */ }; -E struct instance_variables iv; +E struct instance_globals g; -E void instance_variable_init(); +E void instance_globals_init(); #undef E diff --git a/include/rm.h b/include/rm.h index d24c4e567..2997170d1 100644 --- a/include/rm.h +++ b/include/rm.h @@ -9,7 +9,7 @@ /* * The dungeon presentation graphics code and data structures were rewritten * and generalized for NetHack's release 2 by Eric S. Raymond (eric@snark) - * building on Don G. Kneller's MS-DOS implementation. See drawing.c for + * building on Don G Kneller's MS-DOS implementation. See drawing.c for * the code that permits the user to set the contents of the symbol structure. * * The door representation was changed by Ari diff --git a/src/apply.c b/src/apply.c index 710891283..63964f398 100644 --- a/src/apply.c +++ b/src/apply.c @@ -1606,7 +1606,7 @@ int x,y; { return (isok(x, y) && (ACCESSIBLE(levl[x][y].typ) || Passes_walls) - && is_valid_jump_pos(x, y, iv.jumping_is_magic, FALSE)); + && is_valid_jump_pos(x, y, g.jumping_is_magic, FALSE)); } void @@ -1720,7 +1720,7 @@ int magic; /* 0=Physical, otherwise skill level */ pline("Where do you want to jump?"); cc.x = u.ux; cc.y = u.uy; - iv.jumping_is_magic = magic; + g.jumping_is_magic = magic; getpos_sethilite(display_jump_positions, get_valid_jump_position); if (getpos(&cc, TRUE, "the desired position") < 0) return 0; /* user pressed ESC */ @@ -2917,8 +2917,8 @@ get_valid_polearm_position(x, y) int x, y; { return (isok(x, y) && ACCESSIBLE(levl[x][y].typ) - && distu(x, y) >= iv.polearm_range_min - && distu(x, y) <= iv.polearm_range_max); + && distu(x, y) >= g.polearm_range_min + && distu(x, y) <= g.polearm_range_max); } void @@ -2990,8 +2990,8 @@ struct obj *obj; else max_range = 8; /* (P_SKILL(typ) >= P_EXPERT) */ - iv.polearm_range_min = min_range; - iv.polearm_range_max = max_range; + g.polearm_range_min = min_range; + g.polearm_range_max = max_range; /* Prompt for a location */ pline(where_to_hit); diff --git a/src/artifact.c b/src/artifact.c index 2bb4f5078..455856601 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -844,15 +844,15 @@ int tmp; if (!weap || (weap->attk.adtyp == AD_PHYS /* check for `NO_ATTK' */ && weap->attk.damn == 0 && weap->attk.damd == 0)) - iv.spec_dbon_applies = FALSE; + g.spec_dbon_applies = FALSE; else if (otmp->oartifact == ART_GRIMTOOTH) /* Grimtooth has SPFX settings to warn against elves but we want its damage bonus to apply to all targets, so bypass spec_applies() */ - iv.spec_dbon_applies = TRUE; + g.spec_dbon_applies = TRUE; else - iv.spec_dbon_applies = spec_applies(weap, mon); + g.spec_dbon_applies = spec_applies(weap, mon); - if (iv.spec_dbon_applies) + if (g.spec_dbon_applies) return weap->attk.damd ? rnd((int) weap->attk.damd) : max(tmp, 1); return 0; } @@ -976,14 +976,14 @@ char *hittee; /* target's name: "you" or mon_nam(mdef) */ scare_dieroll /= (1 << (mb->spe / 3)); /* if target successfully resisted the artifact damage bonus, reduce overall likelihood of the assorted special effects */ - if (!iv.spec_dbon_applies) + if (!g.spec_dbon_applies) dieroll += 1; /* might stun even when attempting a more severe effect, but in that case it will only happen if the other effect fails; extra damage will apply regardless; 3.4.1: sometimes might just probe even when it hasn't been enchanted */ - do_stun = (max(mb->spe, 0) < rn2(iv.spec_dbon_applies ? 11 : 7)); + do_stun = (max(mb->spe, 0) < rn2(g.spec_dbon_applies ? 11 : 7)); /* the special effects also boost physical damage; increments are generally cumulative, but since the stun effect is based on a @@ -1182,12 +1182,12 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_FIRE, otmp)) { if (realizes_damage) pline_The("fiery blade %s %s%c", - !iv.spec_dbon_applies + !g.spec_dbon_applies ? "hits" : (mdef->data == &mons[PM_WATER_ELEMENTAL]) ? "vaporizes part of" : "burns", - hittee, !iv.spec_dbon_applies ? '.' : '!'); + hittee, !g.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_FIRE); if (!rn2(4)) @@ -1201,8 +1201,8 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_COLD, otmp)) { if (realizes_damage) pline_The("ice-cold blade %s %s%c", - !iv.spec_dbon_applies ? "hits" : "freezes", hittee, - !iv.spec_dbon_applies ? '.' : '!'); + !g.spec_dbon_applies ? "hits" : "freezes", hittee, + !g.spec_dbon_applies ? '.' : '!'); if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_COLD); return realizes_damage; @@ -1210,9 +1210,9 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_ELEC, otmp)) { if (realizes_damage) pline_The("massive hammer hits%s %s%c", - !iv.spec_dbon_applies ? "" : "! Lightning strikes", - hittee, !iv.spec_dbon_applies ? '.' : '!'); - if (iv.spec_dbon_applies) + !g.spec_dbon_applies ? "" : "! Lightning strikes", + hittee, !g.spec_dbon_applies ? '.' : '!'); + if (g.spec_dbon_applies) wake_nearto(mdef->mx, mdef->my, 4 * 4); if (!rn2(5)) (void) destroy_mitem(mdef, RING_CLASS, AD_ELEC); @@ -1223,10 +1223,10 @@ int dieroll; /* needed for Magicbane and vorpal blades */ if (attacks(AD_MAGM, otmp)) { if (realizes_damage) pline_The("imaginary widget hits%s %s%c", - !iv.spec_dbon_applies + !g.spec_dbon_applies ? "" : "! A hail of magic missiles strikes", - hittee, !iv.spec_dbon_applies ? '.' : '!'); + hittee, !g.spec_dbon_applies ? '.' : '!'); return realizes_damage; } @@ -1235,7 +1235,7 @@ int dieroll; /* needed for Magicbane and vorpal blades */ return Mb_hit(magr, mdef, otmp, dmgptr, dieroll, vis, hittee); } - if (!iv.spec_dbon_applies) { + if (!g.spec_dbon_applies) { /* since damage bonus didn't apply, nothing more to do; no further attacks have side-effects on inventory */ return FALSE; diff --git a/src/botl.c b/src/botl.c index 76b8b6425..1c3ab9281 100644 --- a/src/botl.c +++ b/src/botl.c @@ -64,7 +64,7 @@ do_statusline1() Strcpy(nb = eos(nb), rank()); Sprintf(nb = eos(nb), " "); - i = iv.mrank_sz + 15; + i = g.mrank_sz + 15; j = (int) ((nb + 2) - newbot1); /* strlen(newbot1) but less computation */ if ((i - j) > 0) Sprintf(nb = eos(nb), "%*s", i - j, " "); /* pad with spaces */ @@ -345,7 +345,7 @@ max_rank_sz() if (urole.rank[i].f && (r = strlen(urole.rank[i].f)) > maxr) maxr = r; } - iv.mrank_sz = maxr; + g.mrank_sz = maxr; return; } diff --git a/src/decl.c b/src/decl.c index c517edaa5..a71d5539d 100644 --- a/src/decl.c +++ b/src/decl.c @@ -329,7 +329,7 @@ decl_init() #define UNDEFINED_PTR NULL /* move to hack.h if we are keeping */ #define IVMAGIC 0xdeadbeef -const struct instance_variables iv_init = { +const struct instance_globals g_init = { /* apply.c */ 0, /* jumping_is_magic */ -1, /* polearm_range_min */ @@ -366,15 +366,15 @@ const struct instance_variables iv_init = { IVMAGIC /* used to validate that structure layout has been preserved */ }; -struct instance_variables iv; +struct instance_globals g; void -instance_variable_init() +instance_globals_init() { - iv = iv_init; + g = g_init; - nhassert(iv_init.magic == IVMAGIC); - nhassert(iv.havestate == TRUE); + nhassert(g_init.magic == IVMAGIC); + nhassert(g_init.havestate == TRUE); sfcap = default_sfinfo; sfrestinfo = default_sfinfo; diff --git a/src/dog.c b/src/dog.c index a5dadde70..c18b04bf1 100644 --- a/src/dog.c +++ b/src/dog.c @@ -197,7 +197,7 @@ makedog() put_saddle_on_mon(otmp, mtmp); } - if (!iv.petname_used++ && *petname) + if (!g.petname_used++ && *petname) mtmp = christen_monst(mtmp, petname); initedog(mtmp); diff --git a/src/eat.c b/src/eat.c index 94a86568a..69077934b 100644 --- a/src/eat.c +++ b/src/eat.c @@ -101,7 +101,7 @@ register struct obj *obj; || (obj->otyp == EGG)); if (u.umonnum == PM_GELATINOUS_CUBE && is_organic(obj) - /* [g.cubes can eat containers and retain all contents + /* [g-cubes can eat containers and retain all contents as engulfed items, but poly'd player can't do that] */ && !Has_contents(obj)) return TRUE; diff --git a/src/mon.c b/src/mon.c index 043b22e89..2b1ef22c6 100644 --- a/src/mon.c +++ b/src/mon.c @@ -921,7 +921,7 @@ struct monst *mtmp; /* eat organic objects, including cloth and wood, if present; engulf others, except huge rocks and metal attached to player - [despite comment at top, doesn't assume that eater is a g.cube] */ + [despite comment at top, doesn't assume that eater is a g-cube] */ for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) { otmp2 = otmp->nexthere; @@ -996,7 +996,7 @@ struct monst *mtmp; register struct obj *otmp3; /* contents of eaten containers become engulfed; this - is arbitrary, but otherwise g.cubes are too powerful */ + is arbitrary, but otherwise g-cubes are too powerful */ while ((otmp3 = otmp->cobj) != 0) { obj_extract_self(otmp3); if (otmp->otyp == ICE_BOX && otmp3->otyp == CORPSE) { diff --git a/src/muse.c b/src/muse.c index 493850a1f..394b6327e 100644 --- a/src/muse.c +++ b/src/muse.c @@ -668,12 +668,12 @@ struct monst *mtmp; zap_oseen = oseen; mzapmsg(mtmp, otmp, FALSE); otmp->spe--; - iv.m_using = TRUE; + g.m_using = TRUE; mbhit(mtmp, rn1(8, 6), mbhitm, bhito, otmp); /* monster learns that teleportation isn't useful here */ if (level.flags.noteleport) mtmp->mtrapseen |= (1 << (TELEP_TRAP - 1)); - iv.m_using = FALSE; + g.m_using = FALSE; return 2; case MUSE_SCR_TELEPORTATION: { int obj_is_cursed = otmp->cursed; @@ -1397,11 +1397,11 @@ struct monst *mtmp; otmp->spe--; if (oseen) makeknown(otmp->otyp); - iv.m_using = TRUE; + g.m_using = TRUE; buzz((int) (-30 - (otmp->otyp - WAN_MAGIC_MISSILE)), (otmp->otyp == WAN_MAGIC_MISSILE) ? 2 : 6, mtmp->mx, mtmp->my, sgn(mtmp->mux - mtmp->mx), sgn(mtmp->muy - mtmp->my)); - iv.m_using = FALSE; + g.m_using = FALSE; return (DEADMONSTER(mtmp)) ? 1 : 2; case MUSE_FIRE_HORN: case MUSE_FROST_HORN: @@ -1411,20 +1411,20 @@ struct monst *mtmp; } else You_hear("a horn being played."); otmp->spe--; - iv.m_using = TRUE; + g.m_using = TRUE; buzz(-30 - ((otmp->otyp == FROST_HORN) ? AD_COLD - 1 : AD_FIRE - 1), rn1(6, 6), mtmp->mx, mtmp->my, sgn(mtmp->mux - mtmp->mx), sgn(mtmp->muy - mtmp->my)); - iv.m_using = FALSE; + g.m_using = FALSE; return (DEADMONSTER(mtmp)) ? 1 : 2; case MUSE_WAN_TELEPORTATION: case MUSE_WAN_STRIKING: zap_oseen = oseen; mzapmsg(mtmp, otmp, FALSE); otmp->spe--; - iv.m_using = TRUE; + g.m_using = TRUE; mbhit(mtmp, rn1(8, 6), mbhitm, bhito, otmp); - iv.m_using = FALSE; + g.m_using = FALSE; return 2; case MUSE_SCR_EARTH: { /* TODO: handle steeds */ diff --git a/src/pickup.c b/src/pickup.c index 42bc59256..b008b0f8f 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -1558,7 +1558,7 @@ encumber_msg() { int newcap = near_capacity(); - if (iv.oldcap < newcap) { + if (g.oldcap < newcap) { switch (newcap) { case 1: Your("movements are slowed slightly because of your load."); @@ -1576,7 +1576,7 @@ encumber_msg() break; } context.botl = 1; - } else if (iv.oldcap > newcap) { + } else if (g.oldcap > newcap) { switch (newcap) { case 0: Your("movements are now unencumbered."); @@ -1595,7 +1595,7 @@ encumber_msg() context.botl = 1; } - iv.oldcap = newcap; + g.oldcap = newcap; return newcap; } diff --git a/src/restore.c b/src/restore.c index 35992d8e6..99b74d36d 100644 --- a/src/restore.c +++ b/src/restore.c @@ -899,7 +899,7 @@ register int fd; #ifdef MFLOPPY gameDiskPrompt(); #endif - max_rank_sz(); /* to recompute iv.mrank_sz (botl.c) */ + max_rank_sz(); /* to recompute g.mrank_sz (botl.c) */ /* take care of iron ball & chain */ for (otmp = fobj; otmp; otmp = otmp->nobj) if (otmp->owornmask) diff --git a/src/save.c b/src/save.c index de2c99b74..6528887b3 100644 --- a/src/save.c +++ b/src/save.c @@ -210,8 +210,8 @@ dosave0() store_version(fd); store_savefileinfo(fd); store_plname_in_file(fd); - iv.ustuck_id = (u.ustuck ? u.ustuck->m_id : 0); - iv.usteed_id = (u.usteed ? u.usteed->m_id : 0); + g.ustuck_id = (u.ustuck ? u.ustuck->m_id : 0); + g.usteed_id = (u.usteed ? u.usteed->m_id : 0); savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE); savegamestate(fd, WRITE_SAVE | FREE_SAVE); @@ -333,10 +333,10 @@ register int fd, mode; sizeof(struct spell) * (MAXSPELL + 1)); save_artifacts(fd); save_oracles(fd, mode); - if (iv.ustuck_id) - bwrite(fd, (genericptr_t) &iv.ustuck_id, sizeof iv.ustuck_id); - if (iv.usteed_id) - bwrite(fd, (genericptr_t) &iv.usteed_id, sizeof iv.usteed_id); + if (g.ustuck_id) + bwrite(fd, (genericptr_t) &g.ustuck_id, sizeof g.ustuck_id); + if (g.usteed_id) + bwrite(fd, (genericptr_t) &g.usteed_id, sizeof g.usteed_id); bwrite(fd, (genericptr_t) pl_character, sizeof pl_character); bwrite(fd, (genericptr_t) pl_fruit, sizeof pl_fruit); savefruitchn(fd, mode); @@ -379,7 +379,7 @@ savestateinlock() * noop pid rewriting will take place on the first "checkpoint" after * the game is started or restored, if checkpointing is off. */ - if (flags.ins_chkpt || iv.havestate) { + if (flags.ins_chkpt || g.havestate) { /* save the rest of the current game state in the lock file, * following the original int pid, the current level number, * and the current savefile name, which should not be subject @@ -417,13 +417,13 @@ savestateinlock() store_savefileinfo(fd); store_plname_in_file(fd); - iv.ustuck_id = (u.ustuck ? u.ustuck->m_id : 0); - iv.usteed_id = (u.usteed ? u.usteed->m_id : 0); + g.ustuck_id = (u.ustuck ? u.ustuck->m_id : 0); + g.usteed_id = (u.usteed ? u.usteed->m_id : 0); savegamestate(fd, WRITE_SAVE); } bclose(fd); } - iv.havestate = flags.ins_chkpt; + g.havestate = flags.ins_chkpt; } #endif diff --git a/src/trap.c b/src/trap.c index c6ba82ea0..4df8411ee 100644 --- a/src/trap.c +++ b/src/trap.c @@ -2160,7 +2160,7 @@ register struct monst *mtmp; } else { register int tt = trap->ttyp; boolean in_sight, tear_web, see_it, - inescapable = iv.force_mintrap || ((tt == HOLE || tt == PIT) + inescapable = g.force_mintrap || ((tt == HOLE || tt == PIT) && Sokoban && !trap->madeby_u); const char *fallverb; @@ -2276,7 +2276,7 @@ register struct monst *mtmp; || mptr == &mons[PM_BUGBEAR]) You_hear("the roaring of an angry bear!"); } - } else if (iv.force_mintrap) { + } else if (g.force_mintrap) { if (in_sight) { pline("%s evades %s bear trap!", Monnam(mtmp), a_your[trap->madeby_u]); @@ -2425,7 +2425,7 @@ register struct monst *mtmp; if (is_flyer(mptr) || is_floater(mptr) || (mtmp->wormno && count_wsegs(mtmp) > 5) || is_clinger(mptr)) { - if (iv.force_mintrap && !Sokoban) { + if (g.force_mintrap && !Sokoban) { /* openfallingtrap; not inescapable here */ if (in_sight) { seetrap(trap); @@ -2462,7 +2462,7 @@ register struct monst *mtmp; if (is_flyer(mptr) || is_floater(mptr) || mptr == &mons[PM_WUMPUS] || (mtmp->wormno && count_wsegs(mtmp) > 5) || mptr->msize >= MZ_HUGE) { - if (iv.force_mintrap && !Sokoban) { + if (g.force_mintrap && !Sokoban) { /* openfallingtrap; not inescapable here */ if (in_sight) { seetrap(trap); @@ -2551,7 +2551,7 @@ register struct monst *mtmp; a_your[trap->madeby_u]); deltrap(trap); newsym(mtmp->mx, mtmp->my); - } else if (iv.force_mintrap && !mtmp->mtrapped) { + } else if (g.force_mintrap && !mtmp->mtrapped) { if (in_sight) { pline("%s avoids %s spider web!", Monnam(mtmp), a_your[trap->madeby_u]); @@ -4686,9 +4686,9 @@ boolean *noticed; /* set to true iff hero notices the effect; */ /* dotrap calls mintrap when mounted hero encounters a web */ if (u.usteed) dotrapflags |= NOWEBMSG; - ++iv.force_mintrap; + ++g.force_mintrap; dotrap(t, dotrapflags); - --iv.force_mintrap; + --g.force_mintrap; result = (u.utrap != 0); } else { if (mon->mtrapped) @@ -4696,9 +4696,9 @@ boolean *noticed; /* set to true iff hero notices the effect; */ /* you notice it if you see the trap close/tremble/whatever or if you sense the monster who becomes trapped */ *noticed = cansee(t->tx, t->ty) || canspotmon(mon); - ++iv.force_mintrap; + ++g.force_mintrap; result = (mintrap(mon) != 0); - --iv.force_mintrap; + --g.force_mintrap; } return result; } @@ -4739,9 +4739,9 @@ boolean *noticed; /* set to true iff hero notices the effect; */ *noticed = cansee(t->tx, t->ty) || canspotmon(mon); /* monster will be angered; mintrap doesn't handle that */ wakeup(mon, TRUE); - ++iv.force_mintrap; + ++g.force_mintrap; result = (mintrap(mon) != 0); - --iv.force_mintrap; + --g.force_mintrap; /* mon might now be on the migrating monsters list */ } return result; diff --git a/src/u_init.c b/src/u_init.c index a522ac0ad..f18820d5a 100644 --- a/src/u_init.c +++ b/src/u_init.c @@ -991,9 +991,9 @@ register struct trobj *trop; */ obj = mkobj(trop->trclass, FALSE); otyp = obj->otyp; - while (otyp == WAN_WISHING || otyp == iv.nocreate - || otyp == iv.nocreate2 || otyp == iv.nocreate3 - || otyp == iv.nocreate4 || otyp == RIN_LEVITATION + while (otyp == WAN_WISHING || otyp == g.nocreate + || otyp == g.nocreate2 || otyp == g.nocreate3 + || otyp == g.nocreate4 || otyp == RIN_LEVITATION /* 'useless' items */ || otyp == POT_HALLUCINATION || otyp == POT_ACID @@ -1035,16 +1035,16 @@ register struct trobj *trop; case WAN_POLYMORPH: case RIN_POLYMORPH: case POT_POLYMORPH: - iv.nocreate = RIN_POLYMORPH_CONTROL; + g.nocreate = RIN_POLYMORPH_CONTROL; break; case RIN_POLYMORPH_CONTROL: - iv.nocreate = RIN_POLYMORPH; - iv.nocreate2 = SPE_POLYMORPH; - iv.nocreate3 = POT_POLYMORPH; + g.nocreate = RIN_POLYMORPH; + g.nocreate2 = SPE_POLYMORPH; + g.nocreate3 = POT_POLYMORPH; } /* Don't have 2 of the same ring or spellbook */ if (obj->oclass == RING_CLASS || obj->oclass == SPBOOK_CLASS) - iv.nocreate4 = otyp; + g.nocreate4 = otyp; } if (urace.malenum != PM_HUMAN) { diff --git a/src/uhitm.c b/src/uhitm.c index 4328b00ec..e434caacc 100644 --- a/src/uhitm.c +++ b/src/uhitm.c @@ -198,7 +198,7 @@ struct obj *wep; /* uwep for attack(), null for kick_monster() */ && !Stunned) { /* Intelligent chaotic weapons (Stormbringer) want blood */ if (wep && wep->oartifact == ART_STORMBRINGER) { - iv.override_confirmation = TRUE; + g.override_confirmation = TRUE; return FALSE; } if (canspotmon(mtmp)) { @@ -367,7 +367,7 @@ register struct monst *mtmp; /* possibly set in attack_checks; examined in known_hitum, called via hitum or hmonas below */ - iv.override_confirmation = FALSE; + g.override_confirmation = FALSE; /* attack_checks() used to use directly, now it uses bhitpos instead; it might map an invisible monster there */ bhitpos.x = u.ux + u.dx; @@ -448,7 +448,7 @@ int dieroll; { register boolean malive = TRUE; - if (iv.override_confirmation) { + if (g.override_confirmation) { /* this may need to be generalized if weapons other than Stormbringer acquire similar anti-social behavior... */ if (flags.verbose) @@ -601,7 +601,7 @@ struct attack *uattk; /* second attack for two-weapon combat; won't occur if Stormbringer overrode confirmation (assumes Stormbringer is primary weapon) or if the monster was killed or knocked to different location */ - if (u.twoweap && !iv.override_confirmation && malive && m_at(x, y) == mon) { + if (u.twoweap && !g.override_confirmation && malive && m_at(x, y) == mon) { tmp = find_roll_to_hit(mon, uattk->aatyp, uswapwep, &attknum, &armorpenalty); dieroll = rnd(20); diff --git a/src/weapon.c b/src/weapon.c index 2bdd2e549..d72c18b9b 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -399,7 +399,7 @@ register struct monst *mtmp; char mlet = mtmp->data->mlet; - iv.propellor = &zeroobj; + g.propellor = &zeroobj; Oselect(EGG); /* cockatrice egg */ if (mlet == S_KOP) /* pies are first choice for Kops */ Oselect(CREAM_PIE); @@ -431,7 +431,7 @@ register struct monst *mtmp; || !mon_hates_silver(mtmp))) { if ((otmp = oselect(mtmp, pwep[i])) != 0 && (otmp == mwep || !mweponly)) { - iv.propellor = otmp; /* force the monster to wield it */ + g.propellor = otmp; /* force the monster to wield it */ return otmp; } } @@ -452,41 +452,41 @@ register struct monst *mtmp; for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj) if (otmp->oclass == GEM_CLASS && (otmp->otyp != LOADSTONE || !otmp->cursed)) { - iv.propellor = m_carrying(mtmp, SLING); + g.propellor = m_carrying(mtmp, SLING); return otmp; } } /* KMH -- This belongs here so darts will work */ - iv.propellor = &zeroobj; + g.propellor = &zeroobj; prop = (objects[rwep[i]]).oc_skill; if (prop < 0) { switch (-prop) { case P_BOW: - iv.propellor = (oselect(mtmp, YUMI)); - if (!iv.propellor) - iv.propellor = (oselect(mtmp, ELVEN_BOW)); - if (!iv.propellor) - iv.propellor = (oselect(mtmp, BOW)); - if (!iv.propellor) - iv.propellor = (oselect(mtmp, ORCISH_BOW)); + g.propellor = (oselect(mtmp, YUMI)); + if (!g.propellor) + g.propellor = (oselect(mtmp, ELVEN_BOW)); + if (!g.propellor) + g.propellor = (oselect(mtmp, BOW)); + if (!g.propellor) + g.propellor = (oselect(mtmp, ORCISH_BOW)); break; case P_SLING: - iv.propellor = (oselect(mtmp, SLING)); + g.propellor = (oselect(mtmp, SLING)); break; case P_CROSSBOW: - iv.propellor = (oselect(mtmp, CROSSBOW)); + g.propellor = (oselect(mtmp, CROSSBOW)); } - if ((otmp = MON_WEP(mtmp)) && mwelded(otmp) && otmp != iv.propellor + if ((otmp = MON_WEP(mtmp)) && mwelded(otmp) && otmp != g.propellor && mtmp->weapon_check == NO_WEAPON_WANTED) - iv.propellor = 0; + g.propellor = 0; } /* propellor = obj, propellor to use * propellor = &zeroobj, doesn't need a propellor * propellor = 0, needed one and didn't have one */ - if (iv.propellor != 0) { + if (g.propellor != 0) { /* Note: cannot use m_carrying for loadstones, since it will * always select the first object of a type, and maybe the * monster is carrying two but only the first is unthrowable. @@ -633,7 +633,7 @@ register struct monst *mon; break; case NEED_RANGED_WEAPON: (void) select_rwep(mon); - obj = iv.propellor; + obj = g.propellor; break; case NEED_PICK_AXE: obj = m_carrying(mon, PICK_AXE); diff --git a/src/zap.c b/src/zap.c index f2c6f47dd..cf763280e 100644 --- a/src/zap.c +++ b/src/zap.c @@ -1360,13 +1360,13 @@ struct obj *obj; if (obj->otyp == SCR_MAIL) return; #endif - iv.obj_zapped = TRUE; + g.obj_zapped = TRUE; - if (iv.poly_zapped < 0) { + if (g.poly_zapped < 0) { /* some may metamorphosize */ for (i = obj->quan; i; i--) if (!rn2(Luck + 45)) { - iv.poly_zapped = objects[obj->otyp].oc_material; + g.poly_zapped = objects[obj->otyp].oc_material; break; } } @@ -2065,7 +2065,7 @@ schar zz; learnwand(obj); } - iv.poly_zapped = -1; + g.poly_zapped = -1; for (otmp = level.objects[tx][ty]; otmp; otmp = next_obj) { next_obj = otmp->nexthere; /* for zap downwards, don't hit object poly'd hero is hiding under */ @@ -2075,8 +2075,8 @@ schar zz; hitanything += (*fhito)(otmp, obj); } - if (iv.poly_zapped >= 0) - create_polymon(level.objects[tx][ty], iv.poly_zapped); + if (g.poly_zapped >= 0) + create_polymon(level.objects[tx][ty], g.poly_zapped); return hitanything; } @@ -2950,16 +2950,16 @@ struct obj *obj; /* wand or spell */ void zapsetup() { - iv.obj_zapped = FALSE; + g.obj_zapped = FALSE; } void zapwrapup() { /* if do_osshock() set obj_zapped while polying, give a message now */ - if (iv.obj_zapped) + if (g.obj_zapped) You_feel("shuddering vibrations."); - iv.obj_zapped = FALSE; + g.obj_zapped = FALSE; } /* called for various wand and spell effects - M. Stephenson */ @@ -5035,7 +5035,7 @@ int damage, tell; if (damage) { mtmp->mhp -= damage; if (DEADMONSTER(mtmp)) { - if (iv.m_using) + if (g.m_using) monkilled(mtmp, "", AD_RBRE); else killed(mtmp); diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index bd7ee0a74..ce95d3fdf 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -97,7 +97,7 @@ char *argv[]; nethack_enter(argc, argv); - instance_variable_init(); + instance_globals_init(); sys_early_init(); #if defined(WIN32) && defined(TTY_GRAPHICS) diff --git a/sys/share/random.c b/sys/share/random.c index 6d3d5e45c..3ec33b037 100644 --- a/sys/share/random.c +++ b/sys/share/random.c @@ -238,7 +238,7 @@ unsigned x; char * initstate(seed, arg_state, n) -unsigned seed; /* seed for R. N. G. */ +unsigned seed; /* seed for RNG */ char *arg_state; /* pointer to state array */ int n; /* # bytes of state info */ { diff --git a/win/win32/winhack.c b/win/win32/winhack.c index 466044ec2..43bca5a5d 100644 --- a/win/win32/winhack.c +++ b/win/win32/winhack.c @@ -97,7 +97,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, win10_init(); - instance_variable_init(); + instance_globals_init(); sys_early_init(); /* init applicatio structure */ From ee8ce55b7bfcd8a6a62db50df949915c4a985101 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 10:05:21 -0800 Subject: [PATCH 19/32] rumors.c static globals moved to instance_globals. --- include/decl.h | 13 +++++ src/decl.c | 10 ++++ src/rumors.c | 125 ++++++++++++++++++++++--------------------------- 3 files changed, 80 insertions(+), 68 deletions(-) diff --git a/include/decl.h b/include/decl.h index a097dcc17..f13efca21 100644 --- a/include/decl.h +++ b/include/decl.h @@ -460,6 +460,19 @@ struct instance_globals { boolean m_using; /* kludge to use mondided instead of killed */ /* pickup.c */ int oldcap; /* last encumberance */ + /* rumors.c */ + long true_rumor_size; /* rumor size variables are signed so that value -1 + can be used as a flag */ + long false_rumor_size; + unsigned long true_rumor_start; /* rumor start offsets are unsigned because + they're handled via %lx format */ + unsigned long false_rumor_start; + long true_rumor_end; /* rumor end offsets are signed because they're + compared with [dlb_]ftell() */ + long false_rumor_end; + int oracle_flg; /* -1=>don't use, 0=>need init, 1=>init done */ + unsigned oracle_cnt; /* oracles are handled differently from rumors... */ + unsigned long *oracle_loc; /* save.c */ boolean havestate; unsigned ustuck_id; /* need to preserve during save */ diff --git a/src/decl.c b/src/decl.c index a71d5539d..c8aaf7275 100644 --- a/src/decl.c +++ b/src/decl.c @@ -344,6 +344,16 @@ const struct instance_globals g_init = { FALSE, /* m_using */ /* pickup.c */ 0, /* oldcap */ + /* rumors.c */ + 0, /* true_rumor_size */ + 0, /* false_rumor_size */ + UNDEFINED, /* true_rumor_start*/ + UNDEFINED, /* false_rumor_start*/ + UNDEFINED, /* true_rumor_end */ + UNDEFINED, /* false_rumor_end */ + 0, /* oracle_flag */ + 0, /* oracle_cnt */ + NULL, /* oracle_loc */ /* save.c */ TRUE, /* havestate*/ 0, /* ustuck_id */ diff --git a/src/rumors.c b/src/rumors.c index a0f367952..fb84a6126 100644 --- a/src/rumors.c +++ b/src/rumors.c @@ -45,17 +45,6 @@ STATIC_DCL void FDECL(init_rumors, (dlb *)); STATIC_DCL void FDECL(init_oracles, (dlb *)); STATIC_DCL void FDECL(couldnt_open_file, (const char *)); -/* rumor size variables are signed so that value -1 can be used as a flag */ -static long true_rumor_size = 0L, false_rumor_size; -/* rumor start offsets are unsigned because they're handled via %lx format */ -static unsigned long true_rumor_start, false_rumor_start; -/* rumor end offsets are signed because they're compared with [dlb_]ftell() */ -static long true_rumor_end, false_rumor_end; -/* oracles are handled differently from rumors... */ -static int oracle_flg = 0; /* -1=>don't use, 0=>need init, 1=>init done */ -static unsigned oracle_cnt = 0; -static unsigned long *oracle_loc = 0; - STATIC_OVL void init_rumors(fp) dlb *fp; @@ -67,17 +56,17 @@ dlb *fp; (void) dlb_fgets(line, sizeof line, fp); /* skip "don't edit" comment */ (void) dlb_fgets(line, sizeof line, fp); - if (sscanf(line, rumors_header, &true_count, &true_rumor_size, - &true_rumor_start, &false_count, &false_rumor_size, - &false_rumor_start, &eof_offset) == 7 - && true_rumor_size > 0L - && false_rumor_size > 0L) { - true_rumor_end = (long) true_rumor_start + true_rumor_size; - /* assert( true_rumor_end == false_rumor_start ); */ - false_rumor_end = (long) false_rumor_start + false_rumor_size; - /* assert( false_rumor_end == eof_offset ); */ + if (sscanf(line, rumors_header, &true_count, &g.true_rumor_size, + &g.true_rumor_start, &false_count, &g.false_rumor_size, + &g.false_rumor_start, &eof_offset) == 7 + && g.true_rumor_size > 0L + && g.false_rumor_size > 0L) { + g.true_rumor_end = (long) g.true_rumor_start + g.true_rumor_size; + /* assert( g.true_rumor_end == false_rumor_start ); */ + g.false_rumor_end = (long) g.false_rumor_start + g.false_rumor_size; + /* assert( g.false_rumor_end == eof_offset ); */ } else { - true_rumor_size = -1L; /* init failed */ + g.true_rumor_size = -1L; /* init failed */ (void) dlb_fclose(fp); } } @@ -98,7 +87,7 @@ boolean exclude_cookie; char *endp, line[BUFSZ], xbuf[BUFSZ]; rumor_buf[0] = '\0'; - if (true_rumor_size < 0L) /* we couldn't open RUMORFILE */ + if (g.true_rumor_size < 0L) /* we couldn't open RUMORFILE */ return rumor_buf; rumors = dlb_fopen(RUMORFILE, "r"); @@ -109,9 +98,9 @@ boolean exclude_cookie; do { rumor_buf[0] = '\0'; - if (true_rumor_size == 0L) { /* if this is 1st outrumor() */ + if (g.true_rumor_size == 0L) { /* if this is 1st outrumor() */ init_rumors(rumors); - if (true_rumor_size < 0L) { /* init failed */ + if (g.true_rumor_size < 0L) { /* init failed */ Sprintf(rumor_buf, "Error reading \"%.80s\".", RUMORFILE); return rumor_buf; } @@ -124,13 +113,13 @@ boolean exclude_cookie; switch (adjtruth = truth + rn2(2)) { case 2: /*(might let a bogus input arg sneak thru)*/ case 1: - beginning = (long) true_rumor_start; - tidbit = Rand() % true_rumor_size; + beginning = (long) g.true_rumor_start; + tidbit = Rand() % g.true_rumor_size; break; case 0: /* once here, 0 => false rather than "either"*/ case -1: - beginning = (long) false_rumor_start; - tidbit = Rand() % false_rumor_size; + beginning = (long) g.false_rumor_start; + tidbit = Rand() % g.false_rumor_size; break; default: impossible("strange truth value for rumor"); @@ -139,7 +128,7 @@ boolean exclude_cookie; (void) dlb_fseek(rumors, beginning + tidbit, SEEK_SET); (void) dlb_fgets(line, sizeof line, rumors); if (!dlb_fgets(line, sizeof line, rumors) - || (adjtruth > 0 && dlb_ftell(rumors) > true_rumor_end)) { + || (adjtruth > 0 && dlb_ftell(rumors) > g.true_rumor_end)) { /* reached end of rumors -- go back to beginning */ (void) dlb_fseek(rumors, beginning, SEEK_SET); (void) dlb_fgets(line, sizeof line, rumors); @@ -157,7 +146,7 @@ boolean exclude_cookie; exercise(A_WIS, (adjtruth > 0)); } else { couldnt_open_file(RUMORFILE); - true_rumor_size = -1; /* don't try to open it again */ + g.true_rumor_size = -1; /* don't try to open it again */ } /* this is safe either way, so do it always since we can't get the definition * out of makedefs.c @@ -187,7 +176,7 @@ rumor_check() winid tmpwin; char *endp, line[BUFSZ], xbuf[BUFSZ], rumor_buf[BUFSZ]; - if (true_rumor_size < 0L) { /* we couldn't open RUMORFILE */ + if (g.true_rumor_size < 0L) { /* we couldn't open RUMORFILE */ no_rumors: pline("rumors not accessible."); return; @@ -199,9 +188,9 @@ rumor_check() long ftell_rumor_start = 0L; rumor_buf[0] = '\0'; - if (true_rumor_size == 0L) { /* if this is 1st outrumor() */ + if (g.true_rumor_size == 0L) { /* if this is 1st outrumor() */ init_rumors(rumors); - if (true_rumor_size < 0L) + if (g.true_rumor_size < 0L) goto no_rumors; /* init failed */ } tmpwin = create_nhwindow(NHW_TEXT); @@ -213,17 +202,17 @@ rumor_check() Sprintf( rumor_buf, "T start=%06ld (%06lx), end=%06ld (%06lx), size=%06ld (%06lx)", - (long) true_rumor_start, true_rumor_start, true_rumor_end, - (unsigned long) true_rumor_end, true_rumor_size, - (unsigned long) true_rumor_size); + (long) g.true_rumor_start, g.true_rumor_start, g.true_rumor_end, + (unsigned long) g.true_rumor_end, g.true_rumor_size, + (unsigned long) g.true_rumor_size); putstr(tmpwin, 0, rumor_buf); Sprintf( rumor_buf, "F start=%06ld (%06lx), end=%06ld (%06lx), size=%06ld (%06lx)", - (long) false_rumor_start, false_rumor_start, false_rumor_end, - (unsigned long) false_rumor_end, false_rumor_size, - (unsigned long) false_rumor_size); + (long) g.false_rumor_start, g.false_rumor_start, g.false_rumor_end, + (unsigned long) g.false_rumor_end, g.false_rumor_size, + (unsigned long) g.false_rumor_size); putstr(tmpwin, 0, rumor_buf); /* @@ -234,7 +223,7 @@ rumor_check() * the value read in rumors, and display it. */ rumor_buf[0] = '\0'; - (void) dlb_fseek(rumors, (long) true_rumor_start, SEEK_SET); + (void) dlb_fseek(rumors, (long) g.true_rumor_start, SEEK_SET); ftell_rumor_start = dlb_ftell(rumors); (void) dlb_fgets(line, sizeof line, rumors); if ((endp = index(line, '\n')) != 0) @@ -244,7 +233,7 @@ rumor_check() putstr(tmpwin, 0, rumor_buf); /* find last true rumor */ while (dlb_fgets(line, sizeof line, rumors) - && dlb_ftell(rumors) < true_rumor_end) + && dlb_ftell(rumors) < g.true_rumor_end) continue; if ((endp = index(line, '\n')) != 0) *endp = 0; @@ -252,7 +241,7 @@ rumor_check() putstr(tmpwin, 0, rumor_buf); rumor_buf[0] = '\0'; - (void) dlb_fseek(rumors, (long) false_rumor_start, SEEK_SET); + (void) dlb_fseek(rumors, (long) g.false_rumor_start, SEEK_SET); ftell_rumor_start = dlb_ftell(rumors); (void) dlb_fgets(line, sizeof line, rumors); if ((endp = index(line, '\n')) != 0) @@ -262,7 +251,7 @@ rumor_check() putstr(tmpwin, 0, rumor_buf); /* find last false rumor */ while (dlb_fgets(line, sizeof line, rumors) - && dlb_ftell(rumors) < false_rumor_end) + && dlb_ftell(rumors) < g.false_rumor_end) continue; if ((endp = index(line, '\n')) != 0) *endp = 0; @@ -274,7 +263,7 @@ rumor_check() destroy_nhwindow(tmpwin); } else { couldnt_open_file(RUMORFILE); - true_rumor_size = -1; /* don't try to open it again */ + g.true_rumor_size = -1; /* don't try to open it again */ } } @@ -379,11 +368,11 @@ dlb *fp; (void) dlb_fgets(line, sizeof line, fp); /* skip "don't edit" comment*/ (void) dlb_fgets(line, sizeof line, fp); if (sscanf(line, "%5d\n", &cnt) == 1 && cnt > 0) { - oracle_cnt = (unsigned) cnt; - oracle_loc = (unsigned long *) alloc((unsigned) cnt * sizeof(long)); + g.oracle_cnt = (unsigned) cnt; + g.oracle_loc = (unsigned long *) alloc((unsigned) cnt * sizeof(long)); for (i = 0; i < cnt; i++) { (void) dlb_fgets(line, sizeof line, fp); - (void) sscanf(line, "%5lx\n", &oracle_loc[i]); + (void) sscanf(line, "%5lx\n", &g.oracle_loc[i]); } } return; @@ -394,14 +383,14 @@ save_oracles(fd, mode) int fd, mode; { if (perform_bwrite(mode)) { - bwrite(fd, (genericptr_t) &oracle_cnt, sizeof oracle_cnt); - if (oracle_cnt) - bwrite(fd, (genericptr_t) oracle_loc, oracle_cnt * sizeof(long)); + bwrite(fd, (genericptr_t) &g.oracle_cnt, sizeof g.oracle_cnt); + if (g.oracle_cnt) + bwrite(fd, (genericptr_t) g.oracle_loc, g.oracle_cnt * sizeof(long)); } if (release_data(mode)) { - if (oracle_cnt) { - free((genericptr_t) oracle_loc); - oracle_loc = 0, oracle_cnt = 0, oracle_flg = 0; + if (g.oracle_cnt) { + free((genericptr_t) g.oracle_loc); + g.oracle_loc = 0, g.oracle_cnt = 0, g.oracle_flg = 0; } } } @@ -410,11 +399,11 @@ void restore_oracles(fd) int fd; { - mread(fd, (genericptr_t) &oracle_cnt, sizeof oracle_cnt); - if (oracle_cnt) { - oracle_loc = (unsigned long *) alloc(oracle_cnt * sizeof(long)); - mread(fd, (genericptr_t) oracle_loc, oracle_cnt * sizeof(long)); - oracle_flg = 1; /* no need to call init_oracles() */ + mread(fd, (genericptr_t) &g.oracle_cnt, sizeof g.oracle_cnt); + if (g.oracle_cnt) { + g.oracle_loc = (unsigned long *) alloc(g.oracle_cnt * sizeof(long)); + mread(fd, (genericptr_t) g.oracle_loc, g.oracle_cnt * sizeof(long)); + g.oracle_flg = 1; /* no need to call init_oracles() */ } } @@ -431,27 +420,27 @@ boolean delphi; /* early return if we couldn't open ORACLEFILE on previous attempt, or if all the oracularities are already exhausted */ - if (oracle_flg < 0 || (oracle_flg > 0 && oracle_cnt == 0)) + if (g.oracle_flg < 0 || (g.oracle_flg > 0 && g.oracle_cnt == 0)) return; oracles = dlb_fopen(ORACLEFILE, "r"); if (oracles) { winid tmpwin; - if (oracle_flg == 0) { /* if this is the first outoracle() */ + if (g.oracle_flg == 0) { /* if this is the first outoracle() */ init_oracles(oracles); - oracle_flg = 1; - if (oracle_cnt == 0) + g.oracle_flg = 1; + if (g.oracle_cnt == 0) return; } /* oracle_loc[0] is the special oracle; oracle_loc[1..oracle_cnt-1] are normal ones */ - if (oracle_cnt <= 1 && !special) + if (g.oracle_cnt <= 1 && !special) return; /*(shouldn't happen)*/ - oracle_idx = special ? 0 : rnd((int) oracle_cnt - 1); - (void) dlb_fseek(oracles, (long) oracle_loc[oracle_idx], SEEK_SET); + oracle_idx = special ? 0 : rnd((int) g.oracle_cnt - 1); + (void) dlb_fseek(oracles, (long) g.oracle_loc[oracle_idx], SEEK_SET); if (!special) /* move offset of very last one into this slot */ - oracle_loc[oracle_idx] = oracle_loc[--oracle_cnt]; + g.oracle_loc[oracle_idx] = g.oracle_loc[--g.oracle_cnt]; tmpwin = create_nhwindow(NHW_TEXT); if (delphi) @@ -473,7 +462,7 @@ boolean delphi; (void) dlb_fclose(oracles); } else { couldnt_open_file(ORACLEFILE); - oracle_flg = -1; /* don't try to open it again */ + g.oracle_flg = -1; /* don't try to open it again */ } } @@ -515,7 +504,7 @@ struct monst *oracl; break; case 'n': if (umoney <= (long) minor_cost /* don't even ask */ - || (oracle_cnt == 1 || oracle_flg < 0)) + || (g.oracle_cnt == 1 || g.oracle_flg < 0)) return 0; Sprintf(qbuf, "\"Then dost thou desire a major one?\" (%d %s)", major_cost, currency((long) major_cost)); From 4744eeb765a081eeee7faa9bd2661d38094e548d Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 10:15:22 -0800 Subject: [PATCH 20/32] rfilter initialized to UNDEFINED. --- include/hack.h | 7 +++++++ src/decl.c | 2 -- src/role.c | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/hack.h b/include/hack.h index 5785b8590..3525e1695 100644 --- a/include/hack.h +++ b/include/hack.h @@ -22,6 +22,13 @@ 0 \ } +/* UNDEFINED and UNDEFINED_PTR are used to initialize variables whose + initialized value is not relied upon. UNDEFINED can be used to + initialized any value type except pointers. UNDEFINED_PTR can be used + only on pointer types. */ +#define UNDEFINED { 0 } +#define UNDEFINED_PTR NULL + /* symbolic names for capacity levels */ enum encumbrance_types { UNENCUMBERED = 0, diff --git a/src/decl.c b/src/decl.c index c8aaf7275..559969c39 100644 --- a/src/decl.c +++ b/src/decl.c @@ -325,8 +325,6 @@ decl_init() return; } -#define UNDEFINED { 0 } /* move to hack.h if we are keeping */ -#define UNDEFINED_PTR NULL /* move to hack.h if we are keeping */ #define IVMAGIC 0xdeadbeef const struct instance_globals g_init = { diff --git a/src/role.c b/src/role.c index 69adcab7b..b61babf7c 100644 --- a/src/role.c +++ b/src/role.c @@ -769,7 +769,7 @@ const struct Align aligns[] = { static struct { boolean roles[SIZE(roles)]; short mask; -} rfilter; +} rfilter = UNDEFINED; STATIC_DCL int NDECL(randrole_filtered); STATIC_DCL char *FDECL(promptsep, (char *, int)); From 9e40a1dfae8a5874287a11b876d8c740b08dfee5 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 10:22:01 -0800 Subject: [PATCH 21/32] Renamed UNDEFINED to UNDEFINED_VALUE. --- include/hack.h | 6 +++--- src/decl.c | 14 +++++++------- src/role.c | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/hack.h b/include/hack.h index 3525e1695..fd38f5b30 100644 --- a/include/hack.h +++ b/include/hack.h @@ -22,11 +22,11 @@ 0 \ } -/* UNDEFINED and UNDEFINED_PTR are used to initialize variables whose - initialized value is not relied upon. UNDEFINED can be used to +/* UNDEFINED_VALUE and UNDEFINED_PTR are used to initialize variables whose + initialized value is not relied upon. UNDEFINED_VALUE can be used to initialized any value type except pointers. UNDEFINED_PTR can be used only on pointer types. */ -#define UNDEFINED { 0 } +#define UNDEFINED_VALUE { 0 } #define UNDEFINED_PTR NULL /* symbolic names for capacity levels */ diff --git a/src/decl.c b/src/decl.c index 559969c39..3214faab1 100644 --- a/src/decl.c +++ b/src/decl.c @@ -345,10 +345,10 @@ const struct instance_globals g_init = { /* rumors.c */ 0, /* true_rumor_size */ 0, /* false_rumor_size */ - UNDEFINED, /* true_rumor_start*/ - UNDEFINED, /* false_rumor_start*/ - UNDEFINED, /* true_rumor_end */ - UNDEFINED, /* false_rumor_end */ + UNDEFINED_VALUE, /* true_rumor_start*/ + UNDEFINED_VALUE, /* false_rumor_start*/ + UNDEFINED_VALUE, /* true_rumor_end */ + UNDEFINED_VALUE, /* false_rumor_end */ 0, /* oracle_flag */ 0, /* oracle_cnt */ NULL, /* oracle_loc */ @@ -364,12 +364,12 @@ const struct instance_globals g_init = { STRANGE_OBJECT, /* nocreate3 */ STRANGE_OBJECT, /* nocreate4 */ /* uhitm.c */ - UNDEFINED, /* override_confirmation */ + UNDEFINED_VALUE, /* override_confirmation */ /* weapon.c */ UNDEFINED_PTR, /* propellor */ /* zap.c */ - UNDEFINED, /* poly_zap */ - UNDEFINED, /* obj_zapped */ + UNDEFINED_VALUE, /* poly_zap */ + UNDEFINED_VALUE, /* obj_zapped */ IVMAGIC /* used to validate that structure layout has been preserved */ }; diff --git a/src/role.c b/src/role.c index b61babf7c..b5aae5068 100644 --- a/src/role.c +++ b/src/role.c @@ -769,7 +769,7 @@ const struct Align aligns[] = { static struct { boolean roles[SIZE(roles)]; short mask; -} rfilter = UNDEFINED; +} rfilter = UNDEFINED_VALUE; STATIC_DCL int NDECL(randrole_filtered); STATIC_DCL char *FDECL(promptsep, (char *, int)); From c91d5605919d3d0617900742232f751c2cb46d5c Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 10:36:45 -0800 Subject: [PATCH 22/32] restore.c globals moved to instance_globals. --- include/decl.h | 12 +++++++++ src/artifact.c | 2 +- src/decl.c | 19 ++++++++++++++ src/objnam.c | 2 +- src/restore.c | 65 +++++++++++++++++++++--------------------------- win/tty/wintty.c | 3 +-- 6 files changed, 63 insertions(+), 40 deletions(-) diff --git a/include/decl.h b/include/decl.h index f13efca21..18d30daea 100644 --- a/include/decl.h +++ b/include/decl.h @@ -454,12 +454,23 @@ struct instance_globals { messages in artifact_hit() */ /* botl.c */ int mrank_sz; /* loaded by max_rank_sz */ + /* dog.c */ int petname_used; /* user preferred pet name has been used */ + /* muse.c */ boolean m_using; /* kludge to use mondided instead of killed */ + /* pickup.c */ int oldcap; /* last encumberance */ + + /* restore.c */ + int n_ids_mapped; + struct bucket *id_map; + boolean restoring; + struct fruit *oldfruit; + long omoves; + /* rumors.c */ long true_rumor_size; /* rumor size variables are signed so that value -1 can be used as a flag */ @@ -473,6 +484,7 @@ struct instance_globals { int oracle_flg; /* -1=>don't use, 0=>need init, 1=>init done */ unsigned oracle_cnt; /* oracles are handled differently from rumors... */ unsigned long *oracle_loc; + /* save.c */ boolean havestate; unsigned ustuck_id; /* need to preserve during save */ diff --git a/src/artifact.c b/src/artifact.c index 455856601..698913ede 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -536,7 +536,7 @@ long wp_mask; * that can print a message--need to guard against being printed * when restoring a game */ - (void) make_hallucinated((long) !on, restoring ? FALSE : TRUE, + (void) make_hallucinated((long) !on, g.restoring ? FALSE : TRUE, wp_mask); } if (spfx & SPFX_ESP) { diff --git a/src/decl.c b/src/decl.c index 3214faab1..c006cc715 100644 --- a/src/decl.c +++ b/src/decl.c @@ -332,16 +332,29 @@ const struct instance_globals g_init = { 0, /* jumping_is_magic */ -1, /* polearm_range_min */ -1, /* polearm_range_max */ + /* artifact.c */ 0, /* spec_dbon_applies */ + /* botl.c */ 0, /* mrank_sz */ + /* dog.c */ 0, /* petname_used */ + /* mused.c */ FALSE, /* m_using */ + /* pickup.c */ 0, /* oldcap */ + + /* restore.c */ + 0, /* n_ids_mapped */ + 0, /* id_map */ + FALSE, /* restoring */ + UNDEFINED_PTR, /* oldfruit */ + UNDEFINED_VALUE, /* omoves */ + /* rumors.c */ 0, /* true_rumor_size */ 0, /* false_rumor_size */ @@ -352,21 +365,27 @@ const struct instance_globals g_init = { 0, /* oracle_flag */ 0, /* oracle_cnt */ NULL, /* oracle_loc */ + /* save.c */ TRUE, /* havestate*/ 0, /* ustuck_id */ 0, /* usteed_id */ + /* trap.c */ 0, /* force_mintrap */ + /* u_init.c */ STRANGE_OBJECT, /* nocreate */ STRANGE_OBJECT, /* nocreate2 */ STRANGE_OBJECT, /* nocreate3 */ STRANGE_OBJECT, /* nocreate4 */ + /* uhitm.c */ UNDEFINED_VALUE, /* override_confirmation */ + /* weapon.c */ UNDEFINED_PTR, /* propellor */ + /* zap.c */ UNDEFINED_VALUE, /* poly_zap */ UNDEFINED_VALUE, /* obj_zapped */ diff --git a/src/objnam.c b/src/objnam.c index 3fc3e01d2..66e09d746 100644 --- a/src/objnam.c +++ b/src/objnam.c @@ -1200,7 +1200,7 @@ unsigned doname_flags; } /* treat 'restoring' like suppress_price because shopkeeper and bill might not be available yet while restore is in progress */ - if (!iflags.suppress_price && !restoring && is_unpaid(obj)) { + if (!iflags.suppress_price && !g.restoring && is_unpaid(obj)) { long quotedprice = unpaid_cost(obj, TRUE); Sprintf(eos(bp), " (%s, %ld %s)", diff --git a/src/restore.c b/src/restore.c index 99b74d36d..ff14135f0 100644 --- a/src/restore.c +++ b/src/restore.c @@ -58,7 +58,7 @@ static struct restore_procs { /* * Save a mapping of IDs from ghost levels to the current level. This - * map is used by the timer routines when restoring ghost levels. + * map is used by the timer routines when g.restoring ghost levels. */ #define N_PER_BUCKET 64 struct bucket { @@ -72,9 +72,6 @@ struct bucket { STATIC_DCL void NDECL(clear_id_mapping); STATIC_DCL void FDECL(add_id_mapping, (unsigned, unsigned)); -static int n_ids_mapped = 0; -static struct bucket *id_map = 0; - #ifdef AMII_GRAPHICS void FDECL(amii_setpens, (int)); /* use colors from save file */ extern int amii_numcolors; @@ -82,10 +79,6 @@ extern int amii_numcolors; #include "display.h" -boolean restoring = FALSE; -static NEARDATA struct fruit *oldfruit; -static NEARDATA long omoves; - #define Is_IceBox(o) ((o)->otyp == ICE_BOX ? TRUE : FALSE) /* Recalculate level.objects[x][y], since this info was not saved. */ @@ -179,11 +172,11 @@ boolean ghostly; mread(fd, (genericptr_t) tmp_dam, sizeof(*tmp_dam)); if (ghostly) - tmp_dam->when += (monstermoves - omoves); + tmp_dam->when += (monstermoves - g.omoves); Strcpy(damaged_shops, in_rooms(tmp_dam->place.x, tmp_dam->place.y, SHOPBASE)); if (u.uz.dlevel) { - /* when restoring, there are two passes over the current + /* when g.restoring, there are two passes over the current * level. the first time, u.uz isn't set, so neither is * shop_keeper(). just wait and process the damage on * the second pass. @@ -216,7 +209,7 @@ struct obj *otmp; mread(fd, (genericptr_t) otmp, sizeof(struct obj)); /* next object pointers are invalid; otmp->cobj needs to be left - as is--being non-null is key to restoring container contents */ + as is--being non-null is key to g.restoring container contents */ otmp->nobj = otmp->nexthere = (struct obj *) 0; /* non-null oextra needs to be reconstructed */ if (otmp->oextra) { @@ -293,7 +286,7 @@ boolean ghostly, frozen; * immediately after old player died. */ if (ghostly && !frozen && !age_is_relative(otmp)) - otmp->age = monstermoves - omoves + otmp->age; + otmp->age = monstermoves - g.omoves + otmp->age; /* get contents of a container or statue */ if (Has_contents(otmp)) { @@ -513,7 +506,7 @@ register struct obj *otmp; { register struct fruit *oldf; - for (oldf = oldfruit; oldf; oldf = oldf->nextf) + for (oldf = g.oldfruit; oldf; oldf = oldf->nextf) if (oldf->fid == otmp->spe) break; @@ -797,7 +790,7 @@ register int fd; int rtmp; struct obj *otmp; - restoring = TRUE; + g.restoring = TRUE; get_plname_from_file(fd, plname); getlev(fd, 0, (xchar) 0, FALSE); if (!restgamestate(fd, &stuckid, &steedid)) { @@ -805,7 +798,7 @@ register int fd; savelev(-1, 0, FREE_SAVE); /* discard current level */ (void) nhclose(fd); (void) delete_savefile(); - restoring = FALSE; + g.restoring = FALSE; return 0; } restlevelstate(stuckid, steedid); @@ -921,7 +914,7 @@ register int fd; run_timers(); /* expire all timers that have gone off while away */ docrt(); - restoring = FALSE; + g.restoring = FALSE; clear_nhwindow(WIN_MESSAGE); /* Success! */ @@ -1025,10 +1018,10 @@ boolean ghostly; setmode(fd, O_BINARY); #endif /* Load the old fruit info. We have to do it first, so the - * information is available when restoring the objects. + * information is available when g.restoring the objects. */ if (ghostly) - oldfruit = loadfruitchn(fd); + g.oldfruit = loadfruitchn(fd); /* First some sanity checks */ mread(fd, (genericptr_t) &hpid, sizeof(hpid)); @@ -1055,8 +1048,8 @@ boolean ghostly; rest_levl(fd, (boolean) ((sfrestinfo.sfi1 & SFI1_RLECOMP) == SFI1_RLECOMP)); mread(fd, (genericptr_t) lastseentyp, sizeof(lastseentyp)); - mread(fd, (genericptr_t) &omoves, sizeof(omoves)); - elapsed = monstermoves - omoves; + mread(fd, (genericptr_t) &g.omoves, sizeof(g.omoves)); + elapsed = monstermoves - g.omoves; mread(fd, (genericptr_t) &upstair, sizeof(stairway)); mread(fd, (genericptr_t) &dnstair, sizeof(stairway)); mread(fd, (genericptr_t) &upladder, sizeof(stairway)); @@ -1132,7 +1125,7 @@ boolean ghostly; rest_regions(fd, ghostly); if (ghostly) { /* Now get rid of all the temp fruits... */ - freefruitchn(oldfruit), oldfruit = 0; + freefruitchn(g.oldfruit), g.oldfruit = 0; if (lev > ledger_no(&medusa_level) && lev < ledger_no(&stronghold_level) && xdnstair == 0) { @@ -1229,11 +1222,11 @@ clear_id_mapping() { struct bucket *curr; - while ((curr = id_map) != 0) { - id_map = curr->next; + while ((curr = g.id_map) != 0) { + g.id_map = curr->next; free((genericptr_t) curr); } - n_ids_mapped = 0; + g.n_ids_mapped = 0; } /* Add a mapping to the ID map. */ @@ -1243,18 +1236,18 @@ unsigned gid, nid; { int idx; - idx = n_ids_mapped % N_PER_BUCKET; + idx = g.n_ids_mapped % N_PER_BUCKET; /* idx is zero on first time through, as well as when a new bucket is */ /* needed */ if (idx == 0) { struct bucket *gnu = (struct bucket *) alloc(sizeof(struct bucket)); - gnu->next = id_map; - id_map = gnu; + gnu->next = g.id_map; + g.id_map = gnu; } - id_map->map[idx].gid = gid; - id_map->map[idx].nid = nid; - n_ids_mapped++; + g.id_map->map[idx].gid = gid; + g.id_map->map[idx].nid = nid; + g.n_ids_mapped++; } /* @@ -1269,11 +1262,11 @@ unsigned gid, *nidp; int i; struct bucket *curr; - if (n_ids_mapped) - for (curr = id_map; curr; curr = curr->next) { + if (g.n_ids_mapped) + for (curr = g.id_map; curr; curr = curr->next) { /* first bucket might not be totally full */ - if (curr == id_map) { - i = n_ids_mapped % N_PER_BUCKET; + if (curr == g.id_map) { + i = g.n_ids_mapped % N_PER_BUCKET; if (i == 0) i = N_PER_BUCKET; } else @@ -1623,10 +1616,10 @@ register unsigned int len; return; } else { pline("Read %d instead of %u bytes.", rlen, len); - if (restoring) { + if (g.restoring) { (void) nhclose(fd); (void) delete_savefile(); - error("Error restoring old game."); + error("Error g.restoring old game."); } panic("Error reading level file."); } diff --git a/win/tty/wintty.c b/win/tty/wintty.c index d5cac96c3..523b731ee 100644 --- a/win/tty/wintty.c +++ b/win/tty/wintty.c @@ -3171,7 +3171,6 @@ void tty_cliparound(x, y) int x, y; { - extern boolean restoring; int oldx = clipx, oldy = clipy; if (!clipping) @@ -3191,7 +3190,7 @@ int x, y; clipy = clipymax - (LI - 3); } if (clipx != oldx || clipy != oldy) { - if (on_level(&u.uz0, &u.uz) && !restoring) + if (on_level(&u.uz0, &u.uz) && !g.restoring) (void) doredraw(); } } From 43cc162cf921af7addedff23ab13fb3c14984de8 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 10:47:52 -0800 Subject: [PATCH 23/32] Moved read.c globals to instance_globals. --- include/decl.h | 3 +++ src/decl.c | 3 +++ src/detect.c | 20 +++++++++----------- src/read.c | 38 ++++++++++++++++++-------------------- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/include/decl.h b/include/decl.h index 18d30daea..97ded9560 100644 --- a/include/decl.h +++ b/include/decl.h @@ -464,6 +464,9 @@ struct instance_globals { /* pickup.c */ int oldcap; /* last encumberance */ + /* read.c */ + boolean known; + /* restore.c */ int n_ids_mapped; struct bucket *id_map; diff --git a/src/decl.c b/src/decl.c index c006cc715..f9dceec88 100644 --- a/src/decl.c +++ b/src/decl.c @@ -348,6 +348,9 @@ const struct instance_globals g_init = { /* pickup.c */ 0, /* oldcap */ + /* read.c */ + UNDEFINED_VALUE, /* known */ + /* restore.c */ 0, /* n_ids_mapped */ 0, /* id_map */ diff --git a/src/detect.c b/src/detect.c index 416c2611f..79161aea7 100644 --- a/src/detect.c +++ b/src/detect.c @@ -11,8 +11,6 @@ #include "hack.h" #include "artifact.h" -extern boolean known; /* from read.c */ - STATIC_DCL boolean NDECL(unconstrain_map); STATIC_DCL void NDECL(reconstrain_map); STATIC_DCL void FDECL(browse_map, (int, const char *)); @@ -307,7 +305,7 @@ register struct obj *sobj; boolean stale, ugold = FALSE, steedgold = FALSE; int ter_typ = TER_DETECT | TER_OBJ; - known = stale = clear_stale_map(COIN_CLASS, + g.known = stale = clear_stale_map(COIN_CLASS, (unsigned) (sobj->blessed ? GOLD : 0)); /* look for gold carried by monsters (might be in a container) */ @@ -318,7 +316,7 @@ register struct obj *sobj; if (mtmp == u.usteed) { steedgold = TRUE; } else { - known = TRUE; + g.known = TRUE; goto outgoldmap; /* skip further searching */ } } else { @@ -328,7 +326,7 @@ register struct obj *sobj; if (mtmp == u.usteed) { steedgold = TRUE; } else { - known = TRUE; + g.known = TRUE; goto outgoldmap; /* skip further searching */ } } @@ -338,17 +336,17 @@ register struct obj *sobj; /* look for gold objects */ for (obj = fobj; obj; obj = obj->nobj) { if (sobj->blessed && o_material(obj, GOLD)) { - known = TRUE; + g.known = TRUE; if (obj->ox != u.ux || obj->oy != u.uy) goto outgoldmap; } else if (o_in(obj, COIN_CLASS)) { - known = TRUE; + g.known = TRUE; if (obj->ox != u.ux || obj->oy != u.uy) goto outgoldmap; } } - if (!known) { + if (!g.known) { /* no gold found on floor or monster's inventory. adjust message if you have gold in your inventory */ if (sobj) { @@ -484,7 +482,7 @@ register struct obj *sobj; } if (!ct && !ctu) { - known = stale && !confused; + g.known = stale && !confused; if (stale) { docrt(); You("sense a lack of %s nearby.", what); @@ -512,7 +510,7 @@ register struct obj *sobj; } return !stale; } else if (!ct) { - known = TRUE; + g.known = TRUE; You("%s %s nearby.", sobj ? "smell" : "sense", what); if (sobj && sobj->blessed) { if (!u.uedibility) @@ -523,7 +521,7 @@ register struct obj *sobj; struct obj *temp; int ter_typ = TER_DETECT | TER_OBJ; - known = TRUE; + g.known = TRUE; cls(); (void) unconstrain_map(); for (obj = fobj; obj; obj = obj->nobj) diff --git a/src/read.c b/src/read.c index f6b2c3092..8db74ba52 100644 --- a/src/read.c +++ b/src/read.c @@ -12,8 +12,6 @@ ((mndx) == urace.malenum \ || (urace.femalenum != NON_PM && (mndx) == urace.femalenum)) -boolean known; - static NEARDATA const char readable[] = { ALL_CLASSES, SCROLL_CLASS, SPBOOK_CLASS, 0 }; static const char all_count[] = { ALLOW_COUNT, ALL_CLASSES, 0 }; @@ -189,7 +187,7 @@ doread() register struct obj *scroll; boolean confused, nodisappear; - known = FALSE; + g.known = FALSE; if (check_capacity((char *) 0)) return 0; scroll = getobj(readable, "read"); @@ -396,7 +394,7 @@ doread() } if (!seffects(scroll)) { if (!objects[scroll->otyp].oc_name_known) { - if (known) + if (g.known) learnscroll(scroll); else if (!objects[scroll->otyp].oc_uname) docall(scroll); @@ -801,7 +799,7 @@ int howmuch; if (Sokoban) return; - known = TRUE; + g.known = TRUE; for (zx = 0; zx < COLNO; zx++) for (zy = 0; zy < ROWNO; zy++) if (howmuch & ALL_MAP || rn2(7)) { @@ -1013,7 +1011,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ switch (otyp) { #ifdef MAIL case SCR_MAIL: - known = TRUE; + g.known = TRUE; if (sobj->spe == 2) /* "stamped scroll" created via magic marker--without a stamp */ pline("This scroll is marked \"postage due\"."); @@ -1142,7 +1140,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ if (s) { otmp->spe += s; adj_abon(otmp, s); - known = otmp->known; + g.known = otmp->known; /* update shop bill to reflect new higher price */ if (s > 0 && otmp->unpaid) alter_cost(otmp, 0L); @@ -1184,7 +1182,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ exercise(A_CON, FALSE); break; } else - known = TRUE; + g.known = TRUE; } else { /* armor and scroll both cursed */ pline("%s.", Yobjnam2(otmp, "vibrate")); if (otmp->spe >= -6) { @@ -1264,7 +1262,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ You("don't remember there being any magic words on this scroll."); else pline("This scroll seems to be blank."); - known = TRUE; + g.known = TRUE; break; case SCR_REMOVE_CURSE: case SPE_REMOVE_CURSE: { @@ -1350,7 +1348,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ confused ? &mons[PM_ACID_BLOB] : (struct permonst *) 0, FALSE)) - known = TRUE; + g.known = TRUE; /* no need to flush monsters; we ask for identification only if the * monsters are not visible */ @@ -1430,14 +1428,14 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ vis_results ? "is" : "seems", (results < 0) ? "un" : ""); if (vis_results > 0) - known = TRUE; + g.known = TRUE; } break; } case SCR_GENOCIDE: if (!already_known) You("have found a scroll of genocide!"); - known = TRUE; + g.known = TRUE; if (sblessed) do_class_genocide(); else @@ -1446,11 +1444,11 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ case SCR_LIGHT: if (!confused || rn2(5)) { if (!Blind) - known = TRUE; + g.known = TRUE; litroom(!confused && !scursed, sobj); if (!confused && !scursed) { if (lightdamage(sobj, TRUE, 5)) - known = TRUE; + g.known = TRUE; } } else { /* could be scroll of create monster, don't set known ...*/ @@ -1463,7 +1461,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ if (confused || scursed) { level_tele(); } else { - known = scrolltele(sobj); + g.known = scrolltele(sobj); } break; case SCR_GOLD_DETECTION: @@ -1556,7 +1554,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ cvt_sdoor_to_door(&levl[x][y]); /* do_mapping() already reveals secret passages */ } - known = TRUE; + g.known = TRUE; /*FALLTHRU*/ case SPE_MAGIC_MAPPING: if (level.flags.nommap) { @@ -1576,7 +1574,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ } break; case SCR_AMNESIA: - known = TRUE; + g.known = TRUE; forget((!sblessed ? ALL_SPELLS : 0) | (!confused || scursed ? ALL_MAP : 0)); if (Hallucination) /* Ommmmmm! */ @@ -1657,7 +1655,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ else pline_The("%s rumbles %s you!", ceiling(u.ux, u.uy), sblessed ? "around" : "above"); - known = 1; + g.known = 1; sokoban_guilt(); /* Loop through the surrounding squares */ @@ -1682,7 +1680,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ } break; case SCR_PUNISHMENT: - known = TRUE; + g.known = TRUE; if (confused || sblessed) { You_feel("guilty."); break; @@ -1694,7 +1692,7 @@ struct obj *sobj; /* scroll, or fake spellbook object for scroll-like spell */ if (!already_known) You("have found a scroll of stinking cloud!"); - known = TRUE; + g.known = TRUE; pline("Where do you want to center the %scloud?", already_known ? "stinking " : ""); cc.x = u.ux; From 9177cc642b8538979b775ce687236f371b3fc949 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 11:23:03 -0800 Subject: [PATCH 24/32] Moved potion.c globals to instance_globals. --- include/decl.h | 5 +++++ include/hack.h | 12 +++++----- src/apply.c | 14 +++++------- src/artifact.c | 6 ++--- src/decl.c | 5 +++++ src/dogmove.c | 4 +--- src/dokick.c | 4 +--- src/dothrow.c | 6 ++--- src/mhitm.c | 8 +++---- src/monmove.c | 8 +++---- src/mthrowu.c | 5 ++--- src/potion.c | 61 ++++++++++++++++++++++++-------------------------- src/role.c | 2 +- src/uhitm.c | 6 ++--- src/zap.c | 12 +++++----- 15 files changed, 74 insertions(+), 84 deletions(-) diff --git a/include/decl.h b/include/decl.h index 97ded9560..83ed50095 100644 --- a/include/decl.h +++ b/include/decl.h @@ -464,6 +464,11 @@ struct instance_globals { /* pickup.c */ int oldcap; /* last encumberance */ + /* potion.c */ + boolean notonhead; /* for long worms */ + int potion_nothing; + int potion_unkn; + /* read.c */ boolean known; diff --git a/include/hack.h b/include/hack.h index fd38f5b30..e23401f19 100644 --- a/include/hack.h +++ b/include/hack.h @@ -22,11 +22,13 @@ 0 \ } -/* UNDEFINED_VALUE and UNDEFINED_PTR are used to initialize variables whose - initialized value is not relied upon. UNDEFINED_VALUE can be used to - initialized any value type except pointers. UNDEFINED_PTR can be used - only on pointer types. */ -#define UNDEFINED_VALUE { 0 } +/* The UNDEFINED macros are used to initialize variables whose + initialized value is not relied upon. + UNDEFINED_VALUE: used to initialize any scalar type except pointers. + UNDEFINED_VALUES: used to initialize any non scalar type without pointers. + UNDEFINED_PTR: can be used only on pointer types. */ +#define UNDEFINED_VALUE 0 +#define UNDEFINED_VALUES { 0 } #define UNDEFINED_PTR NULL /* symbolic names for capacity levels */ diff --git a/src/apply.c b/src/apply.c index 63964f398..4c5783cf9 100644 --- a/src/apply.c +++ b/src/apply.c @@ -5,8 +5,6 @@ #include "hack.h" -extern boolean notonhead; /* for long worms */ - STATIC_DCL int FDECL(use_camera, (struct obj *)); STATIC_DCL int FDECL(use_towel, (struct obj *)); STATIC_DCL boolean FDECL(its_dead, (int, int, int *)); @@ -325,7 +323,7 @@ register struct obj *obj; context.stethoscope_movement = youmonst.movement; bhitpos.x = u.ux, bhitpos.y = u.uy; /* tentative, reset below */ - notonhead = u.uswallow; + g.notonhead = u.uswallow; if (u.usteed && u.dz > 0) { if (interference) { pline("%s interferes.", Monnam(u.ustuck)); @@ -374,7 +372,7 @@ register struct obj *obj; /* bhitpos needed by mstatusline() iff mtmp is a long worm */ bhitpos.x = rx, bhitpos.y = ry; - notonhead = (mtmp->mx != rx || mtmp->my != ry); + g.notonhead = (mtmp->mx != rx || mtmp->my != ry); if (mtmp->mundetected) { if (!canspotmon(mtmp)) @@ -884,7 +882,7 @@ struct obj *obj; mtmp = bhit(u.dx, u.dy, COLNO, INVIS_BEAM, (int FDECL((*), (MONST_P, OBJ_P))) 0, (int FDECL((*), (OBJ_P, OBJ_P))) 0, &obj); - if (!mtmp || !haseyes(mtmp->data) || notonhead) + if (!mtmp || !haseyes(mtmp->data) || g.notonhead) return 1; /* couldsee(mtmp->mx, mtmp->my) is implied by the fact that bhit() @@ -972,7 +970,7 @@ struct obj *obj; ; else if ((mtmp->minvis && !perceives(mtmp->data)) /* redundant: can't get here if these are true */ - || !haseyes(mtmp->data) || notonhead || !mtmp->mcansee) + || !haseyes(mtmp->data) || g.notonhead || !mtmp->mcansee) pline("%s doesn't seem to notice %s reflection.", Monnam(mtmp), mhis(mtmp)); else @@ -3034,7 +3032,7 @@ struct obj *obj; return 1; /* burn nutrition; maybe pass out */ context.polearm.hitmon = mtmp; check_caitiff(mtmp); - notonhead = (bhitpos.x != mtmp->mx || bhitpos.y != mtmp->my); + g.notonhead = (bhitpos.x != mtmp->mx || bhitpos.y != mtmp->my); (void) thitmonst(mtmp, uwep); } else if (glyph_is_statue(glyph) /* might be hallucinatory */ && sobj_at(STATUE, bhitpos.x, bhitpos.y)) { @@ -3202,7 +3200,7 @@ struct obj *obj; bhitpos = cc; if ((mtmp = m_at(cc.x, cc.y)) == (struct monst *) 0) break; - notonhead = (bhitpos.x != mtmp->mx || bhitpos.y != mtmp->my); + g.notonhead = (bhitpos.x != mtmp->mx || bhitpos.y != mtmp->my); save_confirm = flags.confirm; if (verysmall(mtmp->data) && !rn2(4) && enexto(&cc, u.ux, u.uy, (struct permonst *) 0)) { diff --git a/src/artifact.c b/src/artifact.c index 698913ede..a9ab09cd3 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -14,8 +14,6 @@ * the contents, just the total size. */ -extern boolean notonhead; /* for long worms */ - #define get_artifact(o) \ (((o) && (o)->oartifact) ? &artilist[(int) (o)->oartifact] : 0) @@ -1254,7 +1252,7 @@ int dieroll; /* needed for Magicbane and vorpal blades */ } if (!youdefend) { /* allow normal cutworm() call to add extra damage */ - if (notonhead) + if (g.notonhead) return FALSE; if (bigmonst(mdef->data)) { @@ -1297,7 +1295,7 @@ int dieroll; /* needed for Magicbane and vorpal blades */ return FALSE; wepdesc = artilist[ART_VORPAL_BLADE].name; if (!youdefend) { - if (!has_head(mdef->data) || notonhead || u.uswallow) { + if (!has_head(mdef->data) || g.notonhead || u.uswallow) { if (youattack) pline("Somehow, you miss %s wildly.", mon_nam(mdef)); else if (vis) diff --git a/src/decl.c b/src/decl.c index f9dceec88..1d0731913 100644 --- a/src/decl.c +++ b/src/decl.c @@ -348,6 +348,11 @@ const struct instance_globals g_init = { /* pickup.c */ 0, /* oldcap */ + /* potion.c */ + FALSE, /* notonhead */ + UNDEFINED_VALUE, /* potion_nothing */ + UNDEFINED_VALUE, /* potion_unkn */ + /* read.c */ UNDEFINED_VALUE, /* known */ diff --git a/src/dogmove.c b/src/dogmove.c index 1c8f4dd79..c67d61e23 100644 --- a/src/dogmove.c +++ b/src/dogmove.c @@ -7,8 +7,6 @@ #include "mfndpos.h" -extern boolean notonhead; - STATIC_DCL boolean FDECL(dog_hunger, (struct monst *, struct edog *)); STATIC_DCL int FDECL(dog_invent, (struct monst *, struct edog *, int)); STATIC_DCL int FDECL(dog_goal, (struct monst *, struct edog *, int, int, int)); @@ -1023,7 +1021,7 @@ int after; /* this is extra fast monster movement */ if (after) return 0; /* hit only once each move */ - notonhead = 0; + g.notonhead = 0; mstatus = mattackm(mtmp, mtmp2); /* aggressor (pet) died */ diff --git a/src/dokick.c b/src/dokick.c index 3cce7395f..3eb9b71a9 100644 --- a/src/dokick.c +++ b/src/dokick.c @@ -14,8 +14,6 @@ static NEARDATA const char *gate_str; /* kickedobj (decl.c) tracks a kicked object until placed or destroyed */ -extern boolean notonhead; /* for long worms */ - STATIC_DCL void FDECL(kickdmg, (struct monst *, BOOLEAN_P)); STATIC_DCL boolean FDECL(maybe_kick_monster, (struct monst *, XCHAR_P, XCHAR_P)); @@ -679,7 +677,7 @@ xchar x, y; if (mon->isshk && kickedobj->where == OBJ_MINVENT && kickedobj->ocarry == mon) return 1; /* alert shk caught it */ - notonhead = (mon->mx != bhitpos.x || mon->my != bhitpos.y); + g.notonhead = (mon->mx != bhitpos.x || mon->my != bhitpos.y); if (isgold ? ghitm(mon, kickedobj) /* caught? */ : thitmonst(mon, kickedobj)) /* hit && used up? */ return 1; diff --git a/src/dothrow.c b/src/dothrow.c index d40857447..f5c67a136 100644 --- a/src/dothrow.c +++ b/src/dothrow.c @@ -28,8 +28,6 @@ static NEARDATA const char bullets[] = { ALLOW_COUNT, COIN_CLASS, ALL_CLASSES, /* thrownobj (decl.c) tracks an object until it lands */ -extern boolean notonhead; /* for long worms */ - /* Throw the selected object, asking for direction */ STATIC_OVL int throw_obj(obj, shotlimit) @@ -1090,7 +1088,7 @@ boolean twoweap; /* used to restore twoweapon mode if wielded weapon returns */ || Hallucination || Fumbling); boolean tethered_weapon = (obj->otyp == AKLYS && (wep_mask & W_WEP) != 0); - notonhead = FALSE; /* reset potentially stale value */ + g.notonhead = FALSE; /* reset potentially stale value */ if ((obj->cursed || obj->greased) && (u.dx || u.dy) && !rn2(7)) { boolean slipok = TRUE; @@ -1263,7 +1261,7 @@ boolean twoweap; /* used to restore twoweapon mode if wielded weapon returns */ return; /* alert shk caught it */ } (void) snuff_candle(obj); - notonhead = (bhitpos.x != mon->mx || bhitpos.y != mon->my); + g.notonhead = (bhitpos.x != mon->mx || bhitpos.y != mon->my); obj_gone = thitmonst(mon, obj); /* Monster may have been tamed; this frees old mon */ mon = m_at(bhitpos.x, bhitpos.y); diff --git a/src/mhitm.c b/src/mhitm.c index 774e9a56e..c9e9e9a08 100644 --- a/src/mhitm.c +++ b/src/mhitm.c @@ -6,8 +6,6 @@ #include "hack.h" #include "artifact.h" -extern boolean notonhead; - static NEARDATA boolean vis, far_noise; static NEARDATA long noisetime; static NEARDATA struct obj *otmp; @@ -161,7 +159,7 @@ register struct monst *mtmp; /* mtmp can be killed */ bhitpos.x = mon->mx; bhitpos.y = mon->my; - notonhead = 0; + g.notonhead = 0; result = mattackm(mtmp, mon); if (result & MM_AGR_DIED) @@ -179,7 +177,7 @@ register struct monst *mtmp; if ((result & MM_HIT) && !(result & MM_DEF_DIED) && rn2(4) && mon->movement >= NORMAL_SPEED) { mon->movement -= NORMAL_SPEED; - notonhead = 0; + g.notonhead = 0; (void) mattackm(mon, mtmp); /* return attack */ } @@ -1334,7 +1332,7 @@ register struct attack *mattk; } break; case AD_DRIN: - if (notonhead || !has_head(pd)) { + if (g.notonhead || !has_head(pd)) { if (vis && canspotmon(mdef)) pline("%s doesn't seem harmed.", Monnam(mdef)); /* Not clear what to do for green slimes */ diff --git a/src/monmove.c b/src/monmove.c index 5694a31b8..459c7957d 100644 --- a/src/monmove.c +++ b/src/monmove.c @@ -7,8 +7,6 @@ #include "mfndpos.h" #include "artifact.h" -extern boolean notonhead; - STATIC_DCL void FDECL(watch_on_duty, (struct monst *)); STATIC_DCL int FDECL(disturb, (struct monst *)); STATIC_DCL void FDECL(release_hero, (struct monst *)); @@ -877,7 +875,7 @@ register int after; */ if ((dist2(mtmp->mx, mtmp->my, tx, ty) < 2) && intruder && (intruder != mtmp)) { - notonhead = (intruder->mx != tx || intruder->my != ty); + g.notonhead = (intruder->mx != tx || intruder->my != ty); if (mattackm(mtmp, intruder) == 2) return 2; mmoved = 1; @@ -1222,7 +1220,7 @@ not_special: int mstatus; mtmp2 = m_at(nix, niy); - notonhead = mtmp2 && (nix != mtmp2->mx || niy != mtmp2->my); + g.notonhead = mtmp2 && (nix != mtmp2->mx || niy != mtmp2->my); /* note: mstatus returns 0 if mtmp2 is nonexistent */ mstatus = mattackm(mtmp, mtmp2); @@ -1232,7 +1230,7 @@ not_special: if ((mstatus & MM_HIT) && !(mstatus & MM_DEF_DIED) && rn2(4) && mtmp2->movement >= NORMAL_SPEED) { mtmp2->movement -= NORMAL_SPEED; - notonhead = 0; + g.notonhead = 0; mstatus = mattackm(mtmp2, mtmp); /* return attack */ if (mstatus & MM_DEF_DIED) return 2; diff --git a/src/mthrowu.c b/src/mthrowu.c index 588a516c8..12b251b54 100644 --- a/src/mthrowu.c +++ b/src/mthrowu.c @@ -26,7 +26,6 @@ STATIC_OVL NEARDATA const char *breathwep[] = { "strange breath #9" }; -extern boolean notonhead; /* for long worms */ STATIC_VAR int mesg_given; /* for m_throw()/thitu() 'miss' message */ /* hero is hit by something other than a monster */ @@ -312,7 +311,7 @@ boolean verbose; /* give message(s) even when you can't see what happened */ int objgone = 1; struct obj *mon_launcher = archer ? MON_WEP(archer) : NULL; - notonhead = (bhitpos.x != mtmp->mx || bhitpos.y != mtmp->my); + g.notonhead = (bhitpos.x != mtmp->mx || bhitpos.y != mtmp->my); ismimic = mtmp->m_ap_type && mtmp->m_ap_type != M_AP_MONSTER; vis = cansee(bhitpos.x, bhitpos.y); @@ -477,7 +476,7 @@ struct obj *obj; /* missile (or stack providing it) */ bhitpos.x = x; bhitpos.y = y; - notonhead = FALSE; /* reset potentially stale value */ + g.notonhead = FALSE; /* reset potentially stale value */ if (obj->quan == 1L) { /* diff --git a/src/potion.c b/src/potion.c index 8985755bb..0f6eca011 100644 --- a/src/potion.c +++ b/src/potion.c @@ -5,9 +5,6 @@ #include "hack.h" -boolean notonhead = FALSE; - -static NEARDATA int nothing, unkn; static NEARDATA const char beverages[] = { POTION_CLASS, 0 }; STATIC_DCL long FDECL(itimeout, (long)); @@ -537,17 +534,17 @@ register struct obj *otmp; int retval; otmp->in_use = TRUE; - nothing = unkn = 0; + g.potion_nothing = g.potion_unkn = 0; if ((retval = peffects(otmp)) >= 0) return retval; - if (nothing) { - unkn++; + if (g.potion_nothing) { + g.potion_unkn++; You("have a %s feeling for a moment, then it passes.", Hallucination ? "normal" : "peculiar"); } if (otmp->dknown && !objects[otmp->otyp].oc_name_known) { - if (!unkn) { + if (!g.potion_unkn) { makeknown(otmp->otyp); more_experienced(0, 10); } else if (!objects[otmp->otyp].oc_uname) @@ -566,7 +563,7 @@ register struct obj *otmp; switch (otmp->otyp) { case POT_RESTORE_ABILITY: case SPE_RESTORE_ABILITY: - unkn++; + g.potion_unkn++; if (otmp->cursed) { pline("Ulch! This makes you feel mediocre!"); break; @@ -606,7 +603,7 @@ register struct obj *otmp; break; case POT_HALLUCINATION: if (Hallucination || Halluc_resistance) - nothing++; + g.potion_nothing++; (void) make_hallucinated(itimeout_incr(HHallucination, rn1(200, 600 - 300 * bcsign(otmp))), TRUE, 0L); @@ -618,7 +615,7 @@ register struct obj *otmp; newuhs(FALSE); break; } - unkn++; + g.potion_unkn++; if (is_undead(youmonst.data) || is_demon(youmonst.data) || u.ualign.type == A_CHAOTIC) { if (otmp->blessed) { @@ -663,7 +660,7 @@ register struct obj *otmp; } break; case POT_BOOZE: - unkn++; + g.potion_unkn++; pline("Ooph! This tastes like %s%s!", otmp->odiluted ? "watered down " : "", Hallucination ? "dandelion wine" : "liquid fire"); @@ -683,7 +680,7 @@ register struct obj *otmp; break; case POT_ENLIGHTENMENT: if (otmp->cursed) { - unkn++; + g.potion_unkn++; You("have an uneasy feeling..."); exercise(A_WIS, FALSE); } else { @@ -707,7 +704,7 @@ register struct obj *otmp; /* FALLTHRU */ case POT_INVISIBILITY: if (Invis || Blind || BInvis) { - nothing++; + g.potion_nothing++; } else { self_invis_message(); } @@ -725,7 +722,7 @@ register struct obj *otmp; case POT_FRUIT_JUICE: { int msg = Invisible && !Blind; - unkn++; + g.potion_unkn++; if (otmp->cursed) pline("Yecch! This tastes %s.", Hallucination ? "overripe" : "rotten"); @@ -755,7 +752,7 @@ register struct obj *otmp; newsym(u.ux, u.uy); /* see yourself! */ if (msg && !Blind) { /* Blind possible if polymorphed */ You("can see through yourself, but you are visible!"); - unkn--; + g.potion_unkn--; } break; } @@ -790,8 +787,8 @@ register struct obj *otmp; int x, y; if (Detect_monsters) - nothing++; - unkn++; + g.potion_nothing++; + g.potion_unkn++; /* after a while, repeated uses become less effective */ if ((HDetect_monsters & TIMEOUT) >= 300L) i = 1; @@ -805,11 +802,11 @@ register struct obj *otmp; newsym(x, y); } if (MON_AT(x, y)) - unkn = 0; + g.potion_unkn = 0; } } see_monsters(); - if (unkn) + if (g.potion_unkn) You_feel("lonely."); break; } @@ -874,11 +871,11 @@ register struct obj *otmp; if (!Confusion) { if (Hallucination) { pline("What a trippy feeling!"); - unkn++; + g.potion_unkn++; } else pline("Huh, What? Where am I?"); } else - nothing++; + g.potion_nothing++; make_confused(itimeout_incr(HConfusion, rn1(7, 16 - 8 * bcsign(otmp))), FALSE); @@ -886,9 +883,9 @@ register struct obj *otmp; case POT_GAIN_ABILITY: if (otmp->cursed) { pline("Ulch! That potion tasted foul!"); - unkn++; + g.potion_unkn++; } else if (Fixed_abil) { - nothing++; + g.potion_nothing++; } else { /* If blessed, increase all; if not, try up to */ int itmp; /* 6 times to find one which can be increased. */ @@ -907,7 +904,7 @@ register struct obj *otmp; /* skip when mounted; heal_legs() would heal steed's legs */ if (Wounded_legs && !otmp->cursed && !u.usteed) { heal_legs(0); - unkn++; + g.potion_unkn++; break; } /* FALLTHRU */ @@ -916,21 +913,21 @@ register struct obj *otmp; You("are suddenly moving %sfaster.", Fast ? "" : "much "); } else { Your("%s get new energy.", makeplural(body_part(LEG))); - unkn++; + g.potion_unkn++; } exercise(A_DEX, TRUE); incr_itimeout(&HFast, rn1(10, 100 + 60 * bcsign(otmp))); break; case POT_BLINDNESS: if (Blind) - nothing++; + g.potion_nothing++; make_blinded(itimeout_incr(Blinded, rn1(200, 250 - 125 * bcsign(otmp))), (boolean) !Blind); break; case POT_GAIN_LEVEL: if (otmp->cursed) { - unkn++; + g.potion_unkn++; /* they went up a level */ if ((ledger_no(&u.uz) == 1 && u.uhave.amulet) || Can_rise_up(u.ux, u.uy, &u.uz)) { @@ -1008,7 +1005,7 @@ register struct obj *otmp; that cursed effect yields "you float down" on next turn. Blessed and uncursed get one extra turn duration. */ } else /* already levitating, or can't levitate */ - nothing++; + g.potion_nothing++; if (otmp->cursed) { /* 'already levitating' used to block the cursed effect(s) @@ -1023,7 +1020,7 @@ register struct obj *otmp; (void) doup(); /* in case we're already Levitating, which would have resulted in incrementing 'nothing' */ - nothing = 0; /* not nothing after all */ + g.potion_nothing = 0; /* not nothing after all */ } else if (has_ceiling(&u.uz)) { int dmg = rnd(!uarmh ? 10 : !is_metallic(uarmh) ? 6 : 3); @@ -1031,7 +1028,7 @@ register struct obj *otmp; ceiling(u.ux, u.uy)); losehp(Maybe_Half_Phys(dmg), "colliding with the ceiling", KILLED_BY); - nothing = 0; /* not nothing after all */ + g.potion_nothing = 0; /* not nothing after all */ } } else if (otmp->blessed) { /* at this point, timeout is already at least 1 */ @@ -1115,7 +1112,7 @@ register struct obj *otmp; } if (Stoned) fix_petrification(); - unkn++; /* holy/unholy water can burn like acid too */ + g.potion_unkn++; /* holy/unholy water can burn like acid too */ break; case POT_POLYMORPH: You_feel("a little %s.", Hallucination ? "normal" : "strange"); @@ -1314,7 +1311,7 @@ int how; FALSE))); } else if (has_head(mon->data)) { Sprintf(buf, "%s %s", s_suffix(mnam), - (notonhead ? "body" : "head")); + (g.notonhead ? "body" : "head")); } else { Strcpy(buf, mnam); } diff --git a/src/role.c b/src/role.c index b5aae5068..6b6348f65 100644 --- a/src/role.c +++ b/src/role.c @@ -769,7 +769,7 @@ const struct Align aligns[] = { static struct { boolean roles[SIZE(roles)]; short mask; -} rfilter = UNDEFINED_VALUE; +} rfilter = UNDEFINED_VALUES; STATIC_DCL int NDECL(randrole_filtered); STATIC_DCL char *FDECL(promptsep, (char *, int)); diff --git a/src/uhitm.c b/src/uhitm.c index e434caacc..728c536e7 100644 --- a/src/uhitm.c +++ b/src/uhitm.c @@ -24,8 +24,6 @@ STATIC_DCL boolean FDECL(hmonas, (struct monst *)); STATIC_DCL void FDECL(nohandglow, (struct monst *)); STATIC_DCL boolean FDECL(shade_aware, (struct obj *)); -extern boolean notonhead; /* for long worms */ - #define PROJECTILE(obj) ((obj) && is_ammo(obj)) void @@ -467,7 +465,7 @@ int dieroll; /* we hit the monster; be careful: it might die or be knocked into a different location */ - notonhead = (mon->mx != x || mon->my != y); + g.notonhead = (mon->mx != x || mon->my != y); malive = hmon(mon, weapon, HMON_MELEE, dieroll); if (malive) { /* monster still alive */ @@ -1808,7 +1806,7 @@ register struct attack *mattk; case AD_DRIN: { struct obj *helmet; - if (notonhead || !has_head(pd)) { + if (g.notonhead || !has_head(pd)) { pline("%s doesn't seem harmed.", Monnam(mdef)); tmp = 0; if (!Unchanging && pd == &mons[PM_GREEN_SLIME]) { diff --git a/src/zap.c b/src/zap.c index cf763280e..b58ca0153 100644 --- a/src/zap.c +++ b/src/zap.c @@ -12,8 +12,6 @@ */ #define MAGIC_COOKIE 1000 -extern boolean notonhead; /* for long worms */ - STATIC_DCL void FDECL(polyuse, (struct obj *, int, int)); STATIC_DCL void FDECL(create_polymon, (struct obj *, int)); STATIC_DCL int FDECL(stone_to_flesh_obj, (struct obj *)); @@ -142,7 +140,7 @@ struct obj *otmp; if (u.uswallow && mtmp == u.ustuck) reveal_invis = FALSE; - notonhead = (mtmp->mx != bhitpos.x || mtmp->my != bhitpos.y); + g.notonhead = (mtmp->mx != bhitpos.x || mtmp->my != bhitpos.y); skilled_spell = (otmp && otmp->oclass == SPBOOK_CLASS && otmp->blessed); switch (otyp) { @@ -479,7 +477,7 @@ struct monst *mtmp; struct obj *otmp; mstatusline(mtmp); - if (notonhead) + if (g.notonhead) return; /* don't show minvent for long worm tail */ if (mtmp->minvent) { @@ -2607,7 +2605,7 @@ struct obj *obj; /* wand or spell */ int steedhit = FALSE; bhitpos.x = u.usteed->mx, bhitpos.y = u.usteed->my; - notonhead = FALSE; + g.notonhead = FALSE; switch (obj->otyp) { /* * Wands that are allowed to hit the steed @@ -3290,7 +3288,7 @@ struct obj **pobj; /* object tossed/used, set to NULL } if (mtmp && !(in_skip && M_IN_WATER(mtmp->data))) { - notonhead = (bhitpos.x != mtmp->mx || bhitpos.y != mtmp->my); + g.notonhead = (bhitpos.x != mtmp->mx || bhitpos.y != mtmp->my); if (weapon == FLASHED_LIGHT) { /* FLASHED_LIGHT hitting invisible monster should pass through instead of stop so we call @@ -4038,7 +4036,7 @@ boolean say; /* Announce out of sight hit/miss events if true */ if (type >= 0) mon->mstrategy &= ~STRAT_WAITMASK; buzzmonst: - notonhead = (mon->mx != bhitpos.x || mon->my != bhitpos.y); + g.notonhead = (mon->mx != bhitpos.x || mon->my != bhitpos.y); if (zap_hit(find_mac(mon), spell_type)) { if (mon_reflects(mon, (char *) 0)) { if (cansee(mon->mx, mon->my)) { From 5e6e184c829b86561932dc0497c198c8cd43a7c9 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 11:27:40 -0800 Subject: [PATCH 25/32] define nhassert if not defined by port. --- include/global.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/global.h b/include/global.h index 154c49ebe..c1f171651 100644 --- a/include/global.h +++ b/include/global.h @@ -364,5 +364,9 @@ struct savefile_info { #define nethack_enter(argc, argv) ((void) 0) #endif +/* Supply nhassert macro if not supplied by port */ +#ifndef nhassert +#define nhassert(expression) ((void)0) +#endif #endif /* GLOBAL_H */ From 302cd789e84c1e607e441658e65ce5b18cf26e3f Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 12:27:55 -0800 Subject: [PATCH 26/32] Fix compiler warnings in macos build. --- include/global.h | 3 +- src/decl.c | 2 +- src/hack.c | 4 +-- src/hacklib.c | 16 +++++------ src/objnam.c | 24 ++++++++-------- src/questpgr.c | 10 +++---- src/role.c | 32 +++++++++++----------- sys/unix/NetHack.xcodeproj/project.pbxproj | 2 +- 8 files changed, 47 insertions(+), 46 deletions(-) diff --git a/include/global.h b/include/global.h index c1f171651..123a96cf3 100644 --- a/include/global.h +++ b/include/global.h @@ -366,7 +366,8 @@ struct savefile_info { /* Supply nhassert macro if not supplied by port */ #ifndef nhassert -#define nhassert(expression) ((void)0) +#define nhassert(e) ((void)0) #endif + #endif /* GLOBAL_H */ diff --git a/src/decl.c b/src/decl.c index 1d0731913..8302d7804 100644 --- a/src/decl.c +++ b/src/decl.c @@ -414,6 +414,6 @@ instance_globals_init() sfcap = default_sfinfo; sfrestinfo = default_sfinfo; sfsaveinfo = default_sfinfo; -}; +} /*decl.c*/ diff --git a/src/hack.c b/src/hack.c index 7a5e48116..7a795c373 100644 --- a/src/hack.c +++ b/src/hack.c @@ -1156,10 +1156,10 @@ int x,y; int tx = u.tx; int ty = u.ty; boolean ret; - int g = glyph_at(x,y); + int glyph = glyph_at(x,y); if (x == u.ux && y == u.uy) return TRUE; - if (isok(x,y) && glyph_is_cmap(g) && S_stone == glyph_to_cmap(g) + if (isok(x,y) && glyph_is_cmap(glyph) && S_stone == glyph_to_cmap(glyph) && !levl[x][y].seenv) return FALSE; u.tx = x; diff --git a/src/hacklib.c b/src/hacklib.c index d0c6915f0..a6facde7b 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -1008,33 +1008,33 @@ char *buf; int k; time_t timeresult = (time_t) 0; struct tm t, *lt; - char *g, *p, y[5], mo[3], md[3], h[3], mi[3], s[3]; + char *d, *p, y[5], mo[3], md[3], h[3], mi[3], s[3]; if (buf && strlen(buf) == 14) { - g = buf; + d = buf; p = y; /* year */ for (k = 0; k < 4; ++k) - *p++ = *g++; + *p++ = *d++; *p = '\0'; p = mo; /* month */ for (k = 0; k < 2; ++k) - *p++ = *g++; + *p++ = *d++; *p = '\0'; p = md; /* day */ for (k = 0; k < 2; ++k) - *p++ = *g++; + *p++ = *d++; *p = '\0'; p = h; /* hour */ for (k = 0; k < 2; ++k) - *p++ = *g++; + *p++ = *d++; *p = '\0'; p = mi; /* minutes */ for (k = 0; k < 2; ++k) - *p++ = *g++; + *p++ = *d++; *p = '\0'; p = s; /* seconds */ for (k = 0; k < 2; ++k) - *p++ = *g++; + *p++ = *d++; *p = '\0'; lt = getlt(); if (lt) { diff --git a/src/objnam.c b/src/objnam.c index 66e09d746..e91f0fb4a 100644 --- a/src/objnam.c +++ b/src/objnam.c @@ -3359,21 +3359,21 @@ retry: } else if (!strcmpi(bp, "looking glass")) { ; /* avoid false hit on "* glass" */ } else if (!BSTRCMPI(bp, p - 6, " glass") || !strcmpi(bp, "glass")) { - register char *g = bp; + register char *s = bp; /* treat "broken glass" as a non-existent item; since "broken" is also a chest/box prefix it might have been stripped off above */ - if (broken || strstri(g, "broken")) + if (broken || strstri(s, "broken")) return (struct obj *) 0; - if (!strncmpi(g, "worthless ", 10)) - g += 10; - if (!strncmpi(g, "piece of ", 9)) - g += 9; - if (!strncmpi(g, "colored ", 8)) - g += 8; - else if (!strncmpi(g, "coloured ", 9)) - g += 9; - if (!strcmpi(g, "glass")) { /* choose random color */ + if (!strncmpi(s, "worthless ", 10)) + s += 10; + if (!strncmpi(s, "piece of ", 9)) + s += 9; + if (!strncmpi(s, "colored ", 8)) + s += 8; + else if (!strncmpi(s, "coloured ", 9)) + s += 9; + if (!strcmpi(s, "glass")) { /* choose random color */ /* 9 different kinds */ typ = LAST_GEM + rnd(9); if (objects[typ].oc_class == GEM_CLASS) @@ -3384,7 +3384,7 @@ retry: char tbuf[BUFSZ]; Strcpy(tbuf, "worthless piece of "); - Strcat(tbuf, g); /* assume it starts with the color */ + Strcat(tbuf, s); /* assume it starts with the color */ Strcpy(bp, tbuf); } } diff --git a/src/questpgr.c b/src/questpgr.c index cf6fbdd38..ede98b1bc 100644 --- a/src/questpgr.c +++ b/src/questpgr.c @@ -282,7 +282,7 @@ char who, /* 'd' => deity, 'l' => leader, 'n' => nemesis, 'o' => artifact */ which; /* 'h'|'H'|'i'|'I'|'j'|'J' */ { const char *pnoun; - int g; + int godgend; char lwhich = lowc(which); /* H,I,J -> h,i,j */ /* @@ -298,13 +298,13 @@ char who, /* 'd' => deity, 'l' => leader, 'n' => nemesis, 'o' => artifact */ : (lwhich == 'i') ? "them" : (lwhich == 'j') ? "their" : "?"; } else { - g = (who == 'd') ? quest_status.godgend + godgend = (who == 'd') ? quest_status.godgend : (who == 'l') ? quest_status.ldrgend : (who == 'n') ? quest_status.nemgend : 2; /* default to neuter */ - pnoun = (lwhich == 'h') ? genders[g].he - : (lwhich == 'i') ? genders[g].him - : (lwhich == 'j') ? genders[g].his : "?"; + pnoun = (lwhich == 'h') ? genders[godgend].he + : (lwhich == 'i') ? genders[godgend].him + : (lwhich == 'j') ? genders[godgend].his : "?"; } Strcpy(cvt_buf, pnoun); /* capitalize for H,I,J */ diff --git a/src/role.c b/src/role.c index 6b6348f65..31b512349 100644 --- a/src/role.c +++ b/src/role.c @@ -769,7 +769,7 @@ const struct Align aligns[] = { static struct { boolean roles[SIZE(roles)]; short mask; -} rfilter = UNDEFINED_VALUES; +} rfilter = { UNDEFINED_VALUES, UNDEFINED_VALUE}; STATIC_DCL int NDECL(randrole_filtered); STATIC_DCL char *FDECL(promptsep, (char *, int)); @@ -1757,11 +1757,11 @@ winid where; not_yet[] = " not yet specified", rand_choice[] = " random"; char buf[BUFSZ]; - int r, c, g, a, allowmask; + int r, c, gend, a, allowmask; r = flags.initrole; c = flags.initrace; - g = flags.initgend; + gend = flags.initgend; a = flags.initalign; if (r >= 0) { allowmask = roles[r].allow; @@ -1770,9 +1770,9 @@ winid where; else if (c >= 0 && !(allowmask & ROLE_RACEMASK & races[c].allow)) c = ROLE_RANDOM; if ((allowmask & ROLE_GENDMASK) == ROLE_MALE) - g = 0; /* role forces male (hypothetical) */ + gend = 0; /* role forces male (hypothetical) */ else if ((allowmask & ROLE_GENDMASK) == ROLE_FEMALE) - g = 1; /* role forces female (valkyrie) */ + gend = 1; /* role forces female (valkyrie) */ if ((allowmask & ROLE_ALIGNMASK) == AM_LAWFUL) a = 0; /* aligns[lawful] */ else if ((allowmask & ROLE_ALIGNMASK) == AM_NEUTRAL) @@ -1804,10 +1804,10 @@ winid where; : roles[r].name.m); if (r >= 0 && roles[r].name.f) { /* distinct female name [caveman/cavewoman, priest/priestess] */ - if (g == 1) + if (gend == 1) /* female specified; replace male role name with female one */ Sprintf(index(buf, ':'), ": %s", roles[r].name.f); - else if (g < 0) + else if (gend < 0) /* gender unspecified; append slash and female role name */ Sprintf(eos(buf), "/%s", roles[r].name.f); } @@ -1820,11 +1820,11 @@ winid where; : races[c].noun); putstr(where, 0, buf); Sprintf(buf, "%12s ", "gender:"); - Strcat(buf, (which == RS_GENDER) ? choosing : (g == ROLE_NONE) + Strcat(buf, (which == RS_GENDER) ? choosing : (gend == ROLE_NONE) ? not_yet - : (g == ROLE_RANDOM) + : (gend == ROLE_RANDOM) ? rand_choice - : genders[g].adj); + : genders[gend].adj); putstr(where, 0, buf); Sprintf(buf, "%12s ", "alignment:"); Strcat(buf, (which == RS_ALGNMNT) ? choosing : (a == ROLE_NONE) @@ -1852,7 +1852,7 @@ boolean preselect; anything any; char buf[BUFSZ]; const char *what = 0, *constrainer = 0, *forcedvalue = 0; - int f = 0, r, c, g, a, i, allowmask; + int f = 0, r, c, gend, a, i, allowmask; r = flags.initrole; c = flags.initrace; @@ -1894,16 +1894,16 @@ boolean preselect; case RS_GENDER: what = "gender"; f = flags.initgend; - g = ROLE_NONE; + gend = ROLE_NONE; if (r >= 0) { allowmask = roles[r].allow & ROLE_GENDMASK; if (allowmask == ROLE_MALE) - g = 0; /* genders[male] */ + gend = 0; /* genders[male] */ else if (allowmask == ROLE_FEMALE) - g = 1; /* genders[female] */ - if (g >= 0) { + gend = 1; /* genders[female] */ + if (gend >= 0) { constrainer = "role"; - forcedvalue = genders[g].adj; + forcedvalue = genders[gend].adj; } else if (f >= 0 && (allowmask & ~rfilter.mask) == genders[f].allow) { /* if there is only one gender choice available due to user diff --git a/sys/unix/NetHack.xcodeproj/project.pbxproj b/sys/unix/NetHack.xcodeproj/project.pbxproj index 4a0d53ae5..dcb402a98 100644 --- a/sys/unix/NetHack.xcodeproj/project.pbxproj +++ b/sys/unix/NetHack.xcodeproj/project.pbxproj @@ -810,6 +810,7 @@ 3186A3B121A4B0FD0052BF02 /* bitmfile.h */, 3186A36E21A4B0FA0052BF02 /* botl.h */, 3186A38721A4B0FB0052BF02 /* color.h */, + 31B8A30A21A20D730055BD01 /* config.h */, 3186A3C621A4B0FE0052BF02 /* config1.h */, 3186A37121A4B0FA0052BF02 /* context.h */, 3186A3C521A4B0FE0052BF02 /* coord.h */, @@ -901,7 +902,6 @@ 3186A36D21A4B0F90052BF02 /* xwindowp.h */, 3186A3BF21A4B0FD0052BF02 /* you.h */, 3186A3A721A4B0FD0052BF02 /* youprop.h */, - 31B8A30A21A20D730055BD01 /* config.h */, ); name = include; sourceTree = ""; From 97feda46e00b2cb4b849506544332c7b8baadbe6 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 12:43:38 -0800 Subject: [PATCH 27/32] Moved sex_change_ok to instance_globals. --- include/decl.h | 5 +++++ src/decl.c | 3 +++ src/polyself.c | 12 ++++-------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/decl.h b/include/decl.h index 83ed50095..b41eab5df 100644 --- a/include/decl.h +++ b/include/decl.h @@ -464,6 +464,11 @@ struct instance_globals { /* pickup.c */ int oldcap; /* last encumberance */ + /* polyself.c */ + int sex_change_ok; /* controls whether taking on new form or becoming new + man can also change sex (ought to be an arg to + polymon() and newman() instead) */ + /* potion.c */ boolean notonhead; /* for long worms */ int potion_nothing; diff --git a/src/decl.c b/src/decl.c index 8302d7804..d9db42982 100644 --- a/src/decl.c +++ b/src/decl.c @@ -348,6 +348,9 @@ const struct instance_globals g_init = { /* pickup.c */ 0, /* oldcap */ + /* polyself.c */ + 0, /* sex_change_ok */ + /* potion.c */ FALSE, /* notonhead */ UNDEFINED_VALUE, /* potion_nothing */ diff --git a/src/polyself.c b/src/polyself.c index e136d1898..15fe3d3ce 100644 --- a/src/polyself.c +++ b/src/polyself.c @@ -33,10 +33,6 @@ STATIC_DCL void NDECL(polysense); STATIC_VAR const char no_longer_petrify_resistant[] = "No longer petrify-resistant, you"; -/* controls whether taking on new form or becoming new man can also - change sex (ought to be an arg to polymon() and newman() instead) */ -STATIC_VAR int sex_change_ok = 0; - /* update the youmonst.data structure pointer and intrinsics */ void set_uasmon() @@ -296,7 +292,7 @@ newman() u.ulevelmax = newlvl; u.ulevel = newlvl; - if (sex_change_ok && !rn2(10)) + if (g.sex_change_ok && !rn2(10)) change_sex(); adjabil(oldlvl, (int) u.ulevel); @@ -573,14 +569,14 @@ int psflags; /* The below polyok() fails either if everything is genocided, or if * we deliberately chose something illegal to force newman(). */ - sex_change_ok++; + g.sex_change_ok++; if (!polyok(&mons[mntmp]) || (!forcecontrol && !rn2(5)) || your_race(&mons[mntmp])) { newman(); } else { (void) polymon(mntmp); } - sex_change_ok--; /* reset */ + g.sex_change_ok--; /* reset */ made_change: new_light = emits_light(youmonst.data); @@ -650,7 +646,7 @@ int mntmp; if (!flags.female) dochange = TRUE; } else if (!is_neuter(&mons[mntmp]) && mntmp != u.ulycn) { - if (sex_change_ok && !rn2(10)) + if (g.sex_change_ok && !rn2(10)) dochange = TRUE; } From 3394362f822e0fde954477528290e01834812ced Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 13:13:28 -0800 Subject: [PATCH 28/32] topl.c globals moved to instance_globals. --- include/decl.h | 8 ++++++++ src/cmd.c | 5 ++--- src/decl.c | 8 ++++++++ src/end.c | 6 ++---- src/pline.c | 38 ++++++++++++++++---------------------- win/tty/topl.c | 5 +---- 6 files changed, 37 insertions(+), 33 deletions(-) diff --git a/include/decl.h b/include/decl.h index b41eab5df..ce9fe2d81 100644 --- a/include/decl.h +++ b/include/decl.h @@ -464,6 +464,14 @@ struct instance_globals { /* pickup.c */ int oldcap; /* last encumberance */ + /* pline.c */ + unsigned pline_flags; + char prevmsg[BUFSZ]; +#ifdef DUMPLOG + unsigned saved_pline_index; /* slot in saved_plines[] to use next */ + char *saved_plines[DUMPLOG_MSG_COUNT]; +#endif + /* polyself.c */ int sex_change_ok; /* controls whether taking on new form or becoming new man can also change sex (ought to be an arg to diff --git a/src/cmd.c b/src/cmd.c index eba20ccd3..dfe217335 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -5771,8 +5771,7 @@ char def; { char res, qbuf[QBUFSZ]; #ifdef DUMPLOG - extern unsigned saved_pline_index; /* pline.c */ - unsigned idx = saved_pline_index; + unsigned idx = g.saved_pline_index; /* buffer to hold query+space+formatted_single_char_response */ char dumplog_buf[QBUFSZ + 1 + 15]; /* [QBUFSZ+1+7] should suffice */ #endif @@ -5789,7 +5788,7 @@ char def; } res = (*windowprocs.win_yn_function)(query, resp, def); #ifdef DUMPLOG - if (idx == saved_pline_index) { + if (idx == g.saved_pline_index) { /* when idx is still the same as saved_pline_index, the interface didn't put the prompt into saved_plines[]; we put a simplified version in there now (without response choices or default) */ diff --git a/src/decl.c b/src/decl.c index d9db42982..7a407a9e2 100644 --- a/src/decl.c +++ b/src/decl.c @@ -348,6 +348,14 @@ const struct instance_globals g_init = { /* pickup.c */ 0, /* oldcap */ + /* pline.c */ + 0, /* pline_flags */ + UNDEFINED_VALUES, /* prevmsg */ +#ifdef DUMPLOG + 0, /* saved_pline_index */ + UNDEFINED_VALUES, +#endif + /* polyself.c */ 0, /* sex_change_ok */ diff --git a/src/end.c b/src/end.c index a433acc07..c24835001 100644 --- a/src/end.c +++ b/src/end.c @@ -679,14 +679,12 @@ dump_plines() { int i, j; char buf[BUFSZ], **strp; - extern char *saved_plines[]; - extern unsigned saved_pline_index; Strcpy(buf, " "); /* one space for indentation */ putstr(0, 0, "Latest messages:"); - for (i = 0, j = (int) saved_pline_index; i < DUMPLOG_MSG_COUNT; + for (i = 0, j = (int) g.saved_pline_index; i < DUMPLOG_MSG_COUNT; ++i, j = (j + 1) % DUMPLOG_MSG_COUNT) { - strp = &saved_plines[j]; + strp = &g.saved_plines[j]; if (*strp) { copynchars(&buf[1], *strp, BUFSZ - 1 - 1); putstr(0, 0, buf); diff --git a/src/pline.c b/src/pline.c index ebde8b145..d7acef096 100644 --- a/src/pline.c +++ b/src/pline.c @@ -6,18 +6,12 @@ #define NEED_VARARGS /* Uses ... */ /* comment line for pre-compiled headers */ #include "hack.h" -static unsigned pline_flags = 0; -static char prevmsg[BUFSZ]; - static char *FDECL(You_buf, (int)); #if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__)) static void FDECL(execplinehandler, (const char *)); #endif #ifdef DUMPLOG -/* also used in end.c */ -unsigned saved_pline_index = 0; /* slot in saved_plines[] to use next */ -char *saved_plines[DUMPLOG_MSG_COUNT] = { (char *) 0 }; /* keep the most recent DUMPLOG_MSG_COUNT messages */ void @@ -31,8 +25,8 @@ const char *line; * The core should take responsibility for that and have * this share it. */ - unsigned indx = saved_pline_index; /* next slot to use */ - char *oldest = saved_plines[indx]; /* current content of that slot */ + unsigned indx = g.saved_pline_index; /* next slot to use */ + char *oldest = g.saved_plines[indx]; /* current content of that slot */ if (oldest && strlen(oldest) >= strlen(line)) { /* this buffer will gradually shrink until the 'else' is needed; @@ -41,9 +35,9 @@ const char *line; } else { if (oldest) free((genericptr_t) oldest); - saved_plines[indx] = dupstr(line); + g.saved_plines[indx] = dupstr(line); } - saved_pline_index = (indx + 1) % DUMPLOG_MSG_COUNT; + g.saved_pline_index = (indx + 1) % DUMPLOG_MSG_COUNT; } /* called during save (unlike the interface-specific message history, @@ -55,9 +49,9 @@ dumplogfreemessages() unsigned indx; for (indx = 0; indx < DUMPLOG_MSG_COUNT; ++indx) - if (saved_plines[indx]) - free((genericptr_t) saved_plines[indx]), saved_plines[indx] = 0; - saved_pline_index = 0; + if (g.saved_plines[indx]) + free((genericptr_t) g.saved_plines[indx]), g.saved_plines[indx] = 0; + g.saved_pline_index = 0; } #endif @@ -139,7 +133,7 @@ VA_DECL(const char *, line) * Unfortunately, that means Norep() isn't honored (general issue) and * that short lines aren't combined into one longer one (tty behavior). */ - if ((pline_flags & SUPPRESS_HISTORY) == 0) + if ((g.pline_flags & SUPPRESS_HISTORY) == 0) dumplogmsg(line); #endif /* use raw_print() if we're called too early (or perhaps too late @@ -153,11 +147,11 @@ VA_DECL(const char *, line) } msgtyp = MSGTYP_NORMAL; - no_repeat = (pline_flags & PLINE_NOREPEAT) ? TRUE : FALSE; - if ((pline_flags & OVERRIDE_MSGTYPE) == 0) { + no_repeat = (g.pline_flags & PLINE_NOREPEAT) ? TRUE : FALSE; + if ((g.pline_flags & OVERRIDE_MSGTYPE) == 0) { msgtyp = msgtype_type(line, no_repeat); if (msgtyp == MSGTYP_NOSHOW - || (msgtyp == MSGTYP_NOREP && !strcmp(line, prevmsg))) + || (msgtyp == MSGTYP_NOREP && !strcmp(line, g.prevmsg))) /* FIXME: we need a way to tell our caller that this message * was suppressed so that caller doesn't set iflags.last_msg * for something that hasn't been shown, otherwise a subsequent @@ -181,7 +175,7 @@ VA_DECL(const char *, line) /* this gets cleared after every pline message */ iflags.last_msg = PLNMSG_UNKNOWN; - (void) strncpy(prevmsg, line, BUFSZ), prevmsg[BUFSZ - 1] = '\0'; + (void) strncpy(g.prevmsg, line, BUFSZ), g.prevmsg[BUFSZ - 1] = '\0'; if (msgtyp == MSGTYP_STOP) display_nhwindow(WIN_MESSAGE, TRUE); /* --more-- */ @@ -205,9 +199,9 @@ VA_DECL2(unsigned, pflags, const char *, line) { VA_START(line); VA_INIT(line, const char *); - pline_flags = pflags; + g.pline_flags = pflags; vpline(line, VA_ARGS); - pline_flags = 0; + g.pline_flags = 0; VA_END(); return; } @@ -218,9 +212,9 @@ VA_DECL(const char *, line) { VA_START(line); VA_INIT(line, const char *); - pline_flags = PLINE_NOREPEAT; + g.pline_flags = PLINE_NOREPEAT; vpline(line, VA_ARGS); - pline_flags = 0; + g.pline_flags = 0; VA_END(); return; } diff --git a/win/tty/topl.c b/win/tty/topl.c index 5233e1525..d0fadfb3e 100644 --- a/win/tty/topl.c +++ b/win/tty/topl.c @@ -648,9 +648,6 @@ boolean restoring_msghist; { static boolean initd = FALSE; int idx; -#ifdef DUMPLOG - extern unsigned saved_pline_index; /* pline.c */ -#endif if (restoring_msghist && !initd) { /* we're restoring history from the previous session, but new @@ -662,7 +659,7 @@ boolean restoring_msghist; initd = TRUE; #ifdef DUMPLOG /* this suffices; there's no need to scrub saved_pline[] pointers */ - saved_pline_index = 0; + g.saved_pline_index = 0; #endif } From 3bb5c84bc78fca285e6f4d1590b915901ca433cb Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 13:30:32 -0800 Subject: [PATCH 29/32] Fix g.restoring comments. --- src/restore.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/restore.c b/src/restore.c index ff14135f0..ca8696982 100644 --- a/src/restore.c +++ b/src/restore.c @@ -58,7 +58,7 @@ static struct restore_procs { /* * Save a mapping of IDs from ghost levels to the current level. This - * map is used by the timer routines when g.restoring ghost levels. + * map is used by the timer routines when restoring ghost levels. */ #define N_PER_BUCKET 64 struct bucket { @@ -176,7 +176,7 @@ boolean ghostly; Strcpy(damaged_shops, in_rooms(tmp_dam->place.x, tmp_dam->place.y, SHOPBASE)); if (u.uz.dlevel) { - /* when g.restoring, there are two passes over the current + /* when restoring, there are two passes over the current * level. the first time, u.uz isn't set, so neither is * shop_keeper(). just wait and process the damage on * the second pass. @@ -209,7 +209,7 @@ struct obj *otmp; mread(fd, (genericptr_t) otmp, sizeof(struct obj)); /* next object pointers are invalid; otmp->cobj needs to be left - as is--being non-null is key to g.restoring container contents */ + as is--being non-null is key to restoring container contents */ otmp->nobj = otmp->nexthere = (struct obj *) 0; /* non-null oextra needs to be reconstructed */ if (otmp->oextra) { @@ -1018,7 +1018,7 @@ boolean ghostly; setmode(fd, O_BINARY); #endif /* Load the old fruit info. We have to do it first, so the - * information is available when g.restoring the objects. + * information is available when restoring the objects. */ if (ghostly) g.oldfruit = loadfruitchn(fd); @@ -1619,7 +1619,7 @@ register unsigned int len; if (g.restoring) { (void) nhclose(fd); (void) delete_savefile(); - error("Error g.restoring old game."); + error("Error restoring old game."); } panic("Error reading level file."); } From 72490a9cacee561f21af1ff57f8ad3097612f290 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 13:30:51 -0800 Subject: [PATCH 30/32] Moved pickup globals to instance_globals. --- include/decl.h | 5 ++ src/decl.c | 2 + src/pickup.c | 151 ++++++++++++++++++++++++------------------------- 3 files changed, 80 insertions(+), 78 deletions(-) diff --git a/include/decl.h b/include/decl.h index ce9fe2d81..3359542ef 100644 --- a/include/decl.h +++ b/include/decl.h @@ -463,6 +463,11 @@ struct instance_globals { /* pickup.c */ int oldcap; /* last encumberance */ + /* current_container is set in use_container(), to be used by the + callback routines in_container() and out_container() from askchain() + and use_container(). Also used by menu_loot() and container_gone(). */ + struct obj *current_container; + boolean abort_looting; /* pline.c */ unsigned pline_flags; diff --git a/src/decl.c b/src/decl.c index 7a407a9e2..0fb2cd1d1 100644 --- a/src/decl.c +++ b/src/decl.c @@ -347,6 +347,8 @@ const struct instance_globals g_init = { /* pickup.c */ 0, /* oldcap */ + UNDEFINED_PTR, /* current_container */ + UNDEFINED_VALUE, /* abort_looting */ /* pline.c */ 0, /* pline_flags */ diff --git a/src/pickup.c b/src/pickup.c index b008b0f8f..9b154ebc8 100644 --- a/src/pickup.c +++ b/src/pickup.c @@ -62,12 +62,7 @@ STATIC_DCL void FDECL(tipcontainer, (struct obj *)); /* if you can figure this out, give yourself a hearty pat on the back... */ #define GOLD_CAPACITY(w, n) (((w) * -100L) - ((n) + 50L) - 1L) -/* A variable set in use_container(), to be used by the callback routines - in_container() and out_container() from askchain() and use_container(). - Also used by menu_loot() and container_gone(). */ -static NEARDATA struct obj *current_container; -static NEARDATA boolean abort_looting; -#define Icebox (current_container->otyp == ICE_BOX) +#define Icebox (g.current_container->otyp == ICE_BOX) static const char moderateloadmsg[] = "You have a little trouble lifting", @@ -1695,7 +1690,7 @@ int cindex, ccount; /* index of this container (1..N), number of them (N) */ tmp = rnd(10); losehp(Maybe_Half_Phys(tmp), "carnivorous bag", KILLED_BY_AN); makeknown(BAG_OF_TRICKS); - abort_looting = TRUE; + g.abort_looting = TRUE; return 1; } @@ -1720,7 +1715,7 @@ doloot() boolean prev_loot = FALSE; int num_conts = 0; - abort_looting = FALSE; + g.abort_looting = FALSE; if (check_capacity((char *) 0)) { /* "Can't do that while carrying so much stuff." */ @@ -1791,7 +1786,7 @@ doloot() for (i = 1; i <= n; i++) { cobj = pick_list[i - 1].item.a_obj; timepassed |= do_loot_cont(&cobj, i, n); - if (abort_looting) { + if (g.abort_looting) { /* chest trap or magic bag explosion or */ free((genericptr_t) pick_list); return timepassed; @@ -1816,7 +1811,7 @@ doloot() anyfound = TRUE; timepassed |= do_loot_cont(&cobj, 1, 1); - if (abort_looting) + if (g.abort_looting) /* chest trap or magic bag explosion or */ return timepassed; } @@ -2076,17 +2071,17 @@ STATIC_PTR int in_container(obj) register struct obj *obj; { - boolean floor_container = !carried(current_container); + boolean floor_container = !carried(g.current_container); boolean was_unpaid = FALSE; char buf[BUFSZ]; - if (!current_container) { + if (!g.current_container) { impossible(" no current_container?"); return 0; } else if (obj == uball || obj == uchain) { You("must be kidding."); return 0; - } else if (obj == current_container) { + } else if (obj == g.current_container) { pline("That would be an interesting topological exercise."); return 0; } else if (obj->owornmask & (W_ARMOR | W_ACCESSORY)) { @@ -2141,7 +2136,7 @@ register struct obj *obj; * of evaluation of the parameters is undefined. */ Strcpy(buf, the(xname(obj))); - You("cannot fit %s into %s.", buf, the(xname(current_container))); + You("cannot fit %s into %s.", buf, the(xname(g.current_container))); return 0; } @@ -2157,7 +2152,7 @@ register struct obj *obj; was_unpaid = obj->unpaid ? TRUE : FALSE; /* don't sell when putting the item into your own container, * but handle billing correctly */ - sellobj_state(current_container->no_charge + sellobj_state(g.current_container->no_charge ? SELL_DONTSELL : SELL_DELIBERATE); sellobj(obj, u.ux, u.uy); sellobj_state(SELL_NORMAL); @@ -2174,7 +2169,7 @@ register struct obj *obj; if (rot_alarm) obj->norevive = 1; } - } else if (Is_mbag(current_container) && mbag_explodes(obj, 0)) { + } else if (Is_mbag(g.current_container) && mbag_explodes(obj, 0)) { /* explicitly mention what item is triggering the explosion */ pline("As you put %s inside, you are blasted by a magical explosion!", doname(obj)); @@ -2182,34 +2177,34 @@ register struct obj *obj; if (was_unpaid) addtobill(obj, FALSE, FALSE, TRUE); obfree(obj, (struct obj *) 0); - delete_contents(current_container); + delete_contents(g.current_container); if (!floor_container) - useup(current_container); - else if (obj_here(current_container, u.ux, u.uy)) - useupf(current_container, current_container->quan); + useup(g.current_container); + else if (obj_here(g.current_container, u.ux, u.uy)) + useupf(g.current_container, g.current_container->quan); else panic("in_container: bag not found."); losehp(d(6, 6), "magical explosion", KILLED_BY_AN); - current_container = 0; /* baggone = TRUE; */ + g.current_container = 0; /* baggone = TRUE; */ } - if (current_container) { - Strcpy(buf, the(xname(current_container))); + if (g.current_container) { + Strcpy(buf, the(xname(g.current_container))); You("put %s into %s.", doname(obj), buf); /* gold in container always needs to be added to credit */ if (floor_container && obj->oclass == COIN_CLASS) - sellobj(obj, current_container->ox, current_container->oy); - (void) add_to_container(current_container, obj); - current_container->owt = weight(current_container); + sellobj(obj, g.current_container->ox, g.current_container->oy); + (void) add_to_container(g.current_container, obj); + g.current_container->owt = weight(g.current_container); } /* gold needs this, and freeinv() many lines above may cause * the encumbrance to disappear from the status, so just always * update status immediately. */ bot(); - return (current_container ? 1 : -1); + return (g.current_container ? 1 : -1); } /* askchain() filter used by in_container(); @@ -2221,7 +2216,7 @@ int ck_bag(obj) struct obj *obj; { - return (current_container && obj != current_container); + return (g.current_container && obj != g.current_container); } /* Returns: -1 to stop, 1 item was removed, 0 item was not removed. */ @@ -2234,7 +2229,7 @@ register struct obj *obj; int res, loadlev; long count; - if (!current_container) { + if (!g.current_container) { impossible(" no current_container?"); return -1; } else if (is_gold) { @@ -2248,7 +2243,7 @@ register struct obj *obj; return -1; count = obj->quan; - if ((res = lift_object(obj, current_container, &count, FALSE)) <= 0) + if ((res = lift_object(obj, g.current_container, &count, FALSE)) <= 0) return res; if (obj->quan != count && obj->otyp != LOADSTONE) @@ -2256,15 +2251,15 @@ register struct obj *obj; /* Remove the object from the list. */ obj_extract_self(obj); - current_container->owt = weight(current_container); + g.current_container->owt = weight(g.current_container); if (Icebox) removed_from_icebox(obj); - if (!obj->unpaid && !carried(current_container) - && costly_spot(current_container->ox, current_container->oy)) { - obj->ox = current_container->ox; - obj->oy = current_container->oy; + if (!obj->unpaid && !carried(g.current_container) + && costly_spot(g.current_container->ox, g.current_container->oy)) { + obj->ox = g.current_container->ox; + obj->oy = g.current_container->oy; addtobill(obj, FALSE, FALSE, FALSE); } if (is_pick(obj)) @@ -2391,7 +2386,7 @@ int FDECL((*fn), (OBJ_P)); { /* result is only meaningful while use_container() is executing */ return ((fn == in_container || fn == out_container) - && !current_container); + && !g.current_container); } STATIC_OVL void @@ -2455,7 +2450,7 @@ boolean more_containers; /* True iff #loot multiple and this isn't last one */ int used = 0; long loss; - abort_looting = FALSE; + g.abort_looting = FALSE; emptymsg[0] = '\0'; if (!u_handsy()) @@ -2478,37 +2473,37 @@ boolean more_containers; /* True iff #loot multiple and this isn't last one */ multi_reason = "opening a container"; nomovemsg = ""; } - abort_looting = TRUE; + g.abort_looting = TRUE; return 1; } obj->lknown = 1; - current_container = obj; /* for use by in/out_container */ + g.current_container = obj; /* for use by in/out_container */ /* * From here on out, all early returns go through 'containerdone:'. */ /* check for Schroedinger's Cat */ - quantum_cat = SchroedingersBox(current_container); + quantum_cat = SchroedingersBox(g.current_container); if (quantum_cat) { - observe_quantum_cat(current_container, TRUE, TRUE); + observe_quantum_cat(g.current_container, TRUE, TRUE); used = 1; } - cursed_mbag = Is_mbag(current_container) - && current_container->cursed - && Has_contents(current_container); + cursed_mbag = Is_mbag(g.current_container) + && g.current_container->cursed + && Has_contents(g.current_container); if (cursed_mbag - && (loss = boh_loss(current_container, held)) != 0) { + && (loss = boh_loss(g.current_container, held)) != 0) { used = 1; You("owe %ld %s for lost merchandise.", loss, currency(loss)); - current_container->owt = weight(current_container); + g.current_container->owt = weight(g.current_container); } inokay = (invent != 0 - && !(invent == current_container && !current_container->nobj)); - outokay = Has_contents(current_container); + && !(invent == g.current_container && !g.current_container->nobj)); + outokay = Has_contents(g.current_container); if (!outokay) /* preformat the empty-container message */ - Sprintf(emptymsg, "%s is %sempty.", Ysimple_name2(current_container), + Sprintf(emptymsg, "%s is %sempty.", Ysimple_name2(g.current_container), (quantum_cat || cursed_mbag) ? "now " : ""); /* @@ -2538,13 +2533,13 @@ boolean more_containers; /* True iff #loot multiple and this isn't last one */ * is empty. Do what with it? [:irs nq or ?] */ for (;;) { /* repeats iff '?' or ":' gets chosen */ - outmaybe = (outokay || !current_container->cknown); + outmaybe = (outokay || !g.current_container->cknown); if (!outmaybe) (void) safe_qbuf(qbuf, (char *) 0, " is empty. Do what with it?", - current_container, Yname2, Ysimple_name2, + g.current_container, Yname2, Ysimple_name2, "This"); else - (void) safe_qbuf(qbuf, "Do what with ", "?", current_container, + (void) safe_qbuf(qbuf, "Do what with ", "?", g.current_container, yname, ysimple_name, "it"); /* ask player about what to do with this container */ if (flags.menu_style == MENU_PARTIAL @@ -2554,7 +2549,7 @@ boolean more_containers; /* True iff #loot multiple and this isn't last one */ trying to do both will yield proper feedback */ c = 'b'; } else { - c = in_or_out_menu(qbuf, current_container, outmaybe, inokay, + c = in_or_out_menu(qbuf, g.current_container, outmaybe, inokay, (boolean) (used != 0), more_containers); } } else { /* TRADITIONAL or COMBINATION */ @@ -2581,15 +2576,15 @@ boolean more_containers; /* True iff #loot multiple and this isn't last one */ if (c == '?') { explain_container_prompt(more_containers); } else if (c == ':') { /* note: will set obj->cknown */ - if (!current_container->cknown) + if (!g.current_container->cknown) used = 1; /* gaining info */ - container_contents(current_container, FALSE, FALSE, TRUE); + container_contents(g.current_container, FALSE, FALSE, TRUE); } else break; } /* loop until something other than '?' or ':' is picked */ if (c == 'q') - abort_looting = TRUE; + g.abort_looting = TRUE; if (c == 'n' || c == 'q') /* [not strictly needed; falling thru works] */ goto containerdone; loot_out = (c == 'o' || c == 'b' || c == 'r'); @@ -2599,11 +2594,11 @@ boolean more_containers; /* True iff #loot multiple and this isn't last one */ /* out-only or out before in */ if (loot_out && !loot_in_first) { - if (!Has_contents(current_container)) { + if (!Has_contents(g.current_container)) { pline1(emptymsg); /* is empty. */ - if (!current_container->cknown) + if (!g.current_container->cknown) used = 1; - current_container->cknown = 1; + g.current_container->cknown = 1; } else { add_valid_menu_class(0); /* reset */ if (flags.menu_style == MENU_TRADITIONAL) @@ -2615,7 +2610,7 @@ boolean more_containers; /* True iff #loot multiple and this isn't last one */ } if ((loot_in || stash_one) - && (!invent || (invent == current_container && !invent->nobj))) { + && (!invent || (invent == g.current_container && !invent->nobj))) { You("don't have anything%s to %s.", invent ? " else" : "", stash_one ? "stash" : "put in"); loot_in = stash_one = FALSE; @@ -2645,16 +2640,16 @@ boolean more_containers; /* True iff #loot multiple and this isn't last one */ } } /* putting something in might have triggered magic bag explosion */ - if (!current_container) + if (!g.current_container) loot_out = FALSE; /* out after in */ if (loot_out && loot_in_first) { - if (!Has_contents(current_container)) { + if (!Has_contents(g.current_container)) { pline1(emptymsg); /* is empty. */ - if (!current_container->cknown) + if (!g.current_container->cknown) used = 1; - current_container->cknown = 1; + g.current_container->cknown = 1; } else { add_valid_menu_class(0); /* reset */ if (flags.menu_style == MENU_TRADITIONAL) @@ -2671,16 +2666,16 @@ containerdone: whatever was already inside, now we suddenly do. That can't be helped unless we want to track things item by item and then deal with containers whose contents are "partly known". */ - if (current_container) - current_container->cknown = 1; + if (g.current_container) + g.current_container->cknown = 1; update_inventory(); } - *objp = current_container; /* might have become null */ - if (current_container) - current_container = 0; /* avoid hanging on to stale pointer */ + *objp = g.current_container; /* might have become null */ + if (g.current_container) + g.current_container = 0; /* avoid hanging on to stale pointer */ else - abort_looting = TRUE; + g.abort_looting = TRUE; return used; } @@ -2703,7 +2698,7 @@ boolean put_in; checkfunc = ck_bag; } else { action = "take out"; - objlist = &(current_container->cobj); + objlist = &(g.current_container->cobj); actionfunc = out_container; checkfunc = (int FDECL((*), (OBJ_P))) 0; } @@ -2740,7 +2735,7 @@ boolean put_in; all_categories = FALSE; Sprintf(buf, "%s what type of objects?", action); mflags = (ALL_TYPES | UNPAID_TYPES | BUCX_TYPES | CHOOSE_ALL); - n = query_category(buf, put_in ? invent : current_container->cobj, + n = query_category(buf, put_in ? invent : g.current_container->cobj, mflags, &pick_list, PICK_ANY); if (!n) return 0; @@ -2757,8 +2752,8 @@ boolean put_in; if (loot_everything) { if (!put_in) { - current_container->cknown = 1; - for (otmp = current_container->cobj; otmp; otmp = otmp2) { + g.current_container->cknown = 1; + for (otmp = g.current_container->cobj; otmp; otmp = otmp2) { otmp2 = otmp->nobj; res = out_container(otmp); if (res < 0) @@ -2766,7 +2761,7 @@ boolean put_in; n_looted += res; } } else { - for (otmp = invent; otmp && current_container; otmp = otmp2) { + for (otmp = invent; otmp && g.current_container; otmp = otmp2) { otmp2 = otmp->nobj; res = in_container(otmp); if (res < 0) @@ -2779,9 +2774,9 @@ boolean put_in; if (put_in && flags.invlet_constant) mflags |= USE_INVLET; if (!put_in) - current_container->cknown = 1; + g.current_container->cknown = 1; Sprintf(buf, "%s what?", action); - n = query_objlist(buf, put_in ? &invent : &(current_container->cobj), + n = query_objlist(buf, put_in ? &invent : &(g.current_container->cobj), mflags, &pick_list, PICK_ANY, all_categories ? allow_all : allow_category); if (n) { @@ -2795,7 +2790,7 @@ boolean put_in; } res = put_in ? in_container(otmp) : out_container(otmp); if (res < 0) { - if (!current_container) { + if (!g.current_container) { /* otmp caused current_container to explode; both are now gone */ otmp = 0; /* and break loop */ From 88fc8b79db20a15d01cf1f09bee60f3bb30828b7 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 13:37:30 -0800 Subject: [PATCH 31/32] objnam.c globals moved to instance globals. --- include/decl.h | 7 +++++++ src/decl.c | 3 +++ src/objnam.c | 11 +++-------- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/decl.h b/include/decl.h index 3359542ef..50f015fd5 100644 --- a/include/decl.h +++ b/include/decl.h @@ -461,6 +461,13 @@ struct instance_globals { /* muse.c */ boolean m_using; /* kludge to use mondided instead of killed */ + /* objname.c */ + /* distantname used by distant_name() to pass extra information to + xname_flags(); it would be much cleaner if this were a parameter, + but that would require all of the xname() and doname() calls to be + modified */ + int distantname; + /* pickup.c */ int oldcap; /* last encumberance */ /* current_container is set in use_container(), to be used by the diff --git a/src/decl.c b/src/decl.c index 0fb2cd1d1..fde96e40b 100644 --- a/src/decl.c +++ b/src/decl.c @@ -345,6 +345,9 @@ const struct instance_globals g_init = { /* mused.c */ FALSE, /* m_using */ + /* objname.c */ + 0, /* distantname */ + /* pickup.c */ 0, /* oldcap */ UNDEFINED_PTR, /* current_container */ diff --git a/src/objnam.c b/src/objnam.c index e91f0fb4a..fe9a77c29 100644 --- a/src/objnam.c +++ b/src/objnam.c @@ -210,11 +210,6 @@ struct obj *obj; return TRUE; } -/* used by distant_name() to pass extra information to xname_flags(); - it would be much cleaner if this were a parameter, but that would - require all of the xname() and doname() calls to be modified */ -static int distantname = 0; - /* Give the name of an object seen at a distance. Unlike xname/doname, * we don't want to set dknown if it's not set already. */ @@ -233,9 +228,9 @@ char *FDECL((*func), (OBJ_P)); * object is within X-ray radius and only treat it as distant when * beyond that radius. Logic is iffy but result might be interesting. */ - ++distantname; + ++g.distantname; str = (*func)(obj); - --distantname; + --g.distantname; return str; } @@ -425,7 +420,7 @@ unsigned cxn_flags; /* bitmask of CXN_xxx values */ */ if (!nn && ocl->oc_uses_known && ocl->oc_unique) obj->known = 0; - if (!Blind && !distantname) + if (!Blind && !g.distantname) obj->dknown = TRUE; if (Role_if(PM_PRIEST)) obj->bknown = TRUE; From c9f390a9dbe9862c9e5fc0fea9edb4f257e5a650 Mon Sep 17 00:00:00 2001 From: Bart House Date: Fri, 23 Nov 2018 15:13:22 -0800 Subject: [PATCH 32/32] Modified objects initialization to support re-entry. --- include/decl.h | 2 +- include/extern.h | 4 ++-- src/allmain.c | 2 -- src/decl.c | 9 +-------- src/objects.c | 13 +++++++------ src/sys.c | 3 +++ sys/share/pcmain.c | 1 - sys/winnt/stubs.c | 3 +-- util/lev_main.c | 10 ++++++---- util/makedefs.c | 5 +++-- win/share/tile2bmp.c | 2 ++ win/share/tilemap.c | 1 - win/win32/winhack.c | 1 - 13 files changed, 26 insertions(+), 30 deletions(-) diff --git a/include/decl.h b/include/decl.h index 50f015fd5..5c7356c5d 100644 --- a/include/decl.h +++ b/include/decl.h @@ -545,7 +545,7 @@ struct instance_globals { E struct instance_globals g; -E void instance_globals_init(); +E void decl_globals_init(); #undef E diff --git a/include/extern.h b/include/extern.h index 89910b0bb..deb6941e7 100644 --- a/include/extern.h +++ b/include/extern.h @@ -250,7 +250,7 @@ E void FDECL(destroy_drawbridge, (int, int)); /* ### decl.c ### */ -E void NDECL(decl_init); +E void NDECL(decl_globals_init); /* ### detect.c ### */ @@ -1651,7 +1651,7 @@ E void NDECL(rename_disco); /* ### objects.c ### */ -E void NDECL(objects_init); +E void NDECL(objects_globals_init); /* ### objnam.c ### */ diff --git a/src/allmain.c b/src/allmain.c index 9aa2f93c4..41e59d157 100644 --- a/src/allmain.c +++ b/src/allmain.c @@ -33,9 +33,7 @@ boolean resuming; /* Note: these initializers don't do anything except guarantee that we're linked properly. */ - decl_init(); monst_init(); - objects_init(); /* if a save file created in normal mode is now being restored in explore mode, treat it as normal restore followed by 'X' command diff --git a/src/decl.c b/src/decl.c index fde96e40b..f6c3524be 100644 --- a/src/decl.c +++ b/src/decl.c @@ -318,13 +318,6 @@ const char *ARGV0; /* support for lint.h */ unsigned nhUse_dummy = 0; -/* dummy routine used to force linkage */ -void -decl_init() -{ - return; -} - #define IVMAGIC 0xdeadbeef const struct instance_globals g_init = { @@ -420,7 +413,7 @@ const struct instance_globals g_init = { struct instance_globals g; void -instance_globals_init() +decl_globals_init() { g = g_init; diff --git a/src/objects.c b/src/objects.c index fcca3b0f7..984a67e3e 100644 --- a/src/objects.c +++ b/src/objects.c @@ -61,7 +61,7 @@ struct monst { struct monst *dummy; }; /* lint: struct obj's union */ cost,sdam,ldam,oc1,oc2,nut,color) { obj } #define None (char *) 0 /* less visual distraction for 'no description' */ -NEARDATA struct objdescr obj_descr[] = +NEARDATA struct objdescr obj_descr_init[] = #else /* second pass -- object definitions */ #define BITS(nmkn,mrg,uskn,ctnr,mgc,chrg,uniq,nwsh,big,tuf,dir,sub,mtrl) \ @@ -75,7 +75,7 @@ NEARDATA struct objdescr obj_descr[] = #define HARDGEM(n) (0) #endif -NEARDATA struct objclass objects[] = +NEARDATA struct objclass obj_init[] = #endif { /* dummy object[0] -- description [2nd arg] *must* be NULL */ @@ -1169,13 +1169,14 @@ OBJECT(OBJ(None, None), /* clang-format on */ /* *INDENT-ON* */ -void NDECL(objects_init); +struct objdescr obj_descr[SIZE(obj_descr_init)]; +struct objclass objects[SIZE(obj_init)]; -/* dummy routine used to force linkage */ void -objects_init() +objects_globals_init() { - return; + memcpy(obj_descr, obj_descr_init, sizeof(obj_descr)); + memcpy(objects, obj_init, sizeof(objects)); } #endif /* !OBJECTS_PASS_2_ */ diff --git a/src/sys.c b/src/sys.c index 6adc8f71f..51b966ff2 100644 --- a/src/sys.c +++ b/src/sys.c @@ -20,6 +20,9 @@ struct sysopt sysopt; void sys_early_init() { + decl_globals_init(); + objects_globals_init(); + sysopt.support = (char *) 0; sysopt.recover = (char *) 0; #ifdef SYSCF diff --git a/sys/share/pcmain.c b/sys/share/pcmain.c index ce95d3fdf..da516713a 100644 --- a/sys/share/pcmain.c +++ b/sys/share/pcmain.c @@ -97,7 +97,6 @@ char *argv[]; nethack_enter(argc, argv); - instance_globals_init(); sys_early_init(); #if defined(WIN32) && defined(TTY_GRAPHICS) diff --git a/sys/winnt/stubs.c b/sys/winnt/stubs.c index 9e16c6dce..9241864b1 100644 --- a/sys/winnt/stubs.c +++ b/sys/winnt/stubs.c @@ -37,8 +37,7 @@ char *argv[]; { boolean resuming; - icontext_init(); - sys_early_init(); + early_init(); Strcpy(default_window_sys, "tty"); resuming = pcmain(argc, argv); moveloop(resuming); diff --git a/util/lev_main.c b/util/lev_main.c index 729549791..e1b946489 100644 --- a/util/lev_main.c +++ b/util/lev_main.c @@ -109,8 +109,7 @@ struct lc_vardefs *FDECL(vardef_defined, (struct lc_vardefs *, char *, int)); void FDECL(splev_add_from, (sp_lev *, sp_lev *)); extern void NDECL(monst_init); -extern void NDECL(objects_init); -extern void NDECL(decl_init); +extern void NDECL(objects_globals_init); void FDECL(add_opcode, (sp_lev *, int, genericptr_t)); @@ -256,12 +255,15 @@ char **argv; argc = SIZE(mac_argv); argv = mac_argv; #endif + + decl_globals_init(); + objects_globals_init(); + /* Note: these initializers don't do anything except guarantee that * we're linked properly. */ monst_init(); - objects_init(); - decl_init(); + /* this one does something... */ init_obj_classes(); diff --git a/util/makedefs.c b/util/makedefs.c index 0b97a8bcb..7504ca7b0 100644 --- a/util/makedefs.c +++ b/util/makedefs.c @@ -164,7 +164,7 @@ void NDECL(do_oracles); void NDECL(do_vision); extern void NDECL(monst_init); /* monst.c */ -extern void NDECL(objects_init); /* objects.c */ +extern void NDECL(objects_globals_init); /* objects.c */ static void NDECL(link_sanity_check); static char *FDECL(name_file, (const char *, const char *)); @@ -296,7 +296,6 @@ link_sanity_check() we're linked properly. */ monst_init(); - objects_init(); } @@ -306,6 +305,8 @@ char *options; { boolean more_than_one; + objects_globals_init(); + link_sanity_check(); /* construct the current version number */ diff --git a/win/share/tile2bmp.c b/win/share/tile2bmp.c index 6722bbc26..8fb16e425 100644 --- a/win/share/tile2bmp.c +++ b/win/share/tile2bmp.c @@ -203,6 +203,8 @@ char *argv[]; } #endif + objects_globals_init(); + tilecount = 0; xoffset = yoffset = 0; initflag = 0; diff --git a/win/share/tilemap.c b/win/share/tilemap.c index f859f44e8..1e59381b7 100644 --- a/win/share/tilemap.c +++ b/win/share/tilemap.c @@ -113,7 +113,6 @@ int set, entry; we're linked properly. */ monst_init(); - objects_init(); (void) def_char_to_objclass(']'); condnum = tilenum = 0; diff --git a/win/win32/winhack.c b/win/win32/winhack.c index 43bca5a5d..9dcbb4e4b 100644 --- a/win/win32/winhack.c +++ b/win/win32/winhack.c @@ -97,7 +97,6 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, win10_init(); - instance_globals_init(); sys_early_init(); /* init applicatio structure */