-- 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 = "Meditation",
	display_name = "冥想",
	type = {"wild-gift/call", 1},
	require = gifts_req1,
	points = 5,
	message = "@Source@ meditates on nature.",
	cooldown = 150,
	range = 10,
	no_npc_use = true,
	action = function(self, t)
		local seen = false
		-- Check for visible monsters, only see LOS actors, so telepathy wont prevent it
		core.fov.calc_circle(self.x, self.y, game.level.map.w, game.level.map.h, 20, function(_, x, y) return game.level.map:opaque(x, y) end, function(_, x, y)
			local actor = game.level.map(x, y, game.level.map.ACTOR)
			if actor and actor ~= self and self:reactionToward(actor) < 0 then seen = true end
		end, nil)
		if seen then
			game.logPlayer(self, "There's too much going on for you to use Meditation right now!")
			return
		end

		local dur = 17 - self:getTalentLevel(t)
		local e = 10 + self:getWil(50, true) * self:getTalentLevel(t)
		local tt = e / 2
		local pt = (e - tt) / dur
		self:setEffect(self.EFF_MEDITATION, dur, {per_turn=pt, final=tt})

		game:playSoundNear(self, "talents/spell_generic2")
		return true
	end,
	info = function(self, t)
		local dur = 17 - self:getTalentLevel(t)
		local e = 10 + self:getWil(50, true) * self:getTalentLevel(t)
		local tt = e / 2
		local pt = (e - tt) / dur
		return ([[冥想你与大自然之间的联系，陷于沉思你会眩晕 %d 回合。
		每回合你能回复 %d 失衡值，最后一回合回复 %d 。
		当你被攻击时冥想终止。
		冥想需要宁静和平和，你的视线内不能有任何敌对生物。
		效果受你意志影响有额外加成。]]):
		format(17 - self:getTalentLevel(t), pt, tt)
	end,
}

newTalent{ short_name = "NATURE_TOUCH",
	name = "Nature's Touch",
	display_name = "自然之触",
	type = {"wild-gift/call", 2},
	require = gifts_req2,
	random_ego = "defensive",
	points = 5,
	equilibrium = 10,
	cooldown = 15,
	range = 1,
	requires_target = true,
	no_npc_use = true,
	is_heal = true,
	action = function(self, t)
		local tg = {default_target=self, type="hit", nowarning=true, range=self:getTalentRange(t), first_target="friend"}
		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) > 1 then return nil end
		if not target.undead then
			target:heal(20 + self:combatTalentStatDamage(t, "wil", 30, 500))
		end
		game:playSoundNear(self, "talents/heal")
		return true
	end,
	info = function(self, t)
		return ([[对你或一个目标注入大自然的力量，治疗 %d 点伤害。(对死灵生物无效)。
		治疗量受意志影响有额外加成。]]):
		format(20 + self:combatTalentStatDamage(t, "wil", 30, 500))
	end,
}

newTalent{
	name = "Earth's Eyes",
	display_name = "大地之眼",
	type = {"wild-gift/call", 3},
	require = gifts_req3,
	points = 5,
	random_ego = "utility",
	equilibrium = 3,
	cooldown = 10,
	range = 100,
	radius = function(self, t) return math.ceil(3 + self:getTalentLevel(t)) end,
	requires_target = true,
	no_npc_use = true,
	action = function(self, t)
		local x, y = self:getTarget{type="ball", nolock=true, no_restrict=true, nowarning=true, range=100, radius=self:getTalentRadius(t)}
		if not x then return nil end

		self:magicMap(math.ceil(3 + self:getTalentLevel(t)), x, y)
		game:playSoundNear(self, "talents/spell_generic2")
		return true
	end,
	info = function(self, t)
		local radius = self:getTalentRadius(t)
		return ([[利用你与大自然的联系，你可以查看 %d 半径范围的区域。]]):
		format(radius)
	end,
}

newTalent{
	name = "Nature's Balance",
	display_name = "自然平衡",
	type = {"wild-gift/call", 4},
	require = gifts_req4,
	points = 5,
	equilibrium = 20,
	cooldown = 50,
	range = 10,
	tactical = { BUFF = 2 },
	action = function(self, t)
		local nb = math.ceil(self:getTalentLevel(t) + 2)
		local tids = {}
		for tid, _ in pairs(self.talents_cd) do
			local tt = self:getTalentFromId(tid)
			if tt.type[2] <= self:getTalentLevelRaw(t) and tt.type[1]:find("^wild%-gift/") then
				tids[#tids+1] = tid
			end
		end
		for i = 1, nb do
			if #tids == 0 then break end
			local tid = rng.tableRemove(tids)
			self.talents_cd[tid] = nil
		end
		self.changed = true
		game:playSoundNear(self, "talents/spell_generic2")
		return true
	end,
	info = function(self, t)
		return ([[你的大自然的深刻联系，使你能立刻冷却 %d 个野性技能，等级不超过 %d 。]]):
		format(math.ceil(self:getTalentLevel(t) + 2), self:getTalentLevelRaw(t))
	end,
}

