-- 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 = "Spacetime Tuning",
	display_name = "时空调谐",
	type = {"chronomancy/other", 1},
	hide = true,
	points = 1,
	message = "@Source@ retunes the fabric of spacetime.",
	cooldown = 50,
	tactical = { PARADOX = 2 },
	no_npc_use = true,
	no_energy = true,
	no_unlearn_last = true,
	getAnomaly = function(self, t) return 6 - (self:getTalentLevelRaw(self.T_STATIC_HISTORY) or 0) end,
	getPower = function(self, t) return math.floor(self:getWil()/2) end,
	action = function(self, t)
		-- open dialog to get desired paradox
		local q = engine.dialogs.GetQuantity.new("Retuning the fabric of spacetime...",
		"What's your desired paradox level?", math.floor(self.paradox), nil, function(qty)

			-- get reduction amount and find duration
			local amount = qty - self.paradox
			local dur = math.floor(math.abs(qty-self.paradox)/t.getPower(self, t))

			-- set tuning effect
			if amount >= 0 then
				self:setEffect(self.EFF_SPACETIME_TUNING, dur, {power = t.getPower(self, t)})
			elseif amount < 0 then
				self:setEffect(self.EFF_SPACETIME_TUNING, dur, {power = - t.getPower(self, t)})
			end

		end)
		game:registerDialog(q)
		return true
	end,
	info = function(self, t)
		local chance = t.getAnomaly(self, t)
		return ([[调整紊乱值到理想水平，完成调谐可以知道你法术的失败、异常和走火几率。你在调谐的时候你会眩晕，每回合会调整相当于你一半意志属性的紊乱值高低。
		每次你提高紊乱值都有 %d%% 概率触发一次时空异常，使调谐终止。降低紊乱值不会触发异常。]]):
		format(chance)
	end,
}

newTalent{
	name = "Precognition",
	display_name = "预知未来",
	type = {"chronomancy/chronomancy",1},
	require = temporal_req1,
	points = 5,
	paradox = 5,
	cooldown = 10,
	no_npc_use = true,
	getDuration = function(self, t) return 4 + math.ceil((self:getTalentLevel(t) * 2)) end,
	action = function(self, t)
		if checkTimeline(self) == true then
			return
		end
		game:playSoundNear(self, "talents/spell_generic")
		self:setEffect(self.EFF_PRECOGNITION, t.getDuration(self, t), {})
		return true
	end,
	info = function(self, t)
		local duration = t.getDuration(self, t)
		return ([[你预知未来，允许你探索周围的事物，持续 %d 回合。当此技能结束时，你会回到原来释放该法术的位置。在此过程中的死亡是致命的。
		这个法术会使时间线分裂，所以其他同样能使时间线分裂的技能在此期间不能成功释放。]]):format(duration)
	end,
}

newTalent{
	name = "Spin Fate",
	display_name = "命运之网",
	type = {"chronomancy/chronomancy", 2},
	require = temporal_req2,
	mode = "passive",
	points = 5,
	getDuration = function(self, t) return 1 + math.ceil(self:getTalentLevel(t)) end,
	getSaveBonus = function(self, t) return self:getTalentLevel(t) end,
	do_spin_fate = function(self, t, type)
		local save_bonus = t.getSaveBonus(self, t)
		if self:hasEffect(self.EFF_PRECOGNITION) then
			save_bonus = save_bonus * 2
		end
		if not self:hasEffect(self.EFF_SPIN_FATE) then
			game:playSoundNear(self, "talents/spell_generic")
		end

		local mental_save, physical_save, spell_save, defense_bonus = 0
		if type == "mental" then mental_save = save_bonus end
		if type == "physical" then physical_save = save_bonus end
		if type == "spell" then spell_save = save_bonus end
		if type == "defense" then defense_bonus = save_bonus end
		print("Spin Fate", type, mental_save, physical_save, spell_save, defense_bonus)

		self:setEffect(self.EFF_SPIN_FATE, t.getDuration(self, t), {max_bonus = t.getSaveBonus(self, t) * 10, defense = defense_bonus, mental = mental_save, physical = physical_save, spell = spell_save})
		return true
	end,
	info = function(self, t)
		local save = t.getSaveBonus(self, t)
		local duration = t.getDuration(self, t)
		return ([[当未来逐渐呈现在你面前时，你学会如何对未来事件进行少量的修正。
		每当攻击者对你的防御或抵抗进行判定时，会额外增加 %d 点强度（每个属性最大累积增加 %d ）。
		此效果持续 %d 回合，但是每当此效果激活时会刷新持续时间。
		当预知未来激活时，防御和抵抗值会呈双倍增加。（但是最大累积值仍然相同）。]]):
		format(save, save * 10, duration)
	end,
}

