-- contains functions for determining information about
-- the nearest leyline and its currents

sorcery.ley = {}

-- leylines are invisible force-currents that rise up from the core of the earth, carrying magical energy upwards. they weaken as they get closer to the surface. each leyline also has between one and three 'affinities', which control how easily they can be wielded to perform particular sorts of magic. for instance, telestratic-affine leylines will charge wands enchanted with telestratic spells more quickly than leylines lacking this affinity.

sorcery.ley.estimate = function(pos)
	local affs = {
		'praxic'; 'counterpraxic'; 'cognic';
		'mandatic'; 'occlutic'; 'imperic';
		'syncretic'; 'entropic';
	};
	local forcemap = minetest.get_perlin(0xe9a01d, 3, 2, 150)
	local aff1map = minetest.get_perlin(0x10eb03, 3, 2, 300)
	local aff2map = minetest.get_perlin(0x491e12, 3, 2, 240)
	local txpos = { --- :( :( :( :(
		x = pos.x;
		y = pos.z;
		z = pos.y;
	}

	local normalize = function(map)
		local v = map:get_2d(txpos)
		return (v + 6) / 12 -- seriously??
	end

	local zfac = (txpos.z / -1024) + 1
	local force = math.min(1, normalize(forcemap) * zfac)
	local aff1 = affs[math.ceil(#affs * normalize(aff1map))] or 'fail'
	local aff2v, aff2 = math.ceil(normalize(aff2map) * (#affs * 2))
	if aff2v <= #affs then aff2 = affs[aff2v] end

	return {
		force = force;
		aff = { aff1, aff2 };
	}
end

minetest.register_chatcommand('leyline', {
	description = 'See details about local ley force';
	privs = { server = true };
	func = function(caller,params)
		local pos = minetest.get_player_by_name(caller):get_pos()
		local ley = sorcery.ley.estimate(pos)
		minetest.chat_send_player(caller, 'Leyline force ' .. tostring(ley.force) .. ' with affinities ' .. table.concat(ley.aff, ','))
	end;
})
