-- Load tree's schematics
--local schems_pin_parasol = assert(loadfile(minetest.get_modpath("nalc_mediterranean").."/schematics/pin_parasol.lua"))()
--
--local schems_pin_maritime = assert(loadfile(minetest.get_modpath("nalc_mediterranean").."/schematics/pin_maritime.lua"))()
--
--local schems_cypres = assert(loadfile(minetest.get_modpath("nalc_mediterranean").."/schematics/cypres.lua"))()
--
--local schems_olivier = assert(loadfile(minetest.get_modpath("nalc_mediterranean").."/schematics/olivier.lua"))()

-- Mediterranean Dirt definition
local dirt_data = table.copy(minetest.registered_nodes["default:dirt_with_dry_grass"])

dirt_data.description = "Dirt with Mediterranean Grass"
dirt_data.tiles =
	{"default_dry_grass.png^[multiply:#8ACA00", "default_dirt.png",
	 {name = "default_dirt.png^(default_dry_grass_side.png^[multiply:#8ACA00)",
	  tileable_vertical = false}
	}

minetest.register_node("nalc_mediterranean:dirt_with_mediterranean_grass", dirt_data)

-- Mediterranean pine needles
-- Pine Needles
local needles_data = table.copy(minetest.registered_nodes["default:pine_needles"])
needles_data.description = "Mediterranean Pine Needles"
needles_data.drop = {
	max_items = 1,
	items = {
		{
			items = {'nalc_mediterranean:sapling_umbrella_pine'},
			rarity = 120,
		},
		{
			items = {'nalc_mediterranean:sapling_maritime_pine'},
			rarity = 120,
		},
		{
			items = {'nalc_mediterranean:sapling_cypress'},
			rarity = 120,
		},
		{
			items = {'nalc_mediterranean:needles'},
		}
	}
}

minetest.register_node("nalc_mediterranean:needles", needles_data)

-- Olive tree definition
local tree_data = table.copy(minetest.registered_nodes["default:tree"])
tree_data.description = "Olive tree trunk"
tree_data.tiles = {"default_tree_top.png", "default_tree_top.png", "default_pine_tree.png^[colorize:#8F8F8F6F"}

minetest.register_node("nalc_mediterranean:olive_tree", tree_data)

-- Mediterranean Olive leaves
local leaves_data = table.copy(minetest.registered_nodes["default:leaves"])
leaves_data.description = "Olive Leaves"
leaves_data.tiles = {"feuilles_olivier.png"}
leaves_data.special_tiles = {"feuilles_olivier.png"}
leaves_data.drop = {
	max_items = 1,
	items = {
		{
			items = {'nalc_mediterranean:sapling_olive'},
			rarity = 40,
		},
		{
			items = {'nalc_mediterranean:olive_leaves'},
		}
	}
}

minetest.register_node("nalc_mediterranean:olive_leaves", leaves_data)

-- Saplings Definitions
local path = minetest.get_modpath("nalc_mediterranean").."/schematics"

local function grow_umbrella_pine(pos)
	minetest.place_schematic({x= pos.x -10, y = pos.y - 1, z = pos.z - 10}, path.."/pin_parasol.mts", "random", nil, true)
end

local function grow_maritime_pine(pos)
	minetest.place_schematic({x= pos.x -3, y = pos.y - 1, z = pos.z - 3}, path.."/pin_maritime.mts", "0", nil, true)
end

local function grow_cypress(pos)
	minetest.place_schematic({x= pos.x -3, y = pos.y - 1, z = pos.z - 3}, path.."/cypres.mts", "0", nil, true)
end

local function grow_olive_tree(pos)
	minetest.place_schematic({x= pos.x -3, y = pos.y - 1, z = pos.z - 3}, path.."/olivier.mts", "random", nil, true)
end

