Stop mobs that require ground from spawning in water, remove unused code and variables, some minor microoptimizations

This commit is contained in:
teknomunk 2024-06-30 10:12:12 -05:00
parent 26198c34d4
commit 960b578ad0
1 changed files with 22 additions and 32 deletions

View File

@ -8,6 +8,7 @@ local end_threshold = tonumber(minetest.settings:get("mcl_mobs_end_threshold"))
local overworld_threshold = tonumber(minetest.settings:get("mcl_mobs_overworld_threshold")) or 0 local overworld_threshold = tonumber(minetest.settings:get("mcl_mobs_overworld_threshold")) or 0
local overworld_sky_threshold = tonumber(minetest.settings:get("mcl_mobs_overworld_sky_threshold")) or 7 local overworld_sky_threshold = tonumber(minetest.settings:get("mcl_mobs_overworld_sky_threshold")) or 7
local overworld_passive_threshold = tonumber(minetest.settings:get("mcl_mobs_overworld_passive_threshold")) or 7 local overworld_passive_threshold = tonumber(minetest.settings:get("mcl_mobs_overworld_passive_threshold")) or 7
local debug_time_threshold = tonumber(minetest.settings:get("vl_debug_time_threshold")) or 1000
local get_node = minetest.get_node local get_node = minetest.get_node
local get_item_group = minetest.get_item_group local get_item_group = minetest.get_item_group
@ -272,8 +273,6 @@ WARNING: BIOME INTEGRATION NEEDED -> How to get biome through lua??
local spawn_dictionary = {} local spawn_dictionary = {}
--this is where all of the spawning information is kept for mobs that don't naturally spawn --this is where all of the spawning information is kept for mobs that don't naturally spawn
local non_spawn_dictionary = {} local non_spawn_dictionary = {}
-- This is a precomputed index of what spawns belong to which spawn groups to accelerate selection of valid mobs for given possitions
local spawn_dictionary_by_group = {}
function mcl_mobs:spawn_setup(def) function mcl_mobs:spawn_setup(def)
if not mobs_spawn then return end if not mobs_spawn then return end
@ -700,18 +699,23 @@ local function build_state_for_position(pos, parent_state)
-- Get node and make sure it's loaded and a valid spawn point -- Get node and make sure it's loaded and a valid spawn point
local node = get_node(pos) local node = get_node(pos)
local node_name = node.name
-- Check if it's ground -- Check if it's ground
local is_ground = minetest.get_item_group(node.name,"solid") ~= 0 local is_water = get_item_group(node_name, "water") ~= 0
if not is_ground then local is_ground = false
pos.y = pos.y - 1 if not is_water then
node = get_node(pos) is_ground = get_item_group(node_name,"solid") ~= 0
is_ground = minetest.get_item_group(node.name,"solid") ~= 0 if not is_ground then
pos.y = pos.y - 1
node = get_node(pos)
is_ground = get_item_group(node_name,"solid") ~= 0
end
pos.y = pos.y + 1
end end
pos.y = pos.y + 1
-- Make sure we can spawn here -- Make sure we can spawn here
if not node or node.name == "ignore" or node.name == "mcl_core:bedrock" then return end if not node or node_name == "ignore" or node_name == "mcl_core:bedrock" then return end
-- Build spawn state data -- Build spawn state data
local state = { local state = {
@ -723,9 +727,9 @@ local function build_state_for_position(pos, parent_state)
state.biome = biome_name state.biome = biome_name
state.dimension = dimension state.dimension = dimension
state.is_ground = is_ground and get_item_group(node.name, "leaves") == 0 state.is_ground = is_ground and get_item_group(node_name, "leaves") == 0
state.grass = get_item_group(node.name, "grass_block") ~= 0 state.grass = get_item_group(node_name, "grass_block") ~= 0
state.water = get_item_group(node.name, "water") ~= 0 state.water = is_water
-- Check light level -- Check light level
local gotten_light = get_node_light(pos) local gotten_light = get_node_light(pos)
@ -737,7 +741,7 @@ local function build_state_for_position(pos, parent_state)
end end
else else
-- Modern lighting -- Modern lighting
local light_node = minetest.get_node(pos) local light_node = get_node(pos)
local sky_light = minetest.get_natural_light(pos) or 0 local sky_light = minetest.get_natural_light(pos) or 0
local art_light = minetest.get_artificial_light(light_node.param1) local art_light = minetest.get_artificial_light(light_node.param1)
@ -768,7 +772,7 @@ local function build_state_for_position(pos, parent_state)
end end
local function spawn_group(p, mob, spawn_on, amount_to_spawn, parent_state) local function spawn_group(p, mob, spawn_on, amount_to_spawn, parent_state)
local nn= minetest.find_nodes_in_area_under_air(vector.offset(p,-5,-3,-5),vector.offset(p,5,3,5),spawn_on) local nn= find_nodes_in_area_under_air(vector.offset(p,-5,-3,-5),vector.offset(p,5,3,5),spawn_on)
local o local o
table.shuffle(nn) table.shuffle(nn)
if not nn or #nn < 1 then if not nn or #nn < 1 then
@ -806,17 +810,13 @@ minetest.register_chatcommand("spawn_mob",{
table.insert(modifiers, ":"..capture) table.insert(modifiers, ":"..capture)
end end
local mod1 = string.find(param, ":")
local mobname = param local mobname = param
local mod1 = string.find(param, ":")
if mod1 then if mod1 then
mobname = string.sub(param, 1, mod1-1) mobname = string.sub(param, 1, mod1-1)
end end
local mob = mcl_mobs.spawn(pos,mobname) local mob = mcl_mobs.spawn(pos,mobname)
if mob then if mob then
for c=1, #modifiers do for c=1, #modifiers do
modifs = modifiers[c] modifs = modifiers[c]
@ -926,8 +926,8 @@ if mobs_spawn then
local spawning_position = get_next_mob_spawn_pos(pos) local spawning_position = get_next_mob_spawn_pos(pos)
if spawning_position then return spawning_position end if spawning_position then return spawning_position end
max_loops = max_loops - 1 max_loops = max_loops - 1
end end
return nil return nil
end end
@ -1097,12 +1097,9 @@ if mobs_spawn then
return mcl_mobs.spawn(spawning_position, mob_def.name) return mcl_mobs.spawn(spawning_position, mob_def.name)
end end
--MAIN LOOP --MAIN LOOP
local timer = 0 local timer = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
timer = timer + dtime timer = timer + dtime
if timer < WAIT_FOR_SPAWN_ATTEMPT then return end if timer < WAIT_FOR_SPAWN_ATTEMPT then return end
timer = 0 timer = 0
@ -1120,7 +1117,7 @@ if mobs_spawn then
if total_mobs > mob_cap.total or total_mobs > #players * mob_cap.player then if total_mobs > mob_cap.total or total_mobs > #players * mob_cap.player then
minetest.log("action","[mcl_mobs] global mob cap reached. no cycle spawning.") minetest.log("action","[mcl_mobs] global mob cap reached. no cycle spawning.")
local took = (minetest.get_us_time() - start_time_us) local took = (minetest.get_us_time() - start_time_us)
if took > 1000 then if took > debug_time_threshold then
minetest.log("action","[mcl_mobs] took "..took.." us") minetest.log("action","[mcl_mobs] took "..took.." us")
end end
return return
@ -1136,7 +1133,7 @@ if mobs_spawn then
end end
local took = (minetest.get_us_time() - start_time_us) local took = (minetest.get_us_time() - start_time_us)
if took > 1000 then if took > debug_time_threshold then
minetest.log("action","[mcl_mobs] took "..took.." us") minetest.log("action","[mcl_mobs] took "..took.." us")
end end
end) end)
@ -1157,7 +1154,6 @@ function mob_class:despawn_allowed()
despawn_allowed(self) despawn_allowed(self)
end end
assert(despawn_allowed({can_despawn=false}) == false, "despawn_allowed - can_despawn false failed") assert(despawn_allowed({can_despawn=false}) == false, "despawn_allowed - can_despawn false failed")
assert(despawn_allowed({can_despawn=true}) == true, "despawn_allowed - can_despawn true failed") assert(despawn_allowed({can_despawn=true}) == true, "despawn_allowed - can_despawn true failed")
@ -1213,11 +1209,5 @@ minetest.register_on_mods_loaded(function()
for _,def in pairs(minetest.registered_biomes) do for _,def in pairs(minetest.registered_biomes) do
table.insert(list_of_all_biomes, def.name) table.insert(list_of_all_biomes, def.name)
end end
-- Create spawn groups
for i = 1,#spawn_dictionary do
local spawn_def = spawn_dictionary[i]
end
end) end)