Move raycast_line_of_sight function to mcl_mobs/functions.lua, rework group spawn point selection code, add line of sight check to group origin

This commit is contained in:
teknomunk 2024-07-07 19:17:19 -05:00
parent 99314fd5e0
commit b6c5893a30
4 changed files with 50 additions and 36 deletions

View file

@ -0,0 +1,22 @@
function mcl_mobs.check_line_of_sight(origin, target)
local raycast = minetest.raycast(origin, target, false, true)
local los_blocked = false
for hitpoint in raycast do
if hitpoint.type == "node" then
--TODO type object could block vision, for example chests
local node = minetest.get_node(minetest.get_pointed_thing_position(hitpoint))
if node.name ~= "air" then
local nodef = minetest.registered_nodes[node.name]
if nodef and nodef.walkable then
los_blocked = true
break
end
end
end
end
return not los_blocked
end

View file

@ -6,6 +6,9 @@ local modname = minetest.get_current_modname()
local path = minetest.get_modpath(modname) local path = minetest.get_modpath(modname)
local S = minetest.get_translator(modname) local S = minetest.get_translator(modname)
mcl_mobs.fallback_node = minetest.registered_aliases["mapgen_dirt"] or "mcl_core:dirt" mcl_mobs.fallback_node = minetest.registered_aliases["mapgen_dirt"] or "mcl_core:dirt"
dofile(path .. "/functions.lua")
--api and helpers --api and helpers
-- effects: sounds and particles mostly -- effects: sounds and particles mostly
dofile(path .. "/effects.lua") dofile(path .. "/effects.lua")

View file

@ -20,6 +20,7 @@ local function atan(x)
return atann(x) return atann(x)
end end
end end
local raycast_line_of_sight = mcl_mobs.check_line_of_sight
local registered_fallback_node = minetest.registered_nodes[mcl_mobs.fallback_node] local registered_fallback_node = minetest.registered_nodes[mcl_mobs.fallback_node]
@ -77,28 +78,6 @@ function mob_class:is_node_waterhazard(nodename)
end end
local function raycast_line_of_sight (origin, target)
local raycast = minetest.raycast(origin, target, false, true)
local los_blocked = false
for hitpoint in raycast do
if hitpoint.type == "node" then
--TODO type object could block vision, for example chests
local node = minetest.get_node(minetest.get_pointed_thing_position(hitpoint))
if node.name ~= "air" then
local nodef = minetest.registered_nodes[node.name]
if nodef and nodef.walkable then
los_blocked = true
break
end
end
end
end
return not los_blocked
end
function mob_class:target_visible(origin) function mob_class:target_visible(origin)
if not origin then return end if not origin then return end

View file

@ -33,6 +33,7 @@ local vector_floor = vector.floor
local table_copy = table.copy local table_copy = table.copy
local table_remove = table.remove local table_remove = table.remove
local pairs = pairs local pairs = pairs
local check_line_of_sight = mcl_mobs.check_line_of_sight
local LOGGING_ON = minetest.settings:get_bool("mcl_logging_mobs_spawning", false) local LOGGING_ON = minetest.settings:get_bool("mcl_logging_mobs_spawning", false)
local function mcl_log (message, property) local function mcl_log (message, property)
@ -792,12 +793,22 @@ local function spawn_group(p, mob, spawn_on, amount_to_spawn, parent_state)
-- spawn in a given spot. -- spawn in a given spot.
local o local o
while amount_to_spawn > 0 and #nn > 0 do while amount_to_spawn > 0 and #nn > 0 do
-- Find the next valid group spawn point
local sp
while #nn > 0 and not sp do
-- Select the next spawn position -- Select the next spawn position
local sp = vector.offset(nn[#nn],0,1,0) sp = vector.offset(nn[#nn],0,1,0)
nn[#nn] = nil nn[#nn] = nil
if spawn_protected and minetest.is_protected(sp, "") then
sp = nil
elseif not check_line_of_sight(p, sp) then
sp = nil
end
end
if not sp then return o end
-- Spawning prohibited in protected areas -- Spawning prohibited in protected areas
if not (spawn_protected and minetest.is_protected(sp, "")) then
local state, node = build_state_for_position(sp, parent_state) local state, node = build_state_for_position(sp, parent_state)
if spawn_check(sp, state, node, mob) then if spawn_check(sp, state, node, mob) then
@ -814,7 +825,6 @@ local function spawn_group(p, mob, spawn_on, amount_to_spawn, parent_state)
end end
end end
end end
end
return o return o
end end