-- [Mod] Simple Arcs [pkarcs]
-- by PEAK
-- 06-05-2017

pkarcs = {}

-- define nodes

function pkarcs.register_all(nodename, desc, tile, sound, group, craftmaterial)
   local tile_collection
   if type(tile) == "string" then
      tile_collection[1] = tile
   else
      tile_collection = table.copy(tile)
   end

   minetest.register_node(":pkarcs:"..nodename.."_arc", {
      description = desc.." Arc",
      paramtype = "light",
      paramtype2 = "facedir",
      tiles = tile_collection,
      drawtype = "mesh",
      mesh = 'pkarcs_center.obj',
      groups = group,
      sounds = sound,
      on_place = function(itemstack, placer, pointed_thing)
         if pointed_thing.type ~= "node" then
            return itemstack
         end
         local p1 = pointed_thing.under
         local p0 = pointed_thing.above
         local param2 = 0
         local placer_pos = placer:get_pos()
         if placer_pos then
            local dir = {
               x = p1.x - placer_pos.x,
               y = p1.y - placer_pos.y,
               z = p1.z - placer_pos.z
            }
            param2 = minetest.dir_to_facedir(dir)
         end
         if p0.y-1 == p1.y then
            param2 = param2 + 20
            if param2 == 21 then
               param2 = 23
            elseif param2 == 23 then
               param2 = 21
            end
         end
         local NROT = 4 -- Number of possible "rotations" (4 Up down left right)
         local rot = param2 % NROT
         local wall = math.floor(param2/NROT)
         if rot >=3 then
            rot = 0
         else
            rot = rot +1
         end
         param2 = wall*NROT+rot
         return minetest.item_place(itemstack, placer, pointed_thing, param2)
      end,
   })

   minetest.register_node(":pkarcs:"..nodename.."_outer_arc", {
      description = desc.." Outer Arc",
      paramtype = "light",
      paramtype2 = "facedir",
      tiles = tile_collection,
      drawtype = "mesh",
      mesh = 'pkarcs_O_corner.obj',
      groups = group,
      sounds = sound,
      on_place = function(itemstack, placer, pointed_thing)
         if pointed_thing.type ~= "node" then
            return itemstack
         end
         local p1 = pointed_thing.under
         local p0 = pointed_thing.above
         local param2 = 0
         local placer_pos = placer:get_pos()
         if placer_pos then
            local dir = {
               x = p1.x - placer_pos.x,
               y = p1.y - placer_pos.y,
               z = p1.z - placer_pos.z
            }
            param2 = minetest.dir_to_facedir(dir)
         end
         if p0.y-1 == p1.y then
            param2 = param2 + 20
            if param2 == 21 then
               param2 = 23
            elseif param2 == 23 then
               param2 = 21
            end
         end
         local NROT = 4 -- Number of possible "rotations" (4 Up down left right)
         local rot = param2 % NROT
         local wall = math.floor(param2/NROT)
         if rot >=3 then
            rot = 0
         else
            rot = rot +1
         end
         param2 = wall*NROT+rot
         return minetest.item_place(itemstack, placer, pointed_thing, param2)
      end,
   })

   minetest.register_node(":pkarcs:"..nodename.."_inner_arc", {
      description = desc.." Inner Arc",
      paramtype = "light",
      paramtype2 = "facedir",
      tiles = tile_collection,
      drawtype = "mesh",
      mesh = 'pkarcs_I_corner.obj',
      groups = group,
      sounds = sound,
      on_place = function(itemstack, placer, pointed_thing)
         if pointed_thing.type ~= "node" then
            return itemstack
         end
         local p1 = pointed_thing.under
         local p0 = pointed_thing.above
         local param2 = 0
         local placer_pos = placer:get_pos()
         if placer_pos then
            local dir = {
               x = p1.x - placer_pos.x,
               y = p1.y - placer_pos.y,
               z = p1.z - placer_pos.z
            }
            param2 = minetest.dir_to_facedir(dir)
         end
         if p0.y-1 == p1.y then
            param2 = param2 + 20
            if param2 == 21 then
               param2 = 23
            elseif param2 == 23 then
               param2 = 21
            end
         end
         local NROT = 4 -- Number of possible "rotations" (4 Up down left right)
         local rot = param2 % NROT
         local wall = math.floor(param2/NROT)
         if rot >=3 then
            rot = 0
         else
            rot = rot +1
         end
         param2 = wall*NROT+rot
         return minetest.item_place(itemstack, placer, pointed_thing, param2)
      end,
   })


