sync on
sync rate 0
autocam off
color backdrop 0
set text font "arial"
set text size 16
 
global dscreenheight#
dscreenheight#=12
global dscreenwidth#
dscreenwidth#=20
global l1depth
l1depth=-140
global l2depth
l2depth=-120
global l3depth
l3depth=-100
global lwidth
lwidth=240
global lheight
lheight=180
global textheight
textheight=text height(" ")
 
global gretx#
global grety#
 
stationgrey= rgb(150,150,150)
orange=rgb(255,128,0)
 
//load object "ltransport.3ds",1
//yrotate object 1,-90
//fix object pivot 1
 
shiptest=0
 //1,0.3  16.699
tempmesh=findmesh()
tempobj=findobj()
make object triangle tempobj,1,0,0,0,1,0,0,0,0
make mesh from object tempmesh,tempobj
lock vertexdata for mesh tempmesh
delete mesh from vertexdata 0,get vertexdata vertex count(),0,get vertexdata index count()
addplain(-1.5,0.5,-1,-1.5,0.5,1,1,0.5,1,1,0.5,-1)
setlastquad(0,1,0)
addplain(2,0.2,0.7,2,0.2,-0.7,1,0.5,-1,1,0.5,1)
setlastquad(sin(16.699),cos(16.699),0)
addplain(2,0.2,-0.7,2,0.2,0.7,2,-0.5,0.7,2,-0.5,-0.7)
setlastquad(1,0,0)
addplain(2,-0.5,0.7,2,-0.5,-0.7,1,-0.7,-1,1,-0.7,1)
setlastquad(sin(16.699),-cos(16.699),0)
addplain(-1.5,-0.7,1,-1.5,-0.7,-1,1,-0.7,-1,1,-0.7,1)
setlastquad(0,-1,0)
addplain(-1.5,-0.7,-1,-1.5,-0.7,1,-1.5,0.5,1,-1.5,0.5,-1)
setlastquad(-1,0,0)
addplain(-1.5,-0.7,-1,-1.5,0.5,-1,1,0.5,-1,1,-0.7,-1)
setlastquad(0,0,-1)
addplain(1,-0.7,-1,1,0.5,-1,2,0.2,-0.7,2,-0.5,-0.7)
setlastquad(sin(16.699),0,-cos(16.699))
addplain(-1.5,0.5,1,-1.5,-0.7,1,1,-0.7,1,1,0.5,1)
setlastquad(0,0,1)
addplain(1,0.5,1,1,-0.7,1,2,-0.5,0.7,2,0.2,0.7)
setlastquad(sin(16.699),0,cos(16.699))
unlock vertexdata
delete object tempobj
make object 1,tempmesh,0 
delete mesh tempmesh 
 
stransportobj=findobj()
 
 
autocannonobj=findobj()
make object box autocannonobj,0.5,0.1,0.1
hide object autocannonobj
 
set image colorkey 0,0,0
ink 0,0
box 0,0,800,600
ink rgb(255,255,255),0
for count=1 to 200
  x=rnd(798)+1
  y=rnd(598)+1
  dot x,y
  dot x+1,y
  dot x,y+1
  dot x+1,y+1
next count
get image 1,0,0,800,600,1
 
ink 0,0
box 0,0,800,600
ink rgb(255,255,255),0
for count=1 to 100
  x=rnd(798)+1
  y=rnd(598)+1
  dot x,y
  dot x,y+1
  dot x,y-1
  dot x-1,y
  dot x+1,y
next count
get image 2,0,0,800,600,1
 
ink 0,0
box 0,0,800,600
ink rgb(255,255,255),0
for count=1 to 50
  x=rnd(798)+1
  y=rnd(598)+1
  dot x,y
  dot x,y+1
  dot x,y-1
  dot x-1,y
  dot x+1,y
next count
get image 3,0,0,800,600,1
 
ink 0,0
box 0,0,800,600
ink rgb(255,128,0),0
  dot 1,1
  dot 1,0
  dot 0,1
  dot 2,1
  dot 1,2
  //fillcircle(1,1,2)
get image 4,0,0,3,3,1
 
dim backdropobjs(3,9)
 
for county=0 to 2
  for countx=1 to 3
    obj=findobj()
    backdropobjs(1,(county*3)+countx)=obj
    make object plain obj,lwidth,lheight
    texture object obj,1
    position object obj,(countx-2)*lwidth,l1depth,(county-1)*lheight
    xrotate object obj,90
    set alpha mapping on obj,100
  next countx
next county
 
for county=0 to 2
  for countx=1 to 3
    obj=findobj()
    backdropobjs(2,(county*3)+countx)=obj
    make object plain obj,lwidth,lheight
    texture object obj,2
    position object obj,(countx-2)*lwidth,l2depth,(county-1)*lheight
    xrotate object obj,90
    set alpha mapping on obj,100
  next countx
next county
 
for county=0 to 2
  for countx=1 to 3
    obj=findobj()
    backdropobjs(3,(county*3)+countx)=obj
    make object plain obj,lwidth,lheight
    texture object obj,3
    position object obj,(countx-2)*lwidth,l3depth,(county-1)*lheight
    xrotate object obj,90
    set alpha mapping on obj,100
  next countx
next county
 
 
//---stations
 
//images
//5:dock
//6:plain grey
 
global scsmalldock as integer
global sclargedock as integer
global sclargetube as integer
global scbigtube as integer
global scshortbigtube as integer
 
ink stationgrey,0
box 0,0,800,600
ink orange,0
for count=-6 to 80 step 6
  line count,0,count+6,6
  line count+1,0,count+7,6
  line count+2,0,count+8,6
next count
get image 5,0,0,78,50
 
ink stationgrey,0
box 0,0,1,1
get image 6,0,0,1,1
 
ink rgb(35,20,5),0
box 0,0,1,1
get image 14,0,0,1,1
 
ink 0,0
box 0,0,1,1
get image 7,0,0,1,1
ink 0,0
box 0,0,800,600
ink rgb(20,40,80),0
box 0,screen height()-200,195,screen height()-195
box 195,screen height()-195,200,screen height()
fillcircle(193,screen height()-194,6)
ink rgb(20,20,30),0
box 0,screen height()-195,191,screen height()-190
box 191,screen height()-190,196,screen height()
fillcircle(189,screen height()-189,6)
box 0,screen height()-190,191,screen height()
 
get image 8,0,screen height()-200,200,screen height(),1
 
 
ink 0,0
box 0,0,screen width(),screen height()
ink rgb(20,40,80),0
box screen width()-195,0,screen width(),5
box screen width()-199,5,screen width()-194,screen height()
fillcircle(screen width()-193,6,6)
ink rgb(20,20,30),0
box screen width()-191,5,screen width(),10
box screen width()-195,10,screen width()-190,screen height()
fillcircle(screen width()-189,11,6)
box screen width()-191,10,screen width(),screen height()
ink 0,0
box screen width()-191,screen height()-200,screen width(),screen height()
 
get image 12,screen width()-200,0,screen width(),screen height(),1
get image 17,screen width()-200,0,screen width()-45,200,1
 
ink 0,0
box 0,0,200,200
ink rgb(20,40,80),0
box 5,0,95,30
box 0,5,100,25
fillcircle(6,6,6)
fillcircle(93,6,6)
fillcircle(6,23,6)
fillcircle(93,23,6)
get image 9,0,0,100,30
 
ink 0,0
box 0,0,200,200
ink rgb(20,20,30),0
box 10,5,90,25
box 5,10,95,20
fillcircle(11,11,6)
fillcircle(88,11,6)
fillcircle(11,18,6)
fillcircle(88,18,6)
get image 10,0,0,100,30
 
ink 0,0
box 0,0,800,600
ink rgb(30,30,110),0
fillcircle(4,4,5)
ink rgb(70,70,200),0
fillcircle(4,4,3)
ink rgb(140,140,255),0
fillcircle(4,4,2)
get image 11,0,0,10,10,1
 
ink 0,0
box 0,0,200,200
ink rgb(20,40,80),0
box 5,0,180,30
box 0,5,185,25
fillcircle(6,6,6)
fillcircle(178,6,6)
fillcircle(6,23,6)
fillcircle(178,23,6)
get image 13,0,0,185,30
 
ink 0,0
box 0,0,800,600
ink orange,0
box 0,2,7,9
fillcircle(7,5,3)
get image 15,0,0,10,10,1
 
ink 0,0
box 0,0,800,600
ink rgb(50,50,50),0
fillcircle(4,4,5)
ink rgb(150,150,150),0
fillcircle(4,4,3)
ink rgb(255,255,255),0
fillcircle(4,4,2)
get image 16,0,0,10,10,1
 
 
tempobj=findobj()
makefullcylinder(tempobj,1)
position object tempobj,0,0,0
zrotate object tempobj,90
xrotate object tempobj,180
fix object pivot tempobj
scale object tempobj,100,200,100
tempmesh=findmesh()
make mesh from object tempmesh,tempobj
delete object tempobj
make object tempobj,tempmesh,5
delete mesh tempmesh
scsmalldock=tempobj
hide object tempobj
 
