-- CONTENTS
-- TARDIS exterior functions
-- TARDIS Node Definitions



-- TARDIS EXTERIOR FUNCTIONS
local tardis_on_place = function(pos, placer, itemstack, pointed_thing)
	if placer:get_player_name() == "" then return else
		local meta = minetest.get_meta(pos)
		local name = placer:get_player_name()
		local timer = minetest.get_node_timer(pos)
		if data:get_string(name.."in_pos") == "" then
			data:set_string(name.."out_pos", minetest.serialize(pos)) --set exterior position
			pos.y = pos.y-300
			minetest.place_schematic(pos, minetest.get_modpath("drwho_tardis") .. "/schems/console_room.mts")
			pos.y = pos.y+2
			pos.x = pos.x+7
			pos.z = pos.z+16
			local ometa = minetest.get_meta(pos)
			local otimer = minetest.get_node_timer(pos)
			otimer:start(0.2) --start door timer (in case it doesn't start on construct)
			ometa:set_string("id", name) --set door id
			meta:set_string("id", name) -- set exterior id
			data:set_string(name.."in_pos", minetest.serialize(pos)) --set interior position
			data:set_int(name.."power", 0) --set power
			data:set_int(name.."factor", 1) -- set travel factor
			data:set_string(name.."look", "drwho_tardis:tardis_old") --set skin
			data:set_int(name.."y_dest", 0)
			data:set_int(name.."x_dest", 0)
			data:set_int(name.."z_dest", 0)
			data:set_string(name.."way1", minetest.serialize({x=0, y=0, z=0}) )
			data:set_string(name.."way2", minetest.serialize({x=0, y=0, z=0}) )
			data:set_string(name.."way3",minetest.serialize({x=0, y=0, z=0}) )
			data:set_string(name.."r_pos", "") 
			timer:start(0.2)
		else minetest.set_node(pos, {name = "air"}) minetest.add_item(pos, "drwho_tardis:tardis_old") end
	end
end

-- Check if there are any players or mobs entering a TARDIS and teleport them
local tardis_timer = function(pos)
	local objs = minetest.get_objects_inside_radius(pos, 0.9)
	--for idx,obj in pairs(objs) do -- loop incase there are multiple objs there at once
	local obj = objs[1]
		if obj == nil then return true else --only do code if there is something there
			if obj:is_player() then -- if obj is a player
				local meta = minetest.get_meta(pos)
				local pmeta = obj:get_meta()
				local id = meta:get_string("id")
				local go_pos = minetest.deserialize(data:get_string(id.."in_pos"))
				local name = obj:get_player_name()
				go_pos.z = go_pos.z-1
				obj:set_look_horizontal( math.rad( 180 ))
				obj:set_look_vertical( math.rad( 0 ))
				obj:set_pos(go_pos)
				if minetest.get_modpath("awards") then
					awards.unlock(name, "drwho_tardis:go_inside_tardis") -- unlock award
				end
				pmeta:set_string("id", id)
			else -- if obj is not a player (probably a mob)
				local meta = minetest.get_meta(pos)
				local id = meta:get_string("id")
				local go_pos = minetest.deserialize(data:get_string(id.."in_pos"))
				go_pos.z = go_pos.z-2
				obj:set_pos(go_pos)
			end
		end
		return true
	--end
end




--------------------
--NODE DEFINITIONS--
--------------------
minetest.register_node("drwho_tardis:tardis", {
		description = "Tardis",
		tiles = {"tardis_main.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		light_source = 10,
		groups = {not_in_creative_inventory = 1, tardis = 1},
		after_place_node = tardis_on_place,
		diggable = false,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_locked", {
		description = "Tardis (locked)",
		tiles = {"tardis_main.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		light_source = 10,
		groups = {not_in_creative_inventory = 1, tardis_locked = 1},
		diggable = false
})
minetest.register_node("drwho_tardis:tardis_pink", {
		description = "Tardis",
		tiles = {"tardis_pink.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		light_source = 10,
		groups = {not_in_creative_inventory = 1, tardis = 1},
		diggable = false,
		after_place_node = tardis_on_place,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_pink_locked", {
		description = "Tardis (locked)",
		tiles = {"tardis_pink.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		light_source = 10,
		groups = {not_in_creative_inventory = 1, tardis_locked = 1},
		diggable = false
})
minetest.register_node("drwho_tardis:tardis_yellow", {
		description = "Tardis",
		tiles = {"tardis_yellow.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		light_source = 10,
		diggable = false,
		groups = {not_in_creative_inventory = 1, tardis = 1},
		after_place_node = tardis_on_place,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_yellow_locked", {
		description = "Tardis (locked)",
		tiles = {"tardis_yellow.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		light_source = 10,
		groups = {not_in_creative_inventory = 1, tardis_locked = 1},
		diggable = false
})
minetest.register_node("drwho_tardis:tardis_green", {
	description = "Tardis",
	tiles = {"tardis_green.png"},
	drawtype = "mesh",
	mesh = "tardis_2.obj",
	selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
	collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
	light_source = 10,
	diggable = false,
	groups = {not_in_creative_inventory = 1, tardis = 1},
	after_place_node = tardis_on_place,
	on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_green_locked", {
	description = "Tardis (locked)",
	tiles = {"tardis_green.png"},
	drawtype = "mesh",
	mesh = "tardis_2.obj",
	selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
	collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
	light_source = 10,
	groups = {not_in_creative_inventory = 1, tardis_locked = 1},
	diggable = false
})
minetest.register_node("drwho_tardis:tardis_old", {
		description = "Tardis",
		tiles = {"tardis_old.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		light_source = 10,
		groups = {tardis = 1},
		diggable = false,
		after_place_node = tardis_on_place,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_old_locked", {
		description = "Tardis (locked)",
		tiles = {"tardis_old.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		light_source = 10,
		groups = {not_in_creative_inventory = 1, tardis_locked = 1},
		diggable = false
})
minetest.register_node("drwho_tardis:tardis_stone", {
		description = "Tardis",
		tiles = {"default_stone.png"},
		drawtype = "nodebox",
		node_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		groups = {not_in_creative_inventory = 1, tardis = 1},
		diggable = false,
		after_place_node = tardis_on_place,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_stone_locked", {
		description = "Tardis",
		tiles = {"default_stone.png"},
		drawtype = "nodebox",
		node_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		groups = {not_in_creative_inventory = 1, tardis_locked = 1},
		diggable = false
})
minetest.register_node("drwho_tardis:tardis_empty", {
		description = "Tardis",
		tiles = {"empty.png"},
		drawtype = "nodebox",
		node_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		groups = {not_in_creative_inventory = 1,  tardis = 1},
		diggable = false,
		after_place_node = tardis_on_place,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_empty_locked", {
		description = "Tardis",
		tiles = {"empty.png"},
		drawtype = "nodebox",
		node_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		groups = {not_in_creative_inventory = 1,  tardis_locked = 1},
		diggable = false
})
minetest.register_node("drwho_tardis:tardis_cool", {
		description = "Tardis",
		tiles = {"tardis_cool.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		groups = {not_in_creative_inventory = 1,  tardis = 1},
		diggable = false,
		after_place_node = tardis_on_place,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_cool_locked", {
		description = "Tardis",
		tiles = {"tardis_cool.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		groups = {not_in_creative_inventory = 1,  tardis_locked = 1},
		diggable = false
})
minetest.register_node("drwho_tardis:tardis_leave", {
		description = "Tardis",
		tiles = {"default_leaves.png"},
		drawtype = "nodebox",
		node_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		groups = {not_in_creative_inventory = 1,  tardis = 1},
		diggable = false,
		after_place_node = tardis_on_place,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_leave_locked", {
		description = "Tardis",
		tiles = {"default_leaves.png"},
		drawtype = "nodebox",
		node_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		groups = {not_in_creative_inventory = 1,  tardis_locked = 1},
		diggable = false
})
minetest.register_node("drwho_tardis:tardis_soda", {
		description = "Tardis",
		tiles = {"tardis_main.png"}, --look like a police box, soda skin is removed
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		groups = {not_in_creative_inventory = 1, tardis = 1},
		diggable = false,
		after_place_node = tardis_on_place,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_soda_locked", {
		description = "Tardis",
		tiles = {"tardis_main.png"}, --look like a police box, soda skin is removed
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		groups = {not_in_creative_inventory = 1, tardis_locked = 1},
		diggable = false
})
minetest.register_node("drwho_tardis:tardis_funky", {
		description = "Tardis",
		tiles = {"tardis_funky.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { 0.48, -0.5,-0.5,  0.5,  1.5, 0.5}, {-0.5 , -0.5, 0.48, 0.48, 1.5, 0.5}, {-0.5,  -0.5,-0.5 ,-0.48, 1.5, 0.5}, { -0.8,-0.6,-0.8,0.8,-0.48, 0.8} }},
		groups = {not_in_creative_inventory = 1, tardis = 1},
		diggable = false,
		light_source = 10,
		after_place_node = tardis_on_place,
		on_timer = tardis_timer
})
minetest.register_node("drwho_tardis:tardis_funky_locked", {
		description = "Tardis",
		tiles = {"tardis_funky.png"},
		drawtype = "mesh",
		mesh = "tardis_2.obj",
		selection_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		collision_box = {type = "fixed", fixed = { { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 } }},
		light_source = 10,
		groups = {not_in_creative_inventory = 1, tardis_locked = 1},
		diggable = false
})
