sorcery  tree.lua

File tree.lua from the latest check-in


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

		sorcery.lathe.register {
			input = t.node;
			tech = 'cut';
			output = 'default:stick 18';
			cost = 1;
		}

		for _, n in pairs(nodes) do
			if minetest.registered_items[n] then
				sorcery.lathe.register {
					input = n, tech = 'cut';
					output = 'xdecor:table 2';
					cost = 2;
				}
				if t.lathe then
					for tech, items in pairs(t.lathe) do
					for _, i        in pairs(items)   do
						if minetest.registered_items[i[1]] then
							sorcery.lathe.register {
								input = n;
								tech = tech;
								output = {
									name = i[1];
									count = i[2];
								};
								cost = i[3];
							}
						end
					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