tempobj=findobj()
makefullcylinder(tempobj,2)
position object tempobj,0,0,0
zrotate object tempobj,90
xrotate object tempobj,180
fix object pivot tempobj
scale object tempobj,100,200,100
tempmesh=findmesh()
make mesh from object tempmesh,tempobj
delete object tempobj
make object tempobj,tempmesh,5
delete mesh tempmesh
sclargedock=tempobj
hide object tempobj
 
tempobj=findobj()
makefullcylinder(tempobj,2)
position object tempobj,0,0,0
zrotate object tempobj,90
xrotate object tempobj,180
fix object pivot tempobj
scale object tempobj,100,200,100
tempmesh=findmesh()
make mesh from object tempmesh,tempobj
delete object tempobj
make object tempobj,tempmesh,6
delete mesh tempmesh
sclargetube=tempobj
hide object tempobj
 
tempobj=findobj()
makefullcylinder(tempobj,3)
position object tempobj,0,0,0
zrotate object tempobj,90
xrotate object tempobj,180
fix object pivot tempobj
scale object tempobj,100,200,100
tempmesh=findmesh()
make mesh from object tempmesh,tempobj
delete object tempobj
make object tempobj,tempmesh,6
delete mesh tempmesh
scbigtube=tempobj
hide object tempobj
 
tempobj=findobj()
makefullcylinder(tempobj,3)
position object tempobj,0,0,0
zrotate object tempobj,90
xrotate object tempobj,180
fix object pivot tempobj
scale object tempobj,100,100,100
tempmesh=findmesh()
make mesh from object tempmesh,tempobj
delete object tempobj
make object tempobj,tempmesh,6
delete mesh tempmesh
scshortbigtube=tempobj
hide object tempobj
 
 
type station
  posx# as float
  posy# as float
  pickobj as integer
  left# as float
  top# as float
  right# as float
  bottom# as float
  components as integer
  docks as integer
  name$ as string
  pcommoditiescount as integer
  pweaponscount as integer
  pequipmentcount as integer
  pshipscount as integer
endtype
 
type component
  left# as float
  top# as float
  right# as float
  bottom# as float
  posx# as float
  posy# as float
  obj as integer
  coldistance# as float
endtype
 
type dock
  docktype as integer
  x# as float
  y# as float
  angle# as float
endtype
 
type pcommodity
  etype as integer
  price as integer
  name$ as string
endtype
 
type weapon
  bullettype as integer
  price as integer
  name$ as string
  reloadtime# as float
  damage as float
  imagenum as integer
  spread# as float
  initvelocity# as float
  life# as float
  size# as float
  obj as integer
endtype
 
type pequipment
  etype as integer
  price as integer
  name$ as string
endtype
 
type pship
  etype as integer
  price as integer
  name$ as string
endtype
 
dim stations(0) as station
dim stationcomponents(1,1) as component
dim stationdocks#(1,1) as dock
dim stationpcommodities(0,0) as integer
dim stationpweapons(0,0) as integer
dim stationpequipment(0,0) as integer
dim stationpships(0,0) as integer
dim pcommodities(5) as pcommodity
dim weapons(5) as weapon
dim pequipment(5) as pequipment
dim pships(5) as pship
 
pcommodities(1).etype=1
pcommodities(1).price=20
pcommodities(1).name$="Iron"
pcommodities(2).etype=1
pcommodities(2).price=50
pcommodities(2).name$="Water"
pcommodities(3).etype=1
pcommodities(3).price=30
pcommodities(3).name$="Nickel"
 
//bullettypes
//1-energy pulse
//2-projectile
//3-powered projectile (missile)
 
weapons(1).bullettype=1
weapons(1).price=200
weapons(1).name$="Photon Pulse Cannon"
weapons(1).reloadtime#=0.2
weapons(1).imagenum=16
weapons(1).initvelocity#=15
weapons(1).life#=4
weapons(1).size#=0.5
weapons(1).damage=20
weapons(1).obj=autocannonobj
 
weapons(2).bullettype=3
weapons(2).price=2000
weapons(2).name$="Missile Launcher"
weapons(2).reloadtime#=1.05
weapons(2).imagenum=15
weapons(2).initvelocity#=5
weapons(2).life#=5
weapons(2).size#=0.5
weapons(2).damage=200
weapons(2).obj=autocannonobj
 
weapons(3).bullettype=2
weapons(3).price=1500
weapons(3).name$="150mm Vulcan"+chr$(13)+chr$(10)+"Autocannon "
weapons(3).reloadtime#=0.05
weapons(3).imagenum=15
weapons(3).spread#=15
weapons(3).initvelocity#=20
weapons(3).life#=3
weapons(3).damage=5
weapons(3).size#=0.2
weapons(3).obj=autocannonobj
 
pequipment(1).etype=1
pequipment(1).price=2000
pequipment(1).name$="Ore Refinement"+chr$(13)+chr$(10)+"Module "
pequipment(2).etype=1
pequipment(2).price=1500
pequipment(2).name$="Cargo Module"
 
global maxcomponents as integer
global maxdocks as integer
maxpcommodities=0
maxpweapons=0
maxpequipment=0
maxpships=0
 
