From cb2373f0896becd09f92325d74bf24943b117c1c Mon Sep 17 00:00:00 2001 From: Jordan Leppert Date: Thu, 16 Dec 2021 09:10:36 +0000 Subject: [PATCH] Trunks: Prevent crashing when unknown nodes exist (#9) Extension of commit 9ed47715159962cd42f69c441dcc9eee150e4f66 Co-authored-by: OgelGames --- trunks/generating.lua | 184 +++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 111 deletions(-) diff --git a/trunks/generating.lua b/trunks/generating.lua index 5decb7c..145a945 100644 --- a/trunks/generating.lua +++ b/trunks/generating.lua @@ -4,11 +4,13 @@ -- TWiGS ----------------------------------------------------------------------------------------------- -local fakenode = { - name = "default:stone", -- could be anything that's guaranteed to exist at mapgen time, and isn't buildable_to - param1 = 0, - param2 = 0 -} +function check_node_buildable_to(pos) + local node = minetest.get_node(pos) + local def = minetest.registered_nodes[node.name] + if def then + return def.buildable_to + end +end abstract_trunks.place_twig = function(pos) local twig_size = math.random(1,27) @@ -23,25 +25,6 @@ abstract_trunks.place_twig = function(pos) local west = {x=pos.x-1, y=pos.y+1, z=pos.z } local north_west = {x=pos.x-1, y=pos.y+1, z=pos.z+1} - local node_here = minetest.get_node(right_here) - local node_north = minetest.get_node(north) - local node_n_e = minetest.get_node(north_east) - local node_east = minetest.get_node(east) - local node_s_e = minetest.get_node(south_east) - local node_south = minetest.get_node(south) - local node_s_w = minetest.get_node(south_west) - local node_west = minetest.get_node(west) - local node_n_w = minetest.get_node(north_west) - - node_north = minetest.registered_nodes[node_north.name] and node_north or fakenode - node_n_e = minetest.registered_nodes[node_n_e.name] and node_n_e or fakenode - node_east = minetest.registered_nodes[node_east.name] and node_east or fakenode - node_s_e = minetest.registered_nodes[node_s_e.name] and node_s_e or fakenode - node_south = minetest.registered_nodes[node_south.name] and node_south or fakenode - node_s_w = minetest.registered_nodes[node_s_w.name] and node_s_w or fakenode - node_west = minetest.registered_nodes[node_west.name] and node_west or fakenode - node_n_w = minetest.registered_nodes[node_n_w.name] and node_n_w or fakenode - -- small twigs if twig_size <= 16 then minetest.swap_node(right_here, {name="trunks:twig_"..math.random(1,4), param2=math.random(0,3)}) @@ -50,115 +33,115 @@ abstract_trunks.place_twig = function(pos) if Big_Twigs == true then -- big twig 1 if twig_size == 17 then - if not (minetest.registered_nodes[minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z+1}).name].buildable_to - or minetest.registered_nodes[minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z}).name].buildable_to) then + if not (check_node_buildable_to({x=pos.x+1,y=pos.y,z=pos.z+1}) + or check_node_buildable_to({x=pos.x+1,y=pos.y,z=pos.z})) then - if minetest.registered_nodes[node_here.name].buildable_to then + if check_node_buildable_to(right_here) then minetest.swap_node(right_here, {name="trunks:twig_5"}) end - if minetest.registered_nodes[node_n_e.name].buildable_to then + if check_node_buildable_to(north_east) then minetest.swap_node(north_east, {name="trunks:twig_7"}) end - if minetest.registered_nodes[node_east.name].buildable_to then + if check_node_buildable_to(east) then minetest.swap_node(east, {name="trunks:twig_8"}) end end elseif twig_size == 18 then - if not (minetest.registered_nodes[minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z-1}).name].buildable_to - or minetest.registered_nodes[minetest.get_node({x=pos.x,y=pos.y,z=pos.z-1}).name].buildable_to) then + if not (check_node_buildable_to({x=pos.x+1,y=pos.y,z=pos.z-1}) + or check_node_buildable_to({x=pos.x,y=pos.y,z=pos.z-1})) then - if minetest.registered_nodes[node_here.name].buildable_to then + if check_node_buildable_to(right_here) then minetest.swap_node(right_here, {name="trunks:twig_5", param2=1}) end - if minetest.registered_nodes[node_s_e.name].buildable_to then + if check_node_buildable_to(south_east) then minetest.swap_node(south_east, {name="trunks:twig_7", param2=1}) end - if minetest.registered_nodes[node_south.name].buildable_to then + if check_node_buildable_to(south) then minetest.swap_node(south, {name="trunks:twig_8", param2=1}) end end elseif twig_size == 19 then - if not (minetest.registered_nodes[minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z-1}).name].buildable_to - or minetest.registered_nodes[minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z}).name].buildable_to) then + if not (check_node_buildable_to({x=pos.x+1,y=pos.y,z=pos.z-1}) + or check_node_buildable_to({x=pos.x-1,y=pos.y,z=pos.z})) then - if minetest.registered_nodes[node_here.name].buildable_to then + if check_node_buildable_to(right_here) then minetest.swap_node(right_here, {name="trunks:twig_5", param2=2}) end - if minetest.registered_nodes[node_s_w.name].buildable_to then + if check_node_buildable_to(south_west) then minetest.swap_node(south_west, {name="trunks:twig_7", param2=2}) end - if minetest.registered_nodes[node_west.name].buildable_to then + if check_node_buildable_to(west) then minetest.swap_node(west, {name="trunks:twig_8", param2=2}) end end elseif twig_size == 20 then - if not (minetest.registered_nodes[minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z+1}).name].buildable_to - or minetest.registered_nodes[minetest.get_node({x=pos.x,y=pos.y,z=pos.z+1}).name].buildable_to) then + if not (check_node_buildable_to({x=pos.x-1,y=pos.y,z=pos.z+1}) + or check_node_buildable_to({x=pos.x,y=pos.y,z=pos.z+1})) then - if minetest.registered_nodes[node_here.name].buildable_to then + if check_node_buildable_to(right_here) then minetest.swap_node(right_here, {name="trunks:twig_5", param2=3}) end - if minetest.registered_nodes[node_n_w.name].buildable_to then + if check_node_buildable_to(north_west) then minetest.swap_node(north_west, {name="trunks:twig_7", param2=3}) end - if minetest.registered_nodes[node_north.name].buildable_to then + if check_node_buildable_to(north) then minetest.swap_node(north, {name="trunks:twig_8", param2=3}) end end -- big twig 2 elseif twig_size == 21 then - if not (minetest.registered_nodes[minetest.get_node({x=pos.x,y=pos.y,z=pos.z+1}).name].buildable_to - or minetest.registered_nodes[minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z+1}).name].buildable_to) then + if not (check_node_buildable_to({x=pos.x,y=pos.y,z=pos.z+1}) + or check_node_buildable_to({x=pos.x+1,y=pos.y,z=pos.z+1})) then - if minetest.registered_nodes[node_here.name].buildable_to then + if check_node_buildable_to(right_here) then minetest.swap_node(right_here, {name="trunks:twig_9"}) end - if minetest.registered_nodes[node_north.name].buildable_to then + if check_node_buildable_to(north) then minetest.swap_node(north, {name="trunks:twig_10"}) end - if minetest.registered_nodes[node_n_e.name].buildable_to then + if check_node_buildable_to(north_east) then minetest.swap_node(north_east, {name="trunks:twig_11"}) end end elseif twig_size == 22 then - if not (minetest.registered_nodes[minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z}).name].buildable_to - or minetest.registered_nodes[minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z-1}).name].buildable_to) then + if not (check_node_buildable_to({x=pos.x+1,y=pos.y,z=pos.z}) + or check_node_buildable_to({x=pos.x+1,y=pos.y,z=pos.z-1})) then - if minetest.registered_nodes[node_here.name].buildable_to then + if check_node_buildable_to(right_here) then minetest.swap_node(right_here, {name="trunks:twig_9", param2=1}) end - if minetest.registered_nodes[node_east.name].buildable_to then + if check_node_buildable_to(east) then minetest.swap_node(east, {name="trunks:twig_10", param2=1}) end - if minetest.registered_nodes[node_s_e.name].buildable_to then + if check_node_buildable_to(south_east) then minetest.swap_node(south_east, {name="trunks:twig_11", param2=1}) end end elseif twig_size == 23 then - if not (minetest.registered_nodes[minetest.get_node({x=pos.x,y=pos.y,z=pos.z-1}).name].buildable_to - or minetest.registered_nodes[minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z-1}).name].buildable_to) then + if not (check_node_buildable_to({x=pos.x,y=pos.y,z=pos.z-1}) + or check_node_buildable_to({x=pos.x-1,y=pos.y,z=pos.z-1})) then - if minetest.registered_nodes[node_here.name].buildable_to then + if check_node_buildable_to(right_here) then minetest.swap_node(right_here, {name="trunks:twig_9", param2=2}) end - if minetest.registered_nodes[node_south.name].buildable_to then + if check_node_buildable_to(south) then minetest.swap_node(south, {name="trunks:twig_10", param2=2}) end - if minetest.registered_nodes[node_s_w.name].buildable_to then + if check_node_buildable_to(south_west) then minetest.swap_node(south_west, {name="trunks:twig_11", param2=2}) end end elseif twig_size == 24 then - if not (minetest.registered_nodes[minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z}).name].buildable_to - or minetest.registered_nodes[minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z+1}).name].buildable_to) then + if not (check_node_buildable_to({x=pos.x-1,y=pos.y,z=pos.z}) + or check_node_buildable_to({x=pos.x-1,y=pos.y,z=pos.z+1})) then - if minetest.registered_nodes[node_here.name].buildable_to then + if check_node_buildable_to(right_here) then minetest.swap_node(right_here, {name="trunks:twig_9", param2=3}) end - if minetest.registered_nodes[node_west.name].buildable_to then + if check_node_buildable_to(west) then minetest.swap_node(west, {name="trunks:twig_10", param2=3}) end - if minetest.registered_nodes[node_n_w.name].buildable_to then + if check_node_buildable_to(north_west) then minetest.swap_node(north_west, {name="trunks:twig_11", param2=3}) end end @@ -256,16 +239,7 @@ abstract_trunks.place_trunk = function(pos) local east = {x=pos.x+1, y=pos.y+1, z=pos.z} local east2 = {x=pos.x+2, y=pos.y+1, z=pos.z} - local node_here = minetest.get_node(right_here) - local node_north = minetest.get_node(north) - local node_north2 = minetest.get_node(north2) - local node_south = minetest.get_node(south) - local node_south2 = minetest.get_node(south2) - local node_west = minetest.get_node(west) - local node_west2 = minetest.get_node(west2) - local node_east = minetest.get_node(east) - local node_east2 = minetest.get_node(east2) - if minetest.registered_nodes[node_here.name].buildable_to then -- instead of check_air = true, + if check_node_buildable_to(right_here) then -- instead of check_air = true, for i in pairs(TRuNKS) do local MoD = TRuNKS[i][1] local TRuNK = TRuNKS[i][2] @@ -282,63 +256,63 @@ abstract_trunks.place_trunk = function(pos) end elseif trunk_type == 2 and Horizontal_Trunks == true then if minetest.get_modpath(MoD) ~= nil then - if minetest.registered_nodes[node_north.name].buildable_to then + if check_node_buildable_to(north) then minetest.swap_node(north, {name=MoD..":"..TRuNK, param2=4}) end - if length >= 4 and minetest.registered_nodes[node_north2.name].buildable_to then + if length >= 4 and check_node_buildable_to(north2) then minetest.swap_node(north2, {name=MoD..":"..TRuNK, param2=4}) end minetest.swap_node(right_here, {name=MoD..":"..TRuNK, param2=4}) - if minetest.registered_nodes[node_south.name].buildable_to then + if check_node_buildable_to(south) then minetest.swap_node(south, {name=MoD..":"..TRuNK, param2=4}) end - if length == 5 and minetest.registered_nodes[node_south2.name].buildable_to then + if length == 5 and check_node_buildable_to(south2) then minetest.swap_node(south2, {name=MoD..":"..TRuNK, param2=4}) end else - if minetest.registered_nodes[node_north.name].buildable_to then + if check_node_buildable_to(north) then minetest.swap_node(north, {name="default:tree", param2=4}) end - if length >= 4 and minetest.registered_nodes[node_north2.name].buildable_to then + if length >= 4 and check_node_buildable_to(north2) then minetest.swap_node(north2, {name="default:tree", param2=4}) end minetest.swap_node(right_here, {name="default:tree", param2=4}) - if minetest.registered_nodes[node_south.name].buildable_to then + if check_node_buildable_to(south) then minetest.swap_node(south, {name="default:tree", param2=4}) end - if length == 5 and minetest.registered_nodes[node_south2.name].buildable_to then + if length == 5 and check_node_buildable_to(south2) then minetest.swap_node(south2, {name="default:tree", param2=4}) end end elseif trunk_type == 3 and Horizontal_Trunks == true then if minetest.get_modpath(MoD) ~= nil then - if minetest.registered_nodes[node_west.name].buildable_to then + if check_node_buildable_to(west) then minetest.swap_node(west, {name=MoD..":"..TRuNK, param2=12}) end - if length >= 4 and minetest.registered_nodes[node_west2.name].buildable_to then + if length >= 4 and check_node_buildable_to(west2) then minetest.swap_node(west2, {name=MoD..":"..TRuNK, param2=12}) end minetest.swap_node(right_here, {name=MoD..":"..TRuNK, param2=12}) - if minetest.registered_nodes[node_east.name].buildable_to then + if check_node_buildable_to(east) then minetest.swap_node(east, {name=MoD..":"..TRuNK, param2=12}) end - if length == 5 and minetest.registered_nodes[node_east2.name].buildable_to then + if length == 5 and check_node_buildable_to(east2) then minetest.swap_node(east2, {name=MoD..":"..TRuNK, param2=12}) end else - if minetest.registered_nodes[node_west.name].buildable_to then + if check_node_buildable_to(west) then minetest.swap_node(west, {name="default:tree", param2=12}) end - if length >= 4 and minetest.registered_nodes[node_west2.name].buildable_to then + if length >= 4 and check_node_buildable_to(west2) then minetest.swap_node(west2, {name="default:tree", param2=12}) end minetest.swap_node(right_here, {name="default:tree", param2=12}) - if minetest.registered_nodes[node_east.name].buildable_to then + if check_node_buildable_to(east) then minetest.swap_node(east, {name="default:tree", param2=12}) end - if length == 5 and minetest.registered_nodes[node_east2.name].buildable_to then + if length == 5 and check_node_buildable_to(east2) then minetest.swap_node(east2, {name="default:tree", param2=12}) end end @@ -413,17 +387,9 @@ abstract_trunks.grow_moss_on_trunk = function(pos) local at_side_e = {x=pos.x+1, y=pos.y, z=pos.z} local at_side_s = {x=pos.x, y=pos.y, z=pos.z-1} local at_side_w = {x=pos.x-1, y=pos.y, z=pos.z} - local undrneath = {x=pos.x, y=pos.y-1, z=pos.z} - - local node_here = minetest.get_node(on_ground) - local node_north = minetest.get_node(at_side_n) - local node_east = minetest.get_node(at_side_e) - local node_south = minetest.get_node(at_side_s) - local node_west = minetest.get_node(at_side_w) - local node_under = minetest.get_node(undrneath) --if minetest.get_item_group(node_under.name, "tree") < 1 then - if minetest.registered_nodes[node_here.name].buildable_to then + if check_node_buildable_to(on_ground) then local moss_type = math.random(1,41) local rot = math.random(0,3) if moss_type == 1 then @@ -432,7 +398,7 @@ abstract_trunks.grow_moss_on_trunk = function(pos) minetest.swap_node(on_ground, {name="trunks:moss_plain_"..rot, param2=1}) end end - if minetest.registered_nodes[node_north.name].buildable_to then + if check_node_buildable_to(at_side_n) then local moss_type = math.random(1,31) -- cliche of more moss at north local rot = math.random(0,3) if moss_type == 1 then @@ -441,7 +407,7 @@ abstract_trunks.grow_moss_on_trunk = function(pos) minetest.swap_node(at_side_n, {name="trunks:moss_plain_"..rot, param2=5}) end end - if minetest.registered_nodes[node_east.name].buildable_to then + if check_node_buildable_to(at_side_e) then local moss_type = math.random(1,41) local rot = math.random(0,3) if moss_type == 1 then @@ -450,7 +416,7 @@ abstract_trunks.grow_moss_on_trunk = function(pos) minetest.swap_node(at_side_e, {name="trunks:moss_plain_"..rot, param2=3}) end end - if minetest.registered_nodes[node_south.name].buildable_to then + if check_node_buildable_to(at_side_s) then local moss_type = math.random(1,41) local rot = math.random(0,3) if moss_type == 1 then @@ -459,7 +425,7 @@ abstract_trunks.grow_moss_on_trunk = function(pos) minetest.swap_node(at_side_s, {name="trunks:moss_plain_"..rot, param2=4}) end end - if minetest.registered_nodes[node_west.name].buildable_to then + if check_node_buildable_to(at_side_w) then local moss_type = math.random(1,41) local rot = math.random(0,3) if moss_type == 1 then @@ -520,10 +486,6 @@ abstract_trunks.grow_roots = function(pos) local node_here = minetest.get_node(right_here) local node_below = minetest.get_node(below) - local node_north = minetest.get_node(north) - local node_east = minetest.get_node(east) - local node_south = minetest.get_node(south) - local node_west = minetest.get_node(west) for i in pairs(TRuNKS) do local MoD = TRuNKS[i][1] @@ -532,16 +494,16 @@ abstract_trunks.grow_roots = function(pos) and node_here.name == MoD..":"..TRuNK and string.find(node_below.name, "dirt") and node_here.param2 == 0 then - if minetest.registered_nodes[node_north.name].buildable_to then + if check_node_buildable_to(north) then minetest.swap_node(north, {name="trunks:"..TRuNK.."root", param2=2}) end - if minetest.registered_nodes[node_east.name].buildable_to then + if check_node_buildable_to(east) then minetest.swap_node(east, {name="trunks:"..TRuNK.."root", param2=3}) end - if minetest.registered_nodes[node_south.name].buildable_to then + if check_node_buildable_to(south) then minetest.swap_node(south, {name="trunks:"..TRuNK.."root", param2=0}) end - if minetest.registered_nodes[node_west.name].buildable_to then + if check_node_buildable_to(west) then minetest.swap_node(west, {name="trunks:"..TRuNK.."root", param2=1}) end end