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

-- Generic requires for racial based on talent level
racial_req1 = {
	level = function(level) return 0 + (level-1)  end,
}
racial_req2 = {
	level = function(level) return 8 + (level-1)  end,
}
racial_req3 = {
	level = function(level) return 16 + (level-1)  end,
}
racial_req4 = {
	level = function(level) return 24 + (level-1)  end,
}

------------------------------------------------------------------
-- Highers' powers
------------------------------------------------------------------
newTalentType{ type="race/higher", name = "高等人类", generic = true, description = "角色所拥有的各种种族专长技能。" }

newTalent{
	short_name = "HIGHER_HEAL",
	name = "Gift of the Highborn",
	display_name = "高贵血统",
	type = {"race/higher", 1},
	require = racial_req1,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 5 end,
	tactical = { HEAL = 2 },
	on_pre_use = function(self, t) return not self:hasEffect(self.EFF_REGENERATION) end,
	action = function(self, t)
		self:setEffect(self.EFF_REGENERATION, 10, {power=5 + self:getWil() * 0.5})
		return true
	end,
	info = function(self, t)
		return ([[召唤高贵血统来使你的身体以 %d 点每回合的速率恢复，持续10回合。
		受意志的影响，生命恢复量有额外增益。]]):format(5 + self:getWil() * 0.5)
	end,
}

newTalent{
	name = "Overseer of Nations",
	display_name = "远见卓识",
	type = {"race/higher", 2},
	require = racial_req2,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self.sight = self.sight + 1
	end,
	on_unlearn = function(self, t)
		self.sight = self.sight - 1
	end,
	info = function(self, t)
		return ([[虽然高贵血统并不意味着统治他人（当然也没有特别的意愿去那样做），但是他们经常承担更高的义务。
		他们的本能使得他们比别人有更强的直觉。
		提高 %d 点最大视野范围。]]):format(self:getTalentLevelRaw(t))
	end,
}

newTalent{
	name = "Born into Magic",
	display_name = "魔法亲和",
	type = {"race/higher", 3},
	require = racial_req3,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self.combat_spellresist = self.combat_spellresist + 3
		self.resists[DamageType.ARCANE] = (self.resists[DamageType.ARCANE] or 0) + 5
	end,
	on_unlearn = function(self, t)
		self.combat_spellresist = self.combat_spellresist - 3
		self.resists[DamageType.ARCANE] = (self.resists[DamageType.ARCANE] or 0) - 5
	end,
	info = function(self, t)
		return ([[高等人类们最初是在Alure世纪前由红衣主神们创造的。他们在出生的核心里受到了魔法的熏陶。 
		提高 %d 点法术抵抗和 %d%% 奥术抵抗。]]):format(self:getTalentLevelRaw(t) * 3, self:getTalentLevelRaw(t) * 5)
	end,
}

newTalent{
	name = "Highborn's Bloom",
	display_name = "生命绽放",
	type = {"race/higher", 4},
	require = racial_req4,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 100 - self:getTalentLevel(t) * 5 end,
	tactical = { MANA = 2, VIM = 2, EQUILIBRIUM = 2, STAMINA = 2, POSITIVE = 2, NEGATIVE = 2, PARADOX = 2, PSI = 2 },
	getData = function(self, t)
		local base = self:combatTalentStatDamage(t, "con", 10, 90)
		return {
			stamina = base,
			mana = base * 1.8,
			equilibrium = -base * 1.5,
			vim = base,
			positive = base / 2,
			negative = base / 2,
			paradox = -base * 1.5,
			psi = base * 0.7,
		}
	end,
	action = function(self, t)
		local data = t.getData(self, t)
		for name, v in pairs(data) do
			local inc = "inc"..name:capitalize()
			if self[inc] then self[inc](self, v) end
		end
		return true
	end,
	info = function(self, t)
		local d = t.getData(self, t)
		return ([[激活你的内在魔力，将环境改造成更加适合你的类型。恢复 %d 点耐力， %d 点法力， %d 点自然失衡力， %d 点活力， %d 点正负能量， %d 点紊乱值和 %d 点超能力。
		受体质影响，此效果有额外增益。]]):format(d.stamina, d.mana, d.equilibrium, d.vim, d.positive, d.paradox, d.psi)
	end,
}