local function grow_sapling(pos)
	if not default.can_grow(pos) then
		-- try again 5 min later
		minetest.get_node_timer(pos):start(300)
		return
	end

	local node = minetest.get_node(pos)
	if node.name == "nalc_mediterranean:sapling_umbrella_pine" then
		minetest.log("action", "An Umbrella Pine sapling grows into a tree at "..minetest.pos_to_string(pos))
		grow_umbrella_pine(pos)
	elseif node.name == "nalc_mediterranean:sapling_maritime_pine" then
		minetest.log("action", "A Maritime Pine sapling grows into a tree at "..minetest.pos_to_string(pos))
		grow_maritime_pine(pos)
	elseif node.name == "nalc_mediterranean:sapling_cypress" then
		minetest.log("action", "A Cypress sapling grows into a tree at "..minetest.pos_to_string(pos))
		grow_cypress(pos)
	elseif node.name == "nalc_mediterranean:sapling_olive" then
		minetest.log("action", "An Olive sapling grows into a tree at "..minetest.pos_to_string(pos))
		grow_olive_tree(pos)
	end
end

minetest.register_lbm({
	name = "nalc_mediterranean:convert_saplings_to_node_timer",
	nodenames = {"nalc_mediterranean:sapling_umbrella_pine", "nalc_mediterranean:sapling_maritime_pine", "nalc_mediterranean:sapling_cypress", "nalc_mediterranean:sapling_olive"},
	action = function(pos)
		minetest.get_node_timer(pos):start(math.random(300, 1500))
	end
})

-- Saplings --

local sapling_data = table.copy(minetest.registered_nodes["default:pine_sapling"])

local trees = {
	{ description = "Umbrella Pine Sapling",
	  sapling = "nalc_mediterranean:sapling_umbrella_pine",
	  minp = {x = -10, y = 1, z = -10},
	  maxp = {x = 10, y = 16, z = 10},
	  texture = "default_pine_sapling.png",
	},
	{ description = "Maritime Pine Sapling",
	  sapling = "nalc_mediterranean:sapling_maritime_pine",
	  minp = {x = -3, y = 1, z = -3},
	  maxp = {x = 3, y = 14, z = 3},
	  texture = "default_pine_sapling.png",
	},
	{ description = "Cypress Sapling",
	  sapling = "nalc_mediterranean:sapling_cypress",
	  minp = {x = -3, y = 1, z = -3},
	  maxp = {x = 3, y = 19, z = 3},
	  texture = "default_pine_sapling.png",
	},
	{ description = "Olive Sapling",
	  sapling = "nalc_mediterranean:sapling_olive",
	  minp = {x = -3, y = 1, z = -3},
	  maxp = {x = 3, y = 9, z = 3},
	  texture = "default_sapling.png",
	},
}

for _, tree in pairs(trees) do
	sapling_data.description = tree.description
	sapling_data.tiles = {tree.texture}
	sapling_data.inventory_image = tree.texture
	sapling_data.wield_image = tree.texture
	sapling_data.on_timer = grow_sapling
	sapling_data.on_place = function(itemstack, placer, pointed_thing)
		itemstack = default.sapling_on_place(
			itemstack,
			placer,
			pointed_thing,
			tree.sapling,
			tree.minp,
			tree.maxp,
			4)
		return itemstack
	end

	minetest.register_node(tree.sapling, table.copy(sapling_data))
end

-- Biome méditéranéen
    -- Température : 77
    -- Humidité : 50
    -- Altitude : 3-50

minetest.register_biome({
		name = "mediterranean",
		node_top = "nalc_mediterranean:dirt_with_mediterranean_grass",
		depth_top = 1,
		node_filler = "default:dirt",
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		node_dungeon = "default:cobble",
		node_dungeon_alt = "default:mossycobble",
		node_dungeon_stair = "default:stair_cobble",
		y_min = 4,
		y_max = 150,
		heat_point = 77,
		humidity_point = 50,
})

-- Biome méditéranéen plage
    -- Température : 77
    -- Humidité : 50
    -- Altitude : 0-3
