local world = starlit.world
local lib = starlit.mod.lib

local function woodProps(def) return {
	recover = starlit.type.fab {
		time = { shred = 2; };
		cost = { shredPower = 1.5; };
	};
	recover_vary = function(rng, ctx)
		return starlit.type.fab {
			element = {
				potassium = rng:int(0,1);
				carbon   = rng:int(0,2);
			}
		};
	end;
	mass = 1.5e3;
} end

local function leafProps(def) return {
	recover = starlit.type.fab {
		time = { shred = .5; };
		cost = { shredPower = .3; };
	};
	recover_vary = function(rng, ctx)
		return starlit.type.fab {
			element = {
				potassium = rng:int(0,2);
				carbon   = rng:int(0,1);
			}
		};
	end;
	mass = 100;
} end

local function regLog(id, def)
	local base = table.copy(def)
	base.groups = base.groups or {}
	base.groups.wood = 1
	base.groups.log = 1
	base.groups.falling_node = 1

	local live = table.copy(base)
	live.drop = id
	live.groups.alive = 1
	minetest.register_node(id, base)
	minetest.register_node(id..'_live', live)
end

regLog('starlit_eco:lambent_pine_log', {
	description = 'Lambent Pine Log';
	drawtype = 'normal';
	tiles = {
		'starlit-eco-tree-lambent-pine-trunk-top.png';
		'starlit-eco-tree-lambent-pine-trunk.png';
	};
	_starlit = woodProps{};
})


starlit.item.food.link('starlit_eco:lambent_pine_berry', {
	name = 'Lambent Pine Berry';
	desc = 'Though packed with human-compatible nutrients, these berries are almost painfully sour when eaten raw.';
	tex = lib.image('starlit-eco-plant-berry-bunch.png'):shift{hue=180,sat=-30,lum=30}:render();
	nourish = 150;
	taste = -2 * 60;
	mass = 2;
})

starlit.item.seed.link('starlit_eco:lambent_pine_seed', {
	name = 'Lambent Pine Seed';
	tex = lib.image('starlit-eco-plant-seeds.png'):shift{hue=150, sat=-50, lum=80}:render();
	grow = {kind = 'tree', id = 'starlit_eco:lambent_pine'};
})

minetest.register_node('starlit_eco:lambent_pine_bulb', {
	description = 'Lambent Pine Bulb';
	drawtype = 'nodebox';
	connects_to = {'starlit_eco:lambent_pine_needles'};
	node_box = {
		type = 'connected';
		connect_top = {
			{-.1,  .5, -.1,
			  .1, .3,  .1};
		};
		fixed = {
			{-.2, -.2, -.2,
			  .2,  .3,  .2};
		};
	};
	light_source = 6;
	drop = {
		max_items = 3;
		items = {
			{rarity = 4, items = {'starlit_eco:lambent_pine_seed'}};
			{rarity = 3, items = {'starlit_eco:lambent_pine_berry'}};
			{rarity = 2, items = {'starlit_eco:lambent_pine_berry'}};
			{items = {'starlit_eco:lambent_pine_berry'}};
		};
	};
	groups = {plant=1, attached_node = 4};
	tiles = {
		'starlit-eco-tree-lambent-pine-bulb.png';
	};
	_starlit = woodProps{};
})

minetest.register_node('starlit_eco:lambent_pine_needles', {
	description = 'Lambent Pine Needles';
	groups = {plant = 1;};
	drop = '';
	tiles = {
		'starlit-eco-tree-lambent-pine-needles.png';
		'starlit-eco-tree-lambent-pine-needles.png';
	};
	_starlit = leafProps{};
});

regLog('starlit_eco:starblossom_log', {
	description = 'Starblossom Log';
	drawtype = 'normal';
	tiles = {
		'starlit-eco-tree-starblossom-trunk-top.png';
		'starlit-eco-tree-starblossom-trunk.png';
	};
	_starlit = woodProps{};
})

minetest.register_node('starlit_eco:starblossom_leaves', {
	description = 'Starblossom Leaves';
	groups = {plant = 1;};
	drop = '';
	tiles = {
		'starlit-eco-tree-starblossom-leaves.png';
		'starlit-eco-tree-starblossom-leaves.png';
	};
	_starlit = leafProps{};
});
minetest.register_node('starlit_eco:starblossom_leaves_shine', {
	description = 'Shining Starblossom Leaves';
	groups = {plant = 1;};
	drop = '';
	paramtype = 'light';
	light_source = 4;
	tiles = {
		'starlit-eco-tree-starblossom-leaves.png';
		'starlit-eco-tree-starblossom-leaves.png';
		'starlit-eco-tree-starblossom-leaves.png^starlit-eco-tree-starblossom-shine.png';
	};
	_starlit = leafProps{};
});

starlit.world.ecology.trees.meld {
	['starlit_eco:lambent_pine'] = {
		name = 'Lambent Pine';
		def = {
			axiom = 'TTB';
			rules_a = '[-[&Tf]Tf][+[^Tf]Tf]f';
			rules_c = '[-[&Tff]Tff][+[^Tff]Tff]f';
			rules_b = 'TCA[f]B';

			-- nodes
			trunk = 'starlit_eco:lambent_pine_log_live';
			leaves = 'starlit_eco:lambent_pine_needles';

			angle = 90;

			iterations = 7;
			random_level = 3;
			trunk_type = 'single';
			thin_branches = true;

			fruit = 'starlit_eco:lambent_pine_bulb';
			fruit_chance = 0;
		};
		decorate = {
			{ biomes = {'starlit:forest'};
				place_on = 'starlit:greengraze';
				fill_ratio = 0.004;
				y_min = -30, y_max = 500;
				seed = 0xe8190e;
			};
			{ biomes = {'starlit:steppe'};
				place_on = 'starlit:greengraze';
				fill_ratio = 0.002;
				y_min = -30, y_max = 500;
				seed = 0xe8190e;
			};
		};
	};

	['starlit_eco:starblossom'] = {
		def = {
			axiom = 'TTTTATTTT[&&B]' .. string.rep(string.rep('/', 4) .. '[&&B]', math.floor(360/40));
			rules_a = 'TTTa';
			rules_b = 'FF&B';

			trunk_type = 'double';
			trunk = 'starlit_eco:starblossom_log_live';
			leaves = 'starlit_eco:starblossom_leaves';
			leaves2 = 'starlit_eco:starblossom_leaves_shine';
			leaves2_chance = 20;

			angle = 10;
			iterations = 13;
			random_level = 5;
		};
		decorate = {
			{ biomes = {'starlit:forest'};
				place_on = 'starlit:greengraze';
				fill_ratio = 0.001;
				y_min = -20, y_max = 512;
			};
		};
	};
}

minetest.register_abm {
	label = "lambent pine fruiting";
	nodenames = {'starlit_eco:lambent_pine_needles'};
	neighbors = {'starlit_eco:lambent_pine_log_live'};
	chance = 40;
	interval = 80;
	catch_up = true;
	action = function(pos, node)
		local po = pos:offset(0,-1,0)
		if minetest.get_node(po).name == "air" then
			minetest.add_node(po, {name='starlit_eco:lambent_pine_bulb'})
		end
	end;
}