From 6ea4b43249c95c3452836dd33854e7489bc5dffe Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 26 Jan 2023 03:57:36 +0000 Subject: [PATCH 01/21] Limit group spawn to available cap space. Total mobs check once per spawn cycle. --- mods/ENTITIES/mcl_mobs/spawning.lua | 185 +++++++++++++++++++++++----- 1 file changed, 154 insertions(+), 31 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 6fc600605..3af88d5a4 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -24,11 +24,22 @@ local vector_floor = vector.floor local table_copy = table.copy local table_remove = table.remove - local pairs = pairs + +local LOGGING_ON = minetest.settings:get_bool("mcl_logging_mobs_spawning", true) +local function mcl_log (message) + if LOGGING_ON then + mcl_util.mcl_log (message, "[Mobs spawn]", true) + end +end + + local dbg_spawn_attempts = 0 local dbg_spawn_succ = 0 local dbg_spawn_counts = {} + +local WAIT_FOR_SPAWN_ATTEMPT = 10 + -- range for mob count local aoc_range = 136 local remove_far = true @@ -250,21 +261,45 @@ local function count_mobs_total(mob_type) return num end -local function count_mobs_all() +local function count_mobs_all(categorise_by, pos, max_distance) local mobs_found = {} local num = 0 for _,entity in pairs(minetest.luaentities) do - if entity.is_mob then - local mob_type = entity.type -- animal / monster / npc - local mob_name = entity.name - if mobs_found[mob_name] then - mobs_found[mob_name] = mobs_found[mob_name] + 1 + if entity and entity.is_mob then + + local add_entry = false + + if pos and max_distance then + local mob_pos = entity.object:get_pos() + if mob_pos then + local distance = vector.distance(pos, mob_pos) + + if distance <= max_distance then + --mcl_log("distance is good") + add_entry = true + else + mcl_log("mob_pos: " .. minetest.pos_to_string(mob_pos)) + mcl_log("distance: ".. distance) + end + end + else - mobs_found[mob_name] = 1 + add_entry = true + end + + --local mob_type = entity.type -- animal / monster / npc + if add_entry then + local mob_cat = entity[categorise_by] + if mobs_found[mob_cat] then + mobs_found[mob_cat] = mobs_found[mob_cat] + 1 + else + mobs_found[mob_cat] = 1 + end + num = num + 1 end - num = num + 1 end end + mcl_log("num: ".. num) return mobs_found, num end @@ -280,6 +315,19 @@ local function count_mobs_total_cap(mob_type) return num end +local function output_mob_stats(mob_counts, total_mobs) + if (total_mobs) then + minetest.log("action", "Total mobs found: " .. total_mobs) + end + if mob_counts then + for k, v1 in pairs(mob_counts) do + minetest.log("action", "k: " .. tostring(k)) + minetest.log("action", "v1: " .. tostring(v1)) + end + end +end + + -- global functions function mcl_mobs:spawn_abm_check(pos, node, name) @@ -508,7 +556,6 @@ local function spawn_check(pos,spawn_def,ignore_caps) local is_bedrock = gotten_node == "mcl_core:bedrock" local is_grass = minetest.get_item_group(gotten_node,"grass_block") ~= 0 local mob_count_wide = 0 - local mob_count = 0 if not ignore_caps then mob_count = count_mobs(pos,32,mob_type) @@ -553,8 +600,7 @@ function mcl_mobs.spawn(pos,id) end -local function spawn_group(p,mob,spawn_on,group_max,group_min) - if not group_min then group_min = 1 end +local function spawn_group(p,mob,spawn_on,amount_to_spawn) local nn= minetest.find_nodes_in_area_under_air(vector.offset(p,-5,-3,-5),vector.offset(p,5,3,5),spawn_on) local o table.shuffle(nn) @@ -562,7 +608,8 @@ local function spawn_group(p,mob,spawn_on,group_max,group_min) nn = {} table.insert(nn,p) end - for i = 1, math.random(group_min,group_max) do + + for i = 1, amount_to_spawn do local sp = vector.offset(nn[math.random(#nn)],0,1,0) if spawn_check(nn[math.random(#nn)],mob,true) then if mob.type_of_spawning == "water" then @@ -654,6 +701,59 @@ if mobs_spawn then -- Get pos to spawn, x and z are randomised, y is range + local function mob_cap_space (pos, mob_type, mob_counts) + local type_cap = mob_cap[mob_type] or 15 + + + local mob_count_from_total = mob_counts[mob_type] + if not mob_count_from_total then + mcl_log("none of type found. set as 0") + mob_count_from_total = 0 + end + + local cap_space = type_cap - mob_count_from_total + if cap_space < 1 then + cap_space = 0 + end + + + --type = "monster", + --spawn_class = "hostile", + --type = "animal", + --spawn_class = "passive", + --local cod = { + -- type = "animal", + -- spawn_class = "water", + + + --mcl_log("spawn_class: " .. spawn_class) + mcl_log("mob_type: " .. mob_type) + mcl_log("type_cap: " .. type_cap) + --if mob_type == "animal" then + + mcl_log("mob_count_from_total: " .. mob_count_from_total) + + --local mob_count = count_mobs(pos,32,mob_type) + local mob_count_wide = count_mobs(pos,aoc_range,mob_type) + + mcl_log("mob_count_wide: " .. mob_count_wide) + if mob_count_from_total ~= mob_count_wide then + mcl_log("A difference in mob count") + else + mcl_log("No difference in mob count") + end + + + --mcl_log("mob_count: " .. mob_count) + + mcl_log("Cap space available: " .. cap_space) + --end + + + --and ( mob_count_wide < (mob_cap[mob_type] or 15) ) + --and ( mob_count < 5 ) + return cap_space + end local function spawn_a_mob(pos, dimension, y_min, y_max) --create a disconnected clone of the spawn dictionary @@ -674,6 +774,11 @@ if mobs_spawn then local noise = perlin_noise:get_3d(spawning_position) local current_summary_chance = summary_chance table.shuffle(mob_library_worker_table) + + -- + local mob_counts, total_mobs = count_mobs_all("type", pos, aoc_range) + output_mob_stats(mob_counts, total_mobs) + while #mob_library_worker_table > 0 do local mob_chance_offset = (math_round(noise * current_summary_chance + 12345) % current_summary_chance) + 1 local mob_index = 1 @@ -690,6 +795,14 @@ if mobs_spawn then local spawn_in_group = minetest.registered_entities[mob_def.name].spawn_in_group or 4 local spawn_in_group_min = minetest.registered_entities[mob_def.name].spawn_in_group_min or 1 local mob_type = minetest.registered_entities[mob_def.name].type + + -- TODO use spawn class for caps + --local spawn_class = minetest.registered_entities[mob_def.name].spawn_class + --spawn_class = "water" + + local cap_space = mob_cap_space (pos, mob_type, mob_counts) + + --TODO cap_space > 0 and if spawn_check(spawning_position,mob_def) then if mob_def.type_of_spawning == "water" then spawning_position = get_water_spawn(spawning_position) @@ -702,14 +815,29 @@ if mobs_spawn then minetest.log("warning","[mcl_mobs] mob "..mob_def.name.." refused to spawn at "..minetest.pos_to_string(vector.round(spawning_position))) return end - --everything is correct, spawn mob - local object - if spawn_in_group and ( mob_type ~= "monster" or math.random(5) == 1 ) then - if logging then - minetest.log("action", "[mcl_mobs] A group of mob " .. mob_def.name .. " spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at " .. minetest.pos_to_string(spawning_position, 1)) - end - object = spawn_group(spawning_position,mob_def,{minetest.get_node(vector.offset(spawning_position,0,-1,0)).name},spawn_in_group,spawn_in_group_min) + --everything is correct, spawn mob + --TODO If spawning, need to recheck total, or add to total mobs... + local object + --remove mob_type ~= "monster" or + if spawn_in_group and (mob_type ~= "monster" or math.random(5) == 1) then + + local group_min = spawn_in_group_min + if not group_min then group_min = 1 end + + local amount_to_spawn = math.random(group_min,spawn_in_group) + minetest.log("action", "Spawning quantity: " .. amount_to_spawn) + + if amount_to_spawn > cap_space then + mcl_log("Throttle amount to cap space: " .. cap_space) + amount_to_spawn = cap_space + end + + if logging then + minetest.log("action", "[mcl_mobs] A group of " ..amount_to_spawn .. " " .. mob_def.name .. " mob spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at " .. minetest.pos_to_string(spawning_position, 1)) + end + + object = spawn_group(spawning_position,mob_def,{minetest.get_node(vector.offset(spawning_position,0,-1,0)).name}, amount_to_spawn) else if logging then minetest.log("action", "[mcl_mobs] Mob " .. mob_def.name .. " spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at ".. minetest.pos_to_string(spawning_position, 1)) @@ -717,6 +845,7 @@ if mobs_spawn then object = mcl_mobs.spawn(spawning_position, mob_def.name) end end + end current_summary_chance = current_summary_chance - mob_chance table_remove(mob_library_worker_table, mob_index) @@ -729,7 +858,7 @@ if mobs_spawn then local timer = 0 minetest.register_globalstep(function(dtime) timer = timer + dtime - if timer < 10 then return end + if timer < WAIT_FOR_SPAWN_ATTEMPT then return end timer = 0 local players = get_connected_players() local total_mobs = count_mobs_total_cap() @@ -775,6 +904,7 @@ function mob_class:check_despawn(pos, dtime) end end + minetest.register_chatcommand("mobstats",{ privs = { debug = true }, func = function(n,param) @@ -786,16 +916,9 @@ minetest.register_chatcommand("mobstats",{ minetest.chat_send_player(n,"successful spawns since server start:"..dbg_spawn_succ) - local mob_counts, total_mobs = count_mobs_all() - if (total_mobs) then - minetest.log("action", "Total mobs found: " .. total_mobs) - end - if mob_counts then - for k, v1 in pairs(mob_counts) do - minetest.log("action", "k: " .. tostring(k)) - minetest.log("action", "v1: " .. tostring(v1)) - end - end + --local mob_counts, total_mobs = count_mobs_all("name") -- name + local mob_counts, total_mobs = count_mobs_all("type") + output_mob_stats(mob_counts, total_mobs) end }) From 60529d3d5d5c417e26028577a6ff26d76303ea2d Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 26 Jan 2023 13:44:29 +0000 Subject: [PATCH 02/21] Add in constants for spawn zones --- mods/ENTITIES/mcl_mobs/spawning.lua | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 3af88d5a4..6ad72c3a4 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -40,7 +40,12 @@ local dbg_spawn_counts = {} local WAIT_FOR_SPAWN_ATTEMPT = 10 +local MOB_SPAWN_ZONE_INNER = 24 +local MOB_SPAWN_ZONE_MIDDLE = 32 +local MOB_SPAWN_ZONE_OUTER = 128 --TODO not used yet. Should replace aoc + -- range for mob count +local MOB_CAP_INNER_RADIUS = 32 local aoc_range = 136 local remove_far = true @@ -470,9 +475,10 @@ function mcl_mobs:spawn_specific(name, dimension, type_of_spawning, biomes, min_ summary_chance = summary_chance + chance end + local two_pi = 2 * math.pi local function get_next_mob_spawn_pos(pos) - local distance = math_random(25, 32) + local distance = math_random(MOB_SPAWN_ZONE_INNER + 1, MOB_SPAWN_ZONE_MIDDLE) local angle = math_random() * two_pi local xoff = math_round(distance * math_cos(angle)) local yoff = math_round(distance * math_sin(angle)) @@ -481,7 +487,7 @@ end local function decypher_limits(posy) posy = math_floor(posy) - return posy - 32, posy + 32 + return posy - MOB_SPAWN_ZONE_MIDDLE, posy + MOB_SPAWN_ZONE_MIDDLE end --a simple helper function for mob_spawn @@ -532,6 +538,8 @@ local function has_room(self,pos) return true end + + local function spawn_check(pos,spawn_def,ignore_caps) if not spawn_def then return end dbg_spawn_attempts = dbg_spawn_attempts + 1 @@ -558,7 +566,7 @@ local function spawn_check(pos,spawn_def,ignore_caps) local mob_count_wide = 0 local mob_count = 0 if not ignore_caps then - mob_count = count_mobs(pos,32,mob_type) + mob_count = count_mobs(pos,MOB_CAP_INNER_RADIUS,mob_type) mob_count_wide = count_mobs(pos,aoc_range,mob_type) end @@ -733,7 +741,7 @@ if mobs_spawn then mcl_log("mob_count_from_total: " .. mob_count_from_total) - --local mob_count = count_mobs(pos,32,mob_type) + --local mob_count = count_mobs(pos,MOB_CAP_INNER_RADIUS,mob_type) local mob_count_wide = count_mobs(pos,aoc_range,mob_type) mcl_log("mob_count_wide: " .. mob_count_wide) @@ -802,7 +810,7 @@ if mobs_spawn then local cap_space = mob_cap_space (pos, mob_type, mob_counts) - --TODO cap_space > 0 and + --TODO cap check before or after spawn check - cap_space > 0 and if spawn_check(spawning_position,mob_def) then if mob_def.type_of_spawning == "water" then spawning_position = get_water_spawn(spawning_position) @@ -818,15 +826,15 @@ if mobs_spawn then --everything is correct, spawn mob --TODO If spawning, need to recheck total, or add to total mobs... - local object - --remove mob_type ~= "monster" or + local object --TODO is not used. remove or use? + --TODO ensure animal spawns are more gradual, remove mob_type ~= "monster" or if spawn_in_group and (mob_type ~= "monster" or math.random(5) == 1) then local group_min = spawn_in_group_min if not group_min then group_min = 1 end local amount_to_spawn = math.random(group_min,spawn_in_group) - minetest.log("action", "Spawning quantity: " .. amount_to_spawn) + mcl_log("action", "Spawning quantity: " .. amount_to_spawn) if amount_to_spawn > cap_space then mcl_log("Throttle amount to cap space: " .. cap_space) @@ -910,7 +918,7 @@ minetest.register_chatcommand("mobstats",{ func = function(n,param) minetest.chat_send_player(n,dump(dbg_spawn_counts)) local pos = minetest.get_player_by_name(n):get_pos() - minetest.chat_send_player(n,"mobs within 32 radius of player:"..count_mobs(pos,32)) + minetest.chat_send_player(n,"mobs within 32 radius of player:"..count_mobs(pos,MOB_CAP_INNER_RADIUS)) minetest.chat_send_player(n,"total mobs:"..count_mobs_total()) minetest.chat_send_player(n,"spawning attempts since server start:"..dbg_spawn_attempts) minetest.chat_send_player(n,"successful spawns since server start:"..dbg_spawn_succ) From 7c7f4b930ce8a5e18d53ad041d0d3b4aca3211ed Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 26 Jan 2023 15:20:38 +0000 Subject: [PATCH 03/21] Move cap check out of spawn checks and reimplement close cap check --- mods/ENTITIES/mcl_mobs/spawning.lua | 158 +++++++++++++++++----------- 1 file changed, 95 insertions(+), 63 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 6ad72c3a4..bcb330844 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -49,6 +49,8 @@ local MOB_CAP_INNER_RADIUS = 32 local aoc_range = 136 local remove_far = true +local MOBS_CAP_CLOSE = 5 + local mob_cap = { monster = tonumber(minetest.settings:get("mcl_mob_cap_monster")) or 70, animal = tonumber(minetest.settings:get("mcl_mob_cap_animal")) or 10, @@ -266,46 +268,58 @@ local function count_mobs_total(mob_type) return num end -local function count_mobs_all(categorise_by, pos, max_distance) - local mobs_found = {} +local function count_mobs_add_entry (mobs_list, mob_cat) + if mobs_list[mob_cat] then + mobs_list[mob_cat] = mobs_list[mob_cat] + 1 + else + mobs_list[mob_cat] = 1 + end +end + +--categorise_by can be name or type +local function count_mobs_all(categorise_by, pos) + local mobs_found_wide = {} + local mobs_found_close = {} + local num = 0 for _,entity in pairs(minetest.luaentities) do if entity and entity.is_mob then local add_entry = false + --local mob_type = entity.type -- animal / monster / npc + local mob_cat = entity[categorise_by] - if pos and max_distance then + if pos then local mob_pos = entity.object:get_pos() if mob_pos then local distance = vector.distance(pos, mob_pos) - - if distance <= max_distance then - --mcl_log("distance is good") + mcl_log("distance: ".. distance) + if distance <= MOB_SPAWN_ZONE_MIDDLE then + mcl_log("distance is close") + count_mobs_add_entry (mobs_found_close, mob_cat) + count_mobs_add_entry (mobs_found_wide, mob_cat) + add_entry = true + elseif distance <= MOB_SPAWN_ZONE_OUTER then + mcl_log("distance is wide") + count_mobs_add_entry (mobs_found_wide, mob_cat) add_entry = true else - mcl_log("mob_pos: " .. minetest.pos_to_string(mob_pos)) - mcl_log("distance: ".. distance) + --mcl_log("mob_pos: " .. minetest.pos_to_string(mob_pos)) end end - else + count_mobs_add_entry (mobs_found_wide, mob_cat) add_entry = true end - --local mob_type = entity.type -- animal / monster / npc + if add_entry then - local mob_cat = entity[categorise_by] - if mobs_found[mob_cat] then - mobs_found[mob_cat] = mobs_found[mob_cat] + 1 - else - mobs_found[mob_cat] = 1 - end num = num + 1 end end end mcl_log("num: ".. num) - return mobs_found, num + return mobs_found_close, mobs_found_wide, num end local function count_mobs_total_cap(mob_type) @@ -540,7 +554,7 @@ end -local function spawn_check(pos,spawn_def,ignore_caps) +local function spawn_check(pos, spawn_def) if not spawn_def then return end dbg_spawn_attempts = dbg_spawn_attempts + 1 local dimension = mcl_worlds.pos_to_dimension(pos) @@ -563,16 +577,8 @@ local function spawn_check(pos,spawn_def,ignore_caps) local is_leaf = get_item_group(gotten_node, "leaves") ~= 0 local is_bedrock = gotten_node == "mcl_core:bedrock" local is_grass = minetest.get_item_group(gotten_node,"grass_block") ~= 0 - local mob_count_wide = 0 - local mob_count = 0 - if not ignore_caps then - mob_count = count_mobs(pos,MOB_CAP_INNER_RADIUS,mob_type) - mob_count_wide = count_mobs(pos,aoc_range,mob_type) - end if pos and spawn_def - and ( mob_count_wide < (mob_cap[mob_type] or 15) ) - and ( mob_count < 5 ) and pos.y >= spawn_def.min_height and pos.y <= spawn_def.max_height and spawn_def.dimension == dimension @@ -619,7 +625,7 @@ local function spawn_group(p,mob,spawn_on,amount_to_spawn) for i = 1, amount_to_spawn do local sp = vector.offset(nn[math.random(#nn)],0,1,0) - if spawn_check(nn[math.random(#nn)],mob,true) then + if spawn_check(nn[math.random(#nn)],mob) then if mob.type_of_spawning == "water" then sp = get_water_spawn(sp) end @@ -709,21 +715,7 @@ if mobs_spawn then -- Get pos to spawn, x and z are randomised, y is range - local function mob_cap_space (pos, mob_type, mob_counts) - local type_cap = mob_cap[mob_type] or 15 - - - local mob_count_from_total = mob_counts[mob_type] - if not mob_count_from_total then - mcl_log("none of type found. set as 0") - mob_count_from_total = 0 - end - - local cap_space = type_cap - mob_count_from_total - if cap_space < 1 then - cap_space = 0 - end - + local function mob_cap_space (pos, mob_type, mob_counts_close, mob_counts_wide) --type = "monster", --spawn_class = "hostile", @@ -733,34 +725,72 @@ if mobs_spawn then -- type = "animal", -- spawn_class = "water", - --mcl_log("spawn_class: " .. spawn_class) mcl_log("mob_type: " .. mob_type) + + local type_cap = mob_cap[mob_type] or 15 + local close_zone_cap = MOBS_CAP_CLOSE + mcl_log("type_cap: " .. type_cap) - --if mob_type == "animal" then - mcl_log("mob_count_from_total: " .. mob_count_from_total) - --local mob_count = count_mobs(pos,MOB_CAP_INNER_RADIUS,mob_type) - local mob_count_wide = count_mobs(pos,aoc_range,mob_type) + local mob_total_wide = mob_counts_wide[mob_type] + if not mob_total_wide then + mcl_log("none of type found. set as 0") + mob_total_wide = 0 + end - mcl_log("mob_count_wide: " .. mob_count_wide) - if mob_count_from_total ~= mob_count_wide then - mcl_log("A difference in mob count") - else - mcl_log("No difference in mob count") + local cap_space_wide = type_cap - mob_total_wide + if cap_space_wide < 1 then + cap_space_wide = 0 end - --mcl_log("mob_count: " .. mob_count) + mcl_log("cap_space_wide: " .. cap_space_wide) - mcl_log("Cap space available: " .. cap_space) + local mob_total_close = mob_counts_close[mob_type] + if not mob_total_close then + mcl_log("none of type found. set as 0") + mob_total_close = 0 + end + + local cap_space_close = close_zone_cap - mob_total_close + if cap_space_close < 1 then + cap_space_close = 0 + end + + + mcl_log("cap_space_close: " .. cap_space_close) + + --if mob_type == "animal" then --end + mcl_log("mob_total_wide: " .. mob_total_wide) + local mob_count_wide = count_mobs(pos,aoc_range,mob_type) + + + mcl_log("old mob_count_wide: " .. mob_count_wide) + if mob_total_wide ~= mob_count_wide then + mcl_log("A difference in wide mob count") + else + mcl_log("No difference in wide mob count") + end + + mcl_log("mob_total_close: " .. mob_total_close) + local mob_count_close = count_mobs(pos,MOB_CAP_INNER_RADIUS,mob_type) + + mcl_log("old mob_count_close: " .. mob_count_close) + if mob_total_close ~= mob_count_close then + mcl_log("A difference in close mob count") + else + mcl_log("No difference in close mob count") + end + + --and ( mob_count_wide < (mob_cap[mob_type] or 15) ) --and ( mob_count < 5 ) - return cap_space + return cap_space_wide, cap_space_close end local function spawn_a_mob(pos, dimension, y_min, y_max) @@ -784,8 +814,9 @@ if mobs_spawn then table.shuffle(mob_library_worker_table) -- - local mob_counts, total_mobs = count_mobs_all("type", pos, aoc_range) - output_mob_stats(mob_counts, total_mobs) + local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) + output_mob_stats(mob_counts_close, total_mobs) + output_mob_stats(mob_counts_wide, total_mobs) while #mob_library_worker_table > 0 do local mob_chance_offset = (math_round(noise * current_summary_chance + 12345) % current_summary_chance) + 1 @@ -808,10 +839,11 @@ if mobs_spawn then --local spawn_class = minetest.registered_entities[mob_def.name].spawn_class --spawn_class = "water" - local cap_space = mob_cap_space (pos, mob_type, mob_counts) + local cap_space_wide, cap_space_close = mob_cap_space (pos, mob_type, mob_counts_close, mob_counts_wide) --TODO cap check before or after spawn check - cap_space > 0 and - if spawn_check(spawning_position,mob_def) then + if cap_space_close > 0 and cap_space_wide > 0 and spawn_check(spawning_position,mob_def) then + if mob_def.type_of_spawning == "water" then spawning_position = get_water_spawn(spawning_position) if not spawning_position then @@ -834,11 +866,11 @@ if mobs_spawn then if not group_min then group_min = 1 end local amount_to_spawn = math.random(group_min,spawn_in_group) - mcl_log("action", "Spawning quantity: " .. amount_to_spawn) + mcl_log("Spawning quantity: " .. amount_to_spawn) - if amount_to_spawn > cap_space then - mcl_log("Throttle amount to cap space: " .. cap_space) - amount_to_spawn = cap_space + if amount_to_spawn > cap_space_wide then + mcl_log("Throttle amount to cap space: " .. cap_space_wide) + amount_to_spawn = cap_space_wide end if logging then From a2f9ea81cc1055a24b96c9fdbb5eaee11e9f4a67 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 26 Jan 2023 15:25:59 +0000 Subject: [PATCH 04/21] Fix mobs debug crash --- mods/ENTITIES/mcl_mobs/spawning.lua | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index bcb330844..bd0648d52 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -955,10 +955,7 @@ minetest.register_chatcommand("mobstats",{ minetest.chat_send_player(n,"spawning attempts since server start:"..dbg_spawn_attempts) minetest.chat_send_player(n,"successful spawns since server start:"..dbg_spawn_succ) - - --local mob_counts, total_mobs = count_mobs_all("name") -- name - local mob_counts, total_mobs = count_mobs_all("type") - output_mob_stats(mob_counts, total_mobs) - + local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("name") -- Can use "type" + output_mob_stats(mob_counts_wide, total_mobs) end }) From 0267ad2f31e54d04f03d3b211d0d1cee100c273b Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 26 Jan 2023 15:42:27 +0000 Subject: [PATCH 05/21] Decrease change of groups spawning for peacefuls. Refresh total mobs check on spawn. --- mods/ENTITIES/mcl_mobs/spawning.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index bd0648d52..1e8e29df6 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -26,6 +26,7 @@ local table_copy = table.copy local table_remove = table.remove local pairs = pairs +-- TODO Set logger to false as default local LOGGING_ON = minetest.settings:get_bool("mcl_logging_mobs_spawning", true) local function mcl_log (message) if LOGGING_ON then @@ -841,7 +842,6 @@ if mobs_spawn then local cap_space_wide, cap_space_close = mob_cap_space (pos, mob_type, mob_counts_close, mob_counts_wide) - --TODO cap check before or after spawn check - cap_space > 0 and if cap_space_close > 0 and cap_space_wide > 0 and spawn_check(spawning_position,mob_def) then if mob_def.type_of_spawning == "water" then @@ -857,10 +857,8 @@ if mobs_spawn then end --everything is correct, spawn mob - --TODO If spawning, need to recheck total, or add to total mobs... - local object --TODO is not used. remove or use? - --TODO ensure animal spawns are more gradual, remove mob_type ~= "monster" or - if spawn_in_group and (mob_type ~= "monster" or math.random(5) == 1) then + local spawned + if spawn_in_group and (math.random(5) == 1) then local group_min = spawn_in_group_min if not group_min then group_min = 1 end @@ -877,12 +875,16 @@ if mobs_spawn then minetest.log("action", "[mcl_mobs] A group of " ..amount_to_spawn .. " " .. mob_def.name .. " mob spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at " .. minetest.pos_to_string(spawning_position, 1)) end - object = spawn_group(spawning_position,mob_def,{minetest.get_node(vector.offset(spawning_position,0,-1,0)).name}, amount_to_spawn) + spawned = spawn_group(spawning_position,mob_def,{minetest.get_node(vector.offset(spawning_position,0,-1,0)).name}, amount_to_spawn) else if logging then minetest.log("action", "[mcl_mobs] Mob " .. mob_def.name .. " spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at ".. minetest.pos_to_string(spawning_position, 1)) end - object = mcl_mobs.spawn(spawning_position, mob_def.name) + spawned = mcl_mobs.spawn(spawning_position, mob_def.name) + end + if spawned then + mcl_log("We have spawned") + mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) end end From bd3a4ff0dfbccca173f1d07ef88f2ced9ab5c51b Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 26 Jan 2023 16:44:25 +0000 Subject: [PATCH 06/21] Clean up --- mods/ENTITIES/mcl_mobs/spawning.lua | 47 ++++++++++++++++------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 1e8e29df6..a3c28e923 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -39,6 +39,8 @@ local dbg_spawn_attempts = 0 local dbg_spawn_succ = 0 local dbg_spawn_counts = {} +local remove_far = true + local WAIT_FOR_SPAWN_ATTEMPT = 10 local MOB_SPAWN_ZONE_INNER = 24 @@ -48,8 +50,8 @@ local MOB_SPAWN_ZONE_OUTER = 128 --TODO not used yet. Should replace aoc -- range for mob count local MOB_CAP_INNER_RADIUS = 32 local aoc_range = 136 -local remove_far = true +local MISSING_CAP_DEFAULT = 15 local MOBS_CAP_CLOSE = 5 local mob_cap = { @@ -716,6 +718,7 @@ if mobs_spawn then -- Get pos to spawn, x and z are randomised, y is range + local function mob_cap_space (pos, mob_type, mob_counts_close, mob_counts_wide) --type = "monster", @@ -729,7 +732,7 @@ if mobs_spawn then --mcl_log("spawn_class: " .. spawn_class) mcl_log("mob_type: " .. mob_type) - local type_cap = mob_cap[mob_type] or 15 + local type_cap = mob_cap[mob_type] or MISSING_CAP_DEFAULT local close_zone_cap = MOBS_CAP_CLOSE mcl_log("type_cap: " .. type_cap) @@ -768,29 +771,31 @@ if mobs_spawn then mcl_log("mob_total_wide: " .. mob_total_wide) - local mob_count_wide = count_mobs(pos,aoc_range,mob_type) - - - mcl_log("old mob_count_wide: " .. mob_count_wide) - if mob_total_wide ~= mob_count_wide then - mcl_log("A difference in wide mob count") - else - mcl_log("No difference in wide mob count") - end - mcl_log("mob_total_close: " .. mob_total_close) - local mob_count_close = count_mobs(pos,MOB_CAP_INNER_RADIUS,mob_type) - mcl_log("old mob_count_close: " .. mob_count_close) - if mob_total_close ~= mob_count_close then - mcl_log("A difference in close mob count") - else - mcl_log("No difference in close mob count") + + --TODO Remove old checks + local compare_to_old_checks = true + + if compare_to_old_checks then + local mob_count_wide = count_mobs(pos,aoc_range,mob_type) + local mob_count_close = count_mobs(pos,MOB_CAP_INNER_RADIUS,mob_type) + + mcl_log("old mob_count_wide: " .. mob_count_wide) + if mob_total_wide ~= mob_count_wide then + mcl_log("A difference in wide mob count") + else + mcl_log("No difference in wide mob count") + end + + mcl_log("old mob_count_close: " .. mob_count_close) + if mob_total_close ~= mob_count_close then + mcl_log("A difference in close mob count") + else + mcl_log("No difference in close mob count") + end end - - --and ( mob_count_wide < (mob_cap[mob_type] or 15) ) - --and ( mob_count < 5 ) return cap_space_wide, cap_space_close end From c43c723e08416f0e828054bbdd4eda1dc7a18af9 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Fri, 27 Jan 2023 02:47:01 +0000 Subject: [PATCH 07/21] Clean logging etc. --- mods/ENTITIES/mcl_mobs/spawning.lua | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index a3c28e923..67599c4c8 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -740,7 +740,7 @@ if mobs_spawn then local mob_total_wide = mob_counts_wide[mob_type] if not mob_total_wide then - mcl_log("none of type found. set as 0") + --mcl_log("none of type found. set as 0") mob_total_wide = 0 end @@ -749,12 +749,12 @@ if mobs_spawn then cap_space_wide = 0 end - + mcl_log("mob_total_wide: " .. mob_total_wide) mcl_log("cap_space_wide: " .. cap_space_wide) local mob_total_close = mob_counts_close[mob_type] if not mob_total_close then - mcl_log("none of type found. set as 0") + --mcl_log("none of type found. set as 0") mob_total_close = 0 end @@ -763,36 +763,36 @@ if mobs_spawn then cap_space_close = 0 end - + mcl_log("mob_total_close: " .. mob_total_close) mcl_log("cap_space_close: " .. cap_space_close) --if mob_type == "animal" then --end - mcl_log("mob_total_wide: " .. mob_total_wide) - mcl_log("mob_total_close: " .. mob_total_close) + + --TODO Remove old checks local compare_to_old_checks = true if compare_to_old_checks then - local mob_count_wide = count_mobs(pos,aoc_range,mob_type) + local mob_count_wide = count_mobs(pos,MOB_SPAWN_ZONE_OUTER,mob_type) local mob_count_close = count_mobs(pos,MOB_CAP_INNER_RADIUS,mob_type) - mcl_log("old mob_count_wide: " .. mob_count_wide) if mob_total_wide ~= mob_count_wide then + mcl_log("old mob_count_wide: " .. mob_count_wide) mcl_log("A difference in wide mob count") else - mcl_log("No difference in wide mob count") + --mcl_log("No difference in wide mob count") end - mcl_log("old mob_count_close: " .. mob_count_close) if mob_total_close ~= mob_count_close then + mcl_log("old mob_count_close: " .. mob_count_close) mcl_log("A difference in close mob count") else - mcl_log("No difference in close mob count") + --mcl_log("No difference in close mob count") end end From b5c0830060337ac5be002ab7c888ed9762d5f2f8 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Fri, 27 Jan 2023 03:50:00 +0000 Subject: [PATCH 08/21] Restructure code and tidy --- mods/ENTITIES/mcl_mobs/spawning.lua | 89 +++++++++++++++++------------ 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 67599c4c8..d05ab0827 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -729,6 +729,9 @@ if mobs_spawn then -- type = "animal", -- spawn_class = "water", + --if mob_type == "animal" then + --end + --mcl_log("spawn_class: " .. spawn_class) mcl_log("mob_type: " .. mob_type) @@ -766,14 +769,6 @@ if mobs_spawn then mcl_log("mob_total_close: " .. mob_total_close) mcl_log("cap_space_close: " .. cap_space_close) - --if mob_type == "animal" then - --end - - - - - - --TODO Remove old checks local compare_to_old_checks = true @@ -799,30 +794,45 @@ if mobs_spawn then return cap_space_wide, cap_space_close end - local function spawn_a_mob(pos, dimension, y_min, y_max) - --create a disconnected clone of the spawn dictionary - --prevents memory leak - local mob_library_worker_table = table_copy(spawn_dictionary) + local function find_spawning_position(pos) local goal_pos = get_next_mob_spawn_pos(pos) + local y_min, y_max = decypher_limits(pos.y) + local spawning_position_list = find_nodes_in_area_under_air( + {x = goal_pos.x, y = y_min, z = goal_pos.z}, + {x = goal_pos.x, y = y_max, z = goal_pos.z}, + {"group:solid", "group:water", "group:lava"} + ) + if #spawning_position_list <= 0 then + mcl_log("Spawning position isn't good. Do not spawn: " .. minetest.pos_to_string(goal_pos)) + return + end + local spawning_position = spawning_position_list[math_random(1, #spawning_position_list)] + return spawning_position + end + + local function spawn_a_mob(pos) + --create a disconnected clone of the spawn dictionary, prevents memory leak + local mob_library_worker_table = table_copy(spawn_dictionary) + + local spawning_position = find_spawning_position(pos) + if not spawning_position then + return + end + + -- TODO shouldn't this be based on spawning position? + local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) + -- TODO remove output + output_mob_stats(mob_counts_close, total_mobs) + output_mob_stats(mob_counts_wide, total_mobs) + --grab mob that fits into the spawning location --randomly grab a mob, don't exclude any possibilities - local spawning_position_list = find_nodes_in_area_under_air( - {x = goal_pos.x, y = y_min, z = goal_pos.z}, - {x = goal_pos.x, y = y_max, z = goal_pos.z}, - {"group:solid", "group:water", "group:lava"} - ) - if #spawning_position_list <= 0 then return end - local spawning_position = spawning_position_list[math_random(1, #spawning_position_list)] perlin_noise = perlin_noise or minetest_get_perlin(noise_params) local noise = perlin_noise:get_3d(spawning_position) local current_summary_chance = summary_chance - table.shuffle(mob_library_worker_table) - -- - local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) - output_mob_stats(mob_counts_close, total_mobs) - output_mob_stats(mob_counts_wide, total_mobs) + table.shuffle(mob_library_worker_table) while #mob_library_worker_table > 0 do local mob_chance_offset = (math_round(noise * current_summary_chance + 12345) % current_summary_chance) + 1 @@ -834,15 +844,16 @@ if mobs_spawn then mob_chance = mob_library_worker_table[mob_index].chance step_chance = step_chance + mob_chance end - local mob_def = mob_library_worker_table[mob_index] --minetest.log(mob_def.name.." "..step_chance.. " "..mob_chance) + + local mob_def = mob_library_worker_table[mob_index] if mob_def and mob_def.name and minetest.registered_entities[mob_def.name] then - local spawn_in_group = minetest.registered_entities[mob_def.name].spawn_in_group or 4 - local spawn_in_group_min = minetest.registered_entities[mob_def.name].spawn_in_group_min or 1 - local mob_type = minetest.registered_entities[mob_def.name].type + + local mob_def_ent = minetest.registered_entities[mob_def.name] + local mob_type = mob_def_ent.type -- TODO use spawn class for caps - --local spawn_class = minetest.registered_entities[mob_def.name].spawn_class + --local spawn_class = mob_def_ent.spawn_class --spawn_class = "water" local cap_space_wide, cap_space_close = mob_cap_space (pos, mob_type, mob_counts_close, mob_counts_wide) @@ -856,22 +867,24 @@ if mobs_spawn then return end end - if minetest.registered_entities[mob_def.name].can_spawn and not minetest.registered_entities[mob_def.name].can_spawn(spawning_position) then + if mob_def_ent.can_spawn and not mob_def_ent.can_spawn(spawning_position) then minetest.log("warning","[mcl_mobs] mob "..mob_def.name.." refused to spawn at "..minetest.pos_to_string(vector.round(spawning_position))) return end --everything is correct, spawn mob + + local spawn_in_group = mob_def_ent.spawn_in_group or 4 + local spawned if spawn_in_group and (math.random(5) == 1) then - - local group_min = spawn_in_group_min + local group_min = mob_def_ent.spawn_in_group_min or 1 if not group_min then group_min = 1 end local amount_to_spawn = math.random(group_min,spawn_in_group) - mcl_log("Spawning quantity: " .. amount_to_spawn) if amount_to_spawn > cap_space_wide then + mcl_log("Spawning quantity: " .. amount_to_spawn) mcl_log("Throttle amount to cap space: " .. cap_space_wide) amount_to_spawn = cap_space_wide end @@ -879,7 +892,6 @@ if mobs_spawn then if logging then minetest.log("action", "[mcl_mobs] A group of " ..amount_to_spawn .. " " .. mob_def.name .. " mob spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at " .. minetest.pos_to_string(spawning_position, 1)) end - spawned = spawn_group(spawning_position,mob_def,{minetest.get_node(vector.offset(spawning_position,0,-1,0)).name}, amount_to_spawn) else if logging then @@ -887,8 +899,9 @@ if mobs_spawn then end spawned = mcl_mobs.spawn(spawning_position, mob_def.name) end + if spawned then - mcl_log("We have spawned") + --mcl_log("We have spawned") mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) end end @@ -904,22 +917,24 @@ if mobs_spawn then local timer = 0 minetest.register_globalstep(function(dtime) + timer = timer + dtime if timer < WAIT_FOR_SPAWN_ATTEMPT then return end timer = 0 + local players = get_connected_players() local total_mobs = count_mobs_total_cap() 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.") return end --mob cap per player + for _, player in pairs(players) do local pos = player:get_pos() local dimension = mcl_worlds.pos_to_dimension(pos) -- ignore void and unloaded area if dimension ~= "void" and dimension ~= "default" then - local y_min, y_max = decypher_limits(pos.y) - spawn_a_mob(pos, dimension, y_min, y_max) + spawn_a_mob(pos) end end end) From c2ac33ac61a3352f5b13f773cd8f2ab0f5f6cf09 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Fri, 27 Jan 2023 04:08:38 +0000 Subject: [PATCH 09/21] Mobs now can spawn in multiple positions around you per spawning cycle --- mods/ENTITIES/mcl_mobs/spawning.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index d05ab0827..6d769baf4 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -816,6 +816,8 @@ if mobs_spawn then local spawning_position = find_spawning_position(pos) if not spawning_position then + -- TODO do we log to user, or try again. How many times do we try again. + --mcl_log("abandon this") return end @@ -903,6 +905,13 @@ if mobs_spawn then if spawned then --mcl_log("We have spawned") mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) + local new_spawning_position = find_spawning_position(pos) + if new_spawning_position then + mcl_log("Setting new spawning position") + spawning_position = new_spawning_position + else + mcl_log("Cannot set new spawning position") + end end end From 634379dfe97b15f5c1665084d925a037e4c5712e Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Sat, 28 Jan 2023 00:14:08 +0000 Subject: [PATCH 10/21] Retry if failing to find spawn position --- mods/ENTITIES/mcl_mobs/spawning.lua | 52 ++++++++++++++++++----------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 6d769baf4..44ddc6086 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -42,6 +42,7 @@ local dbg_spawn_counts = {} local remove_far = true local WAIT_FOR_SPAWN_ATTEMPT = 10 +local FIND_SPAWN_POS_RETRIES = 10 local MOB_SPAWN_ZONE_INNER = 24 local MOB_SPAWN_ZONE_MIDDLE = 32 @@ -794,19 +795,33 @@ if mobs_spawn then return cap_space_wide, cap_space_close end - local function find_spawning_position(pos) - local goal_pos = get_next_mob_spawn_pos(pos) - local y_min, y_max = decypher_limits(pos.y) - local spawning_position_list = find_nodes_in_area_under_air( - {x = goal_pos.x, y = y_min, z = goal_pos.z}, - {x = goal_pos.x, y = y_max, z = goal_pos.z}, - {"group:solid", "group:water", "group:lava"} - ) - if #spawning_position_list <= 0 then - mcl_log("Spawning position isn't good. Do not spawn: " .. minetest.pos_to_string(goal_pos)) - return - end - local spawning_position = spawning_position_list[math_random(1, #spawning_position_list)] + local function find_spawning_position(pos, max_times) + local spawning_position + + local max_loops = 1 + if max_times then max_loops = max_times end + + local i = 0 + repeat + local goal_pos = get_next_mob_spawn_pos(pos) + local y_min, y_max = decypher_limits(pos.y) + local spawning_position_list = find_nodes_in_area_under_air( + {x = goal_pos.x, y = y_min, z = goal_pos.z}, + {x = goal_pos.x, y = y_max, z = goal_pos.z}, + {"group:solid", "group:water", "group:lava"} + ) + if #spawning_position_list > 0 then + mcl_log("Spawning positions available: " .. minetest.pos_to_string(goal_pos)) + spawning_position = spawning_position_list[math_random(1, #spawning_position_list)] + else + mcl_log("Spawning position isn't good. Do not spawn: " .. minetest.pos_to_string(goal_pos)) + end + i = i + 1 + if i >= max_loops then + mcl_log("Cancel finding spawn positions at: " .. max_loops) + break + end + until spawning_position return spawning_position end @@ -814,22 +829,20 @@ if mobs_spawn then --create a disconnected clone of the spawn dictionary, prevents memory leak local mob_library_worker_table = table_copy(spawn_dictionary) - local spawning_position = find_spawning_position(pos) + local spawning_position = find_spawning_position(pos, FIND_SPAWN_POS_RETRIES) if not spawning_position then - -- TODO do we log to user, or try again. How many times do we try again. + -- TODO do we log to user --mcl_log("abandon this") return end - -- TODO shouldn't this be based on spawning position? - local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) + local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", spawning_position) -- TODO remove output output_mob_stats(mob_counts_close, total_mobs) output_mob_stats(mob_counts_wide, total_mobs) --grab mob that fits into the spawning location --randomly grab a mob, don't exclude any possibilities - perlin_noise = perlin_noise or minetest_get_perlin(noise_params) local noise = perlin_noise:get_3d(spawning_position) local current_summary_chance = summary_chance @@ -858,7 +871,7 @@ if mobs_spawn then --local spawn_class = mob_def_ent.spawn_class --spawn_class = "water" - local cap_space_wide, cap_space_close = mob_cap_space (pos, mob_type, mob_counts_close, mob_counts_wide) + local cap_space_wide, cap_space_close = mob_cap_space (spawning_position, mob_type, mob_counts_close, mob_counts_wide) if cap_space_close > 0 and cap_space_wide > 0 and spawn_check(spawning_position,mob_def) then @@ -875,7 +888,6 @@ if mobs_spawn then end --everything is correct, spawn mob - local spawn_in_group = mob_def_ent.spawn_in_group or 4 local spawned From 1dc7cab6becc08958f2612d041be298db698add0 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Sat, 28 Jan 2023 00:24:33 +0000 Subject: [PATCH 11/21] Add logging --- mods/ENTITIES/mcl_mobs/spawning.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 44ddc6086..325c538c9 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -496,6 +496,8 @@ end local two_pi = 2 * math.pi local function get_next_mob_spawn_pos(pos) + -- TODO We should consider spawning something a little further away sporadically. + -- It would be good for sky farms and variance, rather than all being on the 24 - 32 block away radius local distance = math_random(MOB_SPAWN_ZONE_INNER + 1, MOB_SPAWN_ZONE_MIDDLE) local angle = math_random() * two_pi local xoff = math_round(distance * math_cos(angle)) From 4482068211bcca4185773457e7f97fa27dd20f15 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Sat, 28 Jan 2023 00:44:48 +0000 Subject: [PATCH 12/21] Nerf group spawning until group spawn is based on pack size, not per spawn attempt --- mods/ENTITIES/mobs_mc/cod.lua | 4 ++-- mods/ENTITIES/mobs_mc/cow+mooshroom.lua | 6 +++--- mods/ENTITIES/mobs_mc/dolphin.lua | 4 ++-- mods/ENTITIES/mobs_mc/horse.lua | 2 +- mods/ENTITIES/mobs_mc/llama.lua | 4 ++-- mods/ENTITIES/mobs_mc/tropical_fish.lua | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/mods/ENTITIES/mobs_mc/cod.lua b/mods/ENTITIES/mobs_mc/cod.lua index bc65faebe..eeb1228c8 100644 --- a/mods/ENTITIES/mobs_mc/cod.lua +++ b/mods/ENTITIES/mobs_mc/cod.lua @@ -40,8 +40,8 @@ local cod = { xp_max = 3, armor = 100, rotate = 180, - spawn_in_group_min = 3, - spawn_in_group = 8, + spawn_in_group_min = 2, -- was 3 + spawn_in_group = 4, -- was 8 nerfed until we can cap them properly locally. this is a group size, not a per spawn attempt tilt_swim = true, collisionbox = {-0.3, 0.0, -0.3, 0.3, 0.79, 0.3}, visual = "mesh", diff --git a/mods/ENTITIES/mobs_mc/cow+mooshroom.lua b/mods/ENTITIES/mobs_mc/cow+mooshroom.lua index 1ec06b44b..64f74c516 100644 --- a/mods/ENTITIES/mobs_mc/cow+mooshroom.lua +++ b/mods/ENTITIES/mobs_mc/cow+mooshroom.lua @@ -14,7 +14,7 @@ local cow_def = { xp_max = 3, collisionbox = {-0.45, -0.01, -0.45, 0.45, 1.39, 0.45}, spawn_in_group = 4, - spawn_in_group_min = 3, + spawn_in_group_min = 2, visual = "mesh", mesh = "mobs_mc_cow.b3d", textures = { { @@ -93,8 +93,8 @@ mcl_mobs.register_mob("mobs_mc:cow", cow_def) -- Mooshroom local mooshroom_def = table.copy(cow_def) mooshroom_def.description = S("Mooshroom") -mooshroom_def.spawn_in_group_min = 4 -mooshroom_def.spawn_in_group = 8 +mooshroom_def.spawn_in_group_min = 2 +mooshroom_def.spawn_in_group = 4 mooshroom_def.textures = { {"mobs_mc_mooshroom.png", "mobs_mc_mushroom_red.png"}, {"mobs_mc_mooshroom_brown.png", "mobs_mc_mushroom_brown.png" } } mooshroom_def.on_rightclick = function(self, clicker) if self:feed_tame(clicker, 1, true, false) then return end diff --git a/mods/ENTITIES/mobs_mc/dolphin.lua b/mods/ENTITIES/mobs_mc/dolphin.lua index 0e5c8e7ee..153734c39 100644 --- a/mods/ENTITIES/mobs_mc/dolphin.lua +++ b/mods/ENTITIES/mobs_mc/dolphin.lua @@ -42,8 +42,8 @@ local dolphin = { walk_chance = 100, breath_max = 120, rotate = 180, - spawn_in_group_min = 3, - spawn_in_group = 5, + spawn_in_group_min = 2, -- was 3 + spawn_in_group = 4, -- was 4. nerfed until water has own cap, and it represents max pack size rather than per spawn attempt tilt_swim = true, collisionbox = {-0.3, 0.0, -0.3, 0.3, 0.79, 0.3}, visual = "mesh", diff --git a/mods/ENTITIES/mobs_mc/horse.lua b/mods/ENTITIES/mobs_mc/horse.lua index 7f61627bc..e0f95df4d 100644 --- a/mods/ENTITIES/mobs_mc/horse.lua +++ b/mods/ENTITIES/mobs_mc/horse.lua @@ -125,7 +125,7 @@ local horse = { type = "animal", spawn_class = "passive", spawn_in_group_min = 2, - spawn_in_group = 6, + spawn_in_group = 4, -- was 6. nerfed until group size is a cap rather than per spawn cycle visual = "mesh", mesh = "mobs_mc_horse.b3d", visual_size = {x=3.0, y=3.0}, diff --git a/mods/ENTITIES/mobs_mc/llama.lua b/mods/ENTITIES/mobs_mc/llama.lua index 503f4207b..32bfc97ea 100644 --- a/mods/ENTITIES/mobs_mc/llama.lua +++ b/mods/ENTITIES/mobs_mc/llama.lua @@ -56,8 +56,8 @@ mcl_mobs.register_mob("mobs_mc:llama", { shoot_interval = 5.5, arrow = "mobs_mc:llamaspit", shoot_offset = 1, --3.5 *would* be a good value visually but it somehow messes with the projectiles trajectory - spawn_in_group_min = 4, - spawn_in_group = 6, + spawn_in_group_min = 2, -- was 4 + spawn_in_group = 4, -- was 6 nerfed until we can cap them properly locally. this is a group size, not a per spawn attempt head_swivel = "head.control", bone_eye_height = 11, diff --git a/mods/ENTITIES/mobs_mc/tropical_fish.lua b/mods/ENTITIES/mobs_mc/tropical_fish.lua index bb9b63f64..702c692b8 100644 --- a/mods/ENTITIES/mobs_mc/tropical_fish.lua +++ b/mods/ENTITIES/mobs_mc/tropical_fish.lua @@ -67,7 +67,7 @@ local tropical_fish = { xp_min = 1, xp_max = 3, armor = 100, - spawn_in_group = 9, + spawn_in_group = 4, -- was 9. nerfed until aquatics use own cap rather than animal, and it represents pack size, not per spawn attempt tilt_swim = true, collisionbox = {-0.2, 0.0, -0.2, 0.2, 0.1, 0.2}, visual = "mesh", From 106979e64a4cee912bb861a86d471560cc351f16 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Sat, 28 Jan 2023 02:31:41 +0000 Subject: [PATCH 13/21] Clean up --- mods/ENTITIES/mcl_mobs/spawning.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 325c538c9..d973009e3 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -27,7 +27,7 @@ local table_remove = table.remove local pairs = pairs -- TODO Set logger to false as default -local LOGGING_ON = minetest.settings:get_bool("mcl_logging_mobs_spawning", true) +local LOGGING_ON = minetest.settings:get_bool("mcl_logging_mobs_spawning", false) local function mcl_log (message) if LOGGING_ON then mcl_util.mcl_log (message, "[Mobs spawn]", true) @@ -741,7 +741,7 @@ if mobs_spawn then local type_cap = mob_cap[mob_type] or MISSING_CAP_DEFAULT local close_zone_cap = MOBS_CAP_CLOSE - mcl_log("type_cap: " .. type_cap) + local mob_total_wide = mob_counts_wide[mob_type] @@ -754,8 +754,9 @@ if mobs_spawn then if cap_space_wide < 1 then cap_space_wide = 0 end + mcl_log("wide: " .. mob_total_wide .. "/" .. type_cap) - mcl_log("mob_total_wide: " .. mob_total_wide) + --mcl_log("mob_total_wide: " .. mob_total_wide) mcl_log("cap_space_wide: " .. cap_space_wide) local mob_total_close = mob_counts_close[mob_type] @@ -769,11 +770,12 @@ if mobs_spawn then cap_space_close = 0 end - mcl_log("mob_total_close: " .. mob_total_close) + + mcl_log("close: " .. mob_total_close .. "/" .. close_zone_cap) mcl_log("cap_space_close: " .. cap_space_close) --TODO Remove old checks - local compare_to_old_checks = true + local compare_to_old_checks = false if compare_to_old_checks then local mob_count_wide = count_mobs(pos,MOB_SPAWN_ZONE_OUTER,mob_type) From 044a91e831b45534f8d950027585ff5a12ada10c Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Sat, 28 Jan 2023 23:48:48 +0000 Subject: [PATCH 14/21] Change spawn cap groups to spawn_type to give water it's own spawn cap. --- mods/ENTITIES/mcl_mobs/spawning.lua | 127 ++++++++++++++-------------- 1 file changed, 65 insertions(+), 62 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index d973009e3..706cc1ee0 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -56,10 +56,10 @@ local MISSING_CAP_DEFAULT = 15 local MOBS_CAP_CLOSE = 5 local mob_cap = { - monster = tonumber(minetest.settings:get("mcl_mob_cap_monster")) or 70, - animal = tonumber(minetest.settings:get("mcl_mob_cap_animal")) or 10, + hostile = tonumber(minetest.settings:get("mcl_mob_cap_monster")) or 70, + passive = tonumber(minetest.settings:get("mcl_mob_cap_animal")) or 10, ambient = tonumber(minetest.settings:get("mcl_mob_cap_ambient")) or 15, - water = tonumber(minetest.settings:get("mcl_mob_cap_water")) or 5, --currently unused + water = tonumber(minetest.settings:get("mcl_mob_cap_water")) or 5, water_ambient = tonumber(minetest.settings:get("mcl_mob_cap_water_ambient")) or 20, --currently unused player = tonumber(minetest.settings:get("mcl_mob_cap_player")) or 75, total = tonumber(minetest.settings:get("mcl_mob_cap_total")) or 500, @@ -280,7 +280,7 @@ local function count_mobs_add_entry (mobs_list, mob_cat) end end ---categorise_by can be name or type +--categorise_by can be name or type or spawn_class local function count_mobs_all(categorise_by, pos) local mobs_found_wide = {} local mobs_found_close = {} @@ -736,14 +736,11 @@ if mobs_spawn then --end --mcl_log("spawn_class: " .. spawn_class) - mcl_log("mob_type: " .. mob_type) + local type_cap = mob_cap[mob_type] or MISSING_CAP_DEFAULT local close_zone_cap = MOBS_CAP_CLOSE - - - local mob_total_wide = mob_counts_wide[mob_type] if not mob_total_wide then --mcl_log("none of type found. set as 0") @@ -754,10 +751,6 @@ if mobs_spawn then if cap_space_wide < 1 then cap_space_wide = 0 end - mcl_log("wide: " .. mob_total_wide .. "/" .. type_cap) - - --mcl_log("mob_total_wide: " .. mob_total_wide) - mcl_log("cap_space_wide: " .. cap_space_wide) local mob_total_close = mob_counts_close[mob_type] if not mob_total_close then @@ -770,9 +763,14 @@ if mobs_spawn then cap_space_close = 0 end + if mob_type == "water" then + mcl_log("mob_type: " .. mob_type .. " and pos: " .. minetest.pos_to_string(pos)) + mcl_log("wide: " .. mob_total_wide .. "/" .. type_cap) + mcl_log("cap_space_wide: " .. cap_space_wide) + mcl_log("close: " .. mob_total_close .. "/" .. close_zone_cap) + mcl_log("cap_space_close: " .. cap_space_close) + end - mcl_log("close: " .. mob_total_close .. "/" .. close_zone_cap) - mcl_log("cap_space_close: " .. cap_space_close) --TODO Remove old checks local compare_to_old_checks = false @@ -840,7 +838,7 @@ if mobs_spawn then return end - local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", spawning_position) + local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("spawn_class", spawning_position) -- TODO remove output output_mob_stats(mob_counts_close, total_mobs) output_mob_stats(mob_counts_wide, total_mobs) @@ -869,66 +867,71 @@ if mobs_spawn then if mob_def and mob_def.name and minetest.registered_entities[mob_def.name] then local mob_def_ent = minetest.registered_entities[mob_def.name] - local mob_type = mob_def_ent.type + --local mob_type = mob_def_ent.type + local mob_spawn_class = mob_def_ent.spawn_class + --mcl_log("mob_spawn_class: " .. mob_spawn_class) - -- TODO use spawn class for caps - --local spawn_class = mob_def_ent.spawn_class - --spawn_class = "water" + local cap_space_wide, cap_space_close = mob_cap_space (spawning_position, mob_spawn_class, mob_counts_close, mob_counts_wide) - local cap_space_wide, cap_space_close = mob_cap_space (spawning_position, mob_type, mob_counts_close, mob_counts_wide) + if cap_space_close > 0 and cap_space_wide > 0 then + mcl_log("Cap space available") - if cap_space_close > 0 and cap_space_wide > 0 and spawn_check(spawning_position,mob_def) then - - if mob_def.type_of_spawning == "water" then - spawning_position = get_water_spawn(spawning_position) - if not spawning_position then - minetest.log("warning","[mcl_mobs] no water spawn for mob "..mob_def.name.." found at "..minetest.pos_to_string(vector.round(pos))) + if spawn_check(spawning_position,mob_def) then + if mob_def.type_of_spawning == "water" then + spawning_position = get_water_spawn(spawning_position) + if not spawning_position then + minetest.log("warning","[mcl_mobs] no water spawn for mob "..mob_def.name.." found at "..minetest.pos_to_string(vector.round(pos))) + return + end + end + if mob_def_ent.can_spawn and not mob_def_ent.can_spawn(spawning_position) then + minetest.log("warning","[mcl_mobs] mob "..mob_def.name.." refused to spawn at "..minetest.pos_to_string(vector.round(spawning_position))) return end - end - if mob_def_ent.can_spawn and not mob_def_ent.can_spawn(spawning_position) then - minetest.log("warning","[mcl_mobs] mob "..mob_def.name.." refused to spawn at "..minetest.pos_to_string(vector.round(spawning_position))) - return - end - --everything is correct, spawn mob - local spawn_in_group = mob_def_ent.spawn_in_group or 4 + --everything is correct, spawn mob + local spawn_in_group = mob_def_ent.spawn_in_group or 4 - local spawned - if spawn_in_group and (math.random(5) == 1) then - local group_min = mob_def_ent.spawn_in_group_min or 1 - if not group_min then group_min = 1 end + local spawned + if spawn_in_group and (math.random(5) == 1) then + local group_min = mob_def_ent.spawn_in_group_min or 1 + if not group_min then group_min = 1 end - local amount_to_spawn = math.random(group_min,spawn_in_group) + local amount_to_spawn = math.random(group_min,spawn_in_group) - if amount_to_spawn > cap_space_wide then - mcl_log("Spawning quantity: " .. amount_to_spawn) - mcl_log("Throttle amount to cap space: " .. cap_space_wide) - amount_to_spawn = cap_space_wide - end + if amount_to_spawn > cap_space_wide then + mcl_log("Spawning quantity: " .. amount_to_spawn) + mcl_log("Throttle amount to cap space: " .. cap_space_wide) + amount_to_spawn = cap_space_wide + end - if logging then - minetest.log("action", "[mcl_mobs] A group of " ..amount_to_spawn .. " " .. mob_def.name .. " mob spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at " .. minetest.pos_to_string(spawning_position, 1)) - end - spawned = spawn_group(spawning_position,mob_def,{minetest.get_node(vector.offset(spawning_position,0,-1,0)).name}, amount_to_spawn) - else - if logging then - minetest.log("action", "[mcl_mobs] Mob " .. mob_def.name .. " spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at ".. minetest.pos_to_string(spawning_position, 1)) - end - spawned = mcl_mobs.spawn(spawning_position, mob_def.name) - end - - if spawned then - --mcl_log("We have spawned") - mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) - local new_spawning_position = find_spawning_position(pos) - if new_spawning_position then - mcl_log("Setting new spawning position") - spawning_position = new_spawning_position + if logging then + minetest.log("action", "[mcl_mobs] A group of " ..amount_to_spawn .. " " .. mob_def.name .. " mob spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at " .. minetest.pos_to_string(spawning_position, 1)) + end + spawned = spawn_group(spawning_position,mob_def,{minetest.get_node(vector.offset(spawning_position,0,-1,0)).name}, amount_to_spawn) else - mcl_log("Cannot set new spawning position") + if logging then + minetest.log("action", "[mcl_mobs] Mob " .. mob_def.name .. " spawns on " ..minetest.get_node(vector.offset(spawning_position,0,-1,0)).name .." at ".. minetest.pos_to_string(spawning_position, 1)) + end + spawned = mcl_mobs.spawn(spawning_position, mob_def.name) end + + if spawned then + --mcl_log("We have spawned") + mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) + local new_spawning_position = find_spawning_position(pos, 3) + if new_spawning_position then + mcl_log("Setting new spawning position") + spawning_position = new_spawning_position + else + mcl_log("Cannot set new spawning position") + end + end + else + mcl_log("Spawn check failed") end + else + mcl_log("Cap space full") end end From 9c2f43a2420b1a47242c9894ba62284c3b100a84 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 30 Jan 2023 01:31:58 +0000 Subject: [PATCH 15/21] Limit peaceful spawning and add configurable option --- mods/ENTITIES/mcl_mobs/spawning.lua | 23 ++++++++++++++++++++--- settingtypes.txt | 6 ++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 706cc1ee0..dbb62cd4e 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -59,12 +59,20 @@ local mob_cap = { hostile = tonumber(minetest.settings:get("mcl_mob_cap_monster")) or 70, passive = tonumber(minetest.settings:get("mcl_mob_cap_animal")) or 10, ambient = tonumber(minetest.settings:get("mcl_mob_cap_ambient")) or 15, - water = tonumber(minetest.settings:get("mcl_mob_cap_water")) or 5, + water = tonumber(minetest.settings:get("mcl_mob_cap_water")) or 10, water_ambient = tonumber(minetest.settings:get("mcl_mob_cap_water_ambient")) or 20, --currently unused player = tonumber(minetest.settings:get("mcl_mob_cap_player")) or 75, total = tonumber(minetest.settings:get("mcl_mob_cap_total")) or 500, } +local peaceful_percentage_spawned = tonumber(minetest.settings:get("mcl_mob_peaceful_percentage_spawned")) or 30 + +mcl_log("Mob cap hostile: " .. mob_cap.hostile) +mcl_log("Mob cap water: " .. mob_cap.water) +mcl_log("Mob cap passive: " .. mob_cap.passive) + +mcl_log("Percentage of peacefuls spawned: " .. peaceful_percentage_spawned) + --do mobs spawn? local mobs_spawn = minetest.settings:get_bool("mobs_spawn", true) ~= false local spawn_protected = minetest.settings:get_bool("mobs_spawn_protected") ~= false @@ -869,14 +877,23 @@ if mobs_spawn then local mob_def_ent = minetest.registered_entities[mob_def.name] --local mob_type = mob_def_ent.type local mob_spawn_class = mob_def_ent.spawn_class - --mcl_log("mob_spawn_class: " .. mob_spawn_class) + mcl_log("mob_spawn_class: " .. mob_spawn_class) local cap_space_wide, cap_space_close = mob_cap_space (spawning_position, mob_spawn_class, mob_counts_close, mob_counts_wide) + if cap_space_close > 0 and cap_space_wide > 0 then mcl_log("Cap space available") - if spawn_check(spawning_position,mob_def) then + -- Spawn caps for animals and water creatures fill up rapidly. Need to throttle this somewhat + -- for performance and for early game challenge. We don't want to reduce hostiles though. + local spawn_hostile = (mob_spawn_class == "hostile") + local spawn_passive = (mob_spawn_class == "passive" or mob_spawn_class == "water") and math.random(100) < peaceful_percentage_spawned + -- or not hostile + mcl_log("Spawn_passive: " .. tostring(spawn_passive)) + mcl_log("Spawn_hostile: " .. tostring(spawn_hostile)) + + if (spawn_hostile or spawn_passive) and spawn_check(spawning_position,mob_def) then if mob_def.type_of_spawning == "water" then spawning_position = get_water_spawn(spawning_position) if not spawning_position then diff --git a/settingtypes.txt b/settingtypes.txt index 8a891ea8e..172b31d01 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -115,6 +115,9 @@ mobs_griefing (Mobs change blocks) bool true # If enabled, mobs won't damage particles when they got hurt. mobs_disable_blood (Disable mob damage particles) bool false +#Percentage of peaceful spawn attempts that succeed (default:30) +mcl_mob_peaceful_percentage_spawned (Peaceful percentage success) int 30 0 100 + #Maximum amount mobs (default:500) mcl_mob_cap_total (Global mob cap) int 500 0 2048 @@ -127,6 +130,9 @@ mcl_mob_cap_monster (Mob cap monsters) int 70 0 2048 #Maximum amount of animals that will spawn near a player (default:10) mcl_mob_cap_animal (Mob cap animals) int 10 0 1024 +#Maximum amount of water mobs that will spawn near a player (default:10) +mcl_mob_cap_water (Mob cap water) int 10 0 1024 + #Maximum amount of ambient mobs that will spawn near a player (default:15) mcl_mob_cap_ambient (Mob cap ambient mobs) int 15 0 1024 From 86b1d8bc3e56ce4ef59164dc7e1f1e4973a38a94 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 30 Jan 2023 23:10:37 +0000 Subject: [PATCH 16/21] Add world gen limits to spawning to avoid things spawning past the world gen limits --- mods/ENTITIES/mcl_mobs/spawning.lua | 43 +++++++++++++++++++---------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index dbb62cd4e..1f1ec8bbf 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -55,6 +55,8 @@ local aoc_range = 136 local MISSING_CAP_DEFAULT = 15 local MOBS_CAP_CLOSE = 5 +local SPAWN_MAPGEN_LIMIT = mcl_vars.mapgen_limit - 150 + local mob_cap = { hostile = tonumber(minetest.settings:get("mcl_mob_cap_monster")) or 70, passive = tonumber(minetest.settings:get("mcl_mob_cap_animal")) or 10, @@ -771,7 +773,7 @@ if mobs_spawn then cap_space_close = 0 end - if mob_type == "water" then + if false and mob_type == "water" then mcl_log("mob_type: " .. mob_type .. " and pos: " .. minetest.pos_to_string(pos)) mcl_log("wide: " .. mob_total_wide .. "/" .. type_cap) mcl_log("cap_space_wide: " .. cap_space_wide) @@ -811,21 +813,31 @@ if mobs_spawn then local max_loops = 1 if max_times then max_loops = max_times end + local y_min, y_max = decypher_limits(pos.y) + + mcl_log("mapgen_limit: " .. SPAWN_MAPGEN_LIMIT) local i = 0 repeat local goal_pos = get_next_mob_spawn_pos(pos) - local y_min, y_max = decypher_limits(pos.y) - local spawning_position_list = find_nodes_in_area_under_air( - {x = goal_pos.x, y = y_min, z = goal_pos.z}, - {x = goal_pos.x, y = y_max, z = goal_pos.z}, - {"group:solid", "group:water", "group:lava"} - ) - if #spawning_position_list > 0 then - mcl_log("Spawning positions available: " .. minetest.pos_to_string(goal_pos)) - spawning_position = spawning_position_list[math_random(1, #spawning_position_list)] + + if math.abs(goal_pos.x) <= SPAWN_MAPGEN_LIMIT and math.abs(goal_pos.z) <= SPAWN_MAPGEN_LIMIT then + local spawning_position_list = find_nodes_in_area_under_air( + {x = goal_pos.x, y = y_min, z = goal_pos.z}, + {x = goal_pos.x, y = y_max, z = goal_pos.z}, + {"group:solid", "group:water", "group:lava"} + ) + if #spawning_position_list > 0 then + mcl_log("Spawning positions available: " .. minetest.pos_to_string(goal_pos)) + spawning_position = spawning_position_list[math_random(1, #spawning_position_list)] + else + mcl_log("Spawning position isn't good. Do not spawn: " .. minetest.pos_to_string(goal_pos)) + end + else - mcl_log("Spawning position isn't good. Do not spawn: " .. minetest.pos_to_string(goal_pos)) + mcl_log("Pos outside mapgen limits: " .. minetest.pos_to_string(goal_pos)) end + + i = i + 1 if i >= max_loops then mcl_log("Cancel finding spawn positions at: " .. max_loops) @@ -877,21 +889,22 @@ if mobs_spawn then local mob_def_ent = minetest.registered_entities[mob_def.name] --local mob_type = mob_def_ent.type local mob_spawn_class = mob_def_ent.spawn_class - mcl_log("mob_spawn_class: " .. mob_spawn_class) + + --mcl_log("mob_spawn_class: " .. mob_spawn_class) local cap_space_wide, cap_space_close = mob_cap_space (spawning_position, mob_spawn_class, mob_counts_close, mob_counts_wide) if cap_space_close > 0 and cap_space_wide > 0 then - mcl_log("Cap space available") + --mcl_log("Cap space available") -- Spawn caps for animals and water creatures fill up rapidly. Need to throttle this somewhat -- for performance and for early game challenge. We don't want to reduce hostiles though. local spawn_hostile = (mob_spawn_class == "hostile") local spawn_passive = (mob_spawn_class == "passive" or mob_spawn_class == "water") and math.random(100) < peaceful_percentage_spawned -- or not hostile - mcl_log("Spawn_passive: " .. tostring(spawn_passive)) - mcl_log("Spawn_hostile: " .. tostring(spawn_hostile)) + --mcl_log("Spawn_passive: " .. tostring(spawn_passive)) + --mcl_log("Spawn_hostile: " .. tostring(spawn_hostile)) if (spawn_hostile or spawn_passive) and spawn_check(spawning_position,mob_def) then if mob_def.type_of_spawning == "water" then From cfa276f722af94df2e2a468cefef80b2ae765397 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Tue, 31 Jan 2023 15:55:11 +0000 Subject: [PATCH 17/21] Add settings for percentage of hostile and peaceful groups spawned --- mods/ENTITIES/mcl_mobs/spawning.lua | 33 ++++++++++++++++------------- settingtypes.txt | 6 ++++++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 1f1ec8bbf..2f90a698d 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -46,7 +46,7 @@ local FIND_SPAWN_POS_RETRIES = 10 local MOB_SPAWN_ZONE_INNER = 24 local MOB_SPAWN_ZONE_MIDDLE = 32 -local MOB_SPAWN_ZONE_OUTER = 128 --TODO not used yet. Should replace aoc +local MOB_SPAWN_ZONE_OUTER = 128 -- range for mob count local MOB_CAP_INNER_RADIUS = 32 @@ -68,12 +68,16 @@ local mob_cap = { } local peaceful_percentage_spawned = tonumber(minetest.settings:get("mcl_mob_peaceful_percentage_spawned")) or 30 +local peaceful_group_percentage_spawned = tonumber(minetest.settings:get("mcl_mob_peaceful_group_percentage_spawned")) or 15 +local hostile_group_percentage_spawned = tonumber(minetest.settings:get("mcl_mob_hostile_group_percentage_spawned")) or 20 mcl_log("Mob cap hostile: " .. mob_cap.hostile) mcl_log("Mob cap water: " .. mob_cap.water) mcl_log("Mob cap passive: " .. mob_cap.passive) mcl_log("Percentage of peacefuls spawned: " .. peaceful_percentage_spawned) +mcl_log("Percentage of peaceful spawns are group: " .. peaceful_group_percentage_spawned) +mcl_log("Percentage of hostile spawns are group: " .. hostile_group_percentage_spawned) --do mobs spawn? local mobs_spawn = minetest.settings:get_bool("mobs_spawn", true) ~= false @@ -734,19 +738,10 @@ if mobs_spawn then local function mob_cap_space (pos, mob_type, mob_counts_close, mob_counts_wide) - --type = "monster", - --spawn_class = "hostile", - --type = "animal", - --spawn_class = "passive", - --local cod = { - -- type = "animal", - -- spawn_class = "water", - - --if mob_type == "animal" then - --end - - --mcl_log("spawn_class: " .. spawn_class) - + -- Some mob examples + --type = "monster", spawn_class = "hostile", + --type = "animal", spawn_class = "passive", + --local cod = { type = "animal", spawn_class = "water", local type_cap = mob_cap[mob_type] or MISSING_CAP_DEFAULT local close_zone_cap = MOBS_CAP_CLOSE @@ -773,6 +768,8 @@ if mobs_spawn then cap_space_close = 0 end + --mcl_log("spawn_class: " .. spawn_class) + if false and mob_type == "water" then mcl_log("mob_type: " .. mob_type .. " and pos: " .. minetest.pos_to_string(pos)) mcl_log("wide: " .. mob_total_wide .. "/" .. type_cap) @@ -922,8 +919,14 @@ if mobs_spawn then --everything is correct, spawn mob local spawn_in_group = mob_def_ent.spawn_in_group or 4 + local spawn_group_hostile = (mob_spawn_class == "hostile") and (math.random(100) < hostile_group_percentage_spawned) + local spawn_group_passive = (mob_spawn_class ~= "hostile") and (math.random(100) < peaceful_group_percentage_spawned) + + mcl_log("spawn_group_hostile: " .. tostring(spawn_group_hostile)) + mcl_log("spawn_group_passive: " .. tostring(spawn_group_passive)) + local spawned - if spawn_in_group and (math.random(5) == 1) then + if spawn_in_group and (spawn_group_hostile or spawn_group_passive) then local group_min = mob_def_ent.spawn_in_group_min or 1 if not group_min then group_min = 1 end diff --git a/settingtypes.txt b/settingtypes.txt index 172b31d01..296951491 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -118,6 +118,12 @@ mobs_disable_blood (Disable mob damage particles) bool false #Percentage of peaceful spawn attempts that succeed (default:30) mcl_mob_peaceful_percentage_spawned (Peaceful percentage success) int 30 0 100 +#Percentage of peaceful spawn attempts that are group spawns (default:15) +mcl_mob_peaceful_group_percentage_spawned (Peaceful group percentage) int 15 0 100 + +#Percentage of hostile spawn attempts that are group spawns (default:20) +mcl_mob_hostile_group_percentage_spawned (Hostile group percentage) int 20 0 100 + #Maximum amount mobs (default:500) mcl_mob_cap_total (Global mob cap) int 500 0 2048 From b817c079baf0bd8a2949b93ebd6fa37cfbf2547a Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Tue, 31 Jan 2023 18:04:01 +0000 Subject: [PATCH 18/21] Improve balancing for early game --- mods/ENTITIES/mcl_mobs/spawning.lua | 13 +++++-------- settingtypes.txt | 12 ++++++------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 2f90a698d..3ff5184ca 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -26,7 +26,6 @@ local table_copy = table.copy local table_remove = table.remove local pairs = pairs --- TODO Set logger to false as default local LOGGING_ON = minetest.settings:get_bool("mcl_logging_mobs_spawning", false) local function mcl_log (message) if LOGGING_ON then @@ -34,7 +33,6 @@ local function mcl_log (message) end end - local dbg_spawn_attempts = 0 local dbg_spawn_succ = 0 local dbg_spawn_counts = {} @@ -59,15 +57,15 @@ local SPAWN_MAPGEN_LIMIT = mcl_vars.mapgen_limit - 150 local mob_cap = { hostile = tonumber(minetest.settings:get("mcl_mob_cap_monster")) or 70, - passive = tonumber(minetest.settings:get("mcl_mob_cap_animal")) or 10, + passive = tonumber(minetest.settings:get("mcl_mob_cap_animal")) or 13, ambient = tonumber(minetest.settings:get("mcl_mob_cap_ambient")) or 15, - water = tonumber(minetest.settings:get("mcl_mob_cap_water")) or 10, + water = tonumber(minetest.settings:get("mcl_mob_cap_water")) or 8, water_ambient = tonumber(minetest.settings:get("mcl_mob_cap_water_ambient")) or 20, --currently unused player = tonumber(minetest.settings:get("mcl_mob_cap_player")) or 75, total = tonumber(minetest.settings:get("mcl_mob_cap_total")) or 500, } -local peaceful_percentage_spawned = tonumber(minetest.settings:get("mcl_mob_peaceful_percentage_spawned")) or 30 +local peaceful_percentage_spawned = tonumber(minetest.settings:get("mcl_mob_peaceful_percentage_spawned")) or 35 local peaceful_group_percentage_spawned = tonumber(minetest.settings:get("mcl_mob_peaceful_group_percentage_spawned")) or 15 local hostile_group_percentage_spawned = tonumber(minetest.settings:get("mcl_mob_hostile_group_percentage_spawned")) or 20 @@ -850,8 +848,7 @@ if mobs_spawn then local spawning_position = find_spawning_position(pos, FIND_SPAWN_POS_RETRIES) if not spawning_position then - -- TODO do we log to user - --mcl_log("abandon this") + minetest.log("action", "[Mobs spawn] Cannot find a valid spawn position after retries: " .. FIND_SPAWN_POS_RETRIES) return end @@ -898,7 +895,7 @@ if mobs_spawn then -- Spawn caps for animals and water creatures fill up rapidly. Need to throttle this somewhat -- for performance and for early game challenge. We don't want to reduce hostiles though. local spawn_hostile = (mob_spawn_class == "hostile") - local spawn_passive = (mob_spawn_class == "passive" or mob_spawn_class == "water") and math.random(100) < peaceful_percentage_spawned + local spawn_passive = (mob_spawn_class ~= "hostile") and math.random(100) < peaceful_percentage_spawned -- or not hostile --mcl_log("Spawn_passive: " .. tostring(spawn_passive)) --mcl_log("Spawn_hostile: " .. tostring(spawn_hostile)) diff --git a/settingtypes.txt b/settingtypes.txt index 296951491..a1416ffb7 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -115,8 +115,8 @@ mobs_griefing (Mobs change blocks) bool true # If enabled, mobs won't damage particles when they got hurt. mobs_disable_blood (Disable mob damage particles) bool false -#Percentage of peaceful spawn attempts that succeed (default:30) -mcl_mob_peaceful_percentage_spawned (Peaceful percentage success) int 30 0 100 +#Percentage of peaceful spawn attempts that succeed (default:40) +mcl_mob_peaceful_percentage_spawned (Peaceful percentage success) int 35 0 100 #Percentage of peaceful spawn attempts that are group spawns (default:15) mcl_mob_peaceful_group_percentage_spawned (Peaceful group percentage) int 15 0 100 @@ -133,11 +133,11 @@ mcl_mob_cap_player (Mob cap per player) int 75 0 2048 #Maximum amount of monsters that will spawn near a player (default:70) mcl_mob_cap_monster (Mob cap monsters) int 70 0 2048 -#Maximum amount of animals that will spawn near a player (default:10) -mcl_mob_cap_animal (Mob cap animals) int 10 0 1024 +#Maximum amount of animals that will spawn near a player (default:13) +mcl_mob_cap_animal (Mob cap animals) int 13 0 1024 -#Maximum amount of water mobs that will spawn near a player (default:10) -mcl_mob_cap_water (Mob cap water) int 10 0 1024 +#Maximum amount of water mobs that will spawn near a player (default:8) +mcl_mob_cap_water (Mob cap water) int 8 0 1024 #Maximum amount of ambient mobs that will spawn near a player (default:15) mcl_mob_cap_ambient (Mob cap ambient mobs) int 15 0 1024 From bf50a17f2e9991122036c46886f6221ef7f75fee Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Tue, 31 Jan 2023 18:13:36 +0000 Subject: [PATCH 19/21] Limit spawning of mobs outside y axis as well --- mods/ENTITIES/mcl_mobs/spawning.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 3ff5184ca..32640a5bf 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -815,7 +815,7 @@ if mobs_spawn then repeat local goal_pos = get_next_mob_spawn_pos(pos) - if math.abs(goal_pos.x) <= SPAWN_MAPGEN_LIMIT and math.abs(goal_pos.z) <= SPAWN_MAPGEN_LIMIT then + if math.abs(goal_pos.x) <= SPAWN_MAPGEN_LIMIT and math.abs(pos.y) <= SPAWN_MAPGEN_LIMIT and math.abs(goal_pos.z) <= SPAWN_MAPGEN_LIMIT then local spawning_position_list = find_nodes_in_area_under_air( {x = goal_pos.x, y = y_min, z = goal_pos.z}, {x = goal_pos.x, y = y_max, z = goal_pos.z}, From b2dbf48e9230076e2df9d0ef8d1b9b83485c43f5 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Tue, 31 Jan 2023 18:24:47 +0000 Subject: [PATCH 20/21] Default setting fix --- settingtypes.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settingtypes.txt b/settingtypes.txt index a1416ffb7..74d3fc138 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -115,7 +115,7 @@ mobs_griefing (Mobs change blocks) bool true # If enabled, mobs won't damage particles when they got hurt. mobs_disable_blood (Disable mob damage particles) bool false -#Percentage of peaceful spawn attempts that succeed (default:40) +#Percentage of peaceful spawn attempts that succeed (default:35) mcl_mob_peaceful_percentage_spawned (Peaceful percentage success) int 35 0 100 #Percentage of peaceful spawn attempts that are group spawns (default:15) From d4c2802afb473bc0ebe82d46069cc7510527d121 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 9 Feb 2023 20:43:46 +0000 Subject: [PATCH 21/21] Implement review/testing feedback --- mods/ENTITIES/mcl_mobs/spawning.lua | 80 +++++++++++++---------------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/spawning.lua b/mods/ENTITIES/mcl_mobs/spawning.lua index 32640a5bf..438935d70 100644 --- a/mods/ENTITIES/mcl_mobs/spawning.lua +++ b/mods/ENTITIES/mcl_mobs/spawning.lua @@ -40,7 +40,8 @@ local dbg_spawn_counts = {} local remove_far = true local WAIT_FOR_SPAWN_ATTEMPT = 10 -local FIND_SPAWN_POS_RETRIES = 10 +local FIND_SPAWN_POS_RETRIES = 16 +local FIND_SPAWN_POS_RETRIES_SUCCESS_RESPIN = 8 local MOB_SPAWN_ZONE_INNER = 24 local MOB_SPAWN_ZONE_MIDDLE = 32 @@ -309,14 +310,14 @@ local function count_mobs_all(categorise_by, pos) local mob_pos = entity.object:get_pos() if mob_pos then local distance = vector.distance(pos, mob_pos) - mcl_log("distance: ".. distance) + --mcl_log("distance: ".. distance) if distance <= MOB_SPAWN_ZONE_MIDDLE then - mcl_log("distance is close") + --mcl_log("distance is close") count_mobs_add_entry (mobs_found_close, mob_cat) count_mobs_add_entry (mobs_found_wide, mob_cat) add_entry = true elseif distance <= MOB_SPAWN_ZONE_OUTER then - mcl_log("distance is wide") + --mcl_log("distance is wide") count_mobs_add_entry (mobs_found_wide, mob_cat) add_entry = true else @@ -334,7 +335,7 @@ local function count_mobs_all(categorise_by, pos) end end end - mcl_log("num: ".. num) + --mcl_log("num: ".. num) return mobs_found_close, mobs_found_wide, num end @@ -350,14 +351,27 @@ local function count_mobs_total_cap(mob_type) return num end -local function output_mob_stats(mob_counts, total_mobs) +local function output_mob_stats(mob_counts, total_mobs, chat_display) if (total_mobs) then - minetest.log("action", "Total mobs found: " .. total_mobs) + local total_output = "Total mobs found: " .. total_mobs + if chat_display then + minetest.log(total_output) + else + minetest.log("action", total_output) + end + end + local detailed = "" if mob_counts then for k, v1 in pairs(mob_counts) do - minetest.log("action", "k: " .. tostring(k)) - minetest.log("action", "v1: " .. tostring(v1)) + detailed = detailed .. tostring(k) .. ": " .. tostring(v1) .. "; " + end + end + if detailed and detailed ~= "" then + if chat_display then + minetest.log(detailed) + else + minetest.log("action", detailed) end end end @@ -510,11 +524,13 @@ local two_pi = 2 * math.pi local function get_next_mob_spawn_pos(pos) -- TODO We should consider spawning something a little further away sporadically. -- It would be good for sky farms and variance, rather than all being on the 24 - 32 block away radius - local distance = math_random(MOB_SPAWN_ZONE_INNER + 1, MOB_SPAWN_ZONE_MIDDLE) + local distance = math_random(MOB_SPAWN_ZONE_INNER, MOB_SPAWN_ZONE_MIDDLE) local angle = math_random() * two_pi + + -- TODO Floor xoff and zoff and add 0.5 so it tries to spawn in the middle of the square. Less failed attempts. local xoff = math_round(distance * math_cos(angle)) - local yoff = math_round(distance * math_sin(angle)) - return vector.offset(pos, xoff, 0, yoff) + local zoff = math_round(distance * math_sin(angle)) + return vector.offset(pos, xoff, 0, zoff) end local function decypher_limits(posy) @@ -776,29 +792,6 @@ if mobs_spawn then mcl_log("cap_space_close: " .. cap_space_close) end - - --TODO Remove old checks - local compare_to_old_checks = false - - if compare_to_old_checks then - local mob_count_wide = count_mobs(pos,MOB_SPAWN_ZONE_OUTER,mob_type) - local mob_count_close = count_mobs(pos,MOB_CAP_INNER_RADIUS,mob_type) - - if mob_total_wide ~= mob_count_wide then - mcl_log("old mob_count_wide: " .. mob_count_wide) - mcl_log("A difference in wide mob count") - else - --mcl_log("No difference in wide mob count") - end - - if mob_total_close ~= mob_count_close then - mcl_log("old mob_count_close: " .. mob_count_close) - mcl_log("A difference in close mob count") - else - --mcl_log("No difference in close mob count") - end - end - return cap_space_wide, cap_space_close end @@ -853,9 +846,8 @@ if mobs_spawn then end local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("spawn_class", spawning_position) - -- TODO remove output - output_mob_stats(mob_counts_close, total_mobs) - output_mob_stats(mob_counts_wide, total_mobs) + --output_mob_stats(mob_counts_close, total_mobs) + --output_mob_stats(mob_counts_wide) --grab mob that fits into the spawning location --randomly grab a mob, don't exclude any possibilities @@ -949,7 +941,7 @@ if mobs_spawn then if spawned then --mcl_log("We have spawned") mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("type", pos) - local new_spawning_position = find_spawning_position(pos, 3) + local new_spawning_position = find_spawning_position(pos, FIND_SPAWN_POS_RETRIES_SUCCESS_RESPIN) if new_spawning_position then mcl_log("Setting new spawning position") spawning_position = new_spawning_position @@ -1028,14 +1020,12 @@ end minetest.register_chatcommand("mobstats",{ privs = { debug = true }, func = function(n,param) - minetest.chat_send_player(n,dump(dbg_spawn_counts)) + --minetest.chat_send_player(n,dump(dbg_spawn_counts)) local pos = minetest.get_player_by_name(n):get_pos() - minetest.chat_send_player(n,"mobs within 32 radius of player:"..count_mobs(pos,MOB_CAP_INNER_RADIUS)) - minetest.chat_send_player(n,"total mobs:"..count_mobs_total()) - minetest.chat_send_player(n,"spawning attempts since server start:"..dbg_spawn_attempts) - minetest.chat_send_player(n,"successful spawns since server start:"..dbg_spawn_succ) + minetest.chat_send_player(n,"mobs: within 32 radius of player/total loaded :"..count_mobs(pos,MOB_CAP_INNER_RADIUS) .. "/" .. count_mobs_total()) + minetest.chat_send_player(n,"spawning attempts since server start:" .. dbg_spawn_succ .. "/" .. dbg_spawn_attempts) local mob_counts_close, mob_counts_wide, total_mobs = count_mobs_all("name") -- Can use "type" - output_mob_stats(mob_counts_wide, total_mobs) + output_mob_stats(mob_counts_wide, total_mobs, true) end })