Scriptname _00E_A0_MyradFlightControlQuest extends Quest 

Actor Property player auto hidden
Actor Property Myrad auto hidden
Actor Property Camera auto hidden

ReferenceAlias Property EVENTleaveFurniture  Auto  
ReferenceAlias Property EVENTlandingApproach  Auto  
ReferenceAlias Property EVENTreturnHome Auto  
ReferenceAlias Property EVENTreturnHome01 Auto  
ReferenceAlias Property EVENTreturnHome02 Auto  
ReferenceAlias Property EVENTplayerWalkAway  Auto  

objectReference Property PlayerGoal Auto  hidden
objectReference Property CamSpawn Auto hidden

objectReference Property PlayerLure Auto hidden
objectReference Property Home Auto hidden

objectReference Property FakeSit Auto hidden

Idle Property groundMountDragonIdle  Auto  
Idle Property IdleWebExit  Auto  
Idle Property IdleDrunk  Auto 
Idle Property grounddismountdragonNPC  Auto  
Idle Property grounddismountdragon  Auto  

ImpactDataSet Property FXDragonTakeoffImpactSet Auto
ImpactDataSet Property FXDragonLandingImpactSet Auto

ImageSpaceModifier Property IMODfadeOut  Auto  
ImageSpaceModifier Property IMODpitchBlack Auto  
ImageSpaceModifier Property IMODtakeOff  Auto  
ImageSpaceModifier Property IMODtakeOffDOF  Auto  
ImageSpaceModifier Property IMODflying  Auto  
ImageSpaceModifier Property IMODtakeLand  Auto  
ImageSpaceModifier Property IMODdizzy  Auto  

Sound  Property SFXmyradSoftHowl Auto
Sound  Property SFXmyradRoadDistant Auto
Sound  Property SFXflyBy Auto
Sound  Property SFXwindLP Auto
Sound  Property SFXplayerDrop Auto

Explosion Property PuffoffSmoke Auto 
Explosion Property PuffoffDragonSmoke Auto 
VisualEffect Property CameraFXstart Auto 
VisualEffect Property CameraFXstart02 Auto
VisualEffect Property CameraFXloop Auto 
 

GlobalVariable Property MyradIsFlying  Auto  
GlobalVariable Property FlightRoute  Auto  

import GAME
import UTILITY

int Property SoundID01 Auto hidden
int Property SoundID02 Auto hidden

Float Property SoundID01volume Auto hidden
Float Property DELAYrefreshCameraFX Auto hidden
Float Property DELAYcinematicEnd Auto hidden
Float Property ReturnAV Auto hidden

bool Property LandingApproach Auto hidden
bool Property Rollercoaster Auto hidden

Event OnINIT()
	player = getPlayer()
ENDevent 

Keyword Property LinklandingApproach Auto 
Keyword Property LINKlandingCamera Auto 
Keyword Property LINKlandingPlayerMarker Auto 
Keyword Property LINKlandingMarker Auto 

objectReference Property ApproachBoogie Auto hidden
objectReference Property LandingZone Auto hidden
objectReference Property LandingCinematicCamera Auto hidden
objectReference Property LandingCinematicPlayerGoal Auto hidden
;====================================================================
Function CallMyrad(actor INCOMING, int FlightNumber)
;====================================================================
{Called by: any source}

		if FlightNumber == 1 
			RollerCoaster = false
		else
			RollerCoaster = false
		endif

		Myrad = INCOMING
		ApproachBoogie = Myrad.GetLinkedRef(LinkLandingApproach)
		LandingCinematicCamera = Myrad.GetLinkedRef(LINKlandingCamera)
		LandingCinematicPlayerGoal = Myrad.GetLinkedRef(LINKlandingPlayerMarker)
		
		CamSpawn = LandingCinematicCamera
		PlayerGoal = LandingCinematicPlayerGoal
		
		Home = Myrad.getLinkedRef()
		FlightRoute.setValue(FlightNumber)

		Myrad.SetHeadTracking(false)
		registerForAnimationEvent(Myrad, "DragonLandEffect")
		registerForAnimationEvent(Myrad, "DragonForcefulLandEffect")
		SFXmyradSoftHowl.play(Myrad)
		MyradIsFlying.setValue(50)
		
		; -------------------------- Are-we-sitting-and-flying-at-the-same-time-again???-failsave(TM)
			if Myrad.GetFlyingState() != 0 && Myrad.getSitState() != 0 
				Myrad.setPosition(Myrad.getPositionX(), Myrad.getPositionY(), Myrad.getPositionZ() +256)
				debug.sendAnimationEvent(Myrad, "FlyStartHover")
				Myrad.SetAllowFlying(false)
				Myrad.SetForcedLandingMarker(Home)
				Myrad.SetAllowFlying(false)
				wait(3)
			endif
		; -----------------------------------------------------------------------------------------

		myrad.evaluatePackage()
		EVENTleaveFurniture.forceRefTo(Myrad)
		LandingApproach = false

