From 09eb6bde1e9a4e0a3e5bf0b55ffeb7dc067d1f71 Mon Sep 17 00:00:00 2001
From: teknomunk <teknomunk@protonmail.com>
Date: Tue, 7 Jan 2025 07:36:19 -0600
Subject: [PATCH] Fix crashes in generating state serialization, make adaptive
 spawn attempt rate actually work

---
 mods/ENTITIES/mcl_mobs/spawning.lua | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua
index db7aeecbb..7404c7744 100644
--- a/mods/ENTITIES/mcl_mobs/spawning.lua
+++ b/mods/ENTITIES/mcl_mobs/spawning.lua
@@ -831,12 +831,11 @@ local function build_state_for_position(pos, parent_state)
 	end
 
 	-- Convert state into a format that can be used as a hash table key
-	state.serialized = string.format("%s:%s:d:%d:%s:%s:%s:%s:%s:%s:%d",
+	state.serialized = string.format("%s:%s:%s:%s:%s:%s:%s:%s:%d",
 		state.biome, state.dimension,
-		state.cap_space_hostile, state.cap_space_passive,
 		state.spawn_hostile, state.spawn_passive,
 		state.is_ground, state.is_grass, state.is_water, state.is_lava,
-		state.light
+		state.light or 0
 	)
 	return state,node
 end
@@ -1219,7 +1218,7 @@ if mobs_spawn then
 		-- Time the function
 		local start_time_us = minetest.get_us_time()
 		handler()
-		local stop_time_us = minetest.get_us_time()
+		local stop_time_us = minetest.get_us_time() + 1
 
 		-- Measure how long this took and calculate the time until the next call
 		local took = stop_time_us - start_time_us
@@ -1231,13 +1230,15 @@ if mobs_spawn then
 	--MAIN LOOP
 	local timer = 0
 	minetest.register_globalstep(function(dtime)
+		timer = timer - dtime
+		if timer > 0 then return end
+
 		local next_spawn, took = fixed_timeslice(timer, dtime, 1000, attempt_spawn)
 		timer = next_spawn
 		if timer > MAX_SPAWN_CYCLE_TIME then timer = MAX_SPAWN_CYCLE_TIME end
 
 		if logging and took > debug_time_threshold then
-			minetest.log("action","[mcl_mobs] took "..took.." us")
-			minetest.log("Next spawn attempt in "..tostring(timer))
+			minetest.log("Next spawn attempt in "..tostring(timer).." previous attempt took "..took.." us")
 		end
 	end)
 end