From c0b40b5cbf5ae88a09214c259d1c672a592e5cca Mon Sep 17 00:00:00 2001 From: HybridDog Date: Sat, 23 May 2015 19:53:30 +0200 Subject: [PATCH] some code updates --- init.lua | 111 +++++++++++++++++++++++++--------------------- src/crafting.lua | 4 +- src/mapgen.lua | 108 ++++++++++++++++++++++---------------------- src/mapgen_v6.lua | 3 +- src/nodes.lua | 12 ++--- src/snowball.lua | 73 +++++++++++++++--------------- src/util.lua | 47 ++++++++++++-------- 7 files changed, 193 insertions(+), 165 deletions(-) diff --git a/init.lua b/init.lua index ffc513a..3285a64 100644 --- a/init.lua +++ b/init.lua @@ -71,7 +71,7 @@ dofile(minetest.get_modpath("snow").."/src/falling_snow.lua") -- Check for "MoreBlocks". If not found, skip this next "dofile". -if (minetest.get_modpath("moreblocks")) then +if minetest.get_modpath("moreblocks") then dofile(minetest.get_modpath("snow").."/src/stairsplus.lua") @@ -81,31 +81,37 @@ end --This also takes into account sourrounding snow and makes snow even. function snow.place(pos) local node = minetest.get_node_or_nil(pos) - local drawtype = "" - if node and minetest.registered_nodes[node.name] then - drawtype = minetest.registered_nodes[node.name].drawtype - end --Oops, maybe there is no node? - if node == nil then + if not node + or not minetest.registered_nodes[node.name] then return end - local bnode = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}) - if node.name == "default:snow" and minetest.get_node_level(pos) < 63 then - if minetest.get_item_group(bnode.name, "leafdecay") == 0 and snow.is_uneven(pos) ~= true then - minetest.add_node_level(pos, 7) + local bnode = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}) + if node.name == "default:snow" then + local level = minetest.get_node_level(pos) + if level < 63 then + if minetest.get_item_group(bnode.name, "leafdecay") == 0 + and not snow.is_uneven(pos) then + minetest.add_node_level(pos, 7) + end + elseif level == 63 then + local p = minetest.find_node_near(pos, 10, "default:dirt_with_grass") + if p + and minetest.get_node_light(p, 0.5) == 15 then + minetest.place_node({x=pos.x, y=pos.y+1, z=pos.z}, {name="default:snow"}) + else + minetest.add_node(pos,{name="default:snowblock"}) + end end - elseif node.name == "default:snow" and minetest.get_node_level(pos) == 63 then - local p = minetest.find_node_near(pos, 10, "default:dirt_with_grass") - if p and minetest.get_node_light(p, 0.5) == 15 then - minetest.place_node({x=pos.x,y=pos.y+1,z=pos.z},{name="default:snow"}) - else - minetest.add_node(pos,{name="default:snowblock"}) - end - elseif node.name ~= "default:ice" and bnode.name ~= "air" then - if drawtype == "normal" or drawtype == "allfaces_optional" then - minetest.place_node({x=pos.x,y=pos.y+1,z=pos.z}, {name="default:snow"}) + elseif node.name ~= "default:ice" + and bnode.name ~= "air" then + local drawtype = minetest.registered_nodes[node.name].drawtype + if drawtype == "normal" + or drawtype == "allfaces_optional" then + pos.y = pos.y+1 + minetest.place_node(pos, {name="default:snow"}) elseif drawtype == "plantlike" then pos.y = pos.y - 1 if minetest.get_node(pos).name == "default:dirt_with_grass" then @@ -117,9 +123,8 @@ end -- Checks if the snow level is even at any given pos. -- Smooth Snow -local smooth_snow = snow.smooth_snow -snow.is_uneven = function(pos) - if smooth_snow then +if snow.smooth_snow then + snow.is_uneven = function(pos) local num = minetest.get_node_level(pos) local get_node = minetest.get_node local add_node = minetest.add_node @@ -127,40 +132,46 @@ snow.is_uneven = function(pos) local foundx local foundy local foundz - for x=-1,1 do - for z=-1,1 do - local node = get_node({x=pos.x+x,y=pos.y,z=pos.z+z}) - local bnode = get_node({x=pos.x+x,y=pos.y-1,z=pos.z+z}) - local drawtype - if node and minetest.registered_nodes[node.name] then - drawtype = minetest.registered_nodes[node.name].drawtype - end - - if drawtype == "plantlike" then - if bnode.name == "default:dirt_with_grass" then - add_node({x=pos.x+x,y=pos.y-1,z=pos.z+z}, {name="default:dirt_with_snow"}) + for z = -1,1 do + for x = -1,1 do + local p = {x=pos.x+x, y=pos.y, z=pos.z+z} + local node = get_node(p) + p.y = p.y-1 + local bnode = get_node(p) + + if node + and minetest.registered_nodes[node.name] + and minetest.registered_nodes[node.name].drawtype == "plantlike" + and bnode.name == "default:dirt_with_grass" then + add_node(p, {name="default:dirt_with_snow"}) + return true + end + + p.y = p.y+1 + if not (x == 0 and z == 0) + and node.name == "default:snow" + and minetest.get_node_level(p) < num then + found = true + foundx = x + foundz = z + elseif node.name == "air" + and bnode.name ~= "air" + and bnode.name ~= "default:snow" then + p.y = p.y-1 + snow.place(p) return true end end - - if (not(x == 0 and y == 0)) and node.name == "default:snow" and minetest.get_node_level({x=pos.x+x,y=pos.y,z=pos.z+z}) < num then - found = true - foundx = x - foundz=z - elseif node.name == "air" and bnode.name ~= "air" then - if not (bnode.name == "default:snow") then - snow.place({x=pos.x+x,y=pos.y-1,z=pos.z+z}) - return true - end - end - end end if found then - local node = get_node({x=pos.x+foundx,y=pos.y,z=pos.z+foundz}) - if snow.is_uneven({x=pos.x+foundx,y=pos.y,z=pos.z+foundz}) ~= true then - minetest.add_node_level({x=pos.x+foundx,y=pos.y,z=pos.z+foundz}, 7) + local p = {x=pos.x+foundx, y=pos.y, z=pos.z+foundz} + if snow.is_uneven(p) ~= true then + minetest.add_node_level(p, 7) end return true end end +else + snow.is_uneven = function() + end end diff --git a/src/crafting.lua b/src/crafting.lua index 657ce66..576b2ac 100644 --- a/src/crafting.lua +++ b/src/crafting.lua @@ -203,7 +203,7 @@ for _, name in pairs(recycle_default_slabs) do recipe = { "snow:slab_"..subname_default, "snow:slab_"..subname_default, - } + } }) end @@ -229,6 +229,6 @@ for _, name in pairs(recycle_snowmod_slabs) do recipe = { "snow:slab_"..subname_snowmod, "snow:slab_"..subname_snowmod, - } + } }) end diff --git a/src/mapgen.lua b/src/mapgen.lua index b8a1129..08d196c 100644 --- a/src/mapgen.lua +++ b/src/mapgen.lua @@ -72,20 +72,21 @@ function snow.make_pine(pos,snow,xmas) local perlin1 = minetest.get_perlin(112,3, 0.5, 150) local try_node = function(pos, node) local n = minetest.get_node(pos).name - if n == "air" or n == "ignore" then - minetest.add_node(pos,node) + if n == "air" + or n == "ignore" then + minetest.add_node(pos, node) end end --Clear ground. - for x=-1,1 do - for z=-1,1 do - if minetest.get_node({x=pos.x+x,y=pos.y,z=pos.z+z}).name == "default:snow" then - minetest.remove_node({x=pos.x+x,y=pos.y,z=pos.z+z}) + for z = -1,1 do + for x = -1,1 do + local p = {x=pos.x+x,y=pos.y,z=pos.z+z} + local nd = minetest.get_node(p).name + if nd == "default:snow" + or nd == "default:snowblock" then + minetest.remove_node(p) + end end - if minetest.get_node({x=pos.x+x,y=pos.y,z=pos.z+z}).name == "default:snowblock" then - minetest.remove_node({x=pos.x+x,y=pos.y,z=pos.z+z}) - end - end end if xmas then minetest.remove_node(pos) @@ -107,7 +108,8 @@ function snow.make_pine(pos,snow,xmas) end if xmas then try_node({x=pos.x,y=pos.y+7,z=pos.z},{name="snow:star_lit"}) -- Added lit star. ~ LazyJ - elseif snow and perlin1:get2d({x=pos.x,y=pos.z}) > 0.53 then + elseif snow + and perlin1:get2d({x=pos.x,y=pos.z}) > 0.53 then try_node({x=pos.x,y=pos.y+7,z=pos.z},{name="default:snow"}) end end @@ -122,59 +124,61 @@ function snow.voxelmanip_pine(pos,a,data) local c_air = minetest.get_content_id("air") local perlin1 = minetest.get_perlin(112,3, 0.5, 150) - --Clear ground. - for x=-1,1 do - for z=-1,1 do - local node = a:index(pos.x+x,pos.y,pos.z+z) - if data[node] == c_snow then - data[node] = c_air - end - end - end - --Make tree. - for i=0, 4 do - if i==1 or i==2 then - for x=-1,1 do - for z=-1,1 do - local x = pos.x + x - local z = pos.z + z + for z = -1,1 do + local z = pos.z + z + for x = -1,1 do + local x = pos.x + x + + --Clear ground. + local node = a:index(x,pos.y,z) + if data[node] == c_snow then + data[node] = c_air + end + + --Make tree. + for i = 1,2 do local node = a:index(x,pos.y+i,z) data[node] = c_pine_needles - if snow and x ~= 0 and z ~= 0 and perlin1:get2d({x=x,y=z}) > 0.53 then + if snow + and x ~= 0 + and z ~= 0 + and perlin1:get2d({x=x,y=z}) > 0.53 then local abovenode = a:index(x,pos.y+i+1,z) data[abovenode] = c_snow end end + end + end + for i=3, 4 do + local x = pos.x + local y = pos.y+i + local z = pos.z + data[a:index(x+1,y,z)] = c_pine_needles + data[a:index(x-1,y,z)] = c_pine_needles + data[a:index(x,y,z+1)] = c_pine_needles + data[a:index(x,y,z-1)] = c_pine_needles + if snow then + if perlin1:get2d({x=x+1,y=z}) > 0.53 then + data[a:index(x+1,y+1,z)] = c_snow + end + if perlin1:get2d({x=x+1,y=z}) > 0.53 then + data[a:index(x-1,y+1,z)] = c_snow + end + if perlin1:get2d({x=x,y=z+1}) > 0.53 then + data[a:index(x,y+1,z+1)] = c_snow + end + if perlin1:get2d({x=x,y=z-1}) > 0.53 then + data[a:index(x,y+1,z-1)] = c_snow end end - if i==3 or i==4 then - local x = pos.x - local y = pos.y+i - local z = pos.z - data[a:index(x+1,y,z)] = c_pine_needles - data[a:index(x-1,y,z)] = c_pine_needles - data[a:index(x,y,z+1)] = c_pine_needles - data[a:index(x,y,z-1)] = c_pine_needles - if snow then - if perlin1:get2d({x=x+1,y=z}) > 0.53 then - data[a:index(x+1,y+1,z)] = c_snow - end - if perlin1:get2d({x=x+1,y=z}) > 0.53 then - data[a:index(x-1,y+1,z)] = c_snow - end - if perlin1:get2d({x=x,y=z+1}) > 0.53 then - data[a:index(x,y+1,z+1)] = c_snow - end - if perlin1:get2d({x=x,y=z-1}) > 0.53 then - data[a:index(x,y+1,z-1)] = c_snow - end - end - end + end + for i=0, 4 do data[a:index(pos.x,pos.y+i,pos.z)] = c_pinetree end data[a:index(pos.x,pos.y+5,pos.z)] = c_pine_needles data[a:index(pos.x,pos.y+6,pos.z)] = c_pine_needles - if snow and perlin1:get2d({x=pos.x,y=pos.z}) > 0.53 then + if snow + and perlin1:get2d({x=pos.x,y=pos.z}) > 0.53 then data[a:index(pos.x,pos.y+7,pos.z)] = c_snow end end diff --git a/src/mapgen_v6.lua b/src/mapgen_v6.lua index 7f0c51c..ea31a27 100644 --- a/src/mapgen_v6.lua +++ b/src/mapgen_v6.lua @@ -139,7 +139,8 @@ minetest.register_on_generated(function(minp, maxp, seed) local ground_y = nil for y = maxp.y, minp.y, -1 do local nodid = data[area:index(x, y, z)] - if nodid ~= c_air and nodid ~= c_ignore then + if nodid ~= c_air + and nodid ~= c_ignore then ground_y = y break end diff --git a/src/nodes.lua b/src/nodes.lua index 03f4f6c..8eb1dcd 100644 --- a/src/nodes.lua +++ b/src/nodes.lua @@ -197,8 +197,9 @@ minetest.register_node("snow:star", { --groups = {snappy=2,dig_immediate=3}, groups = {cracky=1, crumbly=1, choppy=1, oddly_breakable_by_hand=1}, -- Don't want the ornament breaking too easily because you have to punch it to turn it on and off. ~ LazyJ sounds = default.node_sound_glass_defaults({dig = {name="default_glass_footstep", gain=0.2}}), -- Breaking "glass" sound makes it sound like a real, broken, Xmas tree ornament (Sorry, Mom!). ;)- ~ LazyJ - on_punch = function(pos, node, puncher) -- Added a "lit" star that can be punched on or off depending on your preference. ~ LazyJ - minetest.set_node(pos, {name = "snow:star_lit"}) + on_punch = function(pos, node) -- Added a "lit" star that can be punched on or off depending on your preference. ~ LazyJ + node.name = "snow:star_lit" + minetest.set_node(pos, node) nodeupdate(pos) end, }) @@ -217,8 +218,9 @@ minetest.register_node("snow:star_lit", { drop = "snow:star", groups = {cracky=1, crumbly=1, choppy=1, oddly_breakable_by_hand=1, not_in_creative_inventory=1}, sounds = default.node_sound_glass_defaults({dig = {name="default_glass_footstep", gain=0.2}}), - on_punch = function(pos, node, puncher) - minetest.set_node(pos, {name = "snow:star"}) + on_punch = function(pos, node) + node.name = "snow:star" + minetest.set_node(pos, node) nodeupdate(pos) end, }) @@ -328,7 +330,7 @@ minetest.override_item("default:ice", { on_construct = snow_onto_dirt, liquids_pointable = true, --Make ice freeze over when placed by a maximum of 10 blocks. - after_place_node = function(pos, placer, itemstack, pointed_thing) + after_place_node = function(pos) minetest.set_node(pos, {name="default:ice", param2=math.random(0,10)}) end }) diff --git a/src/snowball.lua b/src/snowball.lua index 89a78e8..f599a1c 100644 --- a/src/snowball.lua +++ b/src/snowball.lua @@ -31,17 +31,20 @@ snow_snowball_ENTITY={ --Snowball_entity.on_step()--> called when snowball is moving. snow_snowball_ENTITY.on_step = function(self, dtime) - self.timer=self.timer+dtime + self.timer = self.timer+dtime + if self.timer > 600 then + -- 10 minutes are too long for a snowball to fly somewhere + self.object:remove() + end local pos = self.object:getpos() local node = minetest.get_node(pos) --Become item when hitting a node. - if self.lastpos.x~=nil then --If there is no lastpos for some reason. ~ Splizard + if self.lastpos.x then --If there is no lastpos for some reason. ~ Splizard -- Check to see what is one node above where the snow is -- going to be placed. ~ LazyJ, 2014_04_08 - local abovesnowballtarget = {x=pos.x, y=pos.y+1, z=pos.z} -- Identify the name of the node that was found above. ~ LazyJ, 2014_04_08 - local findwhatisabove = minetest.get_node(abovesnowballtarget).name + local findwhatisabove = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name -- If the node above is air, then it's OK to go on to the next step. ~ LazyJ, 2014_04_08 if findwhatisabove == "air" then -- If the node where the snow is going is anything except air, then it's OK to put @@ -54,12 +57,12 @@ snow_snowball_ENTITY.on_step = function(self, dtime) --minetest.place_node({x=pos.x, y=pos.y+2, z=pos.z}, {name="default:snow"}) self.object:remove() end - else -- If findwhatisabove is not equal to "air" then cancel the snowball + else -- If findwhatisabove is not equal to "air" then cancel the snowball -- with self.object:remove() ~ LazyJ, 2014_04_08 - self.object:remove() + self.object:remove() end end - self.lastpos={x=pos.x, y=pos.y, z=pos.z} + self.lastpos = vector.new(pos) end @@ -142,9 +145,12 @@ minetest.override_item("default:snow", { --Disable placement prediction for snow. node_placement_prediction = "", on_construct = function(pos) - if minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name == "default:dirt_with_grass" - or minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name == "default:dirt" then - minetest.set_node({x=pos.x, y=pos.y-1, z=pos.z}, {name="default:dirt_with_snow"}) + pos.y = pos.y-1 + local node = minetest.get_node(pos) + if node.name == "default:dirt_with_grass" + or node.name == "default:dirt" then + node.name = "default:dirt_with_snow" + minetest.set_node(pos, node) end end, --Remove dirt_with_snow and replace with dirt_with_grass. @@ -158,57 +164,52 @@ minetest.override_item("default:snow", { local level = minetest.get_node_level(pos) minetest.node_dig(pos, node, digger) if minetest.get_node(pos).name ~= node.name then - if digger:get_inventory() then - local _, dropped_item - local left = digger:get_inventory():add_item("main", "default:snow "..tostring(level/7-1)) - if not left:is_empty() then - local p = { - x = pos.x + math.random()/2-0.25, - y = pos.y + math.random()/2-0.25, - z = pos.z + math.random()/2-0.25, - } - minetest.add_item(p, left) - end + local inv = digger:get_inventory() + if not inv then + return + end + local left = inv:add_item("main", "default:snow "..tostring(level/7-1)) + if not left:is_empty() then + local p = { + x = pos.x + math.random()/2-0.25, + y = pos.y + math.random()/2-0.25, + z = pos.z + math.random()/2-0.25, + } + minetest.add_item(p, left) end end end, --Manage snow levels. on_place = function(itemstack, placer, pointed_thing) - local node - local above - local level - local under = pointed_thing.under local oldnode_under = minetest.get_node_or_nil(under) local above = pointed_thing.above - local oldnode_above = minetest.get_node_or_nil(above) - if not oldnode_under or not oldnode_above then + if not oldnode_under + or not above then return end local olddef_under = ItemStack({name=oldnode_under.name}):get_definition() olddef_under = olddef_under or minetest.nodedef_default - local olddef_above = ItemStack({name=oldnode_above.name}):get_definition() - olddef_above = olddef_above or minetest.nodedef_default - -- Place above pointed node - local place_to = {x = above.x, y = above.y, z = above.z} - + local place_to -- If node under is buildable_to, place into it instead (eg. snow) if olddef_under.buildable_to then - place_to = {x = under.x, y = under.y, z = under.z} + place_to = under + else + -- Place above pointed node + place_to = above end - node = minetest.get_node(place_to) - level = minetest.get_node_level(place_to) + local level = minetest.get_node_level(place_to) if level == 63 then minetest.set_node(place_to, {name="default:snowblock"}) else minetest.set_node_level(place_to, level+7) end - if node.name ~= "default:snow" then + if minetest.get_node(place_to).name ~= "default:snow" then local itemstack, placed = minetest.item_place_node(itemstack, placer, pointed_thing) return itemstack, placed end diff --git a/src/util.lua b/src/util.lua index 1ddf4f9..d6ca94a 100644 --- a/src/util.lua +++ b/src/util.lua @@ -79,7 +79,9 @@ end for i,v in pairs(snow) do local t = type(v) - if t == "string" or t == "number" or t == "boolean" then + if t == "string" + or t == "number" + or t == "boolean" then local v = minetest.setting_get("snow_"..i) if v ~= nil then if v == "true" then v = true end @@ -99,7 +101,8 @@ if minetest.register_on_mapgen_init then else snow.legacy = true end -if config and snow.legacy ~= config.legacy then +if config +and snow.legacy ~= config.legacy then saveConfig(minetest.get_modpath("snow").."/config.txt", snow, doc) end @@ -110,7 +113,8 @@ local get_formspec = function() local formspec = "label[0,-0.3;Settings:]" for i,v in pairs(snow) do local t = type(v) - if t == "string" or t == "number" then + if t == "string" + or t == "number" then p = p + 1.5 formspec = formspec.."field[0.3,"..p..";2,1;snow:"..i..";"..i..";"..v.."]" elseif t == "boolean" then @@ -124,22 +128,27 @@ local get_formspec = function() end minetest.register_on_player_receive_fields(function(player, formname, fields) - if formname == "snow:menu" then - for i,v in pairs(snow) do - local t = type(v) - if t == "string" or t == "number" or t == "boolean" then - if fields["snow:"..i] then - if t == "string" then - snow[i] = fields["snow:"..i] + if formname ~= "snow:menu" then + return + end + for i,v in pairs(snow) do + local t = type(v) + if t == "string" or t == "number" or t == "boolean" then + local field = fields["snow:"..i] + if field then + if t == "string" then + snow[i] = field + end + if t == "number" then + snow[i] = tonumber(field) + end + if t == "boolean" then + if field == "true" then + snow[i] = true + elseif field == "false" then + snow[i] = false end - if t == "number" then - snow[i] = tonumber(fields["snow:"..i]) - end - if t == "boolean" then - if fields["snow:"..i] == "true" then snow[i] = true end - if fields["snow:"..i] == "false" then snow[i] = false end - end - end + end end end end @@ -149,7 +158,7 @@ end) minetest.register_chatcommand("snow", { description = "Show a menu for various actions", privs = {server=true}, - func = function(name, param) + func = function(name) minetest.show_formspec(name, "snow:menu", get_formspec()) end, })