local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
local S = minetest.get_translator("eg_mapgen")

-- Sound tables

local hard_sounds = {
	footstep = {name = "stone_footstep", gain = 0.3},
	dig = {name = "stone_dig", gain = 0.5},
	dug = {name = "stone_footstep", gain = 1.0},
	place = {name = "stone_place", gain = 1.0},
}

local soft_sounds = {
	footstep = {name = "dirt_footstep", gain = 0.4},
	dig = {name = "dig_crumbly", gain = 0.5},
	dug = {name = "dirt_footstep", gain = 1.0},
	place = {name = "place_node", gain = 1.0},
}

local water_sounds = {
	--footstep = {name = "water_footstep", gain = 0.2},
}


-- Register terrain nodes

minetest.register_node(modname .. ":stone", {
	description = "Stone",
	tiles = {"mapgen_stone.png"},
	groups = {cracky = 3, stone = 1},
	sounds = hard_sounds,
})
minetest.register_node(modname .. ":limestone", {
	description = S("Limestone"),
	tiles = {"eg_mapgen_limestone.png"},
	groups = {cracky = 3, stone = 1, calcium = 3},
	sounds = hard_sounds,
})


minetest.register_node(modname .. ":grass", {
	description = "Grass",
	tiles = {"mapgen_grass.png"},
	groups = {crumbly = 3},
	sounds = soft_sounds,
})

minetest.register_node(modname .. ":dirt", {
	description = "Dirt",
	tiles = {"mapgen_dirt.png"},
	groups = {crumbly = 3},
	sounds = soft_sounds,
})

minetest.register_node(modname .. ":sand", {
	description = "Sand",
	tiles = {"mapgen_sand.png"},
	groups = {crumbly = 3, falling_node = 1},
	sounds = soft_sounds,
})


-- Register dungeon nodes --TODO: make api mods for these or something

minetest.register_node(modname .. ":stone_block", {
	description = "Stone Block",
	tiles = {"mapgen_stone_block.png"},
	is_ground_content = false,
	groups = {cracky = 3},
	sounds = hard_sounds,
})

minetest.register_node(modname .. ":stone_block_stair", {
	description = "Stone Block Stair",
	drawtype = "nodebox",
	tiles = {
		"mapgen_stone_block_divided.png",
		"mapgen_stone_block.png",
		"mapgen_stone_block_divided.png"
	},
	paramtype = "light",
	paramtype2 = "facedir",
	is_ground_content = false,
	groups = {cracky = 3},
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
			{-0.5, 0.0, 0.0, 0.5, 0.5, 0.5},
		},
	},
	sounds = hard_sounds,
})
eg_slabs.register_slabs({[modname .. ":stone_block"] = "Stone Stairs"})

-- Register water nodes

minetest.register_node(modname .. ":water_source", {
	description = "Saltwater Source",
	drawtype = "liquid",
	tiles = {
		{
			name = "mapgen_water.png",
			backface_culling = false,
		},
		{
			name = "mapgen_water.png",
			backface_culling = true,
		},
	},
	alpha = 160,
	paramtype = "light",
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = true,
	is_ground_content = false,
	waving = 3,
	drop = "",
	drowning = 1,
	liquidtype = "source",
	liquid_alternative_flowing = modname .. ":water_flowing",
	liquid_alternative_source = modname .. ":water_source",
	liquid_viscosity = 1,
	post_effect_color = {a = 103, r = 30, g = 60, b = 90},
	sounds = water_sounds,
})

minetest.register_node(modname .. ":water_flowing", {
	description = "Flowing Saltwater",
	drawtype = "flowingliquid",
	tiles = {"mapgen_water.png"},
	special_tiles = {
		{
			name = "mapgen_water.png",
			backface_culling = false,
		},
		{
			name = "mapgen_water.png",
			backface_culling = false,
		},
	},
	alpha = 160,
	paramtype = "light",
	paramtype2 = "flowingliquid",
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = true,
	is_ground_content = false,
	waving = 3,
	drop = "",
	drowning = 1,
	liquidtype = "flowing",
	liquid_alternative_flowing = modname .. ":water_flowing",
	liquid_alternative_source = modname .. ":water_source",
	liquid_viscosity = 1,
	post_effect_color = {a = 103, r = 30, g = 60, b = 90},
	groups = {not_in_creative_inventory = 1},
	sounds = water_sounds,
})


-- Register river water nodes

-- This is an alternative water node required by mapgens with sloping rivers.
-- It has 'liquid_renewable = false' and a short 'liquid_range' to avoid
-- overflowing riverbanks.

