-- 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 function combatTalentDamage(self, t, min, max)
	return self:combatTalentSpellDamage(t, min, max, (self.level + self:getWil()) * 1.2)
end

newTalent{
	name = "Unnatural Body",
	display_name = "诅咒之体",
	type = {"cursed/cursed-form", 1},
	mode = "passive",
	require = cursed_wil_req1,
	points = 5,
	no_unlearn_last = true,
	on_learn = function(self, t)
		return true
	end,
	on_unlearn = function(self, t)
		return true
	end,
	getHealPerKill = function(self, t)
		return combatTalentDamage(self, t, 15, 50)
	end,
	getMaxUnnaturalBodyHeal = function(self, t)
		return t.getHealPerKill(self, t) * 2
	end,
	getRegenRate = function(self, t)
		return 3 + math.sqrt(self:getTalentLevel(t) * 2) * math.min(1000, self.max_life) * 0.006
	end,
	getResist = function(self, t)
		return -18 + (self:getTalentLevel(t) * 3) + (18 * getHateMultiplier(self, 0, 1))
	end,
	getCurseHealingFactorChange = function(self, t)
		return 0.03
	end,
	updateHealingFactor = function(self, t, armorCount)
		-- initialize
		if self.unnatural_body_healing_factor == nil then
			self.unnatural_body_healing_factor = -0.5
			self.healing_factor = (self.healing_factor or 1) + self.unnatural_body_healing_factor
		end

		-- equipped items changed
		local oldHealingFactor = self.unnatural_body_healing_factor

		if not armorCount then
			local tDefilingTouch = self:getTalentFromId(self.T_DEFILING_TOUCH)
			armorCount = 0
			for id, inven in pairs(self.inven) do
				if self.inven_def[id].is_worn then
					for i, item in ipairs(inven) do
						if item.curse and tDefilingTouch.canCurseItem(self, tDefilingTouch, item) and item.type == "armor" then armorCount = armorCount + 1 end
					end
				end
			end
		end
		local newHealingFactor = -0.5 + t.getCurseHealingFactorChange(self, t) * armorCount

		if self.unnatural_body_healing_factor ~= newHealingFactor then
			self.healing_factor = (self.healing_factor or 1) + newHealingFactor - self.unnatural_body_healing_factor
			self.unnatural_body_healing_factor = newHealingFactor
			self.changed = true
		end
	end,
	do_regenLife  = function(self, t)
		-- initialize
		if self.unnatural_body_healing_factor == nil then
			t.updateHealingFactor(self, t)
		end

		-- heal
		local maxHeal = self.unnatural_body_heal or 0
		if maxHeal > 0 then
			local heal = math.min(t.getRegenRate(self, t), maxHeal)
			local temp = self.healing_factor
			self.healing_factor = 1
			self:heal(heal)
			self.healing_factor = temp

			self.unnatural_body_heal = math.max(0, (self.unnatural_body_heal or 0) - heal)
		end

		-- update resists as well
		local oldResist = self.unnatural_body_resist or 0
		local newResist = t.getResist(self, t)
		self.resists.all = (self.resists.all or 0) - oldResist + newResist
		self.unnatural_body_resist = newResist
	end,
	on_kill = function(self, t, target)
		if target and target.max_life then
			heal = math.min(t.getHealPerKill(self, t), target.max_life)
			if heal > 0 then
				self.unnatural_body_heal = math.min(self.life, (self.unnatural_body_heal or 0) + heal)
				self.unnatural_body_heal = math.min(self.unnatural_body_heal, t.getMaxUnnaturalBodyHeal(self, t))
			end
		end
	end,
	info = function(self, t)
		-- initialize
		if self.unnatural_body_healing_factor == nil then
			t.updateHealingFactor(self, t)
		end

		local healPerKill = t.getHealPerKill(self, t)
		local maxUnnaturalBodyHeal = t.getMaxUnnaturalBodyHeal(self, t)
		local regenRate = t.getRegenRate(self, t)
		local healingFactor = self.unnatural_body_healing_factor or -0.5
		local resist = -18 + (self:getTalentLevel(t) * 3)
		local curseHealingFactorChange = t.getCurseHealingFactorChange(self, t)

		local modifier1, modifier2 = "more", "more"
		if resist > 0 then modifier1 = "less" end
		if resist + 18 > 0 then modifier2 = "less" end
		return ([[你的身体由于憎恨变的更加强大。
		你每次击杀目标都能获得 %d 点总生命回复（最大值 %d ），每回合回复 %0.1f 点生命值。
		这个回复效果无法被减少，但是其他方式的治疗减少 %d%% 效果。
		同时你还会增加 %d%% %s （0仇恨）～ %d%% %s （10+仇恨）所受伤害。
		受意志影响，生命回复有额外加成；并且每穿戴一件诅咒的护甲，你增加 %d%% 治疗回复效果。]]):format(healPerKill, maxUnnaturalBodyHeal, regenRate, -healingFactor * 100, math.abs(resist), modifier1, math.abs(resist + 18), modifier2, curseHealingFactorChange * 100)
	end,
}