ENDfunction 


Function MountMyrad()
{Called by: onActivate, _00E_A0_MyradFlightAliasMyrad}
	SetInChargen(true, true, true)
	ReturnAV = player.getAv("Health")
	player.modAv("health", 1000000)
	IMODfadeOut.apply()
	wait(1.5)
		ForceThirdPerson()
	IMODtakeOffDOF.apply()
	ForceThirdPerson()
	Myrad.PlayIdleWithTarget(groundMountDragonIdle, player)
	wait(4)
	
	shakeCamera(afStrength = 1, afDuration = 1)
	IMODtakeOff.apply()
	SFXmyradSoftHowl.play(Myrad)
	SFXflyBy.play(myrad)
	wait(1)
	
	shakeCamera(afStrength = 0.5, afDuration = 2)
	SFXmyradRoadDistant.play(player)
	wait(2.5)
	
	IMODfadeOut.apply()
	wait(1.5)
	
	IMODpitchBlack.apply()
	IMODflying.apply()
	CameraFXstart.Play(myrad, 3)
	CameraFXstart02.Play(myrad, 2)
	DELAYrefreshCameraFX = getCurrentRealTime() +5
	myrad.disable(0)
	myrad.SetAllowFlying(true)
	myrad.enable(0)
	wait(0.2)

	myrad.setAlpha(0)
	player.setAlpha(0)
	wait(0.2)
	
	MyradIsFlying.setValue(100)
	EVENTleaveFurniture.clear()
	myrad.evaluatePackage()
	debug.sendAnimationEvent(player, "IdleCartExitInstant")
	player.setAngle(0, 0, player.getHeadingAngle(Myrad)+player.getAngleZ()) 
	wait(0.25)
	
	player.SetAllowFlying(true)
	player.SetVehicle(myrad as objectreference)
	debug.sendAnimationEvent(player, "IdleCartTravelPlayerEnterInstant")
	SetCameraTarget(Myrad)
	FadeOutGame(false, true, 0, 3)
	IMODtakeOff.apply()
	EVENTlandingApproach.forceRefTo(Myrad)
	Myrad.evaluatePackage()

	registerForUpdate(0.5)
	loading()
ENDfunction

bool isLoading
EVENT OnUpdate()
	if myrad.GetFlyingState() != 0 && myrad.GetFlyingState() != 4 && isLoading == False
		if getCurrentRealTime() > DELAYrefreshCameraFX
			CameraFXloop.Play(myrad, 11)
			DELAYrefreshCameraFX = getCurrentRealTime() +10
		endif
		player.translateTo(Myrad.getPositionX() +4096 * Math.sin(Myrad.getAngleZ()), Myrad.getPositionY() +4096 * Math.cos(Myrad.getAngleZ()),Myrad.getPositionZ() +1024,0,0,0, 3000)
	elseif myrad.GetFlyingState() == 4
		If LandingApproach == false
			CameraFXloop.stop(myrad)
			registerForAnimationEvent(Myrad, "DragonLandEffect")
			registerForAnimationEvent(Myrad, "DragonForcefulLandEffect")
			IMODtakeOff.apply()
			LandingApproach = true
		endif
		while SoundID01volume > 0 
			SoundID01volume = SoundID01volume -(0.025)
			Sound.SetInstanceVolume(SoundID01, SoundID01volume)
			Sound.SetInstanceVolume(SoundID02, SoundID01volume)
		endWhile
	elseif myrad.GetFlyingState() == 0
	endif
ENDevent


