--[[

	Hyperloop Mod
	=============

	Copyright (C) 2017-2019 Joachim Stolberg

	LGPLv2.1+
	See LICENSE.txt for more information

]]--

-- for lazy programmers
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos
local M = minetest.get_meta

-- Load support for intllib.
local S = vents.S
local NS = vents.NS

local function station_name(pos)
	local dataSet = vents.get_station(pos)
	if dataSet then
		if dataSet.junction == true then
			return S('Junction at ')..SP(pos)
		elseif dataSet.name ~= nil then
			return S('Station \'')..dataSet.name..'\' at '..SP(pos)
		else
			return S('Station at ')..SP(pos)
		end
	end
	return S('Open end at ')..minetest.pos_to_string(pos)
end

function vents.check_network_level(pos, player)
	if vents.free_tube_placement_enabled then
		return
	end
	for key,_ in pairs(vents.Stations.tStations) do
		if pos.y == P(key).y then
			return
		end
	end
	vents.chat(player, S('There is no station/junction on this level. ')..
		S('Do you really want to start a new network?!'))
end

--                       North, East, South, West, Down, Up
local dirs_to_check = {1,2,3,4}  -- horizontal only
if vents.free_tube_placement_enabled then
	dirs_to_check = {1,2,3,4,5,6}  -- all directions
end

local Tube = tubelib2.Tube:new({
	dirs_to_check = dirs_to_check,
	max_tube_length = 1000,
	show_infotext = true,
	primary_node_names = {'vents:tubeS', 'vents:tubeS2', 'vents:tubeA', 'vents:tubeA2'},
	secondary_node_names = {'vents:junction', 'vents:station', 'vents:tube_wifi1'},
	after_place_tube = function(pos, param2, tube_type, num_tubes)
		if num_tubes == 2 then
			minetest.set_node(pos, {name = 'vents:tube'..tube_type..'2', param2 = param2})
		else
			minetest.set_node(pos, {name = 'vents:tube'..tube_type, param2 = param2})
		end
	end,
})

vents.Tube = Tube

minetest.register_node('vents:tubeS', {
	description = S('Vent'),
	inventory_image = minetest.inventorycube('vents_tube.png',
		'hyperloop_tube_open.png', 'vents_tube.png'),
	tiles = {
		-- up, down, right, left, back, front
		'hyperloop_tube_closed.png^[transformR90]',
		'hyperloop_tube_closed.png^[transformR90]',
		'hyperloop_tube_closed.png',
		'hyperloop_tube_closed.png',
		{
			image = 'hyperloop_tube_open_active.png',
			backface_culling = false,
			animation = {
				type = 'vertical_frames',
				aspect_w = 32,
				aspect_h = 32,
				length = 0.5,
			},
		},
	},
	drawtype = 'nodebox',
	node_box = {
		type = 'fixed',
		fixed = {
			{-8/16, -8/16, -8/16, -7/16,  8/16,  8/16},
			{ 7/16, -8/16, -8/16,  8/16,  8/16,  8/16},
			{-8/16,  7/16, -8/16,  8/16,  8/16,  8/16},
			{-8/16, -8/16, -8/16,  8/16, -7/16,  8/16},
			{-7/16, -7/16, -7/16,  7/16,  7/16,  7/16},
		},
	},
	selection_box = {
		type = 'fixed',
		fixed = {-8/16, -8/16, -8/16,  8/16, 8/16, 8/16},
	},

	after_place_node = function(pos, placer, itemstack, pointed_thing)
		if not Tube:after_place_tube(pos, placer, pointed_thing) then
			minetest.remove_node(pos)
			return true
		end
		return false
	end,

	after_dig_node = function(pos, oldnode, oldmetadata, digger)
		Tube:after_dig_tube(pos, oldnode, oldmetadata)
	end,

	paramtype2 = 'facedir', -- important!
	on_rotate = screwdriver.disallow, -- important!
	paramtype = 'light',
	is_ground_content = false,
	groups = {breakable=1},
	sounds = {footstep = {name = 'metal', gain = 1}},
})

