Scriptname _00E_HeartlandTrappedBearScript extends actor  

; For several hours after being released from the cage the bear remains a friend of both the player and bandits for these reasons:
; 1) as a "thank you" to the player
; 2) to allow a 1v1 fight between the player and the poacher
; 3) not to draw other nearby bears to the cage who otherwise often rush to help this bear
; After those several hours elapse, the bear "forgets" about this.

Faction Property BanditFriendFaction Auto
Faction Property PlayerFriendFaction Auto

Function ReleaseFromCage()
	; Debug.Trace(self + ": ReleaseFromCage")
	SetAV("Assistance", 1) ; Help allies
	If IsDead() == False
		_SetStageFlag(1)
		GoToState("GetOutOfCage")
		EvaluatePackage()
		RegisterForSingleUpdateGameTime(12.0)
	Else
		RemoveFromFaction(BanditFriendFaction)
		RemoveFromFaction(PlayerFriendFaction)
		_SetStageFlag(2)
	EndIf
EndFunction

Function DoneGettingOutOfCage()
	GoToState("")
	_SetStageFlag(2)
	; Debug.Trace(self + ": DoneGettingOutOfCage")
EndFunction

State GetOutOfCage
	Event OnDying(Actor akKiller)
		; Debug.Trace(self + ": OnDying")
		DoneGettingOutOfCage()
		UnregisterForUpdateGameTime()
	EndEvent

	Event OnDeath(Actor akKiller)
		; Debug.Trace(self + ": OnDeath")
		DoneGettingOutOfCage()
		UnregisterForUpdateGameTime()
	EndEvent

	Event OnCombatStateChanged(Actor akTarget, Int aeCombatState)
		If aeCombatState != 0
			; Debug.Trace(self + ": OnCombatStateChanged")
			DoneGettingOutOfCage()
		EndIf
	EndEvent
EndState

Event OnUpdateGameTime()
	; Debug.Trace(self + ": OnUpdateGameTime")

	; Failsafe by timeout
	If GetState() == "GetOutOfCage"
		DoneGettingOutOfCage()
	EndIf

	; After some time the bear forgets that humans are its friends
	If Is3DLoaded() == False
		RemoveFromFaction(BanditFriendFaction)
		RemoveFromFaction(PlayerFriendFaction)
	Else
		RegisterForSingleUpdateGameTime(1.0)
	EndIf
EndEvent

Function _SetStageFlag(Int iStage)
	SetAV("Variable01", iStage as Float)
EndFunction