Commit Graph

134 Commits

Author SHA1 Message Date
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
PatR
18cc99150a couple more analyzer complains - nhlua.c
I don't know whether there is potentially a real problem with either
of these.  The two trivial changes should shut the static analyzer.
2023-01-14 15:55:55 -08:00
nhmall
02a48aa8cf split g into multiple structures
The consolidation of global variables from scattered source
files into decl.c and declared in decl.h was begun in 3.7.0.
Their placement in common files was done for centralized
initialization and potential re-initialization during a
"play again" scenario.

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

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

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

A global variable named 'amulets', would be found in ga.
    ga.amulets
     ^ ^
A global varable named 'move', would be found in gm.
    gm.moves
     ^ ^
A global variable named 'val_for_n_or_more' would be found in gv.
    gv.val_for_n_or_more
     ^ ^
A global variable named 'youmonst' would be found in gy.
    gy.youmonst
     ^ ^
2022-11-29 21:53:21 -05:00
nhmall
4b04b1e6ac expand support for noreturn declarations
Although gcc specifies support for declaring a function as
noreturn after the function name and parameters, other compilers
do so via an attribute at the start of the declaration. Add some
macro support for the attribute-at-the-beginning method:
  o MS Visual Studio compiler
  o Upcoming C23 standard (untested at this point)
2022-11-24 00:51:42 -05:00
nhmall
99a93fe50b some C99 changes
Instead of using index() macro defined to strchr, use C99 strchr.
Instead of using rindex() macro defined to strrchr, use C99 strrchr.

If you want to try building on a platform that doesn't offer those
two functions, these are available:
    define NOT_C99       /* to make some non-C99 code available */
    define NEED_INDEX    /* to define a macro for index()  */
    define NEED_RINDX    /* to define a macro for rindex() */
2022-10-29 10:54:25 -04:00
copperwater
f71bff3285 Standardize all core and obj functions with relative coords
This is a large iteration on a previous implementation of making
nh.getmap() parse its coordinates as relative to the last defined map or
room rather than absolute to the entire level. Now, everything in the
nh.* and obj.* functions interprets coords as relative rather than
absolute. (By default; if no map or room has been defined, or if the lua
code is executing after level creation is done, they will interpret the
coordinates as absolute).

The general motivation is basically the same - routines that use
absolute coordinates are difficult to use in level creation routines,
because then the designer has to remember to convert the relative
coordinate to an absolute one (and that was impossible before
nh.abscoord was added, particularly in themed rooms). And once
nh.getmap() takes relative coordinates, it would be very strange to have
all the other functions (setting timers, burying objects, etc) remain
with absolute ones.

In a couple places, code is changed to account for coordinates that are
relative to a *room* (which uses g.coder->croom->[lx,ly] as an offset,
instead of relative to a *map*, which uses [xstart,ystart].
Specifically, selection.iterate did not account for this, and without
this the ice themed room timer was not being started in the proper
place.

All tests are updated to respect the new behavior. Most of the modified
functions are not actually used anywhere in level files; the one
exception is starting a timer in a themed room, and that has been
adjusted.

Documentation updated as well to clarify when various things are tossing
around relative and absolute coordinates, both in comments and in
lua.adoc.
2022-08-31 18:26:05 +03:00
copperwater
a30a45be46 nh.getmap() returns information relative to the most recent des.map
There are many possible use cases for nh.getmap during level creation,
but it's rendered mostly unusable by virtue of always returning data
about the exact x,y coordinate in g.level.locations. (In particular,
it can't currently be used in themed rooms at all, because the themed
room could be anywhere on the level.) This is inconsistent with how most
other coordinate-based functions work following a des.map, which use
coordinates relative to the 0,0 point of the map.

This changes it so that during level creation only, if nh.getmap is used
following a des.map statement, it will look up the coordinates relative
to the origin of the map, consistent with the other functions.
2022-08-31 18:26:05 +03:00
Pasi Kallinen
fd9745f9c6 Command repeating by using cmd queues
This replaces the old pushq/saveq arrays (which were used to save
the keys pressed by the user for repeating a previous command)
with a new command queue.  This means there's no hard-coded limit
to the saved keys, and it can repeat extended commands which are
not bound to any key.
2022-08-09 11:54:45 +03:00
Pasi Kallinen
45613ea771 Experimental #saveoptions command
Add a #saveoptions extended command, to allow saving configuration
settings from within the game. This is still highly experimental,
and gives plenty of warnings before asking to overwrite the file.

Lack of option saving is one of the biggest complaints new players
have, so this should help with it.  More experienced players with
highly customized config file should not use this feature, as it
completely rewrites the file, removing all comments and non-config
lines.
2022-08-05 10:33:55 +03:00
nhmall
3004cf2d34 be more consistent with coordinates 2022-07-02 09:10:03 -04:00
nhmall
30b557f7d5 change xchar to other typedefs
One of the drivers of this change was that screen coordinates require a
type that can hold values greater than 127. Parameters to the window
port routines require a large type in order to be able to have values
a fair bit larger than COLNO and ROWNO passed to them, particularly for
their use to the right of the map window.

This splits the uses of xchar into 3 different situations, and adjusts
their type and size:

                        xchar
                          |
               -----------------------
               |          |          |
            coordxy     xint16     xint8

coordxy: Actual x or y coordinates for various things (moved to 16-bits).

xint16:  Same data size as coordxy, but for non-coordinate use (16-bits).

xint8:   There are only a few use cases initially, where it was very
         plain to see that the variable could remain as 8-bits, rather
         than be bumped to 16-bits.  There are probably more such cases
         that could be changed after additional review.

Note: This first changed all xchar variables to coordxy. Some were
reviewed and got changed to xint16 or xint8 when it became apparent that
their usage was not for coordinates.

This increments EDITLEVEL in patchlevel.h
2022-06-30 23:48:18 -04:00
nhmall
2770223d10 interface groundwork for core-side color decisions
(user-side decisions really, but as it stands right now
user-side decisions/options are made and processed by the core)

add a parameter to add_menu so color can be passed
2022-06-25 13:21:51 -04:00
PatR
5977fa4481 another lua warning bit
I forgot to do this with yesterday's post garbage collection fix
update.  Record lua warnings in paniclog during normal play too, not
just when in wizard mode.
2022-06-01 13:45:57 -07:00
PatR
18639d2ef2 send lua warnings to paniclog instead to player
Now that the garbage collection problem has been fixed, record lua
warnings in the paniclog file rather than showing them on the screen.

Move nhl_warn()'s warnbuf[] to struct g in case restart ever gets
implemented so that it can be cleared if the restart occurred while
a warning message was under construction.
2022-06-01 01:03:11 -07:00