minetest.register_biome({
		name = "mediterranean_shore",
		node_top = "default:sand",
		depth_top = 1,
		node_filler = "default:sand",
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		node_dungeon = "default:cobble",
		node_dungeon_alt = "default:mossycobble",
		node_dungeon_stair = "default:stair_cobble",
		y_min = 0,
		y_max = 3,
		heat_point = 77,
		humidity_point = 50,
})
	
-- Decorations --

minetest.register_decoration({
		name = "nalc_mediterranean:umbrella_pine",
		deco_type = "schematic",
		place_on = {"nalc_mediterranean:dirt_with_mediterranean_grass"},
		sidelen = 16,
		fill_ratio = 0.0005,
		biomes = {"mediterranean"},
		y_min = 4,
		y_max = 60,
		schematic = path.."/pin_parasol.mts",
		flags = "place_center_x, place_center_z",
		rotation = "random",
		spawn_by = "nalc_mediterranean:dirt_with_mediterranean_grass",
		num_spawn_by = 4,
})

minetest.register_decoration({
		name = "nalc_mediterranean:maritime_pine",
		deco_type = "schematic",
		place_on = {"nalc_mediterranean:dirt_with_mediterranean_grass"},
		sidelen = 16,
		--		fill_ratio = 0.005,
		noise_params = {
			offset = 0.003,
			scale = -0.0096,
			spread = {x = 200, y = 200, z = 200},
			seed = 2,
			octaves = 3,
			persist = 0.68,
		},
		biomes = {"mediterranean"},
		y_min = 4,
		y_max = 100,
		schematic = path.."/pin_maritime.mts",
		flags = "place_center_x, place_center_z",
})

minetest.register_decoration({
		name = "nalc_mediterranean:cypress",
		deco_type = "schematic",
		place_on = {"nalc_mediterranean:dirt_with_mediterranean_grass"},
		sidelen = 16,
		--		fill_ratio = 0.0005,
		noise_params = {
			offset = 0,
			scale = 0.003,
			spread = {x = 200, y = 200, z = 200},
			seed = 329,
			octaves = 3,
			persist = 0.6,
		},
		biomes = {"mediterranean"},
		y_min = 4,
		y_max = 40,
		schematic = path.."/cypres.mts",
		flags = "place_center_x, place_center_z",
})

minetest.register_decoration({
		name = "nalc_mediterranean:olive_tree1",
		deco_type = "schematic",
		place_on = {"nalc_mediterranean:dirt_with_mediterranean_grass"},
		sidelen = 16,
		fill_ratio = 0.00005,
--		noise_params = {
--			offset = 0,
--			scale = 0.003,
--			spread = {x = 200, y = 200, z = 200},
--			seed = 329,
--			octaves = 3,
--			persist = 0.6,
--		},
		biomes = {"mediterranean"},
		y_min = 4,
		y_max = 100,
		schematic = path.."/olivier.mts",
		flags = "place_center_x, place_center_z",
		rotation = "random",
})

minetest.register_decoration({
		name = "nalc_mediterranean:olive_tree2",
		deco_type = "schematic",
		place_on = {"nalc_mediterranean:dirt_with_mediterranean_grass"},
		sidelen = 16,
		fill_ratio = 0.0005,
--		noise_params = {
--			offset = 0,
--			scale = 0.003,
--			spread = {x = 200, y = 200, z = 200},
--			seed = 329,
--			octaves = 3,
--			persist = 0.6,
--		},
		biomes = {"mediterranean"},
		y_min = 30,
		y_max = 60,
		schematic = path.."/olivier.mts",
		flags = "place_center_x, place_center_z",
		rotation = "random",
})

-- Leaf decay

if minetest.get_modpath("snow") then
	default.register_leafdecay{
		trunks = {"default:pine_tree"},
		leaves = {"snow:needles", "default:pine_needles", "snow:needles_decorated", "nalc_mediterranean:needles"},
		radius = 5,
	}
