Scriptname _00E_MQ11a_PlayerAliasScript extends ReferenceAlias  

ReferenceAlias Property Alias_Calia Auto

Int FightId
Int PostFightQuestStage
FormList EnemyList
Location EnemyLocation
LocationRefType EnemyLocRefType
Bool bNoReanimation
Int nAliveEnemies
Int OutOfCombatCount


Function StartTrackingFight(Int _fightId, Int _postFightQuestStage, FormList _enemyList, Location _enemyLocation, LocationRefType _enemyLocRefType, Bool _bNoReanimation)
	If FightId == _fightId
		Return
	EndIf

	If FightId != 0
		; Safeguard: clear the previous fight
		PostFightCleanup()
		GoToState("") 
	EndIf

	FightId = _fightId
	PostFightQuestStage = _postFightQuestStage
	EnemyList = _enemyList
	nAliveEnemies = EnemyList.GetSize()
	EnemyLocation = _enemyLocation
	EnemyLocRefType = _enemyLocRefType
	bNoReanimation = _bNoReanimation
	GoToState("TrackFight")
EndFunction

Function RegisterEnemyDeath()
	nAliveEnemies -= 1
EndFunction

Function PostFightCleanup()
	FightId = 0

	If EnemyList
		; Kill'em all as a safeguard!
		Form[] enemies = EnemyList.ToArray()
		Int Index = 0
		While Index < enemies.Length
			Actor enemyRef = (enemies[Index] as Actor)
			If enemyRef.IsDead() == False
				enemyRef.Kill(enemyRef) ; "Suicide" -> no XP for the player, hehe
			EndIf
			Index += 1
		EndWhile

		EnemyList = None
	EndIf
EndFunction

Bool Function CheckEnemiesAreDead()
	Form[] enemies = EnemyList.ToArray()
	Int Index = 0
	While Index < enemies.Length
		If (enemies[Index] as Actor).IsDead() == False
			Return False
		EndIf
		Index += 1
	EndWhile

	Return True
EndFunction

State TrackFight
	Event OnBeginState()
		OutOfCombatCount = 0

		If bNoReanimation
			RegisterForSingleUpdate(2.0)
		Else
			RegisterForSingleUpdate(10.0)
		EndIf
	EndEvent

	Event OnEndState()
		PostFightCleanup()
	EndEvent

	Event OnUpdate()
		Int curStage = GetOwningQuest().GetCurrentStageID()

		If (PostFightQuestStage > 0) && (curStage >= PostFightQuestStage)
			GoToState("")
			Return
		EndIf

		Bool bIsInCombat = GetActorReference().IsInCombat()
		If (curStage >= 95) && (curStage < 130)
			Actor caliaRef = Alias_Calia.GetActorReference()
			If bIsInCombat == False
				If caliaRef.IsInCombat()
					bIsInCombat = True
				ElseIf caliaRef.IsBleedingOut() && (nAliveEnemies > 0)
					bIsInCombat = True
				EndIf
			Else ; bIsInCombat
				If caliaRef.IsInCombat() == False && caliaRef.IsBleedingOut() == False
					; Make Calia join the fray
					Actor targetRef = GetActorReference().GetCombatTarget()
					If targetRef
						caliaRef.StartCombat(targetRef)
					EndIf
				EndIf
			EndIf
		EndIf

		If bIsInCombat == False
			OutOfCombatCount += 1
			If (nAliveEnemies <= 0) || (EnemyLocation.GetRefTypeAliveCount(EnemyLocRefType) <= 0) || (OutOfCombatCount >= 5 && CheckEnemiesAreDead())
				Int oldFightId = (FightId as Int)
				GoToState("")
				(GetOwningQuest() as _00E_MQ11a_Functions).RegisterFightEnd(oldFightId, PostFightQuestStage)
				Return
			EndIf
		Else
			OutOfCombatCount = 0
		EndIf

		RegisterForSingleUpdate(1.0)
	EndEvent
EndState