From 7b1d5501c1035091dc3a04cd647ce8b66fb4a9eb Mon Sep 17 00:00:00 2001 From: cora Date: Thu, 13 Oct 2022 21:58:19 +0200 Subject: [PATCH 1/3] lightning: allow change of strike position in callback --- mods/ENVIRONMENT/lightning/init.lua | 55 ++++++++++++++++------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/mods/ENVIRONMENT/lightning/init.lua b/mods/ENVIRONMENT/lightning/init.lua index 32ff721b2..5bd82334d 100644 --- a/mods/ENVIRONMENT/lightning/init.lua +++ b/mods/ENVIRONMENT/lightning/init.lua @@ -107,29 +107,7 @@ local function choose_pos(pos) return pos, pos2 end --- * pos: optional, if not given a random pos will be chosen --- * returns: bool - success if a strike happened -function lightning.strike(pos) - if lightning.auto then - after(rng:next(lightning.interval_low, lightning.interval_high), lightning.strike) - end - - local pos2 - pos, pos2 = choose_pos(pos) - - if not pos then - return false - end - if lightning.on_strike_functions then - for _, func in pairs(lightning.on_strike_functions) do - -- allow on_strike callbacks to destroy entities by re-obtaining objects for each callback - local objects = get_objects_inside_radius(pos2, 3.5) - func(pos, pos2, objects) - end - end -end - -lightning.register_on_strike(function(pos, pos2, objects) +function lightning.strike_func(pos, pos2, objects) local particle_pos = vector.offset(pos2, 0, (lightning.size / 2) + 0.5, 0) local particle_size = lightning.size * 10 local time = 0.2 @@ -230,7 +208,36 @@ lightning.register_on_strike(function(pos, pos2, objects) end end end -end) +end + +-- * pos: optional, if not given a random pos will be chosen +-- * returns: bool - success if a strike happened +function lightning.strike(pos) + if lightning.auto then + after(rng:next(lightning.interval_low, lightning.interval_high), lightning.strike) + end + + local pos2 + pos, pos2 = choose_pos(pos) + + if not pos then + return false + end + local do_strike = true + if lightning.on_strike_functions then + for _, func in pairs(lightning.on_strike_functions) do + -- allow on_strike callbacks to destroy entities by re-obtaining objects for each callback + local objects = get_objects_inside_radius(pos2, 3.5) + local p,p2,stop = func(pos, pos2, objects) + if p then pos = p end + if p2 then pos2 = p2 end + do_strike = do_strike and not stop + end + end + if do_strike then + lightning.strike_func(pos,pos2,get_objects_inside_radius(pos2, 3.5)) + end +end -- if other mods disable auto lightning during initialization, don't trigger the first lightning. after(5, function(dtime) From e80e44612cb068bb5be5c510809202a62f24283b Mon Sep 17 00:00:00 2001 From: cora Date: Thu, 13 Oct 2022 23:29:37 +0200 Subject: [PATCH 2/3] Add lightning rod --- mods/ENVIRONMENT/lightning/init.lua | 8 +- mods/ITEMS/mcl_lightning_rods/init.lua | 73 ++++++++++++++++++ mods/ITEMS/mcl_lightning_rods/mod.conf | 3 + .../textures/mcl_lightning_rods_rod.png | Bin 0 -> 236 bytes 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 mods/ITEMS/mcl_lightning_rods/init.lua create mode 100644 mods/ITEMS/mcl_lightning_rods/mod.conf create mode 100644 mods/ITEMS/mcl_lightning_rods/textures/mcl_lightning_rods_rod.png diff --git a/mods/ENVIRONMENT/lightning/init.lua b/mods/ENVIRONMENT/lightning/init.lua index 5bd82334d..e6973e1c6 100644 --- a/mods/ENVIRONMENT/lightning/init.lua +++ b/mods/ENVIRONMENT/lightning/init.lua @@ -228,9 +228,11 @@ function lightning.strike(pos) for _, func in pairs(lightning.on_strike_functions) do -- allow on_strike callbacks to destroy entities by re-obtaining objects for each callback local objects = get_objects_inside_radius(pos2, 3.5) - local p,p2,stop = func(pos, pos2, objects) - if p then pos = p end - if p2 then pos2 = p2 end + local p,stop = func(pos, pos2, objects) + if p then + pos = p + pos2 = choose_pos(p) + end do_strike = do_strike and not stop end end diff --git a/mods/ITEMS/mcl_lightning_rods/init.lua b/mods/ITEMS/mcl_lightning_rods/init.lua new file mode 100644 index 000000000..a3cbc951f --- /dev/null +++ b/mods/ITEMS/mcl_lightning_rods/init.lua @@ -0,0 +1,73 @@ +local S = minetest.get_translator("mobs_mc") + +local cbox = { + type = "fixed", + fixed = { + { 0/16, -8/16, 0/16, 2/16, 4/16, 2/16 }, + { 0/16, 4/16, 0/16, 3/16, 8/16, 3/16 }, + } +} + +minetest.register_node("mcl_lightning_rods:rod", { + description = S("Lightning Rod"), + _doc_items_longdesc = S("A block that attracts lightning"), + --inventory_image = "mcl_lightning_rods_rod_inv.png", + tiles = { + "mcl_lightning_rods_rod.png", + "mcl_lightning_rods_rod.png", + "mcl_lightning_rods_rod.png", + "mcl_lightning_rods_rod.png", + "mcl_lightning_rods_rod.png", + "mcl_lightning_rods_rod.png", + }, + drawtype = "nodebox", + is_ground_content = false, + paramtype = "light", + paramtype2 = "facedir", + groups = {pickaxey=2,attracts_lightning=1}, + sounds = mcl_sounds.node_sound_metal_defaults(), + node_box = cbox, + selection_box = cbox, + collision_box = cbox, + on_place = function(itemstack, placer, pointed_thing) + if pointed_thing.type ~= "node" then + return itemstack + end + + local p0 = pointed_thing.under + local p1 = pointed_thing.above + local param2 = 0 + + local placer_pos = placer:get_pos() + if placer_pos then + local dir = { + x = p1.x - placer_pos.x, + y = p1.y - placer_pos.y, + z = p1.z - placer_pos.z + } + param2 = minetest.dir_to_facedir(dir) + end + + if p0.y - 1 == p1.y then + param2 = 20 + elseif p0.x - 1 == p1.x then + param2 = 16 + elseif p0.x + 1 == p1.x then + param2 = 12 + elseif p0.z - 1 == p1.z then + param2 = 8 + elseif p0.z + 1 == p1.z then + param2 = 4 + end + + return minetest.item_place(itemstack, placer, pointed_thing, param2) + end, + + sounds = mcl_sounds.node_sound_glass_defaults(), + _mcl_blast_resistance = 0, +}) + +lightning.register_on_strike(function(pos,pos2,objects) + local lr = minetest.find_node_near(pos,128,{"group:attracts_lightning"},true) + return lr,nil +end) diff --git a/mods/ITEMS/mcl_lightning_rods/mod.conf b/mods/ITEMS/mcl_lightning_rods/mod.conf new file mode 100644 index 000000000..cafd9ba5e --- /dev/null +++ b/mods/ITEMS/mcl_lightning_rods/mod.conf @@ -0,0 +1,3 @@ +name = mcl_lightning_rods +author = cora +depends = mcl_sounds, lightning diff --git a/mods/ITEMS/mcl_lightning_rods/textures/mcl_lightning_rods_rod.png b/mods/ITEMS/mcl_lightning_rods/textures/mcl_lightning_rods_rod.png new file mode 100644 index 0000000000000000000000000000000000000000..71a0817029b40ca0c3638407651824cd5b1c315e GIT binary patch literal 236 zcmVzXxl4Npk4p=SdCc<~~ zz}OB{6t8JqO9k7R)(!vK;kGPvwJ_Dd-={lM-8r_#8i~J-Gl#}dRWbtyIQAVu@^M>G zfT^rR1OR#(dPw-33s1eHTLgP10Ba3YL}<+bFj2HOnh?g(c|#0000 Date: Fri, 14 Oct 2022 04:39:51 +0200 Subject: [PATCH 3/3] Add crafting recipe --- mods/ITEMS/mcl_lightning_rods/init.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mods/ITEMS/mcl_lightning_rods/init.lua b/mods/ITEMS/mcl_lightning_rods/init.lua index a3cbc951f..41d9adfbd 100644 --- a/mods/ITEMS/mcl_lightning_rods/init.lua +++ b/mods/ITEMS/mcl_lightning_rods/init.lua @@ -71,3 +71,12 @@ lightning.register_on_strike(function(pos,pos2,objects) local lr = minetest.find_node_near(pos,128,{"group:attracts_lightning"},true) return lr,nil end) + +minetest.register_craft({ + output = "mcl_lightning_rods:rod", + recipe = { + {"", "mcl_copper:copper_ingot",""}, + {"", "mcl_copper:copper_ingot",""}, + {"", "mcl_copper:copper_ingot",""}, + } +})