'flightcombat_chung.bas by NGUYEN.Chung freeware (2011)    
'
#Define guinogfx 'dont use freebasic gfx
#Ifndef GL_BGRA
#Define GL_BGRA GL_RGBA
#EndIf
#Include Once "gui_chung.bi"
#Include Once "gl_chung.bi"
#Include once "md2loader.bi"
#include Once "win/mmsystem.bi"

#Inclib "./loadobj_chung"
Declare Function loadobj Cdecl Alias "loadobj" (Byval ficload As ZString Ptr,ByVal textlist As ZString Ptr,_ 
         Byval defauttext As ZString Ptr) As Integer        
Declare Function loadobjsize Cdecl Alias "loadobjsize" (Byval ficload As ZString Ptr,ByVal textlist As ZString Ptr,_ 
         Byval defauttext As ZString Ptr, ByVal size As uint) As Integer 'returns resize scale*1000        

'SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS )    
Sub notice(ByRef msg As string,ByRef title As String ="notice")
	messagebox(0,msg,title,MB_TASKMODAL Or 0)
End Sub
Sub confirm(ByRef msg As string,ByRef title As string,ByRef resp As String)
	Dim i As Integer
	i=messagebox(0,msg,title,MB_TASKMODAL Or 1)
	If i=1 Then resp="yes" Else resp="no"
End Sub
Sub initsounds
	Dim As String soundfic
   soundfic="sounds/nature.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias nature",0,0,0)
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias nature2",0,0,0)
   soundfic="sounds/crash.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias crash",0,0,0)
   soundfic="sounds/pneu.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias pneu",0,0,0)
   soundfic="sounds/moteur.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias moteur",0,0,0)
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias moteur2",0,0,0)
   soundfic="sounds/horse.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias horse",0,0,0)
   soundfic="sounds/explosion_1.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias explosion1",0,0,0)
   soundfic="sounds/waterwave.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias waterwave",0,0,0)
   soundfic="sounds/ocean.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias ocean",0,0,0)
   soundfic="sounds/shoot.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias shoot",0,0,0)
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias shoot1",0,0,0)
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias shoot2",0,0,0)
   soundfic="sounds/subwater.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias subwater",0,0,0)
   soundfic="sounds/explosion_2.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias explosion21",0,0,0)
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias explosion22",0,0,0)
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias explosion23",0,0,0)
   soundfic="sounds/aah.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias aah",0,0,0)
   soundfic="sounds/boom.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias boom",0,0,0)
   soundfic="sounds/order1.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias order1",0,0,0)
   soundfic="sounds/order2.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias order2",0,0,0)
   soundfic="sounds/order3.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias order3",0,0,0)
   soundfic="sounds/order4.mp3"
   mcisendstring("open "+chr$(34)+soundfic+chr$(34)+" shareable alias order4",0,0,0)
End Sub 
Sub closesounds
   mcisendstring("close avion",0,0,0)
   mcisendstring("close avion2",0,0,0)
	mcisendstring("close nature",0,0,0)
	mcisendstring("close nature2",0,0,0)
	mcisendstring("close crash",0,0,0)
	mcisendstring("close pneu",0,0,0)
	mcisendstring("close moteur",0,0,0)
	mcisendstring("close moteur2",0,0,0)
	mcisendstring("close horse",0,0,0)
	mcisendstring("close explosion1",0,0,0)
	mcisendstring("close waterwave",0,0,0)
	mcisendstring("close ocean",0,0,0)
	mcisendstring("close shoot",0,0,0)
	mcisendstring("close shoot1",0,0,0)
	mcisendstring("close shoot2",0,0,0)
	mcisendstring("close subwater",0,0,0)
	mcisendstring("close explosion21",0,0,0)
	mcisendstring("close explosion22",0,0,0)
	mcisendstring("close explosion23",0,0,0)
	mcisendstring("close aah",0,0,0)
	mcisendstring("close boom",0,0,0)
	mcisendstring("close order1",0,0,0)
	mcisendstring("close order2",0,0,0)
	mcisendstring("close order3",0,0,0)
	mcisendstring("close order4",0,0,0)
End Sub 
Dim Shared As Single soundvol=3
Sub setsoundvol
	Dim As Integer vol=Int(min(1000.0,2500*soundvol*0.05))
	mcisendstring("setaudio nature volume to "+str(vol),0,0,0)
	mcisendstring("setaudio nature2 volume to "+str(vol),0,0,0)
	mcisendstring("setaudio crash volume to "+str(vol),0,0,0)
	mcisendstring("setaudio pneu volume to "+str(vol),0,0,0)
	mcisendstring("setaudio moteur volume to "+str(vol),0,0,0)
	mcisendstring("setaudio moteur2 volume to "+str(vol),0,0,0)
	mcisendstring("setaudio horse volume to "+str(vol),0,0,0)
	mcisendstring("setaudio explosion1 volume to "+str(vol),0,0,0)
	mcisendstring("setaudio waterwave volume to "+str(vol),0,0,0)
	mcisendstring("setaudio ocean volume to "+str(vol),0,0,0)
	mcisendstring("setaudio shoot volume to "+str(Int(1.12*vol)),0,0,0)
	mcisendstring("setaudio shoot1 volume to "+str(Int(1.12*vol)),0,0,0)
	mcisendstring("setaudio shoot2 volume to "+str(Int(1.12*vol)),0,0,0)
	mcisendstring("setaudio subwater volume to "+str(vol),0,0,0)
	mcisendstring("setaudio explosion21 volume to "+str(vol),0,0,0)
	mcisendstring("setaudio explosion22 volume to "+str(vol),0,0,0)
	mcisendstring("setaudio explosion23 volume to "+str(vol),0,0,0)
	mcisendstring("setaudio aah volume to "+str(Int(1.4*vol)),0,0,0)
	mcisendstring("setaudio boom volume to "+str(Int(1.4*vol)),0,0,0)
	mcisendstring("setaudio order1 volume to "+Str(vol),0,0,0)
	mcisendstring("setaudio order2 volume to "+Str(vol),0,0,0)
	mcisendstring("setaudio order3 volume to "+Str(vol),0,0,0)
	mcisendstring("setaudio order4 volume to "+Str(vol),0,0,0)
End Sub 
Dim Shared As Integer oceansound,naturesound
Sub soundnature
   naturesound=1
   mcisendstring("set nature time format ms",0,0,0)
   mcisendstring("set nature speed 550",0,0,0)
   mcisendstring("set nature2 time format ms",0,0,0)
   mcisendstring("set nature2 speed 550",0,0,0)
   mcisendstring("play nature from 4000 repeat",0,0,0)
   mcisendstring("play nature2 from 0 repeat",0,0,0)
End Sub
Sub stopsoundnature
	naturesound=0
   mcisendstring("stop nature",0,0,0)
   mcisendstring("stop nature2",0,0,0)
End Sub
Sub soundocean
	oceansound=1
   mcisendstring("play ocean from 0 repeat",0,0,0)
End Sub
Sub stopsoundocean
	oceansound=0
   mcisendstring("stop ocean",0,0,0)	
End Sub
Sub soundavion
   mcisendstring("set avion time format ms",0,0,0)
   mcisendstring("set avion speed 550",0,0,0)
   mcisendstring("set avion2 time format ms",0,0,0)
   mcisendstring("set avion2 speed 550",0,0,0)
   mcisendstring("play avion from 4000 repeat",0,0,0)
   mcisendstring("play avion2 from 0 repeat",0,0,0)
End Sub 
Sub stopsoundavion
   mcisendstring("stop avion",0,0,0)
   mcisendstring("stop avion2",0,0,0)
End Sub
Dim Shared As Single tpneu
Sub soundpneu
Dim As Single ttime
   ttime=Timer 
   If tpneu>ttime Then tpneu=ttime 'if midnight
   If ttime>(tpneu+0.08) Then 
   	tpneu=ttime 
   	mcisendstring("play pneu from 0",0,0,0)
   EndIf
End Sub
Sub soundcrash
   mcisendstring("play crash from 0",0,0,0)
End Sub
Sub soundhorse
   mcisendstring("play horse from 0",0,0,0)
End Sub
Sub soundexplosion1
   mcisendstring("play explosion1 from 0",0,0,0)
End Sub
Dim Shared As Single twater 
Sub soundwaterwave
If twater>Timer Then twater=Timer 'if midnight
If Timer>(twater+0.7) Then
  	twater=Timer
   mcisendstring("play waterwave from 0",0,0,0)
EndIf 
End Sub
Sub soundshoot
	mcisendstring("play shoot from 0",0,0,0)	
End Sub
Dim Shared As Integer isoundshoot
Sub soundshoot2
isoundshoot+=1
If isoundshoot>2 Then isoundshoot=1
Select Case isoundshoot
	Case 1
		mcisendstring("play shoot1 from 0",0,0,0)
	Case 2
		mcisendstring("play shoot2 from 0",0,0,0)
End Select
End Sub
Dim Shared As Single tsubwater
Sub soundsubwater
If tsubwater>Timer Then tsubwater=Timer 'if midnight
If Timer>(tsubwater+0.7) Then
  	tsubwater=Timer
   mcisendstring("play subwater from 0",0,0,0)
EndIf 
End Sub
Dim Shared As Integer iexplosion2
Sub soundexplosion2
iexplosion2+=1
If iexplosion2>3 Then iexplosion2=1
Select Case iexplosion2
	Case 1
   mcisendstring("play explosion21 from 0",0,0,0)
	Case 2
   mcisendstring("play explosion22 from 0",0,0,0)
	Case 3
   mcisendstring("play explosion23 from 0",0,0,0)
End Select
End Sub
Sub soundaah
   mcisendstring("play aah from 0",0,0,0)
End Sub
Sub soundboom
   mcisendstring("play boom from 0",0,0,0)
End Sub
Sub soundorder1
   mcisendstring("play order1 from 0",0,0,0)
End Sub
Sub soundorder2
   mcisendstring("play order2 from 0",0,0,0)
End Sub
Sub soundorder3
   mcisendstring("play order3 from 0",0,0,0)
End Sub
Sub soundorder4
   mcisendstring("play order4 from 0",0,0,0)
End Sub
initsounds
setsoundvol
soundnature

Declare Function getterrainheight(ByVal x As Single,ByVal y As Single) As Single
Declare Sub initterrain2
Declare Sub initterrain3
Declare Sub movehorses
Declare Sub drawavion
Declare Sub addtir(ByVal itypetir As Integer=0)
Declare Sub addtir2(ByVal itypetir As Integer=0)
Declare Sub rotavion(ByVal x As Single,ByVal y As Single,ByVal z As Single)
Declare Sub addsmoke(ByVal mx As Single,ByVal my As Single,ByVal mz As Single,ByVal itype As Integer=0)
Declare Sub addboom(ByVal mx As Single,ByVal my As Single,ByVal mz As Single,ByVal itype As Integer=0)
Declare Sub addtirairship(ByVal i As Integer,ByVal itypetir As Integer=0)
Declare Function testtirtriangle(ByVal tx1 As Single,ByVal ty1 As Single,ByVal tz1 As Single, _
	                      ByVal tx2 As Single,ByVal ty2 As Single,ByVal tz2 As Single, _
	                      ByVal tx3 As Single,ByVal ty3 As Single,ByVal tz3 As Single, _
	                      ByVal rtir As Single = 512 ) As Integer
Declare Function mytestairport() As Integer  

    Dim Shared As uint pause,ianim,glnormals,quit,restart,fullscreen=1,compas=1    
    Dim Shared As uint soltexture,arbretext(10),tronctext,buissontext(2),volanttext
    Dim Shared As uint cartext,md2horsetext(4),md2zebratext(3),grasstext(10)
    Dim Shared As uint sunsettext,startext,radartext,pistetext,maptexture
    Dim Shared As uint aviontext,helicetext,feutext,md2iavion,maptext,boattext,boattext2,boattext3
    Dim Shared As uint windtext,cockpittext1,boattext4,corsairtext,firetext,sfiretext
    Dim Shared As uint vg33text,zerotext,zero2text,b25text,alphajettext,spitfiretext,bf109text
    Dim Shared As uint spitfiretext2,p51dtext,cockpittext2,cockpittext3,cockpittext4,f14text
    Dim Shared As Integer n,i,j,k,icar,iciel=6,imap=1,isol=2,igrass=2,imapsol=4   
    Dim Shared As Single time0,time1,time2,time3,nframe,kfps,kfps2,dtime2,maxfps
    Dim Shared As Single mx,my,mz,o1=180,o2,o3,cos1,sin1,cos2,sin2,cos3,sin3,mx0,my0,mz0,yh=17
    Dim Shared As double auxvar,auxvar2,auxvar3,vie=100,vie0=100
    Const pi=ACos(-1),degtorad=pi/180,radtodeg=180/pi
    Dim Shared As Single terrain(-100 To 512+100,-100 To 512+100),waterz
    Dim Shared As Single terrain2(-100 To 512+100,-100 To 512+100)
    Dim Shared As Byte water(-100 To 512+100,-100 To 512+100)
    Dim Shared As vec3_t  terraincolor(-100 To 612,-100 To 612),terraincolor0(-100 To 612,-100 To 612)
Dim Shared As single xmax,ymax
Dim Shared buttons As Integer :Dim Shared As Single ax,ay,az,at,hatx,haty
Dim Shared As Integer bb=0,b(16),b2(16),tjoy(16)
For i=1 To 16:b(i)=0:b2(i)=i:Next  
Dim Shared As Single joy,tdx,tdy,tdz,joy1dx,joy1dy,joy1dz,joy1dr
Dim Shared As Single timehour,timehour0,timehour1,timehour2,dtimehour=0
Dim Shared As String msg,resp
Dim Shared As uint narbre2=2500,ismooth=0,nboat2=9
Dim Shared As Single distscale=1.0,kwave=1.0
Dim Shared As Single x1,y1,z1,x2,y2,z2
Dim Shared As Integer startoption=0
Dim Shared As Integer iaux,iship,nship=18,nship2=10
Dim Shared As Single airshipo1(nship),airshipo2(nship),airshipo3(nship),aux,aux2
Dim Shared As Single airshipx(nship),airshipy(nship),airshipz(nship),airshipwar(nship)
Dim Shared As Integer nboat=12,typeship(nship)
Dim Shared As Single boatx(nboat),boaty(nboat),boatz(nboat),boato1(nboat),boato2(nboat),boato3(nboat)
dim shared as Integer order,torder,war,ncloud2=30,ifog=0
#Include "./movecar.bi"

Dim As String ficin
Dim As String ficini="flightcombat_chung.ini"
Dim As Integer File
If FileExists(ficini)=0 Then
	i=messagebox(0,"create "+ficini+" ?","flightcombat_chung",1)
	If i<>1 Then closesounds:End
   file=FreeFile
	Open ficini For Output As #file
	Close #file
EndIf
file=FreeFile
Open ficini For Input As #file
icar=2
If Not Eof(file) Then Line Input #file,ficin:icar=Val(ficin)
imap=1
If Not Eof(file) Then Line Input #file,ficin:imap=Val(ficin)
isol=2
If Not Eof(file) Then Line Input #file,ficin:isol=Val(ficin)
iciel=6
If Not Eof(file) Then Line Input #file,ficin:iciel=Val(ficin)
to1=0:to2=0
If Not Eof(file) Then Line Input #file,ficin:to1=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:to2=Val(ficin)
igrass=2
If Not Eof(file) Then Line Input #file,ficin:igrass=Val(ficin)
viseur=0
If Not Eof(file) Then Line Input #file,ficin:viseur=Val(ficin)
narbre2=2500
If Not Eof(file) Then Line Input #file,ficin:narbre2=Val(ficin)
distscale=1.0
If Not Eof(file) Then Line Input #file,ficin:distscale=Val(ficin)
fullscreen=1
If Not Eof(file) Then Line Input #file,ficin:fullscreen=Val(ficin)
maxfps=32
If Not Eof(file) Then Line Input #file,ficin:maxfps=Val(ficin)
ismooth=0
If Not Eof(file) Then Line Input #file,ficin:ismooth=Val(ficin)
kwave=1.0
If Not Eof(file) Then Line Input #file,ficin:kwave=Val(ficin)
nboat2=9
If Not Eof(file) Then Line Input #file,ficin:nboat2=Val(ficin)
startoption=0
If Not Eof(file) Then Line Input #file,ficin:startoption=Val(ficin)
volant=1
If Not Eof(file) Then Line Input #file,ficin:volant=Val(ficin)
mx=0:my=0:mz=0
If Not Eof(file) Then Line Input #file,ficin:mx=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:my=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:mz=Val(ficin)
o1=0:o2=0:o3=0
If Not Eof(file) Then Line Input #file,ficin:o1=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:o2=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:o3=Val(ficin)
iship=0
For i=1 To nship
	airshipx(i)=i*4500:airshipy(i)=i*4000:airshipz(i)=1500.0 
