sorcery  tree.lua at [6e7b450ec7]

File tree.lua artifact 95c6839cbe part of check-in 6e7b450ec7


sorcery.tree = {}

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

local measure_sap = function(v) -- v in half-pints
	return string.format('%s pints', v / 2)
end

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
					minetest.get_meta(pos):set_int('sorcery:trunk_node_role', 1)
				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 = measure_sap;
			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