local T = starlit.translator
local lib = starlit.mod.lib

local soilRec = starlit.type.fab {
	element = {
		carbon = 25;
	};
	time = { shred = 2.5; };
};

starlit.terrain = {}
local soilSounds = {
	footstep = 'default-dirt-footstep';
	dug = 'default-dug-node';
}
local sandSounds = {
	footstep = {name='default-sand-footstep',gain=0.1};
	dug = 'default-dug-node';
}
local grassSounds = {
	footstep = 'default-grass-footstep';
	dug = 'default-dug-node';
}
local hardSounds = {
	footstep = 'default-hard-footstep';
	dug = 'default-dug-node';
}

local soilDrop = {
	max_items = 3;
	items = {
		{rarity = 2, items = {'starlit:soil_clump'}};
		{rarity = 3, items = {'starlit:soil_clump'}};
		{rarity = 4, items = {'starlit:soil_clump'}};
	};
}
minetest.register_node('starlit:soil', {
	description = T 'Soil';
	tiles = {'starlit-terrain-soil.png'};
	groups = {looseClump = 2, soil = 1};
	drop = soilDrop;
	sounds = soilSounds;
	_starlit = {
		kind = 'block';
		elements = {};
		recover = soilRec;
	};
})


minetest.register_node('starlit:sand', {
	description = T 'Sand';
	tiles = {'starlit-terrain-sand.png'};
	groups = {looseClump = 1, sand = 1, falling_node = 1};
	drop = '';
	sounds = sandSounds;
	_starlit = {
		kind = 'block';
		fab = starlit.type.fab { element = { silicon = 25 } };
	};
})

minetest.register_node('starlit:lifesilt', {
	description = T 'Lifesilt';
	tiles = {'starlit-terrain-lifesilt.png'};
	groups = {looseClump = 1, lifesilt = 1, falling_node = 1};
	drop = '';
	sounds = sandSounds;
	_starlit = {
		kind = 'block';
		recover = starlit.type.fab { element = { carbon = 16, silicon = 16, rubidium = 4 } };
	};
})

minetest.register_craftitem('starlit:soil_clump', {
	short_description = T 'Soil';
	description = starlit.ui.tooltip {
		title = T 'Soil';
		desc = 'A handful of nutrient-packed soil, suitable for growing plants';
		color = lib.color(0.3,0.2,0.1);
	};
	inventory_image = 'starlit-item-soil.png';
	groups = {looseClump = 2, soil = 1};
	on_place = function(me, luser, point)
		if me:get_count() < 3 then return end
		if minetest.place_node(point.above, {name = 'starlit:soil'}, luser) then
			me:take_item(3)
		end
		return me
	end;
	_starlit = {
		recover = starlit.type.fab { element = { carbon = 25 / 4 } };
	};
})

function starlit.terrain.createGrass(def)
	local drop = {
		max_items = 4;
		items = {
			{
				items = {'starlit:soil'}, rarity = 2;
				tool_groups = { 'shovel', 'trowel' };
			};
		};
	}
	minetest.register_node(def.name, {
		description = T 'Greengraze';
		tiles = {
			def.img .. '.png';
			'starlit-terrain-soil.png';
			{
				name = 'starlit-terrain-soil.png^' .. def.img ..'-overlay.png';
				tileable_vertical = false;
			};
		};
		groups = {looseClump = 2, grass = 1, soil = 1};
		drop = soilDrop;
		sounds = grassSounds;
		_starlit = {
			recover = def.recover;
			recover_vary = def.recover_vary;
		};
	})
end


starlit.terrain.createGrass {
	name = 'starlit:greengraze';
	desc = T 'Greengraze';
	img = 'starlit-terrain-greengraze';
	recover = soilRec;
}

starlit.terrain.createGrass {
	name = 'starlit:undergloam';
	desc = T 'Undergloam';
	-- fungal carpet
	img = 'starlit-terrain-undergloam';
	recover = soilRec;
}

