ScriptName VCA_Affinity_Init extends ReferenceAlias

;-- Properties --------------------------------------

GlobalVariable Property DisplayMode Auto
GlobalVariable Property SupportFarHarbor Auto

FormList Property CompanionReferenceList Auto
FormList Property AffinityMessageList Auto

Faction Property CurrentCompanionFaction Auto
Faction Property HasBeenCompanionFaction Auto

;-- Variables ---------------------------------------

FollowersScript Followers

Float[] CurrentAffinityValuesArray

FormList DisplayMessageList

;-- Functions ---------------------------------------

Event OnInit()
	Followers = FollowersScript.GetScript()

	RefreshCurrentAffinityValuesArray()
	
	RegisterForCustomEvent(Followers, "AffinityEvent")
EndEvent

Event OnPlayerLoadGame()
	
	RefreshCurrentAffinityValuesArray()

EndEvent

Function RefreshCurrentAffinityValuesArray()
	CurrentAffinityValuesArray = new Float[CompanionReferenceList.GetSize()]
	Int i = 0
	While i < CompanionReferenceList.GetSize()
		Actor akCompanion = CompanionReferenceList.GetAt(i) as Actor
		CurrentAffinityValuesArray[i] = akCompanion.getValue(followersscript.GetScript().CA_Affinity)
		i += 1
	EndWhile
EndFunction

Event FollowersScript.AffinityEvent(FollowersScript akSender, Var[] args)

	If ( DisplayMode.GetValue() == 0 )
		DisplayMessageList = AffinityMessageList.GetAt(0) as FormList
	ElseIf( DisplayMode.GetValue() == 1 )
		DisplayMessageList = AffinityMessageList.GetAt(1) as FormList
	ElseIf( DisplayMode.GetValue() == 2 )
		DisplayMessageList = AffinityMessageList.GetAt(2) as FormList
	ElseIf( DisplayMode.GetValue() == 3 )
		DisplayMessageList = AffinityMessageList.GetAt(3) as FormList
	EndIf

	Utility.Wait(5)
	Int i = 0
	While i < CompanionReferenceList.GetSize()
		Actor akCompanion = CompanionReferenceList.GetAt(i) as Actor
		If (akCompanion.IsInFaction(CurrentCompanionFaction) || akCompanion.IsInFaction(HasBeenCompanionFaction))
			If SupportFarHarbor.GetValue() != 0 || i != 6
				Float NewAffinityValue = akCompanion.getValue(followersscript.GetScript().CA_Affinity)
				If NewAffinityValue != CurrentAffinityValuesArray[i]
					Float AffinityChange = NewAffinityValue - CurrentAffinityValuesArray[i]
					CurrentAffinityValuesArray[i] = NewAffinityValue
					ShowAffinity(NewAffinityValue, AffinityChange, DisplayMessageList.GetAt(i) as Message)
				EndIf
			EndIf
		EndIf
		i += 1
	EndWhile
EndEvent


Function ShowAffinity(Float CurrentAffinity, Float AffinityChange, Message Msg)
	If (DisplayMode.GetValue() < 3)
		CurrentAffinity = CurrentAffinity / 10
		AffinityChange = AffinityChange / 10
	EndIf
	Msg.show(CurrentAffinity, AffinityChange, 0, 0, 0, 0, 0, 0, 0)
EndFunction
