1
0
mirror of https://codeberg.org/tenplus1/mobs_redo.git synced 2025-01-09 17:30:21 +01:00

tweak mob hear function

This commit is contained in:
tenplus1 2024-10-01 09:08:52 +01:00
parent 64c6085f09
commit 1dd81eb008
2 changed files with 19 additions and 23 deletions

33
api.lua
View File

@ -18,7 +18,7 @@ end
mobs = { mobs = {
mod = "redo", mod = "redo",
version = "20240917", version = "20241001",
spawning_mobs = {}, spawning_mobs = {},
translate = S, translate = S,
invis = minetest.global_exists("invisibility") and invisibility or {}, invis = minetest.global_exists("invisibility") and invisibility or {},
@ -4743,14 +4743,16 @@ if settings:get_bool("mobs_can_hear") ~= false then
local def = {} ; param = param or {} local def = {} ; param = param or {}
-- store sound position -- store sound position (ignore player and object positioning as background)
if param.pos then if param.pos then
def.pos = param.pos def.pos = param.pos
elseif param.object then -- elseif param.object then
def.pos = param.object:get_pos() -- def.pos = param.object:get_pos()
elseif param.to_player then -- def.object = param.object
local player = minetest.get_player_by_name(param.to_player) -- elseif param.to_player then
def.pos = player and player:get_pos() -- local player = minetest.get_player_by_name(param.to_player)
-- def.pos = player and player:get_pos()
-- def.player = param.to_player
end end
-- if no position found use default function -- if no position found use default function
@ -4767,12 +4769,7 @@ if settings:get_bool("mobs_can_hear") ~= false then
def.gain = spec.gain or param.gain or 1.0 def.gain = spec.gain or param.gain or 1.0
end end
-- store player name or object reference --print("==", def.sound)
if param.to_player then
def.player = param.to_player
elseif param.object then
def.object = param.object
end
def.max_hear_distance = param.max_hear_distance or 32 def.max_hear_distance = param.max_hear_distance or 32
@ -4793,13 +4790,13 @@ if settings:get_bool("mobs_can_hear") ~= false then
def.distance = get_distance(def.pos, obj:get_pos()) def.distance = get_distance(def.pos, obj:get_pos())
local bit = def.gain / def.max_hear_distance local bit = def.gain / def.max_hear_distance
local rem = def.max_hear_distance - def.distance
-- loudness ranges from 0 (cannot hear) to 1.0 (close to source) def.loudness = def.gain - (bit * def.distance)
def.loudness = (bit * rem) / def.gain
-- run custom on_sound function -- run custom on_sound function if heard
ent.on_sound(ent, def) if def.loudness > 0 then
ent.on_sound(ent, def)
end
end end
end end
end end

View File

@ -348,13 +348,12 @@ enhance mob functionality and have them do many interesting things:
'on_sound' (self, def) called when mob is inside the hearing distance of 'on_sound' (self, def) called when mob is inside the hearing distance of
a sound, passes a def table containing: a sound, passes a def table containing:
'sound' the sound being played, 'sound' the sound being played,
'pos' position the sound originated, 'pos' position the sound originated (only sounds with pos detected),
'gain' original gain of sound, 'gain' original gain of sound,
'distance' distance of mob from sound source, 'distance' distance of mob from sound source,
'loudness' how loud sound is to mob (0 = cant hear, 1.0 = near sound), this 'loudness' how loud sound is to mob (percentage of gain heard at distance)
would be used as the main value inside on_sound function, 'player' player name sound originated, [NOT IN USE]
'player' player name sound originated, 'object' object reference sound originated, [NOT IN USE]
'object' object reference sound originated,
'max_hear_distance' max distance sound can be heard from source. 'max_hear_distance' max distance sound can be heard from source.