local T = starsoul.translator
local lib = starsoul.mod.lib

starsoul.terrain = {}
local soilSounds = {}
local grassSounds = {}

minetest.register_node('starsoul:soil', {
	description = T 'Soil';
	tiles = {'default_dirt.png'};
	groups = {dirt = 1};
	drop = '';
	sounds = soilSounds;
	_starsoul = {
		onDestroy = function() end;
		kind = 'block';
		elements = {};
	};
})


minetest.register_node('starsoul:sand', {
	description = T 'Sand';
	tiles = {'default_sand.png'};
	groups = {dirt = 1};
	drop = '';
	sounds = soilSounds;
	_starsoul = {
		kind = 'block';
		fab = starsoul.type.fab { element = { silicon = 25 } };
	};
})
minetest.register_craftitem('starsoul:soil_clump', {
	short_description = T 'Soil';
	description = starsoul.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 = 'starsoul-item-soil.png';
	groups = {soil = 1};
	_starsoul = {
		fab = starsoul.type.fab { element = { carbon = 12 / 4 } };
	};
})

function starsoul.terrain.createGrass(def)
	local function grassfst(i)
		local nextNode = def.name
		if i >= 0 then
			nextNode = nextNode .. '_walk_' .. tostring(i)
		end
		return {
			onWalk = function(pos)
				minetest.set_node_at(pos, def.name .. '_walk_2');
			end;
			onDecay = function(pos,delta)
				minetest.set_node_at(pos, nextNode);
			end;
			onDestroy = function(pos) end;
			fab = def.fab;
			recover = def.recover;
			recover_vary = def.recover_vary;
		};
	end
	local drop = {
		max_items = 4;
		items = {
			{
				items = {'starsoul:soil'}, rarity = 2;
				tool_groups = { 'shovel', 'trowel' };
			};
		};
	}
	minetest.register_node(def.name, {
		description = T 'Greengraze';
		tiles = {
			def.img .. '.png';
			'default_dirt.png';
			{
				name = 'default_dirt.png^' .. def.img ..'_side.png';
				tileable_vertical = false;
			};
		};
		groups = {grass = 1, sub_walk = 1};
		drop = '';
		sounds = grassSounds;
		_starsoul = grassfst(2);
	})
	for i=2,0,-1 do
		local opacity = tostring((i/2.0) * 255)

		minetest.register_node(def.name, {
			description = def.desc;
			tiles = {
				def.img .. '.png^(default_footprint.png^[opacity:'..opacity..')';
				'default_dirt.png';
				{
					name = 'default_dirt.png^' .. def.img ..'_side.png';
					tileable_vertical = false;
				};
			};
			groups = {grass = 1, sub_walk = 1, sub_decay = 5};
			drop = '';
			_starsoul = grassfst(i-1);
			sounds = grassSounds;
		})
	end
end


starsoul.terrain.createGrass {
	name = 'starsoul:greengraze';
	desc = T 'Greengraze';
	img = 'default_grass';
	fab = starsoul.type.fab {
		element = {
			carbon = 12;
		};
		time = {
			shred = 2.5;
		};
	};
}

for _, w in pairs {false,true} do
	minetest.register_node('starsoul:liquid_water' .. (w and '_flowing' or ''), {
		description = T 'Water';
		drawtype = 'liquid';
		waving = 3;
		tiles = {
			{
				name = "default_water_source_animated.png";
				backface_culling = false;
				animation = {
					type = "vertical_frames";
					aspect_w = 16;
					aspect_h = 16;
					length = 2.0;
				};
			};
			{
				name = "default_water_source_animated.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 = false, diggable = false, buildable_to = true;
		is_ground_content = false;
		drop = '';
		drowning = 1;
		liquidtype = w and 'flowing' or 'source';
		liquid_alternative_flowing = 'starsoul:liquid_water_flowing';
		liquid_alternative_source = 'starsoul: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


starsoul.world.mineral.foreach('starsoul:mineral_generate', {}, function(name,m)
	local node = string.format('starsoul:mineral_%s', name)
	local grp = {mineral = 1}
	minetest.register_node(node, {
		description = m.desc;
		tiles = m.tiles or 
				(m.tone and {
					string.format('default_stone.png^[colorizehsl:%s:%s:%s',
						m.tone.hue, m.tone.sat, m.tone.lum)
				}) or {'default_stone.png'};
		groups = grp;
		drop = m.rocks or '';
		_starsoul = {
			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(name, i)
		end
		minetest.register_ore {
			ore = node;
			ore_type = m.dist.kind;
			wherein = {m.dist.among};
			clust_scarcity = m.dist.rare;
			y_max = m.dist.height[1], y_min = m.dist.height[2];
			noise_params = m.dist.noise or {
				offset = 28;
				scale = 16;
				spread = vector.new(128,128,128);
				seed = seed;
				octaves = 1;
			};
		}
	end
end)

starsoul.world.mineral.link('feldspar', {
	desc = T 'Feldspar';
	excludeOre = true;
	recover = starsoul.type.fab {
		time = {
			shred = 3;
		};
		cost = {
			shredPower = 3;
		};
	};
	recover_vary = function(rng, ctx)
		print('vary!', rng:int(), rng:int(0,10))
		return starsoul.type.fab {
			element = {
				aluminum  = rng:int(0,4);
				potassium = rng:int(0,2);
				calcium   = rng:int(0,2);
			}
		};
	end;
})

-- map generation

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