------------------------------------------------------------------
-- Shaloren's powers
------------------------------------------------------------------
newTalentType{ type="race/shalore", name = "永恒精灵", generic = true, is_spell=true, description = "角色所拥有的各种种族专长技能。" }
newTalent{
	short_name = "SHALOREN_SPEED",
	name = "Grace of the Eternals",
	display_name = "不朽的恩赐",
	type = {"race/shalore", 1},
	require = racial_req1,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 4 end,
	tactical = { DEFEND = 1 },
	action = function(self, t)
		local power = 0.1 + self:getDex() / 210
		self:setEffect(self.EFF_SPEED, 8, {power=power})
		return true
	end,
	info = function(self, t)
		return ([[召唤不朽的恩赐之力来增加你 %d%% 的基础速度，持续8回合。
		受敏捷的影响，速度会有额外的提升。]]):format((0.1 + self:getDex() / 210) * 100)
	end,
}

newTalent{
	name = "Magic of the Eternals",
	display_name = "不朽的魔法",
	type = {"race/shalore", 2},
	require = racial_req2,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self.combat_physcrit = self.combat_physcrit + 2
		self.combat_spellcrit = self.combat_spellcrit + 2
		self.combat_mindcrit = self.combat_mindcrit + 2
	end,
	on_unlearn = function(self, t)
		self.combat_physcrit = self.combat_physcrit - 2
		self.combat_spellcrit = self.combat_spellcrit - 2
		self.combat_mindcrit = self.combat_mindcrit - 2
	end,
	info = function(self, t)
		return ([[因为永恒精灵的自然魔法，现实发生了轻微的扭曲。
		提高 %d%%的暴击概率。]]):format(self:getTalentLevelRaw(t) * 2)
	end,
}

newTalent{
	name = "Secrets of the Eternals",
	display_name = "不朽的秘密",
	type = {"race/shalore", 3},
	require = racial_req3,
	points = 5,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 3 end,
	mode = "sustained",
	no_energy = true,
	activate = function(self, t)
		self.invis_on_hit_disable = self.invis_on_hit_disable or {}
		game:playSoundNear(self, "talents/spell_generic2")
		local ret = {
			invis = self:addTemporaryValue("invis_on_hit", self:getTalentLevelRaw(t) * 5),
			power = self:addTemporaryValue("invis_on_hit_power", 5 + self:getMag(20, true)),
			talent = self:addTemporaryValue("invis_on_hit_disable", {[t.id]=1}),
		}
		return ret
	end,
	deactivate = function(self, t, p)
		self:removeTemporaryValue("invis_on_hit", p.invis)
		self:removeTemporaryValue("invis_on_hit_power", p.power)
		self:removeTemporaryValue("invis_on_hit_disable", p.talent)
		return true
	end,
	info = function(self, t)
		return ([[作为Eyal仅存的古老种族，永恒精灵在漫长的岁月里学习到如何用他们与生俱来的精神魔法保护自己。
当受到至少15%%总生命值的伤害时有 %d%% 的概率使自身进入隐形状态（ %d 点隐形等级）5回合。]]):
		format(self:getTalentLevelRaw(t) * 5, 5 + self:getMag(20, true))
	end,
}