minetest.register_node(modname .. ":river_water_source", {
	description = "Freshwater Source",
	drawtype = "liquid",
	tiles = {
		{
			name = "mapgen_river_water.png",
			backface_culling = false,
		},
		{
			name = "mapgen_river_water.png",
			backface_culling = true,
		},
	},
	alpha = 160,
	paramtype = "light",
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = true,
	is_ground_content = false,
	waving = 3,
	drop = "",
	drowning = 1,
	liquidtype = "source",
	liquid_alternative_flowing = modname .. ":river_water_flowing",
	liquid_alternative_source = modname .. ":river_water_source",
	liquid_viscosity = 1,
	liquid_renewable = false,
	liquid_range = 2,
	post_effect_color = {a = 103, r = 30, g = 76, b = 90},
	sounds = water_sounds,
})

minetest.register_node(modname .. ":river_water_flowing", {
	description = "Flowing Freshwater",
	drawtype = "flowingliquid",
	tiles = {"mapgen_river_water.png"},
	special_tiles = {
		{
			name = "mapgen_river_water.png",
			backface_culling = false,
		},
		{
			name = "mapgen_river_water.png",
			backface_culling = false,
		},
	},
	alpha = 160,
	paramtype = "light",
	paramtype2 = "flowingliquid",
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = true,
	is_ground_content = false,
	waving = 3,
	drop = "",
	drowning = 1,
	liquidtype = "flowing",
	liquid_alternative_flowing = modname .. ":river_water_flowing",
	liquid_alternative_source = modname .. ":river_water_source",
	liquid_viscosity = 1,
	liquid_renewable = false,
	liquid_range = 2,
	post_effect_color = {a = 103, r = 30, g = 76, b = 90},
	groups = {not_in_creative_inventory = 1},
	sounds = water_sounds,
})


-- Register magma nodes

minetest.register_node(modname .. ":magma_source", {
	description = "Magma Source",
	drawtype = "liquid",
	tiles = {
		{
			name = "mapgen_magma.png",
			backface_culling = false,
		},
		{
			name = "mapgen_magma.png",
			backface_culling = true,
		},
	},
	paramtype = "light",
	light_source = 13,
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = true,
	is_ground_content = false,
	waving = 3,
	drop = "",
	drowning = 1,
	liquidtype = "source",
	liquid_alternative_flowing = modname .. ":magma_flowing",
	liquid_alternative_source = modname .. ":magma_source",
	liquid_viscosity = 7,
	liquid_renewable = false,
	damage_per_second = 4 * 2,
	post_effect_color = {a = 191, r = 255, g = 64, b = 0},
})

minetest.register_node(modname .. ":magma_flowing", {
	description = "Flowing Magma",
	drawtype = "flowingliquid",
	tiles = {"mapgen_magma.png"},
	special_tiles = {
		{
			name = "mapgen_magma.png",
			backface_culling = false,
		},
		{
			name = "mapgen_magma.png",
			backface_culling = false,
		},
	},
	paramtype = "light",
	paramtype2 = "flowingliquid",
	light_source = 13,
	walkable = false,
	pointable = false,
	diggable = false,
	buildable_to = true,
	is_ground_content = false,
	waving = 3,
	drop = "",
	drowning = 1,
	liquidtype = "flowing",
	liquid_alternative_flowing = modname .. ":magma_flowing",
	liquid_alternative_source = modname .. ":magma_source",
	liquid_viscosity = 7,
	liquid_renewable = false,
	damage_per_second = 4 * 2,
	post_effect_color = {a = 191, r = 255, g = 64, b = 0},
	groups = {not_in_creative_inventory = 1},
})


-- Register aliases for map generators.
-- Tells engine mapgens which nodes to use for 'base terrain': the terrain
-- generated by an engine mapgen before biome nodes are applied.

minetest.register_alias("mapgen_stone", modname .. ":stone")
minetest.register_alias("mapgen_water_source", modname .. ":water_source")
minetest.register_alias("mapgen_river_water_source", modname .. ":river_water_source")


--Register limestone as an ore in stone

minetest.register_ore({
	ore_type = "puff",
	ore = modname .. ":limestone",
	wherein = modname .. ":stone",
	clust_size = 2,
	clust_scarcity = 1,
	clust_num_ores = 8,
	noise_threshold = 0.4,
	noise_params = {
		offset = 0,
		scalse = 2,
		spread = {x = 50, y = 50, z = 50},
		seed = 2749353,
		octaves = 3,
		persist = 0.7,
	},
	np_puff_top = {
		offset = 2,
		scale = 2,
		spread = {x = 100, y = 100, z = 100},
		seed = 593930,
		octaves = 3,
		persist = 0.7
	},
	np_puff_bottom = {
		offset = 2,
		scale = 2,
		spread = {x = 100, y = 100, z = 100},
		seed = 4296291,
		octaves = 3,
		persist = 0.7
	},
})
