Scriptname RenVertibird:RenVertibirdQuestScript extends Quest

Actor Property CurrentVertibird Auto

Keyword Property isVertibird Auto
Keyword Property PilotSlot1 Auto

Perk Property RenVertibirdPerk Auto

Actor Property PlayerRef Auto

Idle Property FurnitureBoardVertibirdPilot Auto

Idle Property FlyStartHover Auto
Idle Property FlyStartCruise Auto
Idle Property VertibirdPropellersOn Auto
Idle Property FlyStartTakeOffDefault Auto

Static Property XMarker Auto

ObjectReference Property PortMarker Auto
ObjectReference Property StarboardMarker Auto

ObjectReference Property PathMarker Auto

float headingangle

ReferenceAlias Property RenVertibirdTravelMarker Auto
ReferenceAlias Property RenVertibirdAlias Auto

int markerdistance = 500

int deadzone = 100

int anglespeed = 10

ObjectReference playercamerapoint

float portzangle
float starboardzangle
float forward_vector_x
float forward_vector_y

Event OnInit()
	if (PlayerRef == none)
		PlayerRef = Game.GetPlayer()
	endif
	PlayerRef.AddPerk(RenVertibirdPerk)				
	
	Self.StartTimer(0.1, 0)
endEvent

Event OnGainLOS(ObjectReference akViewer, ObjectReference akTarget)
	if (akTarget == StarboardMarker)
		headingangle = 90
		debug.notification("Got LOS gain for: " + StarboardMarker)		
	elseif (akTarget == PortMarker)
		headingangle = -90
		debug.notification("Got LOS gain for: " + PortMarker)				
	endif
	Self.RegisterForDirectLOSGain(PlayerRef, PortMarker)
	Self.RegisterForDirectLOSGain(PlayerRef, StarboardMarker)
endEvent

Event OnTimer(int timerid)
	if (CurrentVertibird != none && CurrentVertibird.GetFlyingState() >= 2)
		;CurrentVertibird.SetPlayerControls(true)
		if (PortMarker == none)
			PortMarker = CurrentVertibird.PlaceAtMe(XMarker)
			StarboardMarker = CurrentVertibird.PlaceAtMe(XMarker)

			CurrentVertibird.EnableAI(false)		

			Self.RegisterForRemoteEvent((CurrentVertibird As ObjectReference), "OnTranslationAlmostComplete")			
			Self.RegisterForRemoteEvent((CurrentVertibird As ObjectReference), "OnTranslationFailed")	
			Self.RegisterForRemoteEvent((CurrentVertibird As ObjectReference), "OnTranslationComplete")
			
			CurrentVertibird.TranslateTo(CurrentVertibird.GetPositionX() + Math.cos(CurrentVertibird.GetAngleZ()) * 500, CurrentVertibird.GetPositionY() + Math.sin(CurrentVertibird.GetAngleZ()) * 500, CurrentVertibird.GetPositionZ(), CurrentVertibird.GetAngleX(), CurrentVertibird.GetAngleY(), headingangle, 100, 0.0)
		endif
		
		portzangle = CurrentVertibird.GetAngleZ() - 70
		starboardzangle = CurrentVertibird.GetAngleZ() + 70		
		
		if (PathMarker.GetAngleZ() < CurrentVertibird.GetAngleZ())
			headingangle = CurrentVertibird.GetAngleZ() - anglespeed
		elseif (PathMarker.GetAngleZ() > CurrentVertibird.GetAngleZ())
			headingangle = CurrentVertibird.GetAngleZ() + anglespeed
		endif
		
		float realheading = CurrentVertibird.GetAngleZ()
		
		;/
		PortMarker.MoveTo(PlayerRef, Math.cos(portzangle) * 200, Math.sin(portzangle) * 200, 0)
		StarboardMarker.MoveTo(PlayerRef, Math.cos(starboardzangle) * 200, Math.sin(starboardzangle) * 200, 0)
		/;
		
		;headingangle = headingangle % 360
		if (headingangle > 360)
			headingangle = 0
		elseif (headingangle < 0)
			headingangle = 360
		endif
		
		PathMarker.MoveToNode(PlayerRef, "Camera")		
		
		forward_vector_x = CurrentVertibird.GetPositionX() + Math.cos(PathMarker.GetAngleZ()) * 500
		forward_vector_y = CurrentVertibird.GetPositionY() + Math.sin(PathMarker.GetAngleZ()) * 500
		
		float vertibird_height = CurrentVertibird.GetPositionZ()	
	endif
	
	Self.StartTimer(0.1, 0)
endEvent

Event ObjectReference.OnTranslationComplete(ObjectReference akSender)
	;PathMarker.MoveTo(akSender, Math.cos(headingangle) * markerdistance, Math.sin(headingangle) * markerdistance, 2.0)	
	
	debug.notification("Translation complete. Retranslating...")
	
	CurrentVertibird.TranslateTo(forward_vector_x, forward_vector_y, CurrentVertibird.GetPositionZ(), CurrentVertibird.GetAngleX(), CurrentVertibird.GetAngleY(), CurrentVertibird.GetAngleZ(), 100, 0.0)
endEvent

Event ObjectReference.OnTranslationFailed(ObjectReference akSender)	
	CurrentVertibird.TranslateTo(forward_vector_x, forward_vector_y, CurrentVertibird.GetPositionZ(), CurrentVertibird.GetAngleX(), CurrentVertibird.GetAngleY(), headingangle, 100, 0.0)	
endEvent

Event OnAnimationEvent(ObjectReference akSource, string asEventName)
	debug.notification(akSource + ", " + asEventName)
	if (akSource == PlayerRef)
		
	else
	
	endif
endEvent

Function TransferToVertibird(Actor vertibird)
	debug.notification("Vertibird: " + vertibird.GetFormID())	
	
	PlayerRef.AddKeyword(PilotSlot1)
	Utility.Wait(1.0)
	vertibird.Activate(PlayerRef, true)
	;Game.SetCameraTarget(vertibird)
	Game.ForceFirstPerson()
	;Game.ForceThirdPerson()
	;PlayerRef.SetPlayerControls(false)
	vertibird.EnableAI(true)
	Utility.Wait(10.0)
	
	CurrentVertibird = vertibird	
	
	if (PathMarker == none)
		PathMarker = PlayerRef.PlaceAtNode("Camera", XMarker)
	endif
	
	float VertibirdHeading = CurrentVertibird.GetAngleZ()	
	
	debug.notification("Player, Camera: " + PlayerRef.HasNode("Camera") + ", CamTarget " + PlayerRef.HasNode("CamTarget"))
	
	PlayerRef.SetVehicle(CurrentVertibird)
	
	Self.RegisterForRemoteEvent(CurrentVertibird, "OnTranslationAlmostComplete")	
	
	CurrentVertibird.PlayIdle(FlyStartTakeOffDefault)
endFunction