Scriptname aaaKimachuuDuelForSPID Extends ActiveMagicEffect

Actor Property PlayerRef auto
GlobalVariable property aaaKimachuuDuelGlobal auto
Spell Property aaaKimachuuDuelSpellSetAV auto

Event OnEffectStart(Actor akTarget, Actor akCaster)
	if akTarget.GetRelationshipRank(PlayerRef) == 1
		if (akTarget.IsEssential())
			akTarget.SetRelationshipRank(PlayerRef, -2)
			akTarget.AddSpell(aaaKimachuuDuelSpellSetAV)
			akTarget.GetActorBase().SetInvulnerable()
		endif
	endif
EndEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	if akTarget.GetRelationshipRank(PlayerRef) == -3
		float akTargetHealth = akTarget.GetAVPercentage("Health")
		if (akTargetHealth < 0.3)
			akTarget.SetRelationshipRank(PlayerRef, 3)
			akTarget.StopCombatAlarm()
			akTarget.StopCombat()
			aaaKimachuuDuelGlobal.SetValue(0)
			akTarget.RestoreActorValue("health", 9999)
			akTarget.RemoveSpell(aaaKimachuuDuelSpellSetAV)
			PlayerRef.GetActorBase().SetEssential(false)
			Utility.Wait(1.0)
			akTarget.PushActorAway(akTarget, 2.0)
		else
			akTarget.SetRelationshipRank(PlayerRef, -2)
			akTarget.StopCombatAlarm()
			akTarget.StopCombat()
			akTarget.GetActorBase().SetInvulnerable()
			akTarget.RestoreActorValue("health", 9999)
			aaaKimachuuDuelGlobal.SetValue(0)
			PlayerRef.RestoreActorValue("health", 9999)
			PlayerRef.GetActorBase().SetEssential(false)
			Utility.Wait(1.0)
			PlayerRef.PushActorAway(PlayerRef, 2.0)
		endif
	endif
EndEvent
