Refactor and clean up spawn checks, optimize.

This commit is contained in:
kno10 2024-07-04 12:37:12 +02:00 committed by the-real-herowl
parent 382a35bb44
commit 2e1df31399

View File

@ -753,80 +753,74 @@ end
local function spawn_check(pos, spawn_def) local function spawn_check(pos, spawn_def)
if not spawn_def or not pos then return end if not spawn_def or not pos then return end
dbg_spawn_attempts = dbg_spawn_attempts + 1 dbg_spawn_attempts = dbg_spawn_attempts + 1
local dimension = mcl_worlds.pos_to_dimension(pos) local dimension = mcl_worlds.pos_to_dimension(pos)
local mob_def = minetest.registered_entities[spawn_def.name] if spawn_def.dimension ~= dimension then return end -- wrong dimension
local mob_type = mob_def.type -- find ground node below spawn position
local gotten_node = get_node(pos).name local gotten_node = get_node(pos).name
if not gotten_node then return end if not gotten_node then return end
local is_ground = get_item_group(gotten_node,"solid") ~= 0
local is_ground = minetest.get_item_group(gotten_node,"solid") ~= 0 if not is_ground then -- try node one below instead
if not is_ground then
pos.y = pos.y - 1 pos.y = pos.y - 1
gotten_node = get_node(pos).name gotten_node = get_node(pos).name
is_ground = minetest.get_item_group(gotten_node,"solid") ~= 0 is_ground = get_item_group(gotten_node,"solid") ~= 0
end end
pos.y = pos.y + 1 pos.y = pos.y + 1
local is_water = get_item_group(gotten_node, "water") ~= 0 -- check spawn height
local is_lava = get_item_group(gotten_node, "lava") ~= 0 if pos.y < spawn_def.min_height or pos.y > spawn_def.max_height then return end
local is_leaf = get_item_group(gotten_node, "leaves") ~= 0 mcl_log("spawn_check#1 position checks passed")
local is_bedrock = gotten_node == "mcl_core:bedrock"
local is_grass = minetest.get_item_group(gotten_node,"grass_block") ~= 0
if pos.y >= spawn_def.min_height -- do not spawn on bedrock
and pos.y <= spawn_def.max_height if gotten_node == "mcl_core:bedrock" then return end
and spawn_def.dimension == dimension -- do not spawn ground mobs on leaves
and biome_check(spawn_def.biomes, get_biome_name(pos)) then if spawn_def.type_of_spawning == "ground" and (not is_ground or get_item_group(gotten_node, "leaves") ~= 0) then return end
-- farm animals on grass only
if is_farm_animal(spawn_def.name) and get_item_group(gotten_node, "grass_block") == 0 then return end
-- water mobs only on water
if spawn_def.type_of_spawning == "water" and get_item_group(gotten_node, "water") == 0 then return end
-- lava mobs only on lava
if spawn_def.type_of_spawning == "lava" and get_item_group(gotten_node, "lava") == 0 then return end
mcl_log("Spawn level 1 check - Passed") ---- More expensive calls:
if (is_ground or spawn_def.type_of_spawning ~= "ground") -- check the biome
and (spawn_def.type_of_spawning ~= "ground" or not is_leaf) if not biome_check(spawn_def.biomes, get_biome_name(pos)) then return end
and (not is_farm_animal(spawn_def.name) or is_grass) -- check if there is enough room
and (spawn_def.type_of_spawning ~= "water" or is_water) local mob_def = minetest.registered_entities[spawn_def.name]
and not is_bedrock if not has_room(mob_def,pos) then return end
and has_room(mob_def,pos) -- additional checks (slime etc.)
and (spawn_def.check_position and spawn_def.check_position(pos) or spawn_def.check_position == nil) if spawn_def.check_position and not spawn_def.check_position(pos) then return end
and ( not spawn_protected or not minetest.is_protected(pos, "") ) then if spawn_protected and minetest.is_protected(pos, "") then return end
mcl_log("spawn_check#2 advanced checks passed")
mcl_log("Spawn level 2 check - Passed") -- check light thresholds
local gotten_light = get_node_light(pos) local gotten_light = get_node_light(pos)
-- old lighting
if not modern_lighting then return gotten_light >= spawn_def.min_light and gotten_light <= spawn_def.max_light end
if modern_lighting then local sky_light = minetest.get_natural_light(pos)
local my_node = get_node(pos) local art_light = minetest.get_artificial_light(get_node(pos).param1)
local sky_light = minetest.get_natural_light(pos) if mob_def.spawn_check then
local art_light = minetest.get_artificial_light(my_node.param1) return mob_def.spawn_check(pos, gotten_light, art_light, sky_light)
end
if mob_def.spawn_check then if mob_def.type == "monster" then
return mob_def.spawn_check(pos, gotten_light, art_light, sky_light) if dimension == "nether" then
elseif mob_type == "monster" then if art_light <= nether_threshold then
if dimension == "nether" then return true
if art_light <= nether_threshold then end
return true elseif dimension == "end" then
end if art_light <= end_threshold then
elseif dimension == "end" then return true
if art_light <= end_threshold then end
return true elseif dimension == "overworld" then
end if art_light <= overworld_threshold and sky_light <= overworld_sky_threshold then
elseif dimension == "overworld" then return true
if art_light <= overworld_threshold and sky_light <= overworld_sky_threshold then
return true
end
end
else
-- passive threshold is apparently the same in all dimensions ...
if gotten_light > overworld_passive_threshold then
return true
end
end
else
if gotten_light >= spawn_def.min_light and gotten_light <= spawn_def.max_light then
return true
end
end end
end end
return false
end end
return false -- passive threshold is apparently the same in all dimensions ...
return gotten_light > overworld_passive_threshold
end end
function mcl_mobs.spawn(pos,id) function mcl_mobs.spawn(pos,id)
@ -834,11 +828,7 @@ function mcl_mobs.spawn(pos,id)
if not def or (def.can_spawn and not def.can_spawn(pos)) or not def.is_mob then if not def or (def.can_spawn and not def.can_spawn(pos)) or not def.is_mob then
return false return false
end end
if not dbg_spawn_counts[def.name] then dbg_spawn_counts[def.name] = (dbg_spawn_counts[def.name] or 0) + 1
dbg_spawn_counts[def.name] = 1
else
dbg_spawn_counts[def.name] = dbg_spawn_counts[def.name] + 1
end
return minetest.add_entity(pos, def.name) return minetest.add_entity(pos, def.name)
end end