newTalent{
	name = "Seethe",
	display_name = "狂热沸腾",
	type = {"cursed/cursed-form", 2},
	require = cursed_wil_req2,
	mode = "passive",
	points = 5,
	getHateLossMinHate = function(self, t)
		return math.sqrt(self:getTalentLevel(t)) * 1.3
	end,
	getHateGainMaxHate = function(self, t)
		return math.max(0, self:getTalentLevel(t) * 0.25)
	end,
	getHateGainChange = function(self, t)
		return 0.01
	end,
	on_learn = function(self, t)
	end,
	on_unlearn = function(self, t)
	end,
	info = function(self, t)
		local hateLossMinHate = t.getHateLossMinHate(self, t)
		local hateGainMaxHate = t.getHateGainMaxHate(self, t)
		return ([[你学会让憎恨在心底燃烧。
		当仇恨值低于 %0.2f 时，仇恨值将不会随时间减少。
		当仇恨值低于 %0.2f 时，仇恨值会缓慢提高。]]):format(hateLossMinHate, hateGainMaxHate)
	end
}

--newTalent{
--	name = "Seethe",
--	type = {"cursed/cursed-form", 2},
--	random_ego = "utility",
--	require = cursed_wil_req2,
--	points = 5,
--	cooldown = 400,
--	tactical = { BUFF = 1 },
--	getHateGain = function(self, t)
--		return (math.sqrt(self:getTalentLevel(t)) - 0.5) * 3
--	end,
--	action = function(self, t)
--		self:incHate(t.getHateGain(self, t))

--		game.level.map:particleEmitter(self.x, self.y, 5, "fireflash", {radius=1, tx=self.x, ty=self.y})
--		game:playSoundNear(self, "talents/fireflash")
--		return true
--	end,
--	info = function(self, t)
--		local hateGain = t.getHateGain(self, t)
--		return ([[Focus your rage gaining %0.1f hate.]]):format(hateGain)
--	end,
--}

newTalent{
	name = "Relentless",
	display_name = "鲜血渴望",
	type = {"cursed/cursed-form", 3},
	mode = "passive",
	require = cursed_wil_req3,
	points = 5,
	on_learn = function(self, t)
		self:attr("fear_immune", 0.15)
		self:attr("confusion_immune", 0.15)
		self:attr("knockback_immune", 0.15)
		self:attr("stun_immune", 0.15)
		return true
	end,
	on_unlearn = function(self, t)
		self:attr("fear_immune", -0.15)
		self:attr("confusion_immune", -0.15)
		self:attr("knockback_immune", -0.15)
		self:attr("stun_immune", -0.15)
		return true
	end,
	info = function(self, t)
		return ([[鲜血的渴望控制了你的行为，增加 %d%% 混乱、恐惧、击退和震慑免疫。]]):format(self:getTalentLevelRaw(t) * 15)
	end,
}

newTalent{
	name = "Stoic",
	display_name = "坚忍",
	type = {"cursed/cursed-form", 4},
	require = cursed_wil_req4,
	mode = "passive",
	points = 5,
	on_learn = function(self, t)
		self:incIncStat("con", 3)
		self.hate_per_kill = self.hate_per_kill - 0.03
	end,
	on_unlearn = function(self, t)
		self:incIncStat("con", -3)
		self.hate_per_kill = self.hate_per_kill + 0.03
	end,
	info = function(self, t)
		return ([[长期和诅咒对抗的经历使你逐渐掌握诅咒并将其变成你的一部分，如果这就是你选择的道路，则增加 %d 点体质和 %0.2f 仇恨值/杀死1个单位。]]):format(self:getTalentLevelRaw(t) * 3, 0.8 - 0.03 * self:getTalentLevelRaw(t))
	end,
}