EVENT onAnimationEvent(objectReference deliverator, string eventName)
		if (eventName == "DragonLandEffect")
			Myrad.PlayImpactEffect(FXDragonTakeoffImpactSet, "NPC Pelvis", 0, 0, -1, 512)
			if MyradIsFlying.getValue() == 100
				SFXflyBy.play(myrad)
				shakeCamera(afStrength = 1, afDuration = 1.5)
				IMODtakeLand.apply()
				IMODflying.remove()
				DismountMyrad()
				unregisterForAnimationEvent(Myrad, "DragonLandEffect")
			else
				shakeCamera(afStrength = 1, afDuration = 0.45)
				unregisterForAnimationEvent(Myrad, "DragonLandEffect")
			endif
		elseIf (eventName == "DragonForcefulLandEffect")
			Myrad.PlayImpactEffect(FXDragonLandingImpactSet, "NPC Pelvis", 0, 0, -1, 512)
			if MyradIsFlying.getValue() == 100
				SFXflyBy.play(myrad)
				shakeCamera(afStrength = 1, afDuration = 1.5)
				IMODtakeLand.apply()
				IMODflying.remove()
				DismountMyrad()
				unregisterForAnimationEvent(Myrad, "DragonForcefulLandEffect")
			else
				shakeCamera(afStrength = 1, afDuration = 0.45)
				unregisterForAnimationEvent(Myrad, "DragonForcefulLandEffect")
			endif
		elseIf (eventName == "DragonTakeoffEffect")
			Myrad.PlayImpactEffect(FXDragonTakeoffImpactSet, "NPC Tail8", 0, 0, -1, 2048)
			shakeCamera(afStrength = 1, afDuration = 0.5)
			SFXmyradSoftHowl.play(Myrad)
			SFXflyBy.play(myrad)
			myrad.placeAtme(PuffoffDragonSmoke)
			IMODtakeLand.apply()
			unregisterForAnimationEvent(Myrad, "DragonTakeoffEffect")
		endif
ENDevent 

Function DismountMyrad()
		unregisterForUpdate()
		if CamSpawn == none
			Camera = Myrad.placeAtme(game.getForm(0x00032368)) as actor
			wait(0.1)
			Camera.Setposition(Myrad.getPositionX() +500 * Math.sin(Myrad.getAngleZ()), Myrad.getPositionY() +500 * Math.cos(Myrad.getAngleZ()), Myrad.getPositionZ())
		else
			Camera = CamSpawn.placeAtme(game.getForm(0x00032368)) as actor
			wait(0.1)
			Camera.setAngle(CamSpawn.getAngleX(), CamSpawn.getAngleY(), CamSpawn.getAngleZ())
		endif

		if PlayerGoal == none
			PlayerLure = Myrad.placeAtme(game.getForm(0x000bfb04))
			wait(0.1)
			PlayerLure.Setposition(Myrad.getPositionX() +600 * Math.sin(Myrad.getAngleZ()), Myrad.getPositionY() +600 * Math.cos(Myrad.getAngleZ()), Myrad.getPositionZ())
		else
			PlayerLure = PlayerGoal.placeAtme(game.getForm(0x000bfb04))
		endif
		
		player.SetAllowFlying(false)
		player.SetVehicle(none)
		registerForAnimationEvent(Myrad, "DragonTakeoffEffect")
		Sound.StopInstance(SoundID01)
		Sound.StopInstance(SoundID02)
		wait(0.5)
		
		IMODfadeOut.apply()
		wait(1.5)
		
		IMODpitchBlack.apply()

		player.setPosition(Myrad.getPositionX() +150 * Math.sin(Myrad.getAngleZ()), Myrad.getPositionY() +150 * Math.cos(Myrad.getAngleZ()), Myrad.getPositionZ())			;LZplayerSpawn
		debug.sendAnimationEvent(player, "IdleCartExitInstant")
		player.setAlpha(1)
		Myrad.setAlpha(1)
		game.DisablePlayerControls(abMovement = true, abFighting = true, abCamSwitch = true, \
		abLooking = true, abSneaking = true, abMenu = true, abActivate = true, abJournalTabs = true)
		SetCameraTarget(camera)
		wait(0.25)
		
	if RollerCoaster == true
		Myrad.SetHeadTracking(true)
		Myrad.setLooKAt(player)
		PreparePlayerToGetKicked()
	else
		if Camera.hasLOS(player) == 0
			Camera.setAngle(Camera.getAngleX(), Camera.getAngleY(), Camera.getHeadingAngle(player) +Camera.getAngleZ())
		endif
		FadeOutGame(false, true, 0, 0.5)
		IMODpitchBlack.remove()
		
		Camera.translateTo(Camera.getPositionX(), Camera.getPositionY(), Camera.getPositionZ() -(64), 0,0, Camera.getHeadingAngle(player)+Camera.getAngleZ(), 2)
		setplayerAiDriven(true)
		
		EVENTplayerWalkAway.forceRefTo(player)

		player.evaluatePackage()
		checkPos = player.getPositionX()
		ForceMovement()
	endif
ENDfunction


float checkpos
; ----------------------------------------------------------- Rollercoaster End
function PreparePlayerToGetKicked()
		if player.getSitState() == 0
			Debug.sendAnimationEvent(player, "IdleWebEnterInstant")
			player.setActorValue("Variable03", 5)
			wait(0.25)
			PreparePlayerToGetKicked()
		else
			ThrowOffPlayer()
		endif
ENDfunction

