decals = {}

decals.on_punch = 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

decals.on_rightclick = 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

function decals.register_decal(name, desc)
   minetest.register_node('decals:'..name, {
      description = desc,
      drawtype = 'mesh',
      mesh = 'decals_mesh.obj',
      tiles = {'decals_'..name..'.png'},
      inventory_image = 'decals_'..name..'.png',
      wield_image = 'decals_'..name..'.png',
      paramtype = 'light',
      paramtype2 = 'colorfacedir',
      palette = 'decals_color_palette.png',
      sunlight_propagates = true,
      use_texture_alpha = 'clip',
      walkable = false,
      selection_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      collision_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      groups = {breakable=1},
      on_place = minetest.rotate_and_place,
      on_rightclick = decals.on_rightclick,
      on_punch = decals.on_punch,
   })
end

function decals.register_decal_colored(name, desc)
   minetest.register_node('decals:'..name, {
      description = desc,
      drawtype = 'mesh',
      mesh = 'decals_mesh.obj',
      tiles = {'decals_'..name..'.png'},
      inventory_image = 'decals_'..name..'.png',
      wield_image = 'decals_'..name..'.png',
      paramtype = 'light',
      paramtype2 = 'facedir',
      sunlight_propagates = true,
      use_texture_alpha = 'blend',
      walkable = false,
      selection_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      collision_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      groups = {breakable=1},
      on_place = minetest.rotate_and_place,
   })
end

function decals.register_decal_raillike(name, desc)
   minetest.register_node('decals:'..name, {
      description = desc,
      drawtype = 'raillike',
      paramtype = 'light',
      tiles = {{name='decals_'..name..'_straight.png'}, {name='decals_'..name..'_curved.png'},
               {name='decals_'..name..'_t_junct.png'}, {name='decals_'..name..'_crossing.png'}},
      --tiles = {'decals_'..name..'.png' },
      inventory_image = 'decals_'..name..'_straight.png',
      wield_image = 'decals_'..name..'_straight.png',
      sunlight_propagates = true,
      use_texture_alpha = 'clip',
      walkable = false,
      selection_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      collision_box = {
         type = 'fixed',
         fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
      },
      groups = {breakable=1, rails=1},
   })
end


dofile(minetest.get_modpath('decals')..'/alphanumeric.lua') --Letters and numbers
dofile(minetest.get_modpath('decals')..'/colored.lua') -- It'ems that are already colored, and don't get colorfacedir
dofile(minetest.get_modpath('decals')..'/shapes.lua') -- Different shapes
