Begin A1_DAT_SRT_CoE_App

Short State				; 0 = init, 1 = approach X, 2 = approach Y, 3 = arrive
                                   ; 4 = wait for z-fall-down and restore menus, -1 = done

Float PosX				; target X, Y, Z, angle
Float PosY
Float PosZ
Float Angle

Float Timer                   ; times z-fall-down

Float CurX				; current X location
Float CurY				; current Y location

Float DeltaX				; X distance
Float DeltaY				; Y distance

Short Moves			; cell moves done
Short PrevMoves		; cell moves ( of the previous frame)
Short TotalMoves		; total cell moves to go
Short TempMoves       ; temporary

; show progress
If ( Moves != PrevMoves )
	Set TempMoves to ( TotalMoves / 4 )
If ( Moves == TempMoves )
	ToggleMenus
	MessageBox "Loading: 40 percent done"
	ToggleMenus
EndIf

Set TempMoves to ( totalMoves / 2 )

If ( Moves == TempMoves )
	ToggleMenus
	MessageBox "Loading: 60 percent done"
	ToggleMenus
EndIf 

Set TempMoves to ( TotalMoves / 1.33 )

If ( Moves == TempMoves )
	ToggleMenus
	MessageBox "Loading: 80 percent done"
	ToggleMenus
	EndIf 
EndIf

Set PrevMoves to Moves

If ( state == 0 )
	Set PosX to "A1_DAT_SRT_RecPosX"
	Set PosY to "A1_DAT_SRT_RecPosY"
	Set PosZ to "A1_DAT_SRT_RecPosZ"
	Set Angle to "A1_DAT_SRT_TeleAngle"

	Player->SetPos, Z, posZ
	Player->SetAngle, Z, angle
 
	Set CurX to ( Player->GetPos, X )
	Set CurY to ( Player->GetPos, Y )

If ( PosX > CurX )
	Set DeltaX to ( PosX - CurX )
Else
	Set DeltaX to ( CurX - PosX )
EndIf

If ( PosY > CurY )
	Set DeltaY to ( PosY - CurY )
Else
	Set DeltaY to ( CurY - PosY )
EndIf

Set TotalMoves to ( ( DeltaX + DeltaY  ) / 8192 )
Set PrevMoves to 0
Set Moves to 0

Set State to 1

ElseIf ( State == 1 )
	If ( CurX > ( PosX + 8192 ) )
		Set CurX to ( CurX - 8192 )
		Player->SetPos, X, CurX
		Set Moves to ( Moves + 1 )
ElseIf ( CurX < ( PosX - 8192 ) )
	Set CurX to ( CurX + 8192 )
	Player->SetPos, X, curX
	Set Moves to ( Moves + 1 )
Else
	Set State to 2
EndIf

ElseIf ( State == 2 )
	If ( CurY > ( PosY + 8192 ) )
		Set CurY to ( CurY - 8192 )
		Player->SetPos, Y, CurY
		Set Moves to ( Moves + 1 )
ElseIf ( CurY < ( PosY - 8192 ) )
	Set CurY to ( CurY + 8192 )
	Player->SetPos, Y, CurY
	Set Moves to ( Moves + 1 )
Else
	Set State to 3
EndIf

ElseIf ( State == 3 )  	
	Player->SetPos, X, PosX
	Player->SetPos, Y, PosY
	Player->SetPos, Z, PosZ
	Player->SetAngle, Z, Angle
	ToggleMenus
	MessageBox "Loading: 100 percent done"
	ToggleMenus
  
Set Timer to 0
Set State to 4

; we have generically teleported
ElseIf ( state == 4 )
	; wait a second while the character (normally) falls down
	Set Timer to ( Timer + GetSecondsPassed )
If ( Timer > 1 )
	; restore godmode and menus
	ToggleGodMode
	ToggleMenus
	MenuTest
	MenuTest
	FadeIn 0.25
	Set State to -1
EndIf

Else ; State == -1
	Set State to 0
	StopScript "A1_DAT_SRT_CoE_App"
EndIf

End