'-----------------------------------------------------------
'
'					FLTSIM2D
'
'Longitudinal (2-D: Z & X) flight simulator 
'for Atari 800XL 2017 8-bit BASIC 10-Liner
'
'Jeff Piepmeier
'March 4, 2017
'http://jeffpiepmeier.blogspot.com/
'http://github.com/jeffpiep/
'
'Parsed with TurboBASIC XL Parser Tool 
'http://github.com/dmsc/tbxl-parser
$options +optimize, optimize=-convert_percent-const_replace, optimize=+const_folding
'Tested on Altirra
'http://www.virtualdub.org/altirra.html
'-----------------------------------------------------------

DIM K$(1)
DT = .1 : REM (S) TIME STEP 

CLS : REM CLEAR THE SCREEN
?
?,"FLIGHT SIMULATOR 2D"
POKE 752,1 : REM TURN OFF CURSOR

REPEAT
BASETIME = TIME

REM for idx=1 to 2

REM 'ATMOSPHERE 
REM SIGMA=(1-Z*8.0E-05) : REM LINEARY APPROX FOR RELATIVE AIR DENSITY
REM RHOZ = 1.225*SIGMA : REM AIR DENSITY AT ALTITUDE
REM STALL = ((U-2*FLAPS)>29) ! (Z<1) : REM DETERMINE IF NOT STALLED
REM QSW = 8.1*(U*U+W*W)*RHOZ : REM DYNAMIC PRESSURE * WING AREA

REM 'ANGLE OF ATTACK(S)
REM UNOSING = 1/(U + (U=0)) : REM U != 0
REM SLOPE = W*UNOSING : REM SLOPE OF WIND
REM SLOPE = -(SLOPE<=-1) + (SLOPE>-1)*SLOPE
REM SLOPE = (SLOPE>=1) + (SLOPE<1)*SLOPE
REM ALPHA = SLOPE - .22 * SLOPE^3 : REM WING ANGLE OF ATTACK WRT WIND
REM ALPHAT = ALPHA + OMEGA*UNOSING*4.3 + 8.33E-3*DLTA + .0863*CL - .0873 : REM TAILPLANE AOA

REM 'LIFT & DRAG COEFFICIENTS
REM CLT = .4*ALPHAT -.24*ALPHAT^3 : REM TAILPLANE LIFT COEFFICIENT WITH AREA RATIO
REM CL = 0.3+0.16*FLAPS+4.8*ALPHA+12*ALPHA*ABS(ALPHA)-46*ALPHA^3 : REM WING LIFT COEFFICIENT
REM CL = CL * STALL 
REM CLL = CL+CLT
REM CD = .025 + .0575*CLL*CLL : REM DRAG COEFFICIENT USING OSWALD EFFICIENCY

REM 'ENGINE WITH DROPOFF DUE TO ALTITUDE
REM THRUST=(SIGMA-.05)*THROTTLE*UNOSING*1100 : REM INVERT PROPULSIVE POWER EQUATION
REM THRUST=THRUST*(THRUST<=2000)+2000*(THRUST>2000) : REM LIMIT THRUST TO 2000 N

REM 'TIME STEP EQUATIONS OF MOTION
REM MY = -QSW*(.0308 + CL*(.28-0.1*FLAPS) + CLT*4.3) : REM COMPUTE PITCHING MOMENT, + IS NOSE UP
REM OMEGA = OMEGA + MY*5.48e-5
REM OMEGA = OMEGA * ((Z>.01) ! (OMEGA>0)) : REM NO ROTATION UNLESS POSITIVE WHEN ON GROUND
REM U = U + ( (THRUST-QSW*CD)*1E-3 - SINTH*9.81 - OMEGA*W ) * DT
REM 'U = U * (U>0) : REM FORCE U TO BE POSITIVE. CANNOT GO BACKWARDS OR DO A LOOP  
REM W = W + (   -QSW*CLL*1E-3 + COSTH*9.81 + OMEGA*U ) * DT
REM W = W * ((Z>.01) ! (W<0)) : REM NO VERTICAL VELOCITY IF ON GROUND, UNLESS ITS UP

REM 'INERTIAL FRAME UPDATE
REM T=T+DT
REM THETA = THETA + OMEGA*DT
REM COSTH = 1 - 0.49*THETA*THETA
REM SINTH = THETA -.15*THETA^3
REM VX = (U*COSTH + W*SINTH)
REM VZ = (U*SINTH - W*COSTH)
REM X = X + VX*DT
REM Z = Z + VZ*DT
REM Z = Z * (Z>0) : REM RESTRICT X TO ABOVE GROUND

REM next idx

REM OKTOLAND = OKTOLAND ! (Z>9) : REM BE A LITTLE FORGIVING ON THE TAKEOFF

REM 'PILOT INPUT
REM K$=INKEY$
REM THROTTLE = THROTTLE + 5*((K$="\1F")*(THROTTLE<100)-(K$="\1E")*(THROTTLE>0))
REM 'THROTTLE = THROTTLE + (100-THROTTLE)*((K$="/") & (THROTTLE=0)) - THROTTLE*((K$="/") & (THROTTLE>0))
REM DLTA = DLTA + 0.5*((K$="T")*(DLTA<23)-(K$="B")*(DLTA>-28))
REM FLPIN = FLPIN + ((K$="N")*(FLPIN<3)-(K$="Y")*(FLPIN>0))
REM FLAPS = 0.05*FLPIN+0.95*FLAPS : REM A LITTLE IIR EXPONENTIAL RESPONSE TO MODEL THE FLAP DRIVE MOTOR



POSITION 2,3
?"KTAS",INT(U*1.94)," "
?"PITCH",INT(THETA*573)*.1," "
?"ALT.",INT(Z*3.28)," "
?"VRATE",INT(VZ*197)," "
?
?"ELEV.", DLTA," "
?"POWER", THROTTLE," "
?"FLAPS ", INT(FLAPS*10+0.5)," "
?
?"STALL ", CHR$(161-116*STALL);
'?CHR$(253-221*STALL)
?
?"DIST.", INT(X*1.09)," "
'?"TIME",T,,INT(600/(TIME-BASETIME));"% "
?"TIME",T,,INT(100*(TIME-BASETIME)/6);" MS"


UNTIL OKTOLAND & (Z=0) 
?
IF VZ >-1
	? "TOUCHDOWN"
ELSE
	? "YOU CRASHED!\FD\FD\FD"
ENDIF
