sorcery  tree.lua at [a66c0f02c7]

File tree.lua artifact e98976a563 part of check-in a66c0f02c7


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 nodes = t.node
		if type(nodes) == 'string' then nodes = {nodes} end
		for _, node in pairs(nodes) do
			local def = minetest.registered_nodes[node]
			if def then
				local nextfn = def.on_place
				minetest.override_item(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
		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;
			glow = t.sapglow;
		}
	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 L.tbl.strmatch(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