else
	default.register_leafdecay{
		trunks = {"default:pine_tree"},
		leaves = {"default:pine_needles", "nalc_mediterranean:needles"},
		radius = 5,
	}
end

default.register_leafdecay{
	trunks = {"nalc_mediterranean:olive_tree"},
	leaves = {"nalc_mediterranean:olive_leaves"},
	radius = 3,
}

-- Grass decorations
local function register_grass_decoration(offset, scale, length)
	minetest.register_decoration({
		name = "nalc_mediterranean:grass_" .. length,
		deco_type = "simple",
		place_on = {"nalc_mediterranean:dirt_with_mediterranean_grass"},
		sidelen = 16,
		noise_params = {
			offset = offset,
			scale = scale,
			spread = {x = 200, y = 200, z = 200},
			seed = 329,
			octaves = 3,
			persist = 0.6
		},
		biomes = {"mediterranean"},
		y_max = 31000,
		y_min = 1,
		decoration = "default:grass_" .. length,
	})
end

local function register_dry_grass_decoration(offset, scale, length)
	minetest.register_decoration({
		name = "nalc_mediterranean:dry_grass_" .. length,
		deco_type = "simple",
		place_on = {"nalc_mediterranean:dirt_with_mediterranean_grass"},
		sidelen = 16,
		noise_params = {
			offset = offset,
			scale = scale,
			spread = {x = 200, y = 200, z = 200},
			seed = 330,
			octaves = 3,
			persist = 0.6
		},
		biomes = {"mediterranean"},
		y_max = 31000,
		y_min = 1,
		decoration = "default:dry_grass_" .. length,
	})
end

-- Grasses

register_grass_decoration(-0.03,  0.09,  5)
register_grass_decoration(-0.015, 0.075, 4)
register_grass_decoration(0,      0.06,  3)
register_grass_decoration(0.015,  0.045, 2)
register_grass_decoration(0.03,   0.03,  1)

-- Dry grasses

register_dry_grass_decoration(0.01, 0.05,  5)
register_dry_grass_decoration(0.03, 0.03,  4)
register_dry_grass_decoration(0.05, 0.01,  3)
register_dry_grass_decoration(0.07, -0.01, 2)
register_dry_grass_decoration(0.09, -0.03, 1)

-- Pine bush

minetest.register_decoration({
		name = "nalc_mediterranean:pine_bush",
		deco_type = "schematic",
		place_on = {"nalc_mediterranean:dirt_with_mediterranean_grass"},
		sidelen = 16,
		noise_params = {
			offset = -0.004,
			scale = 0.01,
			spread = {x = 100, y = 100, z = 100},
			seed = 137,
			octaves = 3,
			persist = 0.7,
		},
		biomes = {"mediterranean"},
		y_max = 31000,
		y_min = 4,
		schematic = minetest.get_modpath("default") .. "/schematics/pine_bush.mts",
		flags = "place_center_x, place_center_z",
})

-- Support bonemeal
if minetest.get_modpath("bonemeal") then
	local g_saplings = {}
	for _, tree in pairs(trees) do
		table.insert(g_saplings, {tree.sapling, grow_sapling, "soil"})
	end
	bonemeal:add_sapling(g_saplings)
end

minetest.log("action", "[nalc_mediterranean] loaded.")

-- local mts_save = function(name, schematic)
--	local s = minetest.serialize_schematic(schematic, "mts", {})
--	local path = minetest.get_modpath("nalc_mediterranean") .. "/schematics"
--	local filename = path .. "/" .. name .. ".mts"
--	filename = filename:gsub("\"", "\\\""):gsub("\\", "\\\\")
--	local file, err = io.open(filename, "wb")
--	if err == nil then
--		file:write(s)
--		file:flush()
--		file:close()
--	end
--	print("Wrote: " .. filename)
--end

--mts_save("pin_parasol", schems_pin_parasol)
--mts_save("pin_maritime", schems_pin_maritime)
--mts_save("cypres", schems_cypres)
--mts_save("olivier", schems_olivier)
