sorcery  tools.lua at tip

File tools.lua from the latest check-in


local dagger_uses = 16
local log = sorcery.logger('tools')
minetest.register_tool("sorcery:dagger", {
	description = "Sacrificial Dagger",
	inventory_image = "sorcery_dagger.png",
	tool_capabilities = {
		full_punch_interval = 1.6,
		max_drop_level = 1,
		damage_groups = { fleshy = 2 },
	},
	sound = { breaks = "default_tool_breaks" },
	groups = { sorcery_sanctify = 1, sword = 1 },
	punch_attack_uses = 16
})

local dagger_usefn = function(selfharm)
	return function(stack,user,pointat)
		if (not minetest.is_player(pointat)) and
				selfharm == false then
			return nil
		end
		local trough = minetest.find_node_near(user:get_pos(), 2.5, 'group:sorcery_trough', true)
		if trough then
			local cnn = minetest.get_node(trough).name
			local ctr = minetest.registered_nodes[cnn]._sorcery
			if (not ctr) or not ctr.container then
				log.err('item',cnn,'is marked as a trough, but has no _sorcery.container table')
			else
				ctr = ctr.container
				if (ctr.has == 'sorcery:blood' or not ctr.has) and
					((not ctr.charge) or ctr.charge < (ctr.max or 1)) then
					trough = {
						pos = trough, ctr = ctr;
						def = sorcery.register.liquid.db['sorcery:blood'].containers[ctr.empty or cnn]
					}
				else
					trough = nil
				end
			end
		end

		local inv = user:get_inventory()
		local btl = ItemStack('vessels:glass_bottle')
		if not inv:contains_item('main', btl) then
			return nil
		end

		local blood
		local target
		local caps
		if selfharm then
			blood = ItemStack('sorcery:blood 1')
			target = user
			caps = {
				full_punch_interval = 1.0,
				damage_groups = { fleshy = 3 },
			} 
		else
			if not minetest.is_player(pointat) then
				return nil
			end
			target = pointat
			caps = stack:get_tool_capabilities()
			blood = ItemStack {
				name = 'sorcery:blood';
				count = math.floor(caps.damage_groups.fleshy * 1.5);
			}
		end
		local pos = target:get_pos()
		pos.y = pos.y + 1.5

		local wear = 65535 / dagger_uses
		stack:add_wear(wear)

		if not trough then
			inv:remove_item('main',btl)
			inv:add_item('main',blood)
		else
			local amt = selfharm and math.random(1,2) or 2
			sorcery.liquid.setctr(trough.pos, 'sorcery:blood', (trough.ctr.charge or 0) + (trough.def.res*amt), true)
		end

		target:punch(user, 1.0, caps, nil)
		sorcery.vfx.bloodburst(pos)

		if math.random(3 + sorcery.enchant.strength(stack,'sanctify') * 6) == 1 then
			-- we've used up the consecration
			local unholy = ItemStack("sorcery:dagger")
			unholy:set_wear(stack:get_wear())
			local ench = sorcery.enchant.get(stack)
			if #ench.spells > 0 then
				sorcery.enchant.set(unholy,ench)
			end
			return unholy
		else
			-- consecration is holding, return dagger as-is
			return stack
		end
	end
end


minetest.register_tool("sorcery:dagger_consecrated", {
	description = "Consecrated Dagger",
	inventory_image = "sorcery_dagger_consecrated.png^sorcery_dagger_glow.png",
	wield_image = "sorcery_dagger_consecrated.png",
	tool_capabilities = {
		full_punch_interval = 1.6,
		max_drop_level = 1,
		damage_groups = { fleshy = 6 },
	},
	sound = { breaks = "default_tool_breaks" },
	groups = { sorcery_sanctify = 2, sword = 1 },
	punch_attack_uses = 16,
	on_use = dagger_usefn(false),
	on_secondary_use = dagger_usefn(true),
})