From c6db03267477447fc670689e28d6d66fc98a5683 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Tue, 14 Mar 2023 21:26:31 +0000 Subject: [PATCH 1/4] Rework sculk to be off the xp_step and triggered by events --- mods/ENTITIES/mcl_mobs/mod.conf | 2 +- mods/ENTITIES/mcl_mobs/physics.lua | 12 +++- mods/ITEMS/mcl_sculk/init.lua | 94 +++++++++++++++++++----------- 3 files changed, 72 insertions(+), 36 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/mod.conf b/mods/ENTITIES/mcl_mobs/mod.conf index 0d622f6a9..9c10e9a2b 100644 --- a/mods/ENTITIES/mcl_mobs/mod.conf +++ b/mods/ENTITIES/mcl_mobs/mod.conf @@ -2,4 +2,4 @@ name = mcl_mobs author = PilzAdam description = Adds a mob API for mods to add animals or monsters, etc. depends = mcl_particles -optional_depends = mcl_weather, mcl_explosions, mcl_hunger, mcl_worlds, invisibility, lucky_block, cmi, doc_identifier, mcl_armor, mcl_portals, mcl_experience +optional_depends = mcl_weather, mcl_explosions, mcl_hunger, mcl_worlds, invisibility, lucky_block, cmi, doc_identifier, mcl_armor, mcl_portals, mcl_experience, mcl_sculk diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index 673c08c8a..c6599a28b 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -488,9 +488,19 @@ function mob_class:check_for_death(cause, cmi_cause) self:item_drop(cooked, looting) if ((not self.child) or self.type ~= "animal") and (minetest.get_us_time() - self.xp_timestamp <= math.huge) then - mcl_experience.throw_xp(self.object:get_pos(), math.random(self.xp_min, self.xp_max)) + local pos = self.object:get_pos() + local xp_amount = math.random(self.xp_min, self.xp_max) + + if not mcl_sculk.handle_death(pos, xp_amount) then + --minetest.log("Xp not thrown") + mcl_experience.throw_xp(pos, xp_amount) + else + --minetest.log("xp thrown") + end end end + + end -- execute custom death function diff --git a/mods/ITEMS/mcl_sculk/init.lua b/mods/ITEMS/mcl_sculk/init.lua index 765a01051..487a6bf09 100644 --- a/mods/ITEMS/mcl_sculk/init.lua +++ b/mods/ITEMS/mcl_sculk/init.lua @@ -1,4 +1,7 @@ local S = minetest.get_translator(minetest.get_current_modname()) + +mcl_sculk = {} + local mt_sound_play = minetest.sound_play local spread_to = {"mcl_core:stone","mcl_core:dirt","mcl_core:sand","mcl_core:dirt_with_grass","group:grass_block","mcl_core:andesite","mcl_core:diorite","mcl_core:granite","mcl_core:mycelium","group:dirt","mcl_end:end_stone","mcl_nether:netherrack","mcl_blackstone:basalt","mcl_nether:soul_sand","mcl_blackstone:soul_soil","mcl_crimson:warped_nylium","mcl_crimson:crimson_nylium","mcl_core:gravel"} @@ -102,27 +105,26 @@ local function has_nonsculk(pos) if minetest.get_item_group(minetest.get_node(p).name,"sculk") <= 0 and minetest.get_item_group(minetest.get_node(p).name,"solid") > 0 then return p end end end +local function retrieve_close_spreadable_nodes (p) + local nnn = minetest.find_nodes_in_area(vector.offset(p,-SPREAD_RANGE,-SPREAD_RANGE,-SPREAD_RANGE),vector.offset(p,SPREAD_RANGE,SPREAD_RANGE,SPREAD_RANGE),spread_to) + local nn={} + for _,v in pairs(nnn) do + if has_air(v) then + table.insert(nn,v) + end + end + table.sort(nn,function(a, b) + return vector.distance(p, a) < vector.distance(p, b) + end) + return nn +end -local old_on_step = minetest.registered_entities["mcl_experience:orb"].on_step - -minetest.registered_entities["mcl_experience:orb"].on_step = function(self,dtime) - local p = self.object:get_pos() - local nu = minetest.get_node(vector.offset(p,0,-1,0)) - local ret = old_on_step(self,dtime) - if not self._sculkdrop then - local c = minetest.find_node_near(p,SPREAD_RANGE,{"mcl_sculk:catalyst"}) - if c then - local nnn = minetest.find_nodes_in_area(vector.offset(p,-SPREAD_RANGE,-SPREAD_RANGE,-SPREAD_RANGE),vector.offset(p,SPREAD_RANGE,SPREAD_RANGE,SPREAD_RANGE),spread_to) - local nn={} - for _,v in pairs(nnn) do - if has_air(v) then - table.insert(nn,v) - end - end - table.sort(nn,function(a, b) - return vector.distance(p, a) < vector.distance(p, b) - end) - if nn and #nn > 0 and self._xp > 0 then +local function sculk_stuff (p, xp_amount) + local c = minetest.find_node_near(p,SPREAD_RANGE,{"mcl_sculk:catalyst"}) + if c then + local nn = retrieve_close_spreadable_nodes (p) + if nn and #nn > 0 then + if xp_amount > 0 then local d = math.random(100) --[[ --enable to generate shriekers and sensors if d <= 1 then @@ -136,27 +138,51 @@ minetest.registered_entities["mcl_experience:orb"].on_step = function(self,dtime self.object:remove() return ret else --]] - local r = math.min(math.random(#nn),self._xp) - for i=1,r do - minetest.set_node(nn[i],{name = "mcl_sculk:sculk" }) - set_node_xp(nn[i],math.floor(self._xp / r)) + + + local r = math.min(math.random(#nn), xp_amount) + + + minetest.log("r: ".. r) + for i=1,r do + minetest.set_node(nn[i],{name = "mcl_sculk:sculk" }) + set_node_xp(nn[i],math.floor(xp_amount / r)) + end + for i=1,r do + local p = has_nonsculk(nn[i]) + if p and has_air(p) then + minetest.set_node(vector.offset(p,0,1,0),{name = "mcl_sculk:vein", param2 = 1}) end - for i=1,r do - local p = has_nonsculk(nn[i]) - if p and has_air(p) then - minetest.set_node(vector.offset(p,0,1,0),{name = "mcl_sculk:vein", param2 = 1}) - end - end - set_node_xp(nn[1],get_node_xp(nn[1]) + self._xp % r) - self.object:remove() - return ret + end + set_node_xp(nn[1],get_node_xp(nn[1]) + xp_amount % r) + return true + --self.object:remove() --end end end end - return ret end +function mcl_sculk.handle_death(pos, xp_amount) + --local nu = minetest.get_node(vector.offset(p,0,-1,0)) + local c = minetest.find_node_near(pos ,SPREAD_RANGE,{"mcl_sculk:catalyst"}) + if c then + local nn = retrieve_close_spreadable_nodes (pos) + if nn and #nn > 0 then + return sculk_stuff (pos, xp_amount) + end + + end +end + +minetest.register_on_dieplayer(function(player) + if mcl_sculk.handle_death(player:get_pos(), 5) then + minetest.log("Player is dead. Sculk") + else + minetest.log("Player is dead. not Sculk") + end +end) + minetest.register_node("mcl_sculk:sculk", { description = S("Sculk"), tiles = { From 2c7039fdda4c9038606be99b6518ef88387c9f3e Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Tue, 14 Mar 2023 21:33:14 +0000 Subject: [PATCH 2/4] Clean up code --- mods/ITEMS/mcl_sculk/init.lua | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/mods/ITEMS/mcl_sculk/init.lua b/mods/ITEMS/mcl_sculk/init.lua index 487a6bf09..2b2baee89 100644 --- a/mods/ITEMS/mcl_sculk/init.lua +++ b/mods/ITEMS/mcl_sculk/init.lua @@ -119,7 +119,7 @@ local function retrieve_close_spreadable_nodes (p) return nn end -local function sculk_stuff (p, xp_amount) +local function spread_sculk (p, xp_amount) local c = minetest.find_node_near(p,SPREAD_RANGE,{"mcl_sculk:catalyst"}) if c then local nn = retrieve_close_spreadable_nodes (p) @@ -141,9 +141,8 @@ local function sculk_stuff (p, xp_amount) local r = math.min(math.random(#nn), xp_amount) - - - minetest.log("r: ".. r) + --minetest.log("r: ".. r) + for i=1,r do minetest.set_node(nn[i],{name = "mcl_sculk:sculk" }) set_node_xp(nn[i],math.floor(xp_amount / r)) @@ -164,15 +163,9 @@ local function sculk_stuff (p, xp_amount) end function mcl_sculk.handle_death(pos, xp_amount) + if not pos or not xp_amount then return end --local nu = minetest.get_node(vector.offset(p,0,-1,0)) - local c = minetest.find_node_near(pos ,SPREAD_RANGE,{"mcl_sculk:catalyst"}) - if c then - local nn = retrieve_close_spreadable_nodes (pos) - if nn and #nn > 0 then - return sculk_stuff (pos, xp_amount) - end - - end + return spread_sculk (pos, xp_amount) end minetest.register_on_dieplayer(function(player) From a3f23d0b352c0a929fe9fdb8f8ac3e8f662ccedf Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Tue, 14 Mar 2023 21:37:16 +0000 Subject: [PATCH 3/4] Remove unneeded logging --- mods/ITEMS/mcl_sculk/init.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mods/ITEMS/mcl_sculk/init.lua b/mods/ITEMS/mcl_sculk/init.lua index 2b2baee89..b2ef04152 100644 --- a/mods/ITEMS/mcl_sculk/init.lua +++ b/mods/ITEMS/mcl_sculk/init.lua @@ -142,7 +142,7 @@ local function spread_sculk (p, xp_amount) local r = math.min(math.random(#nn), xp_amount) --minetest.log("r: ".. r) - + for i=1,r do minetest.set_node(nn[i],{name = "mcl_sculk:sculk" }) set_node_xp(nn[i],math.floor(xp_amount / r)) @@ -170,9 +170,9 @@ end minetest.register_on_dieplayer(function(player) if mcl_sculk.handle_death(player:get_pos(), 5) then - minetest.log("Player is dead. Sculk") + --minetest.log("Player is dead. Sculk") else - minetest.log("Player is dead. not Sculk") + --minetest.log("Player is dead. not Sculk") end end) From ef633ce61771e28ebbfcdeeada162676ca593c4a Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 16 Mar 2023 22:01:36 +0000 Subject: [PATCH 4/4] Do not throw XP if in creative --- mods/ENTITIES/mcl_mobs/physics.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index c6599a28b..956eb992c 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -493,7 +493,9 @@ function mob_class:check_for_death(cause, cmi_cause) if not mcl_sculk.handle_death(pos, xp_amount) then --minetest.log("Xp not thrown") - mcl_experience.throw_xp(pos, xp_amount) + if minetest.is_creative_enabled("") ~= true then + mcl_experience.throw_xp(pos, xp_amount) + end else --minetest.log("xp thrown") end