sorcery  tree.lua at [ac4c799281]

File tree.lua artifact be6e1c803c part of check-in ac4c799281


sorcery.tree = {}

sorcery.tree.default = {
	sap = 'Sap';
	sapliq = 'sorcery:sap';
	sapling = 'default:sapling';
}
local log = sorcery.logger('tree')
local L = sorcery.lib

sorcery.register.trees.foreach('sorcery:treesetup', {}, function(id, t)
	-- generates sap and hooks handlers appropriately
	if t.node then
		local def = minetest.registered_nodes[t.node]
		local nextfn = def.on_place
		minetest.override_item(t.node, { on_place = function(stack, who, pointed, ...)
			if nextfn then nextfn(stack, who, pointed, ...) end
			if who ~= nil and pointed.type == 'node' then
				-- local pos = pointed.above
				-- local _, counts = minetest.find_nodes_in_area(
				-- 	vector.offset(pos, -1,-1,-1),
				-- 	vector.offset(pos,  1, 1, 1),
				-- 	t.leaves or 'group:leaves', false)
				-- if counts[next(counts)] > 0 then
				local n = minetest.get_node(pointed.above)
				n.param1 = 1
				minetest.swap_node(pointed.above, n)
				-- end
			end
		end })
	end

	if t.sap == false then return end
	if not t.sapliq then
		t.sapliq = string.format('sorcery:sap_%s', id)
		local sapdesc = t.sap or (t.desc .. ' Tree Sap')
		sorcery.liquid.register {
			id = t.sapliq;
			name = sapdesc;
			kind = 'sorcery:sap';
			color = t.sapcolor or t.color or {119,24,30};
			autogen = true;
			imgvariant = 'sparkle';
			measure = sorcery.liquid.units.pint;
			usetrough = t.sap ~= false;
		}
	end

end)

sorcery.tree.get = function(what)
	local name, pos
	-- FIXME exclude nonliving wood blocks, somehow
	if type(what) == 'string' then
		name = what
	else
		pos = what
		name = sorcery.lib.node.force(what).name
	end
	
	for tn, def in pairs(sorcery.data.trees) do
		if def.node == name then
			return {
				tree = tn;
				def = def;
				pos = pos;
				node = name;
			}
		end
	end

	if minetest.get_item_group(name,'tree') == 0 then return end

	-- no definition; return default tree
	return {
		tree = 'tree';
		def = sorcery.tree.default;
		pos = pos;
		node = name;
	}
end