Next
While Not Eof(file) And ficin<>"finlist"
  iship=min2(nship,iship+1)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipx(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipy(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipz(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipo1(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipo2(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipo3(iship)=Val(ficin)
Wend
ficin=""
order=1
If Not Eof(file) Then Line Input #file,ficin:order=Val(ficin)
ncloud2=30
If Not Eof(file) Then Line Input #file,ficin:ncloud2=Val(ficin)
ifog=0
If Not Eof(file) Then Line Input #file,ficin:ifog=Val(ficin)
nship2=10
If Not Eof(file) Then Line Input #file,ficin:nship2=Val(ficin)
level=1
If Not Eof(file) Then Line Input #file,ficin:level=Val(ficin)
carb=carb0
If Not Eof(file) Then Line Input #file,ficin:carb=Val(ficin)
imapsol=4
If Not Eof(file) Then Line Input #file,ficin:imapsol=Val(ficin)
compas=1
If Not Eof(file) Then Line Input #file,ficin:compas=Val(ficin)
Close #file
carb=max(1000.0,min(carb+1000.0,carb0))
Dim Shared As Single gldistmax
Sub initgl
        /' init OpenGL '/        
	glMatrixMode GL_PROJECTION
	glLoadIdentity
	'              anglevue    xmax/ymax   mxmin,mxmax
	'gluPerspective   48.0,    640.0/480.0,  2.0, 13000.0
	gldistmax=13000
	gluPerspective   48.0,    xmax/ymax,  2.0, gldistmax*25
	glMatrixMode GL_MODELVIEW
	glLoadIdentity
	
	glShadeModel GL_SMOOTH
	glClearColor 0.50, 0.50, 1.0, 0.0
	glClearDepth 1.0
	glEnable GL_DEPTH_TEST
	glDepthFunc GL_LEQUAL
	
	'gldisable gl_cull_face
	glenable gl_lighting
	glenable gl_light0
	glenable gl_normalize
	glnormals=1
   glEnable GL_COLOR_MATERIAL
   glColorMaterial GL_FRONT_and_back,GL_DIFFUSE

	glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST    '' Really Nice Perspective Calculations
	
	Dim LightAmbient(0 to 3) as single => {0.625, 0.625, 0.625, 1.0}   '' Ambient Light Values
	Dim LightAmbient2(0 to 3) as single => {0.7,0.7,0.7, 1.0}   '' Ambient Light Values
	Dim LightAmbient3(0 to 3) as single => {0.1,0.1,0.1, 1.0}   '' Ambient Light Values
	Dim LightDiffuse(0 to 3) as single => {1.0, 1.0, 1.0, 1.0}   '' Diffuse Light Values
	Dim LightPosition(0 to 3) as single => {0.0, 0.0, 2000.0, 1.0}  '' Light Position
	glLightfv GL_LIGHT1, GL_AMBIENT, @LightAmbient(0)   '' Setup The Ambient Light
	glLightfv GL_LIGHT2, GL_AMBIENT, @LightAmbient2(0)   '' Setup The Ambient Light
	glLightfv GL_LIGHT3, GL_AMBIENT, @LightAmbient3(0)   '' Setup The Ambient Light
	'glLightfv GL_LIGHT0, GL_DIFFUSE, @LightDiffuse(0)   '' Setup The Diffuse Light
	'glLightfv GL_LIGHT0, GL_POSITION, @LightPosition(0) '' Position The Light
	'glEnable GL_LIGHT0                                  '' Enable Light One

End Sub

Dim Shared As Single fps,rot
Dim Shared As md2_node Ptr md2_node(100)
Dim Shared As uint md2inode,md2nnode
Function addmd2node(ByVal model As md2_model ptr) As uint 
	If md2nnode>=100 Then Return 0
	md2nnode+=1
   md2_node(md2nnode)  = Callocate (sizeof(md2_node))
	With *md2_node(md2nnode)
      .md2model=model
      .scalex=1
      .scaley=1
      .scalez=1		
	End With
	Return md2nnode
End Function
Sub setnodeposition(ByVal node As md2_node Ptr,ByVal x As Single,ByVal y As Single,ByVal z As single)
	node->x=x:node->y=y:node->z=z
End Sub
Sub setnoderotation(ByVal node As md2_node Ptr,ByVal rx As Single,ByVal ry As Single,ByVal rz As single)
	node->rotx=rx:node->roty=ry:node->rotz=rz
End Sub
Sub setnodescale(ByVal node As md2_node Ptr,ByVal sx As Single,ByVal sy As Single,ByVal sz As single)
	node->scalex=sx:node->scaley=sy:node->scalez=sz
End Sub
Sub setnodetexture(ByVal node As md2_node Ptr,ByVal texture As uint)
	node->texture=texture
End Sub
Sub setanim(ByVal node As md2_node Ptr,ByVal anim As uint)
Dim As uint i
With *node
	If anim<0 Or anim>md2_all Then anim=md2_all
	If anim=md2_random Then i=Int(Rnd*20.9) Else i=anim
	If anim=md2_all Then 
	  .startframe=0
	  .endframe=.md2model ->header.num_frames-1
	  .fps=10
	Else     
	  .startframe=md2_animlist(i).startframe
	  .endframe=md2_animlist(i).endframe
	  .fps=md2_animlist(i).fps
	  'dtime=80*10/(0.01+md2_animlist(i).fps)
	EndIf 
	.startframe=min(.md2model ->header.num_frames-1,.startframe)
	.endframe=min(.md2model ->header.num_frames-1,.endframe)
	.nframe=.startframe
	.anim=anim
End With 
End Sub
Sub setanimationrange(ByVal node As md2_node Ptr,ByVal start As uint,ByVal last As uint,_  
	                   ByVal fps As Single=10)
With *node
 .endframe=min(.md2model->header.num_frames-1,last) 
 .startframe=min(.endframe,start)
 .fps=fps
 .nframe=.startframe
 .anim=md2_other
End With 
End Sub
Dim Shared As uint ntree,narbre
Dim Shared As Single scalex=500,scalez=20,texscale,dxterrain
texscale=800/scalex
Sub drawterrain
Dim As Single x,y
Dim As single i,j,i1,dx=27
dx=27+min(57.0,Int((mz-mzsol0)*0.01*distscale))
dx=Int(min(120.0,dx*max(1.0, distscale*3*narbre2/(narbre2+4*2*ntree))))
dxterrain=dx
For x=max(-356.0,-dx+Int(mx/scalex+10000)-10000) To min(356.0-1,dx+Int(mx/scalex+10000)-10000)
	i=x+256+0.1
	while i<-100:i+=512+200:Wend 
	While i>612.2:i-=512+200:Wend 
   glBegin (GL_TRIANGLE_STRIP)	
	For y=max(-356.0,-dx+Int(my/scalex+10000)-10000) To min(356.0,dx+Int(my/scalex+10000)-10000)
     j=y+256+0.1
	  While j<-100:j+=512+200:Wend 
	  While j>612.2:j-=512+200:Wend 
     glcolor3fv(Cast(glfloat Ptr,@terraincolor(i,j)))
     glTexCoord2f(i/texscale,j/texscale)
 	  glvertex3f((x)*scalex,y*scalex,scalez*terrain(i,j))
 	  i1=i+1:If i1>612.2 Then i1-=512+200
     glcolor3fv(cast(glfloat Ptr,@terraincolor(i1,j)))
     glTexCoord2f((i+1)/texscale,j/texscale)
 	  glvertex3f((x+1)*scalex,y*scalex,scalez*terrain(i1,j))
	Next
	glend()
Next
End Sub
Dim Shared As Single houlex,houley,kwater,houlez(-612*32-200 To 612*32+200),altwater=2200
For houlex=-612*32-200 To 612*32+200
	houlez(houlex)=3.0*(-1.001+Cos(houlex*360*degtorad/32))/scalez
Next
Dim Shared As Single dxwater=10
Sub drawterrain2
Dim As Single x,y,x0,y0
Dim As single i,j,i1,dx=27,j1,dy,dxstep,dystep
Dim As Integer di,nwater
houlex=Int(time2*12) Mod 32 
houley=Int(time2*11) Mod 32 
dx=27+min(57.0,Int((mz-mzsol0)*0.01*distscale))
dx=Int(min(120.0,dx*max(1.0, distscale*3*narbre2/(narbre2+4*2*ntree))))
dxterrain=dx
x0=Int(mx/scalex+10000)-10000
y0=Int(my/scalex+10000)-10000
If mz<(waterz+altwater) And imap>=5 And pause=0 Then
 nwater=0	
 dxwater=Int(20+max(0.0,(mz-waterz)*15/altwater))
 For x=max(-356.0,-dxwater+x0) To min(356.0-1,dxwater+x0)
	i=x+256+0.001
	while i<-100:i+=512+200:Wend 
	While i>612.2:i-=512+200:Wend
	For y=max(-356.0,-dxwater+y0) To min(356.0,dxwater+y0)
     j=y+256+0.001
	  While j<-100:j+=512+200:Wend 
	  While j>612.2:j-=512+200:Wend
	  If water(i,j)=1 Then
	  	  nwater+=1
        terrain(i,j)=terrain(i,j)*0.7+0.3*(-60.0001+kwater*(houlez(i*16+houlex)+houlez(j*16-houley)))	  	
	  EndIf
	Next 
 Next
 nwater=nwater*100/(dxwater*dxwater)
 'kwater=min(4.0,(nwater*nwater*nwater/(365*80000.0)))
 kwater=kwave*min(2.9,(nwater*220*220+nwater*nwater*nwater)/(370*80000.0))
Else 
 kwater=min(0.51,kwave)
 If mzsol0<=(waterz+mzh) Then nwater=400 Else nwater=0	
EndIf
If mz>(mzsol0+1700) Then
	If naturesound=1 Then stopsoundnature
	If oceansound=1 Then stopsoundocean
Else 
   If nwater>220 And kwater>=0.5 Then
	   If oceansound=0 Then soundocean
   Else 
	   If oceansound=1 Then stopsoundocean
   EndIf
   If nwater<240 Then 
   	If naturesound=0 Then soundnature
   Else 
   	If naturesound=1 Then stopsoundnature
   EndIf
EndIf
If Abs(cos1)>Abs(sin1) Then 
 If cos1>0 Then
	dx=dx:dxstep=1
 Else
	dx=-dx:dxstep=-1
 EndIf
 dy=Abs(dx):dystep=1
 For x=max(-356.0,min(356.0-1,-dx+x0)) To max(-356.0,min(356.0-1,dx+x0)) Step dxstep
	i=x+256+0.1
	while i<-100:i+=512+200:Wend 
	While i>612.2:i-=512+200:Wend
 	i1=i+1:If i1>612.2 Then i1-=512+200
	di=4 
   glBegin (GL_TRIANGLE_STRIP)	
	For y=max(-356.0,-dy+y0) To min(356.0,dy+y0)
     j=y+256+0.1
	  While j<-100:j+=512+200:Wend 
	  While j>612.2:j-=512+200:Wend 
     glcolor3fv(Cast(glfloat Ptr,@terraincolor(i,j)))
     glTexCoord2f(i/texscale,j/texscale)
 	  glvertex3f((x)*scalex,y*scalex,scalez*terrain(i,j))
     glcolor3fv(cast(glfloat Ptr,@terraincolor(i1,j)))
     glTexCoord2f((i+1)/texscale,j/texscale)
 	  glvertex3f((x+1)*scalex,y*scalex,scalez*terrain(i1,j))
 	  di+=1
 	  If di>=4 Then 
 	  	di=0
      rotavion(x*scalex-mx,y*scalex-my,scalez*terrain(i,j)-mz)
      If x2<(max(Abs(y2),Abs(z2))-7000) Then y+=8:di=4
 	  EndIf
	Next
	glend()
 Next
Else 
 dx=Abs(dx):dxstep=1
 If sin1>0 Then
	dy=dx:dystep=1
 Else
	dy=-dx:dystep=-1
 EndIf
 For y=max(-356.0,min(356.0-1,-dy+y0)) To max(-356.0,min(356.0-1,dy+y0)) Step dystep
	j=y+256+0.1
	while j<-100:j+=512+200:Wend 
	While j>612.2:j-=512+200:Wend
 	j1=j+1:If j1>612.2 Then j1-=512+200
	di=4 
   glBegin (GL_TRIANGLE_STRIP)	
	For x=max(-356.0,-dx+x0) To min(356.0,dx+x0)
     i=x+256+0.1
	  While i<-100:i+=512+200:Wend 
	  While i>612.2:i-=512+200:Wend 
     glcolor3fv(Cast(glfloat Ptr,@terraincolor(i,j)))
     glTexCoord2f(i/texscale,j/texscale)
 	  glvertex3f((x)*scalex,y*scalex,scalez*terrain(i,j))
     glcolor3fv(cast(glfloat Ptr,@terraincolor(i,j1)))
     glTexCoord2f((i)/texscale,(j+1)/texscale)
 	  glvertex3f((x)*scalex,(y+1)*scalex,scalez*terrain(i,j1))
 	  di+=1
 	  If di>=4 Then 
 	  	di=0
      rotavion(x*scalex-mx,y*scalex-my,scalez*terrain(i,j)-mz)
      If x2<(max(Abs(y2),Abs(z2))-7000) Then x+=8:di=4
 	  EndIf
	Next
	glend()
 Next
EndIf 
End Sub
Sub drawsol
Dim As Single dx=2000,tx=7
	glpushmatrix
   glenable(gl_texture_2d)
	gldisable(gl_lighting)
	If mz<(mzsol0+80) Or imapsol<=1 Then 
	   glcolor3f(0,0.7,0)
		glbindtexture(gl_texture_2d,soltexture)
      texscale=800/scalex
	Else
	   glcolor3f(1.0,1.0,1.0)
		glbindtexture(gl_texture_2d,maptexture)
      texscale=16000/scalex
	EndIf
	'gltexdisk(0,2000)
   'gltexcarre(dx,tx)
	drawterrain2()
	glpopmatrix
End Sub
'Dim Shared As uint ntree,narbre=700
Dim Shared As uint arbrelist,distarbre=5000
narbre=2500:distarbre=5000*Sqr(narbre/700)
narbre2=min2(narbre2,narbre)
Dim Shared As Single arbrex(narbre),arbrey(narbre),arbrez(narbre),arbrerot(narbre),arbrerot0(narbre)
Dim Shared As Single arbrescalex(narbre),arbrescalez(narbre),arbretype(narbre)
Sub drawarbre
If arbrelist<>0 Then
   glcalllist arbrelist
Else
 arbrelist=glgenlists(1)
 glnewlist arbrelist,gl_compile 
 Dim As Single auxy,auxz,scale=2
 auxy=68*scale:auxz=70*scale
 glcolor3f(1,1,1)
 gltexcarre2 auxy,auxz
 'glrotatef( 60,0,0,1)
 'gltexcarre2 auxy,auxz
 gltexcarre2rot auxy,auxz,60
 'glrotatef( 60,0,0,1)
 'gltexcarre2 auxy,auxz
 gltexcarre2rot auxy,auxz,120 
 glbindtexture(gl_texture_2d,tronctext)
 gltexcylindre 1.5*scale,1.2*scale,35*scale,4,4
glendlist
EndIf 
End Sub
Dim Shared As uint nbuisson=35,buissonlist,distbuisson
Dim Shared As Single buissonx(nbuisson),buissony(nbuisson),buissonz(nbuisson),buissonrot(nbuisson),buissonrot0(nbuisson)
Dim Shared As Single buissontype(nbuisson)
Sub drawbuisson
If buissonlist<>0 Then
   glcalllist buissonlist
Else
 buissonlist=glgenlists(1)
 glnewlist buissonlist,gl_compile 
 Dim As Single auxy,auxz,scale=2
 auxy=40*scale:auxz=15*scale
 glcolor3f(1,1,1)
 gltexcarre2 auxy,auxz
 'glrotatef( 60,0,0,1)
 'gltexcarre2 auxy,auxz
 gltexcarre2rot auxy,auxz,60
 'glrotatef( 60,0,0,1)
 'gltexcarre2 auxy,auxz
 gltexcarre2rot auxy,auxz,120
 'glbindtexture(gl_texture_2d,tronctext)
 'gltexcylindre 1.5*scale,1.2*scale,7.5*scale
glendlist
EndIf 
End Sub
Dim Shared As uint nhouse=10,house1list,building3text,rooftext
Dim Shared As Single tx,ty,tz,housex(nhouse),housey(nhouse),housez(nhouse),disthouse
Sub drawhouse1
Dim As Single x=60,y=60,z=60,z1=104
If house1list<>0 Then
 glcalllist house1list
Else
 house1list=glgenlists(1)
 glnewlist house1list,gl_compile	
 glEnable GL_BLEND
 glBlendFunc GL_one_minus_SRC_ALPHA,GL_SRC_ALPHA
 glcolor3f( 0.9,1,1)
 gltexquad  x,y,0, x,0-y,0, x,0-y,z, x,y,z
 gltexquad  0-x,0-y,0, 0-x,y,0, 0-x,y,z, 0-x,0-y,z
 gltexquad  x,0-y,0, 0-x,0-y,0, 0-x,0-y,z, x,0-y,z
 gltexquad  0-x,y,0, x,y,0, x,y,z, 0-x,y,z
 glcolor3f( 240/255,50/255,50/255)
 gldisable GL_BLEND
 glbindtexture(gl_texture_2d,rooftext)
 x+=1:y+=1
 gltextriangle  x,0-y,z, 0,0,z1, 0-x,0-y,z
 gltextriangle  0-x,0-y,z, 0,0,z1, 0-x,y,z
 gltextriangle  0-x,y,z, 0,0,z1, x,y,z
 gltextriangle  x,y,z, 0,0,z1, x,0-y,z
tx=x:ty=y:tz=z1
glendlist
EndIf 
End Sub
Dim Shared As uint nhouse2=10,house2list
Dim Shared As Single house2x(nhouse2),house2y(nhouse2),house2z(nhouse2),disthouse2
Sub drawhouse2
Dim As Single x=50,y=110,z=64,z1=112
If house2list<>0 Then
 glcalllist house2list
Else
 house2list=glgenlists(1)
 glnewlist house2list,gl_compile	
 glEnable GL_BLEND
 glBlendFunc GL_one_minus_SRC_ALPHA,GL_SRC_ALPHA
 glcolor3f( 0.9,1,1)
 gltexquad  x,y,0, x,0-y,0, x,0-y,z, x,y,z
 gltexquad  0-x,0-y,0, 0-x,y,0, 0-x,y,z, 0-x,0-y,z
 gltexquad  x,0-y,0, 0-x,0-y,0, 0-x,0-y,z, x,0-y,z
 gltexquad  0-x,y,0, x,y,0, x,y,z, 0-x,y,z
 glcolor3f( 220/255,40/255,40/255)
 gldisable GL_BLEND
 glbindtexture(gl_texture_2d,rooftext)
 x+=1:y+=1
 gltextriangle  x,0-y,z, 0,0,z1, 0-x,0-y,z
 gltextriangle  0-x,0-y,z, 0,0,z1, 0-x,y,z
 gltextriangle  0-x,y,z, 0,0,z1, x,y,z
 gltextriangle  x,y,z, 0,0,z1, x,0-y,z
tx=x:ty=y:tz=z1
glendlist
EndIf 
End Sub
Sub drawbuilding1(ByVal h As Single)
Dim As Single x=200,y=200,z,z1,tx,ty
z=h:z1=z+20
tx=x/150:ty=z/120
 'glEnable GL_BLEND
 'glBlendFunc GL_one_minus_SRC_ALPHA,GL_SRC_ALPHA
 glenable(gl_texture_2d)
 gldisable(gl_lighting)
 glbindtexture(gl_texture_2d,building3text)
 gltexquad  x,y,0, x,0-y,0, x,0-y,z, x,y,z,tx,ty
 gltexquad  0-x,0-y,0, 0-x,y,0, 0-x,y,z, 0-x,0-y,z,tx,ty
 gltexquad  x,0-y,0, 0-x,0-y,0, 0-x,0-y,z, x,0-y,z,tx,ty
 gltexquad  0-x,y,0, x,y,0, x,y,z, 0-x,y,z,tx,ty
 If z<200 Then
 	z1=z+54:glcolor3f(0.9,0.3,0.4)
 Else
   glcolor3f( 0.4,0.35,0.4)
 EndIf 
 'gldisable GL_BLEND
 gltriangle  x,0-y,z, 0,0,z1, 0-x,0-y,z
 gltriangle  0-x,0-y,z, 0,0,z1, 0-x,y,z
 gltriangle  0-x,y,z, 0,0,z1, x,y,z
 gltriangle  x,y,z, 0,0,z1, x,0-y,z
tx=x:ty=y:tz=z1
End Sub
Dim Shared As Single dmx0,dmy0,dmx00,dmy00,dmx000,dmy000
Dim Shared As Integer ntown=200,ntown2,towntype(ntown),townchanged=1
Dim Shared As Integer showtown(ntown),townntower(ntown)
Dim Shared As Single  townx(ntown),towny(ntown),townz(ntown)
Dim Shared As Single  townbuild(ntown,6,6),townh(ntown,6,6),towndz(ntown,6,6)
Dim Shared As Single  towndx(ntown,6,6),towndy(ntown,6,6),townvie(ntown,6,6)
Sub drawtowns
Dim As Integer i,ii,jj
Dim As Single h,x,y,z,r
For i=1 To ntown2
 showtown(i)=0	
 If Abs(townx(i)-mx-dmx0)<dxterrain*scalex Then'30000 Then
  If Abs(towny(i)-my-dmy0)<dxterrain*scalex Then'30000 Then
   rotavion(townx(i)-mx-dmx0,towny(i)-my-dmy0,townz(i)-mz)
   If x2>(Abs(y2)-8000) Then
    For ii=0 To 6
     For jj=0 To 6
      If townbuild(i,ii,jj)>0 Then    	
         x=towndx(i,ii,jj)-dmx0
         y=towndy(i,ii,jj)-dmy0
         z=towndz(i,ii,jj)
         h=townh(i,ii,jj)
         rotavion(x-mx,y-my,z-mz)
         If x2>(Abs(y2)-h*1.4-400) Then
    	      showtown(i)=1
    	      r=210
    	      If Abs(x-mx)<r Then
         	 	If Abs(y-my)<r Then
    	      		If Abs(z-mz)<(h+10) Then 
                    mx -= cos1 *(10)*kfps
                    my -= sin1 *(10)*kfps
                    If plane=1 Then
                    	  mx-=cos1*v*2*kfps
                    	  my-=sin1*v*2*kfps
                    	  o1+=(Rnd-0.5)*140
                    	  testcrash
                    EndIf
    	 		      EndIf
         	 	EndIf
    	      EndIf
            If townvie(i,ii,jj)<0 Then 
               glcolor3f(1,0,0)
            Else 
            	If townbuild(i,ii,jj)=3 And h>200 Then
            	   glcolor3f(0,1,1)
            	Else	
            		glcolor3f(1,1,1)
            	EndIf
            EndIf
         	glpushmatrix
         	gltranslatef(x,y,z)
         	'glsphere(400)
         	drawbuilding1(h)
         	glpopmatrix
         EndIf 
      EndIf    
     Next jj
    Next ii
   EndIf  
  EndIf
 EndIf
Next i 
End Sub 
Sub setfog
Dim As Single dfog=120000,dfog2=121000
Dim fogcolor(0 to 3) as single => {1,1,1, 1.0}
glFogi(GL_FOG_MODE, gl_linear)
glFogfv(GL_FOG_COLOR, @fogcolor(0))
glFogf(GL_FOG_DENSITY, 1.0)
glHint(GL_FOG_HINT, gl_nicest)'GL_DONT_CARE)
If ifog=0 Then	dfog=120000
If ifog=1 Then	dfog=70000
If ifog=2 Then	dfog=60000
If ifog=3 Then	dfog=50000
If ifog=4 Then	dfog=40000
If ifog=5 Then	dfog=30000
If ifog=6 Then	dfog=20000
If ifog=7 Then	dfog=12000
If ifog=8 Then	dfog=7000
If ifog=9 Then	dfog=2000
If ifog=10 Then dfog=400:dfog2=95000
If ifog=11 Then dfog=100:dfog2=65000
If ifog=12 Then dfog=100:dfog2=50000
If ifog=13 Then dfog=100:dfog2=40000
If ifog=14 Then dfog=100:dfog2=30000
If ifog=15 Then dfog=100:dfog2=20000
glFogf(GL_FOG_START, dfog)
glFogf(GL_FOG_END, dfog2)
glEnable(GL_FOG)
End Sub
Dim Shared As uint domelist,distdome=25000
Dim Shared As Integer ndome=30'70
Dim Shared As Single domex(ndome),domey(ndome),domez(ndome),domer(ndome)
Dim Shared As Integer dometype(ndome),showdome(ndome)
Dim Shared As Single domevie(ndome),domevie0(ndome)
Sub drawdomes
Dim As Integer i,ii
Dim As Single r
'glEnable GL_BLEND
'glBlendFunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA
gldisable(gl_texture_2d)
glenable(gl_lighting)
ii=max2(5,200000/(10000+mz))
For i=1 To ndome
 showdome(i)=0	
 If Abs(domex(i)-mx)<10000 Then
  If Abs(domey(i)-my)<10000 Then
    rotavion(domex(i)-mx,domey(i)-my,domez(i)-mz)
    If x2>(Abs(y2)-500) And domez(i)>waterz Then
    	 showdome(i)=1
    	 r=domer(i)+10
    	 If Abs(x2)<r Then
    	 	If Abs(y2)<r Then
    	 		If Abs(z2)<r Then 
               mx -= cos1 *(10)*kfps
               my -= sin1 *(10)*kfps
               If plane=1 Then testcrash
    	 		EndIf
    	 	EndIf
    	 EndIf
       glpushmatrix
       If dometype(i)=0 Then
         gltranslatef(domex(i),domey(i),domez(i))
         If domevie(i)>0 Then 
         	glcolor3f(0,1,1)
         Else
         	glcolor3f(0.9,0.15,0.1)
         EndIf
       	glsphere(100,ii,ii)
       Else
         gltranslatef(domex(i),domey(i),domez(i)-74)
       	glcolor3f(0.5,0.6,0.6)
       	glcylindre(35,15,94,ii,ii)
       	gltranslatef(0,0,124)
         If domevie(i)>0 Then
         	glcolor3f(0,1,1)
         Else
         	glcolor3f(0.9,0.15,0.1)
         EndIf
       	glsphere(42,ii,ii)
       EndIf
       glpopmatrix
    EndIf 
  EndIf 
 EndIf  
Next i
glcolor3f(1,1,1)
'gldisable gl_blend
End Sub
Sub initdomes
	Randomize((imap-1)*100)
	distdome=30000
	For i=1 To ndome
	  dometype(i)=0
	  domer(i)=100	
	  If Rnd>0.35 Then dometype(i)=1:domer(i)=50	
     domex(i)=(Rnd-0.5)*2*distdome
     domey(i)=(Rnd-0.5)*2*distdome
     If dometype(i)=0 Then
        domevie0(i)=400
     	  domez(i)=getterrainheight(domex(i),domey(i))
     	  If domez(i)>(waterz+0.01) Then domez(i)+=58.5 Else domez(i)=waterz-1
     Else
     	  domevie0(i)=200
     	  domez(i)=getterrainheight(domex(i),domey(i))
     	  If domez(i)>(waterz+0.01) Then domez(i)+=68.5 Else domez(i)=waterz-1
     EndIf
     domevie(i)=domevie0(i)
	Next
End Sub
Dim Shared As Integer nairport=800,nairport2,airporttype(nairport)
Dim Shared As Integer showairport(nairport),airportntower(nairport)
Dim Shared As Single  airportx(nairport),airporty(nairport),airportz(nairport)
Dim Shared As Single  airportix(nairport),airportiy(nairport)
Dim Shared As Single  airportr(nairport),airportvie(nairport,3)
Sub drawairports
Dim As Integer i,ii,itower
Dim As Single r,x,y
ii=max2(5,200000/(10000+mz))
For i=1 To nairport2
 showairport(i)=0	
 If Abs(airportx(i)-mx-dmx0)<dxterrain*scalex Then'30000 Then
  If Abs(airporty(i)-my-dmy0)<dxterrain*scalex Then'30000 Then
   For itower=1 To airportntower(i)
    x=airportx(i)-dmx0
    y=airporty(i)-dmy0-(itower-1)*470
    rotavion(x-mx,y-my,airportz(i)-mz)
    If x2>(Abs(y2)-2000) Then
    	 showairport(i)=1
    	 r=airportr(i)+10
    	 If Abs(x2)<r Then
    	 	If Abs(y2)<r Then
    	 		If Abs(z2)<r Then 
               mx -= cos1 *(10)*kfps
               my -= sin1 *(10)*kfps
               If plane=1 Then testcrash
    	 		EndIf
    	 	EndIf
    	 EndIf
       glpushmatrix
       gldisable(gl_texture_2d)
       glenable(gl_lighting)
       If airporttype(i)=2 Or airporttype(i)=4 Then
         gltranslatef(x,y,airportz(i))
         If airportvie(i,itower)>0 Then 
         	glcolor3f(0,1,1)
         Else
         	glcolor3f(0.9,0.15,0.1)
         EndIf
       	glsphere(100,ii,ii) 'dome
       Else 	
         gltranslatef(x,y,airportz(i)-74)
         glcolor3f(0.5,0.6,0.6)
         glcylindre(35,15,94,ii,ii)
         gltranslatef(0,0,124)
         If airportvie(i,itower)>0 Then
        	  glcolor3f(0,1,1)
         Else
        	  glcolor3f(0.9,0.15,0.1)
         EndIf
         glsphere(42,ii,ii) 'tower
       EndIf   
       gldisable(gl_lighting)
       glcolor3f(1,1,1)
       glenable(gl_texture_2d)
       glbindtexture(gl_texture_2d,pistetext)	
       If airporttype(i)=3 Then 'tower 3pistes
           gltranslatef(0,-240,-67+74-124+(mz-mzsol0)*20/(3000+mz-mzsol0))
       	  gltexcarre5(2000,200,-1)
       	  gltranslatef(0,-470,0)
       	  gltexcarre5(2000,200,-1)
       	  gltranslatef(0,-470,0)
       	  gltexcarre5(2000,200,-1)
       ElseIf airporttype(i)=4 Then 'dome 3pistes
       	  gltranslatef(0,-290,-57+(mz-mzsol0)*20/(3000+mz-mzsol0))
       	  gltexcarre5(2000,200,-1)
       	  gltranslatef(0,-470,0)
       	  gltexcarre5(2000,200,-1)
       	  gltranslatef(0,-470,0)
       	  gltexcarre5(2000,200,-1)         	
       ElseIf airporttype(i)=2 Then 'dome 1piste
       	  gltranslatef(0,-290,-57+(mz-mzsol0)*20/(3000+mz-mzsol0))
       	  gltexcarre5(2000,200,-1)
       Else
       	  gltranslatef(0,-240,-67+74-124+(mz-mzsol0)*20/(3000+mz-mzsol0))
       	  gltexcarre5(2000,200,-1)
       EndIf   
       glpopmatrix
    EndIf
   Next itower 
  EndIf 
 EndIf  
Next i
glcolor3f(1,1,1)
'gldisable gl_blend
End Sub
Function testairport(ByVal x As Single,ByVal y As Single) As Integer
Dim As Integer i
Dim As Single dx,dy
For i=1 To nairport2
	If Abs(airportx(i)-dmx0-x)<2000 Then
	  If Abs(airporty(i)-dmy0-y)<2000 Then 	
		Select Case airporttype(i)
        Case 3,5 
         dx=airportx(i)-dmx0-x			
       	If Abs(dx)<1000 Then
            dy=airporty(i)-dmy0-y-240
         	If Abs(dy)<100 Then Return 1
         	dy-=470
           	If Abs(dy)<100 Then Return 1
       	   dy-=470
       	   If Abs(dy)<100 Then Return 1
       	EndIf    
			Case 4
         dx=airportx(i)-dmx0-x			
       	If Abs(dx)<1000 Then
            dy=airporty(i)-dmy0-y-290
         	If Abs(dy)<100 Then Return 1
         	dy-=470
           	If Abs(dy)<100 Then Return 1
       	   dy-=470
       	   If Abs(dy)<100 Then Return 1
       	EndIf    
			Case 2 
         dx=airportx(i)-dmx0-x			
       	If Abs(dx)<1000 Then
            dy=airporty(i)-dmy0-y-290
         	If Abs(dy)<100 Then Return 1
		   EndIf 
			Case Else 
         dx=airportx(i)-dmx0-x			
       	If Abs(dx)<1000 Then
            dy=airporty(i)-dmy0-y-240
         	If Abs(dy)<100 Then Return 1
		   EndIf 
		End Select 
	  EndIf 	
	EndIf
Next i
Return 0
End Function
Function mytestairport() As Integer
Dim As Integer i
Dim As Single dx,dy
If (mz-mzsol0)>1 Then Return 0
For i=1 To nairport2
	If showairport(i)=1 Then 
		Select Case airporttype(i)
        Case 3,4,5 
         dx=airportx(i)-dmx0-mx			
       	If Abs(dx)<1000 Then
            dy=airporty(i)-dmy0-my-240
         	If Abs(dy)<100 Then Return 1
         	dy-=470
           	If Abs(dy)<100 Then Return 1
       	   dy-=470
       	   If Abs(dy)<100 Then Return 1
       	EndIf    
		Case Else
         dx=airportx(i)-dmx0-mx			
       	If Abs(dx)<1000 Then
            dy=airporty(i)-dmy0-my-240
         	If Abs(dy)<100 Then Return 1
		   EndIf 
		End Select 
	EndIf
Next i
Return 0
End Function
Dim Shared As uint carlist
Dim Shared As Single carx,cary,carz,cursorx,cursory,cursorz
Sub glplacecursor(ByVal x As Single,ByVal y As Single,ByVal z As Single=-40)
   glLoadIdentity()
   cursorx=(-0.31+23.7*(-1+2*x/xmax)*(xmax/ymax)*(520/700))
   cursory=-0.25+17.1*(1-2*y/ymax)
   cursorz=z
   glTranslatef (cursorx,cursory,cursorz)
End Sub 
Sub gldrawtext(ByRef text As String,ByVal x As Single,ByVal y As Single,ByVal scale As Single=1.0)
   glLoadIdentity()
   gldisable(gl_texture_2d)
   'glTranslatef (22*(-1+2*x/xmax)*(xmax/ymax)*(520/700),17*(1-2*y/ymax),-38)
   glTranslatef ((-0.31+23.7*(-1+2*x/xmax)*(xmax/ymax)*(520/700)),-0.25+17.1*(1-2*y/ymax),-40)
   glscalef(scale,scale,scale)
   guigltext(text)
End Sub 
Sub drawboussole
'gldrawtext "$",0,0
'gldrawtext "$",xmax/2,ymax/2
'gldrawtext "$",xmax,0
'gldrawtext "$",0,ymax
'gldrawtext "$",xmax,ymax
Dim As Single rx=12,dx=20,dy=20,scale=0.8,cosx,sinx
glpushmatrix
If tourelle=0 Or plane=0 Then
	cosx=cos1:sinx=sin1 
Else
	cosx=tcos1:sinx=tsin1
EndIf
gldrawtext "+", xmax-dx, ymax-dy,scale
gldrawtext "S", xmax-dx+cosx*rx, ymax-dy+sinx*rx,scale
gldrawtext "N", xmax-dx-cosx*rx, ymax-dy-sinx*rx,scale
gldrawtext "W", xmax-dx-sinx*rx, ymax-dy+cosx*rx,scale
gldrawtext "E", xmax-dx+sinx*rx, ymax-dy-cosx*rx,scale
glpopmatrix
End Sub
Dim Shared As Single distmax=100000,radardx,radardy,radarrx,oradar=0
Dim Shared As Single airshipmove(nship),airshipvie0(nship)
Sub setradarcolor(ByVal i As Integer)
Dim As Integer typeship0
typeship0=typeship(i)
If typeship0=0 Then glcolor3f(0.2,0.3,1)	
If typeship0=1 Then glcolor3f(0,0,1)	
If typeship0=2 Then glcolor3f(1,0,0)	
If typeship0=3 Then glcolor3f(0.77,0,0)
If typeship0=4 Then glcolor3f(0.2,0.2,1)	
If typeship0=5 Then glcolor3f(0,0,1)
If typeship0=6 Then glcolor3f(0,0,1)
If typeship0=7 Then glcolor3f(0,0,1)
If typeship0=8 Then glcolor3f(0,0,1)
If typeship0=9 Then glcolor3f(0,0,1)
If typeship0=10 Then glcolor3f(0,0,1)
End Sub 	
Sub drawcompas
Dim As Single dx,dy,rx:dx=xmax*0.40:dy=ymax*0.9'ymax-50
 gldisable gl_texture_2d
 glcolor3f(0.099,0.982,0.23)
 glpushmatrix
 glplacecursor dx+5,dy-5
 glrotatef(-o3,0,0,1)
 rx=3.9
 gldisk(0,rx)
 glcolor3f(0,0.22,0)
 gllinewidth(2)
 glligne(-rx,0,-0.1,rx,0,-0.1)
 glligne(0,0,-0.1,0,rx*cos2*0.97,-0.1)
 glpopmatrix
End Sub  
Dim Shared As Single radaro1
Sub drawradar2
If volant>=3 And compas=1 Then drawcompas
Dim As Single dx,dy,rx:dx=xmax*0.60:dy=ymax*0.9'ymax-50
 'glenable gl_alpha_test
 'glAlphaFunc(gl_less,1/254)
 glenable gl_texture_2d
 glcolor3f(1,1,1)
 glbindtexture(gl_texture_2d,radartext)
 glpushmatrix
 'glplacecursor(xmax2+55,ymax-55)
 glplacecursor dx+5,dy-5
 glTranslatef (0,0,-0.01)
 radaro1+=kfps*2
 glrotatef(radaro1,0,0,1)
 'gltexcarre(4)
 'gltexdisk(0,4*475/ymax,40)
 gltexdisk(0,4*475/471,40)
 rx=ymax-dy+12'61
 gldrawtext "N",dx-rx*cos1,dy-rx*sin1
 glpopmatrix
 'gldisable gl_alpha_test
End Sub
Sub drawradar
Dim As Integer i
Dim As Single dx=35,dy=85-15,rx=30*ymax/520
radardx=dx+3*(xmax/700):radardy=dy+6*(ymax/520):radarrx=rx
'gldisable GL_DEPTH_TEST
glpushmatrix
glplacecursor dx+5,dy-5
glTranslatef (0,0,-0.01)
glEnable GL_BLEND
glBlendFunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA
glcolor4f(0.3,1,0.3,0.7)
gldisk(0,2)
gldisable GL_BLEND 
glcolor3f(0,0.5,0.5)
gldrawtext "+",dx,dy
glcolor3f(0.9,1,1)
gldrawtext "+",dx+rx*mx/distmax,dy-rx*my/distmax
glcolor3f(0.9,1,0)
gldrawtext "+",dx+rx*avionx/distmax,dy-rx*aviony/distmax
i=1
If boatz(i)<waterz and nboat2>=i Then 
  glcolor3f(0,0,1)
  gldrawtext "+",dx+rx*max(-0.9,min(0.9,boatx(i)/distmax)),dy-rx*max(-0.9,min(0.9,boaty(i)/distmax))
EndIf 
i=1
If max(Abs(airshipx(i)),Abs(airshipy(i)))<distmax Then 
   setradarcolor(i)
   gldrawtext("+",dx+rx*airshipx(i)/distmax,dy-rx*airshipy(i)/distmax,0.59)
EndIf 
For i=3 To nship2 Step 4
  If max(Abs(airshipx(i)),Abs(airshipy(i)))<distmax Then 
     setradarcolor(i)
     gldrawtext("+",dx+rx*airshipx(i)/distmax,dy-rx*airshipy(i)/distmax,0.59)
  EndIf 
Next i
glpopmatrix
'End Sub
'Sub drawradar2
Dim As Integer mapx,mapy,dmx,dmy
Dim As Single kaux=3.5/2.5
If plane=1 And car<=3 And volant>=1 And tourelle=0 Then
 drawradar2()	
 glpushmatrix
 dx=xmax*0.60:dy=ymax*0.9'ymax-50
 /'glplacecursor(dx+5,dy-5)'xmax*0.57,ymax*0.768,-30)
 'glTranslatef (0,0,-0.01)
 glenable GL_BLEND
 glBlendFunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA
 glcolor4f(0.3,1,0.3,0.7)
 glcolor4f(0.3,1,0.3,0.95)
 gldisk(0,4)'2.5)
 gldisable GL_BLEND
 '/ 
 'glcolor4f(0,1,1,1)
 'oradar+=kfps*2
 'If oradar>3600 Then oradar-=3600
 'glplacecursor(dx,dy-12)
 'gllinewidth(1)
 'glligne(0,0,1, 2.3*Cos(oradar*degtorad),-2.3*Sin(oradar*degtorad),1)
 'glcolor3f(0,0.5,0.5)
 glcolor3f(0,0.2,0.2)
 'gldrawtext "+",dx,dy
 glcolor3f(1,1,1)
     dmx=avionx-mx:dmy=aviony-my
     'dmx=0:dmy=89000
     If Abs(dmx)<90000 And Abs(dmy)<90000 Then 
       dmx*=0.4:dmy*=0.4
       aux=Sqr(dmx*dmx+dmy*dmy)
       If aux<=36000 Then
         'mapx=(dmx*cos1+dmy*sin1)/(200+aux/54)
         'mapy=(-dmx*sin1+dmy*cos1)/(200+aux/54)
         mapx=57*((dmx*cos1+dmy*sin1)/(5000+aux))*ymax/471
         mapy=57*((-dmx*sin1+dmy*cos1)/(5000+aux))*ymax/471
         'If (mapx*mapx+mapy*mapy)<(50*50) Then    	
       	  gldrawtext("+", dx-mapy,dy-mapx,1.5 )    	
         'EndIf
       EndIf
     EndIf   
  
 For i=1 To nship2
     dmx=airshipx(i)-mx:dmy=airshipy(i)-my
     'dmx=-000:dmy=70000
     If Abs(dmx)<90000 And Abs(dmy)<90000 Then 
       dmx*=0.4:dmy*=0.4
       aux=Sqr(dmx*dmx+dmy*dmy)
       If aux<=36000 Then 
         mapx=57*((dmx*cos1+dmy*sin1)/(5000+aux))*ymax/471
         mapy=57*((-dmx*sin1+dmy*cos1)/(5000+aux))*ymax/471
         'If (mapx*mapx+mapy*mapy)<(50*50) Then
           setradarcolor(i)
       	  gldrawtext("+", dx-mapy,dy-mapx,1.5 )    	
         'EndIf
       EndIf
     EndIf 
 Next i
 glcolor3f(0,0,1)
 For i=1 To nboat2
   If boatz(i)<=(waterz+0.1) Then 
     dmx=boatx(i)-mx:dmy=boaty(i)-my
     'dmx=-000:dmy=70000
     If Abs(dmx)<90000 And Abs(dmy)<90000 Then 
       dmx*=0.4:dmy*=0.4
       aux=Sqr(dmx*dmx+dmy*dmy)
       If aux<=36000 Then 
         mapx=57*((dmx*cos1+dmy*sin1)/(5000+aux))*ymax/471
         mapy=57*((-dmx*sin1+dmy*cos1)/(5000+aux))*ymax/471
         'If (mapx*mapx+mapy*mapy)<(50*50) Then
       	  gldrawtext(".", dx-mapy,dy-mapx,1.5 )    	
         'EndIf
       EndIf
     EndIf  
   EndIf   
 Next i
 For i=1 To nairport2 
   dmx=airportx(i)-dmx0-mx
   If Abs(dmx)<90000 Then 
      dmy=airporty(i)-dmy0-my
     'dmx=-000:dmy=70000
     If Abs(dmy)<90000 Then 
       dmx*=0.4:dmy*=0.4
       aux=Sqr(dmx*dmx+dmy*dmy)
       If aux<=36000 Then 
         mapx=57*((dmx*cos1+dmy*sin1)/(5000+aux))*ymax/471
         mapy=57*((-dmx*sin1+dmy*cos1)/(5000+aux))*ymax/471
         'If (mapx*mapx+mapy*mapy)<(50*50) Then
           If airporttype(i)>=3 Then 
           	  glcolor3f(1,1,0.3)
           Else
           	  glcolor3f(0.8,0.8,0.8)
           EndIf
       	  gldrawtext(".", dx-mapy,dy-mapx,1 )    	
         'EndIf
       EndIf
     EndIf
   EndIf   
 Next i
 For i=1 To ntown2 
   dmx=townx(i)-dmx0-mx
   If Abs(dmx)<90000 Then 
      dmy=towny(i)-dmy0-my
     'dmx=-000:dmy=70000
     If Abs(dmy)<90000 Then 
       dmx*=0.4:dmy*=0.4
       aux=Sqr(dmx*dmx+dmy*dmy)
       If aux<=36000 Then 
         mapx=57*((dmx*cos1+dmy*sin1)/(5000+aux))*ymax/471
         mapy=57*((-dmx*sin1+dmy*cos1)/(5000+aux))*ymax/471
         'If (mapx*mapx+mapy*mapy)<(50*50) Then
           If towntype(i)>=2 Then 
           	  glcolor3f(0.4,0.8,0)
           Else
           	  glcolor3f(0,0.8,0)
           EndIf
       	  gldrawtext("^", dx-mapy,dy-mapx,0.8 )    	
         'EndIf
       EndIf
     EndIf
   EndIf   
 Next i
 glcolor3f(1,1,1)
 glpopmatrix
Else
 drawboussole	
EndIf 	
'glenable GL_DEPTH_TEST
End Sub
Sub drawvolant
 glenable gl_alpha_test
 glAlphaFunc(gl_less,1/254)
 glcolor3f(1,1,1)
 glbindtexture(gl_texture_2d,volanttext)
 glpushmatrix
 glplacecursor(xmax*0.43,ymax*0.768,-30)
 If volantrot>120 Then volantrot=120
 If volantrot<-120 Then volantrot=-120
 glrotatef(volantrot,0,0,1)
 volantrot=0.75*volantrot+1e-10
 gltexcarre(5)
 glpopmatrix
End Sub
Dim Shared As Single windo1,vwind
Sub drawwind
 glenable gl_alpha_test
 glAlphaFunc(gl_less,1/254)
 glcolor3f(1,1,1)
 glbindtexture(gl_texture_2d,windtext)
 glpushmatrix
 glplacecursor(xmax-55,55)
 glrotatef(windo1-o1,0,0,1)
 gltexcarre(3)
 glpopmatrix
 gldisable gl_alpha_test
End Sub
Dim Shared As Single drawmapx=50,drawmapy=159+9,k8=6
'waterz=-60*scalez
Sub drawmap2
 glcolor3f(1,1,1)
 glbindtexture(gl_texture_2d,maptext)
 glpushmatrix
 glplacecursor(100,220)
 gltexcarre(2.5*2)
 Dim As Single aux=200/(k8*512*scalex)'200/(6*512*500) 
 gldrawtext("+",100-5+(mx+dmx0)*aux,220+5-(my+dmy0)*aux)
 glpopmatrix	
End Sub
Dim Shared As Integer mapdisplay=0
Sub displaymap
 gldisable GL_DEPTH_TEST
 glenable(gl_texture_2d)
 glcolor3f(1,1,1)
 glbindtexture(gl_texture_2d,maptext)
 glpushmatrix
 glplacecursor(xmax/2-150,200)
 gltexcarre(2.5*4*640/(0.1+ymax))
 Dim As Single aux=380/(k8*512*scalex) 
 glcolor3f(0.8,0.8,0.8)
 Dim As integer i
 For i=1 To nairport2
    If airporttype(i)>=3 Then
     	glcolor3f(1,1,0.3)
    Else 
     	glcolor3f(0.8,0.8,0.8)
    EndIf
   gldrawtext("-",xmax/2-150-5+airportx(i)*aux,200+5-airporty(i)*aux,1.7) 	
 Next
 For i=1 To ntown2
    If towntype(i)>=2 Then
     	glcolor3f(1,0.8,0.3)
    Else 
     	glcolor3f(0.8,0.8,0.8)
    EndIf
   gldrawtext("^",xmax/2-150-5+townx(i)*aux,200+5-towny(i)*aux,1.2) 	
 Next
 glcolor3f(1,1,1)
 gldrawtext("+",xmax/2-150-5+(mx+dmx0)*aux,200+5-(my+dmy0)*aux)
 glpopmatrix
 glenable GL_DEPTH_TEST
End Sub
Sub displaymap2
Dim As Single dx,tx,ty,dtx
 gldisable GL_DEPTH_TEST
 glenable(gl_texture_2d)
 glcolor3f(1,1,1)
 glbindtexture(gl_texture_2d,maptext)
 glpushmatrix
 glplacecursor(xmax/2-150,200)
 dx=(2.5*4*640/(0.1+ymax))
 Dim As Single aux=1/(k8*512*scalex)
 tx=0.5+(mx+dmx0)*aux
 ty=0.5+(my+dmy0)*aux
 dtx=0.2
 dtx=min(dtx,tx)
 dtx=min(dtx,ty)
 dtx=min(dtx,1.0-tx)
 dtx=min(dtx,1.0-ty)
 dtx=max(0.05,dtx)
	glbegin(gl_quads)
	glTexCoord2f(tx-dtx,ty-dtx)
	glvertex3f(-dx,-dx,0)
	gltexcoord2f(tx+dtx,ty-dtx)
	glvertex3f(dx,-dx,0)
	glTexCoord2f(tx+dtx,ty+dtx)
	glvertex3f(dx,dx,0)
	gltexcoord2f(tx-dtx,ty+dtx)
	glvertex3f(-dx,dx,0)
	glend()
 glcolor3f(0.8,0.8,0.8)
 aux*=200/dtx
 Dim As integer i,ix,iy
 For i=1 To nairport2
 	ix=(airportx(i)-mx-dmx0)*aux
 	iy=(airporty(i)-my-dmy0)*aux
 	If Abs(ix)<182 And Abs(iy)<187 Then 
      If airporttype(i)>=3 Then
      	glcolor3f(1,1,0.3)
      Else 
      	glcolor3f(0.8,0.8,0.8)
      EndIf
 		gldrawtext("-",xmax/2-150-5+ix,200+5-iy,1.87) 	
 	EndIf
 Next
 For i=1 To ntown2
 	ix=(townx(i)-mx-dmx0)*aux
 	iy=(towny(i)-my-dmy0)*aux
 	If Abs(ix)<182 And Abs(iy)<187 Then 
      If towntype(i)>=2 Then
      	glcolor3f(1,0.8,0.3)
      Else 
      	glcolor3f(0.8,0.8,0.8)
      EndIf
 		gldrawtext("^",xmax/2-150-5+ix,200+5-iy,1.3) 	
 	EndIf
 Next
 glcolor3f(1,1,1)
 gldrawtext("+",xmax/2-150-5,200+5)
 glpopmatrix
 glenable GL_DEPTH_TEST
End Sub
Sub drawmap
 gldisable GL_DEPTH_TEST
 glcolor3f(1,1,1)
 glbindtexture(gl_texture_2d,maptext)
 glpushmatrix
 glplacecursor(50,159)
 gltexcarre(2.5*640/(0.1+ymax))
 Dim As Single x,y,aux=100/(k8*512*scalex)
 x=max(-48.0,min(48.0,(mx+dmx0)*aux))
 y=max(-48.0,min(48.0,(my+dmy0)*aux)) 
 gldrawtext("+",50-5+x,159+5-y)
 glpopmatrix
 glenable GL_DEPTH_TEST
 If mapdisplay=1 Then displaymap()
 If mapdisplay=2 Then displaymap2()
 If Abs(dmx0+mx-dmx000)<distmax/2 And Abs(dmy0+my-dmy000)<distmax/2 Then 
 	If Abs(dmx0-dmx000)>1 Or Abs(dmy0-dmy000)>1 Then
 		mx0=mx:my0=my
 		mx+=dmx0-dmx000:my+=dmy0-dmy000
      dmx=avionx-mx0:dmy=aviony-my0
      If Sqr(dmx*dmx+dmy*dmy)<30000 Then 
      	avionx+=dmx0-dmx000:aviony+=dmy0-dmy000
      EndIf
      dmx00=dmx0+(mx0-mx)*2:dmy00=dmy0+(my0-my)*2
      dmx0-=mx-mx0:dmy0-=my-my0
      'dmx00=dmx0:dmy00=dmy0
 		'dmx0=dmx000:dmy0=dmy000
 		If imap>=5 Then initterrain3
 	EndIf
 EndIf
End Sub
Dim Shared As Single caro2,caro3
Sub drawcar
glpushmatrix
glplacecursor(xmax*0.57,ymax*0.75,-21)
caro2=0.055*caro2+0.945*sin2*10
caro3=0.055*caro3+0.945*sin3*10
glrotatef(caro2,1,0,0)
glrotatef(caro3,0,0,1)
glenable gl_texture_2d
glbindtexture(gl_texture_2d,cartext)
If carlist<>0 Then
   glcalllist carlist:glpopmatrix
Else
 carlist=glgenlists(1)
 glnewlist carlist,gl_compile 
 gldisable GL_DEPTH_TEST
 glEnable GL_BLEND
 glBlendFunc GL_one_minus_SRC_ALPHA,GL_SRC_ALPHA
 gldisable gl_lighting
 glcolor3f(1,1,1)
 glscalef(1,1.22,1)
 loadobjsize("objects/car.obj",@"",@"",83)
 gldisable GL_BLEND
 glEnable GL_DEPTH_TEST
glendlist
glpopmatrix
EndIf 
End Sub
Dim Shared As Single helicerot0
Sub drawcockpit
Dim As single ymax2
ymax2=ymax*(0.95+(1.0-sin2)*cos3*0.058)
gldisable GL_DEPTH_TEST
glpushmatrix
if typeavion<7 Then 
 glbindtexture(gl_texture_2d,helicetext)	
 glEnable GL_BLEND
 'glBlendFunc GL_SRC_color,GL_ONE_MINUS_SRC_color
 glBlendFunc GL_SRC_alpha,GL_ONE_MINUS_SRC_alpha
 glcolor4f(1,1,1,0.62)
 glplacecursor(xmax*0.5,ymax2*0.785)
 'helicerot0=helicerot0+(8.5+v)*1.5*kfps
 helicerot0=helicerot0+(9+v)*1.35*kfps
 If helicerot0>10000 Then helicerot0-=10000
 glrotatef(-helicerot0,0,0,1)
 gltexcarre4(40,40)
 gldisable gl_blend
EndIf 
glenable gl_alpha_test
glAlphaFunc(gl_less,10/254)
Select Case typeavion
	Case 1,4,6 
	  If typeavion=1 Then glcolor3f(1,1,0.88)	
	  If typeavion=4 Then glcolor3f(0.8,1,0.8)	
	  If icar=12 Then glcolor3f(0.75,1,0.75)	
     glbindtexture(gl_texture_2d,cockpittext1)
     'glplacecursor(xmax*0.5,ymax*0.5)
     'gltexcarre4(56,36)
     glplacecursor(xmax*0.5,ymax2*0.505)
     gltexcarre4(56,36.5)
	Case 2  
     glbindtexture(gl_texture_2d,cockpittext2)
     glplacecursor(xmax*0.5,ymax2*0.527)
     gltexcarre4(64,37.8)
	Case 3,5
	  If icar=8 Then glcolor3f(0.82,1,0.9)	
     glbindtexture(gl_texture_2d,cockpittext3)
     glplacecursor(xmax*0.5,ymax2*0.526)
     gltexcarre4(64,37.8)
	Case 7,8
	  If typeavion=7 Then glcolor3f(0.8,0.87,1)	
     glbindtexture(gl_texture_2d,cockpittext4)
     glplacecursor(xmax*0.5,ymax2*0.57)
     gltexcarre4(64,41)
End Select  
glpopmatrix
glenable GL_DEPTH_TEST
gldisable gl_alpha_test
End Sub
Dim Shared As uint avionlist,corsair0list,corsairlist,vg33list,zerolist,b25list,alphajetlist
Dim Shared As uint alphajet0list,vg330list,spitfirelist,spitfire0list,bf109list,bf1090list
Dim Shared As uint p51dlist,p51d0list,f14list,f140list,zero0list
Sub drawcorsair0
glenable gl_texture_2d
glbindtexture(gl_texture_2d,corsairtext)
glscalef(1.05,1.05,1.05)
	If corsair0list<>0 Then
		glcalllist(corsair0list)
	Else
		corsair0list=glgenlists(1)
		glnewlist corsair0list,gl_compile
		'loadobjsize("objects/corsair.obj",@"",@"",94)
		loadobjsize("objects/f4ucockpit.obj",@"",@"",42.52)
      glendlist
	EndIf	
End Sub
Sub drawalphajet0
glenable gl_texture_2d
glbindtexture(gl_texture_2d,alphajettext)
'glscalef(1.05,1.05,1.05)
	If alphajet0list<>0 Then
		glcalllist(alphajet0list)
	Else
		alphajet0list=glgenlists(1)
		glnewlist alphajet0list,gl_compile
		loadobjsize("objects/alphajet_cockpit.obj",@"",@"",77)
      glendlist
	EndIf	
End Sub
Sub drawvg330
glenable gl_texture_2d
glbindtexture(gl_texture_2d,vg33text)
'glscalef(1.05,1.05,1.05)
	If vg330list<>0 Then
		glcalllist(vg330list)
	Else
		vg330list=glgenlists(1)
		glnewlist vg330list,gl_compile
		loadobjsize("objects/vg33_cockpit.obj",@"",@"",54)
      glendlist
	EndIf	
End Sub
Sub drawspitfire0
glenable(gl_lighting)
glenable(gl_light2)
glenable gl_texture_2d
If icar=7 Then 
	glbindtexture(gl_texture_2d,spitfiretext)
Else 
	glbindtexture(gl_texture_2d,spitfiretext2)
EndIf
	If spitfire0list<>0 Then
		glcalllist(spitfire0list)
	Else
		spitfire0list=glgenlists(1)
		glnewlist spitfire0list,gl_compile
		loadobjsize("objects/spitfire_cockpit.obj",@"",@"",79.3)
      glendlist
	EndIf	
gldisable(gl_light2)
gldisable(gl_lighting)
End Sub
Sub drawbf1090
glenable(gl_lighting)
glenable(gl_light2)
glenable gl_texture_2d
glbindtexture(gl_texture_2d,bf109text)
	If bf1090list<>0 Then
		glcalllist(bf1090list)
	Else
		bf1090list=glgenlists(1)
		glnewlist bf1090list,gl_compile
		loadobjsize("objects/bf109_cockpit.obj",@"",@"",55)
      glendlist
	EndIf	
gldisable(gl_light2)
gldisable(gl_lighting)
End Sub
Sub drawp51d0
glenable(gl_lighting)
glenable(gl_light2)
glenable gl_texture_2d
glbindtexture(gl_texture_2d,p51dtext)
	If p51d0list<>0 Then
		glcalllist(p51d0list)
	Else
		p51d0list=glgenlists(1)
		glnewlist p51d0list,gl_compile
		loadobjsize("objects/p51d_cockpit.obj",@"",@"",95)
      glendlist
	EndIf	
gldisable(gl_light2)
gldisable(gl_lighting)
End Sub
Sub drawf140
glenable gl_texture_2d
glbindtexture(gl_texture_2d,f14text)
	If f140list<>0 Then
		glcalllist(f140list)
	Else
		f140list=glgenlists(1)
		glnewlist f140list,gl_compile
		loadobjsize("objects/f14_cockpit2.obj",@"",@"",50)
      glendlist
	EndIf	
End Sub
Sub drawzero0
glenable(gl_lighting)
glenable(gl_light3)
glenable gl_texture_2d
if icar=11 Then 
	glbindtexture(gl_texture_2d,zerotext)
Else 
	glbindtexture(gl_texture_2d,zero2text)
EndIf
	If zero0list<>0 Then
		glcalllist(zero0list)
	Else
		zero0list=glgenlists(1)
		glnewlist zero0list,gl_compile
		loadobjsize("objects/zero1_cockpit.obj",@"",@"",37.3)
      glendlist
	EndIf	
gldisable(gl_light3)
gldisable(gl_lighting)
End Sub
Sub drawcockpit2
Dim As single ymax2
ymax2=ymax*(0.9875+(1.0-sin2)*cos3*0.0125)
glpushmatrix
If typeavion<7 Then 
 glbindtexture(gl_texture_2d,helicetext)	
 gldisable GL_DEPTH_TEST
 glEnable GL_BLEND
 'glBlendFunc GL_SRC_color,GL_ONE_MINUS_SRC_color
 glBlendFunc GL_SRC_alpha,GL_ONE_MINUS_SRC_alpha
 glcolor4f(1,1,1,0.62)
 glplacecursor(xmax*0.5,ymax2*0.785)
 'helicerot0=helicerot0+(8.5+v)*1.5*kfps
 helicerot0=helicerot0+(9+v)*1.35*kfps
 If helicerot0>10000 Then helicerot0-=10000
 glrotatef(-helicerot0,0,0,1)
 gltexcarre4(40,40)
 gldisable gl_blend
 glenable GL_DEPTH_TEST
EndIf
If typeavion=2 Then glplacecursor(xmax*0.5,ymax2*0.925,-25.28)
If typeavion=7 Then glplacecursor(xmax*0.5,ymax2*0.85,-21)
If typeavion=1 Then glplacecursor(xmax*0.5,ymax2*0.87,-25.9)
If typeavion=3 Then glplacecursor(xmax*0.5035,ymax2*0.74,-9.2)
If typeavion=4 Then glplacecursor(xmax*0.503,ymax2*0.855,-9.3)
If typeavion=5 Then glplacecursor(xmax*0.5,ymax2*0.98,-20)
If typeavion=8 Then glplacecursor(xmax*0.505,ymax2*0.92,-22)
If typeavion=6 Then glplacecursor(xmax*0.5,ymax2*1.075,-31)
'glplacecursor(xmax*0.5,ymax*0.57,-6)
caro2=0.055*caro2+0.945*sin2*10
caro3=0.055*caro3+0.945*sin3*10
glrotatef(-90,1,0,0)
glrotatef(90,0,0,1)
glrotatef(-0.115*caro3,1,0,0)
glrotatef(-0.105*caro2,0,1,0)
If typeavion=2 Then drawcorsair0
If typeavion=7 Then drawalphajet0
If typeavion=1 Then drawvg330
If typeavion=3 Then drawspitfire0
If typeavion=4 Then drawbf1090
If typeavion=5 Then drawp51d0
If typeavion=8 Then drawf140
If typeavion=6 Then drawzero0
glpopmatrix
End Sub
Sub drawcockpit20
     glenable(gl_texture_2d)
     glbindtexture(gl_texture_2d,corsairtext)
     glcolor3f(1,1,1)	
     glpushmatrix
     gltranslatef(mx,my,mz)
     glrotatef(o1,0,0,1)
     glrotatef(-o2,0,1,0)
     glrotatef(-0.85*o3,1,0,0)
    'gltranslatef(0,0,8)
     gltranslatef(23,0,0)'-22)
     'gltranslatef(22,0,2.5)
     drawcorsair0
     glpopmatrix
End Sub
Dim Shared As Integer boatlist(5),typeboat(nboat),voilelist,voile2list,voileboat(nboat)
Dim Shared As Single vboat(nboat),vboat0(nboat),boatvie(nboat),boatvie0(nboat)
Dim Shared As Single tboatturn(nboat),boatvie00(nboat)
Dim Shared As Single boatdo1(nboat),boatdo10(nboat),boatturn(nboat),distboatmax=70000'45000
Dim Shared As Single dxboat(nboat),dyboat(nboat)
Dim Shared As Single boatco1(nboat),boatsi1(nboat),boatpoids(nboat),timeboat(nboat)
Dim Shared As Single dzboat1(nboat),dzboat2(nboat),dzboat3(nboat),dxboatmax(nboat)
Dim Shared As Integer boatwar(nboat)
Dim Shared As Single boatdcao1(nboat),boatdcao2(nboat),timeboatdca(nboat)
Dim Shared As Single distboatx(nboat),boattarget(nboat)
nboat2=min2(nboat,nboat2)
#Include "boatfires.bi"
Sub initboat
Randomize()
windo1=Rnd*360
For i=1 To 4
	typeboat(i)=0
	vboat0(i)=5
	boatx(i)=-1000+45000*(i Mod 2):boaty(i)=-1000+45000*((int(i/2.5))Mod 2)
	boato1(i)=Rnd*360
	dxboat(i)=300
	dyboat(i)=30
	dxboatmax(i)=490
	dzboat1(i)=110
	dzboat2(i)=170
	dzboat3(i)=90
	boatpoids(i)=1
	boatvie0(i)=200
	boatvie(i)=boatvie0(i)
Next
For i=5 To 6
	typeboat(i)=1
	vboat0(i)=1.8'5/3
	boatx(i)=-2000+20000*(i Mod 2)*rnd:boaty(i)=-2000+20000*((Int((i-4)/2.5))Mod 2)*rnd
	boato1(i)=Rnd*360
	dxboat(i)=170
	dyboat(i)=17
	dxboatmax(i)=150
	dzboat1(i)=1.5
	dzboat2(i)=5
	dzboat3(i)=1.5
	boatpoids(i)=0.5
	boatvie0(i)=40
	boatvie(i)=boatvie0(i)
Next
For i=7 To 7
	typeboat(i)=3
	vboat0(i)=3.5
	boatx(i)=-2000+20000*((i-1) Mod 2)*rnd:boaty(i)=-2000+20000*((Int((i-6)/2.5))Mod 2)*rnd
	boato1(i)=Rnd*360
	dxboat(i)=400
	dyboat(i)=40
	dxboatmax(i)=430
	dzboat1(i)=59
	dzboat2(i)=51
	dzboat3(i)=95
	boatpoids(i)=1
	boatvie0(i)=200
	boatvie(i)=boatvie0(i)
Next
For i=8 To 8
	typeboat(i)=2
	vboat0(i)=3
	boatx(i)=-2000+20000*((i-1) Mod 2)*rnd:boaty(i)=-2000+20000*((Int((i-6)/2.5))Mod 2)*rnd
	boato1(i)=Rnd*360
	dxboat(i)=400
	dyboat(i)=40
	dxboatmax(i)=430
	dzboat1(i)=59
	dzboat2(i)=51
	dzboat3(i)=95
	boatpoids(i)=1
	boatvie0(i)=200
	boatvie(i)=boatvie0(i)
Next
For i=9 To 9
	typeboat(i)=0
	vboat0(i)=5
	boatx(i)=-1000+45000*(i Mod 2):boaty(i)=-1000+45000*((int(i/2.5))Mod 2)
	boato1(i)=Rnd*360
	dxboat(i)=300
	dyboat(i)=30
	dxboatmax(i)=490
	dzboat1(i)=110
	dzboat2(i)=170
	dzboat3(i)=90
	boatpoids(i)=1
	boatvie0(i)=200
	boatvie(i)=boatvie0(i)
Next
For i=1 To 1'i=10 To 10
	typeboat(i)=4    'carrier
	vboat0(i)=3.7
	boatx(i)=-2000+20000*((i-1) Mod 2)*rnd:boaty(i)=-2000+20000*((Int((i-6)/2.5))Mod 2)*rnd
	boato1(i)=Rnd*360
	dxboat(i)=1850'2050
	dyboat(i)=200'280
	dxboatmax(i)=1850'2050
	dzboat1(i)=180
	dzboat2(i)=180
	dzboat3(i)=180
	boatpoids(i)=1
	boatvie0(i)=300
	boatvie(i)=boatvie0(i)
Next
For i=10 To nboat'i=11 To nboat
	typeboat(i)=0
	vboat0(i)=5
	boatx(i)=-1000+45000*(i Mod 2)+4000*rnd:boaty(i)=-1000+45000*((int(i/2.5))Mod 2)+4000*rnd
	boato1(i)=Rnd*360
	dxboat(i)=300
	dyboat(i)=30
	dxboatmax(i)=490
	dzboat1(i)=110
	dzboat2(i)=170
	dzboat3(i)=90
	boatpoids(i)=1
	boatvie0(i)=200
	boatvie(i)=boatvie0(i)
Next
i=1'10
boatx(i)=80:boaty(i)=900
boato1(i)=90
i=9
boatx(i)=-2000:boaty(i)=4000
boato1(i)=50+Rnd*80
For i=1 To nboat
	timeboat(i)=-999999
Next
End Sub 
initboat()
Dim Shared As Single tboatmort(nboat)
Dim Shared As Integer testmort(nboat)
Dim Shared As Single tirx,tiry,tirz,dtirx,dtiry,dtirz
Sub tirboat(ByVal i As Integer,ByVal iairship As Integer=0)
Dim As Single distboat,co1,si1,co2,si2,dx,dy,dz,tdx,tdy,tdz,disttir,aux
If testmort(i)=0 Then  
	If distboatx(i)<14000 Or boattarget(i)=0 Then 
	   dx=mx-boatx(i)
	   dy=my-boaty(i)
	   dz=mz-boatz(i)-dzboat2(i)
	   aux=min(Abs(dx),1100.0)
	   aux=min(abs(dy),aux)
	   dx+=aux*cos1*cos2
	   dy+=aux*sin1*cos2
	   dz+=aux*sin2
	   distboat=Sqr(dx*dx+dy*dy+dz*dz)
	Else 
		dx=airshipx(boattarget(i))-boatx(i)
		dy=airshipy(boattarget(i))-boaty(i)
		dz=airshipz(boattarget(i))-boatz(i)-dzboat2(i)
	   distboat=Sqr(dx*dx+dy*dy+dz*dz)
	   If distboat>17000 Then boattarget(i)=0
	EndIf 	
	'distboatx=max(Abs(dx),Abs(dy)) 
	'distboat=max(Abs(dz),distboatx)
	'auxvar2=distboat
	If distboat<17000 Then
		If iairship=0 Then 
		  co1=Cos(boatdcao1(i)*degtorad)
		  si1=Sin(boatdcao1(i)*degtorad)
		  co2=Cos(boatdcao2(i)*degtorad)
		  si2=Sin(boatdcao2(i)*degtorad)
		Else 
		  co1=Cos((boatdcao1(i)+airshipo1(iairship))*degtorad)
		  si1=Sin((boatdcao1(i)+airshipo1(iairship))*degtorad)
		  co2=Cos((boatdcao2(i))*degtorad)
		  si2=Sin((boatdcao2(i))*degtorad)
		EndIf 	
		tdy=-dx*si1+dy*co1
		tdz=-distboat*si2+dz*co2
      boatdcao1(i)+=Sgn(tdy)*kfps*min(7.0,Abs(tdy/400)+0.25)
      boatdcao2(i)+=Sgn(tdz)*kfps*min(7.0,Abs(tdz/400)+0.25)
      boatdcao2(i)=max(-80.0,min(80.0,boatdcao2(i)))				
	EndIf
	If distboat<14000 Then
      If time2<timeboatdca(i)-100 Then timeboatdca(i)=time2 'if midnight
      If time2>(timeboatdca(i)+1) Then
	      timeboatdca(i)=time2
	      soundexplosion2
	      tirx=boatx(i):tiry=boaty(i):tirz=boatz(i)+dzboat2(i)
		   'tdx=-dx*cos1-dy*sin1
		   disttir=distboat*1.27
	      dtirx=disttir*co1*co2
	      dtiry=disttir*si1*co2
	      dtirz=disttir*si2
	      If iairship=0 Then 
	      	addtir2(1)
	      Else
	      	addtir2(2000+iairship)
	      EndIf
      EndIf    
	EndIf
EndIf
End Sub
Sub tirb25(ByVal iship As Integer)
Dim As Integer i
i=0
boatx(i)=airshipx(iship)
boaty(i)=airshipy(iship)
boatz(i)=airshipz(iship)
distboatx(i)=max(Abs(boatx(i)-mx),Abs(boaty(i)-my))
tirboat(i,iship)
End Sub
Sub resetboat(ByVal iboat As Integer)
Dim As Integer j
Dim As Single do1,r
   testmort(iboat)=0
   boatvie(iboat)=boatvie0(iboat)
	For j=1 To 40
	 do1=Rnd*360
	 r=distboatmax*(3+Rnd)/4.1'*Sqr(nboat2/4)
	 boatx(iboat)=mx+r*Cos(do1*degtorad)
	 boaty(iboat)=my+r*Sin(do1*degtorad)
	 boatz(iboat)=getterrainheight(boatx(iboat),boaty(iboat))
	 If boatz(iboat)<waterz Then Exit For 
	Next 
	boatwar(iboat)=0
	boattarget(iboat)=0
	For j=0 To 3
		tdegats(iboat,j)=0
	Next
   vdegats(iboat)=0
	For j=1 To nfire
		timefire(j,iboat)=0
	Next
End Sub
Function testboat(ByVal iboat As Integer,ByVal x As Single,ByVal y As Single) As Integer
Dim As Integer i2,test
Dim As Single x1,y1
If getterrainheight(x,y)>=waterz Then Return 1
test=0
For i2=1 To nboat2
  If i2<>iboat Then 	
	 x1=(x-boatx(i2))*boatco1(i2)+(y-boaty(i2))*boatsi1(i2)
	 If Abs(x1)<dxboatmax(i2) Then 
		y1=-(x-boatx(i2))*boatsi1(i2)+(y-boaty(i2))*boatco1(i2)
	   If Abs(y1)<(2*dyboat(i2)) Then test=2:Exit For
 	 EndIf
  EndIf 	
Next
Return test 
End Function
Dim Shared As Integer myboat,myboat0
Sub moveboat
Dim As Integer i,test
Dim As Single co1,si1,do1,dmmx1,dmmx2,dmmx3,mz10,boato10,boatx2,boaty2,boatdo100,ddir,co10,si10
mz11=-999999
If Rnd<kfps*0.1 Then
	windo1+=(Rnd-0.5)*10 '1.5 '*5
	windco1=Cos(windo1*degtorad):windsi1=Sin(windo1*degtorad)
EndIf
While windo1>180:windo1-=360:Wend
While windo1<-180:windo1+=360:Wend
If plane=0 Then v=vboat(myboat)
myboat0=myboat:myboat=0
For i=1 To nboat2
 While boatx(i)<mx-distboatmax:boatx(i)+=2*distboatmax:boato1(i)=Rnd*360:Wend
 While boatx(i)>mx+distboatmax:boatx(i)-=2*distboatmax:boato1(i)=Rnd*360:Wend	
 While boaty(i)<my-distboatmax:boaty(i)+=2*distboatmax:boato1(i)=Rnd*360:Wend
 While boaty(i)>my+distboatmax:boaty(i)-=2*distboatmax:boato1(i)=Rnd*360:Wend
If testmort(i)=1 Then
	boatz(i)-=kfps*0.22
	soundsubwater
	If boatz(i)<(waterz-400) Then
		testmort(i)=0
		resetboat(i)
	EndIf
Else
 If boatwar(i)<>0 And Rnd<0.3*kfps Then
 	If typeboat(i)=0 Or typeboat(i)=4 Then tirboat(i)
 EndIf
 co1=Cos(boato1(i)*degtorad)
 si1=Sin(boato1(i)*degtorad)
 mz10=-999999:boato10=boato1(i)
 distboatx(i)=max(Abs(boatx(i)-mx),Abs(boaty(i)-my))
 If distboatx(i)<(dxwater*scalex) And mz<(waterz+altwater) Then 
  mmx=dxboat(i)+0.1
  mmy=dyboat(i)+0.1
  dmmx1=getterrainheight(boatx(i)+mmx*co1,boaty(i)+mmx*si1)
  dmmx3=getterrainheight(boatx(i)-mmx*co1,boaty(i)-mmx*si1)
  dmmx2=(dmmx1+dmmx3)/2
  boatz(i)=dmmx2
  If getterrainheight(boatx(i),boaty(i))<waterz Then boatz(i)=min(waterz-0.001,boatz(i))
  boato2(i)=Atn((dmmx1-dmmx2)/(mmx))*radtodeg
  dmmy=getterrainheight(boatx(i)-mmy*si1,boaty(i)+mmy*co1) - _ 
       getterrainheight(boatx(i)+mmy*si1,boaty(i)-mmy*co1)
  boato3(i)=-Atn(dmmy/(2*mmy))*radtodeg
  If typeboat(i)=4 Then 
  	  boato2(i)*=0.4:boato3(i)*=0.4
  EndIf
  If (myboat0=i Or myboat0=0) And mz<(waterz+mzh+200)  Then
   If distboatx(i)<dxboatmax(i) Then 
  	 y1=-(mx-boatx(i))*si1+(my-boaty(i))*co1
  	 If Abs(y1)<(mmy+mmy) Then
  	 	mmx=dxboatmax(i)
   	x1=(mx-boatx(i))*co1+(my-boaty(i))*si1
  	   If x1<(mmx) And x1>(mmx*0.3) Then
  	   	mz10=dmmx1+dzboat1(i)+mzh
  	   Else 
  	      If x1<=(mmx*0.31) And x1>=(-mmx*0.31) Then
  	    	   mz10=dmmx2+dzboat2(i)+mzh
  	      Else 
      	   If x1<(-mmx*0.3) And x1>(-mmx) Then
  	         	mz10=dmmx3+dzboat3(i)+mzh
  	       	EndIf 
  	   	EndIf 
  	   EndIf
  	 EndIf 
   EndIf
   If mz10>-99999 Then
   	If mz<=(mz10+30) Or plane=0 Then myboat=i
   	If typeboat(i)=4 Then 'carrier
   		pistez=mz10
   	EndIf
   EndIf
  EndIf
 Else 
  boatz(i)=getterrainheight(boatx(i),boaty(i))
  boato2(i)=0:boato3(i)=0
 EndIf
 If boatz(i)<=waterz And Abs(boatdo1(i))<0.1 Then
  co10=co1:si10=si1
  If vboat(i)<0 Then co1=-co1:si1=-si1	
  If testboat(i,boatx(i)+co1*dxboatmax(i)-si1*2*dyboat(i),boaty(i)+dxboatmax(i)*si1+co1*2*dyboat(i))>=1 Then
  	boatdo1(i)=-1
  Else 
   If testboat(i,boatx(i)+co1*dxboatmax(i)+si1*2*dyboat(i),boaty(i)+dxboatmax(i)*si1-co1*2*dyboat(i))>=1 Then
 	  boatdo1(i)=1
   EndIf 
  EndIf 
  'If testboat(i,boatx(i)+kfps*co1*vboat(i),boaty(i)+kfps*si1*vboat(i))=1 Then
  test=testboat(i,boatx(i)+0.8*co1*dxboatmax(i),boaty(i)+0.8*si1*dxboatmax(i))
  If test>=1 Then 
 	  If boatdo1(i)<=0 Then boatdo1(i)=-1 Else boatdo1(i)=1
     If test>=2 Then boatdo1(i)*=8 
  EndIf   
  If Abs(boatdo1(i))<0.1 Then 
   	If getterrainheight(boatx(i)+co1*1700-si1*90,boaty(i)+si1*1700+co1*90)>=(waterz) Then
 	   	'boatdo1(i)=-0.4
 	      boatdo10(i)=-0.4:boatturn(i)=40
 	   Else
 	    	If getterrainheight(boatx(i)+co1*1700+si1*90,boaty(i)+si1*1700-co1*90)>=(waterz) Then
 	    		'boatdo1(i)=0.4
 	    		boatdo10(i)=0.4:boatturn(i)=40
   	   EndIf 
   	EndIf
  EndIf
  If Rnd<0.1 Then
     If Abs(boatdo1(i))>0.1 Then boatdo1(i)=-boatdo1(i)'(Rnd-0.5)*2
  EndIf
  co1=co10:si1=si10
 EndIf
 If Abs(boatdo1(i))>0.1 Then 
 	If Abs(tboatturn(i))<=400 Then
 		do1=boatdo1(i)*kfps*abs(vboat(i))/vboat0(i)
 		boato1(i)+=do1
 		tboatturn(i)+=do1
 	Else 
 	   boatx(i)+=kfps*co1*vboat(i)
 	   boaty(i)+=kfps*si1*vboat(i)
 	EndIf
   boatturn(i)=0	
 EndIf 
 boatdo100=boatdo1(i)
 If typeboat(i)=0 Then vboat(i)+=0.2*kfps*(0.25*(vboat0(i)-vboat(i))-0.27*boatpoids(i)*Sin(boato2(i)))
 If typeboat(i)=4 Then vboat(i)+=0.2*kfps*(0.25*(vboat0(i)-vboat(i))-0.27*boatpoids(i)*Sin(boato2(i)))
 If typeboat(i)>=1 And typeboat(i)<=3 Then
 	 vwind=( vboat0(i)*(0.065+0.015)/(0.065+0.005) )*(0.92+kwave*0.08)
 	 ddir=boato1(i)-windo1
 	 While boato1(i)>180:boato1(i)-=360:Wend
 	 While boato1(i)<-180:boato1(i)+=360:Wend
 	 While ddir>180:ddir-=360:Wend
 	 While ddir<-180:ddir+=360:wend
 	 ddir=ddir*degtorad
    vboat(i)+=0.17*kfps*(0.065*(vwind-vboat(i))*cos((ddir)/2)+0.005*vwind*Cos(ddir)-0.015*vboat(i) _  
              -0.27*boatpoids(i)*Sin(boato2(i))) 	
    boato3(i)-=Sin(ddir)*0.2
 EndIf
 If Abs(boatdo1(i))<2 Then
    boatx2=boatx(i)+kfps*co1*vboat(i)
    boaty2=boaty(i)+kfps*si1*vboat(i)
 Else 
    boatx2=boatx(i)+kfps*co1*vboat(i)*0.2
    boaty2=boaty(i)+kfps*si1*vboat(i)*0.2
 EndIf 
 If getterrainheight(boatx2,boaty2)<waterz Or boatz(i)>=waterz Then
	boatdo1(i)=0
 	If tboatturn(i)>kfps Then tboatturn(i)-=kfps*0.5
 	If tboatturn(i)<-kfps Then tboatturn(i)+=kfps*0.5
 	boatx(i)=boatx2
 	boaty(i)=boaty2
   If boatturn(i)>0.1 Then
   	boatturn(i)-=1
   	boato1(i)+=boatdo10(i)*kfps*Abs(vboat(i))/vboat0(i)
   Else 
      If Rnd<0.1 Or i=9 Then 
    	  If Rnd<0.1/(1+fps) Then
    	  	 If time2<timeboat(i) Or time2>(timeboat(i)+500) Then 
   		   boatturn(i)=Rnd*90/0.4
   		   If i<>9 Then 
   		   	boatdo10(i)=sgn2(Rnd-0.5)*0.4
   		   Else
   		      boatdo10(i)=0.4*Sgn(co1*(boaty(10)+4000-boaty(i))-si1*(boatx(10)-4000-boatx(i)))
   		   EndIf
    	    EndIf 
    	  EndIf 
   	EndIf
   EndIf
 Else 
 	If boatdo1(i)<0.15 Then boatdo1(i)=sgn2(Rnd-0.5)
 EndIf
 If plane>=0 And mz10>-99999 Then
    mz11=mz10
 	 If Abs(boatdo100)<0.5 Then 
   	If guitestkey(vk_1) Or guitestkey(vk_numpad7) Or (hatx=-1 And plane=0) Or _ 
        (plane=0 And guitestkey(vk_numpad4)) Then boato1(i)+=kfps*0.8*vboat(i)/vboat0(i):timeboat(i)=time2
 	   If guitestkey(vk_2) Or guitestkey(vk_numpad9) Or (hatx=1 And plane=0) Or _  
 	     (plane=0 And guitestkey(vk_numpad6)) Then boato1(i)-=kfps*0.8*vboat(i)/vboat0(i):timeboat(i)=time2
 	 EndIf 
    If typeboat(i)=4 And Abs(boatdo1(i))<0.1 Then boato1(i)+=0.7*(boato10-boato1(i))
 	 o1+=boato1(i)-boato10
    co1=Cos(boato1(i)*degtorad)
    si1=Sin(boato1(i)*degtorad)
 	 mx=boatx(i)+co1*x1-si1*y1
 	 my=boaty(i)+si1*x1+co1*y1
 Else 	 
    If typeboat(i)=4 And Abs(boatdo1(i))<0.1 Then boato1(i)+=0.7*(boato10-boato1(i))
 EndIf
 boatco1(i)=co1
 boatsi1(i)=si1
EndIf 
Next i
End Sub
Sub testpiste
Dim As Integer i
piste=0
If myboat<>0 Then piste=1
End Sub
'Dim Shared As Single x1,y1,z1,x2,y2,z2
Sub drawvoile
  If voilelist<>0 Then
     glcalllist voilelist
  Else	
    voilelist=glgenlists(1)
    glnewlist voilelist,gl_compile 
    loadobjsize("objects/_voile.obj",@"",@"",310)
    glendlist    
  EndIf 	
End Sub
Sub drawvoile2
  If voile2list<>0 Then
     glcalllist voile2list
  Else	
    voile2list=glgenlists(1)
    glnewlist voile2list,gl_compile 
    loadobjsize("objects/_voile2.obj",@"",@"",310)
    glendlist    
  EndIf 	
End Sub
Sub drawboat
Dim As uint i,scale
Dim As Single do1
glenable(gl_texture_2d)
For i=1 To nboat2
 If typeboat(i)=0 Then 
    glenable(gl_lighting)
    glenable(gl_normalize)
	 gldisable(gl_light1)
    glbindtexture(gl_texture_2d,boattext)
 Else
 	 If typeboat(i)=1 Then  
       'gldisable(gl_lighting)
       glenable(gl_lighting)
       glenable(gl_normalize)
	    glenable(gl_light1)
       glbindtexture(gl_texture_2d,boattext2)
 	 Else 
       If typeboat(i)=2 Or typeboat(i)=3 Then 
          glenable(gl_lighting)
          glenable(gl_normalize)
	       gldisable(gl_light1)
          glbindtexture(gl_texture_2d,boattext3)
       Else 
          glenable(gl_lighting)
          glenable(gl_normalize)
	       gldisable(gl_light1)
          glbindtexture(gl_texture_2d,boattext4)
       EndIf 	
 	 EndIf 	
 EndIf   
 rotavion(boatx(i)-mx,boaty(i)-my,boatz(i)-mz)
 If x2>(max(Abs(y2),Abs(z2))-dxboatmax(i)-80.500) And boatz(i)<=(waterz+0.1) Then 	
  If max(Abs(boatx(i)-mx),Abs(boaty(i)-my))<dxterrain*scalex Then 	
   glpushmatrix
   gltranslatef(boatx(i),boaty(i),boatz(i))
   glrotatef(boato1(i),0,0,1)
   If Abs(boato2(i))>1 Then glrotatef(-boato2(i),0,1,0)
   If Abs(boato3(i))>1 Then glrotatef(-boato3(i),1,0,0)
   If boatlist(typeboat(i))<>0 Then
     glcalllist boatlist(typeboat(i))
     If typeboat(i)=3 Then 
        gltranslatef(-350,0,90)
        do1=boato1(i)-windo1
        While do1<-180:do1+=360:Wend
        While do1>180:do1-=360:Wend
        If do1<-5 And do1>-175 Then voileboat(i)=-1
        If do1>5 And do1<175 Then voileboat(i)=1
        If voileboat(i)<0 Then
        	  If do1>0 Then do1=-do1
           glrotatef(-30-do1*0.4,0,0,1)
           drawvoile
        Else
        	  If do1<0 Then do1=-do1
           glrotatef(30-do1*0.4,0,0,1)
           drawvoile2
        EndIf 	
     EndIf
     glpopmatrix
   Else	
    boatlist(typeboat(i))=glgenlists(1)
    glnewlist boatlist(typeboat(i)),gl_compile 
    glcolor3f(0.75,0.75,0.9)
    Select Case typeboat(i)
    	Case 0
    	loadobjsize("objects/ship_lowpoly.obj",@"",@"",500)
      Case 1 
        loadobjsize("objects/sailship.obj",@"",@"",358)
    	Case 2,3
        loadobjsize("objects/_vessel.obj",@"",@"",600)
      Case Else 
        loadobjsize("objects/shipcarrier.obj",@"",@"",1800)
    End Select 
    glendlist
    glpopmatrix
   EndIf 
  EndIf 
 EndIf  
Next i
gldisable(gl_light1)
End Sub 
#Include "./airship.bi"
Dim Shared As Single tsun,kxsoleil,kysoleil,kzsoleil,ksoleil,heure
Sub drawstars
gldisable GL_LIGHTING
gldisable GL_DEPTH_TEST
glEnable GL_TEXTURE_2D
glbindtexture(gl_texture_2d,startext)
If heure<6 Or heure>20 Then 
	aux=1'210/256'184/256'170/256
Else 
	aux=0.34
EndIf
glcolor3f(aux,aux,aux)
glpushmatrix
gltranslatef( mx,my,mz)
glrotatef(-heure*170/24,0,0,1)
glrotatef(-50*(1-Abs(heure-12)/12),0,1,0)
gltranslatef( 100000,0,0)
gltexcarre3(200000,200000)
gltranslatef( -100000,100000,0)
glrotatef(90,0,0,1)
gltexcarre3(200000,200000)
gltranslatef( -100000,100000,0)
glrotatef(90,0,0,1)
gltexcarre3(200000,200000)
gltranslatef( -100000,100000,0)
glrotatef(90,0,0,1)
gltexcarre3(200000,200000)
gltranslatef( -100000,0,100000)
glrotatef(90,0,1,0)
gltexcarre3(200000,200000)
glEnable GL_DEPTH_TEST
glpopmatrix
End Sub
Sub drawsun
Dim As Single x,y,z,aux
    If guitestkey(vk_f3) Then
    	confirm("change time of day ?","confirm",resp)
    	If resp="yes" Then
    	  timehour1=heure	
        If timehour1<=6 Or timehour1>=20 Then timehour2=6.1
    	  If timehour1>6 And timehour1<=8 Then timehour2=12
        If timehour1>8 And timehour1<=18 Then timehour2=19.1
        If timehour1>18 And timehour1<20 Then timehour2=1.5
        timehour1=timehour2
        dtimehour=(timehour2-timehour+24)
        While dtimehour>24:dtimehour-=24:Wend 
        tsun=0
     	EndIf   
    EndIf 
tsun-=1
if tsun<0 then
  tsun=200'200
  timehour=val(Left(Time(),2))+Val(Mid(Time(),4,2))/60
  'heure=0.1*(Timer Mod 240)
  heure=timehour+dtimehour
  While heure>24:heure-=24:Wend 
  'x=0-800*(12-heure):y=6000:z=410*(13.5-abs(12-heure))
  x=0-800*(12-heure):y=6000:z=490*(12-abs(12-heure))
  aux=0.8*10000*4*(1-Abs(12-heure)/140)/Sqr(x*x+y*y+z*z)
  kxsoleil=aux*x:kysoleil=aux*y:kzsoleil=aux*z
  'ksoleil=1-Abs(12-heure)/50'25
  If heure<=4 or heure>=21 Then
  	  ksoleil=0.4
  EndIf 
  If heure>4 And heure<=6 Then
  	  aux=(heure-4)/2
  	  ksoleil=0.74*aux+(1-aux)*0.4
  EndIf     
  If heure>6 And heure<=8 Then
  	  aux=(heure-6)/2
  	  ksoleil=1.0*aux+(1-aux)*0.74
  EndIf
  If heure>8 And heure<=17 Then
  	  ksoleil=1.0
  EndIf
  If heure>17 And heure<=19 Then 
  	  aux=(19-heure)/2
  	  ksoleil=1.0*aux+(1-aux)*0.74 
  EndIf
  If heure>19 And heure<=21 Then 
  	  aux=(21-heure)/2
  	  ksoleil=0.74*aux+(1-aux)*0.4
  EndIf
  'ksoleil=1-Abs(12-heure)/35
  'ksoleil=ksoleil*ksoleil
  'glClearColor 0.5, 0.5*ksoleil, 1.0*ksoleil, 0.0
End if
End Sub
Sub drawsunset
Dim As Single aux
aux=ksoleil'0.5
glClearColor 0.5*aux, 0.5*aux, 1.0*aux, 0.0
glpushmatrix
'gltranslatef( mx+40000,my,mz-5000)
gltranslatef( mx-kxsoleil*5,my-kysoleil*5,mz+kzsoleil*5)
glrotatef(-heure*170/24,0,0,1)
glrotatef(-50*(1-Abs(heure-12)/12),0,1,0)
gldisable GL_LIGHTING
'glenable GL_LIGHTING
gldisable GL_DEPTH_TEST
'glEnable GL_COLOR_LOGIC_OP
glEnable GL_TEXTURE_2D
'glLogicOp GL_OR
glbindtexture(gl_texture_2d,sunsettext)
aux=210/256'184/256'170/256
glcolor3f(aux,aux,aux*0.6)
'glcolor3f( aux,aux*ksoleil,aux*ksoleil)
gltexcarre3(20000*5,18000*5)
'glDisable GL_COLOR_LOGIC_OP
glEnable GL_DEPTH_TEST
glpopmatrix
End Sub
Dim Shared As Single owind,tcielx,tciely,ciellist
Dim Shared As uint cieltext 
Sub drawciel
'If heure<=3.2001 Or heure>=20.8001 Then drawsunset
glEnable GL_BLEND
glBlendFunc GL_SRC_color,GL_ONE'_MINUS_SRC_color
If heure<8 or heure>18 Then drawstars
drawsunset
If iciel>1 Then
gldisable GL_DEPTH_TEST
glcolor3f(ksoleil,ksoleil,ksoleil)'1,1,1)
glpushmatrix
Dim As Single x,y,xtex,ytex,aux=25000 '5000
owind=(owind+(rnd(1)-0.5))
if owind<0 then owind=owind+360
if owind>360 then owind=owind-360
tcielx=int(tcielx+8*kfps*cos(owind*degtorad))mod aux
tciely=int(tciely+8*kfps*sin(owind*degtorad))mod aux
gltranslatef (tcielx+aux*(Int(400.5+mx/aux)-400),tciely+aux*(Int(400.5+my/aux)-400),3500)
If ciellist<>0 Then
	glCallList ciellist:glpopmatrix
Else 
 ciellist=glgenlists(1)
 glNewList ciellist , gl_compile
 gldisable GL_LIGHTING
 'glenable GL_LIGHTING
 'glEnable GL_COLOR_LOGIC_OP
 glEnable GL_TEXTURE_2D
 glLogicOp GL_OR
 'glColor3f( 120/255,140/255,255/255)
 'glcolor3f(1,1,1)
 glbindtexture(gl_texture_2d,cieltext)
 x=-50000
 y=0-x
 xtex=2*50000/aux:ytex=xtex
 glBegin GL_QUADS
 'glnormal3f(0,0,1)
 glTexCoord2f(0,0)
 glVertex3f(x,x,0)
 glTexCoord2d(xtex,0)
 glVertex3f(y,x,0)
 glTexCoord2f(xtex,ytex)
 glVertex3f(y,y,0)
 glTexCoord2f(0,ytex)
 glVertex3f(x,y,0)
 glEnd
 'glDisable GL_COLOR_LOGIC_OP
 glDisable GL_TEXTURE_2D
 'gldisable GL_LIGHTING  
 glEndList
 glpopmatrix
EndIf 
EndIf 
glEnable GL_DEPTH_TEST
gldisable GL_BLEND
End Sub
'Dim Shared As Single x1,y1,z1,x2,y2,z2
Sub rotavion(ByVal x As Single,ByVal y As Single,ByVal z As Single)
If tourelle=0 Then 
 x1=x*cos1+y*sin1
 'y1=0-x*sin1+y*cos1
 'z1=z
 x2=x1*cos2+z*sin2
 'y2=y1
 y2=-x*sin1+y*cos1
 z2=-x1*sin2+z*cos2
Else
 x1=x*tcos1+y*tsin1
 'y1=0-x*tsin1+y*tcos1
 'z1=z
 x2=x1*tcos2+z*tsin2
 'y2=y1
 y2=-x*tsin1+y*tcos1
 z2=-x1*tsin2+z*tcos2
EndIf  
End Sub
Sub rotavion2(ByVal x As Single,ByVal y As Single)
If tourelle=0 Then 
 x2=x*cos1+y*sin1
 'y1=0-x*sin1+y*cos1
 'z1=z
 'x2=x1'*cos2+z*sin2
 'y2=y1
 y2=-x*sin1+y*cos1
 'z2=-x1*sin2+z*cos2
Else
 x2=x*tcos1+y*tsin1
 'y1=0-x*tsin1+y*tcos1
 'z1=z
 'x2=x1'*tcos2+z*tsin2
 'y2=y1
 y2=-x*tsin1+y*tcos1
 'z2=-x1*tsin2+z*tcos2
EndIf  
End Sub
Dim Shared As Single tircos1,tirsin1,tircos2,tirsin2,tirdist
Dim Shared As Single impactdx,impactx,impacty,impactz,rimpact
Dim Shared As Integer impact 
Sub rotaviontir2(ByVal x As Single,ByVal y As Single,ByVal z As Single,_
	             ByRef x2 As Single,ByRef y2 As Single,ByRef z2 As single)
 x1=x*tircos1+y*tirsin1
 'y1=0-x*sin1+y*cos1
 'z1=z
 x2=x1*tircos2+z*tirsin2
 'y2=y1
 y2=-x*tirsin1+y*tircos1
 z2=-x1*tirsin2+z*tircos2
End Sub 
Dim Shared As Single avx,avy,avz,avx1,avy1,avz1,avx2,avy2,avz2,avx3,avy3,avz3
Dim Shared As Single x3,y3,z3,x30,y30,z30,disttir
Dim Shared As Integer explosion1,impact40
Sub rotaviontir(ByVal avx As Single,ByVal avy As Single,ByVal avz As Single)
If tourelle=0 Then 
 avx3=avx
 avy3=avy*cos3+avz*sin3
 avz3=-avy*sin3+avz*cos3
 avx2=avx3*cos2-avz3*sin2
 avy2=avy3
 avz2=avx3*sin2+avz3*cos2
 avx1=avx2*cos1-avy2*sin1
 avy1=avx2*sin1+avy2*cos1
 avz1=avz2
Else
 avx3=avx
 avy3=avy*tcos3+avz*tsin3
 avz3=-avy*tsin3+avz*tcos3
 avx2=avx3*tcos2-avz3*tsin2
 avy2=avy3
 avz2=avx3*tsin2+avz3*tcos2
 avx1=avx2*tcos1-avy2*tsin1
 avy1=avx2*tsin1+avy2*tcos1
 avz1=avz2
EndIf  
End Sub
Dim Shared As Single mcos1,msin1,mcos2,msin2,mcos3,msin3
Sub rotationMd2(ByVal avx As Single,ByVal avy As Single,ByVal avz As Single) 
 avx3=avx
 avy3=avy*mcos3+avz*msin3
 avz3=-avy*msin3+avz*mcos3
 avx2=avx3*mcos2-avz3*msin2
 avy2=avy3
 avz2=avx3*msin2+avz3*mcos2
 avx1=avx2*mcos1-avy2*msin1
 avy1=avx2*msin1+avy2*mcos1
 avz1=avz2
End Sub  
Sub rotationMd22(ByVal avx As Single,ByVal avy As Single,ByVal avz As Single) 
 avx1=avx*mcos1+avy*msin1
 avy1=-avx*msin1+avy*mcos1
 avz1=avz
 avx2=avx1*mcos2+avz1*msin2
 avy2=avy1
 avz2=-avx1*msin2+avz1*mcos2
 avx3=avx2
 avy3=avy2*mcos3-avz2*msin3
 avz3=avy2*msin3+avz2*mcos3
End Sub 
Function testtirtriangle(ByVal tx1 As Single,ByVal ty1 As Single,ByVal tz1 As Single, _
	                      ByVal tx2 As Single,ByVal ty2 As Single,ByVal tz2 As Single, _
	                      ByVal tx3 As Single,ByVal ty3 As Single,ByVal tz3 As Single, _
	                      ByVal rtir As Single = 512 ) As Integer 
Dim As Single x1,y1,z1,x2,y2,z2,x3,y3,z3,a,b,det,x
rotaviontir2(tx1-tirx,ty1-tiry,tz1-tirz,x1,y1,z1)
If Abs(y1)>rtir Then Return 0
If Abs(z1)>rtir Then Return 0
If x1>(tirdist+rtir) Then Return 0
If x1<(-rtir) Then Return 0
rotaviontir2(tx2-tirx,ty2-tiry,tz2-tirz,x2,y2,z2)
rotaviontir2(tx3-tirx,ty3-tiry,tz3-tirz,x3,y3,z3)
'x1+a*(x2-x1)+b*(x3-x2)=x
'y1+a*(y2-y1)+b*(y3-y2)=0 '0<a<1 '0<b<a 
'z1+a*(z2-z1)+b*(z3-z2)=0
'b=-(y1+a*(y2-y1))/(y3-y2) 
det=(z2-z1)*(y3-y2)-(y2-y1)*(z3-z2)
If Abs(det)<0.01 Then Return 0
a=(-z1*(y3-y2)+y1*(z3-z2))/det
If a<0 Or a>1 Then Return 0
b=(z1*(y2-y1)-y1*(z2-z1))/det
If b<0 Or b>a Then Return 0
x=x1+a*(x2-x1)+b*(x3-x2)
If x<0 Or x>tirdist Then Return 0
impactdx=x
tirdist=x
impact=1
Return 1
End Function
Function testtircarre(ByVal tx1 As Single,ByVal ty1 As Single,ByVal tz1 As Single, _
	                      ByVal tx2 As Single,ByVal ty2 As Single,ByVal tz2 As Single, _
	                      ByVal tx3 As Single,ByVal ty3 As Single,ByVal tz3 As Single, _
	                      ByVal rtir As Single = 512 ) As Integer 
Dim As Single x1,y1,z1,x2,y2,z2,x3,y3,z3,a,b,det,x
rotaviontir2(tx1-tirx,ty1-tiry,tz1-tirz,x1,y1,z1)
If Abs(y1)>rtir Then Return 0
If Abs(z1)>rtir Then Return 0
If x1>(tirdist+rtir) Then Return 0
If x1<(-rtir) Then Return 0
rotaviontir2(tx2-tirx,ty2-tiry,tz2-tirz,x2,y2,z2)
rotaviontir2(tx3-tirx,ty3-tiry,tz3-tirz,x3,y3,z3)
'x1+a*(x2-x1)+b*(x3-x1)=x
'y1+a*(y2-y1)+b*(y3-y1)=0 '0<a<1 '0<b<1 
'z1+a*(z2-z1)+b*(z3-z1)=0
'b=-(y1+a*(y2-y1))/(y3-y1) 
det=(z2-z1)*(y3-y1)-(y2-y1)*(z3-z1)
If Abs(det)<0.001 Then Return 0
a=(-z1*(y3-y1)+y1*(z3-z1))/det
If a<0 Or a>1 Then Return 0
b=(z1*(y2-y1)-y1*(z2-z1))/det
If b<0 Or b>1 Then Return 0
x=x1+a*(x2-x1)+b*(x3-x1)
If x<0 Or x>tirdist Then Return 0
impactdx=x
tirdist=x
impact=1
Return 1
End Function
Function testtirMd2(ByVal md2node As md2_node Ptr) As Integer 
Dim As Single x0,y0,z0,x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4,sx,sy,sz
Dim As Integer test=0 
sx=md2node->scalex
sy=md2node->scaley
sz=md2node->scalez
bxmin=md2node->md2model->bxmin*sx
bymin=md2node->md2model->bymin*sy
bzmin=md2node->md2model->bzmin*sz
bxmax=md2node->md2model->bxmax*sx
bymax=md2node->md2model->bymax*sy
bzmax=md2node->md2model->bzmax*sz
rmax=Abs(bxmin)
If rmax<Abs(bymin) Then rmax=Abs(bymin)
If rmax<Abs(bzmin) Then rmax=Abs(bzmin)
If rmax<Abs(bxmax) Then rmax=Abs(bxmax)
If rmax<Abs(bymax) Then rmax=Abs(bymax)
If rmax<Abs(bzmax) Then rmax=Abs(bzmax)
x0=md2node->x
y0=md2node->y
z0=md2node->z
rotaviontir2(x0-tirx,y0-tiry,z0-tirz,x1,y1,z1)
If Abs(y1)>rmax Then Return 0
If Abs(z1)>rmax Then Return 0
If x1>(tirdist+rmax) Then Return 0
If x1<(-rmax) Then Return 0
rmax=rmax*2*1.41	
mcos1=Cos(md2node->rotz*degtorad)
msin1=Sin(md2node->rotz*degtorad)
mcos2=Cos(-md2node->roty*degtorad)
msin2=Sin(-md2node->roty*degtorad)
mcos3=Cos(-md2node->rotx*degtorad)
msin3=Sin(-md2node->rotx*degtorad)
rotationMd2(bxmin,bymin,bzmin)
x1=x0+avx1:y1=y0+avy1:z1=z0+avz1
rotationMd2(bxmax,bymin,bzmin)	
x2=x0+avx1:y2=y0+avy1:z2=z0+avz1
rotationMd2(bxmin,bymax,bzmin)	
x3=x0+avx1:y3=y0+avy1:z3=z0+avz1
rotationMd2(bxmin,bymin,bzmax)	
x4=x0+avx1:y4=y0+avy1:z4=z0+avz1
/'
test=test Or testtircarre(x1,y1,z1,x2,y2,z2,x3,y3,z3,rmax)
'gltriangle(x1,y1,z1,x2,y2,z2,x3,y3,z3)
test=test Or testtircarre(x4,y4,z4,x4+(x2-x1),y4+(y2-y1),z4+(z2-z1),x4+(x3-x1),y4+(y3-y1),z4+(z3-z1),rmax)
'gltriangle(x4,y4,z4,x4+(x2-x1),y4+(y2-y1),z4+(z2-z1),x4+(x3-x1),y4+(y3-y1),z4+(z3-z1))
test=test Or testtircarre(x1,y1,z1,x2,y2,z2,x4,y4,z4,rmax)
'gltriangle(x1,y1,z1,x2,y2,z2,x4,y4,z4)
test=test Or testtircarre(x3,y3,z3,x3+(x2-x1),y3+(y2-y1),z3+(z2-z1),x3+(x4-x1),y3+(y4-y1),z3+(z4-z1),rmax)
'gltriangle(x3,y3,z3,x3+(x2-x1),y3+(y2-y1),z3+(z2-z1),x3+(x4-x1),y3+(y4-y1),z3+(z4-z1))
test=test Or testtircarre(x1,y1,z1,x3,y3,z3,x4,y4,z4,rmax)
'gltriangle(x1,y1,z1,x3,y3,z3,x4,y4,z4)
test=test Or testtircarre(x2,y2,z2,x2+(x3-x1),y2+(y3-y1),z2+(z3-z1),x2+(x4-x1),y2+(y4-y1),z2+(z4-z1),rmax)
'gltriangle(x2,y2,z2,x2+(x3-x1),y2+(y3-y1),z2+(z3-z1),x2+(x4-x1),y2+(y4-y1),z2+(z4-z1))
'/
test=test Or testtircarre((x1+x4)*0.5,(y1+y4)*0.5,(z1+z4)*0.5,_   
                           x2+(x4-x1)*0.5,y2+(y4-y1)*0.5,z2+(z4-z1)*0.5,_
                           x3+(x4-x1)*0.5,y3+(y4-y1)*0.5,z3+(z4-z1)*0.5,rmax)
'gltriangle((x1+x4)*0.5,(y1+y4)*0.5,(z1+z4)*0.5,x2+(x4-x1)*0.5,y2+(y4-y1)*0.5,z2+(z4-z1)*0.5,_
'                           x3+(x4-x1)*0.5,y3+(y4-y1)*0.5,z3+(z4-z1)*0.5)
test=test Or testtircarre((x1+x2)*0.5,(y1+y2)*0.5,(z1+z2)*0.5,_   
                           x3+(x2-x1)*0.5,y3+(y2-y1)*0.5,z3+(z2-z1)*0.5,_
                           x4+(x2-x1)*0.5,y4+(y2-y1)*0.5,z4+(z2-z1)*0.5,rmax)
'gltriangle((x1+x2)*0.5,(y1+y2)*0.5,(z1+z2)*0.5,x3+(x2-x1)*0.5,y3+(y2-y1)*0.5,z3+(z2-z1)*0.5,_
'                           x4+(x2-x1)*0.5,y4+(y2-y1)*0.5,z4+(z2-z1)*0.5)
test=test Or testtircarre((x1+x3)*0.5,(y1+y3)*0.5,(z1+z3)*0.5,_  
                           x2+(x3-x1)*0.5,y2+(y3-y1)*0.5,z2+(z3-z1)*0.5,_
                           x4+(x3-x1)*0.5,y4+(y3-y1)*0.5,z4+(z3-z1)*0.5,rmax)
'gltriangle((x1+x3)*0.5,(y1+y3)*0.5,(z1+z3)*0.5,x2+(x3-x1)*0.5,y2+(y3-y1)*0.5,z2+(z3-z1)*0.5,_
'                           x4+(x3-x1)*0.5,y4+(y3-y1)*0.5,z4+(z3-z1)*0.5)
Return test
End Function  
Function testtirboat(ByVal iboat As Integer) As Integer  
Dim As Single x0,y0,z0,x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4,x42,y42,z42
Dim As Integer test=0 
bxmin=-dxboatmax(iboat)*0.9
bymin=-dyboat(iboat)*1.5
bzmin=0'-dyboat(iboat)
bxmax=dxboatmax(iboat)*0.9
bymax=-bymin
bzmax=max(dzboat1(iboat),dzboat2(iboat))'dyboat(iboat)*2.7'5.9'2.7
bzmax=max(dzboat3(iboat),bzmax)*1.5
rmax=Abs(bxmin)
If rmax<Abs(bymin) Then rmax=Abs(bymin)
If rmax<Abs(bzmin) Then rmax=Abs(bzmin)
If rmax<Abs(bxmax) Then rmax=Abs(bxmax)
If rmax<Abs(bymax) Then rmax=Abs(bymax)
If rmax<Abs(bzmax) Then rmax=Abs(bzmax)
x0=boatx(iboat)
y0=boaty(iboat)
z0=boatz(iboat)
rotaviontir2(x0-tirx,y0-tiry,z0-tirz,x1,y1,z1)
If Abs(y1)>rmax Then Return 0
If Abs(z1)>rmax Then Return 0
If x1>(tirdist+rmax) Then Return 0
If x1<(-rmax) Then Return 0
rmax=rmax*2*1.41	
mcos1=boatco1(iboat)
msin1=boatsi1(iboat)
mcos2=Cos(boato2(iboat)*degtorad)
msin2=Sin(boato2(iboat)*degtorad)
mcos3=Cos(boato3(iboat)*degtorad)
msin3=Sin(boato3(iboat)*degtorad)
rotationMd2(bxmin,bymin,bzmin)
x1=x0+avx1:y1=y0+avy1:z1=z0+avz1
rotationMd2(bxmax,bymin,bzmin)	
x2=x0+avx1:y2=y0+avy1:z2=z0+avz1
rotationMd2(bxmin,bymax,bzmin)	
x3=x0+avx1:y3=y0+avy1:z3=z0+avz1
rotationMd2(bxmin,bymin,bzmax)	
x4=x0+avx1:y4=y0+avy1:z4=z0+avz1
If typeboat(iboat)>=1 and typeboat(iboat)<=3 Then 
   rotationMd2(bxmin,bymin,max(170.0,bzmax*3.5))	
   x42=x0+avx1:y42=y0+avy1:z42=z0+avz1
 test=test Or testtircarre((x1*0.5+x2*0.5),(y1*0.5+y2*0.5),(z1*0.5+z2*0.5),_  
                           x3+(x2-x1)*0.5,y3+(y2-y1)*0.5,z3+(z2-z1)*0.5,_
                           x42+(x2-x1)*0.5,y42+(y2-y1)*0.5,z42+(z2-z1)*0.5,rmax)
 test=test Or testtircarre((x1*0.7+x2*0.3),(y1*0.7+y2*0.3),(z1*0.7+z2*0.3),_  
                           x3+(x2-x1)*0.3,y3+(y2-y1)*0.3,z3+(z2-z1)*0.3,_
                           x42+(x2-x1)*0.3,y42+(y2-y1)*0.3,z42+(z2-z1)*0.3,rmax)
 test=test Or testtircarre((x1*0.28+x2*0.72),(y1*0.28+y2*0.72),(z1*0.28+z2*0.72),_  
                           x3+(x2-x1)*0.72,y3+(y2-y1)*0.72,z3+(z2-z1)*0.72,_
                           x42+(x2-x1)*0.72,y42+(y2-y1)*0.72,z42+(z2-z1)*0.72,rmax)
EndIf    

test=test Or testtircarre(x1+0.59*(x4-x1),y1+0.59*(y4-y1),z4+0.59*(z4-z1), _   
                          x2+0.59*(x4-x1),y2+0.59*(y4-y1),z2+0.59*(z4-z1),_
                          x3+0.59*(x4-x1),y3+0.59*(y4-y1),z3+0.59*(z4-z1),rmax)
'test=test Or testtircarre((x1+x4)/2,(y1+y4)/2,(z1+z4)/2, x2+(x4-x1)/2,y2+(y4-y1)/2,z2+(z4-z1)/2,_
'                           x3+(x4-x1)/2,y3+(y4-y1)/2,z3+(z4-z1)/2,rmax)
'gltriangle((x1+x4)/2,(y1+y4)/2,(z1+z4)/2,x2+(x4-x1)/2,y2+(y4-y1)/2,z2+(z4-z1)/2,_
'                           x3+(x4-x1)/2,y3+(y4-y1)/2,z3+(z4-z1)/2)
test=test Or testtircarre( x1+(x2-x1)*0.1,y1+(y2-y1)*0.1,z1+(z2-z1)*0.1,_  
                           x3+(x2-x1)*0.1,y3+(y2-y1)*0.1,z3+(z2-z1)*0.1,_
                           x4+(x2-x1)*0.1,y4+(y2-y1)*0.1,z4+(z2-z1)*0.1,rmax)
test=test Or testtircarre( x1+(x2-x1)*0.8,y1+(y2-y1)*0.8,z1+(z2-z1)*0.8,_  
                           x3+(x2-x1)*0.8,y3+(y2-y1)*0.8,z3+(z2-z1)*0.8,_
                           x4+(x2-x1)*0.8,y4+(y2-y1)*0.8,z4+(z2-z1)*0.8,rmax)
'gltriangle((x1+x2)/2,(y1+y2)/2,(z1+z2)/2, x3+(x2-x1)/2,y3+(y2-y1)/2,z3+(z2-z1)/2,_
'                           x4+(x2-x1)/2,y4+(y2-y1)/2,z4+(z2-z1)/2)
test=test Or testtircarre((x1+x3)/2,(y1+y3)/2,(z1+z3)/2, x2+(x3-x1)/2,y2+(y3-y1)/2,z2+(z3-z1)/2,_
                           x4+(x3-x1)/2,y4+(y3-y1)/2,z4+(z3-z1)/2,rmax)
'gltriangle((x1+x3)/2,(y1+y3)/2,(z1+z3)/2, x2+(x3-x1)/2,y2+(y3-y1)/2,z2+(z3-z1)/2,_
'                           x4+(x3-x1)/2,y4+(y3-y1)/2,z4+(z3-z1)/2)
Return test	
End Function
Function testtirsphere(ByVal x As Single,ByVal y As Single,ByVal z As Single,ByVal r As single) As Integer  
Dim As Single x1,y1,z1
rotaviontir2(x-tirx,y-tiry,z-tirz,x1,y1,z1)
If Abs(y1)>r Then Return 0
If Abs(z1)>r Then Return 0
If x1>(tirdist+r) Then Return 0
If x1<(-r) Then Return 0
tirdist=max(1.0,min(tirdist,x1-r))
'auxvar3+=1
Return 1
End Function 
Function testtirbuilding(ByVal x As Single,ByVal y As Single,ByVal z As Single,_  
	                      ByVal h As Single,ByVal r As single) As Integer 
Dim As Integer i,test
If h<=r Then Return testtirsphere(x,y,z+h-r,r)
test=0
For i=0 To Int(h/r-1)
	test=test Or testtirsphere(x,y,z+i*r,r)
Next
test=test Or testtirsphere(x,y,z+h-r,r)
Return test
End Function
Function testtirterrain() As Integer 
Dim As Integer x,y,init,x0,y0,z0,x1,y1,z1,x2,y2,z2,x3,y3,z3,test=0
Dim As Integer i,j,i1,dx=30
dx=1+tirdist/scalex
For x=-dx+Int(tirx/scalex+10000)-10000 To dx+Int(tirx/scalex+10000)-10000
	i=x+256
	while i<0:i+=512:Wend 
	While i>=512:i-=512:Wend
	init=0 	
	For y=-dx+Int(tiry/scalex+10000)-10000 To dx+Int(tiry/scalex+10000)-10000
     j=y+256
	  While j<0:j+=512:Wend 
	  While j>=512:j-=512:Wend 
 	  x0=x2:y0=y2:z0=z2
 	  x1=x3:y1=y3:z1=z3
 	  x2=x*scalex:y2=y*scalex:z2=scalez*terrain(i,j)
 	  i1=i+1:If i1>=512 Then i1-=512
 	  x3=(x+1)*scalex:y3=y*scalex:z3=scalez*terrain(i1,j)
 	  If init>0 Then 
        test=test Or testtirtriangle(x0,y0,z0,x1,y1,z1,x2,y2,z2)
        test=test Or testtirtriangle(x1,y1,z1,x2,y2,z2,x3,y3,z3)
 	  EndIf 
 	  init=1
	Next
Next
Return test 
End Function 
Dim Shared As Integer itir,ntir=80,ftir(ntir),itesttir,typetir(ntir),ftir0=30'15
Dim Shared As Single vtirx(ntir),vtiry(ntir),vtirz(ntir)
Dim Shared As Single tirstartx(ntir),tirstarty(ntir),tirstartz(ntir)
Sub addtir(ByVal itypetir As Integer=0)
	itir=(itir+1)Mod ntir
	typetir(itir)=itypetir	
	ftir(itir)=ftir0
	aux=30'8*2
	tirstartx(itir)=tirx
	tirstarty(itir)=tiry
	tirstartz(itir)=tirz
	vtirx(itir)=(dtirx)/aux*(1+0.012*(Rnd-0.5))
	vtiry(itir)=(dtiry)/aux*(1+0.012*(Rnd-0.5))
	vtirz(itir)=(dtirz)/aux*(1+0.012*(Rnd-0.5))
End Sub
Sub addtirairship(ByVal i As Integer,ByVal itypetir As Integer=0)
Dim As Single disttir=20000
	tirx=airshipx(i)
	tiry=airshipy(i)
	tirz=airshipz(i)
	dtirx=disttir*airshipco1(i)*airshipco2(i)
	dtiry=disttir*airshipsi1(i)*airshipco2(i)
	dtirz=disttir*airshipsi2(i)
	addtir(itypetir)
End Sub
Sub addtir2(ByVal itypetir As Integer=0)
Dim As Single vtir=1000,disttir,aux
	itir=(itir+1)Mod ntir
	typetir(itir)=itypetir
	disttir=Sqr(dtirx*dtirx+dtiry*dtiry+dtirz*dtirz)
	ftir(itir)=min2(30,disttir/vtir)'ftir0
	aux=max2(1,disttir/vtir)	
	tirstartx(itir)=tirx
	tirstarty(itir)=tiry
	tirstartz(itir)=tirz
	vtirx(itir)=(dtirx/aux)*(1+0.2*(Rnd-0.5))
	vtiry(itir)=(dtiry/aux)*(1+0.2*(Rnd-0.5))
	vtirz(itir)=(dtirz/aux)*(1+0.2*(Rnd-0.5))
End Sub
Dim Shared As Single avionsmoke
Dim Shared As uint nhorse=17,md2ihorse(nhorse),testsoundshoot'nhorse=29
Dim Shared As Single horsex(nhorse),horsey(nhorse),horsez(nhorse),horseo1(nhorse),horseo2(nhorse)
Sub testtir(ByVal itypetir As Integer=0)
Dim As Integer ishiptir,typeship0,itower,ii,jj,ii1,jj1
ishiptir=itypetir-1000
If itypetir>2000 Then ishiptir=itypetir-2000
tirdist=Sqr(dtirx*dtirx +dtiry*dtiry +dtirz*dtirz +1e-20)
tirsin2=dtirz/tirdist
tircos2=Sqr(max(0.0001,1.0-tirsin2*tirsin2))
tircos1=dtirx/(tirdist*tircos2)
tirsin1=dtiry/(tirdist*tircos2)
impact=0
If itypetir<>1 Then 
 If testtirterrain() Then rimpact=65
 If itypetir=0 Then 
   For i=1 To nhorse
     If testtirMd2(md2_node(md2ihorse(i))) Then rimpact=40:impact=1000+i	
   Next
 EndIf   
 For i=1 To nboat2
  If boatz(i)<waterz Then 	
    If testtirboat(i) Then rimpact=80:impact=3000+i
  EndIf 
 Next
EndIf 
For i=1 To ndome
	If showdome(i)=1 Then
		If dometype(i)=0 Then 
			If testtirsphere(domex(i),domey(i),domez(i),domer(i)) Then
				rimpact=65:impact=4000+i
			EndIf
		Else
			If testtirsphere(domex(i),domey(i),domez(i)+50,domer(i)) Then
				rimpact=50:impact=4000+i
			EndIf
		EndIf 	
	EndIf
Next
For i=1 To nairport2
  If showairport(i)=1 Then 
	Select Case airporttype(i)
	   Case 1,3 'tower
			If testtirsphere(airportx(i)-dmx0,airporty(i)-dmy0,airportz(i)+50,airportr(i)) Then
				rimpact=50:impact=5000+i
				itower=1
			EndIf		
	   Case 2,4 'dome
			If testtirsphere(airportx(i)-dmx0,airporty(i)-dmy0,airportz(i)+50,airportr(i)) Then
				rimpact=65:impact=5000+i
				itower=1
			EndIf
		Case 5
			If testtirsphere(airportx(i)-dmx0,airporty(i)-dmy0,airportz(i)+50,airportr(i)) Then
				rimpact=50:impact=5000+i
				itower=1
			EndIf
			If testtirsphere(airportx(i)-dmx0,airporty(i)-dmy0-470,airportz(i)+50,airportr(i)) Then
				rimpact=50:impact=5000+i
				itower=2
			EndIf
			If testtirsphere(airportx(i)-dmx0,airporty(i)-dmy0-470-470,airportz(i)+50,airportr(i)) Then
				rimpact=50:impact=5000+i
				itower=3
			EndIf
	End Select	
  EndIf
Next
For i=1 To ntown2
	If showtown(i)=1 Then
		For ii=0 To 6
			For jj=0 To 6
			 If townbuild(i,ii,jj)>0 Then 	
				If testtirbuilding(towndx(i,ii,jj)-dmx0,towndy(i,ii,jj)-dmy0,_ 
				                   towndz(i,ii,jj),townh(i,ii,jj),200) Then
				   ii1=ii:jj1=jj
				   rimpact=65:impact=6000+i                
			   EndIf                  
			 EndIf 
			Next
		Next
	EndIf
Next
'$$$If testtirMd2(md2_node(md2iavion)) Then rimpact=30:turnavion=Rnd*200:impact=2000
If plane<>2 Or itypetir<>0 Then
	If testtirsphere(avionx,aviony,avionz,90) Then rimpact=30:impact=2000
EndIf
For i=1 To nship2
	If i<>ishiptir Then 
	 If typeship(i)=4 Then 'b25	
	   If testtirsphere(airshipx(i),airshipy(i),airshipz(i),160) Then 
		  rimpact=30:impact=2000+i
	   EndIf 
	 Else
	   If testtirsphere(airshipx(i),airshipy(i),airshipz(i),97) Then 
		  rimpact=30:impact=2000+i
	   EndIf 
	 EndIf 	
	EndIf
Next
If itypetir<>0 Then 
	If itypetir=1 Then 'boat
		If testtirsphere(mx,my,mz,57)=1 Then impact=10000
	Else 
      'If testtirsphere(mx,my,mz,90)=1 Then impact=10000
      If testtirsphere(mx,my,mz,170)=1 Then impact=10000
	EndIf
EndIf

If impact>1000 And impact<2000 Then
  i=impact-1000
  soundhorse
  horsex(i)+=70*cos1
  horsey(i)+=70*sin1
  horsez(i)=getterrainheight(horsex(i),horsey(i))
  setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))			
EndIf
If impact=2000 Then 
   turnavion=Rnd*200
   avionvie-=force*(1+Rnd)*5.9*0.5*kfps/(1.0+klevel)
   If avionvie<avionvie0*0.4 And Rnd<0.4 Then avionsmoke=4000	
EndIf
If impact>2000 And impact<3000 Then
	i=impact-2000
	typeship0=typeship(i)
   If itypetir=0 And _   
    ((typeship0<>1 And typeship0<>0 And typeship0<5) or rnd<0.1) Then
   	airshipwar(i)=10000
      target=i:typetarget=2:targetdist=0 'airship
   EndIf
   airshipvie(i)-=force*((1+Rnd)*1.3/(1.0+klevel))*kfps
	If airshipvie(i)<(-kfps-150.5) Then soundboom:rimpact*=3:airshipvie(i)=-150
   If airshipvie(i)<airshipvie0(i)*0.4 And Rnd<0.4 Then airshipsmoke(i)=4000
   testairshipboom(i)	
	If testsoundshoot=0 Then soundshoot:testsoundshoot=1
   impactx=tirx+tircos1*tircos2*tirdist
   impacty=tiry+tirsin1*tircos2*tirdist
   impactz=tirz+tirsin2*tirdist
	addsmoke(impactx,impacty,impactz,1)
EndIf
If impact>3000 And impact<4000 Then 'boat
	i=impact-3000
	If testsoundshoot=0 Then soundshoot:testsoundshoot=1
   impactx=tirx+tircos1*tircos2*tirdist
   impacty=tiry+tirsin1*tircos2*tirdist
   impactz=tirz+tirsin2*tirdist
	addsfire(impactx,impacty,impactz,i,0.89)
   mcos1=boatco1(i)
   msin1=boatsi1(i)
   mcos2=Cos(boato2(i)*degtorad)
   msin2=Sin(boato2(i)*degtorad)
   mcos3=Cos(boato3(i)*degtorad)
   msin3=Sin(boato3(i)*degtorad)
	rotationMd22(impactx-boatx(i),impacty-boaty(i),impactz-boatz(i))
	If Rnd<0.35 Then '0.35 Then 
		addfire(avx3,avy3,avz3,i,350+Rnd*200)
	Else 
		addfire(avx3,avy3,avz3,i,15)
	EndIf
	boatvie(i)-=force*(1+Rnd)*kfps*0.3
	If boatvie(i)<(-kfps-99.5) Then soundboom:rimpact*=3:boatvie(i)=-99
   If itypetir=0 Then
   	boatwar(i)=1
   	target=i:typetarget=4:targetdist=0 'boat
   EndIf
EndIf
If impact>4000 And impact<5000 Then
	i=impact-4000
	var domevie_0=domevie(i)
	domevie(i)-=force*((1+Rnd)*3.3/(1.0+klevel))*kfps
   If domevie(i)<0 And domevie_0>=0 Then
   	soundboom
   	addboom(domex(i),domey(i),domez(i))
   	addsmoke(domex(i),domey(i),domez(i)+50,2)
   EndIf
	addsmoke(impactx,impacty,impactz)
EndIf 
If impact>5000 And impact<6000 Then
	i=impact-5000
	var airportvie_0=airportvie(i,itower)
	airportvie(i,itower)-=force*((1+Rnd)*3.3/(1.0+klevel))*kfps
   If airportvie(i,itower)<0 And airportvie_0>=0 Then
   	soundboom
   	addboom(airportx(i)-dmx0,airporty(i)-dmy0-(itower-1)*470,airportz(i))
   	addsmoke(airportx(i)-dmx0,airporty(i)-dmy0-(itower-1)*470,airportz(i)+50,2)
   EndIf
	addsmoke(impactx,impacty,impactz)
EndIf 
If impact>6000 And impact<7000 Then
	i=impact-6000
   impactx=tirx+tircos1*tircos2*tirdist
   impacty=tiry+tirsin1*tircos2*tirdist
   impactz=tirz+tirsin2*tirdist
	var townvie_0=townvie(i,ii1,jj1)
	townvie(i,ii1,jj1)-=force*((1+Rnd)*3.3/(1.0+klevel))*kfps
   If townvie(i,ii1,jj1)<0 And townvie_0>=0 Then
   	soundboom
   	addboom(towndx(i,ii1,jj1)-dmx0,towndy(i,ii1,jj1)-dmy0,impactz)
   	addsmoke(towndx(i,ii1,jj1)-dmx0,towndy(i,ii1,jj1)-dmy0,impactz+50,2)
   EndIf
	If Rnd<0.1 Then 
		addsmoke(impactx,impacty,impactz,3)
	Else
		addsmoke(impactx,impacty,impactz,1)
	EndIf
	townchanged=1
EndIf 
If (itypetir=1 Or itypetir>2000) And ftir(itesttir)<1 Then rimpact=30:impact=1
If impact<>0 And itypetir<>0 Then soundshoot2
End Sub
Dim Shared As Integer iimpact,nimpact=5
Dim Shared As Single impactmx(nimpact),impactmy(nimpact),impactmz(nimpact),impacttime(nimpact)
Dim Shared As Single impactmr(nimpact),tshoot
Sub drawtir 
glpushmatrix
impactx=tirx+tircos1*tircos2*tirdist*0.9
impacty=tiry+tirsin1*tircos2*tirdist*0.9
impactz=tirz+tirsin2*tirdist*0.9
gldisable gl_lighting
gldisable gl_texture_2d
glcolor3f(1,0.3,0)
gllinewidth(4.5)
glligne(tirx,tiry,tirz,impactx,impacty,impactz)
If impact>=1 Then
	iimpact+=1
	If iimpact>nimpact Then iimpact=0
	impacttime(iimpact)=time2+rimpact*0.001
	impactmx(iimpact)=impactx
	impactmy(iimpact)=impacty
	impactmz(iimpact)=impactz
	impactmr(iimpact)=rimpact*rforce
EndIf 
glcolor3f(1,1,1)
glpopmatrix
End Sub
Dim Shared As Integer ttir
Sub tir
If time2<tshoot Then tshoot=time2 'if midnight
If time2>(tshoot+0.1) And ttir=0 Then
ttir=1
soundexplosion1:tshoot=time2
disttir=30000'14000'8000
x30=0:y30=-40:z30=-1.7'5.5
x3=disttir:y3=-43:z3=z30-disttir/23'20'33.4 '/30
if viseur=2 then z3=z30-20
rotaviontir(x30,y30,z30)	
tirx=mx+avx1:tiry=my+avy1:tirz=mz+avz1
rotaviontir(x3,y3,z3)	
dtirx=mx+avx1-tirx:dtiry=my+avy1-tiry:dtirz=mz+avz1-tirz
addtir
y30=-y30:y3=-y3
rotaviontir(x30,y30,z30)	
tirx=mx+avx1:tiry=my+avy1:tirz=mz+avz1
rotaviontir(x3,y3,z3)	
dtirx=mx+avx1-tirx:dtiry=my+avy1-tiry:dtirz=mz+avz1-tirz
addtir
Else 
 ttir=0	
EndIf 
End Sub
Sub drawviseur
glcolor3f(0,1,1)
	'If viseur=1 Then gldrawtext("- -",xmax/2-12.5,ymax/2+30,2.5)
	'If viseur=2 Then gldrawtext("- -",xmax/2-12.5,ymax/2+7,2.5)
	'If viseur=1 Then gldrawtext("_   _",xmax/2-17,ymax/2+16.2,1.5)
If volant<>6 Or tourelle=1 Then 
	If viseur=1 Then gldrawtext("_   _",xmax/2-17,ymax/2+24.3,1.5)
	If viseur=2 Then gldrawtext("_   _",xmax/2-17,ymax/2-7.2,1.5)
Else
	If viseur=1 Then gldrawtext("+",xmax/2-4,(0.9-0.4*sin2)*ymax/2,1)
	If viseur=2 Then gldrawtext("+",xmax/2-4,(0.8-0.4*sin2)*ymax/2,1)
EndIf 
End Sub
Dim Shared smoketext As uint
Dim Shared As Integer ncloud=200
Dim Shared As Single cloudx(ncloud),cloudy(ncloud),cloudz(ncloud),cloudr(ncloud)
Dim Shared As Single distcloud=80000
Sub initcloud
Dim As Integer i
ncloud2=min2(ncloud,ncloud2)
For i=1 To ncloud
	cloudx(i)=(Rnd-0.5)*2*distcloud+mx
	cloudy(i)=(Rnd-0.5)*2*distcloud+my
	cloudz(i)=max(getterrainheight(cloudx(i),cloudy(i))+1000+1200*Rnd,waterz+3000+1200*Rnd)
	cloudr(i)=1500+Rnd*1000
Next
End Sub
Sub drawcloud(ByVal i As Integer)
	      glpushmatrix
	      gltranslatef(cloudx(i),cloudy(i),cloudz(i))	      
	      glbindtexture(gl_texture_2d,smoketext)
	      glcolor3f(1,1,1)
	      If tourelle=0 Then 
	        glrotatef(o1,0,0,1)
	        glrotatef(o2,0,1,0)
	      Else
	        glrotatef(to1+o1,0,0,1)
	        glrotatef(to2,0,1,0)
	      EndIf
	      gltexcarre3(cloudr(i)*1.5,cloudr(i))
	      glpopmatrix
End Sub 
Sub drawclouds()
Dim As Integer test
If ncloud2>0 Then 
'glenable gl_alpha_test
'glAlphaFunc(gl_greater,10/254)
glEnable GL_BLEND
glBlendFunc GL_SRC_color,GL_ONE'_MINUS_SRC_ALPHA
gldisable GL_DEPTH_TEST
glenable(gl_texture_2d)
For i=1 To ncloud2
	test=0
	If cloudx(i)<mx-distcloud Then cloudx(i)+=2*distcloud:test=1
	If cloudx(i)>mx+distcloud Then cloudx(i)-=2*distcloud:test=1
	If cloudy(i)<my-distcloud Then cloudy(i)+=2*distcloud:test=1
	If cloudy(i)>my+distcloud Then cloudy(i)-=2*distcloud:test=1
	If test=1 Then
		cloudz(i)=max(getterrainheight(cloudx(i),cloudy(i))+1000+1200*Rnd,waterz+3000+1200*Rnd)
	EndIf
	drawcloud(i)
Next
gldisable GL_BLEND
glEnable GL_DEPTH_TEST	
'gldisable gl_alpha_test
EndIf 
End Sub
Dim Shared As Integer ismoke=0,nsmoke=50'20
Dim Shared As Integer smoketime(nsmoke),typesmoke(nsmoke)
Dim Shared As Single tsmoke=0,smokex(nsmoke),smokey(nsmoke),smokez(nsmoke)
Sub addsmoke(ByVal mx As Single,ByVal my As Single,ByVal mz As Single,ByVal itype As Integer=0)
Dim i As Integer
For i=1 To nsmoke
	ismoke+=1
	If ismoke>nsmoke Then ismoke=0
	If typesmoke(ismoke)<>2 Then
		Exit For  
	Else 
	   If time2>(smoketime(ismoke)+17) Then Exit For
   EndIf 
Next i
	smokex(ismoke)=mx
	smokey(ismoke)=my
	smokez(ismoke)=mz
   smoketime(ismoke)=time2
   typesmoke(ismoke)=itype	
End Sub
Dim Shared As Integer iboom=0,nboom=4'20
Dim Shared As Integer boomtime(nboom),typeboom(nboom)
Dim Shared As Single boomx(nboom),boomy(nboom),boomz(nboom)
Sub addboom(ByVal mx As Single,ByVal my As Single,ByVal mz As Single,ByVal itype As Integer=0)
	iboom+=1
	If iboom>nboom Then iboom=0
	boomx(iboom)=mx
	boomy(iboom)=my
	boomz(iboom)=mz
   boomtime(iboom)=time2
   typeboom(iboom)=itype
   addsmoke(mx,my,mz,2)	
End Sub
Sub testtirs
Dim As Integer i,ishiptir
Dim As Single r,dt
testsoundshoot=0
	For itesttir=0 To ntir
		If ftir(itesttir)>0 Then
			ftir(itesttir)-=1
			tirx=tirstartx(itesttir)
			tiry=tirstarty(itesttir)
			tirz=tirstartz(itesttir)
			dtirx=vtirx(itesttir)
			dtiry=vtiry(itesttir)
			dtirz=vtirz(itesttir)
			tirstartx(itesttir)+=dtirx
			tirstarty(itesttir)+=dtiry
			tirstartz(itesttir)+=dtirz
			aux=0.95
			vtirx(itesttir)=aux*vtirx(itesttir)
			vtiry(itesttir)=aux*vtiry(itesttir)
			vtirz(itesttir)=aux*vtirz(itesttir)-2.0'19.0
			testtir(typetir(itesttir))
			drawtir
			If impact<>0 Then
				ftir(itesttir)=0
				If typetir(itesttir)=1 Or typetir(itesttir)>2000 Then 'dca
					addsmoke(impactx,impacty,impactz,1)
					aux=10+Abs(impactx-mx)+Abs(impacty-my)+Abs(impactz-mz)
					'If aux<2200 Then vie-=(1+Rnd)*400*400/(aux*aux)
					If aux<2200 Then vie-=(Rnd)*400*400/(aux*aux)
					ishiptir=typetir(itesttir)-2000
					For i=1 To nship2
					  If i<>ishiptir Then 	
					   aux=10+Abs(impactx-airshipx(i))+Abs(impacty-airshipy(i))+Abs(impactz-airshipz(i))
					   'If aux<2200 Then airshipvie(i)-=(1+Rnd)*400*400/(aux*aux)						
					   If aux<2200 Then airshipvie(i)-=(Rnd)*400*400/(aux*aux)						
					  EndIf 
					Next
				EndIf
				If typetir(itesttir)>1000 Then
					addsmoke(impactx+dmx*90,impacty+dmy*90,impactz+dmz*90,1)
					aux=20+Abs(impactx-mx)+Abs(impacty-my)+Abs(impactz-mz)
					If aux<200 Then vie-=(1+Rnd)*200/(aux)
				EndIf
			EndIf
		EndIf
	Next
	glenable(gl_texture_2d)
   glEnable GL_BLEND
   glBlendFunc GL_SRC_color,GL_ONE'_MINUS_SRC_ALPHA
	gldisable GL_DEPTH_TEST
	For i=0 To nsmoke
		dt=4.07
		If typesmoke(i)=2 Then dt=17
		If typesmoke(i)=3 Then dt=35
		If time2<(smoketime(i)+dt) Then 			
		   If time2<(smoketime(i)-100) Then smoketime(i)=time2 'if midnight
	      glpushmatrix
	      gltranslatef(smokex(i),smokey(i),smokez(i))
	      glbindtexture(gl_texture_2d,smoketext)
	      glcolor3f(1,1,1)
	      If tourelle=0 Then 
	        glrotatef(o1,0,0,1)
	        glrotatef(o2,0,1,0)
	      Else
	        glrotatef(to1+o1,0,0,1)
	        glrotatef(to2,0,1,0)
	      EndIf   	
	      If typesmoke(i)=0 Then 
	      	r=50+30*(time2-smoketime(i))
	      Else 
	      	If typesmoke(i)=1 Then 
	      		r=150+90*(time2-smoketime(i))
	      	Else 
	      		r=200+200*(time2-smoketime(i))
	      	EndIf
	      EndIf
	      gltexcarre3(r,r)
	      glpopmatrix
		EndIf
	Next
	glEnable GL_DEPTH_TEST
	For i=0 To nboom
		If time2<(boomtime(i)+3.4) Then
		   If time2<(boomtime(i)-100) Then boomtime(i)=time2-100 'if midnight
			gldisable(gl_texture_2d)
         glBlendFunc GL_SRC_alpha,GL_ONE_MINUS_SRC_ALPHA
	      glpushmatrix
	      gltranslatef(boomx(i),boomy(i),boomz(i))
	      glcolor4f(1,0.8,0.8, 0.3)'0.25)
	      dt=max(0.001,time2-boomtime(i))
	      r=75*(0.5+3*sqr(dt/2))*2
         'scale=(0.5+3*sqr((time0-aux)/2000))*2+(boomscale(i)-1)
	      glsphere(r*0.5,12,12)
	      glsphere(r*0.7,12,12)
	      glsphere(r,12,12)
	      glpopmatrix
	      glcolor4f(1,1,1, 1)
			glenable(gl_texture_2d)
		EndIf   
   Next i
	gldisable GL_BLEND
	glEnable GL_DEPTH_TEST
	For i=0 To nimpact
		If time2<(impacttime(i)+0.03) Then 			
		   If time2<(impacttime(i)-100) Then impacttime(i)=time2 'if midnight
	      glpushmatrix
	      gltranslatef(impactmx(i),impactmy(i),impactmz(i))
	      glbindtexture(gl_texture_2d,feutext)
	      'glcolor3f(0.85,0,0)
	      gltexsphere(impactmr(i),10,10)
	      glpopmatrix
		EndIf
	Next
tsmoke+=kfps*(0.5+Rnd)
If tsmoke>3 Then 
	tsmoke=0
EndIf
If avionvie<avionvie0 Then avionvie=max(0.0,avionvie+0.1*kfps)
If tsmoke=0 And avionsmoke>0.1 Then
	If avionvie>avionvie0*0.8 Then avionsmoke=0
	avionsmoke-=kfps
	addsmoke(avionx,aviony,avionz)
EndIf
End Sub
Sub drawMd2node(ByVal md2node As md2_node Ptr,ByVal glnormal As Integer=0)
Dim As Single dn,dtime,dx,dy,dz,dist
If md2node->md2model<>0 Then
	dx=md2node->x-mx:dy=md2node->y-my:dz=md2node->z-mz
	dist=dx*dx+dy*dy'+dz*dz
   If (dist)<(gldistmax*gldistmax) And (md2node->z)>waterz Then   	
     'rotavion2(md2node->x-mx,md2node->y-my)
     rotavion(dx,dy,dz)
     If x2>(max(Abs(z2),Abs(y2))-300) Then 	
        With *md2node
        If glnormal=0 Then gldisable(gl_lighting)
        glpushmatrix
        glTranslatef (.x,.y,.z)
        glRotatef (.rotz, 0.0, 0.0, 1.0)
        glRotatef (.roty, 0.0, 1.0, 0.0)
        If (Abs(.rotx)>1) Then glRotatef (.rotx, 1.0, 0.0, 0.0)
        glscalef  (.scalex,.scaley,.scalez)

        /' draw model '/
        'glsphere 10
        dtime=80*10/(0.01+.fps)
        dn=(time1-time0)/dtime
        .nframe+=dn+Rnd*0.001
        If .nframe>=.endframe Or .nframe<.startframe Then .nframe=.startframe

        'drawMd2 ((md2node))
        'drawMd2_glcmds((md2node))
        'drawMd2_inter ((md2node))
        'drawMd2_inter_glcmds(md2node)

        If (md2node->md2model->header.num_glcmds)=0 Then
          If md2node->md2model->header.num_frames>1 And dist<(3000*3000) Then 
        	   drawMd2_inter(md2node ,glnormal)
          Else
         	drawMd2(md2node ,glnormal)
          EndIf 
        Else 	
          If md2node->md2model->header.num_frames>1 And dist<(3000*3000) Then 
        	   drawMd2_inter_glcmds(md2node ,glnormal)
        	 Else 
        	   drawMd2_glcmds(md2node ,glnormal)
          EndIf 
        EndIf	
        
        End With  
        With md2node->joints 
        If .njoint>0 Then
        	For i=1 To .njoint
           glpushmatrix
           glTranslatef (.vxjoint(i),.vyjoint(i),.vzjoint(i))
           glulookat(0,0,0, .vxjoint2(i)-.vxjoint(i),.vyjoint2(i)-.vyjoint(i),.vzjoint2(i)-.vzjoint(i),_
                  .vxjoint3(i)-.vxjoint(i),.vyjoint3(i)-.vyjoint(i),.vzjoint3(i)-.vzjoint(i))
           If .subjoint(i)<>0 Then .subjoint(i)()
           glpopmatrix
        	Next i
        EndIf 
        End With
        glpopmatrix	
        glenable(gl_lighting)
     EndIf 
   EndIf   
EndIf 
End Sub
Dim Shared As uint ngrass=500,distgrass=500
Dim Shared As Single grassx(ngrass),grassy(ngrass),grassz(ngrass),grassrot(ngrass),grassrot0(ngrass)
Dim Shared As Single grassscalex(ngrass),grassscalez(ngrass),grasstype(ngrass),grasstx(ngrass)
Dim Shared As Single grasswind,grasswindo1,grassscalez2(ngrass)
Sub drawgrass
Dim As Integer changegrass,ix,iy
Dim As Single x=30,y=20,tx=1,x0,x1,dtx=0.8,grasso1,aux
x1=x*0.5:x0=0-x1
    If Rnd<0.15*kfps Then
    	grasswind+=(Rnd-0.5)*2.5
    	grasswind=max(-10.0,min(10.0,grasswind))
    	grasswindo1+=(Rnd-0.5)*30
    	While grasswindo1<-180:grasswindo1+=360:Wend
    	While grasswindo1>180:grasswindo1-=360:Wend
    Else
    	grasswind=(1-min(0.8,0.15*kfps))*grasswind+1e-10
    EndIf
    For i=1 To ngrass
     changegrass=0	
     While grassx(i)<mx-distgrass :grassx(i)+=distgrass*2:changegrass=1:Wend 
     While grassx(i)>mx+distgrass :grassx(i)-=distgrass*2:changegrass=1:Wend 	
     While grassy(i)<my-distgrass :grassy(i)+=distgrass*2:changegrass=1:Wend 
     While grassy(i)>my+distgrass :grassy(i)-=distgrass*2:changegrass=1:Wend 
     If changegrass=1 Then
     	 If testairport(grassx(i),grassy(i))=0 Then
     	 	grassz(i)=getterrainheight(grassx(i),grassy(i))	
     	 Else
     	 	grassz(i)=waterz-1
     	 EndIf
       ix=Int(grassx(i)/scalex+10000)-10000
       iy=Int(grassy(i)/scalex+10000)-10000
       ix=max2(0,min2(512,ix))
       iy=max2(0,min2(512,iy))
       aux=1-terraincolor(ix,iy).x
       grassscalez2(i)=1.6*aux*grassscalez(i)
     EndIf
  'rotavion2(grassx(i)-mx,grassy(i)-my)
  rotavion(grassx(i)-mx,grassy(i)-my,grassz(i)-mz)
  If x2>(max(Abs(y2),Abs(z2))-80) And grassz(i)>waterz Then 	
     If igrass=2 Then 
     	 glbindtexture(gl_texture_2d,grasstext(1))	
     Else 	 
     	 glbindtexture(gl_texture_2d,grasstext(grasstype(i)))	
     EndIf
     glpushmatrix
     gltranslatef(grassx(i),grassy(i),grassz(i))
     glrotatef(grassrot(i),0,0,1)
     grasso1=grassrot(i)-grasswindo1+(Abs(grassx(i)-mx)+Abs(grassy(i)-my))*0.18
     While grasso1<-180:grasso1+=360:Wend
     While grasso1>180:grasso1-=360:Wend
     If grasso1>0 Then 
       glrotatef(grasswind,0,1,0)
     Else 
       glrotatef(-grasswind,0,1,0)
     EndIf
     glscalef(grassscalex(i),grassscalex(i),grassscalez2(i)) 
	  tx=grasstx(i)
	glbegin(gl_quads)
	glTexCoord2f(tx,0)
	glvertex3f(0,x0,0)
	gltexcoord2f(tx+dtx,0)
	glvertex3f(0,x1,0)
	glTexCoord2f(tx+dtx,1)
	glvertex3f(0,x1,y)
	gltexcoord2f(tx,1)
	glvertex3f(0,x0,y)
	glend()
     glpopmatrix
   EndIf 
    Next i 
End Sub
Sub drawhelice0
     glbindtexture(gl_texture_2d,helicetext)	
     glEnable GL_BLEND
     'glBlendFunc GL_SRC_color,GL_ONE_MINUS_SRC_color
     glBlendFunc GL_SRC_alpha,GL_ONE_MINUS_SRC_alpha
     helicerot0=helicerot0+(9+v)*1.35*kfps
     If helicerot0>10000 Then helicerot0-=10000
     glrotatef(helicerot0,1,0,0) 
     gltexcarre3(50,50)
     gldisable gl_blend
End Sub 
Sub drawhelice(ByVal i As Integer)
     glbindtexture(gl_texture_2d,helicetext)	
     glEnable GL_BLEND
     'glBlendFunc GL_SRC_color,GL_ONE_MINUS_SRC_color
     glBlendFunc GL_SRC_alpha,GL_ONE_MINUS_SRC_alpha
     airshiphelicerot(i)+=airshipv(i)*1.5
     If airshiphelicerot(i)>10000 Then airshiphelicerot(i)-=10000
     glrotatef(airshiphelicerot(i),1,0,0) 
     gltexcarre3(50,50)
     gldisable gl_blend
End Sub 
Sub drawhelice2(ByVal i As Integer)
     glbindtexture(gl_texture_2d,helicetext)	
     glEnable GL_BLEND
     'glBlendFunc GL_SRC_color,GL_ONE_MINUS_SRC_color
     glBlendFunc GL_SRC_alpha,GL_ONE_MINUS_SRC_alpha
     airshiphelicerot(i)+=airshipv(i)*1.5
     If airshiphelicerot(i)>10000 Then airshiphelicerot(i)-=10000
     glpushmatrix
     glrotatef(airshiphelicerot(i),1,0,0) 
     gltexcarre3(70,70)
     glpopmatrix
     gldisable gl_blend
End Sub 
Sub drawavion
	If avionlist<>0 Then
		glcalllist(avionlist)
	Else
		avionlist=glgenlists(1)
		glnewlist avionlist,gl_compile
		loadobjsize("objects/c150.obj",@"",@"",94)
      glendlist
	EndIf	
End Sub
Sub drawcorsair
gldisable(gl_lighting)
glbindtexture(gl_texture_2d,corsairtext)
	If corsairlist<>0 Then
		glcalllist(corsairlist)
	Else
		corsairlist=glgenlists(1)
		glnewlist corsairlist,gl_compile
		loadobjsize("objects/f4u.obj",@"",@"",100)
      glendlist
	EndIf	
End Sub
Sub drawvg33
gldisable(gl_lighting)
glbindtexture(gl_texture_2d,vg33text)
	If vg33list<>0 Then
		glcalllist(vg33list)
	Else
		vg33list=glgenlists(1)
		glnewlist vg33list,gl_compile
		loadobjsize("objects/vg33lowpoly.obj",@"",@"",100)
      glendlist
	EndIf	
End Sub
Sub drawalphajet
gldisable(gl_lighting)
glbindtexture(gl_texture_2d,alphajettext)
	If alphajetlist<>0 Then
		glcalllist(alphajetlist)
	Else
		alphajetlist=glgenlists(1)
		glnewlist alphajetlist,gl_compile
		loadobjsize("objects/alphajet.obj",@"",@"",102)
      glendlist
	EndIf	
End Sub
Sub drawspitfire
glenable(gl_lighting)
glenable(gl_light1)
If icar=7 Then 
	glbindtexture(gl_texture_2d,spitfiretext)
Else 
	glbindtexture(gl_texture_2d,spitfiretext2)
EndIf
	If spitfirelist<>0 Then
		glcalllist(spitfirelist)
	Else
		spitfirelist=glgenlists(1)
		glnewlist spitfirelist,gl_compile
		loadobjsize("objects/spitfire_lowpoly2.obj",@"",@"",100)
      glendlist
	EndIf	
gldisable(gl_light1)
End Sub
Sub drawbf109
glenable(gl_lighting)
glenable(gl_light1)
glbindtexture(gl_texture_2d,bf109text)
	If bf109list<>0 Then
		glcalllist(bf109list)
	Else
		bf109list=glgenlists(1)
		glnewlist bf109list,gl_compile
		loadobjsize("objects/bf109.obj",@"",@"",100)
      glendlist
	EndIf	
gldisable(gl_light1)
End Sub
Sub drawp51d
glenable(gl_lighting)
glenable(gl_light1)
glbindtexture(gl_texture_2d,p51dtext)
	If p51dlist<>0 Then
		glcalllist(p51dlist)
	Else
		p51dlist=glgenlists(1)
		glnewlist p51dlist,gl_compile
		loadobjsize("objects/p51d.obj",@"",@"",100)
      glendlist
	EndIf	
gldisable(gl_light1)
End Sub
Sub drawf14
glenable(gl_lighting)
glenable(gl_light1)
glbindtexture(gl_texture_2d,f14text)
	If f14list<>0 Then
		glcalllist(f14list)
	Else
		f14list=glgenlists(1)
		glnewlist f14list,gl_compile
		loadobjsize("objects/f14_2.obj",@"",@"",107)
      glendlist
	EndIf	
gldisable(gl_light1)
End Sub
Sub drawzero
glenable(gl_lighting)
glenable(gl_light3)
if icar=12 Then 
	glbindtexture(gl_texture_2d,zero2text)
Else 
	glbindtexture(gl_texture_2d,zerotext)
EndIf
	If zerolist<>0 Then
		glcalllist(zerolist)
	Else
		zerolist=glgenlists(1)
		glnewlist zerolist,gl_compile
		loadobjsize("objects/zero1.obj",@"",@"",100)
      glendlist
	EndIf	
gldisable(gl_light3)	
End Sub
Sub drawzero2
glenable(gl_lighting)
glbindtexture(gl_texture_2d,zero2text)
	If zerolist<>0 Then
		glcalllist(zerolist)
	Else
		zerolist=glgenlists(1)
		glnewlist zerolist,gl_compile
		loadobjsize("objects/zero1.obj",@"",@"",100)
      glendlist
	EndIf	
End Sub
Sub drawb25
glenable(gl_lighting)
glbindtexture(gl_texture_2d,b25text)
	If b25list<>0 Then
		glcalllist(b25list)
	Else
		b25list=glgenlists(1)
		glnewlist b25list,gl_compile
		loadobjsize("objects/b25_lowpoly.obj",@"",@"",250)
      glendlist
	EndIf	
End Sub
Sub drawcockpit3
   glpushmatrix
   gltranslatef(mx,my,mz)
   glrotatef(o1,0,0,1)
   glrotatef(-o2,0,1,0)
   glrotatef(-o3,1,0,0)
   If typeavion=2 Then drawcorsair:gltranslatef(47,0,0)
  	If typeavion=1 Then drawvg33:gltranslatef(50,0,0)
   If typeavion=7 Then drawalphajet
   If typeavion=3 Then drawspitfire:gltranslatef(50,0,0)
   If typeavion=4 Then drawbf109:gltranslatef(50,0,0)
   If typeavion=5 Then drawp51d:gltranslatef(62,0,0)
   If typeavion=8 Then drawf14
   If typeavion=6 Then drawzero:gltranslatef(41,0,0)
   If typeavion<7 Then 
   	drawhelice0
   EndIf
   glpopmatrix
If tsmoke=0 And vie<0.4*vie0 Then
	addsmoke(mx,my,mz)
EndIf
End Sub
Sub initairship
Dim As Integer i
If order<4 Then torder=Timer+99999 Else torder=Timer+15
For i=1 To nship
	'airshipx(i)=mx-i*1200+4000:airshipy(i)=my+i*1000-4000:airshipz(i)=mz+200.0 
Next
For i=1 To nship
	Select Case i
		Case 1,2
			typeship(i)=0
			airshipmove(i)=1
			airshipvie0(i)=300
		Case 3
			typeship(i)=4
			airshipmove(i)=0.59
			airshipvie0(i)=400'800
		Case 4,5,6
			typeship(i)=1
			airshipmove(i)=1
			airshipvie0(i)=300
		Case 7,8,9,10,15,16
			typeship(i)=2
			airshipmove(i)=1
			airshipvie0(i)=300
		Case 11,12,13,14,17,18
			typeship(i)=3
			airshipmove(i)=1
			airshipvie0(i)=300
		Case Else 
			typeship(i)=2
			airshipmove(i)=1
			airshipvie0(i)=300
	End Select
Next
For i=1 To nship
	airshipv(i)=max(vcruise,v):setairship(i)=1:dturnairship(i)=0
   airshipvie(i)=airshipvie0(i)
Next
End Sub
Sub drawairship
Dim As Single distmax2
distmax2=distmax+30000
'gldisable(gl_lighting)
glenable(gl_normalize)
glenable(gl_texture_2d)
For i=1 To nship2
  'If airshipx(i)<-distmax Then airshipx(i)=-distmax:airshipo1(i)+=90:setairship(i)=1
  'If airshipx(i)>distmax Then airshipx(i)=distmax:airshipo1(i)+=90:setairship(i)=1
  'If airshipy(i)<-distmax Then airshipy(i)=-distmax:airshipo1(i)+=90:setairship(i)=1
  'If airshipy(i)>distmax Then airshipy(i)=distmax:airshipo1(i)+=90:setairship(i)=1
  While airshipx(i)<-distmax2:airshipx(i)+=2*distmax2:Wend
  While airshipx(i)>distmax2:airshipx(i)-=2*distmax2:Wend 
  While airshipy(i)<-distmax2:airshipy(i)+=2*distmax2:Wend 
  While airshipy(i)>distmax2:airshipy(i)-=2*distmax2:Wend 
  If pause=0 Then moveairship(i)
  rotavion(airshipx(i)-mx,airshipy(i)-my,airshipz(i)-mz)
  If x2>(Abs(y2)-250) And x2<30000 Then
     If airshipvie(i)>0 Then 
     	glcolor3f(1,1,1)	
     Else
      glcolor3f(1,0.5,0.5)	
     EndIf 
     glpushmatrix
     gltranslatef(airshipx(i),airshipy(i),airshipz(i))
     glrotatef(airshipo1(i),0,0,1)
     glrotatef(-airshipo2(i),0,1,0)
     glrotatef(-airshipo3(i),1,0,0)'+90,1,0,0)
     If typeship(i)=0 Then drawvg33:gltranslatef(50,0,0):drawhelice(i)
     If typeship(i)=1 Then drawcorsair:gltranslatef(47,0,0):drawhelice(i)
     If typeship(i)=2 Then
     	  If typeavion=6 Then 
     	  	  drawvg33:gltranslatef(50,0,0):drawhelice(i)
     	  Else 
     	  	  drawzero:gltranslatef(41,0,0):drawhelice(i)
     	  EndIf
     EndIf
     If typeship(i)=3 Then
     	  If typeavion=6 Then 
     	  	  drawcorsair:gltranslatef(47,0,0):drawhelice(i)
     	  Else 
     	  	  drawzero2:gltranslatef(41,0,0):drawhelice(i)
     	  EndIf
     EndIf 	  
     If typeship(i)=4 Then
     	  drawb25:gltranslatef(100,71,0):drawhelice2(i)
     	  gltranslatef(0,-142,0):glrotatef(90,1,0,0):drawhelice2(i)
     EndIf
     If typeship(i)=5 Then drawalphajet
     If typeship(i)=6 Then drawspitfire:gltranslatef(50,0,0):drawhelice(i)
     If typeship(i)=7 Then drawbf109:gltranslatef(50,0,0):drawhelice(i)
     If typeship(i)=8 Then drawp51d:gltranslatef(62,0,0):drawhelice(i)
     If typeship(i)=9 Then drawf14
     If typeship(i)=10 Then drawzero:gltranslatef(41,0,0):drawhelice(i)
     glpopmatrix
  EndIf 
  If airshipvie(i)<airshipvie0(i) And airshipvie(i)>0 Then airshipvie(i)+=0.003*kfps
  If tsmoke=0 And airshipsmoke(i)>0.1 Then
	  If airshipvie(i)>airshipvie0(i)*0.8 Then airshipsmoke(i)=0
	  airshipsmoke(i)-=kfps
	  addsmoke(airshipx(i),airshipy(i),airshipz(i))
  EndIf
Next i
glcolor3f(1,1,1)
End Sub
Sub drawvie
 	'gldisable(gl_lighting)
   gldisable gl_texture_2d
   gldisable gl_depth_test
  	glpushmatrix
 	glplacecursor(10,240)
 	glcolor3f(0,1,1)
   gllinewidth(10)
 	glligne(0,0,-0.02,5,0,-0.02)
 	If vie>0.2*vie0 Then
 		glcolor3f(0,0.89,0)
 	Else 
 		glcolor3f(0.89,0,0)
 	EndIf
 	glligne(0,0,0, 5.0*max(0.0,vie)/vie0,0,0)
 	glpopmatrix
 	'glenable(gl_lighting)
   glenable gl_texture_2d
   glenable gl_depth_test	
End Sub
Sub drawmort
Dim As Integer i,j,iboat
 gldisable GL_DEPTH_TEST
 gldisable gl_lighting 
 gldisable gl_texture_2d
 glEnable GL_BLEND
 glBlendFunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA
 glcolor4f(1,0.7,0,0.35)
 glpushmatrix
 glplacecursor(xmax/2,ymax/2)
 glcarre(50*xmax/1000)
 glpopmatrix
 glcolor4f(1,1,1,1)
 gldisable GL_BLEND
 glenable GL_DEPTH_TEST
 glenable gl_texture_2d
 If vie<0.1 Then
 	soundaah
 	soundboom
 	guirefreshopenGL()
 	Sleep  5000
 	vie=vie0
 	target=0
 	war=0
 	For iboat=1 To nboat2
 		boatvie(iboat)=boatvie0(iboat)
 		boatwar(iboat)=0
 		boattarget(iboat)=0
	   For j=0 To 3
		   tdegats(iboat,j)=0
	   Next
      vdegats(iboat)=0
	   For j=1 To nfire
		   timefire(j,iboat)=0
	   Next
 	Next
 	For i=1 To nship
 		airshipwar(i)=0
 		airshipvie(i)=airshipvie0(i)
 	Next
 	confirm "you are dead !","confirm",resp
   Sleep 500
 EndIf
End Sub
'Dim Shared As Single vm20,vm2,do222,o222,ko222=1,cos222,sin222
Sub display ()
Dim As Single x,y,z,aux
Dim As Integer changearbre,changebuisson,changehouse,changehouse2,changedome
    If pause=0 Then moveavion
    'avionx=0:aviony=0:avionz=10:aviono1=30:aviono2=-20:aviono3=40

If car>=1 And plane>0 And suspension>0.01 Then 'suspensions 
 vm20=vm2
 vm2=vmx2*cos1+vmy2*sin1
 'o2+=(vm2-vm20)*8*suspension/(0.02+kfps/2)'*5*
 o2+=(vm2-vm20)*5*suspension/(0.02+kfps/2)'*5*
 do222+=(o2-o222)*0.04*kfps-0.062*do222*kfps*ko222 
 o222+=do222*0.3*kfps
Else
 vm20=vm2
 vm2=vmx2*cos1+vmy2*sin1
 o222=o2
EndIf 
cos222=Cos(o222*degtorad):sin222=Sin(o222*degtorad)
If v>4 Then suspension=max(0.0,suspension-0.08*kfps)

    glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
    glLoadIdentity ()
    If tourelle=0 Or plane=0 Then
    	If plane>0 Then
    	  If volant<>6 Then
    		If car>=1 Then
    			glulookat(mx,my,mz+yh, mx+1000*cos1*cos222,my+1000*sin1*cos222,_   
    			          mz+1000*sin222+yh, -sin3*sin1*0.85,sin3*cos1*0.85,cos3)
    		Else 
    	  	   glulookat(mx,my,mz+yh, mx+1000*dmx,my+1000*dmy,mz+1000*dmz+yh, -sin3*sin1*0.85,sin3*cos1*0.85,cos3)
    		EndIf
    	  Else 
    	  	glulookat(mx-200*dmx,my-200*dmy,mz+yh+20-165*dmz, mx,my,mz+yh, -sin3*sin1*0.85,sin3*cos1*0.85,cos3)
    	  	'x=mx-200*dmx:y=my-200*dmy:z=mz+yh+20-165*dmz 'cf bug freebasic
    	  	'glulookat(x,y,z, mx,my,mz+yh,  -sin3*sin1*0.85,sin3*cos1*0.85,cos3)
    	  EndIf
    	Else 
    		glulookat(mx,my,mz+yh, mx+1000*dmx,my+1000*dmy,mz+1000*dmz+yh, 0,0,1)
    	EndIf
    Else 
    	to2=max2(-89,min2(89,to2))
    	While to1<-180:to1+=360:Wend
    	While to1>180:to1-=360:wend
    	tcos1=Cos((to1+o1)*degtorad):tsin1=Sin((to1+o1)*degtorad)
    	aux=to2+o2*Cos(to1*degtorad)
    	tcos2=Cos(aux*degtorad):tsin2=Sin(aux*degtorad)
    	tcos3=1.0:tsin3=0.0
      tdx=tcos1*tcos2:tdy=tsin1*tcos2:tdz=tsin2   	
      glulookat(mx,my,mz+yh,mx+tdx*1000,my+tdy*1000,mz+tdz*1000+yh, -tsin3*tsin1,tsin3*tcos1,tcos3)
    EndIf
    gldisable(GL_FOG)
    If guitestkey(vk_f12)<>0 Then '$$ screenshot cockpit
    	glcolor3f(255,255,255)
    	drawcockpit2()
    	guirefreshopengl
    	While guitestkey(vk_space)=0
    		guiscan:sleep 1000
    	Wend
    EndIf
    drawciel
    drawsun
    drawclouds
    If ifog>0 Then glEnable(GL_FOG)
    drawsol
    If ktir=1 Then ktir=0:tir() Else ttir=0
    gldisable(GL_FOG)
    testtirs
    If ifog>0 Then glEnable(GL_FOG)
    glcolor3f(1,1,1)
    glenable(gl_texture_2d)
            
    time1=Timer*1000
    If time1<time0 Or pause=1 Then time0=time1 'midnight
    glcolor3f(1,1,1)
    If md2nnode>0 Then 
      For md2inode=1 To md2nnode
         drawMd2node(md2_node(md2inode))
      Next md2inode
    EndIf 
    time0=time1
    If pause=0 Then movehorses
    
    If imap>=5 Then
      If pause=0 Then moveboat
  	   drawboat
    Else 
    	mz11=-999999
    EndIf
        
    gldisable(gl_normalize)
    gldisable(gl_lighting) 
    glenable gl_alpha_test
    glAlphaFunc(gl_less,1/254)
    ntree=0
    For i=1 To narbre2
     changearbre=0	
     While arbrex(i)<mx-distarbre :arbrex(i)+=distarbre*2:changearbre=1:Wend 
     While arbrex(i)>mx+distarbre :arbrex(i)-=distarbre*2:changearbre=1:Wend 	
     While arbrey(i)<my-distarbre :arbrey(i)+=distarbre*2:changearbre=1:Wend 
     While arbrey(i)>my+distarbre :arbrey(i)-=distarbre*2:changearbre=1:Wend 
     If changearbre=1 Then
     	  If testairport(arbrex(i),arbrey(i))=0 Then
     	  	  arbrez(i)=getterrainheight(arbrex(i),arbrey(i))
     	  Else
     	  	  arbrez(i)=waterz-1
     	  EndIf
     EndIf
  If arbrez(i)>waterz Then 
   'rotavion2(arbrex(i)-mx,arbrey(i)-my)
   rotavion(arbrex(i)-mx,arbrey(i)-my,arbrez(i)-mz)
   If x2>(Abs(y2)-150) Then 	
    ntree+=1
    If x2>(Abs(z2)-150) Then  
     glbindtexture(gl_texture_2d,arbretext(arbretype(i)))	
     glpushmatrix
     gltranslatef(arbrex(i),arbrey(i),arbrez(i))
     glrotatef(arbrerot(i),0,0,1)
     glscalef(arbrescalex(i),arbrescalex(i),arbrescalez(i)) 
     drawarbre
     glpopmatrix
    EndIf  
   EndIf  
  EndIf    
    Next i
    glscalef(1,1,1)
    For i=1 To nbuisson
     changebuisson=0	
     While buissonx(i)<mx-distbuisson :buissonx(i)+=distbuisson*2:changebuisson=1:Wend 
     While buissonx(i)>mx+distbuisson :buissonx(i)-=distbuisson*2:changebuisson=1:Wend 	
     While buissony(i)<my-distbuisson :buissony(i)+=distbuisson*2:changebuisson=1:Wend 
     While buissony(i)>my+distbuisson :buissony(i)-=distbuisson*2:changebuisson=1:Wend 
     If changebuisson=1 Then
        If testairport(buissonx(i),buissony(i))=0 Then
     	     buissonz(i)=getterrainheight(buissonx(i),buissony(i))
        Else
     	     buissonz(i)=waterz-1
        EndIf
     EndIf
  If buissonz(i)>waterz Then    
   'rotavion2(buissonx(i)-mx,buissony(i)-my)
   rotavion(buissonx(i)-mx,buissony(i)-my,buissonz(i)-mz)
   If x2>(Abs(y2)-80) Then 	
    If x2>(Abs(z2)-80) Then  
     glbindtexture(gl_texture_2d,buissontext(buissontype(i)))	
     glpushmatrix
     gltranslatef(buissonx(i),buissony(i),buissonz(i))
     glrotatef(buissonrot(i),0,0,1) 
     drawbuisson
     glpopmatrix
    EndIf
   EndIf  
  EndIf    
    Next i
    
    If igrass>=2 And mz<(mzsol0+290) Then drawgrass
    
    gldisable gl_alpha_test
    gldisable(gl_normalize)
    gldisable(gl_lighting) 
    For i=1 To nhouse
     changehouse=0	
     If housex(i)<mx-disthouse Then housex(i)+=disthouse*2:changehouse=1
     If housex(i)>mx+disthouse Then housex(i)-=disthouse*2:changehouse=1	
     If housey(i)<my-disthouse Then housey(i)+=disthouse*2:changehouse=1
     If housey(i)>my+disthouse Then housey(i)-=disthouse*2:changehouse=1
     If changehouse=1 Then
     	 x=60:y=60
     	 housez(i)=getterrainheight(housex(i)-x,housey(i)-y)		
       housez(i)=min(housez(i),getterrainheight(housex(i)+x,housey(i)-y))		
       housez(i)=min(housez(i),getterrainheight(housex(i)+x,housey(i)+y))		
       housez(i)=min(housez(i),getterrainheight(housex(i)-x,housey(i)+y))		
     EndIf
  'rotavion2(housex(i)-mx,housey(i)-my)
  rotavion(housex(i)-mx,housey(i)-my,housez(i)-mz)
  If x2>(Abs(y2)-500) And housez(i)>waterz Then 	
     glbindtexture(gl_texture_2d,building3text)	
     glpushmatrix
     gltranslatef(housex(i),housey(i),housez(i))
     'glrotatef(houserot(i),0,0,1)
     drawhouse1
     glpopmatrix
  EndIf 
    Next i 
    For i=1 To nhouse2
     changehouse2=0	
     If house2x(i)<mx-disthouse2 Then house2x(i)+=disthouse2*2:changehouse2=1
     If house2x(i)>mx+disthouse2 Then house2x(i)-=disthouse2*2:changehouse2=1	
     If house2y(i)<my-disthouse2 Then house2y(i)+=disthouse2*2:changehouse2=1
     If house2y(i)>my+disthouse2 Then house2y(i)-=disthouse2*2:changehouse2=1
     If changehouse2=1 Then
     	 x=50:y=110
     	 house2z(i)=getterrainheight(house2x(i)-x,house2y(i)-y)		
       house2z(i)=min(house2z(i),getterrainheight(house2x(i)+x,house2y(i)-y))		
       house2z(i)=min(house2z(i),getterrainheight(house2x(i)+x,house2y(i)+y))		
       house2z(i)=min(house2z(i),getterrainheight(house2x(i)-x,house2y(i)+y))		
     EndIf
  'rotavion2(house2x(i)-mx,house2y(i)-my)
  rotavion(house2x(i)-mx,house2y(i)-my,house2z(i)-mz)
  If x2>(Abs(y2)-500) And house2z(i)>waterz Then 	
     glbindtexture(gl_texture_2d,building3text)	
     glpushmatrix
     gltranslatef(house2x(i),house2y(i),house2z(i))
     'glrotatef(house2rot(i),0,0,1)
     drawhouse2
     glpopmatrix
  EndIf    
    Next i


    For i=1 To ndome
     changedome=0	
     If domex(i)<mx-distdome Then domex(i)+=distdome*2:changedome=1
     If domex(i)>mx+distdome Then domex(i)-=distdome*2:changedome=1	
     If domey(i)<my-distdome Then domey(i)+=distdome*2:changedome=1
     If domey(i)>my+distdome Then domey(i)-=distdome*2:changedome=1
     If changedome=1 Then
       If dometype(i)=0 Then
     	  domevie0(i)=400
     	  domez(i)=getterrainheight(domex(i),domey(i))
     	  If domez(i)>(waterz+0.01) Then domez(i)+=58.5 Else domez(i)=waterz-1
       Else
     	  domevie0(i)=200
     	  domez(i)=getterrainheight(domex(i),domey(i))
     	  If domez(i)>(waterz+0.01) Then domez(i)+=68.5 Else domez(i)=waterz-1
       EndIf
       domevie(i)=domevie0(i)
     EndIf
    Next i
    drawdomes()
    
    drawairports()
    
    drawtowns()
    
    gldisable gl_alpha_test
    x=-500:y=100:z=max(Abs(x-mx),Abs(y-my))
    If z<(dxterrain*scalex) Then 
     glEnable GL_BLEND
     glBlendFunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA
     glpushmatrix
     gldisable(gl_texture_2d)
     gltranslatef(x,y,getterrainheight(x,y)+60)
     glenable(gl_lighting)
     glcolor4f(0,1,1,0.92)
     i=max2(5,200000/(10000+z))
     glsphere(100,i,i)
     glcolor3f(1,1,1)
     glpopmatrix
     gldisable gl_blend
     gldisable gl_alpha_test
     gldisable(gl_lighting)
    EndIf
    

  'moveavion
  'avionx=0:aviony=0:avionz=10:aviono1=120:aviono2=-30':aviono3=40
  rotavion(avionx-mx,aviony-my,avionz-mz)
  If x2>(Abs(y2)-500) Then
  	  aux=Abs(x2)
     glenable(gl_texture_2d)
     glbindtexture(gl_texture_2d,aviontext)
     glcolor3f(1,1,1)	
     glpushmatrix
     gltranslatef(avionx,aviony,avionz)
     glrotatef(aviono1,0,0,1)
     glrotatef(-aviono2,0,1,0)
     glrotatef(-aviono3,1,0,0)'+90,1,0,0)
     drawavion
     'gltranslatef(-96,24,0)
     gltranslatef(-96,0,24)
     If heure>18 Or heure<8 Then 
      tlight+=kfps
      If tlight>15 Then'27 Then
      	If tlight>40 Then tlight=0
         gldisable(gl_texture_2d)
     	   glcolor3f(1,0.1,0)
     	   glsphere(max(1.2,min(80.0,aux/400)),4,4)
     	   glcolor3f(1,1,1)
         glenable(gl_texture_2d)
      EndIf 
     EndIf
     gltranslatef(96+45,0,-24)
     glbindtexture(gl_texture_2d,helicetext)	
     glEnable GL_BLEND
     'glBlendFunc GL_SRC_color,GL_ONE_MINUS_SRC_color
     glBlendFunc GL_SRC_alpha,GL_ONE_MINUS_SRC_alpha
     helicerot=helicerot+avionv*1.5
     If helicerot>10000 Then helicerot-=10000
     glrotatef(helicerot,1,0,0) 
     gltexcarre3(40,40)
     gldisable gl_blend
     glpopmatrix
  EndIf 
  
  drawairship()

    glenable gl_texture_2d
  	 If imap>=5 Then 
      gldisable(gl_lighting)
   	glcolor3f(1,1,1)
  	   For i=1 To nboat2
  	   	glpushmatrix
  	   	gltranslatef(boatx(i),boaty(i),boatz(i))
         glrotatef(boato1(i),0,0,1)
         If Abs(boato2(i))>1 Then glrotatef(-boato2(i),0,1,0)
         If Abs(boato3(i))>1 Then glrotatef(-boato3(i),1,0,0)
 	   	drawfires(i)
  	   	glpopmatrix
  	   	drawsfire(i)
  	   	aux=min(25.0,2*vdegats(i))
  	   	For j=0 To 3
  	   		aux+=min(5.0,tdegats(i,j))
  	   	Next
  	   	aux=aux/45.0
  	   	vboat(i)=vboat(i)-min(0.3,0.04*aux*kfps)*vboat(i)
  	   	aux=time2+99999
  	   	'If tdegats(i,1)>=5 And tdegats(i,2)>=5 And tdegats(i,3)>=5 Then 
  	   	If tdegats(i,1)>=4.9 And tdegats(i,2)>=4.9 And tdegats(i,3)>=4.9 Then 
  	   		'tshootleft(i)=0
  	   		'tshootright(i)=0
  	   		If tdegats(i,0)>=3.9 Then
  	   			vboat(i)=0
  	   			aux=time2
  	   		EndIf
  	   	EndIf
  	   	If boatvie(i)<boatvie0(i) And boatvie(i)>0 Then
  	   		boatvie(i)+=0.02*kfps*boatvie0(i)/200
  	   	EndIf
  	   	If aux>=time2+9999 And boatvie(i)>0 Then
  	   		tboatmort(i)=aux
  	   	Else 
  	   		tboatmort(i)=min(time2,tboatmort(i))
  	   	EndIf
  	   Next
  	   For i=1 To nboat2
  	   	If boatvie00(i)>=0 And boatvie(i)<0 Then
  	   	  boatvie(i)=-99
  	   	  boatvie00(i)=-99	
           soundboom
           addboom(boatx(i),boaty(i),boatz(i))
  	   	Else
  	   	  boatvie00(i)=boatvie(i)
  	   	EndIf
  	   	If time2>(tboatmort(i)+4) Then
  	   		tboatmort(i)=time2+99999
  	   	   testmort(i)=1
  	   	EndIf
  	   Next i
  	 EndIf   
    
    glenable gl_texture_2d
    If plane=1 And tourelle=0 Then
      If volant=1 Then drawcar  	
      If volant=3 Then drawcockpit  	
      If volant=4 Then drawcockpit2  	
      If volant=6 Then drawcockpit3  	
    	If volant>=1 And volant<=2 Then drawvolant
      gldisable gl_blend
      gldisable gl_alpha_test
      gldisable(gl_lighting)
    EndIf
    
    If imap>=5 Then
      gldisable GL_DEPTH_TEST
      'If mz<(waterz+altwater) Then drawwind
      If mz<(mzsol0+7000) Then
      	tdrawwind=1:drawwind
      Else
      	tdrawwind=0
      EndIf
    	drawmap
    EndIf
    
    vie=min(vie0,vie+0.002*kfps)
    drawvie
    If vie<0.001 Then drawmort

        glLoadIdentity ()
	     gldisable GL_DEPTH_TEST
        gldisable(gl_texture_2d)
        drawviseur
        glcolor3f(0.85,0,0)
        gldrawtext("fps "+Str(Int(fps)),15,12,1.2)
        'gldrawtext("nvertices "+Str(Int(md2_node(1)->md2model->header.num_vertices)),15,35,1.2)
        If (icar=2 Or icar=5) And tourelle=0 Then 
        	glcolor3f(0.3,0.4,1)
        Else 	
        	glcolor3f(0.4,1,0.4)
        EndIf
        If tourelle=1 And plane>=1 Then gldrawtext("tourelle",xmax-159,17,1.2)
        If Abs(auxvar)>0.00001 Then gldrawtext("auxvar= "+Str(auxvar),15,ymax-59,1.2)
        If Abs(auxvar2)>0.00001 Then gldrawtext("auxvar2= "+Str(auxvar2),15,ymax-79,1.2)
        If Abs(auxvar3)>0.00001 Then gldrawtext("auxvar3= "+Str(auxvar3),15,ymax-99,1.2)
        gldrawtext("prop= "+Str(Int(prop))+"  v= "+Str(Int(v*4.5))+"."+Str(Int(v*45+1000)Mod 10),15,ymax-35,1.2)
        gldrawtext("x= "+Str(Int(mx+dmx0))+"  y= "+Str(Int(my+dmy0))+"  z= "+Str(Int(mz-waterz)),15,ymax-10,1.2)
        If carb<carb0*0.1 Then glcolor3f(1,0,0) Else glcolor3f(0,1,1)
        gldrawtext("carb="+Str(Int(carb)),10,270,1)
        'drawboussole
        drawradar
        If time2<torder Then 
        	  glcolor3f(0,0,1)
        	  If order=1 Then gldrawtext("formation",xmax-70,17,1)
        	  If order=2 Then gldrawtext("attack",xmax-70,17,1)
        	  If order=3 Then gldrawtext("cover",xmax-70,17,1)
        	  If order=4 Then gldrawtext("break",xmax-70,17,1)
        EndIf
        glenable(gl_texture_2d)
        glEnable GL_DEPTH_TEST
                           
End Sub 
Sub subquit
	quit=1
End Sub
Sub subpause
	If pause=0 Then pause=1 Else pause=0
End Sub
Sub subresp
resp=guigettext("win2.resp")
End Sub
Sub prompt(ByRef msg As String,ByRef resp0 As String)
statictext("win2.msg","msg",10,10,400,40)
edittext("win2.resp","",@subresp,10,60,400,20)
openwindow("win2","flightcombat_chung",50,50,450,180)
printgui("win2.msg",msg)
printgui("win2.resp",resp0)
guisetfocus("win2.resp")
While guitestkey(vk_escape)=0 And guitestkey(vk_return)=0
	guiwait
	Sleep 100
Wend
guiscan
resp=guigettext("win2.resp")
guiclosewindow("win2")
End Sub
Sub subnarbre
	resp=Str(narbre2)
	msg="number of trees : enter a number (100..2500)  last="+Str(narbre2)
	prompt(msg,resp)
	i=Val(resp)
	i=max2(100,min2(2500,i))
	If i<>narbre2 Then
		narbre2=i
	EndIf
End Sub
Sub subdistscale
Dim As Single x	
	resp=Str(distscale)
	msg="max distance scale : enter a number (0.3 low .. 1.0 high)  last="+Str(distscale)
	prompt(msg,resp)
	x=Val(resp)
	x=max2(0.3,min2(1.0,x))
	If x<>distscale Then
		distscale=x
	EndIf
End Sub
Sub subreset(ByVal nomsg As Integer=0)
	If nomsg=0 Then
		confirm("reset ?","reset",resp)
	Else
		resp="yes"
	EndIf
	If resp="yes" Then
		dmx00=dmx0:dmy00=dmy0
		dmx0=dmx000:dmy0=dmy000
		If imap>=5 Then initterrain3
		mx=0:my=0
		mz=getterrainheight(mx,my)+mzh
		mz1=mz:mzsol0=mz:v=0
		o1=90:o2=0:o3=0
		volume=550:setvol
       
      dim as single x,y
      For i=1 To nhorse 
        If i<=6 Then 
           x=-190+Rnd*700:y=-100+Rnd*700
        Else
  	        If i<=12 Then 
              x=Rnd*700:y=-100+Rnd*700
  	        Else 
  	           If i<=17 Then 	
                 x=300+Rnd*700:y=300+Rnd*700
              Else 
  	              If i<=23 Then  
                    x=1500+Rnd*500:y=1500+Rnd*500
  	              Else 
  	                 If i<=27 Then 	
                       x=1000+Rnd*500:y=-700+Rnd*500
  	                 Else 
                       x=700+Rnd*500:y=-30+Rnd*500
  	                 EndIf 	
  	              EndIf 	
  	           EndIf
  	        EndIf  
        EndIf
        horseo1(i)=Rnd*360
        horsex(i)=x
        horsey(i)=y
        horsez(i)=getterrainheight(x,y)
        setnodeposition(md2_node(md2ihorse(i)),x,y,horsez(i))
        setnoderotation(md2_node(md2ihorse(i)),0,0,horseo1(i))
      Next i  
		
		initboat()

      For i=1 To nship
	      airshipx(i)=mx-i*1200+4000:airshipy(i)=my+i*1000-4000:airshipz(i)=mz+200.0 
      Next
      
      target=0
      
      initcloud()

	EndIf
End Sub
Sub submaxfps
Dim As Single x	
	resp=Str(maxfps)
	msg="max fps : enter a number (10 .. 100)  last="+Str(maxfps)
	prompt(msg,resp)
	x=Val(resp)
	x=max2(10,min2(100,x))
	If x<>maxfps Then
		maxfps=x
	EndIf
End Sub
Sub subsmooth
Dim As Integer x
If imap>=5 Then 	
	resp=Str(ismooth)
	msg="smooth terrain : enter a number (0 .. 8)  last="+Str(ismooth)
	prompt(msg,resp)
	x=Val(resp)
	x=max2(0,min2(8,x))
	If x<>ismooth Then
		ismooth=x
		dmx00=dmx0:dmy00=dmy0
		initterrain3
	EndIf
Else 
   notice("smooth terrain : country map only !")
EndIf 	
End Sub
Sub subwave
Dim As Single x
Dim As Integer ix,iy
If imap>=5 Then 	
	resp=Str(kwave)
	msg="water wave height : enter a number (0.1 - 8.0)  last="+resp+Chr(13)+_  
	    "0.1=calm  1=normal  4=stormy  8=tempest"
	prompt(msg,resp)
	x=Val(resp)
	x=max(0.1,min(8.0,x))
	If x<>kwave Then
		kwave=x
	   For ix=-100 To 612
		  For iy=-100 To 612
		  	 If water(ix,iy)=1 Then terrain(ix,iy)=-60.0001
		  Next
      Next   
	EndIf
Else 
   notice("water wave height : country map only !")
EndIf 	
End Sub
Sub subnboat
	resp=Str(nboat2)
	msg="number of boats : enter a number (4..12)  last="+resp
	prompt(msg,resp)
	i=Val(resp)
	i=max2(4,min2(12,i))
	If i<>nboat2 Then
		nboat2=i
	EndIf
End Sub
Sub subncloud
	resp=Str(ncloud2)
	msg="number of clouds : enter a number (0..200)  last="+resp
	prompt(msg,resp)
	i=Val(resp)
	i=max2(0,min2(200,i))
	If i<>ncloud2 Then
		ncloud2=i
	EndIf
End Sub
Sub subnship
	resp=Str(nship2)
	msg="number of planes : enter a number (4..18)  last="+resp
	prompt(msg,resp)
	i=Val(resp)
	i=max2(4,min2(18,i))
	If i<>nship2 Then
		nship2=i
	EndIf
End Sub
Sub sublevel
	resp=Str(level)
	msg="level : enter a number (0=easy,2=normal,4=hard) last="+resp
	prompt(msg,resp)
	i=Val(resp)
	i=max2(0,min2(4,i))
	If i<>level Then
		level=i
	EndIf
End Sub
Sub subcombo1
Dim As Integer typeship0,airshipmove0,airshipvie00,i
	getcomboindex("win.combo1",icar)
	If plane=0 Then v=0
	If icar=1 Then plane=0
	If icar=2 Then plane=1:car=1
	If icar=3 Then plane=1:car=2
	If icar=4 Then plane=1:car=3
	If icar=5 Then plane=1:car=0:typeavion=1
	If icar=6 Then plane=1:car=0:typeavion=2
	If icar=7 Then plane=1:car=0:typeavion=3
	If icar=8 Then plane=1:car=0:typeavion=3
	If icar=9 Then plane=1:car=0:typeavion=4
	If icar=10 Then plane=1:car=0:typeavion=5
	If icar=11 Then plane=1:car=0:typeavion=6
	If icar=12 Then plane=1:car=0:typeavion=6
	If icar=13 Then plane=1:car=0:typeavion=7
	If icar=14 Then plane=1:car=0:typeavion=8
	If icar=15 Then plane=2:car=0:typeavion=2
	If plane>=1 Then
		initavion
		If cartext<>0 Then guideletetexture(cartext)
      If car=1 Or (car=0 And typeavion=1) Then 
 	     cartext=guiloadtexture("objects/car.jpg",240,245)
      Else
        If car=2 Or (car=0 And typeavion=7) Then 	
 	       cartext=guiloadtexture("objects/car2.jpg",240,245)
        Else 
 	       cartext=guiloadtexture("objects/car3.jpg",240,245)
        EndIf 	
      EndIf
      If car>=1 Then volant=volant Mod 3
      typeship0=1:airshipmove0=1:airshipvie00=300 
      If typeavion=1 Then typeship0=0:airshipmove0=1:airshipvie00=300      
      If typeavion=2 Then typeship0=1:airshipmove0=1:airshipvie00=300       
      If typeavion=3 Then typeship0=6:airshipmove0=1:airshipvie00=300       
      If typeavion=4 Then typeship0=7:airshipmove0=1:airshipvie00=300       
      If typeavion=5 Then typeship0=8:airshipmove0=1:airshipvie00=300 
      If typeavion=6 Then typeship0=10:airshipmove0=1:airshipvie00=300 
      If typeavion=7 Then typeship0=5:airshipmove0=1:airshipvie00=300 
      If typeavion=8 Then typeship0=9:airshipmove0=1:airshipvie00=400 
      For i=4 To 6
      	typeship(i)=typeship0
			airshipmove(i)=airshipmove0
			airshipvie0(i)=airshipvie00
			If plane=2 Then 
				airshipx(i)+=avionx-mx
				airshipy(i)+=aviony-my
			EndIf
      Next
	Else
		stopsoundavion
	EndIf
	guisetfocus("win.graph")
	tourelle=0
End Sub
Sub subsol
  getcomboindex("win.sol",isol)
  If soltexture<>0 Then guideletetexture(soltexture)
  If isol=1 Then soltexture=guiloadtexture("media/sol.jpg")
  If isol=2 Then soltexture=guiloadtexture("media/herbe.jpg")
  If isol=3 Then soltexture=guiloadtexture("media/herbe2.jpg")
  If isol=4 Then soltexture=guiloadtexture("media/desert2.jpg")
  If isol=5 Then soltexture=guiloadtexture("media/desert.jpg")
  guisetfocus("win.graph")
End Sub
Sub submapsol
getcomboindex("win.mapsol",imapsol)
If maptexture<>0 Then guideletetexture(maptexture)
If imapsol>1 Then 
  maptexture=guiloadtexture("media/map"+Str(imapsol-2)+".jpg")
Else 
  maptexture=guiloadtexture("media/map"+Str(2)+".jpg")
EndIf 
guisetfocus("win.graph")
End Sub 
Sub subciel
Dim As uint i
  getcomboindex("win.ciel",i)
  If i>0 Then iciel=i
  If iciel>=2 Then 
   If cieltext<>0 Then guideletetexture(cieltext)
   cieltext=guiloadtexture("media/ciel"+Str(iciel-2)+".jpg")
  EndIf 
  guisetfocus("win.graph")
End Sub 
Dim Shared As uint x960=1200
'Dim Shared As UShort map2(1 To x960,1 To x960)
Dim Shared As Short map2(1 To x960,1 To x960)
Dim Shared As UByte map0(1 To x960,1 To x960)
Dim Shared As Short map(1 To x960,1 To x960)
Dim Shared As String addon(100)
Sub getmap00
Dim As Integer file 
Dim As String filename="media/heightmap1200x1200.bin"
file = FreeFile        
If Open ( filename For binary access read as #file) <> 0 Then
	notice "error open file "+filename
	Close #file
Else 
	Get #file,,map2()
	Close #file
EndIf
Dim As UShort aux=&h7fff'255+256+512+1024
Dim As Short x
For i=1 To x960
	For j=1 To x960 
      x=map2(i,j)
      If x>=0 Then
      	map(i,j)=(x)'/2.0
      Else
      	map(i,j)=0
      EndIf
	Next
Next
'ReDim As Short map2(1,1)
End Sub 
Sub getmap0
Dim As Integer file 
Dim As String filename="media/heightmap1200x1200.bin"
file = FreeFile        
If Open ( filename For binary access read as #file) <> 0 Then
	notice "error open file "+filename
	Close #file
Else 
	Get #file,,map2()
	Close #file
EndIf
aux=0
For i=1 To x960
	For j=1 To x960 
		map(i,j)=map2(i,j)And 255
	Next
Next
'ReDim As Short map2(1,1)
file = FreeFile
Open "media/heightmapbyte1200x1200.bin" for binary as #file
Put #file,1,map()
Close #file
End Sub 
Dim Shared As Integer dh0=0
Sub getmap
Dim As Integer file,i,j,k 
Dim As String filename,ficin
dh0=0
If imap=5 Then filename="media/heightmapbyte1200x1200_france.bin":dmx0=-160000:dmy0=-180000
If imap=6 Then filename="media/heightmapbyte1200x1200_england.bin":dmx0=100000:dmy0=-78000
If imap=7 Then filename="media/heightmapbyte1200x1200_yougo.bin":dmx0=17000:dmy0=-50000
If imap=8 Then filename="media/heightmapbyte1200x1200_grece.bin":dmx0=31000:dmy0=-390000
If imap=9 Then filename="media/heightmapbyte1200x1200_israel.bin":dmx0=-270000:dmy0=7000
If imap=10 Then filename="media/heightmapbyte1200x1200_japan.bin":dmx0=31000:dmy0=-405000
If imap=11 Then filename="media/heightmapbyte1200x1200_vietnam.bin":dmx0=91000:dmy0=-513000
If imap=12 Then filename="media/heightmapbyte1200x1200_hawai.bin":dmx0=-114000:dmy0=75000
If FileExists("addon/"+addon(imap)+".bin")<>0 Then
	filename="addon/"+addon(imap)+".bin"
	dmx0=0:dmy0=0
	If FileExists("addon/"+addon(imap)+".txt") Then 
      file = FreeFile        
      Open "addon/"+addon(imap)+".txt" For Input As #file
      If Not Eof(file) Then Line Input #file,ficin:dmx0=Val(ficin)		
      If Not Eof(file) Then Line Input #file,ficin:dmy0=Val(ficin)
      If Not Eof(file) Then Line Input #file,ficin:dh0=Val(ficin)
      Close #file		
	EndIf
EndIf
If FileExists(filename)=0 Then
	imap=5:filename="media/heightmapbyte1200x1200_france.bin":dmx0=-160000:dmy0=-180000
EndIf
file = FreeFile        
If Open ( filename For binary access read as #file) <> 0 Then
	notice "error open file "+filename
	Close #file
Else 
	Get #file,,map0()
	Close #file
EndIf
Dim As Integer h,h0,dhh,dh,dh0,waterh,dhh0,dh1=128
For i=1 To x960
	h=-9999:h0=-9999:dhh=0:dh=0:dh0=0:waterh=4
	For j=1 To x960
		h0=h
		h=map0(i,j)
		dh0=dh
		dh=h-h0
		dhh0=dhh
		If dh>dh1 Then
			If dhh>1 And j>1 Then dhh-=256
		ElseIf dh<-dh1 Then
			If dhh<1200 And j>1 Then dhh+=256
		EndIf
		If dhh=0 And (h<14 or h0<14) Then waterh=4
		If waterh>0 Then
			waterh-=1:dhh=0
			map2(i,j)=-9999
		Else
   		map2(i,j)=dhh
		EndIf
	Next
Next
For i=1 To x960
	h=-9999:h0=-9999:dhh=0:dh=0:dh0=0:waterh=4
	For j=1 To x960 
		h0=h
		h=map0(j,i)
		dh0=dh
		dh=h-h0
		dhh0=dhh
		If dh>dh1 Then
			If dhh>1 And j>1 Then dhh-=256
		ElseIf dh<-dh1 Then
			If dhh<1200 And j>1 Then dhh+=256
		EndIf
		If dhh=0 And (h<14 Or h0<14) Then waterh=4
		If waterh>0 Then
			waterh-=1:dhh=0
			map(j,i)=h
		ElseIf map2(j,i)<-9990 Then
			dhh=0
			map(j,i)=h
		Else 
			dhh=min2(dhh,map2(j,i))
			map(j,i)=h+dhh'min2(h+dhh,map2(j,i))
		EndIf 
	Next
Next '/
'ReDim As Short map2(1,1)
End Sub 
Dim Shared As Single mapx0,mapy0
Sub initterrain2
Dim As Integer ix,iy,z00,z10,z01,z11,jx,jy,ii,jj
Dim As Single aux,i,j,dx,dy,h,hh,hh1,hh2,hh3,alt
	For ix=-100 To 612
		For iy=-100 To 612
			i=(x960*(0.5+(ix-256+dmx0/scalex)/(k8*512)))
			j=(x960*(0.5-(iy-256+dmy0/scalex)/(k8*512)))
			'jx=Int(ix+(dmx0/scalex)+512000)Mod 512
			'jy=Int(iy+(dmy0/scalex)+512000)Mod 512
			'While i<1:i+=x960-1:Wend
			'While i>x960-1:i-=x960-1:Wend
			'While j<1:j+=x960-1:Wend
			'While j>x960-1:j-=x960-1:Wend
			If i>=1 And i<x960 And j>=1 And j<x960 Then 
			  ii=Int(i)
			  dx=i-ii
			  jj=Int(j)
			  dy=j-jj
			  z00=map(jj,ii)
			  z10=map(jj,ii+1)
			  z01=map(jj+1,ii)
			  z11=map(jj+1,ii+1)
           h=z00*(1-dx)*(1-dy)+z10*(dx)*(1-dy)+z01*(1-dx)*dy+z11*dx*dy
			Else 
			  h=0	
			EndIf
			terrain(ix,iy)=h
		Next
	Next
   Dim As Integer iaux
   iaux=0	
   While iaux<ismooth
  	 iaux+=1
	 For ix=-100 To 612
		For iy=-100 To 612
			terrain2(ix,iy)=terrain(ix,iy)
		Next
	 Next 	
	 For ix=-100+1 To 612-1
		For iy=-100+1 To 612-1
	      terrain(ix,iy)=(terrain2(ix,iy)*7+terrain2(ix-1,iy)+terrain2(ix+1,iy)+_
	                       terrain2(ix,iy-1)+terrain2(ix,iy+1))/11		
		Next
	 Next
   Wend 
	For ix=-100 To 612
		For iy=-100 To 612
			jx=Int(ix+(dmx0/scalex)+512000)Mod 512
			jy=Int(iy+(dmy0/scalex)+512000)Mod 512
			h=terrain(ix,iy)
         terrain(ix,iy)=h-74+dh0
         If terrain(ix,iy)>-60 Then
           alt=(h-74)*scalez
           hh=1.0:hh1=1.0:hh2=0:hh3=0
           If alt>1800 Then hh=min(1.5,1.0+(alt-1800)/400)
           If alt>2000 Then hh1=min(3.5,1.0+(alt-2000)/400)
           If alt>3000 Then hh2=0.7
           If h<24 Then hh3=min(0.32,(24-h)/24)
           terraincolor(ix,iy).x=min(1.0,terraincolor0(jx,jy).x*hh+hh2)
           terraincolor(ix,iy).y=min(1.0,terraincolor0(jx,jy).y*hh1+hh2+hh3+0.04)
           terraincolor(ix,iy).z=min(1.0,terraincolor0(jx,jy).z*hh1+hh2)
         Else
           terrain(ix,iy)=-60.0001	
		     terraincolor(ix,iy).z=1
		     terraincolor(ix,iy).y=0.2
		     terraincolor(ix,iy).x=0.2
		   EndIf 
			terrain2(ix,iy)=terrain(ix,iy)
		   'aux=max(aux,terrain(ix,iy))
		Next
	Next
	'notice Str(aux)
	For ix=-100+1 To 612-1
		For iy=-100+1 To 612-1
			water(ix,iy)=1
		Next
	Next
	For ix=-100+1 To 612-1
	  For iy=-100+1 To 612-1
		  For jx=-1+ix To 1+ix
		  	For jy=-1+iy To 1+iy
			  If terrain(jx,jy)>=-60 Then
			     water(ix,iy)=0
			     Exit For,For
			  EndIf		  		
		  	Next
		  Next
	  Next
	Next
End Sub
Sub initairport()
Dim As Integer i,j,k,h,h1,test
Randomize(imap*100)
nairport2=0
airporty(0)=-999
For i=30 To x960-30
	For j=30 To x960-30
		h=map(i,j)
		If (h-74+dh0)>(-60) Then
			If h=map(i,j+1) And h=map(i,j-1) Then
				If h=map(i+1,j) And h=map(i+1,j+1) And h=map(i+1,j-1) Then
						test=0
						For k=nairport2 To 1 Step -1
							If Abs(airporty(k)-i)<70 Then
								If Abs(airportx(k)-j)<70 Then
									j=max2(j,airportx(k)+70-1)
									test=1:Exit For
								EndIf
							EndIf
						Next k
						If test=0 Then
							nairport2+=1
							airportx(nairport2)=j
							airporty(nairport2)=i
							airportz(nairport2)=(h-74+dh0)*scalez'+68.5
					      If h=map(i+2,j) And h=map(i+2,j+1) And h=map(i+2,j-1) Then
								airporttype(nairport2)=3+Int(Rnd*2.9) '3pistes
							Else
								airporttype(nairport2)=1+Int(Rnd*1.4) '1piste
						   EndIf
						   'airporttype(nairport2)=5
						   Select Case airporttype(nairport2)
						   	Case 1,3
						   		airportz(nairport2)+=68.5 'tower
						   		airportvie(nairport2,1)=200
						   		airportr(nairport2)=50
						   		airportntower(nairport2)=1
						   	Case 2,4
						   		airportz(nairport2)+=58.5 'dome
						   		airportvie(nairport2,1)=400
						   		airportr(nairport2)=100
						   		airportntower(nairport2)=1
						   	Case Else 
						   		airportz(nairport2)+=68.5 '3towers
						   		airportvie(nairport2,1)=200
						   		airportvie(nairport2,2)=200
						   		airportvie(nairport2,3)=200
						   		airportr(nairport2)=50
						   		airportntower(nairport2)=3
						   End Select
							j+=70
						EndIf
						If nairport2>=nairport Then Exit For,For
				EndIf
			EndIf
		EndIf
	Next j
Next i
For i=1 To nairport2
			'i=(x960*(0.5+(dmx0/scalex)/(k8*512)))
			'j=(x960*(0.5-(dmy0/scalex)/(k8*512)))
	'dmx0=scalex*(k8*512)*mapx0/x960
	'dmy0=scalex*(k8*512)*mapy0/x960
	airportix(i)=airportx(i)
	airportiy(i)=airporty(i)
	airportx(i)=k8*512*scalex*(airportx(i)/x960-0.5)
	airporty(i)=-k8*512*scalex*(airporty(i)/x960-0.5)-260
Next
End Sub
Sub savetownvie()
Dim As Integer i,File
Dim As String fic
fic=exepath+"/save/map"+Str(imap)+".townvie" 
file=freefile
Open fic For Binary Access write As #file
Put #file,,townvie()
Close #file
End Sub 
Sub loadtownvie()
Dim As Integer i,File,ii,jj
Dim As String fic
fic=exepath+"/save/map"+Str(imap)+".townvie" 
For i=0 To ntown
 For ii=0 To 6
	For jj=0 To 6
		townvie(i,ii,jj)=200+townh(i,ii,jj)*0.5
	Next jj
 Next ii
Next i 
If FileExists(fic)=0 Then
	Exit Sub 
EndIf
file=freefile
Open fic For Binary Access Read As #file
Get #file,,townvie()
Close #file
townchanged=0
End Sub 
Sub inittown()
Dim As Integer i,j,k,h,h1,test,test0,test2,ii,jj,dii
Dim As Single townvie0
Randomize(imap*100)
ntown2=0
towny(0)=-999
For i=30 To x960-30
	For j=30 To x960-30
		h=map(i,j)
		If (h-74+dh0)>(-60) Then
			test0=0
			dii=3
			For ii=i-dii To i+dii
				For jj=j-dii To j+dii
					If Abs(h-map(ii,jj))>5 Then test0=1:Exit For,For
				Next
			Next
			If test0=0 Then 
						test=0
						For k=ntown2 To 1 Step -1
							If Abs(towny(k)-i)<70 Then
								If Abs(townx(k)-j)<70 Then
									j=max2(j,townx(k)+70-1)
									test=1:Exit For
								EndIf
							EndIf
						Next k
						If test=0 Then 
						 For k=nairport2 To 1 Step -1
							If Abs(airportiy(k)-i)<20 Then
								If Abs(airportix(k)-j)<20 Then
									j=max2(j,airportix(k)+20-1)
									test=1:Exit For
								EndIf
							EndIf
						 Next k
						EndIf  
						If test=0 Then
							ntown2+=1
							townx(ntown2)=j
							towny(ntown2)=i
							townz(ntown2)=(h-74+dh0)*scalez'+68.5
					      test2=0
					      For ii=i-dii To i+dii
					      	For jj=j-dii To j+dii
					      		test2=max2(test2,Abs(h-map(ii,jj)))
					      	Next
					      Next
					      If test2<=4 Then 
								towntype(ntown2)=2
							Else
								towntype(ntown2)=1
						   EndIf
						   'towntype(ntown2)=1
						   Select Case towntype(ntown2)
						   	Case 1
						   		dii=3
						   		For ii=i-dii To i+dii
						   		 For jj=j-dii To j+dii
						   			townbuild(ntown2,ii-i+dii,jj-j+dii)=Int(Rnd*3)
						   			townh(ntown2,ii-i+dii,jj-j+dii)=50+Rnd*500
						   			h1=map(ii,jj)
						   			towndz(ntown2,ii-i+dii,jj-j+dii)=(h1-74+dh0-1)*scalez
						   		   'If towndz(ntown2,ii-i+dii,jj-j+dii)<waterz Then
						   		   '	townbuild(ntown2,ii-i+dii,jj-j+dii)=0
						   		   'EndIf
						   		 Next
						   		Next
						   	Case Else
						   		dii=3
						   		For ii=i-dii To i+dii
						   		 For jj=j-dii To j+dii
						   			townbuild(ntown2,ii-i+dii,jj-j+dii)=Int(Rnd*4)
						   			h1=50+Rnd*800
						   			If h1>450 Then h1*=1.45
						   			townh(ntown2,ii-i+dii,jj-j+dii)=h1
						   			h1=map(ii,jj)
						   			towndz(ntown2,ii-i+dii,jj-j+dii)=(h1-74+dh0-1)*scalez
						   		   'If towndz(ntown2,ii-i+dii,jj-j+dii)<waterz Then
						   		   '	townbuild(ntown2,ii-i+dii,jj-j+dii)=0
						   		   'EndIf
						   		 Next
						   		Next
						   End Select
							j+=70
						EndIf
						If ntown2>=ntown Then Exit For,For
   		EndIf
		EndIf
	Next j
Next i
loadtownvie()
For i=1 To ntown2
			'i=(x960*(0.5+(dmx0/scalex)/(k8*512)))
			'j=(x960*(0.5-(dmy0/scalex)/(k8*512)))
	'dmx0=scalex*(k8*512)*mapx0/x960
	'dmy0=scalex*(k8*512)*mapy0/x960
	For ii=0 To 6
		For jj=0 To 6
			towndx(i,ii,jj)=k8*512*scalex*((townx(i)-3+jj)/x960-0.5)
	      towndy(i,ii,jj)=-k8*512*scalex*((towny(i)-3+ii)/x960-0.5)-260
	      If Rnd<0.3 Or townvie(i,ii,jj)>0 Then 
	        townvie0=200+townh(i,ii,jj)*0.5
	        townvie(i,ii,jj)=min(townvie0,max(-90.0,townvie(i,ii,jj)+townvie0/5))
	      EndIf    
		Next
	Next
	townx(i)=k8*512*scalex*(townx(i)/x960-0.5)
	towny(i)=-k8*512*scalex*(towny(i)/x960-0.5)-260
Next
End Sub
Function initmaptexture() As uint 
Dim As hbitmap hbmp
Dim As hdc bmpdc
Dim As uint x,y,bmpx,bmpy,itexture
Dim As Any Ptr bmpbits
x=300:y=300
hbmp=guicreateDIBbmp(x,y)
bmpbits=guigetbmpdibbits(hbmp)
bmpdc=createcompatibleDC(guiwindc0)
selectobject(bmpdc,hbmp)
guigetbmpXY(bmpdc,bmpx,bmpy)
Dim As uint pix,k:Dim As uint i,j,r
ReDim As uint picbits(1 To bmpx*bmpy)
GetBitmapBits hbmp,bmpx*bmpy*4,@PicBits(1)
For i=1 To bmpx
	For j=1 To bmpy
		k=map(j*x960/bmpx,i*x960/bmpy)
		If k>(14-dh0) Then
			If k>255 Then 
      		r=((k Shr 8)Shl 7)And 255:r=(r Shl 16)Or(r Shl 8)
	   		picbits(i+(j-1)*bmpx)=(k Shl 9)Or r 'green land
			Else 
	   		picbits(i+(j-1)*bmpx)=(k Shl 8) 'green land
			EndIf 	
		Else 	
			picbits(i+(j-1)*bmpx)=180 'blue water
		EndIf
	Next
Next
SetBitmapBits hbmp,bmpx*bmpy*4,@PicBits(1)
glGenTextures(1, @itexture)
'guinotice("texture="+Str(itexture),"ok")
glEnable GL_TEXTURE_2D 
glBindTexture(GL_TEXTURE_2D, itexture) 
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, bmpx,bmpy, GL_BGRA ,GL_UNSIGNED_BYTE ,bmpbits )
'gluBuild2DMipmaps(GL_TEXTURE_2D, 4, bmpx,bmpy, GL_BGRA ,GL_UNSIGNED_byte ,@(picbits(1)) ) 
'guinotice("ok mipmap")
glBindTexture (GL_TEXTURE_2D, 0)
deletedc(bmpdc)
deleteobject(hbmp)
Return itexture 
End function
Sub initterrain
Dim As hbitmap hbmp,hbmp2
Dim As hdc bmpdc,bmp2dc
Dim As Integer x,y,bmpx,bmpy,bmp2x,bmp2y,itexture,pix
Dim As Single h,k,hmin
If FileExists("media/terrain_height"+Str(imap)+".jpg")<>0 Then
	hbmp=guiloadimage("media/terrain_height"+Str(imap)+".jpg")
Else 	
	hbmp=guiloadimage("media/terrain_height"+Str(1)+".jpg")
EndIf
bmpdc=createcompatibleDC(guiwindc0)
selectobject(bmpdc,hbmp)
guigetbmpXY(bmpdc,bmpx,bmpy)
If FileExists("media/terrain_texture"+Str(imap)+".jpg")<>0 Then
	hbmp2=guiloadimage("media/terrain_texture"+Str(imap)+".jpg")
Else
  If FileExists("addon/"+addon(imap)+".jpg")<>0 Then
	 hbmp2=guiloadimage("addon/"+addon(imap)+".jpg")
  Else
	 hbmp2=guiloadimage("media/terrain_texture"+Str(1)+".jpg")
  EndIf 
EndIf
bmp2dc=createcompatibleDC(guiwindc0)
selectobject(bmp2dc,hbmp2)
guigetbmpXY(bmp2dc,bmp2x,bmp2y)
For x=-100 To 612
	For y=-100 To 612
		terrain(x,y)=0-74
		terraincolor(x,y).z=1
		terraincolor(x,y).y=0.2
		terraincolor(x,y).x=0.2
	Next
Next
hmin=255
For x=0 To 512-1
	For y=0 To 512-1
		pix=getpixel(bmpdc,Int(0.1+bmpx*x/512),Int(0.1+bmpy*y/512))
		h=((pix And 255)+((pix Shr 8)And 255)+((pix Shr 16)And 255))/3
		terrain(x,y)=h-74
		hmin=min(hmin,h)
		k=min(1.0,max(0.5,(h-20)/20))
		If imap<4 Then 
			pix=getpixel(bmp2dc,Int(0.1+bmp2x*((x*4) Mod 512)/512),Int(0.1+bmp2y*((y*4) Mod 512)/512))
		Else 
			pix=getpixel(bmp2dc,Int(0.1+bmp2x*(x)/512),Int(0.1+bmp2y*(y)/512))
		EndIf
      terraincolor(x,y).z=k*((pix Shr 16)And 255)/255.0
      terraincolor(x,y).y=k*((pix Shr 8)And 255)/255.0
      terraincolor(x,y).x=k*(pix And 255)/255.0
      terraincolor0(x,y).z=terraincolor(x,y).z
      terraincolor0(x,y).y=terraincolor(x,y).y
      terraincolor0(x,y).x=terraincolor(x,y).x
	Next
Next
deletedc(bmpdc)
deletedc(bmp2dc)
deleteobject(hbmp2)
deleteobject(hbmp)
dmx0=0:dmy0=0
If imap>=5 Then 
   getmap
   initterrain2
   waterz=-60*scalez
Else
	waterz=-9999*scalez
   Dim As Integer ix,iy
   Dim As Single alt,hh,hh1,hh2,hh3
   For ix=0 To 512
   	For iy=0 To 512
           alt=(terrain(ix,iy)-hmin)*scalez
           hh=1.0:hh1=1.0:hh2=0:hh3=0
           If alt>1800 Then hh=min(1.5,1.0+(alt-1800)/400)
           If alt>2000 Then hh1=min(3.5,1.0+(alt-2000)/400)
           If alt>3000 Then hh2=0.7
           'h=terrain(ix,iy)+74
           'If h<7 Then hh3=min(0.32,(7-h)/24)
           terraincolor(ix,iy).x=min(1.0,terraincolor0(ix,iy).x*hh+hh2)
           terraincolor(ix,iy).y=min(1.0,terraincolor0(ix,iy).y*hh1+hh2+hh3)
           terraincolor(ix,iy).z=min(1.0,terraincolor0(ix,iy).z*hh1+hh2)
   	Next
   Next
   For x=-100 To 612
	 For y=-100 To 612
		If x<0 Or x>=512 Or y<0 Or y>=512 Then 
		   terraincolor(x,y).z=0.1
		   terraincolor(x,y).y=0.59
		   terraincolor(x,y).x=0.1
		EndIf 
		water(x,y)=0
		terrain2(x,y)=terrain(x,y)
	 Next
   Next
EndIf
dmx000=dmx0:dmy000=dmy0
End Sub
Sub terrainline(ix As Integer,iy As Integer,jx As Integer,jy As Integer, _  
	             r As Single=1.0,g As Single=0,b As single=0.9)
Dim As Integer x,y,xx,yy
Dim As Single dx,dy,dist,xxx,yyy,h0,h
randomize(ix+iy+jx+jy)
x=Int((ix-dmx0)/scalex+100000)-100000+256
y=Int((iy-dmy0)/scalex+100000)-100000+256
xx=Int((jx-dmx0)/scalex+100000)-100000+256
yy=Int((jy-dmy0)/scalex+100000)-100000+256
xxx=x
yyy=y
dist=Abs(xx-xxx)+Abs(yy-yyy)+1
dx=0.5*((Rnd-0.5)*0.4+(xx-xxx)/dist)
dy=0.5*((Rnd-0.5)*0.4+(yy-yyy)/dist)
h=-9999:h0=h
While dist>3
  If Rnd<0.01 Or (dist<20 And Rnd<0.05) Then
     dx=0.5*((Rnd-0.5)*0.4*min(1.0,dist/80.0)+(xx-xxx)/dist)
     dy=0.5*((Rnd-0.5)*0.4*min(1.0,dist/80.0)+(yy-yyy)/dist)
  EndIf
  xxx+=dx
  yyy+=dy
  dist=Abs(xx-xxx)+Abs(yy-yyy)+1
  If Int(xxx)<>x Or Int(yyy)<>y Then 
      x=Int(xxx)
      y=Int(yyy)
		If x>-100 And x<612 And y>-100 And y<612 Then 
			h0=h:h=terrain(x,y):If Abs(h-h0)>40 And h0>(-999) Then Exit While
			If water(x,y)=0 And water(x-1,y)=0 And water(x+1,y)=0 _ 
			   And water(x,y-1)=0 And water(x,y+1)=0 Then  
   		   terraincolor(x,y).x=r
	   	   terraincolor(x,y).y=g
		      terraincolor(x,y).z=b
		   EndIf    
		EndIf
  EndIf 		
Wend 
End Sub
Dim Shared As Integer nobj,nobj2:nobj=nairport+ntown
Dim Shared As Integer objx(nobj),objy(nobj),objtype(nobj)
Sub initterrainlines()
Dim As Integer i,j,k,ix,iy,jx,jy,dx,dy,dx2,dy2,dx3,dy3,dxmax,dist,dist1
Dim As Integer dx4,dy4,dx5,dy5
i=1:j=1
For k=1 To nairport2+ntown2
	If towny(i)>=airporty(j) And i<=ntown2 Then 
		objx(k)=townx(i):objy(k)=towny(i):i+=1
		objtype(k)=0
	ElseIf j>nairport2 Then  
		objx(k)=townx(i):objy(k)=towny(i):i+=1
		objtype(k)=0
	Else
		objx(k)=airportx(j):objy(k)=airporty(j):j+=1
		objtype(k)=1
	EndIf
Next
nobj2=nairport2+ntown2
For i=1 To nobj2-1
	ix=objx(i)
	iy=objy(i)
	dxmax=300000
	If objtype(i)=1 Then dxmax=120000
	dx=dxmax:dy=dxmax:dx2=dx:dy2=dy:dx3=dx:dy3=dy:dist=dxmax
	dx4=dx:dy4=dy:dx5=dx:dy5=dy
	For j=nobj2 To i+1 Step -1
		If objtype(j)=0 Then dist1=dist Else dist1=min2(dist,120000)
		If Abs(objx(j)-ix)<=dist1 Then
			If Abs(objy(j)-iy)<=dist1 Then
				'dx5=dx4:dy5=dy4
				'dx4=dx3:dy4=dy3
				'dx3=dx2:dy3=dy2
				If objtype(i)=0 Then dx2=dx:dy2=dy
				dx=objx(j)-ix
				dy=objy(j)-iy
				dist=max2(Abs(dx),Abs(dy))
			EndIf
		EndIf
	Next
	'dx=10000:dy=10000
	If Abs(dx)<dxmax And Abs(dy)<dxmax Then terrainline(ix,iy,ix+dx,iy+dy)
	If Abs(dx2)<dxmax And Abs(dy2)<dxmax Then	terrainline(ix,iy,ix+dx2,iy+dy2)
	'If Abs(dx3)<dxmax And Abs(dy3)<dxmax Then	terrainline(ix,iy,ix+dx3,iy+dy3)
	'If Abs(dx4)<dxmax And Abs(dy4)<dxmax Then	terrainline(ix,iy,ix+dx4,iy+dy4)
	'If Abs(dx5)<dxmax And Abs(dy5)<dxmax Then	terrainline(ix,iy,ix+dx5,iy+dy5)
Next
End Sub
Function getterrainheight(ByVal x As Single,ByVal y As Single) As Single
Dim As Single h,dx,dy,z00,z10,z01,z11 
Dim As Integer i,j,i1,j1
	i=Int(x/scalex+100000)-100000
	dx=x/scalex-i
	j=Int(y/scalex+100000)-100000
	dy=y/scalex-j
	i+=256
	j+=256
	while i<-100:i+=512+200:Wend 
	While i>=612:i-=512+200:Wend 
	while j<-100:j+=512+200:Wend 
	While j>=612:j-=512+200:Wend
	i1=i+1
	j1=j+1
	While i1>=612:i1-=512+200:Wend
	While j1>=612:j1-=512+200:Wend 
z00=terrain(i,j)
z10=terrain(i1,j)
z01=terrain(i,j1)
z11=terrain(i1,j1)
If dx<=(1.0-dy) Then 
     h=( dx*(z10-z00) +dy*(z01-z00) +z00)
Else 
     h=( (1-dx)*(z01-z11) +(1-dy)*(z10-z11) +z11 )
EndIf 
Return h*scalez
End Function
Sub submap
Dim As uint i
  getcomboindex("win.map",i)
  i=i+4
  If i>0 And i<>imap Then
  	 imap=i:initterrain
  	 subreset(1)
  	 quit=1:restart=1
  EndIf
  guisetfocus("win.graph")
End Sub
Sub subgrass
Dim As uint i
  getcomboindex("win.grass",i)
  If i>0 Then igrass=i
  guisetfocus("win.graph")
End Sub
Sub subfog
Dim As uint i
  getcomboindex("win.fog",i)
  If i>0 Then ifog=i-1
  setfog
  guisetfocus("win.graph")
End Sub
Sub Subscreen
	If fullscreen=0 Then fullscreen=1 Else fullscreen=0
	quit=1:restart=1
End Sub
Sub initcar 
 If car=1 Or (car=0 And typeavion=1) Then 
 	cartext=guiloadtexture("objects/car.jpg",240,245)
 Else
 	If car=2 Then 
 	   cartext=guiloadtexture("objects/car2.jpg",240,245)
 	Else 
 	   cartext=guiloadtexture("objects/car3.jpg",240,245)
 	EndIf 	
 EndIf
End Sub  
Sub initarbres
	Randomize((imap-1)*100)
	'distarbre=5000
	distbuisson=distarbre
   For i=1 To 7
   	arbretext(i)=guiloadtexture("media/arbre"+Str(i)+".jpg",180)
   Next
	For i=1 To narbre
	  arbretype(i)=Int(Rnd*6.25)+1	
     arbrex(i)=(Rnd-0.5)*2*distarbre
     arbrey(i)=(Rnd-0.5)*2*distarbre
     arbrez(i)=getterrainheight(arbrex(i),arbrey(i))
     arbrerot(i)=Rnd*360 
     arbrescalex(i)=1+Rnd*0.15
     arbrescalez(i)=1+Rnd*0.3
     arbrerot0(i)=arbrerot(i)		
	Next
	buissontext(1)=arbretext(2)
	buissontext(2)=arbretext(5)
	For i=1 To nbuisson
	  buissontype(i)=Int(Rnd*1.99)+1	
     buissonx(i)=(Rnd-0.5)*2*distbuisson
     buissony(i)=(Rnd-0.5)*2*distbuisson
     buissonz(i)=getterrainheight(buissonx(i),buissony(i))
     buissonrot(i)=Rnd*360 
     buissonrot0(i)=buissonrot(i)		
	Next
End Sub
Sub testairportarbres()
Dim As Integer i
For i=1 To narbre2 
     If testairport(arbrex(i),arbrey(i))=0 Then 
     	  arbrez(i)=getterrainheight(arbrex(i),arbrey(i))
     Else
     	  arbrez(i)=waterz-1
     EndIf
Next
For i=1 To nbuisson
     If testairport(buissonx(i),buissony(i))=0 Then
     	  buissonz(i)=getterrainheight(buissonx(i),buissony(i))
     Else
     	  buissonz(i)=waterz-1
     EndIf
Next
End Sub 
Sub initgrass
Dim As Integer ix,iy
Dim As Single aux
	'distgrass=500
   For i=1 To 2
   	grasstext(i)=guiloadtexture("media/grass"+Str(i)+".gif",200)
   Next
	For i=1 To ngrass
	  grasstype(i)=Int(Rnd*1.19)+1	
     grassx(i)=(Rnd-0.5)*2*distgrass
     grassy(i)=(Rnd-0.5)*2*distgrass
     grassz(i)=getterrainheight(grassx(i),grassy(i))
     grassrot(i)=Rnd*180 
     grassscalex(i)=1+Rnd*0.15
     grassscalez(i)=1+Rnd*0.3
     grasstx(i)=rnd
     grassrot0(i)=grassrot(i)		
     ix=Int(grassx(i)/scalex+10000)-10000
     iy=Int(grassy(i)/scalex+10000)-10000
     ix=max2(0,min2(512,ix))
     iy=max2(0,min2(512,iy))
     aux=1-terraincolor(ix,iy).x
     grassscalez2(i)=1.6*aux*grassscalez(i)
	Next
End Sub 
Sub inithouses
Dim As Single x=60,y=60
	disthouse=10000
	building3text=guiloadtexture("media/building3.jpg",35,70)
	rooftext=guiloadtexture("media/roof.jpg")
	For i=1 To nhouse
     housex(i)=(Rnd-0.5)*2*disthouse
     housey(i)=(Rnd-0.5)*2*disthouse
     housez(i)=getterrainheight(housex(i)-x,housey(i)-y)		
     housez(i)=min(housez(i),getterrainheight(housex(i)+x,housey(i)-y))		
     housez(i)=min(housez(i),getterrainheight(housex(i)+x,housey(i)+y))		
     housez(i)=min(housez(i),getterrainheight(housex(i)-x,housey(i)+y))		
	Next
x=50:y=110
   disthouse2=14000 
	For i=1 To nhouse2
     house2x(i)=(Rnd-0.5)*2*disthouse2
     house2y(i)=(Rnd-0.5)*2*disthouse2
     house2z(i)=getterrainheight(house2x(i)-x,house2y(i)-y)		
     house2z(i)=min(house2z(i),getterrainheight(house2x(i)+x,house2y(i)-y))		
     house2z(i)=min(house2z(i),getterrainheight(house2x(i)+x,house2y(i)+y))		
     house2z(i)=min(house2z(i),getterrainheight(house2x(i)-x,house2y(i)+y))		
	Next
End Sub
'Dim Shared As uint nhorse=29,md2ihorse(nhorse)
Dim Shared As uint disthorse=12000
Dim Shared As md2_model Ptr md2horse
'Dim Shared As Single horsex(nhorse),horsey(nhorse),horsez(nhorse),horseo1(nhorse),horseo2(nhorse)
Dim Shared As Integer inithorse=0
Sub inithorses
Dim As uint md2inode
Dim As Single x,y
For i=1 To 4
	md2horsetext(i)=guiloadtexture("objects/horse"+Str(i)+".jpg")	
Next
For i=1 To 3
	md2zebratext(i)=guiloadtexture("objects/zebra"+Str(i)+".jpg")	
Next
aviontext=guiloadtexture("objects/c150.jpg")
corsairtext=guiloadtexture("objects/f4u.jpg")
vg33text=guiloadtexture("objects/vg33.jpg")
zerotext=guiloadtexture("objects/zero1.jpg")
zero2text=guiloadtexture("objects/zerogreen.jpg")
b25text=guiloadtexture("objects/b25.jpg")
alphajettext=guiloadtexture("objects/alphajet.jpg")
spitfiretext=guiloadtexture("objects/spitfire_lowpoly2.jpg")
spitfiretext2=guiloadtexture("objects/spitfire_lowpoly22.jpg")
bf109text=guiloadtexture("objects/bf109.jpg")
p51dtext=guiloadtexture("objects/p51d.jpg")
f14text=guiloadtexture("objects/f14_2.jpg")
For i=1 To nhorse
  md2inode=addmd2node(md2horse)
  If i<=12 Then 
  	setnodetexture(md2_node(md2inode),md2horsetext((i Mod 4)+1))
   setnodescale(md2_node(md2inode),0.85,0.85,0.85)
   x=-190+Rnd*700:y=-100+Rnd*700
  Else
  	setnodetexture(md2_node(md2inode),md2zebratext((i Mod 3)+1))
   setnodescale(md2_node(md2inode),0.77,0.77,0.65)
   x=Rnd*700:y=-100+Rnd*700
  EndIf
  If inithorse=1 Then 
  	x=horsex(i):y=horsey(i)
  Else 
  	horseo1(i)=Rnd*360
  EndIf
  horsex(i)=x
  horsey(i)=y
  horsez(i)=getterrainheight(x,y)
  setnodeposition(md2_node(md2inode),x,y,horsez(i)) 
  setanim(md2_node(md2inode),md2_all)
  'setanimationrange(md2_node(md2inode),1,11)
  md2ihorse(i)=md2inode
  If i=5 Then setnodescale(md2_node(md2inode),0.5,0.5,0.5)
  If i=12 Then setnodescale(md2_node(md2inode),0.5,0.5,0.5)
  If i=20 Then setnodescale(md2_node(md2inode),0.5,0.5,0.5)
  setnoderotation(md2_node(md2inode),0,0,horseo1(i))
Next i
inithorse=1
End Sub
Dim Shared As Single thorse(nhorse)
Sub movehorses
Dim As Single co1,si1,co2,si2,dmx,dmy,dmz,vhorse,horsex0,horsey0,horsez0
Dim As Integer changehorse,horsesound
Dim As md2_node Ptr md2node
For i=1 To nhorse
  md2node=md2_node(md2ihorse(i))	
  horseo1(i)=md2node->rotz
  co1=Cos(degtorad*horseo1(i))
  si1=Sin(degtorad*horseo1(i))	
  si2=0.6*max(-0.9,sin(degtorad*horseo2(i)))
  If si2>0 Then 
  	co2=max(0.4,Cos(degtorad*horseo2(i)))
  Else 	
  	co2=max(0.1,Cos(degtorad*horseo2(i)))
  EndIf
  If i<=6 Then
    If max(Abs(horsex(1)-md2node->x),Abs(horsey(1)-md2node->y-i*50))<500 Then
   	vhorse=5
    Else 
  	   vhorse=6.2
    EndIf 
  Else 
  	 If i<=12 Then 
      If max(Abs(horsex(7)-md2node->x),Abs(horsey(7)-md2node->y-(i-6)*50))<500 Then
   	  vhorse=4.5
      Else 
  	    vhorse=5.7
      EndIf
    Else 
    	If i<=17 Then 
        If max(Abs(horsex(13)-md2node->x),Abs(horsey(13)-md2node->y-(i-12)*50))<500 Then
   	   vhorse=4.5
        Else 
  	      vhorse=5.7
        EndIf
    	Else
    	  If i<=23 Then 	
          If max(Abs(horsex(18)-md2node->x),Abs(horsey(18)-md2node->y-(i-17)*150))<500 Then
   	     vhorse=4.5
          Else 
  	        vhorse=5.7
          EndIf
    	  Else
    	  	 If i<=27 Then 
            If max(Abs(horsex(24)-md2node->x),Abs(horsey(24)-md2node->y-(i-23)*50))<500 Then
   	       vhorse=4.5
            Else 
  	          vhorse=5.7
            EndIf
    	  	 Else 
            If max(Abs(horsex(28)-md2node->x),Abs(horsey(28)-md2node->y-(i-28)*50))<400 Then
   	       vhorse=4.5
            Else 
  	          vhorse=5.7
            EndIf
    	  	 EndIf 	
    	  EndIf 	
    	EndIf 	
    EndIf 
  EndIf
  horsex0=horsex(i)
  horsey0=horsey(i)
  horsez0=horsez(i)
  horsex(i)=md2node->x+vhorse*co1*co2*(1+si2)*kfps*2	
  horsey(i)=md2node->y+vhorse*si1*co2*(1+si2)*kfps*2	
  horsez(i)=getterrainheight(horsex(i),horsey(i))
  dmz=GetTerrainHeight(horsex(i)+40*co1,horsey(i)+40*si1)-horsez(i)
  horseo2(i)=-Atn(dmz/40)*radtodeg
  If horseo2(i)<-30 Then
      dmy=getterrainheight(horsex(i)+40*si1,horsey(i)-40*co1)-horsez(i)
   	horseo1(i)+=sgn2(dmy)*40'3*kfps
  Else 
  	   If (horsez(i)<=waterz And horsez0>=(waterz)) Then   	   	
        dmy=getterrainheight(horsex(i)+40*si1,horsey(i)-40*co1)-horsez(i)
   	  horseo1(i)-=sgn2(dmy)*40'3*kfps
  	   EndIf
  EndIf
  If horsez(i)<waterz And horsez0>(waterz+1e-3) And time2<(thorse(i)+10.000) Then
  	 horsex(i)=horsex0
  	 horsey(i)=horsey0
  	 horsez(i)=horsez0
  Else 
  	 thorse(i)=time2
  EndIf
  If Rnd<0.02*kfps Then
  	  If i=1 Or i=7 Or i=13 Or i=18 Or i=24 Or i=28 Then 
  	    If Rnd<0.05 Then
  	    	horseo1(i)+=(Rnd-0.5)*120
  	    	If (Abs(horsex(i))+Abs(horsey(i)))>(distmax-7000) Then
  	    		If Abs(horsex(i))>Abs(horsey(i)) Then
  	    			If horsex(i)>0 Then horseo1(i)=180 Else horseo1(i)=0
  	    		Else
  	    		   If horsey(i)>0 Then horseo1(i)=-90 Else horseo1(i)=90
  	    	   EndIf 
  	    	EndIf
  	    EndIf
  	  Else 
  	    If i<=6 Then 
  	    	dmx=(horsex(1)-horsex(i))*si1-(horsey(1)-horsey(i)-i*50)*co1
  	    Else 
  	    	If i<=12 Then 
  	    	   dmx=(horsex(7)-horsex(i))*si1-(horsey(7)-horsey(i)-(i-6)*50)*co1
  	    	Else
  	    		If i<=17 Then 
  	    	     dmx=(horsex(13)-horsex(i))*si1-(horsey(13)-horsey(i)-(i-12)*50)*co1
  	    		Else 
  	    		  If i<=23 Then 
  	    		  	 dmx=(horsex(18)-horsex(i))*si1-(horsey(18)-horsey(i)-(i-17)*150)*co1
  	    		  Else
  	    		  	 If i<=27 Then 
  	    		  	 	dmx=(horsex(24)-horsex(i))*si1-(horsey(24)-horsey(i)-(i-23)*50)*co1
  	    		  	 Else 
  	    		  	 	dmx=(horsex(28)-horsex(i))*si1-(horsey(28)-horsey(i)-(i-27)*50)*co1
  	    		  	 EndIf
  	    		  EndIf
  	    		EndIf 		
  	    	EndIf
  	    EndIf
  	    horseo1(i)-=Sgn2(dmx)*2*max(4.0,vhorse)
  	  EndIf 
     If max(Abs(horsex(i)-mx),Abs(horsey(i)-my))<300 And horsez(i)>(waterz) Then
     	 horsesound=1
     EndIf
  EndIf
  setnodeposition(md2node,horsex(i),horsey(i),horsez(i))	
  setnoderotation(md2node,0,horseo2(i),horseo1(i))	
Next i
  If horsesound=1 And Rnd<0.1 Then soundhorse
  horsesound=0
  changehorse=0:i=1
  horsex0=horsex(i):horsey0=horsey(i) 
  While horsex(i)<mx-disthorse :horsex(i)+=disthorse*2:changehorse=1:Wend 
  While horsex(i)>mx+disthorse :horsex(i)-=disthorse*2:changehorse=1:Wend 	
  While horsey(i)<my-disthorse :horsey(i)+=disthorse*2:changehorse=1:Wend 
  While horsey(i)>my+disthorse :horsey(i)-=disthorse*2:changehorse=1:Wend 
  If changehorse=1 Then
    horsez(i)=getterrainheight(horsex(i),horsey(i))
    setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
    For i=2 To 6
    	If max(Abs(horsex(i)-mx),Abs(horsey(i)-my))>4000 Then 
  		 horsex(i)+=horsex(1)-horsex0
  		 horsey(i)+=horsey(1)-horsey0
       horsez(i)=getterrainheight(horsex(i),horsey(i))
       setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
  	   EndIf 
  	 Next
  EndIf
  changehorse=0:i=7
  horsex0=horsex(i):horsey0=horsey(i) 
  While horsex(i)<mx-disthorse :horsex(i)+=disthorse*2:changehorse=1:Wend 
  While horsex(i)>mx+disthorse :horsex(i)-=disthorse*2:changehorse=1:Wend 	
  While horsey(i)<my-disthorse :horsey(i)+=disthorse*2:changehorse=1:Wend 
  While horsey(i)>my+disthorse :horsey(i)-=disthorse*2:changehorse=1:Wend 
  If changehorse=1 Then
    horsez(i)=getterrainheight(horsex(i),horsey(i))
    setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
    For i=8 To 12
    	If max(Abs(horsex(i)-mx),Abs(horsey(i)-my))>4000 Then 
   	 horsex(i)+=horsex(7)-horsex0
  		 horsey(i)+=horsey(7)-horsey0
       horsez(i)=getterrainheight(horsex(i),horsey(i))
       setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
  	   EndIf 
  	 Next
  EndIf
  changehorse=0:i=13
  horsex0=horsex(i):horsey0=horsey(i) 
  While horsex(i)<mx-disthorse :horsex(i)+=disthorse*2:changehorse=1:Wend 
  While horsex(i)>mx+disthorse :horsex(i)-=disthorse*2:changehorse=1:Wend 	
  While horsey(i)<my-disthorse :horsey(i)+=disthorse*2:changehorse=1:Wend 
  While horsey(i)>my+disthorse :horsey(i)-=disthorse*2:changehorse=1:Wend 
  If changehorse=1 Then
    horsez(i)=getterrainheight(horsex(i),horsey(i))
    setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
    For i=14 To nhorse'17
    	If max(Abs(horsex(i)-mx),Abs(horsey(i)-my))>4000 Then 
  		 horsex(i)+=horsex(13)-horsex0
  		 horsey(i)+=horsey(13)-horsey0
       horsez(i)=getterrainheight(horsex(i),horsey(i))
       setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
  	   EndIf 
  	 Next
  EndIf
/'  changehorse=0:i=18
  horsex0=horsex(i):horsey0=horsey(i) 
  While horsex(i)<mx-disthorse :horsex(i)+=disthorse*2:changehorse=1:Wend 
  While horsex(i)>mx+disthorse :horsex(i)-=disthorse*2:changehorse=1:Wend 	
  While horsey(i)<my-disthorse :horsey(i)+=disthorse*2:changehorse=1:Wend 
  While horsey(i)>my+disthorse :horsey(i)-=disthorse*2:changehorse=1:Wend 
  If changehorse=1 Then
    horsez(i)=getterrainheight(horsex(i),horsey(i))
    setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
    For i=19 To 23 
    	If max(Abs(horsex(i)-mx),Abs(horsey(i)-my))>4000 Then 
  		 horsex(i)+=horsex(18)-horsex0
  		 horsey(i)+=horsey(18)-horsey0
       horsez(i)=getterrainheight(horsex(i),horsey(i))
       setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
  	   EndIf 
  	 Next
  EndIf
  changehorse=0:i=24
  horsex0=horsex(i):horsey0=horsey(i) 
  While horsex(i)<mx-disthorse :horsex(i)+=disthorse*2:changehorse=1:Wend 
  While horsex(i)>mx+disthorse :horsex(i)-=disthorse*2:changehorse=1:Wend 	
  While horsey(i)<my-disthorse :horsey(i)+=disthorse*2:changehorse=1:Wend 
  While horsey(i)>my+disthorse :horsey(i)-=disthorse*2:changehorse=1:Wend 
  If changehorse=1 Then
    horsez(i)=getterrainheight(horsex(i),horsey(i))
    setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
    For i=25 To 27
    	If max(Abs(horsex(i)-mx),Abs(horsey(i)-my))>4000 Then 
  		 horsex(i)+=horsex(24)-horsex0
  		 horsey(i)+=horsey(24)-horsey0
       horsez(i)=getterrainheight(horsex(i),horsey(i))
       setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
  	   EndIf 
  	 Next
  EndIf
  changehorse=0:i=28
  horsex0=horsex(i):horsey0=horsey(i) 
  While horsex(i)<mx-disthorse :horsex(i)+=disthorse*2:changehorse=1:Wend 
  While horsex(i)>mx+disthorse :horsex(i)-=disthorse*2:changehorse=1:Wend 	
  While horsey(i)<my-disthorse :horsey(i)+=disthorse*2:changehorse=1:Wend 
  While horsey(i)>my+disthorse :horsey(i)-=disthorse*2:changehorse=1:Wend 
  If changehorse=1 Then
    horsez(i)=getterrainheight(horsex(i),horsey(i))
    setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
    For i=29 To nhorse
    	If max(Abs(horsex(i)-mx),Abs(horsey(i)-my))>4000 Then 
  		 horsex(i)+=horsex(28)-horsex0
  		 horsey(i)+=horsey(28)-horsey0
       horsez(i)=getterrainheight(horsex(i),horsey(i))
       setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
  	   EndIf 
  	 Next
  EndIf '/
  For i=1 To nhorse
  	 If Abs(horsex(i)-mx-40*cos1)<40 Then
  	 	If Abs(horsey(i)-my-40*sin1)<40 Then
   	 If Abs(horsez(i)-mz)<100 And horsez(i)>(waterz) Then
     	   soundhorse
  	 		soundcrash
  	 		horsex(i)+=40*cos1
  	 		horsey(i)+=40*sin1
  	 		v=v-15
         horsez(i)=getterrainheight(horsex(i),horsey(i))
         setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))		
  	 	 EndIf 
  	 	EndIf
  	 EndIf
  Next
End Sub 
Sub testarbre
Dim dx As Single
    If plane=1 Then dx=18 Else dx=12
    If Abs(mx-mx0)>0.1 Or Abs(my-my0)>0.1 Then
      For i=1 To narbre2
  	     If Abs(arbrex(i)-mx)<dx Then
  	     	 If Abs(arbrey(i)-my)<dx Then
   	     	 If Abs(arbrez(i)-mz)<150 And arbrez(i)>waterz Then
               mx -= cos1 *(10)*kfps
               my -= sin1 *(10)*kfps
               If plane=1 Then testcrash
               Exit For
   	     	 EndIf 
  	     	 EndIf
  	     EndIf
      Next i
    EndIf 
    For i=1 To narbre2
    		If Rnd<3/(1+fps) Then arbrerot(i)=arbrerot0(i)+Rnd
    Next
    For i=1 To nbuisson
    		If Rnd<3/(1+fps) Then buissonrot(i)=buissonrot0(i)+Rnd
    Next
End Sub 
Sub subleftmouse
Dim As Single x,y,ix,i
x=guimousex:y=guimousey
If Sqr((x-radardx)*(x-radardx)+(y-radardy)*(y-radardy))<radarrx Then
	mx=distmax*(x-radardx)/radarrx
	my=distmax*(radardy-y)/radarrx
	mzsol0=getterrainheight(mx,my)+mzh
	mz=max(mz,mzsol0)
   If (plane=1 And car<=0 And prop>500) Or plane=2 Then
	  For ix=0 To 3000 Step 20
    	 mz=max(mz,getterrainheight(mx+ix*cos1,my+ix*sin1)+mzh+100)
	  Next
     o2=0:o3=0:v=max(v,vcruise)
   EndIf
   mz1=mz
   If plane=2 Then avionx=mx:aviony=my:avionz=mz
   If imap>=5 And myboat>0 Then
   	boatx(myboat)=mx+boatx(myboat)-mx0
   	boaty(myboat)=my+boaty(myboat)-my0
   	boatz(myboat)=getterrainheight(boatx(myboat),boaty(myboat)) 
   EndIf
   For i=4 To 6 'wingmen
     If max(Abs(airshipx(i)-mx0),Abs(airshipy(i)-my0))<30000 Then 
   	 airshipx(i)+=mx-mx0
   	 airshipy(i)+=my-my0
     EndIf 	
   Next
EndIf
If Abs(x-drawmapx)<50 And Abs(y-drawmapy)<50 And (imap>=5) Then
	mapx0=Int(x960*(x-drawmapx)/105)
	mapy0=Int(x960*(drawmapy-y)/105)
'If Abs(x-drawmapx)<100 And Abs(y-drawmapy)<100 Then
'	mapx0=Int(x960*(x-drawmapx)/200)
'	mapy0=Int(x960*(drawmapy-y)/200)
	dmx00=dmx0:dmy00=dmy0
	dmx0=scalex*(k8*512)*mapx0/x960
	dmy0=scalex*(k8*512)*mapy0/x960
   initterrain3
   If plane=1 And car<=0 And prop>500 Then
	  For ix=0 To 3000 Step 20
    	 mz=max(mz,getterrainheight(mx+ix*cos1,my+ix*sin1)+mzh+100)
	  Next
     o2=0:o3=0:v=max(v,vcruise)
   EndIf
   mz1=mz
EndIf
guisetfocus("win.graph")
End Sub
Sub initterrain3
Dim As Single x,y
   initterrain2
	mzsol0=getterrainheight(mx,my)+mzh
	mz=max(mz,mzsol0)
   For i=1 To narbre2
         			arbrex(i)+=dmx0-dmx00:arbrey(i)+=dmy0-dmy00
            	  	arbrez(i)=getterrainheight(arbrex(i),arbrey(i))
   Next
   For i=1 To nbuisson
         			buissonx(i)+=dmx0-dmx00:buissony(i)+=dmy0-dmy00
                  buissonz(i)=getterrainheight(buissonx(i),buissony(i))
   Next
   For i=1 To nhouse
         			housex(i)+=dmx0-dmx00:housey(i)+=dmy0-dmy00
         			housez(i)=getterrainheight(housex(i),housey(i))
   Next
   For i=1 To nhouse2
         			house2x(i)+=dmx0-dmx00:house2y(i)+=dmy0-dmy00
         			house2z(i)=getterrainheight(house2x(i),house2y(i))
   Next
   For i=1 To ndome
       domex(i)+=dmx0-dmx00:domey(i)+=dmy0-dmy00
       If dometype(i)=0 Then
     	  domez(i)=getterrainheight(domex(i),domey(i))
     	  If domez(i)>(waterz+0.01) Then domez(i)+=58.5 Else domez(i)=waterz-1
       Else
     	  domez(i)=getterrainheight(domex(i),domey(i))
     	  If domez(i)>(waterz+0.01) Then domez(i)+=68.5 Else domez(i)=waterz-1
       EndIf
   Next
   For i=1 To nhorse
         			horsex(i)+=dmx0-dmx00:horsey(i)+=dmy0-dmy00
         			horsez(i)=getterrainheight(horsex(i),horsey(i))
                  setnodeposition(md2_node(md2ihorse(i)),horsex(i),horsey(i),horsez(i))			
   Next
   For i=1 To ngrass
   	grassz(i)=waterz-1
   Next
   For i=1 To nboat2
     If i<>myboat Then 
       boatx(i)+=dmx0-dmx00
       boaty(i)+=dmy0-dmy00
     Else 
     	 boatx(i)=mx+boatx(i)-mx0
     	 boaty(i)=my+boaty(i)-my0
     EndIf 
     boatz(i)=getterrainheight(boatx(i),boaty(i))
   Next 
   For i=1 To nship
     If i<4 Or i>6 Or max(Abs(airshipx(i)-mx0),Abs(airshipy(i)-my0))>30000 Then 
     	 airshipx(i)+=dmx0-dmx00:airshipy(i)+=dmy0-dmy00
     Else 
   	 airshipx(i)+=mx-mx0
   	 airshipy(i)+=my-my0
     EndIf
   Next i
   For i=1 To ncloud
   	cloudx(i)+=mx-mx0
   	cloudy(i)+=my-my0
   	cloudz(i)=max(getterrainheight(cloudx(i),cloudy(i))+1000,cloudz(i))
   Next
   testairportarbres()
   initterrainlines()
End Sub
Sub testjoystick
    If GetJoystick(0,buttons,ax,ay,az,at)=0 Then
    	joystick=1
    Else 
      joystick=0
    	ax=-1000:ay=-1000:az=-1000:at=-1000
    	joy1dx=0:joy1dy=0:joy1dz=0:joy1dr=0
    	For i=1 To 16:b(i)=0:Next 
    EndIf    	
End Sub
Sub subjoystick 
Dim As Single vv=7
Dim As Integer order0
'If joystick=1 Then 
    If GetJoystick(0,buttons,ax,ay,az,at,,,hatx,haty)=0 Then
    	joystick=1
    	bb=buttons:For i=1 To 16:b(i)=0:Next 
    	b(b2(1))=bb And 1:b(b2(2))=bb And 2:b(b2(3))=bb And 4:b(b2(4))=bb And 8
    	b(b2(5))=bb And 16:b(b2(6))=bb And 32:b(b2(7))=bb And 64:b(b2(8))=bb And 128
    	b(b2(9))=bb And 256:b(b2(10))=bb And 512:b(b2(11))=bb And 1024:b(b2(12))=bb And 2048
      If ax>-100 Then joy1dx=(-ax)*32767 Else joy1dx=0
      If ay>-100 Then joy1dy=(ay)*32767 Else joy1dy=0
      If az>-100 Then joy1dz=(-az)*32767 Else joy1dz=0
      If at>-100 Then joy1dr=(-at)*32767 Else joy1dr=0    
    Else
    	joystick=0
    	ax=-1000:ay=-1000:az=-1000:at=-1000:hatx=-1000:haty=-1000
    	joy1dx=0:joy1dy=0:joy1dz=0:joy1dr=0
    	For i=1 To 16:b(i)=0:Next 
    EndIf
'EndIf 
If plane=0 Then 
   If joystick=1 Then 
    If abs(joy1dx)<4000 then joy1dx=0:ax=-1000
    If abs(joy1dy)<4000 then joy1dy=0:ay=-1000
    If abs(joy1dz)<4000 Then joy1dz=0:az=-1000
    If abs(joy1dr)<4000 Then joy1dr=0:at=-1000
    If ay>-100 Then 
    	If ay>0 Then ay=ay*0.5
      mx -= cos1*10*kfps*ay  
      my -= sin1*10*kfps*ay
    EndIf
    If at>-100 Then
      mx += sin1*3*kfps*at
      my -= cos1*3*kfps*at	
    EndIf
    If ax>-100 Then o1-=ax*4*kfps
    If az>-100 Then o2+=az*4*kfps
    if b(3)<>0 then keytir
    If b(7)<>0 Then keytir
   EndIf  

    If guitestkey(vk_numpad1) Then rot-=3*kfps
    If guitestkey(vk_numpad3) Then rot+=3*kfps
    If guitestkey(vk_numpad8) Then o2=min(89.0,o2+3*kfps)
    If guitestkey(vk_numpad2) Then o2=max(-89.0,o2-3*kfps)
    If guitestkey(vk_numpad5) Or b(10) Then o2=0
    If guitestkey(vk_left) Or guitestkey(vk_numpad1) Then o1+=3*kfps
    If guitestkey(vk_right) Or guitestkey(vk_numpad3) Then o1-=3*kfps
    If guitestkey(vk_up) Then mx+=vv*cos1*kfps:my+=vv*sin1*kfps
    If guitestkey(vk_down) Then mx-=0.5*vv*cos1*kfps:my-=0.5*vv*sin1*kfps
    If guitestkey(vk_SPACE)<>0 then keytir
    If guitestkey(vk_v)<>0 Then
	    If ikey0(vk_v)=0 Then
		    ikey0(vk_v)=1:viseur=(viseur+1)Mod 3
	    EndIf
    Else
      ikey0(vk_v)=0
    EndIf

    mz=getterrainheight(mx,my)+mzh
    mz1=max(mz1,mz)
    v=0

Else
	 keyplane 
	 'testtirs
    movecar
    setspeed
    setvol
    dirv=(mx-mx0)*cos1+(my-my0)*sin1
    If guitestkey(vk_r)<>0 Then
	   If piste=1 And Abs(v)<0.1 And boatwar(myboat)=0 Then 
	   	confirm("repare ?","repare",resp)
	   	If resp="yes" Then vie=vie0:carb=carb0
	   EndIf
    EndIf
    If (plane=1 And car<=0) Or plane=2  Then
      order0=order:order=0	
      If hatx=+1 Or guitestkey(vk_1)<>0 Then order=1:torder=time2+999999  
      If haty=-1 Or guitestkey(vk_2)<>0 Then order=2:torder=time2+999999  
      If haty=+1 Or guitestkey(vk_3)<>0 Then order=3:torder=time2+999999  
      If hatx=-1 Or guitestkey(vk_4)<>0 Then order=4:torder=time2+15
      If order<>0 Then
      	If order=1 Then soundorder1
      	If order=2 Then soundorder2
      	If order=3 Then soundorder3
      	If order=4 Then soundorder4
      	Sleep 200
      Else
      	order=order0
      EndIf
    EndIf  
EndIf
testpiste
If piste=1 Then mzsol0=max(mzsol0,pistez)':mz1=max(mz1,mz)
testmz
End Sub 
Sub testo123
    While o1>180:o1-=360:Wend
    While o1<-180:o1+=360:Wend
    While o2>180:o2-=360:Wend
    While o2<-180:o2+=360:Wend
    While o3>180:o3-=360:Wend
    While o3<-180:o3+=360:Wend
    If plane=0 Then  
      If o2<-89 Then o2=-89
      If o2>89 Then o2=89    
    Else
      'If o2>90 then o2=180-o2:o1=180+o1:o3=180+o3
      'If o2<-90 Then o2=-180-o2:o1=180+o1:o3=180+o3
      If o2>87 then o2=174-o2:o1=180+o1:o3=180+o3
      If o2<-87 Then o2=-174-o2:o1=180+o1:o3=180+o3
    EndIf 	
End Sub
Sub save(ByVal fic As String)
Dim As Integer i,File
file=freefile
Open fic For Output As #file
Print #file,mx
Print #file,my
Print #file,mz
Print #file,o1
Print #file,o2
Print #file,o3
Print #file,dmx0
Print #file,dmy0
Print #file,imap
Print #file,icar
Print #file,avionx
Print #file,aviony
Print #file,avionz
Print #file,aviono1
Print #file,aviono2
Print #file,aviono3
Print #file,avionz900
Print #file,volume
Print #file,v
For i=1 To nhorse
	Print #file,horsex(i)
	Print #file,horsey(i)
	Print #file,horsez(i)
	Print #file,horseo1(i)
	Print #file,horseo2(i)
Next
Print #file,"finlist"
For i=1 To nboat
	Print #file,boatx(i)
	Print #file,boaty(i)
	Print #file,boatz(i)
	Print #file,boato1(i)
	Print #file,boato2(i)
	Print #file,boato3(i)
	Print #file,vboat(i)
Next
Print #file,"finlist"
Print #file,myboat
Print #file,windo1
For i=1 To nship
	Print #file,airshipx(i)
	Print #file,airshipy(i)
	Print #file,airshipz(i)
	Print #file,airshipo1(i)
	Print #file,airshipo2(i)
	Print #file,airshipo3(i)
Next i
Print #file,"finlist"
Print #file,order
Close #file
End Sub
Sub load(ByVal fic As String)
Dim As Integer File,icar0,imap0,ihorse,iboat,iship
Dim As String ficin
icar0=icar:imap0=imap

If FileExists(fic) Then 

file=freefile
Open fic For input As #file
mx=0:my=0:mz=0
If Not Eof(file) Then Line Input #file,ficin:mx=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:my=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:mz=Val(ficin)
o1=0:o2=0:o3=0
If Not Eof(file) Then Line Input #file,ficin:o1=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:o2=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:o3=Val(ficin)
dmx0=0:dmy0=0
If Not Eof(file) Then Line Input #file,ficin:dmx0=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:dmy0=Val(ficin)
imap=1
If Not Eof(file) Then Line Input #file,ficin:imap=Val(ficin)
icar=1
If Not Eof(file) Then Line Input #file,ficin:icar=Val(ficin)
avionx=0:aviony=0:avionz=0
If Not Eof(file) Then Line Input #file,ficin:avionx=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:aviony=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:avionz=Val(ficin)
aviono1=0:aviono2=0:aviono3=0:setavion=1
If Not Eof(file) Then Line Input #file,ficin:aviono1=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:aviono2=Val(ficin)
If Not Eof(file) Then Line Input #file,ficin:aviono3=Val(ficin)
avionz900=1000
If Not Eof(file) Then Line Input #file,ficin:avionz900=Val(ficin)
volume=1000
If Not Eof(file) Then Line Input #file,ficin:volume=Val(ficin)
v=0
If Not Eof(file) Then Line Input #file,ficin:v=Val(ficin)
ihorse=0
While Not Eof(file) And ficin<>"finlist"
  ihorse=min2(nhorse,ihorse+1)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then horsex(ihorse)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then horsey(ihorse)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then horsez(ihorse)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then horseo1(ihorse)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then horseo2(ihorse)=Val(ficin)
  setnodeposition(md2_node(md2ihorse(ihorse)),horsex(ihorse),horsey(ihorse),horsez(ihorse))
  setnoderotation(md2_node(md2ihorse(ihorse)),0,horseo2(ihorse),horseo1(ihorse))	
Wend
ficin=""
iboat=0
While Not Eof(file) And ficin<>"finlist"
  iboat=min2(nboat,iboat+1)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then boatx(iboat)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then boaty(iboat)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then boatz(iboat)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then boato1(iboat)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then boato2(iboat)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then boato3(iboat)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then vboat(iboat)=Val(ficin)
Wend
ficin=""
myboat=0
If Not Eof(file) Then Line Input #file,ficin:myboat=Val(ficin)
windo1=Rnd*360
If Not Eof(file) Then Line Input #file,ficin:windo1=Val(ficin)
iship=0
For i=1 To nship
	'airshipx(i)=i*4500:airshipy(i)=i*4000:airshipz(i)=1500.0
	setairship(i)=1 
Next
While Not Eof(file) And ficin<>"finlist"
  iship=min2(nship,iship+1)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipx(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipy(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipz(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipo1(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipo2(iship)=Val(ficin)
  If Not Eof(file) And ficin<>"finlist" Then Line Input #file,ficin
  If ficin<>"finlist" Then airshipo3(iship)=Val(ficin)
Wend
ficin=""
order=1
If Not Eof(file) Then Line Input #file,ficin:order=Val(ficin)
Close #file

mx0=mx:my0=my:mz0=mz
dmx00=dmx0:dmy00=dmy0
mz1=mz:mz11=mz
mzsol0=getterrainheight(mx,my)+mzh
cos1=Cos(o1*degtorad):sin1=Sin(o1*degtorad)
cos2=Cos(o2*degtorad):sin2=Sin(o2*degtorad)
cos3=Cos(o3*degtorad):sin3=Sin(o3*degtorad)
dmx=cos1*cos2:dmy=sin1*cos2:dmz=sin2

If icar<>icar0 Then 
  selectcomboindex("win.combo1",icar)
  subcombo1
EndIf   
If imap<>imap0 Then
  selectcomboindex("win.map",imap-4)
  initterrain
  quit=1:restart=1
Else 
  If imap>=5 Then
  	  initterrain3	
  EndIf
EndIf

EndIf 
End Sub
Sub subsave
Dim As String fic,dir0
Dim As Integer ret 
dir0=CurDir  
fic=filedialog("save",CurDir+"\save\*.save")
fic=Trim(fic)
If InStr(fic,".")=0 And fic<>"" Then fic=fic+".save"
If Right(fic,5)=".save" Then 
	confirm("save in "+fic+" ?","confirm",resp)
	If resp="yes" Then
		save(fic)
	EndIf
EndIf
ret=ChDir(dir0)
End Sub
Sub subload
Dim As String fic,dir0
Dim As Integer ret 
dir0=CurDir  
fic=filedialog("load",CurDir+"\save\*.save")
fic=Trim(fic)
ret=ChDir(dir0)
If Right(fic,5)=".save" Then 
	If FileExists(fic) Then load(fic)
EndIf
ret=ChDir(dir0)
End Sub
Sub substart
	resp=Str(startoption)
	msg="start option : enter a number (0=reset  1=continue)  last="+resp
	prompt(msg,resp)
	i=Val(resp)
	i=max2(0,min2(1,i))
	If i<>startoption Then
		startoption=i
	EndIf
End Sub
Sub subwar
If war=0 Then 
	confirm("declare war ?","war",resp)
	If resp="yes" Then
		war=1
		For i=7 To nship
			airshipwar(i)=30000
		Next
	EndIf
Else 
	confirm("stop war ?","war",resp)
	If resp="yes" Then
		war=0
		For i=1 To nship
			airshipwar(i)=0
		Next
		For i=1 To nboat
			boatwar(i)=0
		Next
	EndIf
EndIf
End Sub
Sub subhelp
Dim As String msg,resp,cr=Chr(13)
msg="F1 => help"+cr
msg+="escape => quit"+cr
msg+="F9 => window/fullscreen"+cr
msg+="F3 => change time of day"+cr
msg+="J,K  => prev/next wheel/cockpit (show/hide)"+cr
msg+="P  => pause"+cr
msg+="T  => tourelle view (on/off)"+cr
msg+="pageup ,num+, N => accelere"+cr
msg+="pagedown ,num-, B => decelere"+cr
msg+="V  => visor     C => compas"+cr
msg+="space => shoot    control => brakes"+cr
msg+="R  => repare"+cr
msg+="M  => zoom map"+cr
msg+="W  => declare war"+cr
msg+="1,2,3,4 => give wingmen orders"+cr
msg+="ctrl+T => number of trees"+cr
msg+="ctrl+D => distmax scale"+cr
msg+="ctrl+N => number of planes"+cr
msg+="ctrl+F => max fps"+cr
msg+="ctrl+C => number of clouds"+cr
msg+="ctrl+S => smooth terrain"+cr
msg+="ctrl+W => water wave height"+cr
msg+="ctrl+B => number of boats"+cr
msg+="5,num7 / 6,num9  => turn boat left / right"+cr
msg+="ctrl+R => reset"+cr 
msg+="ctrl+O => start option (reset/continue)"+cr
msg+="ctrl+L => select level (easy,normal,hard)"+cr
msg+=cr+"joystick and gamepad supported"+cr 
confirm(msg,"help",resp)
End Sub
Sub main
End Sub
Dim As String filename4="objects/horse.md2"
Dim As String fic
Dim As Single x,y,z,tfps,dist
Dim As uint wx,wy,winx,winy,init=0
Randomize(imap) 
time2=Timer:time3=time2-0.01:tfps=0.01:kfps=1

guibackgroundcolor(100,255,150)

i=13
fic=Dir("addon/*.bin")
fic=Right(fic,Len(fic)-InStrRev(fic,"/"))
addon(i)=Left(fic,Len(fic)-4)
While i<100 And addon(i)<>""
  	 i+=1
    fic=Dir()
    fic=Right(fic,Len(fic)-InStrRev(fic,"/"))
 	 addon(i)=Left(fic,Len(fic)-4)
Wend
initterrain()
plane=1:car=1
initavion
initairship
initcloud 
mz=max(mz,getterrainheight(mx,my)+mzh)
mz11=-999999

        debut:
        restart=0:md2nnode=0
        arbrelist=0:buissonlist=0:house1list=0:house2list=0
        carlist=0:ciellist=0:For i=0 To 4:boatlist(i)=0:Next
        voilelist=0:voile2list=0:avionlist=0:corsair0list=0:corsairlist=0:vg33list=0:zerolist=0
        b25list=0:alphajetlist=0:alphajet0list=0:vg330list=0:spitfirelist=0:spitfire0list=0
        bf109list=0:bf1090list=0:p51dlist=0:p51d0list=0:f14list=0:f140list=0:zero0list=0
        soltexture=0:cartext=0:cieltext=0:maptexture=0
        If fullscreen=0 Then
        	  ScreenInfo(wx,wy)
           xmax=min2(wx-30,750-30):ymax=min2(wy-129,470)
           'xmax=min2(wx-30,800-30):ymax=min2(wy-129,600-100)
           winx=max2(0,wx/2-(xmax+30)/2):winy=max2(0,wy/2-(ymax+129)/2)
        Else
           ScreenInfo(wx,wy)
           'wx=800:wy=600
           xmax=wx-30:ymax=wy-129
           winx=0:winy=0
        EndIf    
        button("win.test","quit",@subquit,10,ymax+20,72,30) 
        button("win.pause","pause",@subpause,251,ymax+20,50,30) 
        button("win.help","help",@subhelp,310,ymax+24,45,24) 
        button("win.load","load",@subload,655,ymax+24,40,19) 
        button("win.save","save",@subsave,703,ymax+24,40,19) 
        combobox("win.sol",@subsol,172,ymax+24,70,200)
        combobox("win.map",@submap,99,ymax+24,61,500)
        combobox("win.combo1",@subcombo1,368,ymax+24,77,400)
        combobox("win.ciel",@subciel,455,ymax+24,58,400)
        'combobox("win.grass",@subgrass,522,ymax+24,67,400)
        combobox("win.fog",@subfog,522,ymax+24,60,400)
        combobox("win.mapsol",@submapsol,589,ymax+24,60,400)
        graphicbox("win.graph",10,10,xmax,ymax,"opengl")
        'openwindow("win","my window",100,70,xmax+30,ymax+100)
        openwindow("win","flightcombat_chung",winx,winy,xmax+30,ymax+100)
        guistartOpenGL("win.graph")
        trapclose("win",@subquit)
        addcombo("win.combo1","foot")
        addcombo("win.combo1","car")
        addcombo("win.combo1","car2")
        addcombo("win.combo1","car3")
        addcombo("win.combo1","vg33")
        addcombo("win.combo1","corsair")
        addcombo("win.combo1","spitfire")
        addcombo("win.combo1","spitfire2")
        addcombo("win.combo1","bf109")
        addcombo("win.combo1","p51d")
        addcombo("win.combo1","zero")
        addcombo("win.combo1","zero2")
        addcombo("win.combo1","jet")
        addcombo("win.combo1","f14")
        addcombo("win.combo1","tour")
        selectcomboindex("win.combo1",icar)
        subcombo1
        'addcombo("win.map","map1")
        'addcombo("win.map","map2")
        'addcombo("win.map","map3")
        'addcombo("win.map","map4")
        addcombo("win.map","france")
        addcombo("win.map","england")
        addcombo("win.map","yougo")
        addcombo("win.map","grece")
        addcombo("win.map","israel")
        addcombo("win.map","japan")
        addcombo("win.map","vietnam")
        addcombo("win.map","hawai")
        For i=13 To 100
        	If addon(i)<>"" Then addcombo("win.map",addon(i)) Else Exit For 
        Next
        selectcomboindex("win.map",imap-4)
        addcombo("win.sol","sol")
        addcombo("win.sol","herbe")
        addcombo("win.sol","herbe2")
        addcombo("win.sol","desert2")
        addcombo("win.sol","desert")
        selectcomboindex("win.sol",isol)
        subsol
        addcombo("win.mapsol","none")
        addcombo("win.mapsol","map0")
        addcombo("win.mapsol","map1")
        addcombo("win.mapsol","map2")
        addcombo("win.mapsol","map3")
        addcombo("win.mapsol","map4")
        selectcomboindex("win.mapsol",imapsol)
        submapsol
        addcombo("win.ciel","ciel")
        addcombo("win.ciel","ciel0")
        addcombo("win.ciel","ciel1")
        addcombo("win.ciel","ciel2")
        addcombo("win.ciel","ciel3")
        addcombo("win.ciel","ciel4")
        addcombo("win.ciel","ciel5")
        addcombo("win.ciel","ciel6")
        selectcomboindex("win.ciel",iciel)
        subciel
        'addcombo("win.grass","nograss")
        'addcombo("win.grass","grass1")
        'addcombo("win.grass","grass2")
        'selectcomboindex("win.grass",igrass)
        igrass=2
        For i=0 To 15
        	addcombo("win.fog","fog"+Str(i))
        Next
        selectcomboindex("win.fog",ifog+1)
        trapLeftmouse("win.graph",@subleftmouse)
        guisetfocus("win.graph")
        
        initgl
        glclearcolor 0,0.7,0, 0.0
        glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
        i=xmax*0.35:j=ymax*0.4
        glcolor3f(0,0,0.3)
        gldrawtext("Flightcombat_chung",i,j,3)
        glcolor3f(1,1,1)
        gldrawtext("a program by NGUYEN.Chung",i+40,j+70,1.5)
        guirefreshopenGL()

        tronctext=guiloadtexture("media/tronc.jpg")
        volanttext=guiloadtexture("objects/volant.jpg",252)
        startext=guiloadtexture("media/stars.jpg")
        sunsettext=guiloadtexture("media/sun.jpg")
        helicetext=guiloadtexture("objects/helice.jpg",40,47)
        cockpittext1=guiloadtexture("media/cockpit1.jpg",245)'252)
        cockpittext2=guiloadtexture("media/cockpit2.jpg",237)'252)
        cockpittext3=guiloadtexture("media/cockpit3.jpg",237)'252)
        cockpittext4=guiloadtexture("media/cockpit4.jpg",237)'252)
        feutext=guiloadtexture("media/feu.jpg")
        smoketext=guiloadtexture("media/smoke.bmp",55)
        firetext=guiloadtexture("media/fire.jpg",20,175)
        sfiretext=guiloadtexture("media/smoke.bmp")
        boattext=guiloadtexture("objects/ship.jpg")
        boattext2=guiloadtexture("objects/sailship.jpg")
        boattext3=guiloadtexture("objects/_vessel.jpg")
        boattext4=guiloadtexture("objects/shipcarrier.jpg")
        windtext=guiloadtexture("media/vent.jpg",253)
        radartext=guiloadtexture("media/radar.jpg",253)
        pistetext=guiloadtexture("media/piste.jpg")
        If imap>=5 Then
        	maptext=initmaptexture()
        EndIf
        'initcar
        initarbres
        inithouses
        initdomes
        initairport
        inittown
        initterrainlines
        initgrass
        setfog
        md2horse=loadmd2model(filename4,0,0,-2)

        Randomize()        
        inithorses
        If startoption=1 And init=0 Then load("save/startup.save0")
        mzsol0=getterrainheight(mx,my)+mzh'25
 	     mz=max(mz,mzsol0):mz1=mz:mz11=mzsol0
        init=1
        
        time2=Timer:time3=time2-0.01
        
        While quit=0 And guitestkey(vk_escape)=0
         	guiscan
        	   If guitestkey(vk_f9)<>0 Then subscreen
        	   If guitestkey(vk_f1)<>0 Then subhelp
        	   If guitestkey(vk_p)<>0 Then notice("pause","pause"):time2=Timer:time3=time2-0.01
        	   If guitestkey(vk_m)<>0 And imap>=5 Then
        	   	mapdisplay=(mapdisplay+1)Mod 3:Sleep 200:guiscan 
        	   EndIf
        	   If guitestkey(vk_t)<>0 And guitestkey(vk_control)<>0 Then subnarbre
        	   If guitestkey(vk_d)<>0 And guitestkey(vk_control)<>0 Then subdistscale
        	   If guitestkey(vk_r)<>0 And guitestkey(vk_control)<>0 Then subreset
        	   If guitestkey(vk_f)<>0 And guitestkey(vk_control)<>0 Then submaxfps
        	   If guitestkey(vk_s)<>0 And guitestkey(vk_control)<>0 Then subsmooth
        	   If guitestkey(vk_w)<>0 And guitestkey(vk_control)<>0 Then subwave
        	   If guitestkey(vk_b)<>0 And guitestkey(vk_control)<>0 Then subnboat
        	   If guitestkey(vk_o)<>0 And guitestkey(vk_control)<>0 Then substart
        	   If guitestkey(vk_c)<>0 And guitestkey(vk_control)<>0 Then subncloud
        	   If guitestkey(vk_n)<>0 And guitestkey(vk_control)<>0 Then subnship
       	   If guitestkey(vk_l)<>0 And guitestkey(vk_control)<>0 Then sublevel
        	   If guitestkey(vk_w)<>0 Then subwar
         	mx0=mx:my0=my:mz0=mz
         	distmax=scalex*256
         	If Sqr(mx*mx+my*my)>distmax Then
         	 mx=-mx*0.98
         	 my=-my*0.98
			    mx=mx0+Int(mx-mx0):my=my0+Int(my-my0)
         	 If imap>=5 Then 
         	 	dmx00=dmx0+(mx0-mx)*2:dmy00=dmy0+(my0-my)*2
         		dmx0-=mx-mx0:dmy0-=my-my0
         		initterrain3
         		mx0=mx:my0=my:mz0=mz 
         	 EndIf 
             mzsol0=getterrainheight(mx,my)+mzh
             mz=max(mz,mzsol0)
			    If plane=2 Then avionx=mx:aviony=my:avionz=mz
         	EndIf
         	subjoystick()
            testo123
         	cos1=Cos(o1*degtorad):sin1=Sin(o1*degtorad)
         	cos2=Cos(o2*degtorad):sin2=Sin(o2*degtorad)
         	cos3=Cos(o3*degtorad):sin3=Sin(o3*degtorad)
            dmx=cos1*cos2:dmy=sin1*cos2:dmz=sin2
           	testarbre()
           	display()
           	'For i=1 To md2nnode
           	'	If md2_node(i)->anim=md2_random And Rnd<0.7/(0.1+fps) Then
           	'		If pause=0 Then setanim(md2_node(i),md2_random)
           	'	EndIf
           	'Next
           	guirefreshopenGL() 
           	time3=time2
           	time2=Timer
           	dtime2=1.0/max(10.0,min(200.0,maxfps))-0.0015
           	'If time2<(time3+25) Then Sleep (time3+30-time2)'20'30
           	If time2<(time3+dtime2) Then
           		If time2<time3 Then time2=time3 'if midnight
           		Sleep Int((time3+dtime2+0.001-time2)*1000)'20'30
           	   time2=time3+dtime2+0.001
           	EndIf
           	tfps=0.85*tfps+0.15*min(1.0,(time2-time3))
           	fps=1.0/max(0.001,tfps)
           	kfps=min(10.0,30/fps)
           	kfps2=min(2.0,25/fps) 	
        Wend
        
        guicloseOpenGL()
        deleteMD2model(md2horse)
        For i=1 To md2nnode
        	 If md2_node(i)<>0 Then
        	 	DeAllocate(md2_node(i))
        	 	md2_node(i)=NULL 
        	 EndIf
        Next

guiclose
If restart=1 Then restart=0:quit=0:guireset:GoTo debut
closesounds
If startoption=1 Then save("save/startup.save0")
file=freefile
Open ficini For Output As #file
Print #file,icar
Print #file,imap
Print #file,isol
Print #file,iciel
Print #file,to1
Print #file,to2
Print #file,igrass
Print #file,viseur
Print #file,narbre2
Print #file,distscale
Print #file,fullscreen
Print #file,maxfps
Print #file,ismooth
Print #file,kwave
Print #file,nboat2
Print #file,startoption
Print #file,volant
Print #file,mx
Print #file,my
Print #file,mz
Print #file,o1
Print #file,o2
Print #file,o3
For i=1 To nship
	Print #file,airshipx(i)
	Print #file,airshipy(i)
	Print #file,airshipz(i)
	Print #file,airshipo1(i)
	Print #file,airshipo2(i)
	Print #file,airshipo3(i)
Next i
Print #file,"finlist"
Print #file,order
Print #file,ncloud2
Print #file,ifog
Print #file,nship2
Print #file,level
Print #file,carb
Print #file,imapsol
Print #file,compas
Close #file
If townchanged=1 Or Rnd<0.2 Then savetownvie()
guiquit 
End 