for _, w in pairs {false,true} do
	minetest.register_node('starlit:liquid_water' .. (w and '_flowing' or ''), {
		description = T 'Water';
		drawtype = 'liquid';
		waving = 3;
		tiles = {
			{
-- 				name = "default_water_source_animated.png";
				name = "starlit-water.png";
				backface_culling = false;
				animation = {
					type = "vertical_frames";
					aspect_w = 16;
					aspect_h = 16;
					length = 2.0;
				};
			};
			{
-- 				name = "default_water_source_animated.png";
				name = "starlit-water.png";
				backface_culling = true;
				animation = {
					type = "vertical_frames";
					aspect_w = 16;
					aspect_h = 16;
					length = 2.0;
				};
			};
		};
		use_texture_alpha = 'blend';
		paramtype = 'light';
		walkable = false, pointable = "blocking", diggable = false, buildable_to = true;
		is_ground_content = false;
		drop = '';
		drowning = 1;
		liquidtype = w and 'flowing' or 'source';
		liquid_alternative_flowing = 'starlit:liquid_water_flowing';
		liquid_alternative_source = 'starlit:liquid_water';
		liquid_viscosity = 1;
		liquid_renewable = true;
		liquid_range = 2;
		drowning = 40;
		post_effect_color = {a=103, r=10, g=40, b=70};
		groups = {water = 3, liquid = 3};
	});
end


starlit.world.mineral.foreach('starlit:mineral-generate', {}, function(node,m)
	local grp = {mineral = 1}
	minetest.register_node(node, {
		short_description = m.name;
		description = starlit.ui.tooltip {
			title = m.name;
			desc = m.desc;
			color = m.tone;
		};
		tiles = m.tiles or 
				(m.tone and {
					lib.image'starlit-terrain-granite.png':shift(m.tone):render()
				}) or nil;
		groups = grp;
		drop = m.rocks or '';
		sounds = hardSounds;
		_starlit = {
			kind = 'block';
			elements = m.elements;
			fab = m.fab;
			recover = m.recover;
			recover_vary = m.recover_vary;
		};
	})
	if not m.excludeOre then
		local seed = 0
-- 		grp.ore = 1
		for i = 1, #m.name do
			seed = seed*50 + string.byte(m.name, i)
		end
		minetest.register_ore {
			ore = node;
			ore_type = m.dist.kind;
			wherein = m.dist.among;
			clust_scarcity = m.dist.rare;
			clust_num_ores = m.dist.ores;
			clust_size = m.dist.clust;
			y_min = m.dist.height[1], y_max = m.dist.height[2];
			column_height_min = m.dist.sheetCols and m.dist.sheetCols[1] or nil;
			column_height_max = m.dist.sheetCols and m.dist.sheetCols[2] or nil;
			noise_params = m.dist.noise or {
				scale = 1;
				spread = vector.new(16,16,16);
				seed = seed;
				octaves = 2;
			};
		}
	end
end)

starlit.world.mineral.link('starlit:mineral_feldspar', {
	name = T 'Feldspar';
	tiles = {'starlit-terrain-feldspar.png'};
	excludeOre = true;
	recover = starlit.type.fab {
		time = { shred = 3; };
		cost = { shredPower = 3; };
	};
	recover_vary = function(rng, ctx)
		return starlit.type.fab {
			element = {
				aluminum  = rng:int(0,4);
				potassium = rng:int(0,2);
				calcium   = rng:int(0,2);
			}
		};
	end;
})

starlit.world.mineral.link('starlit:mineral_granite', {
	name = T 'Granite';
	tiles = {'starlit-terrain-granite.png'};
	dist = {
		kind = 'sheet';
		among = 'starlit:mineral_feldspar';
		height = {-200,30};
		sheetCols = {1, 16};
	};
	recover = starlit.type.fab {
		time = { shred = 3.5; };
		cost = { shredPower = 8; };
	};
	recover_vary = function(rng, ctx)
		return starlit.type.fab {
			element = {
				iron      = rng:int(0,4);
				magnesium = rng:int(0,2);
				titanium  = rng:int(0,1);
			}
		};
	end;
})

starlit.world.mineral.link('starlit:mineral_cuprite', {
	name = T 'Cuprite';
	dist = {
		kind = 'blob';
		among = 'starlit:mineral_feldspar';
		height = {-20,20};
		clust = 8;
		ores = 1;
		rare = 1000;
		color = lib.color(.5,.2,.1);
		noise = {
			scale = 1;
			spread = vector.new(5,5,5);
			seed = 0x1faf7e1;
			octaves = 1
		}
	};
	tiles = {'starlit-terrain-cuprite.png'};
	recover = starlit.type.fab {
		element = { copper = 10 };
		time = { shred = 3; };
		cost = { shredPower = 3; };
	};
})

-- map generation

minetest.register_alias('mapgen_stone', 'starlit:mineral_feldspar')
minetest.register_alias('mapgen_water_source', 'starlit:liquid_water')
minetest.register_alias('mapgen_river_water_source', 'starlit:liquid_water')

