Scriptname RenWireless:RenWirelessQuestScript extends Quest Conditional

Holotape Property RenWirelessCommHolo Auto

Actor Property TARef Auto
Actor Property prevactor Auto

Static Property XMarker Auto
ObjectReference tempmarker

ObjectReference Property PlayerRef Auto

Perk Property RenWirelessHamRadioPerk Auto

int tries = 0

Sound Property RenWirelessCommStartup Auto
Sound Property RenWirelessCommEnd Auto

bool pipboyopen = false
; needed to avoid a "race" condition.

Actor Property Dogmeat Auto

Static Property CollisionMarker Auto

bool Property farharborinstalled = false Auto Conditional

bool Property DimaAlive Auto Conditional
bool Property TektusAlive Auto Conditional
bool Property AveryAlive Auto Conditional

Sound Property RenRadioStaticLP Auto

Actor Dima
Actor Avery
Actor Tektus

int staticint

Event OnInit()
	Self.RegisterForMenuOpenCloseEvent("DialogueMenu")	
	Self.RegisterForMenuOpenCloseEvent("PipboyMenu")
	Self.RegisterForMenuOpenCloseEvent("TerminalMenu")
	(PlayerRef As Actor).AddPerk(RenWirelessHamRadioPerk)
	if (Game.GetDifficulty() < 5)
		; not hardcore mode, so give the holotape.
		if (PlayerRef.GetItemCount(RenWirelessCommHolo) > 0)
			PlayerRef.RemoveItem(RenWirelessCommHolo, -1, true)
		endif
		PlayerRef.AddItem(RenWirelessCommHolo, 1)
	endif
	if (Game.GetFormFromFile(0x01005425, "DLCCoast.esm") != none)
		farharborinstalled = false
		; keeping this disabled for now, still trying to fix bugs
		; related to DiMA.
		Dima = Game.GetFormFromFile(0x01005425, "DLCCoast.esm") As Actor
		Avery = Game.GetFormFromFile(0x01005c82, "DLCCoast.esm") As Actor
		Tektus = Game.GetFormFromFile(0x01004485, "DLCCoast.esm") As Actor
	endif
endEvent

Event OnDistanceGreaterThan(ObjectReference akObj1, ObjectReference akObj2, float afDistance)
	ReturnActor()	
	Self.UnregisterForDistanceEvents(akObj1, akObj2)	
endEvent

Event OnTimer(int timerid)
	if (TARef != none && TARef.IsTalking() == true)
		if (staticint == 0)
			staticint = RenRadioStaticLP.Play(TARef)
		endif
	else
		if (staticint != 0)
			Sound.StopInstance(staticint)
			staticint = 0
		endif
	endif
	Self.StartTimer(0.1, 0)
endEvent

Function ReturnActor()
	Self.CancelTimer(0)
	if (staticint != 0)
		Sound.StopInstance(staticint)
		staticint = 0
	endif	
	if (tempmarker != none)
		TARef.MoveTo(tempmarker)
	else
		TARef.MoveToMyEditorLocation()
	endif		
	
	TARef.SetAlpha(1.0, false)
	TARef.SetGhost(false)
	TARef.SetRestrained(false)							
	
	if (tempmarker != none)
		tempmarker.Disable()
		tempmarker.DeleteWhenAble()
		tempmarker = none
	endif
	Self.UnregisterForDistanceEvents(TARef, PlayerRef)
	TARef = none
endFunction

Function CallActor()
	if (TARef != none)
		prevactor = TARef
		RenWirelessCommStartup.Play(PlayerRef)
	
		Utility.Wait(1.5)
	
		tempmarker = TARef.PlaceAtMe(XMarker, 1, true, false, true)
	
		Self.StartTimer(0.1, 0)		
	
		TARef.MoveTo(PlayerRef, -200.0 * Math.Sin(PlayerRef.GetAngleZ()), -200.0 * Math.Cos(PlayerRef.GetAngleZ()), -1000)		
		
		while (TARef != none && TARef.Is3DLoaded() == false || TARef.IsBoundGameObjectAvailable() == false)
			; spin...
		endWhile			
		
		if (TARef == none)
			TARef = prevactor
			ReturnActor()
		endif

		TARef.SetAlpha(0.0, false)						
		Utility.Wait(0.1)			
		TARef.SetGhost(true)
		Utility.Wait(0.1)			
		TARef.SetRestrained(true)		
		Utility.Wait(0.1)			
		TARef.SetAlpha(0.0, false)						
		Utility.Wait(0.5)
		
		Self.RegisterForDistanceGreaterThanEvent(TARef, PlayerRef, 300)							
		
		TARef.MoveTo(PlayerRef, 120.0 * Math.Sin(PlayerRef.GetAngleZ()), 120.0 * Math.Cos(PlayerRef.GetAngleZ()), 0.0)						
		
		TARef.SetAngle(TARef.GetAngleX(), TARef.GetAngleY(), TARef.GetAngleZ() + TARef.GetHeadingAngle(PlayerRef))	
		
		TARef.SetAlpha(0.0, false)		
		
		while (TARef != none && TARef.IsTalking() == true)
			Utility.Wait(1.0)				
		endWhile	
		
		TARef.Activate(PlayerRef, true)			
		
		if (TARef == none)
			TARef = prevactor
			ReturnActor()
		endif			
	endif
endFunction

Event OnMenuOpenCloseEvent(string asMenuName, bool abOpening)

	if (farharborinstalled == true)
		DimaAlive = !Dima.IsDead()
		TektusAlive = !Tektus.IsDead()
		AveryAlive = !Avery.IsDead()
		
		if (Dima.IsDisabled() == true)
			DimaAlive = false
		endif
		if (Avery.IsDisabled() == true)
			AveryAlive = false
		endif
		if (Tektus.IsDisabled() == true)
			TektusAlive = false
		endif
	endif
	
	if (asMenuName == "DialogueMenu" && abOpening == false)
		if (TARef != none)
			ReturnActor()		
			Utility.Wait(0.5)
			RenWirelessCommEnd.Play(PlayerRef)
		endif
	endif
	if (asMenuName == "PipboyMenu" && abOpening == true)
		pipboyopen = true
		if (TARef != none)
			ReturnActor()
		endif
		TARef = none
	endif
	if (asMenuName == "TerminalMenu" && abOpening == true)
		if (TARef != none)
			ReturnActor()
		endif
		TARef = none	
	endif	
	if (asMenuName == "TerminalMenu" && abOpening == false && pipboyopen == false)
		if (TARef != none)
			CallActor()
		endif
	endif
	if (asMenuName == "PipboyMenu" && abOpening == false)
		pipboyopen = false
		if (TARef != none)
			CallActor()
		endif
	endif
endEvent