-- 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

local Stats = require "engine.interface.ActorStats"

newTalent{
	name = "Stalk",
	display_name = "追踪",
	type = {"cursed/endless-hunt", 1},
	mode = "sustained",
	require = cursed_wil_req1,
	points = 5,
	cooldown = 0,
	no_energy = true,
	tactical = { BUFF = 5 },
	activate = function(self, t)
		return {
			hit = false, -- was any target hit this turn
			hit_target = nil, -- which single target was hit this turn
			hit_turns = 0, -- how many turns has the target been hit
		}
	end,
	deactivate = function(self, t, p)
		return true
	end,
	getDuration = function(self, t)
		return 40
	end,
	getHitHateChange = function(self, t, bonus)Patbonus = math.min(bonus, 3)
		return 0.05 * bonus
	end,
	getAttackChange = function(self, t, bonus)
		return math.floor(self:combatTalentStatDamage(t, "wil", 10, 30) * math.sqrt(bonus))
	end,
	getStalkedDamageMultiplier = function(self, t, bonus)
		return 1 + self:combatTalentIntervalDamage(t, "str", 0.1, 0.35, 0.4) * bonus / 3
	end,
	doStalk = function(self, t, target)
		if self:hasEffect(self.EFF_STALKER) or target:hasEffect(self.EFF_STALKED) then
			-- doesn't support multiple stalkers, stalkees
			game.logPlayer(self, "You are having trouble focusing on your prey!")
			return false
		end

		local duration = t.getDuration(self, t)
		self:setEffect(self.EFF_STALKER, duration, { target=target, bonus = 1 })
		target:setEffect(self.EFF_STALKED, duration, { source=self })

		game.level.map:particleEmitter(target.x, target.y, 1, "stalked_start")

		return true
	end,
	on_targetDied = function(self, t, target)
		self:removeEffect(self.EFF_STALKER)
		target:removeEffect(self.EFF_STALKED)

		-- prevent stalk targeting this turn
		local stalk = self:isTalentActive(self.T_STALK)
		if stalk then
			stalk.hit = false
			stalk.hit_target = nil
			stalk.hit_turns = 0
		end
	end,
	info = function(self, t)
		local duration = t.getDuration(self, t)
		return ([[当你连续两回合持续近战攻击同一个目标时，你将憎恨目标并追踪目标，效果持续 %d 回合或直到目标死亡。你每回合攻击追踪目标都将获得可叠加增益效果,该效果在你不攻击会减少1重效果。
		1重增益: +%d 攻击, +%d%% 近战伤害,当目标被击中时，每回合增加 +%0.3f 仇恨值。
		2重增益: +%d 攻击, +%d%% 近战伤害,当目标被击中时，每回合增加 +%0.3f 仇恨值。
		3重增益: +%d 攻击, +%d%% 近战伤害,当目标被击中时，每回合增加 +%0.3f 仇恨值。
		受意志影响，攻击和伤害有额外加成。]]):format(duration,
		t.getAttackChange(self, t, 1), t.getStalkedDamageMultiplier(self, t, 1) * 100 - 100, t.getHitHateChange(self, t, 1),
		t.getAttackChange(self, t, 2), t.getStalkedDamageMultiplier(self, t, 2) * 100 - 100, t.getHitHateChange(self, t, 2),
		t.getAttackChange(self, t, 3), t.getStalkedDamageMultiplier(self, t, 3) * 100 - 100, t.getHitHateChange(self, t, 3))
	end,
}

newTalent{
	name = "Beckon",
	display_name = "引诱思维",
	type = {"cursed/endless-hunt", 2},
	require = cursed_wil_req2,
	points = 5,
	cooldown = 10,
	hate = 0.2,
	tactical = { DISABLE = 2 },
	range = 10,
	getDuration = function(self, t)
		return math.min(20, math.floor(5 + self:getTalentLevel(t) * 2))
	end,
	getChance = function(self, t)
		return math.floor(42 + (math.sqrt(self:getTalentLevel(t)) - 1) * 20)
	end,
	getSpellpowerChange = function(self, t)
		return -self:combatTalentStatDamage(t, "wil", 8, 33)
	end,
	getMindpowerChange = function(self, t)
		return -self:combatTalentStatDamage(t, "wil", 8, 33)
	end,
	action = function(self, t)
		local range = self:getTalentRange(t)

		local tg = {type="hit", pass_terrain=true, range=range}
		local x, y, target = self:getTarget(tg)
		if not x or not y or not target then return nil end
		if core.fov.distance(self.x, self.y, x, y) > range then return nil end

		local duration = t.getDuration(self, t)
		local chance = t.getChance(self, t)
		local spellpowerChange = t.getSpellpowerChange(self, t)
		local mindpowerChange = t.getMindpowerChange(self, t)
		target:setEffect(target.EFF_BECKONED, duration, { source=self, range=range, chance=chance, spellpowerChange=spellpowerChange, mindpowerChange=mindpowerChange })

		return true
	end,
	info = function(self, t)
		local duration = t.getDuration(self, t)
		local chance = t.getChance(self, t)
		local spellpowerChange = t.getSpellpowerChange(self, t)
		local mindpowerChange = t.getMindpowerChange(self, t)
		return ([[捕猎者和猎物之间的联系能让你给予目标思想暗示,引诱他们更靠近你。
		在 %d 回合内，目标会试图接近你,甚至推开路径上的其他单位。每回合有 %d%% 的几率，它们会取消原有动作并直接向你走去。此效果会减少目标注意力,在它们到达你所在位置之前，降低其 %d 点法术强度和精神强度。
		受意志影响，法术和精神强度的降低效果有额外加成。]]):format(duration, chance, -spellpowerChange)
	end,
}

