-- ToME - Tales of Middle-Earth
-- 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 = "Gesture of Pain",
	display_name = "痛苦手势",
	type = {"cursed/gestures", 1},
	mode = "sustained",
	require = cursed_cun_req1,
	points = 5,
	random_ego = "attack",
	tactical = { ATTACK = 2 },
	getBaseDamage = function(self, t)
		return self:combatTalentMindDamage(t, 0, 125)
	end,
	getSecondAttackChance = function(self, t)
		return 20
	end,
	preAttack = function(self, t, target)
		if self.hate < 0.1 then
			game.logPlayer(self, "You do not have enough hate to use Gesture of Pain.")
			return false
		end
		if self:getFreeHands() == 0 then
			game.logPlayer(self, "You do not have a free hand to use Gesture of Pain.")
			return false
		end

		return true
	end,
	attack = function(self, t, target)
		local freeHands = self:getFreeHands()
		local hit = false

		local mindpower = self:combatMindpower()
		local baseDamage = t.getBaseDamage(self, t)
		if self:checkHit(mindpower, target:combatMentalResist()) then
			local damage = baseDamage * rng.float(0.5, 1)
			self:project({type="hit", x=target.x,y=target.y}, target.x, target.y, DamageType.MIND, { dam=damage,alwaysHit=true,criticals=true,crossTierChance=100 })
			self:incHate(-0.1)
			game:playSoundNear(self, "actions/melee_hit_squish")
			hit = true
		else
			game.logSeen(self, "%s resists the Gesture of Pain.", target.name:capitalize())
			game:playSoundNear(self, "actions/melee_miss")
		end

		if not target.dead and freeHands > 1 and self.hate >= 0.1 and rng.chance(t.getSecondAttackChance(self, t)) then
			if self:checkHit(mindpower, target:combatMentalResist()) then
				local damage = baseDamage * rng.float(0.5, 1)
				self:project({type="hit", x=target.x,y=target.y}, target.x, target.y, DamageType.MIND, { dam=damage,alwaysHit=true,criticals=true,crossTierChance=100 })
				game:playSoundNear(self, "actions/melee_hit_squish")
				hit = true
				self:incHate(-0.1)
			else
				game.logSeen(self, "%s resists the Gesture of Pain.", target.name:capitalize())
				game:playSoundNear(self, "actions/melee_miss")
			end
		end

		if hit then
			game.level.map:particleEmitter(target.x, target.y, 1, "melee_attack", {color=colors.VIOLET})
		end

		return self:combatSpeed(), hit
	end,
	activate = function(self, t)
		return {}
	end,
	deactivate = function(self, t, p)
		return true
	end,
	info = function(self, t)
		local baseDamage = t.getBaseDamage(self, t)
		local secondAttackChance = t.getSecondAttackChance(self, t)
		return ([[用一种痛苦暗示代替攻击打入对方的心灵，造成 %0.1f ～ %0.1f 精神伤害。
		需要1只空手。第2只空手会增加 %d%% 概率造成二次打击。每次打击消耗0.1仇恨值。
		用跨层效果可以造成暴击。
		受精神强度影响，伤害有额外加成。]]):format(damDesc(self, DamageType.MIND, baseDamage * 0.5), damDesc(self, DamageType.MIND, baseDamage), secondAttackChance)
	end,
}

newTalent{
	name = "Gesture of Command",
	display_name = "命令手势",
	type = {"cursed/gestures", 2},
	require = cursed_cun_req2,
	mode = "passive",
	points = 5,
	getMindpowerChange = function(self, t, freeHands)
		freeHands = freeHands or self:getFreeHands()
		if freeHands == 0 then return 0 end

		local change = math.pow(self:getTalentLevel(t), 0.7) * 4
		if freeHands > 1 then change = change * 1.4 end
		return math.floor(change)
	end,
	info = function(self, t)
		local mindpowerChange1 = t.getMindpowerChange(self, t, 1)
		local mindpowerChange2 = t.getMindpowerChange(self, t, 2)
		return ([[通过手势来控制你的精神强度。
		1只手空闲时，增加 %d 点精神强度。
		2只手空闲时，增加 %d 点精神强度。]]):format(mindpowerChange1, mindpowerChange2)
	end,
}

