Convert spaces to tabs and add unknown node checks to xpanes

This commit is contained in:
ShadowNinja 2014-07-06 21:49:47 -04:00
parent 5e0c49345a
commit ca7f6bb97a

View File

@ -1,175 +1,186 @@
xpanes = {} xpanes = {}
local function rshift(x, by) local function rshift(x, by)
return math.floor(x / 2 ^ by) return math.floor(x / 2 ^ by)
end end
local directions = { local directions = {
{x = 1, y = 0, z = 0}, {x = 1, y = 0, z = 0},
{x = 0, y = 0, z = 1}, {x = 0, y = 0, z = 1},
{x = -1, y = 0, z = 0}, {x = -1, y = 0, z = 0},
{x = 0, y = 0, z = -1}, {x = 0, y = 0, z = -1},
} }
local function update_pane(pos,name) local function update_pane(pos, name)
if minetest.get_node(pos).name:find("xpanes:"..name) == nil then if not minetest.get_node(pos).name:find("^xpanes:"..name) then
return return
end end
local sum = 0 local sum = 0
for i = 1, 4 do for i, dir in pairs(directions) do
local node = minetest.get_node({x = pos.x + directions[i].x, y = pos.y + directions[i].y, z = pos.z + directions[i].z}) local node = minetest.get_node({
local pane_num = minetest.registered_nodes[node.name].groups.pane or 0 x = pos.x + dir.x,
if (minetest.registered_nodes[node.name].walkable ~= false and minetest.registered_nodes[node.name].drawtype ~= "nodebox") or pane_num > 0 then y = pos.y + dir.y,
sum = sum + 2 ^ (i - 1) z = pos.z + dir.z
end })
end local def = minetest.registered_nodes[node.name]
if sum == 0 then local pane_num = def and def.groups.pane or 0
sum = 15 if pane_num > 0 or not def or (def.walkable ~= false and
end def.drawtype ~= "nodebox") then
minetest.set_node(pos, {name = "xpanes:"..name.."_"..sum}) sum = sum + 2 ^ (i - 1)
end
end
if sum == 0 then
sum = 15
end
minetest.set_node(pos, {name = "xpanes:"..name.."_"..sum})
end end
local function update_nearby(pos,n) local function update_nearby(pos, node)
if n == nil then n = minetest.get_node(pos) end node = node or minetest.get_node(pos)
if not n or not n.name then return end if node.name:sub(1, 7) ~= "xpanes:" then return end
local name = string.sub(n.name,8,10) local underscore_pos = node.name:find("_") or 0
if name ~= "bar" then name = "pane" end local name = node.name:sub(8, underscore_pos - 1)
for i = 1,4 do for i, dir in pairs(directions) do
update_pane({x = pos.x + directions[i].x, y = pos.y + directions[i].y, z = pos.z + directions[i].z}, name) update_pane({
end x = pos.x + dir.x,
y = pos.y + dir.y,
z = pos.z + dir.z
}, name)
end
end end
local half_blocks = { local half_boxes = {
{0, -0.5, -1/32, 0.5, 0.5, 1/32}, {0, -0.5, -1/32, 0.5, 0.5, 1/32},
{-1/32, -0.5, 0, 1/32, 0.5, 0.5}, {-1/32, -0.5, 0, 1/32, 0.5, 0.5},
{-0.5, -0.5, -1/32, 0, 0.5, 1/32}, {-0.5, -0.5, -1/32, 0, 0.5, 1/32},
{-1/32, -0.5, -0.5, 1/32, 0.5, 0} {-1/32, -0.5, -0.5, 1/32, 0.5, 0}
} }
local full_blocks = { local full_boxes = {
{-0.5, -0.5, -1/32, 0.5, 0.5, 1/32}, {-0.5, -0.5, -1/32, 0.5, 0.5, 1/32},
{-1/32, -0.5, -0.5, 1/32, 0.5, 0.5} {-1/32, -0.5, -0.5, 1/32, 0.5, 0.5}
} }
local sb_half_blocks = { local sb_half_boxes = {
{0, -0.5, -0.06, 0.5, 0.5, 0.06}, {0, -0.5, -0.06, 0.5, 0.5, 0.06},
{-0.06, -0.5, 0, 0.06, 0.5, 0.5}, {-0.06, -0.5, 0, 0.06, 0.5, 0.5},
{-0.5, -0.5, -0.06, 0, 0.5, 0.06}, {-0.5, -0.5, -0.06, 0, 0.5, 0.06},
{-0.06, -0.5, -0.5, 0.06, 0.5, 0} {-0.06, -0.5, -0.5, 0.06, 0.5, 0}
} }
local sb_full_blocks = { local sb_full_boxes = {
{-0.5, -0.5, -0.06, 0.5, 0.5, 0.06}, {-0.5, -0.5, -0.06, 0.5, 0.5, 0.06},
{-0.06, -0.5, -0.5, 0.06, 0.5, 0.5} {-0.06, -0.5, -0.5, 0.06, 0.5, 0.5}
} }
--register panes and bars
function xpanes.register_pane(name, def) function xpanes.register_pane(name, def)
for i = 1, 15 do for i = 1, 15 do
local need = {} local need = {}
local cnt = 0 local cnt = 0
for j = 1, 4 do for j = 1, 4 do
if rshift(i, j - 1) % 2 == 1 then if rshift(i, j - 1) % 2 == 1 then
need[j] = true need[j] = true
cnt = cnt + 1 cnt = cnt + 1
end end
end end
local take = {} local take = {}
local take2 = {} local take2 = {}
if need[1] == true and need[3] == true then if need[1] == true and need[3] == true then
need[1] = nil need[1] = nil
need[3] = nil need[3] = nil
table.insert(take, full_blocks[1]) table.insert(take, full_boxes[1])
table.insert(take2, sb_full_blocks[1]) table.insert(take2, sb_full_boxes[1])
end end
if need[2] == true and need[4] == true then if need[2] == true and need[4] == true then
need[2] = nil need[2] = nil
need[4] = nil need[4] = nil
table.insert(take, full_blocks[2]) table.insert(take, full_boxes[2])
table.insert(take2, sb_full_blocks[2]) table.insert(take2, sb_full_boxes[2])
end end
for k in pairs(need) do for k in pairs(need) do
table.insert(take, half_blocks[k]) table.insert(take, half_boxes[k])
table.insert(take2, sb_half_blocks[k]) table.insert(take2, sb_half_boxes[k])
end end
local texture = def.textures[1] local texture = def.textures[1]
if cnt == 1 then if cnt == 1 then
texture = def.textures[1].."^"..def.textures[2] texture = def.textures[1].."^"..def.textures[2]
end end
minetest.register_node("xpanes:"..name.."_"..i, { minetest.register_node("xpanes:"..name.."_"..i, {
drawtype = "nodebox", drawtype = "nodebox",
tiles = {def.textures[3], def.textures[3], texture}, tiles = {def.textures[3], def.textures[3], texture},
paramtype = "light", paramtype = "light",
groups = def.groups, groups = def.groups,
drop = "xpanes:"..name, drop = "xpanes:"..name,
sounds = def.sounds, sounds = def.sounds,
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = take fixed = take
}, },
selection_box = { selection_box = {
type = "fixed", type = "fixed",
fixed = take2 fixed = take2
} }
}) })
end end
minetest.register_node("xpanes:"..name, def) minetest.register_node("xpanes:"..name, def)
minetest.register_craft({ minetest.register_craft({
output = "xpanes:"..name.." 16", output = "xpanes:"..name.." 16",
recipe = def.recipe recipe = def.recipe
}) })
end end
minetest.register_on_placenode(update_nearby) minetest.register_on_placenode(update_nearby)
minetest.register_on_dignode(update_nearby) minetest.register_on_dignode(update_nearby)
xpanes.register_pane("pane", { xpanes.register_pane("pane", {
description = "Glass Pane", description = "Glass Pane",
tiles = {"xpanes_space.png"}, tiles = {"xpanes_space.png"},
drawtype = "airlike", drawtype = "airlike",
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
walkable = false, walkable = false,
pointable = false, pointable = false,
diggable = false, diggable = false,
buildable_to = true, buildable_to = true,
air_equivalent = true, air_equivalent = true,
textures = {"default_glass.png","xpanes_pane_half.png","xpanes_white.png"}, textures = {"default_glass.png","xpanes_pane_half.png","xpanes_white.png"},
inventory_image = "default_glass.png", inventory_image = "default_glass.png",
wield_image = "default_glass.png", wield_image = "default_glass.png",
sounds = default.node_sound_glass_defaults(), sounds = default.node_sound_glass_defaults(),
groups = {snappy=2,cracky=3,oddly_breakable_by_hand=3,pane=1}, groups = {snappy=2, cracky=3, oddly_breakable_by_hand=3, pane=1},
on_construct = function(pos) on_construct = function(pos)
update_pane(pos, "pane") update_pane(pos, "pane")
end, end,
recipe = { recipe = {
{'default:glass', 'default:glass', 'default:glass'}, {'default:glass', 'default:glass', 'default:glass'},
{'default:glass', 'default:glass', 'default:glass'} {'default:glass', 'default:glass', 'default:glass'}
} }
}) })
xpanes.register_pane("bar", { xpanes.register_pane("bar", {
description = "Iron bar", description = "Iron bar",
tiles = {"xpanes_space.png"}, tiles = {"xpanes_space.png"},
drawtype = "airlike", drawtype = "airlike",
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
walkable = false, walkable = false,
pointable = false, pointable = false,
diggable = false, diggable = false,
buildable_to = true, buildable_to = true,
air_equivalent = true, air_equivalent = true,
textures = {"xpanes_bar.png","xpanes_bar.png","xpanes_space.png"}, textures = {"xpanes_bar.png","xpanes_bar.png","xpanes_space.png"},
inventory_image = "xpanes_bar.png", inventory_image = "xpanes_bar.png",
wield_image = "xpanes_bar.png", wield_image = "xpanes_bar.png",
groups = {snappy=2,cracky=3,oddly_breakable_by_hand=3,pane=1}, groups = {snappy=2, cracky=3, oddly_breakable_by_hand=3, pane=1},
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
on_construct = function(pos) on_construct = function(pos)
update_pane(pos, "bar") update_pane(pos, "bar")
end, end,
recipe = { recipe = {
{'default:steel_ingot', 'default:glass', 'default:glass'}, {'default:steel_ingot', 'default:glass', 'default:glass'},
{'default:glass', 'default:glass', 'default:glass'} {'default:glass', 'default:glass', 'default:glass'}
} }
}) })