newTalent{
	name = "Harass Prey",
	display_name = "痛苦折磨",
	type = {"cursed/endless-hunt", 3},
	require = cursed_wil_req3,
	points = 5,
	random_ego = "attack",
	cooldown = 6,
	hate = 0.5,
	tactical = { ATTACK = { PHYSICAL = 3 } },
	getCooldownDuration = function(self, t)
		return 3 + math.floor(self:getTalentLevel(t) * 0.75)
	end,
	getDamageMultiplier = function(self, t, hate)
		return getHateMultiplier(self, 0.35, 0.67, true, hate)
	end,
	getTargetDamageChange = function(self, t)
		return -self:combatTalentStatDamage(t, "wil", 0.7, 0.9)
	end,
	getDuration = function(self, t)
		return 2
	end,
	on_pre_use = function(self, t)
		local eff = self:hasEffect(self.EFF_STALKER)
		return eff and not eff.target.dead and core.fov.distance(self.x, self.y, eff.target.x, eff.target.y) <= 1
	end,
	action = function(self, t)
		local damageMultipler = t.getDamageMultiplier(self, t)
		local cooldownDuration = t.getCooldownDuration(self, t)
		local targetDamageChange = t.getTargetDamageChange(self, t)
		local duration = t.getDuration(self, t)
		local effStalker = self:hasEffect(self.EFF_STALKER)
		local target = effStalker.target
		if not target or target.dead then return nil end

		target:setEffect(target.EFF_HARASSED, duration, { source=self, damageChange=targetDamageChange })

		for i = 1, 2 do
			if not target.dead and self:attackTarget(target, nil, damageMultipler, true) then
				-- remove effects
				local tids = {}
				for tid, lev in pairs(target.talents) do
					local t = target:getTalentFromId(tid)
					if not target.talents_cd[tid] and t.mode == "activated" and not t.innate then tids[#tids+1] = t end
				end

				local t = rng.tableRemove(tids)
				if not t then break end
				target.talents_cd[t.id] = rng.range(3, 5)
				game.logSeen(target, "%s's %s is disrupted!", target.name:capitalize(), t.name)
			end
		end

		return true
	end,
	info = function(self, t)
		local damageMultipler = t.getDamageMultiplier(self, t)
		local cooldownDuration = t.getCooldownDuration(self, t)
		local targetDamageChange = t.getTargetDamageChange(self, t)
		local duration = t.getDuration(self, t)
		return ([[用两次快速的攻击折磨你追踪的目标,每次攻击造成 %d%% （0仇恨）～ %d%% （10+仇恨）的伤害。并且每次攻击都将中断目标某项技能或符文，持续 %d 回合。目标会因为你的攻击而气馁,它的伤害降低 %d%% ，持续 %d 回合。
		受意志影响，伤害降低有额外加成。
		当装备被诅咒的武器时（+2.5仇恨值），仇恨技能效果有额外加成。]]):format(t.getDamageMultiplier(self, t, 0) * 100, t.getDamageMultiplier(self, t, 10) * 100, cooldownDuration, -targetDamageChange * 100, duration)
	end,
}

newTalent{
	name = "Surge",
	display_name = "杀意涌动",
	type = {"cursed/endless-hunt", 4},
	mode = "sustained",
	require = cursed_wil_req4,
	points = 5,
	cooldown = 10,
	no_energy = true,
	getMovementSpeedChange = function(self, t)
		return self:combatTalentStatDamage(t, "wil", 0.1, 1.1)
	end,
	getDefenseChange = function(self, t, hasDualweapon)
		if hasDualweapon or self:hasDualWeapon() then return self:combatTalentStatDamage(t, "wil", 4, 40) end
		return 0
	end,
	preUseTalent = function(self, t)
		-- prevent AI's from activating more than 1 talent
		if self ~= game.player and (self:isTalentActive(self.T_CLEAVE) or self:isTalentActive(self.T_REPEL)) then return false end
		return true
	end,
	activate = function(self, t)
		-- deactivate other talents and place on cooldown
		if self:isTalentActive(self.T_CLEAVE) then
			self:useTalent(self.T_CLEAVE)
		elseif self:knowTalent(self.T_CLEAVE) then
			local tCleave = self:getTalentFromId(self.T_CLEAVE)
			self.talents_cd[self.T_CLEAVE] = tCleave.cooldown
		end

		if self:isTalentActive(self.T_REPEL) then
			self:useTalent(self.T_REPEL)
		elseif self:knowTalent(self.T_REPEL) then
			local tRepel = self:getTalentFromId(self.T_REPEL)
			self.talents_cd[self.T_REPEL] = tRepel.cooldown
		end

		local movementSpeedChange = t.getMovementSpeedChange(self, t)
		return {
			moveId = self:addTemporaryValue("movement_speed", movementSpeedChange),
			luckId = self:addTemporaryValue("inc_stats", { [Stats.STAT_LCK] = -3 })
		}
	end,
	deactivate = function(self, t, p)
		if p.moveId then self:removeTemporaryValue("movement_speed", p.moveId) end
		if p.luckId then self:removeTemporaryValue("inc_stats", p.luckId) end

		return true
	end,
	info = function(self, t)
		local movementSpeedChange = t.getMovementSpeedChange(self, t)
		local defenseChange = t.getDefenseChange(self, t, true)
		return ([[让杀意激发你敏捷的身手,提高你 %d%% 移动速度.不顾一切的移动会带给你厄运(-3幸运)。
		分裂攻击,杀意涌动和无所畏惧不能同时开启,并且激活其中一个也会使另外两个进入冷却。
		你的移动速度与双武器提供的完美平衡，使闪避增加 %d 点。
		受意志影响，移动速度和防御有额外加成。]]):format(movementSpeedChange * 100, defenseChange)
	end,
}