newTalent{
	name = "Gesture of Power",
	display_name = "力量手势",
	type = {"cursed/gestures", 3},
	require = cursed_cun_req3,
	mode = "passive",
	points = 5,
	getMindCritChange = function(self, t, freeHands)
		freeHands = freeHands or self:getFreeHands()
		if freeHands == 0 then return 0 end

		local change = math.pow(self:getTalentLevel(t), 0.5) * 2
		if freeHands > 1 then change = change * 1.4 end
		return change
	end,
	info = function(self, t)
		local mindCritChange1 = t.getMindCritChange(self, t, 1)
		local mindCritChange2 = t.getMindCritChange(self, t, 2)
		return ([[通过1个手势增强你的精神攻击，增加精神攻击的暴击率。
		有1只空手，你可以增加 %0.1f%% 暴击概率。
		有2只空手，你可以增加 %0.1f%% 暴击概率。]]):format(mindCritChange1, mindCritChange2)
	end,
}

newTalent{
	name = "Gesture of Guarding",
	display_name = "守护手势",
	type = {"cursed/gestures", 4},
	require = cursed_cun_req4,
	mode = "sustained",
	cooldown = 10,
	points = 5,
	getDamageResistChange = function(self, t, distance, freeHands)
		freeHands = freeHands or self:getFreeHands()
		if freeHands == 0 then return 0 end

		local change = math.pow(self:getTalentLevel(t), 0.5) * 1.15
		if freeHands > 1 then change = change * 1.4 end
		return change * math.min(7, distance)
	end,
	getIncDamageChange = function(self, t, distance)
		local change = -(2 + math.pow(self:getTalentLevel(t), 0.5) * 0.8)
		return change * math.min(7, distance)
	end,
	on_damageReceived = function(self, t, type, dam, src)
		if src and src.x and src.y and (self.x ~= src.x or self.y ~= src.y) and self:hasLOS(src.x, src.y) then
			local distance = core.fov.distance(src.x, src.y, self.x, self.y)
		print("===dec by", t.getDamageResistChange(self, t, distance), distance)
			dam = dam * (100 - t.getDamageResistChange(self, t, distance)) / 100
		end
		return dam
	end,
	on_damageInflicted = function(self, t, type, dam, target)
		if target and target.x and target.y and (self.x ~= target.x or self.y ~= target.y) and self:hasLOS(target.x, target.y) then
			local distance = core.fov.distance(target.x, target.y, self.x, self.y)
		print("===inc by", t.getIncDamageChange(self, t, distance), distance)
			dam = dam * (100 + t.getIncDamageChange(self, t, distance)) / 100
		end
		return dam
	end,
	activate = function(self, t)
		return {}
	end,
	deactivate = function(self, t, p)
		return true
	end,
	info = function(self, t)
		local damageResistChange1 = t.getDamageResistChange(self, t, 1, 1)
		local damageResistChangeMax1 = t.getDamageResistChange(self, t, 1000, 1)
		local damageResistChange2 = t.getDamageResistChange(self, t, 1, 2)
		local damageResistChangeMax2 = t.getDamageResistChange(self, t, 1000, 2)
		local incDamageChange = t.getIncDamageChange(self, t, 1)
		local incDamageChangeMax = t.getIncDamageChange(self, t, 1000)
		return ([[当技能激活时，你可以挥动手来防御即将到来的攻击。攻击越远，伤害减少的越多，最远7格范围。
		有1只空手，每格距离减少 %0.1f%% 伤害（最大减少 %0.1f%%）。有2只空手，每格距离减少 %0.1f%% 伤害（最大减少 %0.1f%% ）。
		保护自己需要精神高度集中，并且每格距离减少 %0.1f%% 你所造成的伤害（最大减少 %0.1f%% ）。]]):format(damageResistChange1, damageResistChangeMax1, damageResistChange2, damageResistChangeMax2, -incDamageChange, -incDamageChangeMax)
	end,
}
