From f4861ced2ae577f54870ba88159eff862bc8c29f Mon Sep 17 00:00:00 2001 From: tenplus1 Date: Wed, 2 Oct 2024 14:57:07 +0100 Subject: [PATCH] add on_sound node support and example. --- api.lua | 22 ++++++++-- api.txt | 8 ++++ crafts.lua | 54 +++++++++++++++++++++++++ mod.conf | 2 +- textures/mobs_hearing_vines.png | Bin 0 -> 114 bytes textures/mobs_hearing_vines_active.png | Bin 0 -> 126 bytes 6 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 textures/mobs_hearing_vines.png create mode 100644 textures/mobs_hearing_vines_active.png diff --git a/api.lua b/api.lua index a8e34df..906fa7c 100644 --- a/api.lua +++ b/api.lua @@ -18,7 +18,7 @@ end mobs = { mod = "redo", - version = "20241001", + version = "20241002", spawning_mobs = {}, translate = S, invis = minetest.global_exists("invisibility") and invisibility or {}, @@ -4794,13 +4794,27 @@ if settings:get_bool("mobs_can_hear") ~= false then def.loudness = def.gain - (bit * def.distance) -- run custom on_sound function if heard - if def.loudness > 0 then - ent.on_sound(ent, def) - end + if def.loudness > 0 then ent.on_sound(ent, def) end end end end + -- find nodes that can hear up to 8 blocks away + local dist = min(def.max_hear_distance, 8) + local ps = minetest.find_nodes_in_area( + vector.subtract(def.pos, dist), + vector.add(def.pos, dist), {"group:on_sound"}) + + if #ps > 0 then + + for n = 1, #ps do + + local ndef = minetest.registered_nodes[minetest.get_node(ps[n]).name] + + if ndef and ndef.on_sound then ndef.on_sound(ps[n], def) end + end + end + return old_sound_play(spec, param, eph) end end diff --git a/api.txt b/api.txt index 3e3a08c..71ddb8a 100644 --- a/api.txt +++ b/api.txt @@ -357,6 +357,14 @@ enhance mob functionality and have them do many interesting things: 'max_hear_distance' max distance sound can be heard from source. +Hearing Nodes +------------- + +If a node has the {on_sound = 1} group and an on_sound() function set as above then +nodes within 8 blocks of a sound will be activated and the function run. Check the +"mobs:hearing_vines" node as an example which has mesecons support when active. + + Internal Variables ------------------ diff --git a/crafts.lua b/crafts.lua index 91b824d..2203f08 100644 --- a/crafts.lua +++ b/crafts.lua @@ -421,3 +421,57 @@ minetest.register_craft({ recipe = "mobs:meatblock_raw", cooktime = 30 }) + +-- hearing vines (if mesecons active it acts like blinkyplant) + +local mod_mese = minetest.get_modpath("mesecons") + +minetest.register_node("mobs:hearing_vines", { + description = S("Hearing Vines"), + drawtype = "firelike", + waving = 1, + tiles = {"mobs_hearing_vines.png"}, + inventory_image = "mobs_hearing_vines.png", + wield_image = "mobs_hearing_vines.png", + paramtype = "light", + sunlight_propagates = true, + walkable = false, + buildable_to = true, + groups = {snappy = 3, flammable = 3, attached_node = 1, on_sound = 1}, + sounds = mobs.node_sound_leaves_defaults(), + selection_box = { + type = "fixed", fixed = {-6 / 16, -0.5, -6 / 16, 6 / 16, -0.25, 6 / 16}, + }, + on_sound = function(pos, def) + minetest.set_node(pos, {name = "mobs:hearing_vines_active"}) + end +}) + +minetest.register_node("mobs:hearing_vines_active", { + description = S("Active Hearing Vines"), + drawtype = "firelike", + waving = 1, + tiles = {"mobs_hearing_vines_active.png"}, + inventory_image = "mobs_hearing_vines_active.png", + wield_image = "mobs_hearing_vines_active.png", + paramtype = "light", + sunlight_propagates = true, + walkable = false, + buildable_to = true, + light_source = 1, + damage_per_second = 4, + drop = "mobs:hearing_vines", + groups = {snappy = 3, flammable = 3, attached_node = 1, not_in_creative_inventory = 1}, + sounds = mobs.node_sound_leaves_defaults(), + selection_box = { + type = "fixed", fixed = {-6 / 16, -0.5, -6 / 16, 6 / 16, -0.25, 6 / 16}, + }, + on_construct = function(pos) + minetest.get_node_timer(pos):start(1) + if mod_mese then mesecon.receptor_on(pos) end + end, + on_timer = function(pos) + minetest.set_node(pos, {name = "mobs:hearing_vines"}) + if mod_mese then mesecon.receptor_off(pos) end + end +}) diff --git a/mod.conf b/mod.conf index 9ba31aa..f6353e8 100644 --- a/mod.conf +++ b/mod.conf @@ -1,4 +1,4 @@ name = mobs description = Adds a mob api for mods to add animals or monsters etc. -optional_depends = default, tnt, invisibility, lucky_block, cmi, toolranks, pathfinder, player_api, mtobjid, visual_harm_1ndicators, mcl_sounds +optional_depends = default, tnt, invisibility, lucky_block, cmi, toolranks, pathfinder, player_api, mtobjid, visual_harm_1ndicators, mcl_sounds, mesecons min_minetest_version = 5.0 diff --git a/textures/mobs_hearing_vines.png b/textures/mobs_hearing_vines.png new file mode 100644 index 0000000000000000000000000000000000000000..97febe358934cf9f29fe26b5af50dcdf4811eab5 GIT binary patch literal 114 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!93?!50ihlx9oB=)|u0UGSQ(P%fVa^1@dq56j zNswPKgTu2MX+Vy+r;B3<$Mxid1tJ^NHmDhRFfcMtILO7o_l@!EQR5kcKqU;Gu6{1- HoD!M<5IY+C literal 0 HcmV?d00001 diff --git a/textures/mobs_hearing_vines_active.png b/textures/mobs_hearing_vines_active.png new file mode 100644 index 0000000000000000000000000000000000000000..179aa73ceeeac1c16f41325c0296c38922eda009 GIT binary patch literal 126 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!93?!50ihlx9JOMr-u0UGSQ(P%f;p+aTrE5Pu z0`eJ4g8YIR9G=}s19B8RT^vI=t|uobh<`9JkoX|+!9d10%{O5J15+EzHI^_2rN7KJ UD^A>A3{=bD>FVdQ&MBb@03u}~5dZ)H literal 0 HcmV?d00001