newTalent{
	name = "Timeless",
	display_name = "超越永恒",
	type = {"race/shalore", 4},
	require = racial_req4,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 3 end,
	tactical = {
		BUFF = function(self, t, target)
			local nb = 0
			for eff_id, p in pairs(self.tmp) do
				local e = self.tempeffect_def[eff_id]
				if e.status == "beneficial" then nb = nb + 1 end
			end
			return nb
		end,
		CURE = function(self, t, target)
			local nb = 0
			for eff_id, p in pairs(self.tmp) do
				local e = self.tempeffect_def[eff_id]
				if e.status == "detrimental" then nb = nb + 1 end
			end
			return nb
		end,
	},
	action = function(self, t)
		local target = self
		local todel = {}
		for eff_id, p in pairs(target.tmp) do
			local e = target.tempeffect_def[eff_id]
			if e.type ~= "other" then
				if e.status == "beneficial" then
					p.dur = p.dur + self:getTalentLevelRaw(t)
				elseif e.status == "detrimental" then
					p.dur = p.dur - self:getTalentLevelRaw(t) * 2
					if p.dur <= 0 then todel[#todel+1] = eff end
				end
			end
		end
		while #todel > 0 do
			target:removeEffect(table.remove(todel))
		end

		game:playSoundNear(self, "talents/spell_generic")
		return true
	end,
	info = function(self, t)
		return ([[世界在不断的变老，而你似乎永恒不变。对于你来说，时间是不同寻常的。
		减少 %d 回合负面状态的持续时间并增加 %d 回合增益状态的持续时间。]]):
		format(self:getTalentLevelRaw(t) * 2, self:getTalentLevelRaw(t))
	end,
}

------------------------------------------------------------------
-- Thaloren's powers
------------------------------------------------------------------
newTalentType{ type="race/thalore", name = "自然精灵", generic = true, description = "角色所拥有的各种种族专长技能。" }
newTalent{
	short_name = "THALOREN_WRATH",
	name = "Wrath of the Woods",
	display_name = "森林之怒",
	type = {"race/thalore", 1},
	require = racial_req1,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 5 end,
	tactical = { ATTACK = 1, DEFEND = 1 },
	action = function(self, t)
		self:setEffect(self.EFF_ETERNAL_WRATH, 5, {power=7 + self:getWil(10, true)})
		return true
	end,
	info = function(self, t)
		return ([[召唤远古森林之力，提高 %d%% 所有伤害并减少 %d%% 所承受伤害5回合。
		受意志影响,此效果有额外加成。]]):format(7 + self:getWil(10, true), 7 + self:getWil(10, true))
	end,
}

newTalent{
	name = "Unshackled",
	display_name = "潜能爆发",
	type = {"race/thalore", 2},
	require = racial_req2,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self.combat_physresist = self.combat_physresist + 5
		self.combat_mentalresist = self.combat_mentalresist + 5
	end,
	on_unlearn = function(self, t)
		self.combat_physresist = self.combat_physresist - 5
		self.combat_mentalresist = self.combat_mentalresist - 5
	end,
	info = function(self, t)
		return ([[自然精灵一度自由的生活在他们热爱的森林，从不关心外界的事物。
		提高物理和精神抵抗 %d 点。]]):format(self:getTalentLevelRaw(t) * 5)
	end,
}

newTalent{
	name = "Guardian of the Wood",
	display_name = "森林守护",
	type = {"race/thalore", 3},
	require = racial_req3,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self:attr("disease_immune", 0.12)
		self.resists[DamageType.BLIGHT] = (self.resists[DamageType.BLIGHT] or 0) + 4
	end,
	on_unlearn = function(self, t)
		self:attr("disease_immune", -0.12)
		self.resists[DamageType.BLIGHT] = (self.resists[DamageType.BLIGHT] or 0) - 4
	end,
	info = function(self, t)
		return ([[你是森林的一部分，它保护你免受侵蚀。 
		提高 %d%% 疾病抵抗和 %d%%枯萎抵抗。]]):format(self:getTalentLevel(t) * 12, self:getTalentLevel(t) * 4)
	end,
}

newTalent{
	name = "Nature's Pride",
	display_name = "自然的骄傲",
	type = {"race/thalore", 4},
	require = racial_req4,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 3 end,
	tactical = { ATTACK = { PHYSICAL = 2 }, DISABLE = { stun = 1, knockback = 1 } },
	range = 4,
	action = function(self, t)
		local tg = {type="bolt", nowarning=true, range=self:getTalentRange(t), nolock=true, talent=t}
		local tx, ty, target = self:getTarget(tg)
		if not tx or not ty then return nil end
		local _ _, tx, ty = self:canProject(tg, tx, ty)
		target = game.level.map(tx, ty, Map.ACTOR)
		if target == self then target = nil end

		-- Find space
		for i = 1, 2 do
			local x, y = util.findFreeGrid(tx, ty, 5, true, {[Map.ACTOR]=true})
			if not x then
				game.logPlayer(self, "Not enough space to summon!")
				return
			end

			local NPC = require "mod.class.NPC"
			local m = NPC.new{
				type = "immovable", subtype = "plants",
				display = "#",
				name = "treant", color=colors.GREEN,
				resolvers.nice_tile{image="invis.png", add_mos = {{image="npc/immovable_plants_treant.png", display_h=2, display_y=-1}}},
				desc = "A very strong near-sentient tree.",

				body = { INVEN = 10, MAINHAND=1, OFFHAND=1, BODY=1 },

				rank = 3,
				life_rating = 13,
				max_life = resolvers.rngavg(50,80),
				infravision = 10,

				autolevel = "none",
				ai = "summoned", ai_real = "tactical", ai_state = { talent_in=2, },
				stats = {str=0, dex=0, con=0, cun=0, wil=0, mag=0},
				combat = { dam=resolvers.levelup(resolvers.rngavg(15,25), 1, 1.3), atk=resolvers.levelup(resolvers.rngavg(15,25), 1, 1.3), dammod={str=1.1} },
				inc_stats = { str=25 + self:getWil() * self:getTalentLevel(t) / 5, dex=18, con=10 + self:getTalentLevel(t) * 2, },

				level_range = {1, nil}, exp_worth = 0,
				silent_levelup = true,

				combat_armor = 13, combat_def = 8,
				resolvers.talents{ [Talents.T_STUN]=3, [Talents.T_KNOCKBACK]=2, },

				faction = self.faction,
				summoner = self, summoner_gain_exp=true,
				summon_time = 6,
				ai_target = {actor=target}
			}
			setupSummon(self, m, x, y)
		end

		game:playSoundNear(self, "talents/spell_generic")
		return true
	end,
	info = function(self, t)
		return ([[自然与你同在，你可以时刻感受到森林的召唤。
		召唤2个精英树人，持续6回合。
		受你的意志影响，他们的力量会有额外增加。]]):format()
	end,
}

------------------------------------------------------------------
-- Dwarves' powers
------------------------------------------------------------------
newTalentType{ type="race/dwarf", name = "矮人", generic = true, description = "角色所拥有的各种种族专长技能。" }
newTalent{
	short_name = "DWARF_RESILIENCE",
	name = "Resilience of the Dwarves",
	display_name = "钢筋铁骨",
	type = {"race/dwarf", 1},
	require = racial_req1,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 5 end,
	tactical = { DEFEND = 2 },
	action = function(self, t)
		self:setEffect(self.EFF_DWARVEN_RESILIENCE, 8, {
			armor=5 + self:getCon() / 5,
			physical=10 + self:getCon() / 5,
			spell=10 + self:getCon() / 5,
		})
		return true
	end,
	info = function(self, t)
		return ([[召唤矮人一族的传奇血统来增加你 %d 点护甲，%d 点法术和 %d 物理抵抗，持续8回合。
		受你的体质影响，此效果有额外增益。]]):format(5 + self:getCon() / 5, 10 + self:getCon() / 5, 10 + self:getCon() / 5)
	end,
}

newTalent{
	name = "Stoneskin",
	display_name = "石化皮肤",
	type = {"race/dwarf", 2},
	require = racial_req2,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self:attr("auto_stoneskin", 6)
	end,
	on_unlearn = function(self, t)
		self:attr("auto_stoneskin", -6)
	end,
	info = function(self, t)
		return ([[矮人皮肤是一种复杂的结构，它可以在受到打击后自动硬化。
		当被击打时有15%%的概率增加 %d 点护甲，持续5回合。]]):format(self:getTalentLevelRaw(t) * 6)
	end,
}

newTalent{
	name = "Power is Money",
	display_name = "金钱就是力量",
	type = {"race/dwarf", 3},
	require = racial_req3,
	points = 5,
	mode = "passive",
	info = function(self, t)
		return ([[金钱是矮人王国的心脏，它控制了所有其他决定。
		基于你的金币持有量，增加物理、精神和法术抵抗。
		+1豁免值每 %d 单位金币，上限 %d 。]]):format(80 - self:getTalentLevelRaw(t) * 5, self:getTalentLevelRaw(t) * 10)
	end,
}

newTalent{
	name = "Stone Walking",
	display_name = "穿墙术",
	type = {"race/dwarf", 4},
	require = racial_req4,
	points = 5,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 5 end,
	range = 1,
	no_npc_use = true,
	getRange = function(self, t) return math.floor(1 + self:getCon(4, true) + self:getTalentLevel(t)) end,
	action = function(self, t)
		local tg = {type="bolt", range=self:getTalentRange(t), nolock=true, talent=t}
		local x, y = self:getTarget(tg)
		if not x or not y then return nil end
		local _ _, x, y = self:canProject(tg, x, y)
		self:probabilityTravel(x, y, t.getRange(self, t))
		game:playSoundNear(self, "talents/earth")
		return true
	end,
	info = function(self, t)
		local range = t.getRange(self, t)
		return ([[虽然矮人的起源对其他种族来说始终是不解之谜，但是很显然他们的起源与石头密不可分。
你可以指定任何一堵墙并立刻穿过它，出现在另一侧。
移动距离 %d 格（受体质和分类天赋等级影响有额外加成）]]):
		format(range)
	end,
}

------------------------------------------------------------------
-- Halflings' powers
------------------------------------------------------------------
newTalentType{ type="race/halfling", name = "半身人", generic = true, description = "角色所拥有的各种种族专长技能。" }
newTalent{
	short_name = "HALFLING_LUCK",
	name = "Luck of the Little Folk",
	display_name = "小不点的幸运",
	type = {"race/halfling", 1},
	require = racial_req1,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 5 end,
	tactical = { ATTACK = 2 },
	action = function(self, t)
		self:setEffect(self.EFF_HALFLING_LUCK, 5, {
			physical=10 + self:getCun() / 2,
			spell=10 + self:getCun() / 2,
		})
		return true
	end,
	info = function(self, t)
		return ([[召唤小不点的幸运和机智来提高你 %d%% 的物理和法术伤害暴击率5回合。
		受你的灵巧影响，此效果有额外增益。]]):format(10 + self:getCun() / 2, 10 + self:getCun() / 2)
	end,
}

newTalent{
	name = "Duck and Dodge",
	display_name = "闪避",
	type = {"race/halfling", 2},
	require = racial_req2,
	points = 5,
	mode = "passive",
	info = function(self, t)
		return ([[半身人在漫长的岁月里学会利用他们矮个的优势来对抗其他种族。
		提高近程闪避，闪避值取决敌人和你的身材比值。（+ %d 闪避每个比值）]]):
		format(self:getTalentLevelRaw(t) * 1.2)
	end,
}

newTalent{
	name = "Militant Mind",
	display_name = "英勇",
	type = {"race/halfling", 3},
	require = racial_req3,
	points = 5,
	mode = "passive",
	info = function(self, t)
		return ([[半身人曾是一个有组织纪律的种族，敌人越多他们越团结。
		如果有2个或多个敌人在你的视野里，你的物理强度、法术强度和精神强度将会提高 %0.1f 每个敌人。（上限 %d 个敌人）]]):
		format(self:getTalentLevel(t) * 0.6, self:getTalentLevel(t))
	end,
}

newTalent{
	name = "Indomitable",
	display_name = "不屈意志",
	type = {"race/halfling", 4},
	require = racial_req4,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 5 end,
	tactical = { DEFEND = 1 },
	action = function(self, t)
		self:setEffect(self.EFF_FREE_ACTION, 3 + self:getTalentLevel(t), {})
		return true
	end,
	info = function(self, t)
		return ([[半身人以骁勇善战闻名世界，他们曾经在战场上对抗其他种族千年时间。
		立刻使你对震慑、眩晕和定身免疫 %d 回合。]]):format(3 + self:getTalentLevel(t))
	end,
}

------------------------------------------------------------------
-- Orcs' powers
------------------------------------------------------------------
newTalentType{ type="race/orc", name = "orc", generic = true, description = "The various racial bonuses a character can have." }
newTalent{
	short_name = "ORC_FURY",
	name = "Orcish Fury",
	type = {"race/orc", 1},
	require = racial_req1,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 4 end,
	tactical = { ATTACK = 2 },
	action = function(self, t)
		self:setEffect(self.EFF_ORC_FURY, 5, {power=10 + self:getWil(20, true)})
		return true
	end,
	info = function(self, t)
		return ([[Summons your lust for blood and destruction, increasing all damage by %d%% for 5 turns.
		The bonus will increase with the Willpower stat.]]):format(10 + self:getWil(20, true))
	end,
}

newTalent{
	name = "Hold the Ground",
	type = {"race/orc", 2},
	require = racial_req2,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self.combat_physresist = self.combat_physresist + 5
		self.combat_mentalresist = self.combat_mentalresist + 5
	end,
	on_unlearn = function(self, t)
		self.combat_physresist = self.combat_physresist - 5
		self.combat_mentalresist = self.combat_mentalresist - 5
	end,
	info = function(self, t)
		return ([[Orcs have been the prey of the other races for thousands of years, with or without reasons. They have learnt to withstand things that would break weaker races.
		Increase physical and mental save by +%d.]]):format(self:getTalentLevelRaw(t) * 5)
	end,
}

newTalent{
	name = "Skirmisher",
	type = {"race/orc", 3},
	require = racial_req3,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self.resists_pen.all = (self.resists_pen.all or 0) + 5
	end,
	on_unlearn = function(self, t)
		self.resists_pen.all = (self.resists_pen.all or 0) - 5
	end,
	info = function(self, t)
		return ([[Orcs have seen many battles, and won many of them.
		Increase all damage penetration by %d%%.]]):format(self:getTalentLevelRaw(t) * 5)
	end,
}

newTalent{
	name = "Pride of the Orcs",
	type = {"race/orc", 4},
	require = racial_req4,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 4 end,
	is_heal = true,
	tactical = { DEFEND = 1, HEAL = 2, CURE = function(self, t, target)
		local nb = 0
		for eff_id, p in pairs(self.tmp) do
			local e = self.tempeffect_def[eff_id]
			if e.status == "detrimental" and (e.type == "physical" or e.type == "magical" or e.type == "mental") then
				nb = nb + 1
			end
		end
		return nb
	end },
	action = function(self, t)
		local target = self
		local effs = {}

		-- Go through all temporary effects
		for eff_id, p in pairs(target.tmp) do
			local e = target.tempeffect_def[eff_id]
			if e.status == "detrimental" and (e.type == "physical" or e.type == "magical" or e.type == "mental") then
				effs[#effs+1] = {"effect", eff_id}
			end
		end

		for i = 1, math.ceil(self:getTalentLevel(t) * 3 / 5) do
			if #effs == 0 then break end
			local eff = rng.tableRemove(effs)

			if eff[1] == "effect" then
				target:removeEffect(eff[2])
			end
		end
		self:heal(25 + self:getCon() * 2.3)
		return true
	end,
	info = function(self, t)
		return ([[Call upon the will of all the Orc Prides to survive this battle.
		Heals for %d life and removes up to %d detrimental effects.
		The bonus will increase with the Constitution stat.]]):format(25 + self:getCon() * 2.3, math.ceil(self:getTalentLevel(t) * 3 / 5))
	end,
}

------------------------------------------------------------------
-- Yeeks' powers
------------------------------------------------------------------
newTalentType{ type="race/yeek", name = "夺心魔", generic = true, description = "角色所拥有的各种种族专长技能。" }
newTalent{
	short_name = "YEEK_WILL",
	name = "Dominant Will",
	display_name = "主导意志",
	type = {"race/yeek", 1},
	require = racial_req1,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 3 end,
	range = 4,
	no_npc_use = true,
	requires_target = true,
	direct_hit = true,
	target = function(self, t) return {type="hit", range=self:getTalentRange(t), talent=t} end,
	action = function(self, t)
		local tg = self:getTalentTarget(t)
		local x, y = self:getTarget(tg)
		if not x or not y then return nil end
		self:project(tg, x, y, function(px, py)
			local target = game.level.map(px, py, Map.ACTOR)
			if not target or target.dead then return end
			if not target:canBe("instakill") or target.rank > 2 or target.undead or not target:checkHit(self:getWil(20, true) + self.level * 1.5, target.level) then
				game.logSeen(target, "%s resists the mental assault!", target.name:capitalize())
				return
			end
			target:takeHit(1, self)
			target:takeHit(1, self)
			target:takeHit(1, self)
			target:setEffect(target.EFF_DOMINANT_WILL, 4 + self:getWil(10), {src=self})
		end)
		return true
	end,
	info = function(self, t)
		return ([[粉碎目标的意志，使你可以完全控制它的行动 %s 回合。
		当技能结束时，你的意志会脱离而目标会因身体崩溃而死亡。
		此技能无法用于精英或不死族。
		受你的意志影响，持续时间有额外加成。]]):format(4 + self:getWil(10))
	end,
}

newTalent{
	name = "Unity",
	display_name = "强化思维",
	type = {"race/yeek", 2},
	require = racial_req2,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self:attr("confusion_immune", 0.12)
		self:attr("silence_immune", 0.12)
		self.combat_mentalresist = self.combat_mentalresist + 4
	end,
	on_unlearn = function(self, t)
		self:attr("confusion_immune", -0.12)
		self:attr("silence_immune", -0.12)
		self.combat_mentalresist = self.combat_mentalresist - 4
	end,
	info = function(self, t)
		return ([[你的思维和精神网络变的更加协调并且增强对负面效果的抵抗。
		增加 %d%% 混乱和沉默抵抗并增加你 %d 点精神抵抗。]]):format(self:getTalentLevelRaw(t) * 12, self:getTalentLevelRaw(t) * 4)
	end,
}

newTalent{
	name = "Quickened",
	display_name = "迅捷",
	type = {"race/yeek", 3},
	require = racial_req3,
	points = 5,
	mode = "passive",
	on_learn = function(self, t)
		self.global_speed_base = self.global_speed_base + 0.03
		self:recomputeGlobalSpeed()
	end,
	on_unlearn = function(self, t)
		self.global_speed_base = self.global_speed_base - 0.03
		self:recomputeGlobalSpeed()
	end,
	info = function(self, t)
		return ([[基于“精神网络”，夺心魔新陈代谢很快，思维很快并且献祭也很快。
		增加 %d%%整体速度。]]):format(self:getTalentLevelRaw(t) * 3)
	end,
}

newTalent{
	name = "Wayist",
	display_name = "快速支援",
	type = {"race/yeek", 4},
	require = racial_req4,
	points = 5,
	no_energy = true,
	cooldown = function(self, t) return 50 - self:getTalentLevel(t) * 3 end,
	range = 4,
	no_npc_use = true,
	action = function(self, t)
		local tg = {type="bolt", nowarning=true, range=self:getTalentRange(t), nolock=true, talent=t}
		local tx, ty, target = self:getTarget(tg)
		if not tx or not ty then return nil end
		local _ _, tx, ty = self:canProject(tg, tx, ty)
		target = game.level.map(tx, ty, Map.ACTOR)
		if target == self then target = nil end

		-- Find space
		for i = 1, 3 do
			local x, y = util.findFreeGrid(tx, ty, 5, true, {[Map.ACTOR]=true})
			if not x then
				game.logPlayer(self, "Not enough space to summon!")
				return
			end

			local NPC = require "mod.class.NPC"
			local m = NPC.new{
				type = "humanoid", subtype = "yeek",
				display = "y",
				name = "yeek mindslayer", color=colors.YELLOW,
				resolvers.nice_tile{image="invis.png", add_mos = {{image="npc/humanoid_yeek_yeek_mindslayer.png", display_h=2, display_y=-1}}},
				desc = "A wayist that came to help.",

				body = { INVEN = 10, MAINHAND=1, OFFHAND=1, BODY=1 },

				rank = 3,
				life_rating = 8,
				max_life = resolvers.rngavg(50,80),
				infravision = 10,

				autolevel = "none",
				ai = "summoned", ai_real = "tactical", ai_state = { talent_in=2, },
				stats = {str=0, dex=0, con=0, cun=0, wil=0, mag=0},
				inc_stats = { str=25 + self:getWil() * self:getTalentLevel(t) / 5, mag=10, cun=25 + self:getWil() * self:getTalentLevel(t) / 5, wil=25 + self:getWil() * self:getTalentLevel(t) / 5, dex=18, con=10 + self:getTalentLevel(t) * 2, },

				resolvers.equip{
					{type="weapon", subtype="longsword", autoreq=true},
					{type="weapon", subtype="dagger", autoreq=true},
				},

				level_range = {1, nil}, exp_worth = 0,
				silent_levelup = true,

				combat_armor = 13, combat_def = 8,
				resolvers.talents{
					[Talents.T_KINETIC_SHIELD]={base=1, every=5, max=5},
					[Talents.T_KINETIC_AURA]={base=1, every=5, max=5},
					[Talents.T_CHARGED_AURA]={base=1, every=5, max=5},
				},

				faction = self.faction,
				summoner = self, summoner_gain_exp=true,
				summon_time = 6,
				ai_target = {actor=target}
			}
			setupSummon(self, m, x, y)
		end

		game:playSoundNear(self, "talents/spell_generic")
		return true
	end,
	info = function(self, t)
		return ([[通过夺心魔的精神网络，迅速召集帮手。
		在你周围召唤3个夺心魔精英。]])
	end,
}

-- Yeek's power: ID
newTalent{
	short_name = "YEEK_ID",
	name = "Knowledge of the Way",
	display_name = "维网的力量",
	type = {"base/race", 1},
	no_npc_use = true,
	no_unlearn_last = true,
	on_learn = function(self, t) self.auto_id = 2 end,
	action = function(self, t)
		local Chat = require("engine.Chat")
		local chat = Chat.new("elisa-orb-scrying", {name="The Way"}, self, {version="yeek"})
		chat:invoke()
		return true
	end,
	info = function(self, t)
		return ([[你可以与维网取得联系并寻找资料，增加你脑内信息，可以帮你辨识所有你不能辨识的物品。]])
	end,
}