function ThrowOffPlayer()
	if player.playidle(IdleWebExit) == 0
		player.playidle(IdleWebExit)
		wait (0.15)
		ThrowOffPlayer()
	else
		if Camera.hasLOS(player) == 0
			Camera.setAngle(Camera.getAngleX(), Camera.getAngleY(), Camera.getHeadingAngle(player) +Camera.getAngleZ())
		endif
		SFXplayerDrop.play(player)
		FadeOutGame(false, true, 0, 0.5)
		IMODpitchBlack.remove()
		wait(0.15)
		shakeCamera(afStrength = 0.45, afDuration = 0.5)
		player.placeAtme(PuffoffSmoke)
		player.placeAtme(PuffoffSmoke)
		wait(0.15)
		Camera.translateTo(Camera.getPositionX(), Camera.getPositionY(), Camera.getPositionZ() -(64), 0,0, Camera.getHeadingAngle(player)+Camera.getAngleZ(), 2)

		player.setLooKAt(player)
		wait(0.25)
		WaitForDropIdleToFinishAndFunctionNamesAreGettingBetter()
	endif
ENDfunction

function WaitForDropIdleToFinishAndFunctionNamesAreGettingBetter()
		if player.getSitState() == 0
			player.setActorValue("Variable03", 0)
			GoDizzy()
		else
			wait(0.25)
			WaitForDropIdleToFinishAndFunctionNamesAreGettingBetter()
		endif
ENDfunction

function GoDizzy()
		if player.playidle(IdleDrunk) == 0
			player.playidle(IdleDrunk)
			debug.notification("DRUNK")
			wait(0.15)
			GoDizzy()
		else
			setplayerAiDriven(true)
			EVENTplayerWalkAway.forceRefTo(player)
			player.evaluatePackage()
			checkPos = player.getPositionX()
			ForceMovement()
		endif
ENDfunction
; -----------------------------------------------------------

function ForceMovement()
		if player.getPositionX() == checkPos
			player.evaluatePackage()
			wait(0.25)
			ForceMovement()
		else
			if RollerCoaster == true
				wait(3)
			else
				wait(0.5)
				Myrad.SetHeadTracking(true)
				Myrad.setLooKAt(player)
				wait(1.25)
			endif

			FlightRoute.setValue(0)
			MyradIsFlying.setValue(0) 
			EVENTreturnHome.forceRefTo(Myrad)
			Myrad.evaluatePackage()
			Myrad.clearLookAt()
			
			wait(0.75)

			gotoState("FIN")
			
			if RollerCoaster == true
				DELAYcinematicEnd = getCurrentRealTime() +7
			else
				DELAYcinematicEnd = getCurrentRealTime() +5
			endif
			registerForUpdate(0.25)
		endif 
ENDfunction

function Loading()

		isLoading = true
		player.SetAllowFlying(false)
		player.SetVehicle(none)
		SetCameraTarget(player)
		myrad.disable(0)
		wait(0.1)
		SFXmyradRoadDistant.play(player)
		player.moveTo(ApproachBoogie)
		SFXmyradRoadDistant.play(player)
		wait(0.1)
		game.DisablePlayerControls(abMovement = true, abFighting = true, abCamSwitch = true, \
		abLooking = true, abSneaking = true, abMenu = true, abActivate = true, abJournalTabs = true)
		SetHudCartMode(true)
		Myrad.moveTo(ApproachBoogie)
		wait(0.1)
		myrad.enable(0)
		SetCameraTarget(Myrad)
		myrad.setAlpha(0)
		myrad.evaluatePackage()
		FadeOutGame(false, true, 2, 3.5)
		IMODtakeOff.apply()
		IMODpitchBlack.remove()
		wait(0.2)
		myrad.evaluatePackage()
		isLoading = false
		SoundID01volume = 1
		SoundID02 = SFXwindLP.Play(Myrad)
		Sound.SetInstanceVolume(SoundID02, SoundID01volume)

ENDfunction

state FIN
	Event OnUpdate()
		if getCurrentRealTime() > DELAYcinematicEnd || player.getDistance(playerLure) < 200
			unregisterforUpdate()
			
			gotoState ("NULL")
			IMODfadeOut.apply()
			SFXmyradRoadDistant.play(Myrad)
			player.ResetHealthAndLimbs()
			wait(1.5)
			EVENTplayerWalkAway.clear()
			player.FORCEAV("health", ReturnAV)
			SetCameraTarget(player)
			enableplayercontrols()
			SetHudCartMode(false)
			SetInChargen(false, true, false)
			setplayerAiDriven(false)
			debug.sendAnimationEvent(player, "IdleCartExitInstant")
			EVENTplayerWalkAway.clear()
			wait(0.25)
			
			CamSpawn = none
			PlayerGoal = none
			camera.delete()
			PlayerLure.delete()

		endif
	ENDevent
ENDstate