newTalent{
	name = "Foresight",
	display_name = "深谋远虑",
	type = {"chronomancy/chronomancy",3},
	mode = "passive",
	require = temporal_req3,
	points = 5,
	getRadius = function(self, t) return 3 + math.floor(self:getTalentLevel(t) * 2) end,
	do_precog_foresight = function(self, t)
		self:magicMap(t.getRadius(self, t))
		self:setEffect(self.EFF_SENSE, 1, {
			range = t.getRadius(self, t),
			actor = 1,
			object = 1,
			trap = 1,
		})
	end,
	info = function(self, t)
		local radius = t.getRadius(self, t)
		return ([[在预知未来技能结束后（或用预知之门取消），你可以得到 %d 格半径范围的视野，显示周围的地形、敌人、物品和陷阱。]]):
		format(radius)
	end,
}

newTalent{
	name = "Moment of Prescience",
	display_name = "预知之门",
	type = {"chronomancy/chronomancy", 4},
	require = temporal_req4,
	points = 5,
	paradox = 10,
	cooldown = 24,
	getDuration = function(self, t) return 2 + math.floor(self:getTalentLevel(t)) end,
	getPercentage = function(self, t) return self:getTalentLevel(t) * 10 end,
	getDetection = function(self, t) return self:getTalentLevel(t) * 5 end,
	on_pre_use = function(self, t, silent) if not self:hasEffect(self.EFF_PRECOGNITION) then if not silent then game.logPlayer(self, "Precognition must be active to use this talent.") end return false end return true end,
	tactical = { BUFF = 4 },
	no_energy = true,
	no_npc_use = true,
	action = function(self, t)
		game:playSoundNear(self, "talents/spell_generic")
		-- clear the chronoworlds and the timed effect
		if game._chronoworlds then game._chronoworlds = nil end
		game.player.tmp[self.EFF_PRECOGNITION] = nil

		-- check for Spin Fate
		local eff = self:hasEffect(self.EFF_SPIN_FATE)
		local mental_power, physical_power, spell_power, accuracy = 0
		local percentage = t.getPercentage(self, t)/100
		if eff then
			mental_power = (eff.cur_mental or eff.mental) * percentage
			physical_power = (eff.cur_physical or eff.physical) * percentage
			spell_power = (eff.cur_spell or eff.spell) * percentage
			accuracy_power = (eff.cur_defense or eff.defense) * percentage
		end

		self:setEffect(self.EFF_PRESCIENCE, t.getDuration(self, t), {detect=t.getDetection(self, t), mental=mental_power, physical=physical_power, spell=spell_power, accuracy=accuracy_power})
		if self:knowTalent(self.T_FORESIGHT) then
			local t = self:getTalentFromId(self.T_FORESIGHT)
			t.do_precog_foresight(self, t)
		end
		return true
	end,
	info = function(self, t)
		local detect = t.getDetection(self, t)
		local duration = t.getDuration(self, t)
		local percent = t.getPercentage(self, t)
		return ([[当预知未来激活时，你可以使用预知之门取消它，直接从时间线上回到之前的状态。
		这么做会把你的意识完全融入时间，增加侦查潜行和隐形单位强度 %d 点，持续 %d 回合。
		当此技能激活时，你身上若存在命运之网则增加 %d%% 作为命中、攻击强度、法术强度或精神强度。
		此技能不需要施法时间。]]):
		format(detect, duration, percent)
	end,
}