﻿--[[ 馭獸訣 ]]

--[[ 設定旗標方法

controller.SetAchievementFlag(模式,"旗標名稱",值, 方法);

模式 		= 數字：0 單一遊戲制  1 總體累計制
旗標名稱 	= 文字 (需用雙引號包住)
值			= 數字 (必須是整數)
方法		= 數字 0 增加   1 減少   2 設定為   3 清除

]]

--[[  印出頭上文字方法

controller.PrintLaunch(unit, "文字");

單位		= BattleUnit：
文字		= 訊息文字 (需用雙引號包住)

]]

--捕捉成功機率
local CaptureRatio = 1;

function BeginBattle()
	CaptureRatio = 1;
	--初級誘餌
	if unit.HasSkill("SkillTree171_005") then
		CaptureRatio = CaptureRatio + 0.05;
	end
	--中級誘餌
	if unit.HasSkill("SkillTree171_006") then
		CaptureRatio = CaptureRatio + 0.05;
	end
	--馭獸秘典
	if unit.HasSkill("SkillTree171_007") then
		CaptureRatio = CaptureRatio + 0.05;
	end
	--高級誘餌
	if unit.HasSkill("SkillTree171_012") then
		CaptureRatio = CaptureRatio + 0.05;
	end
	--獸王訣．絆
	if unit.HasSkill("SkillTree171_013") then
		CaptureRatio = CaptureRatio + 0.1;
	end
end

--造成傷害前判定是否為獸王
function AfterAttackUnit(info, damage )	

	if info.skill.Id ~= item.Id then 
	return 
	end
	
	--必須是特定獸王ID
	local canCapture = false
	if  damage.target.Id == "n50070"   --雪翼雕
	then
		canCapture = true
	end
	if (canCapture == false) then 
		controller.PrintLaunch(damage.target, "此目標無法馴服");
		return
	end
	
	--捕捉率加成
	local ratioAdd = 0;
	--獸王的血 (hp/maxHp * 100%)
	local maxHp = damage.target.MaxHP;
	local hp = damage.target.HP;
	local percentage = hp / maxHp;
	--80%以上倒扣0.5，70%以下遞減
	if percentage >= 0.8 then
		ratioAdd = -0.5;
	elseif percentage <= 0.9 then
		ratioAdd = 0.03;
	elseif percentage <= 0.8 then
		ratioAdd = 0.06;
	elseif percentage <= 0.7 then
		ratioAdd = 0.09;
	elseif percentage <= 0.6 then
		ratioAdd = 0.12;
	elseif percentage <= 0.5 then
		ratioAdd = 0.15;
	elseif percentage <= 0.4 then
		ratioAdd = 0.25;
	elseif percentage <= 0.3 then
		ratioAdd = 0.35;
	end
	--捕捉成功率 = 基礎捕捉率 + 捕捉率加成
	local captureRatio = CaptureRatio + ratioAdd;
	--捕捉成功
	if math.random() <= captureRatio then	
		--跳訊息
		controller.PrintLaunch(damage.target, "捕捉成功");
	
		--雪翼雕離場
		damage.target.HP = 0;
		
		controller.SetBlackboard("f50070isCaptured", 2, 1);
		controller.Talk("n50070bt032");

	else
		--跳訊息
		controller.PrintLaunch(damage.target, "捕捉失敗");
	end		
end