-- ToME - Tales of Maj'Eyal
-- Copyright (C) 2009, 2010, 2011 Nicolas Casalini
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Nicolas Casalini "DarkGod"
-- darkgod@te4.org

newTalent{
	name = "Arcane Combat",
	display_name = "奥术武器",
	type = {"technique/magical-combat", 1},
	mode = "sustained",
	points = 5,
	require = techs_req1,
	sustain_stamina = 20,
	no_energy = true,
	cooldown = 5,
	tactical = { BUFF = 2 },
	do_trigger = function(self, t, target)
		if self.x == target.x and self.y == target.y then return nil end

		if rng.percent(20 + self:getTalentLevel(t) * (1 + self:getDex(9, true))) then
			local spells = {}
			local fatigue = (100 + 2 * self:combatFatigue()) / 100
			local mana = self:getMana() - 1
			if self:knowTalent(self.T_FLAME) and mana > self:getTalentFromId(self.T_FLAME).mana * fatigue then spells[#spells+1] = self.T_FLAME end
			if self:knowTalent(self.T_LIGHTNING) and mana > self:getTalentFromId(self.T_LIGHTNING).mana * fatigue then spells[#spells+1] = self.T_LIGHTNING end
			if self:knowTalent(self.T_EARTHEN_MISSILES) and mana > self:getTalentFromId(self.T_EARTHEN_MISSILES).mana * fatigue then spells[#spells+1] = self.T_EARTHEN_MISSILES end
			local tid = rng.table(spells)
			if tid then
				local l = self:lineFOV(target.x, target.y)
				l:set_corner_block()
				local lx, ly, is_corner_blocked = l:step(true)
				local target_x, target_y = lx, ly
				-- Check for terrain and friendly actors
				while lx and ly and not is_corner_blocked and core.fov.distance(self.x, self.y, lx, ly) <= 10 do
					local actor = game.level.map(lx, ly, engine.Map.ACTOR)
					if actor and (self:reactionToward(actor) >= 0) then
						break
					elseif game.level.map:checkEntity(lx, ly, engine.Map.TERRAIN, "block_move") then
						target_x, target_y = lx, ly
						break
					end
					target_x, target_y = lx, ly
					lx, ly = l:step(true)
				end
				print("[ARCANE COMBAT] autocast ",self:getTalentFromId(tid).name)
				local old_cd = self:isTalentCoolingDown(self:getTalentFromId(tid))
				self:forceUseTalent(tid, {ignore_energy=true, force_target={x=target_x, y=target_y, __no_self=true}})
				-- Do not setup a cooldown
				if not old_cd then
					self.talents_cd[tid] = nil
				end
				self.changed = true
			end
		end
	end,
	activate = function(self, t)
		return {}
	end,
	deactivate = function(self, t, p)
		return true
	end,
	info = function(self, t)
		return ([[允许你使用武器释放法术伤害，在你每次攻击时都有 %d%% 概率附加一次火焰、闪电或石系法术伤害。
		通过这种方式触发法术不会造成对应技能进入CD状态，但是只有在对应技能CD好时才可以触发法术伤害。
		受敏捷影响，触发概率有额外加成。]]):
		format(20 + self:getTalentLevel(t) * (1 + self:getDex(9, true)))
	end,
}

newTalent{
	name = "Arcane Dexterity",
	display_name = "奥术灵巧",
	type = {"technique/magical-combat", 2},
	mode = "passive",
	points = 5,
	require = techs_req2,
	info = function(self, t)
		return ([[你额外增加相当于你 %d%% 敏捷的法术强度。]]):
		format(15 + self:getTalentLevel(t) * 5)
	end,
}

newTalent{
	name = "Arcane Feed",
	display_name = "奥术充能",
	type = {"technique/magical-combat", 3},
	mode = "sustained",
	points = 5,
	cooldown = 5,
	sustain_stamina = 40,
	require = techs_req3,
	range = 10,
	tactical = { BUFF = 2 },
	activate = function(self, t)
		local power = self:getTalentLevel(t) / 14
		return {
			regen = self:addTemporaryValue("mana_regen", power),
		}
	end,
	deactivate = function(self, t, p)
		self:removeTemporaryValue("mana_regen", p.regen)
		return true
	end,
	info = function(self, t)
		return ([[当技能激活时，每回合恢复 %0.2f 法力值。]]):format(self:getTalentLevel(t) / 14)
	end,
}

newTalent{
	name = "Arcane Destruction",
	display_name = "奥术毁灭",
	type = {"technique/magical-combat", 4},
	mode = "passive",
	points = 5,
	require = techs_req4,
	info = function(self, t)
		return ([[在你的武器上附上法术能量，增加 %d 点物理强度。
		受你的魔法和敏捷影响，按比例加成物理强度。]]):
		format(self:combatSpellpower() * self:getTalentLevel(Talents.T_ARCANE_DESTRUCTION) / 9)
	end,
}

