FaceDeer / dfcaverns

Replicating Dwarf Fortress' underground flora in Minetest
Other
16 stars 9 forks source link

Crash from subterrane.get_cavern_value() returning nil in df_caverns/level3.lua #39

Closed Veyrdite closed 1 year ago

Veyrdite commented 1 year ago

dfcaverns release = 13917 (latest from contentdb) minetest 5.6.1-_1

I'm deep underground and the game is constantly crashing.

Maybe caused by invalid biomes as I uninstalled a biome related mod today?

AsyncErr: Lua: Runtime error from mod 'df_ambience' in callback environment_Step(): ...veyrdite/.minetest/mods/dfcaverns/df_caverns/level3.lua:50: attempt to compare nil with number
stack traceback:
    ...veyrdite/.minetest/mods/dfcaverns/df_caverns/level3.lua:50: in function 'val'
    ...veyrdite/.minetest/mods/dfcaverns/df_caverns/shared.lua:34: in function 'get_biome'
    .../veyrdite/.minetest/mods/dfcaverns/df_ambience/init.lua:57: in function 'get_player_data'
    .../veyrdite/.minetest/mods/dfcaverns/df_ambience/init.lua:81: in function 'get_ambience'
    .../veyrdite/.minetest/mods/dfcaverns/df_ambience/init.lua:105: in function <.../veyrdite/.minetest/mods/dfcaverns/df_ambience/init.lua:91>
    /usr/share/minetest/builtin/game/register.lua:431: in function </usr/share/minetest/builtin/game/register.lua:417>
Veyrdite commented 1 year ago

Commenting out the comparison got me out of the constant crashes, allowing me to continue playing:

df_caverns.register_biome_check(function(pos, heat, humidity)
    if pos.y < df_caverns.config.level3_min or pos.y >= df_caverns.config.level2_min then
        return nil
    end
    local biome = get_biome(heat, humidity)
    --[[
    if biome == "bloodnether" then
        if subterrane.get_cavern_value("cavern layer 3", pos) < 0 then
            return "nethercap"
        end
        return "bloodthorn"
    end
    ]]--
    return biome
end)
FaceDeer commented 1 year ago

Odd. That should be impossible, get_cavern_value only returns nil when it's being called for a pos that's outside of the elevation range that the cavern is defined for and the initial pos.y check at the beginning of register_biome_check should be guarding against this.

It shouldn't have anything to do with other biomes, dfcaverns isn't actually using the built-in biome system.

The commenting-out that you did will cause nethercap and bloodthorn caverns to no longer spawn. A better approach would be to turn the line

if subterrane.get_cavern_value("cavern layer 3", pos) < 0 then

into

if (subterrane.get_cavern_value("cavern layer 3", pos) or 0) < 0 then

That'll make the comparison safe against nil returns.

I'll tinker around a bit on the weekend to see if I can figure out why this "impossible" result happened. Have you changed the boundary elevations of the various cavern layers to something other than the defaults, perhaps?

FaceDeer commented 1 year ago

I figured it out. The ambient sound mod was checking for the biome that the player's current position was in, and it was sending raw un-rounded position vectors into the method - something that never happened before the ambient sound mod was added recently. So if the player happened to be in the one-node-wide boundary between layers 2 and 3 in a bloodthorn/nethercap biome this crash could occur. I've added two fixes, one of them a nil check in the layer 3 biome function and the other a vector.round operation to "sanitize" the position being checked.

I'll make a release later today. I'm going to take this opportunity to add some documentation to the next release, if you want the fix now you can grab it from the repository.

MeseCraft commented 1 year ago

I know this is closed, and will update to the patched version here in a moment, but just verifying this report, as I've been seeing it too.

2022-11-05 16:25:40: ERROR[Main]: ServerError: AsyncErr: Lua: Runtime error from mod 'df_ambience' in callback environment_Step(): ...mes/MeseCraft/mods/WORLD/dfcaverns/df_caverns/level3.lua:50: attempt to compare nil with number 2022-11-05 16:25:40: ERROR[Main]: stack traceback: 2022-11-05 16:25:40: ERROR[Main]: ...mes/MeseCraft/mods/WORLD/dfcaverns/df_caverns/level3.lua:50: in function 'val' 2022-11-05 16:25:40: ERROR[Main]: ...mes/MeseCraft/mods/WORLD/dfcaverns/df_caverns/shared.lua:34: in function 'get_biome' 2022-11-05 16:25:40: ERROR[Main]: ...ames/MeseCraft/mods/WORLD/dfcaverns/df_ambience/init.lua:57: in function 'get_player_data' 2022-11-05 16:25:40: ERROR[Main]: ...ames/MeseCraft/mods/WORLD/dfcaverns/df_ambience/init.lua:81: in function 'get_ambience' 2022-11-05 16:25:40: ERROR[Main]: ...ames/MeseCraft/mods/WORLD/dfcaverns/df_ambience/init.lua:105: in function <...ames/MeseCraft/mods/WORLD/dfcaverns/df_ambience/init.lua:91> 2022-11-05 16:25:40: ERROR[Main]: ...-server-stable/minetest/bin/../builtin/game/register.lua:429: in function <...-server-stable/minetest/bin/../builtin/game/register.lua:415>