-- an optional 'enchant' function can be defined, and will
-- be called when the enchantment is placed on the object
local allgroups = {
	'sword'; 'pick'; 'pickaxe';
	'sickle'; 'scythe'; 'shovel';
	'hoe'; 'helmet'; 'leggings';
	'chestplate'; 'boots';
} 
return {
	endure = { -- withstand more blows
		name = 'Endure';
		cost = 1;
		tone = {232,102,255};
		desc = 'durability magnified';
		affinity = 'counterpraxic';
		groups = allgroups;
		recipe = {
			{lens = 'convex',    gem = 'amethyst', dmg = 2};
			{lens = 'rectifier', gem = 'emerald',  dmg = 4};
			{lens = 'convex',    gem = 'emerald',  dmg = 2};
		};
		apply = function(stack,power,base)
			local caps = table.copy(stack:get_definition().tool_capabilities)
			for g,v in pairs(caps.groupcaps) do
				local unit = base.groupcaps[g].uses * 0.6
				caps.groupcaps[g].uses = v.uses + unit*power
			end
			stack:get_meta():set_tool_capabilities(caps)
			return stack
		end;
	};
	drain = {
		groups = {'sword'};
		cost = 4;
	}; -- health vampirism
	harvest = { -- kills or digging ore replenish durability
		name = 'Harvest';
		cost = 0; -- energy is only depleted when repair takes place
		tone = {255,84,187};
		affinity = 'syncretic';
		groups = {
			'pick'; 'pickaxe'; 'sword';
		};
		recipe = {
			{lens = 'amplifier', gem = 'ruby',     dmg = 5};
			{lens = 'concave',   gem = 'mese',     dmg = 1};
			{lens = 'concave',   gem = 'sapphire', dmg = 1};
		};
		desc = 'some damage is repaired when used to mine ore or kill an attacker';
		on_dig = function(ctx)
			local orepfx = "stone_with_" -- }:<
			-- local oredrop = ' lump'
			local dug = minetest.get_node(ctx.target.under)
			local barename = string.sub(dug.name, string.find(dug.name, ':') + 1)
			print('is ore? ',dug.name,barename)
			if minetest.get_item_group(dug.name, ore) ~= 0 or
			   string.sub(barename,1,string.len(orepfx)) == orepfx 
			then
				print('is ore!')
				ctx.tool:add_wear(-(sorcery.enchant.strength(ctx.tool,'harvest') * 2000))
				ctx.cost = 3
			end
		end;
	};
	conserve = { -- use less magical energy
		name = 'Conserve';
		tone = {84,255,144};
		cost = 0;
		desc = 'enchantments last longer before running out of power to sustain them.';
		groups = allgroups;
		affinity = 'syncretic';
		recipe = {
			{lens = 'rectifier', gem = 'mese',     dmg = 7};
			{lens = 'rectifier', gem = 'sapphire', dmg = 2};
			{lens = 'rectifier', gem = 'amethyst', dmg = 2};
		};
		-- implemented in sorcery/enchanter.lua:register_on_dig
	};
	dowse = { -- send up flare when valuable ores are nearby
		name = 'Dowse';
		tone = {241,251,113};
		cost = 1;
		desc = 'strike colored sparks when used to dig near valuable ore.';
		groups = {'pick','pickaxe'};
		affinity = 'cognic';
		recipe = {
			{lens = 'concave', gem = 'ruby',     dmg = 3};
			{lens = 'concave', gem = 'emerald',  dmg = 3};
			{lens = 'concave', gem = 'sapphire', dmg = 3};
		};
		on_dig = function(ctx)
			local range = 4*sorcery.enchant.strength(ctx.tool,'dowse')
			local colors = {
				['default:stone_with_gold'    ] = {255,234,182};
				['default:stone_with_mese'    ] = {231,255,151};
				['default:stone_with_diamond' ] = {180,253,255};
				['sorcery:stone_with_iridium' ] = {243,180,255};
				['sorcery:stone_with_tungsten'] = {119,234,196};
			}
			local search = {} for k in pairs(colors)
				do search[#search+1] = k end
			local nodes = minetest.find_nodes_in_area(
				vector.subtract(ctx.pos,range),
				vector.add(ctx.pos,range), search)
			for _,n in pairs(nodes) do
				-- we're going to use some ugly math tricks
				-- to avoid having to do a square root, since
				-- that's an expensive operation and we don't
				-- need that level of precision; we can
				-- approximate the distance without it
				local delta = vector.subtract(n,ctx.pos)
				local dstsq = (delta.x^2) + (delta.y^2) + (delta.z^2)
				if dstsq < range^2 then
					local dstfac = 1 - (dstsq / range^2)
					ctx.sparks[#ctx.sparks+1] = {
						color = sorcery.lib.color(colors[minetest.get_node(n).name]);
						count = 100 * dstfac;
					}
				end
			end
		end;
	};
	pierce = { -- faster mining speed
		name = 'Pierce';
		cost = 3;
		tone = {113,240,251};
		groups = {
			'pick';'pickaxe';'axe';'shovel';'sickle';
		};
		desc = 'rip through solid stone or wood like a hot knife through butter';
		recipe = {
			{lens = 'amplifier', gem = 'diamond',  dmg = 4};
			{lens = 'amplifier', gem = 'ruby',     dmg = 4};
			{lens = 'rectifier', gem = 'diamond',  dmg = 2};
		};
		affinity = 'praxic';
		apply = function(stack,power,base)
			local caps = table.copy(stack:get_definition().tool_capabilities)
			for g,v in pairs(caps.groupcaps) do
				for i,t in pairs(v.times) do
					local unit = base.groupcaps[g].times[i] * 0.15
					caps.groupcaps[g].times[i] = math.max(0.01, t - unit*power)
				end
			end
			stack:get_meta():set_tool_capabilities(caps)
			return stack
		end;
	};
	rend = { -- more damage / mine higher level blocks
		name = 'Rend';
		affinity = 'praxic';
		tone = {251,203,113};
		groups = {'sword';'pick';'pickaxe';};
		recipe = {
			{lens = 'convex',    gem = 'mese',    dmg = 3};
			{lens = 'amplifier', gem = 'emerald', dmg = 7};
			{lens = 'amplifier', gem = 'diamond', dmg = 7};
		};
		cost = 5;
		desc = 'cleave through sturdy ores and tear mortal flesh with fearsome ease';
		apply = function(stack,power,base)
			local caps = table.copy(stack:get_definition().tool_capabilities)
			for g,v in pairs(caps.groupcaps) do
				local unit = 2
				caps.groupcaps[g].maxlevel = caps[g].maxlevel + math.floor(unit*power)
			end
			stack:get_meta():set_tool_capabilities(caps)
			return stack
		end;
	};
}
