Commit Graph

147 Commits

Author SHA1 Message Date
nhmall
588b3ae92f replace some leading tabs that had crept in 2024-06-10 10:57:59 -04:00
PatR
9ba0cf2ff0 fix memory leaks for #quit while in tutorial
The new change to reset discoveries and monster-stats when exiting
the tutorial used dynamic data which wouldn't be freed if player
used #quit and declined to resume the regular game.

It turns out that such a leak was already present for start-of-game
inventory that gets stashed away during the tutorial.

In both cases, it could happen at most once per game so wasn't a big
deal as far as memory leaks go.
2024-03-21 15:28:13 -07:00
PatR
a79d6d8e63 partly fix issue #1134 - tutorial leaks obj info
Reported by AndrioCelos, a handful of objects in the tutorial can
be discovered via use, and such discoveries were carrying over to
normal play when the tutorial ended.

This causes the hero to forget such discoveries.  The player will
still be able to remember them.  The proper fix would be to discard
the initialized but not-yet-started game when entering the tutorial
and then start a whole new one when exiting so that saving and
restoring game state would become unnecessary.  This doesn't do that.

This also causes monster birth and death statistics to be reset when
exiting the tutorial.  Affects the #vanquished command and potentially
extinctionist play.

Closes #1134
2024-03-21 08:32:58 -07:00
Pasi Kallinen
8e83fcb1cc Fix returning from tutorial
Removing these two values from the stack seems to prevent calling
the leave_tutorial hook.
2024-03-20 22:44:12 +02:00
nhkeni
54c3dd35ac Merge branch 'keni-staticfn' into NetHack-3.7 2024-03-16 09:38:21 -04:00
nhmall
79648c6ce2 some variables not referenced in another translation unit made static
Also adds some cross-refence comments for some variables that are
referenced in another translation unit.
2024-03-15 16:00:14 -04:00
nhkeni
9c0ed8ae63 NOSTATICFN for src/* 2024-03-14 17:41:51 -04:00
nhkeni
acf60063d5 Add missing prototypes for static functions to avoid warnings. 2024-02-29 10:49:53 -05:00
nhmall
688ac6ffbe remove register from variable declarations 2024-02-19 16:30:07 -05:00
nhkeni
aa8aa24d3b git glue memory leaks
- fix stack imbalances
- call panic() instead of nhl_error() if L==0 to prevent immediate crash
- drop unused argument prep in get_nh_lua_variables
- change nhl_pcall_handle to clean up stack after calling impossible()
- don't force full garbage collection runs
- don't collect garbage to do memory usage checking
- remove unnecessary stack resets
2024-02-04 22:07:05 -05:00
Pasi Kallinen
0d7fc06eb9 Do garbage collection on luacore
Another lua state that is not freed until end of game,
so clean up the stack and do GC regularly.
2024-02-03 12:55:10 +02:00
Pasi Kallinen
e7a7526ea9 Minor code formatting 2024-02-03 10:56:16 +02:00
nhkeni
6f47094b8d Add a sanity check to nhl_init() 2024-01-21 12:44:05 -05:00
nhkeni
bacfa6ef40 More lua setup cleanup
Stop (poorly) tracking the amount of memory in use and get
 it from Lua instead.
Simplify nhl_alloc.
2024-01-17 15:04:40 -05:00
Pasi Kallinen
5d28e24477 Some lua state allocator fixes
Run GC on the themeroom lua states, as they're not freed
until end of game.

Allocate the exact amount of data we use instead of padding it.

Free our state data after closing the lua state; doing it
the other way is the way to madness, which was kept at bay
by the padded allocation amount.
2024-01-17 17:41:44 +02:00
PatR
4609f23332 lua timer fix
Commit 7a533a911c about three weeks
ago reordered a couple of timers.  The list of timers in nhlua.c was
overlooked at the time.

The theme room "Ice room" was affected.  I'm surprised that sanity
checking while running the fuzzer didn't notice and complain.
2024-01-12 11:31:48 -08:00
nhmall
6274ad3259 whitespace adjustment src/nhlua.c
I noticed some tabs had crept into the file, so adjust the whitespace.
2024-01-12 12:33:26 -05:00
nhkeni
aebc666efa clean up lua error message, catch failed lua_newstate call 2024-01-12 11:06:25 -05:00
nhkeni
2c26176de8 blind attempt to satisfy MS-DOS compile 2024-01-05 18:23:55 -05:00
nhkeni
0745d3fbe5 Free user data when destroying a lua state. 2024-01-05 17:26:18 -05:00
nhkeni
c7ab9a0565 Some lua catchup and cleanup
- add nhl_pcall_handle() to wrap all nhl_pcall calls that didn't check
  return value and either panic() or impossible()
- add --loglua (unix only) to dump Lua memory and steps info to livelog
- remove old logging
- set memory and step limits on all Lua VMs
2024-01-04 10:37:38 -05:00
nhmall
b368d4fbe9 revert a nonnull instance that deviated from the stated rules 2023-12-15 00:52:15 -05:00
nhmall
6467b983eb part2: remediate some nonnull-related compiler warnings
do.c:296:16: warning: nonnull parameter 'obj' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]
  296 |         while (obj && (otmp = obj_nexto_xy(obj, x, y, TRUE)) != 0) {
      |                ^~~ ~~
../include/extern.h:538:43: note: declared 'nonnull' here
  538 |                             const char *) NONNULLPTRS;
      |                                           ^
../include/tradstdc.h:378:36: note: expanded from macro 'NONNULLPTRS'
  378 | #define NONNULLPTRS __attribute__((nonnull))
      |                                    ^
1 warning generated.

invent.c:807:12: warning: nonnull parameter 'objlist' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]
  807 |     while (objlist) {
      |     ~~~~~  ^~~~~~~
../include/extern.h:1230:61: note: declared 'nonnull' here
 1230 | extern struct obj *merge_choice(struct obj *, struct obj *) NONNULLPTRS;
      |                                                             ^
../include/tradstdc.h:378:36: note: expanded from macro 'NONNULLPTRS'
  378 | #define NONNULLPTRS __attribute__((nonnull))
      |                                    ^
1 warning generated.

monmove.c:2091:23: warning: nonnull parameter 'mtmp' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]
 2091 |     boolean is_pet = (mtmp && mtmp->mtame && !mtmp->isminion);
      |                       ^~~~ ~~
../include/extern.h:1844:67: note: declared 'nonnull' here
 1844 | extern boolean undesirable_disp(struct monst *, coordxy, coordxy) NONNULLARG1;
      |                                                                   ^
../include/tradstdc.h:379:36: note: expanded from macro 'NONNULLARG1'
  379 | #define NONNULLARG1 __attribute__((nonnull (1)))
      |                                    ^
1 warning generated.

nhlua.c:2095:9: warning: nonnull parameter 'L' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]
 2095 |     if (L)
      |     ~~  ^
../include/extern.h:1985:35: note: declared 'nonnull' here
 1985 | extern void nhl_done(lua_State *) NONNULLARG1;
      |                                   ^
../include/tradstdc.h:379:36: note: expanded from macro 'NONNULLARG1'
  379 | #define NONNULLARG1 __attribute__((nonnull (1)))
      |                                    ^
1 warning generated.

steal.c:59:12: warning: nonnull parameter 'chain' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]
   59 |     while (chain && chain->otyp != GOLD_PIECE)
      |            ^~~~~ ~~
../include/extern.h:2910:43: note: declared 'nonnull' here
 2910 | extern struct obj *findgold(struct obj *) NONNULLARG1;
      |                                           ^
../include/tradstdc.h:379:36: note: expanded from macro 'NONNULLARG1'
  379 | #define NONNULLARG1 __attribute__((nonnull (1)))
      |                                    ^
1 warning generated.

utf8map.c:232:9: warning: nonnull parameter 'gmap' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]
  232 |     if (gmap) {
      |     ~~  ^~~~
../include/extern.h:3318:28: note: declared 'nonnull' here
 3318 |               long ucolor) NONNULLPTRS;
      |                            ^
../include/tradstdc.h:378:36: note: expanded from macro 'NONNULLPTRS'
  378 | #define NONNULLPTRS __attribute__((nonnull))
      |                                    ^
1 warning generated.

worn.c:895:15: warning: nonnull parameter 'objchain' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]
  895 |     if (on && objchain)
      |            ~~ ^~~~~~~~
../include/extern.h:3664:51: note: declared 'nonnull' here
 3664 | extern void bypass_objlist(struct obj *, boolean) NONNULLARG1;
      |                                                   ^
../include/tradstdc.h:379:36: note: expanded from macro 'NONNULLARG1'
  379 | #define NONNULLARG1 __attribute__((nonnull (1)))
      |                                    ^
worn.c:897:12: warning: nonnull parameter 'objchain' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]
  897 |     while (objchain) {
      |     ~~~~~  ^~~~~~~~
../include/extern.h:3664:51: note: declared 'nonnull' here
 3664 | extern void bypass_objlist(struct obj *, boolean) NONNULLARG1;
      |                                                   ^
../include/tradstdc.h:379:36: note: expanded from macro 'NONNULLARG1'
  379 | #define NONNULLARG1 __attribute__((nonnull (1)))
      |                                    ^
worn.c:908:12: warning: nonnull parameter 'objchain' will evaluate to 'true' on first encounter [-Wpointer-bool-conversion]
  908 |     while (objchain) {
      |     ~~~~~  ^~~~~~~~
../include/extern.h:3665:53: note: declared 'nonnull' here
 3665 | extern struct obj *nxt_unbypassed_obj(struct obj *) NONNULLARG1;
      |                                                     ^
../include/tradstdc.h:379:36: note: expanded from macro 'NONNULLARG1'
  379 | #define NONNULLARG1 __attribute__((nonnull (1)))
      |                                    ^
3 warnings generated.
2023-12-14 20:06:57 -05:00
nhmall
1e927a4228 Merge branch 'fix-nhlua' of https://github.com/argrath/NetHack into NetHack-3.7 2023-12-09 21:44:21 -05:00
PatR
b1f67a9842 lua memory usage
Silence a bunch of complaints from heaputil about freeing Null
pointers.  The C standard allows that but it makes the data collected
about mallocs and frees not match up when nethack has MONITOR_HEAP
enabled.
2023-12-07 11:48:05 -08:00
SHIRAKATA Kentaro
027b325a3d remove unnecessary null-check on get_table_mapchr_opt()
`name` here is always non-null, otherwise it leads segv at earlier code.
2023-12-06 11:23:10 +09:00
nhmall
d064ac2cda more cast style consistency 2023-11-13 20:31:02 -05:00
Pasi Kallinen
dd5ca5b058 Change menu_headings to accept color and attribute
Instead of just accepting an attribute, it's now possible to
use a color, or both color and attribute, for example:

OPTIONS=menu_headings:inverse
OPTIONS=menu_headings:red
OPTIONS=menu_headings:red&underline

Default is still just inverse.
This lets the player change the menu heading color without
needing to use menu colors for them.

Also makes it so the core uses NO_COLOR instead of 0, for all
the menu lines which don't have any prefedefined color.

Tested for tty, curses, x11, qt, and win32
2023-11-13 07:33:56 +02:00
Pasi Kallinen
70c47c4054 Simplify add_menu, part 6 2023-11-03 21:48:46 +02:00
nhmall
e4c3d87a4c fix a small memory allocation that was never freed 2023-09-23 10:16:07 -04:00
PatR
dd05f5183e addinv_nomerge()
Replace several instances of
 obj->nomerge = 1;
 addinv(obj);
 obj->nomerge = 0;
with new
 addinv_nomerge(obj);
and add various related comments.
2023-09-19 14:40:33 -07:00
PatR
3fdcd5f4e0 fix #K4006 - 'null obj after quiver merge' panic
Using apply to unlight a lit potion of oil makes it unlit, removes
it from inventory, and then re-adds it to try to force it to merge
with other potions of oil.  If it was wielded and the other potions
were quivered, the game would panic.  When merging, they get forced
into the weapon slot in preference to the quiver slot.

Unwearing it before freeinv+addinv would solve this but also leave
the hero with nothing wielded, even if it didn't merge with another
stack.  Instead, don't try to merge if the potion being unlit happens
to be worn.

3.6.x was subject to this too and the fix is small+isolated but the
situation is so uncommon that I haven't bothered backporting it.

Applying a lump of royal jelly and then not picking anything to rub
it on had a similar problem.  It also panicked if the applied lump
was wielded and other lumps were quivered.  The fix is different
because the stack it gets split from during apply is known.  This one
doesn't impact 3.6.x; applying jelly to eggs wasn't implemented yet.
2023-09-19 07:16:00 -07:00
nhmall
079ea3a449 get build working on Windows following cd9f145d
The code prior to cd9f145d would have returned 0 on an argc other
than 2 or 3 so this just does the same.
2023-06-24 13:18:16 -04:00
PatR
cd9f145dba fix leaving the tutorial levels
While running the tutorial, the Save command is disabled.  When the
tutorial was extended to two levels, stashing and restoring the
hero's equipment stopped working as intended if player entered the
second level.  The attempted fix for that broke re-enabling Save
even if the player left the tutorial without entering its second
level.

This seems to fix things, but I'm flailing around with barely a clue
here.  A couple of simpler attempts didn't work and I haven't figured
out why, so this is a bit more complex than what I wanted.

Reorganizing nhl_callback() isn't part of the fix, just avoids use
of some redundant code.
2023-06-23 15:19:36 -07:00
PatR
2bbfed2183 fix github issue #1046 - tutorial anomalies
Reported by Noisytoot:  going from level tut-1 to tut-2 returned the
hero's starting equipment too soon, and exiting the tutorial from
tut-2 let the hero keep any equipment acquired within the tutorial.

Entering and leaving the tutorial was being handled by lua code in
the level description of tut-1 and adding a second level messed that
up.  I didn't see any way of handing that with level-specific lua
code so I made it become the core's responsibility.  gotolevel()
knows when the hero is moving from one dungeon branch to another so
it can recognize entry to or exit from the tutorial easily.

While fixing this, prevent #invoke of the Eye of the Aethiopica from
offering the tutorial as a candidate destination (was feasible if it
had been entered at start of game).

Not fixed:  levels visited in the tutorial become part of #overview.

Show location as "Tutorial:1" instead of "Dlvl:1" on status lines.
Only tested with tty; some interfaces handle location themselves and
may need their own fixup for this.

Fixes #1046
2023-06-03 16:39:12 -07:00
nhmall
b35d8a34ed update Lua version supported 2023-05-28 22:58:47 -04:00
Pasi Kallinen
cb73cbedb7 Tutorial: backup and restore struct you 2023-04-09 18:15:31 +03:00
SHIRAKATA Kentaro
3b567dd74e prevent free()ing static buffer on save_luadata() 2023-04-06 02:17:31 -07:00
Pasi Kallinen
2a7be0b050 Tutorial: hunger and eating food
Reset hunger when exiting the tutorial.
Add an optional second parameter to lua pline, forcing a more-prompt.
2023-03-18 14:08:13 +02:00
Pasi Kallinen
0b5956ba34 Tutorial: reset inventory letters 2023-03-05 17:54:14 +02:00
Pasi Kallinen
fc7a32b86e Tutorial level
Add a tutorial level to teach commands to new players.
Very much a WIP.

Breaks save and bones compat.
2023-03-01 14:00:29 +02:00
Pasi Kallinen
5d659cf1f6 Tips and option to disable them
Adds a more general way to handle gameplay tips, and adds
a boolean option "tips", which can be used to disable all
tips.  Adds a helpful longer message when the game goes
into the "farlook" mode.

Also adds a lua binding to easily show multi-line text
in a menu window.

Breaks save compat.
2023-02-19 15:56:18 +02:00
Pasi Kallinen
7401b44fa1 Walls of lava
Add "walls of lava", basically lava which blocks vision and
require a bit more than just levitation or flight to move through.

No levels use this yet, as testing isn't thorough enough.
2023-02-06 19:23:42 +02:00
nhkeni
31d5fecbb2 leave a note on a potential future Lua sandbox issue 2023-02-04 09:50:49 -05:00
nhmall
77d5f048a4 place a comment to identify the unreachable code 2023-01-20 20:41:03 -05:00
nhmall
cbb2830012 re-suppress the three warnings that reappeared
src/nhlua.c(1118) : warning C4702: unreachable code
src/nhlua.c(1045) : warning C4702: unreachable code
src/nhlua.c(1107) : warning C4702: unreachable code
2023-01-20 20:32:36 -05:00
PatR
0f257ec538 new static analyzer fix - nhlua.c
Cope with get_nh_lua_variable() possibly returning Null.

Either or both of the DISABLE_WARNING_UNREACHABLE_CODE and
RESTORE_WARNING_UNREACHABLE_CODE in the vicinity looked misplaced so
I took them out.  They may need to be added back in.
2023-01-20 17:18:27 -08:00
Pasi Kallinen
1113373892 Fix vibrating square
The Gehennom changes broke the vibrating square, allowing hero to go
down into the Sanctum via stairs without performing the invocation.

Fix this by making the hellfill lua check for invocation level, and
placing down the vibrating square trap, instead of stairs.
2023-01-19 12:15:46 +02:00
nhmall
b82a19ec02 suppress a new warning 2023-01-15 15:39:22 -05:00
Pasi Kallinen
6abb12aee0 Lua: Persistent variables
Add a way for the lua scripts to set and retrieve variables
that are persistent - saved and restored with the game.

Invalidates saves.
2023-01-15 10:34:45 +02:00