-- 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 = "Stone Vines",
	display_name = "石藤",
	type = {"wild-gift/earthen-vines", 1},
	require = gifts_req1,
	points = 5,
	mode = "sustained",
	sustain_equilibrium = 15,
	cooldown = 30,
	tactical = { ATTACK = { PHYSICAL = 2 }, BUFF = 2, DISABLE = { pin = 2 } },
	radius = function(self, t) return 4 + math.ceil(self:getTalentLevel(t) / 2) end,
	getValues = function(self, t) return 4 + self:getTalentLevelRaw(t), self:combatTalentStatDamage(t, "wil", 3, 50) end,
	do_vines = function(self, t)
		local p = self:isTalentActive(t.id)
		local rad = self:getTalentRadius(t)

		local tgts = {}
		local grids = core.fov.circle_grids(self.x, self.y, rad, true)
		for x, yy in pairs(grids) do for y, _ in pairs(grids[x]) do
			local a = game.level.map(x, y, Map.ACTOR)
			if a and self:reactionToward(a) < 0 and not a:hasEffect(a.EFF_STONE_VINE) then
				tgts[#tgts+1] = a
			end
		end end
		if #tgts <= 0 then return end

		-- Randomly take targets
		local tg = {type="hit", range=self:getTalentRange(t), talent=t}
		local a, id = rng.table(tgts)
		local hit, chance = a:checkHit(self:combatTalentStatDamage(t, "wil", 5, 110), a:combatPhysicalResist(), 0, 95, 5)
		if a:canBe("pin") and hit then
			local turns, dam = t.getValues(self, t)
			a:setEffect(a.EFF_STONE_VINE, turns, {dam=dam, src=self, free=rad*2, free_chance=100-chance})
			game.level.map:particleEmitter(self.x, self.y, math.max(math.abs(a.x-self.x), math.abs(a.y-self.y)), "stonevine", {tx=a.x-self.x, ty=a.y-self.y})
			game:playSoundNear(self, "talents/stone")
		end
	end,
	activate = function(self, t)
		return {
			movid = self:addTemporaryValue("movement_speed", -0.5),
			particle = self:addParticles(Particles.new("stonevine_static", 1, {})),
		}
	end,
	deactivate = function(self, t, p)
		self:removeTemporaryValue("movement_speed", p.movid)
		self:removeParticles(p.particle)
		return true
	end,
	info = function(self, t)
		local rad = self:getTalentRadius(t)
		local turns, dam = t.getValues(self, t)
		return ([[从你的脚下生长出石藤，每回合会随机攻击 %d 范围内的目标。
		受影响的目标会被定在原地，受到 %0.2f 物理伤害，持续 %d 回合。
		目标在离你 %d 格远处时会从石藤中摆脱出来。
		当石藤激活时，你的移动速度下降50%%。
		每个回合，被石藤缠住的生物都有一定几率逃脱。
		伤害有意志影响有额外加成。]]):
		format(rad, damDesc(self, DamageType.PHYSICAL, dam), turns, rad*2)
	end,
}

newTalent{
	name = "Eldritch Vines",
	display_name = "怪异之藤",
	type = {"wild-gift/earthen-vines", 2},
	require = gifts_req2,
	points = 5,
	mode = "passive",
	info = function(self, t)
		return ([[每次石藤造成伤害会回复 %0.2f 失衡值和 %0.2f 法力。]])
		:format(self:getTalentLevel(t) / 4, self:getTalentLevel(t) / 3)
	end,
}

newTalent{
	name = "Rockwalk",
	display_name = "石行术",
	type = {"wild-gift/earthen-vines", 3},
	require = gifts_req3,
	points = 5,
	equilibrium = 15,
	cooldown = 10,
	requires_target = true,
	range = 20,
	tactical = { HEAL = 2, CLOSEIN = 2 },
	action = function(self, t)
		local tg = {type="hit", range=self:getTalentRange(t)}
		local x, y, target = self:getTarget(tg)
		if not x or not y or not target then return nil end
		if not target:hasEffect(target.EFF_STONE_VINE) then return nil end

		self:heal(100 + self:combatTalentStatDamage(t, "wil", 40, 630))
		local tx, ty = util.findFreeGrid(x, y, 2, true, {[Map.ACTOR]=true})
		if tx and ty then
			local ox, oy = self.x, self.y
			self:move(tx, ty, true)
			if config.settings.tome.smooth_move > 0 then
				self:resetMoveAnim()
				self:setMoveAnim(ox, oy, 8, 5)
			end
		end

		return true
	end,
	info = function(self, t)
		return ([[与一个石藤融合，遁入地下并出现在另外一个石藤缠绕的目标身边。
		与石藤的融合能让你回复 %0.2f 生命。
		治疗量受意志影响有额外加成。]])
		:format(100 + self:combatTalentStatDamage(t, "wil", 40, 630))
	end,
}

newTalent{
	name = "吞噬",
	type = {"wild-gift/earthen-vines", 4},
	require = gifts_req4,
	points = 5,
	equilibrium = 15,
	cooldown = 10,
	requires_target = true,
	range = 20,
	tactical = { ATTACK = { PHYSICAL = 2 }, CLOSEIN = 2 },
	action = function(self, t)
		local tg = {type="hit", range=self:getTalentRange(t)}
		local x, y, target = self:getTarget(tg)
		if not x or not y or not target then return nil end
		if not target:hasEffect(target.EFF_STONE_VINE) then return nil end

		DamageType:get(DamageType.PHYSICAL).projector(self, target.x, target.y, DamageType.PHYSICAL, 80 + self:combatTalentStatDamage(t, "wil", 40, 330))

		if target.dead then return end

		local tx, ty = util.findFreeGrid(self.x, self.y, 2, true, {[Map.ACTOR]=true})
		if tx and ty then
			local ox, oy = target.x, target.y
			target:move(tx, ty, true)
			if config.settings.tome.smooth_move > 0 then
				target:resetMoveAnim()
				target:setMoveAnim(ox, oy, 8, 5)
			end
		end

		return true
	end,
	info = function(self, t)
		return ([[用你的石藤吞噬目标，强制将其拉至你身边。
		同时造成 %0.2f 物理伤害。
		受意志影响伤害有额外加成。]])
		:format(80 + self:combatTalentStatDamage(t, "wil", 40, 330))
	end,
}
