--[[
wall center = wc
wall edge = we
center = ce

Horizontal/Vertical
]]

timbers = {}

timbers.dye_more = function(pos, node, puncher, pointed_thing)
   local player = puncher:get_player_name()
   local wield = puncher:get_wielded_item()
   local wield_name = wield:get_name()
   if minetest.is_protected(pos, player) and not minetest.check_player_privs(puncher, 'protection_bypass') or wield_name == 'creative:tool_breaking' then
      return
   end
      minetest.swap_node(pos, {name = node.name, param2 = node.param2+32})
end

timbers.dye_less = function(pos, node, clicker)
   local player = clicker:get_player_name()
   if minetest.is_protected(pos, player) and not minetest.check_player_privs(clicker, 'protection_bypass') then
      return
   end
      minetest.swap_node(pos, {name = node.name, param2 = node.param2-32})
end

minetest.register_node('timbers:placeholder', {
   description = 'empty node :P',
   drawtype = 'airlike',
   paramtype = 'light',
   walkable = false,
   pointable = false,
   groups = {not_in_creative_inventory = 1, empty_node=1},
})

local function placement(pos, placer, itemstack, pointed_thing)
   local nodeu = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z})
   local nodeu_name = nodeu.name
   local replacement = string.sub(nodeu_name, 1, 13)..'2'
   if nodeu_name == itemstack:get_name() then
      minetest.set_node({x=pos.x, y=pos.y-1, z=pos.z},{name = replacement, param2=nodeu.param2})
      minetest.set_node(pos,{name = 'timbers:placeholder'})
   end
end

local function remove_empty(pos, node, digger)
   minetest.set_node({x=pos.x, y=pos.y+1, z=pos.z},{name = 'air'})
end

--Single height/width
local v_ce_1 = {
   type = 'fixed',
   fixed = {-.125, -.5, -.125, .125, .5, .125}}

minetest.register_node('timbers:v_ce_1', {
   description = 'Single Timber (Centered)',
   drawtype = 'mesh',
   mesh = 'timbers_v_ce_1.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = v_ce_1,
   collision_box = v_ce_1,
   groups = {oddly_breakable_by_hand=3, breakable=1},
   after_place_node = placement,
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

local v_wc_1 = {
   type = 'fixed',
   fixed = {-.125, -.5, .5, .125, .5, .25}}

minetest.register_node('timbers:v_wc_1', {
   description = 'Single Timber (Centered on Wall)',
   drawtype = 'mesh',
   mesh = 'timbers_v_wc_1.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = v_wc_1,
   collision_box = v_wc_1,
   groups = {oddly_breakable_by_hand=3, breakable=1},
   after_place_node = placement,
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

local v_we_1 = {
   type = 'fixed',
   fixed = {.25, -.5, .25, .5, .5, .5}}

minetest.register_node('timbers:v_we_1', {
   description = 'Single Timber (Inside Corner)',
   drawtype = 'mesh',
   mesh = 'timbers_v_we_1.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = v_we_1,
   collision_box = v_we_1,
   groups = {oddly_breakable_by_hand=3, breakable=1},
   after_place_node = placement,
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

local h_ce_1 = {
   type = 'fixed',
   fixed = {-.5, -.125, -.125, .5, .125, .125}}

minetest.register_node('timbers:h_ce_1', {
   description = 'Single Timber (Centered)',
   drawtype = 'mesh',
   mesh = 'timbers_h_ce_1.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = h_ce_1,
   collision_box = h_ce_1,
   groups = {oddly_breakable_by_hand=3, breakable=1},
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

local h_wc_1 = {
   type = 'fixed',
   fixed = {-.5, -.5, -.125, .5, -.25, .125}}

minetest.register_node('timbers:h_wc_1', {
   description = 'Single Timber (Centered on Wall)',
   drawtype = 'mesh',
   mesh = 'timbers_h_wc_1.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = h_wc_1,
   collision_box = h_wc_1,
   groups = {oddly_breakable_by_hand=3, breakable=1},
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

local h_we_1 = {
   type = 'fixed',
   fixed = {-.5, -.5, .25, .5, -.25, .5}}

minetest.register_node('timbers:h_we_1', {
   description = 'Single Timber (Inside Corner)',
   drawtype = 'mesh',
   mesh = 'timbers_h_we_1.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = h_we_1,
   collision_box = h_we_1,
   groups = {oddly_breakable_by_hand=3, breakable=1},
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

--Double height/width
local v_ce_2 = {
   type = 'fixed',
   fixed = {-.125, -.5, -.125, .125, 1.5, .125}}

minetest.register_node('timbers:v_ce_2', {
   description = 'Double Timber (Centered)',
   drawtype = 'mesh',
   mesh = 'timbers_v_ce_2.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = v_ce_2,
   collision_box = v_ce_2,
   groups = {oddly_breakable_by_hand=3, not_in_creative_inventory=1, breakable=1},
   drop = 'timbers:v_ce_1 2',
   after_dig_node = remove_empty,
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

local v_wc_2 = {
   type = 'fixed',
   fixed = {-.125, -.5, .5, .125, 1.5, .25}}

minetest.register_node('timbers:v_wc_2', {
   description = 'Double Timber (Centered on Wall)',
   drawtype = 'mesh',
   mesh = 'timbers_v_wc_2.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = v_wc_2,
   collision_box = v_wc_2,
   groups = {oddly_breakable_by_hand=3, not_in_creative_inventory=1, breakable=1},
   drop = 'timbers:v_wc_1 2',
   after_dig_node = remove_empty,
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

local v_we_2 = {
   type = 'fixed',
   fixed = {.25, -.5, .25, .5, 1.5, .5}}

minetest.register_node('timbers:v_we_2', {
   description = 'Double Timber (Inside Corner)',
   drawtype = 'mesh',
   mesh = 'timbers_v_we_2.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = v_we_2,
   collision_box = v_we_2,
   groups = {oddly_breakable_by_hand=3, not_in_creative_inventory=1, breakable=1},
   drop = 'timbers:v_we_1 2',
   after_dig_node = remove_empty,
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

minetest.register_node('timbers:v_we_2_support', {
   description = 'Double Timber (Inside Corner Support)',
   drawtype = 'mesh',
   mesh = 'timbers_v_we_2_support.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = v_we_2,
   collision_box = v_we_2,
   groups = {oddly_breakable_by_hand=3, breakable=1},
   after_dig_node = remove_empty,
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})

minetest.register_node('timbers:v_wc_2_support', {
   description = 'Double Timber (Centered on Wall Support)',
   drawtype = 'mesh',
   mesh = 'timbers_v_wc_2_support.obj',
   tiles = {'timbers_wood.png'},
   paramtype = 'light',
   paramtype2 = 'colorfacedir',
   palette = 'timbers_palette.png',
   selection_box = v_wc_2,
   collision_box = v_wc_2,
   groups = {oddly_breakable_by_hand=3, breakable=1},
   after_dig_node = remove_empty,
   on_rightclick = timbers.dye_less,
   on_punch = timbers.dye_more,
})