minetest.register_node('vents:tubeS2', {
	description = 'Vent',
	tiles = {
		-- up, down, right, left, back, front
		'vents_tube.png',
		'vents_tube.png',
		'vents_tube.png',
		'vents_tube.png',
		{
			image = 'hyperloop_tube_open_active.png',
			backface_culling = false,
			animation = {
				type = 'vertical_frames',
				aspect_w = 32,
				aspect_h = 32,
				length = 0.5,
			},
		},
	},
	drawtype = 'nodebox',
	node_box = {
		type = 'fixed',
		fixed = {
			{-8/16, -8/16, -8/16, -7/16,  8/16,  8/16},
			{ 7/16, -8/16, -8/16,  8/16,  8/16,  8/16},
			{-8/16,  7/16, -8/16,  8/16,  8/16,  8/16},
			{-8/16, -8/16, -8/16,  8/16, -7/16,  8/16},
			{-7/16, -7/16, -7/16,  7/16,  7/16,  7/16},
		},
	},
	selection_box = {
		type = 'fixed',
		fixed = {-8/16, -8/16, -8/16,  8/16, 8/16, 8/16},
	},

	after_dig_node = function(pos, oldnode, oldmetadata, digger)
		Tube:after_dig_tube(pos, oldnode, oldmetadata)
	end,

	paramtype2 = 'facedir', -- important!
	on_rotate = screwdriver.disallow, -- important!
	paramtype = 'light',
	is_ground_content = false,
	diggable = false,
	groups = {not_in_creative_inventory=1},
	sounds = {footstep = {name = 'metal', gain = 1}},
})

minetest.register_node('vents:tubeA', {
	description = S('Vent'),
	inventory_image = minetest.inventorycube('vents_tube.png',
		'hyperloop_tube_open.png', 'vents_tube.png'),
	tiles = {
		-- up, down, right, left, back, front
		'hyperloop_tube_closed.png^[transformR90]',
		{
			image = 'hyperloop_tube_open_active.png',
			backface_culling = false,
			animation = {
				type = 'vertical_frames',
				aspect_w = 32,
				aspect_h = 32,
				length = 0.5,
			},
		},
		'hyperloop_tube_closed.png',
		'hyperloop_tube_closed.png',
		'hyperloop_tube_closed.png^[transformR90]',
		{
			image = 'hyperloop_tube_open_active.png',
			backface_culling = false,
			animation = {
				type = 'vertical_frames',
				aspect_w = 32,
				aspect_h = 32,
				length = 0.5,
			},
		},
	},
	drawtype = 'nodebox',
	node_box = {
		type = 'fixed',
		fixed = {
			{-8/16, -8/16, -8/16, -7/16,  8/16,  8/16},
			{ 7/16, -8/16, -8/16,  8/16,  8/16,  8/16},
			{-8/16,  7/16, -8/16,  8/16,  8/16,  8/16},
			{-8/16, -8/16,  7/16,  8/16,  8/16,  8/16},
			{-8/16, -8/16, -8/16,  8/16, -7/16, -7/16},
			{-7/16, -7/16, -7/16,  7/16,  7/16,  7/16},
		},
	},
	selection_box = {
		type = 'fixed',
		fixed = {-8/16, -8/16, -8/16,  8/16, 8/16, 8/16},
	},

	after_dig_node = function(pos, oldnode, oldmetadata, digger)
		Tube:after_dig_tube(pos, oldnode, oldmetadata)
	end,

	paramtype2 = 'facedir', -- important!
	on_rotate = screwdriver.disallow, -- important!
	paramtype = 'light',
	is_ground_content = false,
	groups = {breakable=1, not_in_creative_inventory=1},
	drop = 'vents:tubeS',
	sounds = {footstep = {name = 'metal', gain = 1}},
})

minetest.register_node('vents:tubeA2', {
	description = 'Hyperloop Tube',
	tiles = {'vents_tube.png',},

	after_dig_node = function(pos, oldnode, oldmetadata, digger)
		Tube:after_dig_tube(pos, oldnode, oldmetadata)
	end,

	paramtype2 = 'facedir', -- important!
	on_rotate = screwdriver.disallow, -- important!
	paramtype = 'light',
	is_ground_content = false,
	diggable = false,
	groups = {breakable=1, not_in_creative_inventory=1},
	sounds = {footstep = {name = 'metal', gain = 1}},
})