read readcount
for count=1 to readcount
  read readname$
  read readx#
  read ready#
  addstation(readname$,readx#,ready#)
  read readcountb
  for countb=1 to readcountb
    read readcomponent
    read readx#
    read ready#
    read readrot#
    addstationcomponent(count,readcomponent,readx#,ready#,readrot#)
  next countb
  read readnum
  for countb=1 to readnum
    read readid
    stations(count).pcommoditiescount=stations(count).pcommoditiescount+1
    if stations(count).pcommoditiescount>maxpcommodities then maxpcommodities=stations(count).pcommoditiescount
    dim stationpcommodities(array count(stations(0))+1,maxpcommodities+1)
    stationpcommodities(array count(stations(0)),stations(count).pcommoditiescount)=readid
  next countb
  read readnum
  for countb=1 to readnum
    read readid
    stations(count).pweaponscount=stations(count).pweaponscount+1
    if stations(count).pweaponscount>maxpweapons then maxpweapons=stations(count).pweaponscount
    dim stationpweapons(array count(stations(0))+1,maxpweapons+1)
    stationpweapons(array count(stations(0)),stations(count).pweaponscount)=readid
  next countb
  read readnum
  for countb=1 to readnum
    read readid
    stations(count).pequipmentcount=stations(count).pequipmentcount+1
    if stations(count).pequipmentcount>maxpequipment then maxpequipment=stations(count).pequipmentcount
    dim stationpequipment(array count(stations(0))+1,maxpequipment+1)
    stationpequipment(array count(stations(0)),stations(count).pequipmentcount)=readid
  next countb
  read readnum
  for countb=1 to readnum
    read readid
    stations(count).pshipscount=stations(count).pshipscount+1
    if stations(count).pshipscount>maxpships then maxpships=stations(count).pshipscount
    dim stationpships(array count(stations(0))+1,maxpships+1)
    stationpships(array count(stations(0)),stations(count).pshipscount)=readid
  next countb
next count
 
type shipstats
  topspeed# as float
  turnspeed# as float
  acceleration# as float
  dockx# as float
  docky# as float
  dockrot# as float
  coldistance# as float
  weapons as integer
  monscale# as float
endtype
 
dim shiptypes (1) as shipstats
shiptypes(1).topspeed#=20
shiptypes(1).turnspeed#=90
shiptypes(1).acceleration#=0.8
shiptypes(1).dockx#=0
shiptypes(1).docky#=1
shiptypes(1).dockrot#=-90
shiptypes(1).coldistance#=3
shiptypes(1).weapons=2
shiptypes(1).monscale#=1
 
type colpoint
  x# as float
  y# as float
endtype
 
dim shipcolpoints(1,4) as colpoint
shipcolpoints(1,1).x#=-1.5
shipcolpoints(1,1).y#=1
shipcolpoints(1,2).x#=-1.5
shipcolpoints(1,2).y#=-1
shipcolpoints(1,3).x#=2
shipcolpoints(1,3).y#=1
shipcolpoints(1,4).x#=2
shipcolpoints(1,4).y#=-1
 
type ship
  posx# as float
  posy# as float
  velx# as float
  vely# as float
  angle# as float
  throttle# as float
  obj as integer
  shiptype as integer
  permission as integer
  docking as integer
  docknum as integer
  targetx# as float
  targety# as float
  targetrot# as float
  docked as integer
  launching as integer
  credits as integer
endtype
 
dim ships (1) as ship
ships(1).obj = 1
ships(1).shiptype=1
ships(1).permission=0
ships(1).docking=0
ships(1).docked=0
ships(1).launching=0
ships(1).credits=200
 
type shipweapon
  weapontype as integer
  ammunition as integer
  posx# as float
  posy# as float
  posz# as float
  reload# as float
  monobj as integer
endtype
 
dim shipweapons(1,5) as shipweapon
shipweapons(1,1).posx#=1.2
shipweapons(1,1).posy#=-0.2
shipweapons(1,1).posz#=0.8
shipweapons(1,2).posx#=1.2
shipweapons(1,2).posy#=-0.2
shipweapons(1,2).posz#=-0.8
 
for count=1 to 5
  shipweapons(1,count).monobj=findobj()
  make object cube shipweapons(1,count).monobj,0.3
  hide object shipweapons(1,count).monobj
  lock object on shipweapons(1,count).monobj
  set object cull shipweapons(1,count).monobj,0
  set object ambience shipweapons(1,count).monobj,rgb(20,40,80)
  set object diffuse shipweapons(1,count).monobj,rgb(20,40,80)
  set object emissive shipweapons(1,count).monobj,rgb(10,20,40)
next count
 
rotate camera 90,0,0
position camera 0,40,0
poscamera(0,0)
oldtime=timer()
 
type particle
  velx# as float
  vely# as float
  velz# as float
  posx# as float
  posy# as float
  posz# as float
  speed# as float
  obj as integer
endtype
 
flameobj=findobj()
make object plain flameobj,0.2,0.2
texture object flameobj, 4
 
dim particles(0) as particle 
 
dim selobjs(8)
 
selobjs(1)=findobj()
make object triangle selobjs(1),0,0,0,0,0,0,0,0,0
 
tempobj=findobj()
make object box tempobj,1.5,0.1,0.1
tempmesh=findmesh()
make mesh from object tempmesh,tempobj
add limb selobjs(1),1,tempmesh
offset limb selobjs(1),1,0.75,0,0
delete mesh tempmesh
delete object tempobj
 
tempobj=findobj()
make object box tempobj,0.1,1.5,0.1
tempmesh=findmesh()
make mesh from object tempmesh,tempobj
add limb selobjs(1),2,tempmesh
offset limb selobjs(1),2,0,0.75,0
delete mesh tempmesh
delete object tempobj
 
tempobj=findobj()
make object box tempobj,0.1,0.1,1.5
tempmesh=findmesh()
make mesh from object tempmesh,tempobj
add limb selobjs(1),3,tempmesh
offset limb selobjs(1),3,0,0,0.75
delete mesh tempmesh
delete object tempobj
 
make mesh from object tempmesh,selobjs(1)
delete object selobjs(1)
make object selobjs(1),tempmesh,0
delete mesh tempmesh
 
for count=2 to 8
  selobjs(count)=findobj()
  clone object selobjs(count),selobjs(1) 
  show object selobjs(count)
next count
 
//   6 _____ 7
//   /|    /|   
//  /_|___/ |
//5|  |__|8_|  
// |2/   | /3
// |/____|/
//1       4
 
yrotate object selobjs(2),90 
yrotate object selobjs(3),180 
yrotate object selobjs(4),270
xrotate object selobjs(5),90  
xrotate object selobjs(6),90 
yrotate object selobjs(6),90   
zrotate object selobjs(7),90  
yrotate object selobjs(7),180 
zrotate object selobjs(8),90 
yrotate object selobjs(8),270 
for count=1 to 8
  fix object pivot selobjs(count)
next count
 
docklighta=findobj()
make object plain docklighta,0.4,0.4
xrotate object docklighta,90
fix object pivot docklighta
texture object docklighta,11
hide object docklighta
docklightb=findobj()
make object plain docklightb,0.4,0.4
xrotate object docklightb,90
fix object pivot docklightb
hide object docklightb
texture object docklightb,11
 
type button
  left as integer
  top as integer
  right as integer
  bottom as integer
  text$ as string
  textx as integer
  texty as integer
  image as integer
  id as integer
  visible as boolean
  highlighted as boolean
endtype
 
dim buttons(0) as button
 
type asteroid
  posx# as float
  posy# as float
  velx# as float
  vely# as float
  rotx# as float
  roty# as float
  rotz# as float
  obj as integer
  ore as integer
  si as integer
  size# as float
endtype
 
dim asteroids(0) as asteroid
 
for count=1 to 500
  x#=frnd#(400)-200
  y#=frnd#(400)-200
  clear=1
  for countb=1 to array count(stations(0))
    if dist#(x#-stations(countb).posx#,y#-stations(countb).posy#)<30  then clear=0
  next countb
  if clear=1
    makeasteroid(0,frnd#(2)+1,x#,y#)
  else
    count=count-1
  endif
next count
 
 
type bullet
  obj as integer
  posx# as float
  posy# as float
  velx# as float
  vely# as float
  damage as float
  imagenum as integer
  life# as float
  elapsed# as float
  size# as float
endtype
 
dim bullets(0) as bullet
 
 
 
seltype=1
selobj=1
 
rmenuon=0
rmenux=0
rmenuy=0
rmenusel=-1
 
currstationmenu=1
 
global messages$
 
dim rmenuitems$()
 
shipmonobj=findobj()
clone object shipmonobj,ships(1).obj
lock object on shipmonobj
position object shipmonobj,13,-9,20
set object ambience shipmonobj,rgb(20,40,80)
set object diffuse shipmonobj,rgb(20,40,80)
set object emissive shipmonobj,rgb(10,20,40)
set object wireframe shipmonobj,1
set object cull shipmonobj,0
hide object shipmonobj
 
monbackobj=findobj()
make object plain monbackobj,10,10
set object ambience monbackobj,rgb(20,20,30)
set object diffuse monbackobj,rgb(20,20,30)
set object emissive monbackobj,rgb(10,10,15)
lock object on monbackobj
position object monbackobj,20,-13.5,30
hide object monbackobj
 
lockedtextobj=findobj()
make object plain lockedtextobj,1,1
hide object lockedtextobj
lock object on lockedtextobj
 
textobj=findobj()
make object plain textobj,1,1
hide object textobj
 
if shiptest<>0
  ships(1).docked=1
  show object shipmonobj
  show object monbackobj
endif
 
texture object 1,6
 
 
do
  ink rgb(255,255,255),0
  newtime=timer()
  time#=(newtime-oldtime)/1000.0
  oldtime=newtime
 
  ang1#=ships(1).angle#
 
  if (ships(1).docking=0) AND (ships(1).docked=0) AND (ships(1).launching=0)
    if leftkey() then ships(1).angle#=wrapvalue(ships(1).angle#+(shiptypes(ships(1).shiptype).turnspeed#*time#))
    if rightkey() then ships(1).angle#=wrapvalue(ships(1).angle#-(shiptypes(ships(1).shiptype).turnspeed#*time#)) 
    if upkey() then ships(1).throttle#=ships(1).throttle#+(1*time#)
    if ships(1).throttle#>1 then ships(1).throttle#=1
    if downkey() then ships(1).throttle#=ships(1).throttle#-(1*time#)
    if ships(1).throttle#<0 then ships(1).throttle#=0
 endif
 
    acceleration#=shiptypes(ships(1).shiptype).acceleration#
    topspeed#=shiptypes(ships(1).shiptype).topspeed#
    pos1x#=ships(1).posx#
    pos1y#=ships(1).posy#
    ships(1).velx#=ships(1).velx#+(((cos(ships(1).angle#)*(topspeed#*ships(1).throttle#))-ships(1).velx#)*(acceleration#*time#))
    ships(1).vely#=ships(1).vely#+(((sin(ships(1).angle#)*(topspeed#*ships(1).throttle#))-ships(1).vely#)*(acceleration#*time#))
    ships(1).posx#=ships(1).posx#+(ships(1).velx#*time#)
    ships(1).posy#=ships(1).posy#+(ships(1).vely#*time#)
    pos2x#=ships(1).posx#
    pos2y#=ships(1).posy#
    ang2#=ships(1).angle#
 
    coldist#=0
 
    repeatcount=0
    shiptravel#=0
    prevcol=0
    shipmag#=sqrt(sqr#(ships(1).velx#)+sqr#(ships(1).vely#))
    while (coldist#<>-1) //AND (repeatcount<40)
      repeatcount=repeatcount+1
      colcount=0
      coldist#=-1
      colnum=9
      for counta = 1 to array count(stations(0))
        for countb=1 to stations(counta).components
          range#=stationcomponents(counta,countb).coldistance#+shiptypes(ships(1).shiptype).coldistance#
          if within(stations(counta).posx#+stationcomponents(counta,countb).posx#,stations(counta).posy#+stationcomponents(counta,countb).posy#,ships(1).posx#-range#,ships(1).posy#+range#,ships(1).posx#+range#,ships(1).posy#-range#)
 
            for countc=1 to 4
              xa1#=pos1x#+rotatex#(shipcolpoints(1,countc).x#,shipcolpoints(1,countc).y#,ang1#)
              ya1#=pos1y#+rotatey#(shipcolpoints(1,countc).x#,shipcolpoints(1,countc).y#,ang1#)
              xa2#=pos2x#+rotatex#(shipcolpoints(1,countc).x#,shipcolpoints(1,countc).y#,ang2#)
              ya2#=pos2y#+rotatey#(shipcolpoints(1,countc).x#,shipcolpoints(1,countc).y#,ang2#)
              for countd=1 to 4
                select countd
                  case 1
                    xb1#=stations(counta).posx#+stationcomponents(counta,countb).left#+0.001
                    yb1#=stations(counta).posy#+stationcomponents(counta,countb).top#+0.001
                    xb2#=stations(counta).posx#+stationcomponents(counta,countb).right#
                    yb2#=stations(counta).posy#+stationcomponents(counta,countb).top#
                  endcase
                  case 2
                    xb1#=stations(counta).posx#+stationcomponents(counta,countb).right#+0.001
                    yb1#=stations(counta).posy#+stationcomponents(counta,countb).top#
                    xb2#=stations(counta).posx#+stationcomponents(counta,countb).right#
                    yb2#=stations(counta).posy#+stationcomponents(counta,countb).bottom#
                  endcase
                  case 3
                    xb1#=stations(counta).posx#+stationcomponents(counta,countb).right#
                    yb1#=stations(counta).posy#+stationcomponents(counta,countb).bottom#+0.001
                    xb2#=stations(counta).posx#+stationcomponents(counta,countb).left#
                    yb2#=stations(counta).posy#+stationcomponents(counta,countb).bottom#
                  endcase
                  case 4
                    xb1#=stations(counta).posx#+stationcomponents(counta,countb).left#+0.001
                    yb1#=stations(counta).posy#+stationcomponents(counta,countb).bottom#
                    xb2#=stations(counta).posx#+stationcomponents(counta,countb).left#
                    yb2#=stations(counta).posy#+stationcomponents(counta,countb).top#
                  endcase
                endselect
                remstart
                ink rgb(255,0,0),0
                position object textobj,xb1#,0,yb1#
                lx1=object screen x(textobj)
                ly1=object screen y(textobj)
                position object textobj,xb2#,0,yb2#
                lx2=object screen x(textobj)
                ly2=object screen y(textobj)
                line lx1,ly1,lx2,ly2 remend
                colnum=colnum+1
                if colnum<>prevcol
                  if getintersect(xa1#,ya1#,xa2#,ya2#,xb1#,yb1#,xb2#,yb2#)
                    if (coldist#=-1) OR (sqrt(sqr#(gretx#-xa1#)+sqr#(grety#-ya1#))<coldist#)
                      currcol=colnum
                      coldist#=sqrt(sqr#(gretx#-xa1#)+sqr#(grety#-ya1#))
                      shipdist#=sqrt(sqr#(gretx#-pos1x#)+sqr#(grety#-pos1y#))
                      shipdeg#=wrapvalue((-atanfull(ships(1).velx#,ships(1).vely#))+90)
 
                      normaldeg#=wrapvalue(-atanfull(xb2#-xb1#,yb2#-yb1#))
                      newdeg#=normaldeg#-(shipdeg#-normaldeg#)-180
                      colx#=gretx#-(xa1#-pos1x#)
                      coly#=grety#-(ya1#-pos1y#)
                    endif
                  endif
                endif
              next countd
            next countc
 
          endif
        next countb
      next counta
 
      if coldist#<>-1
        prevcol=currcol
        ships(1).velx#=cos(newdeg#)*shipmag#
        ships(1).vely#=sin(newdeg#)*shipmag#
        shiptravel#=shiptravel#+coldist#
        ships(1).posx#=colx#+(cos(newdeg#)*((shipmag#*time#)-shiptravel#))
        ships(1).posy#=coly#+(sin(newdeg#)*((shipmag#*time#)-shiptravel#))
        ships(1).angle#=ang1#
        ang2#=ang1#
        pos1x#=colx#
        pos1y#=coly#
        pos2x#=ships(1).posx#
        pos2y#=ships(1).posy#
      endif     
    endwhile
 
    if spacekey()
        for count=1 to shiptypes(ships(1).shiptype).weapons
          if shipweapons(1,count).weapontype<>0
            shipweapons(1,count).reload#=shipweapons(1,count).reload#+time#
            while shipweapons(1,count).reload#>weapons(shipweapons(1,count).weapontype).reloadtime#
              shipweapons(1,count).reload#=shipweapons(1,count).reload#-weapons(shipweapons(1,count).weapontype).reloadtime#
              addbullet(shipweapons(1,count).weapontype,ships(1).posx#+rotatex#(shipweapons(1,count).posx#,shipweapons(1,count).posz#,ships(1).angle#),ships(1).posy#+rotatey#(shipweapons(1,count).posx#,shipweapons(1,count).posz#,ships(1).angle#),ships(1).velx#,ships(1).vely#,ships(1).angle#,shipweapons(1,count).reload#)
            endwhile
          endif          
        next count
    endif
 
 
    for count=1 to array count(bullets(0))
      for countb=1 to array count(asteroids(0))
        range#=bullets(count).size#+asteroids(countb).size#
        if within(bullets(count).posx#,bullets(count).posy#,asteroids(countb).posx#-range#,asteroids(countb).posy#+range#,asteroids(countb).posx#+range#,asteroids(countb).posy#-range#)
          rad#=asteroids(countb).size#/2
          for countc=1 to 4
            select countc
              case 1
                xb1#=asteroids(countb).posx#-rad#
                yb1#=asteroids(countb).posy#+rad#+0.001
                xb2#=asteroids(countb).posx#+rad#
                yb2#=asteroids(countb).posy#+rad#
              endcase
              case 2
                xb1#=asteroids(countb).posx#+rad#+0.001
                yb1#=asteroids(countb).posy#+rad#
                xb2#=asteroids(countb).posx#+rad#
                yb2#=asteroids(countb).posy#-rad#
              endcase
              case 3
                xb1#=asteroids(countb).posx#+rad#
                yb1#=asteroids(countb).posy#-rad#+0.001
                xb2#=asteroids(countb).posx#-rad#
                yb2#=asteroids(countb).posy#-rad#
              endcase
              case 4
                xb1#=asteroids(countb).posx#-rad#+0.001
                yb1#=asteroids(countb).posy#-rad#
                xb2#=asteroids(countb).posx#-rad#
                yb2#=asteroids(countb).posy#+rad#
              endcase
            endselect
            position object textobj,xb1#,0,yb1#
            xa1#=bullets(count).posx#
            ya1#=bullets(count).posy#
            xa2#=bullets(count).posx#+(bullets(count).velx#*time#)//+(asteroids(countb).velx#*time#)
            ya2#=bullets(count).posy#+(bullets(count).vely#*time#)//+(asteroids(countb).vely#*time#)
            if getintersect(xa1#,ya1#,xa2#,ya2#,xb1#,yb1#,xb2#,yb2#)
              asteroids(countb).si=asteroids(countb).si-bullets(count).damage
 
              for countp=1 to 5
                addparticle(gretx#,grety#,2,3,0,360,-20,20,10,0,0,0,flameobj)
              next countp
              delete object bullets(count).obj
              array delete element bullets(0),count
 
              count=count-1
              if asteroids(countb).si<0
                ships(1).credits=ships(1).credits+(asteroids(countb).size#-1)*75
                x#=asteroids(countb).posx#
                y#=asteroids(countb).posy#
                size#=asteroids(countb).size#
                for countp=1 to 50
                  addparticle(x#+(frnd#(size#)-size#/2),y#+(frnd#(size#)-size#/2),4,7,0,360,-20,20,10,0,0,0,flameobj)
                next countp
                delete object asteroids(countb).obj
                array delete element asteroids(0),countb
                countb=countb-1
              endif
            endif
          next countc
        endif
      next countb
    next count
 
 
    testtext$="Frame Rate: "+str$(screen fps())
 
 
    for count=1 to array count(asteroids(0))
      asteroids(count).posx#=asteroids(count).posx#+asteroids(count).velx#
      asteroids(count).posy#=asteroids(count).posy#+asteroids(count).vely#
      obj=asteroids(count).obj
      position object asteroids(count).obj,asteroids(count).posx#,0,asteroids(count).posy#
      rotate object asteroids(count).obj, object angle x(obj)+(asteroids(count).rotx#*time#),object angle y(obj)+(asteroids(count).roty#*time#),object angle z(obj)+(asteroids(count).rotz#*time#)
    next count
 
  position object ships(1).obj,ships(1).posx#,0,ships(1).posy#
  yrotate object ships(1).obj,-ships(1).angle#
 
  for countp=1 to int((time#+particletime#)/(1-(0.9*ships(1).throttle#)))
    backoffset#=frnd#(0.6)-0.3
    addparticle(ships(1).posx#+rotatex#(-1.5,backoffset#,ships(1).angle#),ships(1).posy#+rotatey#(-1.5,backoffset#,ships(1).angle#),3,5,(ships(1).angle#+180)-20,(ships(1).angle#+180)+20,-20,20,10,0.02,ships(1).velx#,ships(1).vely#,flameobj)
  next countp
  particletime#=((time#+particletime#)/0.02)-int((time#+particletime#)/0.02)
  updateparticles(time#)
 
  updatebullets(time#)
 
  poscamera(ships(1).posx#,ships(1).posy#)
  refreshbackdrop(ships(1).posx#,ships(1).posy#)
 
    if ships(1).permission<>-1
    docklightcount#=wrapvalue(docklightcount#+(360.0*time#))
    docklightscale#=200+(sin(docklightcount#)*50.0)
    scale object docklighta,docklightscale#,docklightscale#,100//docklightscale#
    scale object docklightb,docklightscale#,docklightscale#,100//docklightscale#
  endif
 
  if (ships(1).docking=1) OR (ships(1).launching=1)
    ships(1).throttle#=0
    rotinterval#=180.0*time#
    moveinterval#=1.0*time#
    achieved=0
    if valwithin(ships(1).angle#-ships(1).targetrot#,0,rotinterval#)
      ships(1).angle#=ships(1).targetrot#
      achieved=achieved+1
    else
      if ships(1).angle#-ships(1).targetrot#<0 then ships(1).angle#=ships(1).angle#+rotinterval#
      if ships(1).angle#-ships(1).targetrot#>0 then ships(1).angle#=ships(1).angle#-rotinterval#
    endif
 
    if valwithin(ships(1).posx#,ships(1).targetx#,moveinterval#)
      ships(1).posx#=ships(1).targetx#
      achieved=achieved+1
    else
      if ships(1).posx#<ships(1).targetx# then ships(1).posx#=ships(1).posx#+moveinterval#
      if ships(1).posx#>ships(1).targetx# then ships(1).posx#=ships(1).posx#-moveinterval#
    endif
 
    if valwithin(ships(1).posy#,ships(1).targety#,moveinterval#)
      ships(1).posy#=ships(1).targety#
      achieved=achieved+1
    else
      if ships(1).posy#<ships(1).targety# then ships(1).posy#=ships(1).posy#+moveinterval#
      if ships(1).posy#>ships(1).targety# then ships(1).posy#=ships(1).posy#-moveinterval#
    endif
 
    if achieved>2
      if ships(1).docking=1
        ships(1).docked=ships(1).permission
        ships(1).docking=0
        ships(1).permission=-1
        ships(1).velx#=0
        ships(1).vely#=0
        hide object docklighta
        hide object docklightb
        message("Docked")    
 
        currstationmenu=1
        stationmenuscroll=0
        addbutton("Launch",1,screen width()-190,12,185,30,13,1)
        addbutton("Commodities",1,screen width()-190,buttony,185,30,13,2)
        addbutton("Equipment",1,screen width()-190,buttony,185,30,13,3)
        addbutton("Weapons",1,screen width()-190,buttony,185,30,13,4)
        addbutton("Ships",1,screen width()-190,buttony,185,30,13,5)
        for count=1 to 20
          addbutton("",0,0,0,0,0,0,count+10)
        next count
        for count=1 to shiptypes(ships(1).shiptype).weapons
          show object shipweapons(1,count).monobj
          set object wireframe shipweapons(1,count).monobj,1
          set object ambience shipweapons(1,count).monobj,rgb(20,40,80)
          set object diffuse shipweapons(1,count).monobj,rgb(20,40,80)
          set object emissive shipweapons(1,count).monobj,rgb(10,20,40)
        next count
        set object wireframe shipweapons(1,1).monobj,0
        color object shipweapons(1,1).monobj,rgb(255,255,255)
        show object shipmonobj
        show object monbackobj
        selectedwep=1
        buttons(getbutton(21)).highlighted=1
 
      else
        ships(1).launching=0
      endif
    endif
  endif
 
  if ships(1).docked<>0
    paste image 12,screen width()-200,0,1
    paste image 17,screen width()-350,screen height()-200,1
    ink rgb(255,255,255),0
    shipmonrot#=wrapvalue(shipmonrot#+(40.0*time#))
    yrotate object shipmonobj,shipmonrot#
    for count=1 to shiptypes(ships(1).shiptype).weapons      
      yrotate object shipweapons(1,count).monobj ,shipmonrot#
      position object shipweapons(1,count).monobj,rotatex#(shipweapons(1,count).posx#,shipweapons(1,count).posz#,-shipmonrot#)+13.0,shipweapons(1,count).posy#-9.0,rotatey#(shipweapons(1,count).posx#,shipweapons(1,count).posz#,-shipmonrot#)+20.0
    next count
 
    buttony=70
    setbutton(getbutton(2),"Commodities",screen width()-190,buttony,185,30,13,2,0,1)
    if currstationmenu=1  
      for count=1 to 10
        buttony=buttony+30
        if count+stationmenuscroll>stations(ships(1).docked).pcommoditiescount
          buttons(getbutton(10+count)).visible=0
        else
          buttons(getbutton(10+count)).visible=1
          setbutton(getbutton(10+count),pcommodities(stationpcommodities(ships(1).docked,count+stationmenuscroll)).name$,screen width()-190,buttony,185,30,0,10+count,1,1)
          text screen width()-50,buttons(getbutton(10+count)).top+(15-(textheight/2)),str$(pcommodities(stationpcommodities(ships(1).docked,count+stationmenuscroll)).price)+"cr"
        endif 
      next count
    endif
    buttony=buttony+40
    setbutton(getbutton(3),"Equipment",screen width()-190,buttony,185,30,13,3,0,1)
    if currstationmenu=2  
      for count=1 to 10
        buttony=buttony+30
        if count+stationmenuscroll>stations(ships(1).docked).pequipmentcount
          buttons(getbutton(10+count)).visible=0
        else
          buttons(getbutton(10+count)).visible=1
          setbutton(getbutton(10+count),pequipment(stationpequipment(ships(1).docked,count+stationmenuscroll)).name$,screen width()-190,buttony,185,30,0,10+count,1,1)
          text screen width()-50,buttons(getbutton(10+count)).top+(15-(textheight/2)),str$(pequipment(stationpequipment(ships(1).docked,count+stationmenuscroll)).price)+"cr"
        endif 
      next count
    endif
    buttony=buttony+40
    setbutton(getbutton(4),"Weapons",screen width()-190,buttony,185,30,13,4,0,1)
    if currstationmenu=3 
      for count=1 to 10
        buttony=buttony+30
        if count+stationmenuscroll>stations(ships(1).docked).pweaponscount
          buttons(getbutton(10+count)).visible=0
        else
          buttons(getbutton(10+count)).visible=1
          setbutton(getbutton(10+count),weapons(stationpweapons(ships(1).docked,count+stationmenuscroll)).name$,screen width()-190,buttony,185,30,0,10+count,1,1)
          text screen width()-50,buttons(getbutton(10+count)).top+(15-(textheight/2)),str$(weapons(stationpweapons(ships(1).docked,count+stationmenuscroll)).price)+"cr"
        endif 
      next count
    endif
    buttony=buttony+40
    setbutton(getbutton(5),"Ships",screen width()-190,buttony,185,30,13,5,0,1)
    if currstationmenu=4 
      for count=1 to 10
        buttony=buttony+30
        if count+stationmenuscroll>stations(ships(1).docked).pshipscount
          buttons(getbutton(10+count)).visible=0
        else
          buttons(getbutton(10+count)).visible=1
          setbutton(getbutton(10+count),pships(stationpships(ships(1).docked,count+stationmenuscroll)).name$,screen width()-190,buttony,185,30,0,10+count,1,1)
          text screen width()-50,buttons(getbutton(10+count)).top+(15-(textheight/2)),str$(pships(stationpships(ships(1).docked,count+stationmenuscroll)).price)+"cr"
        endif 
      next count
    endif
    buttony=buttony+40
    text screen width()-190,screen height()-220,str$(ships(1).credits)+" credits"
    for count=1 to shiptypes(ships(1).shiptype).weapons
      ink rgb(255,255,255),0
      if weapons(shipweapons(1,count).weapontype).name$<>""
        setbutton(getbutton(20+count),weapons(shipweapons(1,count).weapontype).name$,screen width()-340,screen height()-(200-(count*30)),185,30,0,20+count,1,1)
      else
        setbutton(getbutton(20+count),"(No Weapon)",screen width()-340,screen height()-(200-(count*30)),185,30,0,20+count,1,1)
      endif
    next count
  endif
 
  buttonsel=-1
  for count=1 to array count(buttons(0))
 
    if buttons(count).visible=1
 
      if buttons(count).image<>0 
        paste image buttons(count).image,buttons(count).left,buttons(count).top,1
        if within(mousex(),mousey(),buttons(count).left,buttons(count).bottom,buttons(count).right,buttons(count).top)
          ink rgb(20,20,30),0
          buttonsel=buttons(count).id
        else
          ink rgb(255,255,255),0
        endif
      else
        if within(mousex(),mousey(),buttons(count).left,buttons(count).bottom,buttons(count).right,buttons(count).top)
          ink rgb(20,40,80),0
          buttonsel=buttons(count).id
        else
          ink rgb(255,255,255),0
        endif
      endif
 
      if buttons(count).highlighted=1
        ink rgb(20,40,80),0
        text buttons(count).textx,buttons(count).texty,"["+buttons(count).text$+"]"
      else
        text buttons(count).textx,buttons(count).texty,buttons(count).text$
      endif
    endif
  next count
 
 
  mmx=mousemovex()
  mmy=mousemovey()
  movecam=0
  lmb=0
  mmb=0
  rmb=0
 
  mv=mouseclick()
  if int(mv/8)=1
    mv=mv-8
  endif
  if int(mv/4)=1
    mv=mv-4
    mmb=1
  endif
  if int(mv/2)=1
    mv=mv-2
    rmb=1
  endif
  if mv=1
    lmb=1
  endif
 
  if rmb=1
    rseltype=0
    dim rmenuitems$(0)
    for count=1 to array count(stations(0))
      if pick object(mousex(),mousey(),stations(count).pickobj,stations(count).pickobj)=stations(count).pickobj
        rseltype=1
        rselobj=count
        if ships(1).permission=count
          array insert at bottom rmenuitems$(0)
          rmenuitems$(array count(rmenuitems$(0)))="Dock"
        else
          array insert at bottom rmenuitems$(0)
          rmenuitems$(array count(rmenuitems$(0)))="Request Dock"
        endif
      endif
    next count
    rmenuon=1
    rmenux=mousex()
    rmenuy=mousey()
  endif
 
  rmenucount=0
  if rmenuon=1
    rmenusel=-1
    for count= 1 to array count(rmenuitems$(0))
      paste image 9,rmenux,rmenuy+((count-1)*32),1
      if within(mousex(),mousey(),rmenux,rmenuy+((count-1)*32)+30,rmenux+100,rmenuy+((count-1)*32))
        ink rgb(20,40,80),0
        paste image 10,rmenux,rmenuy+((count-1)*32),1
        rmenusel=count
      else
        paste image 10,rmenux,rmenuy+((count-1)*32),1
        ink rgb(255,255,255),0
      endif
      text rmenux+12,rmenuy+7+((count-1)*32),rmenuitems$(count)
    next count
  endif
 
 
 
  if lmb=1
    if lclick=0
      if rmenusel=-1
        if buttonsel=-1
        for count=1 to array count(stations(0))
          if pick object(mousex(),mousey(),stations(count).pickobj,stations(count).pickobj)=stations(count).pickobj
            seltype=1
            selobj=count
          endif
        next count
        else
          if (buttonsel<10)
            select buttonsel
              case 1
                ships(1).launching=1
                ships(1).targetrot#=ships(1).angle#
                ships(1).targetx#=ships(1).posx#+rotatex#(2,0,stationdocks#(ships(1).docked,ships(1).docknum).angle#)
                ships(1).targety#=ships(1).posy#+rotatey#(2,0,stationdocks#(ships(1).docked,ships(1).docknum).angle#)
                ships(1).docked=0
                dim buttons(0)
                hide object shipmonobj
                hide object monbackobj
                for count=1 to shiptypes(ships(1).shiptype).weapons
                  hide object shipweapons(1,count).monobj
                next count
              endcase
              case 2
                currstationmenu=1
                stationmenuscroll=0
              endcase
              case 3
                currstationmenu=2
                stationmenuscroll=0
              endcase
              case 4
                currstationmenu=3
                stationmenuscroll=0
              endcase
              case 5
                currstationmenu=4
                stationmenuscroll=0
              endcase
            endselect
          else
            if (buttonsel<20)
              select currstationmenu
                case 3
 
                  if ships(1).credits>=weapons(stationpweapons(ships(1).docked,(buttonsel-10)+stationmenuscroll)).price//ships(1).permission,buttonsel+stationmenuscroll)).price
                    ships(1).credits=ships(1).credits-weapons(stationpweapons(ships(1).docked,(buttonsel-10)+stationmenuscroll)).price//stationpweapons(ships(1).permission,1)).price
                    shipweapons(1,selectedwep).weapontype=stationpweapons(ships(1).docked,(buttonsel-10)+stationmenuscroll)
                    delete object shipweapons(1,selectedwep).monobj
                    clone object shipweapons(1,selectedwep).monobj,weapons(shipweapons(1,selectedwep).weapontype).obj
                    lock object on shipweapons(1,selectedwep).monobj
                    yrotate object shipweapons(1,selectedwep).monobj ,shipmonrot#
                    position object shipweapons(1,selectedwep).monobj,rotatex#(shipweapons(1,selectedwep).posx#,shipweapons(1,selectedwep).posz#,-shipmonrot#)+13.0,shipweapons(1,selectedwep).posy#-9.0,rotatey#(shipweapons(1,selectedwep).posx#,shipweapons(1,selectedwep).posz#,-shipmonrot#)+20.0
                  endif
                endcase
              endselect
            else
 
              buttons(getbutton(selectedwep+20)).highlighted=0
              set object wireframe shipweapons(1,selectedwep).monobj,1
              set object ambience shipweapons(1,selectedwep).monobj,rgb(20,40,80)
              set object diffuse shipweapons(1,selectedwep).monobj,rgb(20,40,80)
              set object emissive shipweapons(1,selectedwep).monobj,rgb(10,20,40)
 
              selectedwep=buttonsel-20
 
              buttons(getbutton(buttonsel)).highlighted=1
              set object wireframe shipweapons(1,selectedwep).monobj,0
              color object shipweapons(1,selectedwep).monobj,rgb(255,255,255)
 
              endif
          endif
        endif
      else
        if rmenuitems$(rmenusel)="Request Dock"
          ships(1).docknum=rnd(stations(selobj).docks-1)+1
          message("Permission to board granted. Please proceed to dock "+str$(ships(1).docknum))
          ships(1).permission=selobj
          position object docklighta,stations(rselobj).posx#+stationdocks#(ships(1).permission,ships(1).docknum).x#+rotatex#(0,0.5,stationdocks#(ships(1).permission,ships(1).docknum).angle#),0,stations(rselobj).posy#+stationdocks#(ships(1).permission,ships(1).docknum).y#+rotatey#(0,0.5,stationdocks#(ships(1).permission,ships(1).docknum).angle#)
          position object docklightb,stations(rselobj).posx#+stationdocks#(ships(1).permission,ships(1).docknum).x#+rotatex#(0,-0.5,stationdocks#(ships(1).permission,ships(1).docknum).angle#),0,stations(rselobj).posy#+stationdocks#(ships(1).permission,ships(1).docknum).y#+rotatey#(0,-0.5,stationdocks#(ships(1).permission,ships(1).docknum).angle#)
          show object docklighta
          show object docklightb
        endif
        if rmenuitems$(rmenusel)="Dock"
          ships(1).targetrot#=stationdocks#(ships(1).permission,ships(1).docknum).angle#-shiptypes(ships(1).shiptype).dockrot#
          ships(1).targetx#=stationdocks#(ships(1).permission,ships(1).docknum).x#+stations(ships(1).permission).posx#-rotatex#(shiptypes(ships(1).shiptype).dockx#,shiptypes(ships(1).shiptype).docky#,ships(1).targetrot#)
          ships(1).targety#=stationdocks#(ships(1).permission,ships(1).docknum).y#+stations(ships(1).permission).posy#-rotatey#(shiptypes(ships(1).shiptype).dockx#,shiptypes(ships(1).shiptype).docky#,ships(1).targetrot#)
          if sqrt(sqr#(ships(1).targetx#-ships(1).posx#)+sqr#(ships(1).targety#-ships(1).posy#))<5
            message("Autodocking...") 
            ships(1).docking=1 
          else
            message("Ship to distant to enable autodock")
          endif
        endif
      endif
      rmenuon=0
      rmenusel=-1
    endif
    lclick=1
  else
  lclick=0
  endif
 
 
  ink rgb(255,255,255),0  
  if seltype=1
    selmovecount#=wrapvalue(selmovecount#+(270.0*time#))
    selmovement#=0.8+(sin(selmovecount#)/10.0)
    x#=stations(selobj).posx#
    y#=stations(selobj).posy#
    position object selobjs(1),x#+stations(selobj).left#-selmovement#,-1,y#+stations(selobj).bottom#-selmovement#
    position object selobjs(2),x#+stations(selobj).left#-selmovement#,-1,y#+stations(selobj).top#+selmovement#
    position object selobjs(3),x#+stations(selobj).right#+selmovement#,-1,y#+stations(selobj).top#+selmovement#
    position object selobjs(4),x#+stations(selobj).right#+selmovement#,-1,y#+stations(selobj).bottom#-selmovement#
    position object selobjs(5),x#+stations(selobj).left#-selmovement#,1,y#+stations(selobj).bottom#-selmovement#
    position object selobjs(6),x#+stations(selobj).left#-selmovement#,1,y#+stations(selobj).top#+selmovement#
    position object selobjs(7),x#+stations(selobj).right#+selmovement#,1,y#+stations(selobj).top#+selmovement#
    position object selobjs(8),x#+stations(selobj).right#+selmovement#,1,y#+stations(selobj).bottom#-selmovement#
    //text screen width()-200,screen height()-300,stations(selobj).name$
  else
  endif
 
 
  text 5,5,testtext$//screen fps())
  text 5,20,"Throttle: "+str$(ships(1).throttle#)
  text 5,35,"X: "+str$(ships(1).posx#)+"  Y: "+str$(ships(1).posy#)
  text 5,70,"Credits: "+str$(ships(1).credits)
 
  text screen width()-600,5,messages$
 
  //position object textobj,stationdocks#(1,1).x#,0,stationdocks#(1,1).y#
 
  paste image 8,0,screen height()-200
  radarrange#=80
  ink rgb(255,255,255),0
  for count = 1 to array count(stations(0))
    if within(stations(count).posx#,stations(count).posy#,ships(1).posx#-radarrange#-15,ships(1).posy#+radarrange#+15,ships(1).posx#+radarrange#+15,ships(1).posy#-radarrange#-15)
      for countb=1 to stations(count).components
        if within(stations(count).posx#+stationcomponents(count,countb).posx#,stations(count).posy#+stationcomponents(count,countb).posy#,ships(1).posx#-radarrange#,ships(1).posy#+radarrange#,ships(1).posx#+radarrange#,ships(1).posy#-radarrange#)
          x1=(((stations(count).posx#+stationcomponents(count,countb).left#)-ships(1).posx#)*(180.0/(radarrange#*2)))+95
          y1=(screen height()-95)-(((stations(count).posy#+stationcomponents(count,countb).top#)-ships(1).posy#)*(180.0/(radarrange#*2)))
          x2=(((stations(count).posx#+stationcomponents(count,countb).right#)-ships(1).posx#)*(180.0/(radarrange#*2)))+95
          y2=(screen height()-95)-(((stations(count).posy#+stationcomponents(count,countb).bottom#)-ships(1).posy#)*(180.0/(radarrange#*2)))
          ink rgb(255,255,255),0
          box x1,y1,x2,y2
        endif
      next countb
      for countb=1 to stations(count).docks
        if within(stations(count).posx#+stationdocks#(count,countb).x#,stations(count).posy#+stationdocks#(count,countb).y#,ships(1).posx#-radarrange#,ships(1).posy#+radarrange#,ships(1).posx#+radarrange#,ships(1).posy#-radarrange#)
          x1=(((stations(count).posx#+stationdocks#(count,countb).x#)-ships(1).posx#)*(180.0/(radarrange#*2)))+95
          y1=(screen height()-95)-(((stations(count).posy#+stationdocks#(count,countb).y#)-ships(1).posy#)*(180.0/(radarrange#*2)))
          ink rgb(255,200,0),0
          box x1-1,y1-1,x1+1,y1+1  
        endif
      next countb
    endif
  next count
  for count= 1 to array count(asteroids(0))
    if within(asteroids(count).posx#,asteroids(count).posy#,ships(1).posx#-radarrange#,ships(1).posy#+radarrange#,ships(1).posx#+radarrange#,ships(1).posy#-radarrange#)
      x1=((asteroids(count).posx#-ships(1).posx#)*(180.0/(radarrange#*2)))+95
      y1=(screen height()-95)-((asteroids(count).posy#-ships(1).posy#)*(180.0/(radarrange#*2)))
      ink rgb(125,75,0),0
      box x1-1,y1-1,x1+1,y1+1
      //rotate object asteroids(count).obj, object angle x(obj)+(asteroids(count).rotx#*time#),object angle y(obj)+(asteroids(count).roty#*time#),object angle z(obj)+(asteroids(count).rotz#*time#)
    endif
  next count
  ink rgb(0,0,255),0
  box 93,screen height()-97,97,screen height()-93
  ink rgb(20,20,30),0
  //box 0,screen height()-195,191,screen height()-190
  //box 191,screen height()-190,196,screen height()
  sync
loop
 
function poscamera(x#,y#)
  if x#>camera position x()+dscreenwidth# then position camera x#-dscreenwidth#,40,camera position z()
  if x#<camera position x()-dscreenwidth# then position camera x#+dscreenwidth#,40,camera position z()
  if y#>camera position z()+dscreenheight# then position camera camera position x(),40,y#-dscreenheight#
  if y#<camera position z()-dscreenheight# then position camera camera position x(),40,y#+dscreenheight#
endfunction
 
function findobj()
res=1
while object exist(res)=1
res=res+1
endwhile
endfunction res
 
function findmesh()
res=1
while mesh exist(res)=1
res=res+1
endwhile
endfunction res
 
function refreshbackdrop(x#,y#)
  backx=int(x#/lwidth)*lwidth
  backy=int(y#/lheight)*lheight
  for county=0 to 2
    for countx=1 to 3
      position object backdropobjs(1,(county*3)+countx),backx+(countx-2)*lwidth,l1depth,backy+(county-1)*lheight
    next countx
  next county
  for county=0 to 2
    for countx=1 to 3
      position object backdropobjs(2,(county*3)+countx),backx+(countx-2)*lwidth,l2depth,backy+(county-1)*lheight
    next countx
  next county
  for county=0 to 2
    for countx=1 to 3
      position object backdropobjs(3,(county*3)+countx),backx+(countx-2)*lwidth,l3depth,backy+(county-1)*lheight
    next countx
  next county
endfunction
 
function rotatex#(x#,y#,value#)
       result#=((x#*cos(value#))-(y#*sin(value#)))
endfunction result#
 
function rotatey#(x#,y#,value#)
       result#=((x#*sin(value#))+(y#*cos(value#)))
endfunction result#
 
function frnd#(range#)
ret#=rnd(range#*1000.0)/1000.0
endfunction ret#
 
function addparticle(x#,y#,minspeed#,maxspeed#,minyrot#,maxyrot#,minzrot#,maxzrot#,angleblur#,time#,velx#,vely#,sourceobj)
  array insert at bottom particles(0)
  num=array count(particles(0))
 
  angley#=(frnd#(maxyrot#-minyrot#)+minyrot#)+(frnd#(angleblur#)-(angleblur#/2.0))
  anglez#=(frnd#(maxzrot#-minzrot#)+minzrot#)+(frnd#(angleblur#)-(angleblur#/2.0))
  vel#=frnd#(maxspeed#-minspeed#)+minspeed#
 
  particles(num).velx#=((cos(angley#)*cos(anglez#))*vel#)
  particles(num).vely#=(sin(anglez#)*vel#)
  particles(num).velz#=((sin(angley#)*cos(anglez#))*vel#)
  particles(num).speed#=vel#
 
  interval#=frnd#(time#)
  particles(num).posx#=x#+(particles(num).velx#*interval#)
  particles(num).posy#=(particles(num).vely#*interval#)
  particles(num).posz#=y#+(particles(num).velz#*interval#)
 
  particles(num).velx#=particles(num).velx#+velx#
  particles(num).velz#=particles(num).velz#+vely#
 
  particles(num).obj=findobj()
  instance object particles(num).obj,sourceobj
  position object particles(num).obj,particles(num).posx#,particles(num).posy#,particles(num).posz#
  point object particles(num).obj,camera position x(),camera position y(), camera position z()
endfunction
 
function updateparticles(time#)
for num = 1 to array count(particles(0))
  particles(num).posx#=particles(num).posx#+(particles(num).velx#*time#)
  particles(num).posy#=particles(num).posy#+(particles(num).vely#*time#)
  particles(num).posz#=particles(num).posz#+(particles(num).velz#*time#)
  particles(num).velx#=particles(num).velx#*(1.0-(0.9999*time#))
  particles(num).vely#=particles(num).vely#*(1.0-(0.9999*time#))
  particles(num).velz#=particles(num).velz#*(1.0-(0.9999*time#))
  particles(num).speed#=particles(num).speed#*(1.0-(0.9999*time#))
  if particles(num).speed#<2
    delete object particles(num).obj
    array delete element particles(0),num
    num=num-1 
  else
    position object particles(num).obj,particles(num).posx#,particles(num).posy#,particles(num).posz#
    if particles(num).speed#<3
      scale#=((particles(num).speed#-2.0))*100
      scale object particles(num).obj,scale#,scale#,scale#
    endif
  endif
  //point object particles(num).obj,camera position x(),camera position y(), camera position z()
next count
endfunction
 
function makefullcylinder(obj,size)  
  make object cylinder obj,size
  triobj=1
  while object exist(triobj)=1
    triobj=triobj+1
  endwhile
  make object triangle triobj,0,0,0,0,0,0,0,0,0
  trimesh=1
  while mesh exist(trimesh)=1
    trimesh=trimesh+1
  endwhile
  make mesh from object trimesh,triobj   
 
  tempmesh=1
  while mesh exist(tempmesh)=1
    tempmesh=tempmesh+1
  endwhile
  make mesh from object tempmesh,obj
 
  lock vertexdata for mesh tempmesh
  vertcount=get vertexdata vertex count()-1
 
  for count=0 to vertcount-3 step 2
    add mesh to vertexdata trimesh
    vcount=get vertexdata vertex count()
    set vertexdata position vcount-1,0,size/2.0,0
    set vertexdata position vcount-2,get vertexdata position x(count+2),size/2.0,get vertexdata position z(count+2)
    set vertexdata position vcount-3,get vertexdata position x(count),size/2.0,get vertexdata position z(count)
    set indexdata get vertexdata index count()-1,vcount-1
    set indexdata get vertexdata index count()-2,vcount-2
    set indexdata get vertexdata index count()-3,vcount-3
    set vertexdata normals vcount-1,0,1,0
    set vertexdata normals vcount-2,0,1,0
    set vertexdata normals vcount-3,0,1,0
    set vertexdata uv vcount-1,0.5,0
    set vertexdata uv vcount-2,0.5,0
    set vertexdata uv vcount-3,0.5,0
  next count
 
  for count=1 to vertcount-2 step 2
    add mesh to vertexdata trimesh
    vcount=get vertexdata vertex count()
    set vertexdata position vcount-1,0,-size/2.0,0
    set vertexdata position vcount-2,get vertexdata position x(count),-size/2.0,get vertexdata position z(count)
    set vertexdata position vcount-3,get vertexdata position x(count+2),-size/2.0,get vertexdata position z(count+2)
    set indexdata get vertexdata index count()-1,vcount-1
    set indexdata get vertexdata index count()-2,vcount-2
    set indexdata get vertexdata index count()-3,vcount-3
    set vertexdata normals vcount-1,0,-1,0
    set vertexdata normals vcount-2,0,-1,0
    set vertexdata normals vcount-3,0,-1,0
    set vertexdata uv vcount-1,0.5,0
    set vertexdata uv vcount-2,0.5,0
    set vertexdata uv vcount-3,0.5,0
  next count
 
  unlock vertexdata
 
  delete object obj
  make object obj,tempmesh,0
  delete mesh tempmesh 
endfunction
 
function addstation(name$,x#,y#)
  array insert at bottom stations(0)
  dim stationcomponents(array count(stations(0))+1,maxcomponents+1)
  dim stationdocks#(array count(stations(0))+1,maxdocks+1)
  tempobj=findobj()
  stations(array count(stations(0))).pickobj=tempobj
  stations(array count(stations(0))).name$=name$
  stations(array count(stations(0))).posx#=x#
  stations(array count(stations(0))).posy#=y#
  make object box tempobj,1,1,1
  texture object tempobj,7
  set alpha mapping on tempobj,100
endfunction
 
function updatestationpickbox(station)
  tempobj=stations(array count(stations(0))).pickobj
  delete object tempobj
  width#=stations(station).right#-stations(station).left#
  height#=stations(station).top#-stations(station).bottom#
  make object box tempobj,width#,2,height#
  position object tempobj,stations(station).posx#+stations(station).left#+(width#/2),0,stations(station).posy#+stations(station).bottom#+(height#/2)
  //set object wireframe tempobj,1
  //hide object tempobj
  texture object tempobj,7
  set alpha mapping on tempobj,100
endfunction
 
function addstationcomponent(station,component,x#,y#,rot#)
  stations(station).components=stations(station).components+1
  if stations(station).components>maxcomponents
    maxcomponents=stations(station).components
    dim stationcomponents(array count(stations(0))+2,maxcomponents+2)
  endif
 
  num=stations(station).components
 
  select component
    case 1
      stationcomponents(station,num).left#=-abs(rotatex#(2,1,rot#))+x#
      stationcomponents(station,num).top#=abs(rotatey#(2,1,rot#))+y#
      stationcomponents(station,num).right#=abs(rotatex#(2,1,rot#))+x#
      stationcomponents(station,num).bottom#=-abs(rotatey#(2,1,rot#))+y#
      tempobj=findobj()
      stationcomponents(station,num).obj=tempobj
      clone object tempobj,sclargetube
      yrotate object tempobj,rot#
      position object tempobj,x#+stations(station).posx#,0,y#+stations(station).posy#
    endcase
    case 2
      stationcomponents(station,num).left#=-abs(rotatex#(1,0.5,rot#))+x#
      stationcomponents(station,num).top#=abs(rotatey#(1,0.5,rot#))+y#
      stationcomponents(station,num).right#=abs(rotatex#(1,0.5,rot#))+x#
      stationcomponents(station,num).bottom#=-abs(rotatey#(1,0.5,rot#))+y#
      tempobj=findobj()
      stationcomponents(station,num).obj=tempobj
      clone object tempobj,scsmalldock
      yrotate object tempobj,rot#
      position object tempobj,x#+stations(station).posx#,0,y#+stations(station).posy#
      addstationdock(station,1,cos(rot#)+x#,sin(rot#)+y#,rot#)
    endcase
    case 3
      stationcomponents(station,num).left#=-abs(rotatex#(2,1,rot#))+x#
      stationcomponents(station,num).top#=abs(rotatey#(2,1,rot#))+y#
      stationcomponents(station,num).right#=abs(rotatex#(2,1,rot#))+x#
      stationcomponents(station,num).bottom#=-abs(rotatey#(2,1,rot#))+y#
      tempobj=findobj()
      stationcomponents(station,num).obj=tempobj
      clone object tempobj,sclargedock
      yrotate object tempobj,rot#
      position object tempobj,x#+stations(station).posx#,0,y#+stations(station).posy#
    endcase
    case 4
      stationcomponents(station,num).left#=-abs(rotatex#(3,1.5,rot#))+x#
      stationcomponents(station,num).top#=abs(rotatey#(3,1.5,rot#))+y#
      stationcomponents(station,num).right#=abs(rotatex#(3,1.5,rot#))+x#
      stationcomponents(station,num).bottom#=-abs(rotatey#(3,1.5,rot#))+y#
      tempobj=findobj()
      stationcomponents(station,num).obj=tempobj
      clone object tempobj,scbigtube
      yrotate object tempobj,rot#
      position object tempobj,x#+stations(station).posx#,0,y#+stations(station).posy#
    endcase
    case 5
      stationcomponents(station,num).left#=-abs(rotatex#(1.5,1.5,rot#))+x#
      stationcomponents(station,num).top#=abs(rotatey#(1.5,1.5,rot#))+y#
      stationcomponents(station,num).right#=abs(rotatex#(1.5,1.5,rot#))+x#
      stationcomponents(station,num).bottom#=-abs(rotatey#(1.5,1.5,rot#))+y#
      tempobj=findobj()
      stationcomponents(station,num).obj=tempobj
      clone object tempobj,scshortbigtube
      yrotate object tempobj,rot#
      position object tempobj,x#+stations(station).posx#,0,y#+stations(station).posy#
    endcase
  endselect
  stationcomponents(station,num).posx#=x#
  stationcomponents(station,num).posy#=y#
  if stationcomponents(station,num).left#<stations(station).left# then stations(station).left#=stationcomponents(station,num).left#
  if stationcomponents(station,num).top#>stations(station).top# then stations(station).top#=stationcomponents(station,num).top#
  if stationcomponents(station,num).right#>stations(station).right# then stations(station).right#=stationcomponents(station,num).right#
  if stationcomponents(station,num).bottom#<stations(station).bottom# then stations(station).bottom#=stationcomponents(station,num).bottom#
  if dist#(stationcomponents(station,num).left#-x#,stationcomponents(station,num).top#-y#)>stationcomponents(station,num).coldistance# then stationcomponents(station,num).coldistance#=dist#(stationcomponents(station,num).left#-x#,stationcomponents(station,num).top#-y#)
  if dist#(stationcomponents(station,num).left#-x#,stationcomponents(station,num).bottom#-y#)>stationcomponents(station,num).coldistance# then stationcomponents(station,num).coldistance#=dist#(stationcomponents(station,num).left#-x#,stationcomponents(station,num).bottom#-y#)
  if dist#(stationcomponents(station,num).right#-x#,stationcomponents(station,num).top#-y#)>stationcomponents(station,num).coldistance# then stationcomponents(station,num).coldistance#=dist#(stationcomponents(station,num).right#-x#,stationcomponents(station,num).top#-y#)
  if dist#(stationcomponents(station,num).right#-x#,stationcomponents(station,num).bottom#-y#)>stationcomponents(station,num).coldistance# then stationcomponents(station,num).coldistance#=dist#(stationcomponents(station,num).right#-x#,stationcomponents(station,num).bottom#-y#)
  updatestationpickbox(station)
endfunction
 
function addstationdock(station,docktype,x#,y#,rot#)
  stations(station).docks=stations(station).docks+1
  if stations(station).docks>maxdocks
    maxdocks=stations(station).docks
    dim stationdocks#(array count(stations(0))+1,maxdocks+1)
  endif
  num=stations(station).docks
  stationdocks#(station,num).docktype=docktype
  stationdocks#(station,num).x#=x#
  stationdocks#(station,num).y#=y#
  stationdocks#(station,num).angle#=rot#
endfunction
 
Function Dline(ret,x1#,y1#,z1#,x2#,y2#,z2#,color As DWord)
   if ret=0 then ret=findobj()
   Make Object Triangle ret,x1#,y1#,z1#,x2#,y2#,z2#,x2#,y2#,z2#+0.01
   Set Object WireFrame ret,1
   Color Object ret,color
   set object specular ret,rgb(255,255,255)
   set object specular power ret,100
EndFunction ret
 
function FillCircle( CX as integer, CY as integer, R as integer ) `Not mine!!! From somewhere on the DBPro forums
   local x as integer
   local y as integer
   local i as integer
   local s as integer
 
   s=R*0.70710678 : ` this number is sin(45) - precalculate!
   box CX-s, CY-s, CX+s+1, CY+s+1
   s=s+1 : ` Faster than 'inc s'?
   i=R*R
   for y=s to R
      x=sqrt( i-(y*y) )
 
      ` Draw top and bottom
      box CX-x, CY-y, CX+x+1, CY-y+1
      box CX-x, CY+y, CX+x+1, CY+y+1
 
      ` Draw left and right
      box CX-y, CY-x, CX-y+1, CY+x+1
      box CX+y, CY-x, CX+y+1, CY+x+1
   next y
endfunction
 
function valwithin(val1#,val2#,range#)
ret=0
if (val1#<val2#+range#) AND (val1#>val2#-range#) then ret=1
endfunction ret
 
function within(x#,y#,left#,top#,right#,bottom#)
ret=0
if (x#>left#) AND (x#<right#) AND (y#>bottom#) AND (y#<top#) then ret=1 
endfunction ret
 
function message(string$)
  messages$=messages$+chr$(13)+chr$(10)+string$
endfunction
 
function sqr#(val#)
ret#=val#*val#
endfunction ret#
 
function addbutton(text$,visible,left,top,width,height,image,id)
      array insert at bottom buttons(0)
      num=array count(buttons(0))
      buttons(num).text$=text$
      buttons(num).left=left
      buttons(num).top=top
      buttons(num).right=left+width
      buttons(num).bottom=top+height
      but