end

-- register nodes

function pkarcs.register_node(name)
   local node_def = minetest.registered_nodes[name]
   if not node_def then
      minetest.log("warning", "[pkarcs] Skipping unknown node: ".. name)
      return
   end
   local node_name = name:split(':')[2]

   if not node_def.tiles then
      node_def.tiles = table.copy(node_def.tile_images)
      node_def.tile_images = nil
   end

   pkarcs.register_all(node_name, node_def.description, node_def.tiles, node_def.sounds, node_def.groups, name)
end

function pkarcs.register_craft(station, mod, node)
   pkarcs.register_node(mod..':'..node)

   stations.dual_register_recipe(station, {
      input = {
         [mod..':'..node] = 1,
      },
      output = 'pkarcs:'..node..'_outer_arc 3',
   })

   stations.dual_register_recipe(station, {
      input = {
         [mod..':'..node] = 1,
      },
      output = 'pkarcs:'..node..'_arc 2',
   })

   stations.dual_register_recipe(station, {
      input = {
         [mod..':'..node] = 1,
      },
      output = 'pkarcs:'..node..'_inner_arc',
   })
end

pkarcs.register_craft('woodworking', 'default', 'wood')
pkarcs.register_craft('woodworking', 'default', 'junglewood')
pkarcs.register_craft('woodworking', 'default', 'pine_wood')
pkarcs.register_craft('woodworking', 'default', 'acacia_wood')
pkarcs.register_craft('woodworking', 'default', 'aspen_wood')

pkarcs.register_craft('stone_carving', 'asteroid', 'redstone')
pkarcs.register_craft('stone_carving', 'caverealms', 'glow_obsidian')
pkarcs.register_craft('stone_carving', 'caverealms', 'glow_obsidian_2')
pkarcs.register_craft('stone_carving', 'darkage', 'basalt')
pkarcs.register_craft('stone_carving', 'darkage', 'gneiss')
pkarcs.register_craft('stone_carving', 'darkage', 'rhyolitic_tuff')
pkarcs.register_craft('stone_carving', 'darkage', 'marble')
pkarcs.register_craft('stone_carving', 'darkage', 'ors')
pkarcs.register_craft('stone_carving', 'darkage', 'schist')
pkarcs.register_craft('stone_carving', 'darkage', 'serpentine')
pkarcs.register_craft('stone_carving', 'darkage', 'shale')
pkarcs.register_craft('stone_carving', 'darkage', 'slate')
pkarcs.register_craft('stone_carving', 'darkage', 'tuff')
pkarcs.register_craft('stone_carving', 'default', 'brick')
pkarcs.register_craft('stone_carving', 'default', 'desert_stone')
pkarcs.register_craft('stone_carving', 'default', 'desert_sandstone')
pkarcs.register_craft('stone_carving', 'default', 'obsidian')
pkarcs.register_craft('stone_carving', 'default', 'sandstone')
pkarcs.register_craft('stone_carving', 'default', 'silver_sandstone')
pkarcs.register_craft('stone_carving', 'default', 'stone')
pkarcs.register_craft('stone_carving', 'default', 'obsidian')
pkarcs.register_craft('stone_carving', 'moreblocks', 'coal_stone')
pkarcs.register_craft('stone_carving', 'moreblocks', 'iron_stone')
pkarcs.register_craft('stone_carving', 'nether', 'rack')
