DECLARE SUB O2D.MTScreenAK (Object AS ANY)
DECLARE SUB PRT.ImplodeText (Text$, Ex%, Ey%, Size%)
DECLARE SUB ENMY.AddEnemy (EnemyN AS INTEGER, X%, Y%, Ang%)
DECLARE SUB ENMY.DoTrail (I%, OldX%, OldY%)
DECLARE SUB ENMY.DrawEnemys (I%)
DECLARE SUB ENMY.Track (I%, X%, Y%)
DECLARE SUB PWRUP.AddPowerUp (Index%, X%, Y%)
DECLARE SUB PRJ.AddProjectile (Projs() AS ANY, fObject AS ANY, Template AS ANY, MAXPROJS AS INTEGER)
DECLARE SUB AddLight (X AS INTEGER, Y AS INTEGER, Size AS INTEGER, Colour AS INTEGER)
DECLARE SUB PRT.AddPart (X%, Y%, Xd AS SINGLE, Yd AS SINGLE, Size AS INTEGER, Ttl AS INTEGER, SetClr AS INTEGER, Colour AS INTEGER, Flags AS INTEGER)
DECLARE SUB O2D.Check (Object AS ANY)
DECLARE SUB O2D.Draw (Object AS ANY)
DECLARE SUB O2D.GetRadius (Object AS ANY)
DECLARE SUB O2D.MTScreen (Object AS ANY)
DECLARE SUB O2D.Propell (Object AS ANY, Speed AS SINGLE)
DECLARE SUB SetCam (X%, Y%, Zoom AS SINGLE)
DEFINT A-Z
'$DYNAMIC

DECLARE SUB BPset64 (BYVAL Layer AS INTEGER, BYVAL X AS INTEGER, BYVAL Y AS INTEGER, BYVAL Colour AS INTEGER)
DECLARE SUB BBoxF64 (BYVAL Layer AS INTEGER, BYVAL X1 AS INTEGER, BYVAL Y1 AS INTEGER, BYVAL X2 AS INTEGER, BYVAL Y2 AS INTEGER, BYVAL Colour AS INTEGER)
DECLARE SUB DoLighting ()
DECLARE SUB DoHighScores (KillEff#, WhatToDo%)
DECLARE SUB DoStars ()
DECLARE SUB MENU.MultyPlayer ()
DECLARE SUB PRT.CircleExp (X%, Y%, density%, Size%, grav%)
DECLARE SUB PRT.ClearAllParts ()
DECLARE SUB PRT.MoveAndDrawParticles ()
DECLARE SUB TX.CPrint (Text$)
DECLARE SUB TX.Locate (Y%)
DECLARE SUB GAME.LevelMode ()
DECLARE SUB GAME.ScoreMode (score AS LONG, KillEff#, TotalTime#)
DECLARE SUB MUSIC.Play (MusicN AS STRING)
DECLARE SUB RemoveMusic (MusicChannel AS INTEGER)
DECLARE SUB ShutdownDS4QB ()
DECLARE SUB PauseMusic (MusicChannel AS INTEGER)
DECLARE SUB ResumeMusic (MusicChannel AS INTEGER)
DECLARE SUB LoadMusic (MusicName AS STRING, MusicType AS INTEGER, MusicChannel AS INTEGER, Repeat AS INTEGER, Enable3D AS INTEGER)
DECLARE SUB SetVolume (MODVol%, SAMVol%, MP3Vol%)
DECLARE SUB InitDS4QB ()
DECLARE SUB LoadSample ()
DECLARE SUB PlaySample (SFXT%)
DECLARE SUB ScrollMessage (FileName AS STRING)
DECLARE SUB Explosions (St#)
DECLARE SUB PlaySound (SoundN%)
DECLARE FUNCTION NumIZE% (stringy$)

'$INCLUDE: 'DEXTERN.BI'
'$INCLUDE: 'XGMAE.BI'

DIM SHARED MVolume AS INTEGER
DIM SHARED MUSICON AS INTEGER
DIM SHARED SndLook(1 TO 8, 1 TO 44) AS INTEGER
DIM SHARED SndFile(1 TO 17) AS STRING

REM $STATIC
FUNCTION BlahS$ (N%)
 N$ = LTRIM$(RTRIM$(STR$(N%)))
 IF LEN(N$) = 1 THEN BlahS$ = "0" + N$: EXIT FUNCTION
 BlahS$ = N$
END FUNCTION

SUB DEB.Add (X, Y, Angle, Speed, Scale)
 FOR I = 1 TO UBOUND(Debris, 1)
  IF Debris(I).Used = 0 THEN
   Debris(I).Object.Model = dIs(27)
   Debris(I).Object.Sprite = -1
   O2D.GetRadius Debris(I).Object
   Debris(I).Object.Colour = 240
   Debris(I).Object.X = X
   Debris(I).Object.Y = Y
   Debris(I).Object.Ang = Angle
   Debris(I).Object.Speed = Speed
   Debris(I).Object.Scale = Scale / 2
   Debris(I).Used = -1
   EXIT SUB
  END IF
 NEXT I
END SUB

SUB DEB.Clear
 FOR I = 1 TO UBOUND(Debris, 1)
  Debris(I).Used = 0
 NEXT I
END SUB

SUB DEB.MoveDraw
 FOR I = 1 TO UBOUND(Debris, 1)
  IF Debris(I).Used THEN
   Debris(I).Object.AngOff = Debris(I).Object.AngOff + (Debris(I).Object.Speed * 2)
   O2D.Check Debris(I).Object
   O2D.Propell Debris(I).Object, Debris(I).Object.Speed
   O2D.MTScreen Debris(I).Object
   O2D.Draw Debris(I).Object
  END IF
 NEXT I
END SUB

FUNCTION Distance (X, Y, X1, Y1)
 DIM xv AS LONG, yv AS LONG
 xv = X1 - X
 yv = Y1 - Y
 Distance = SQR(xv * xv + yv * yv)
END FUNCTION

SUB ENMY.DoAi (I)
 DIM temp AS Object2D

 IF Enemys(I).Used THEN
  ENMY.DrawEnemys I
  SELECT CASE Enemys(I).Ai
   CASE 1      ' Asteriod type movement.. "stupid drifter"
   
    IF Enemys(I).Status = 1 THEN
      IF Enemys(I).Object.Scale = .5 THEN
        Enemys(I).Object.Speed = 1 + (RND * .5)
        Enemys(I).Object.Ang = INT(RND * 360)
        IF DiffLev > 1 THEN ScoreWin = ScoreWin + 10
        IF DiffLev = 3 THEN ScoreWin = ScoreWin + 10
       ELSE
        IF Enemys(I).Object.Ang = 0 THEN Enemys(I).Object.Ang = INT(RND * 360)
        Enemys(I).Object.Speed = 3
        IF DiffLev = 3 THEN Enemys(I).Object.Speed = 4
      END IF
      Enemys(I).Status = 2
     ELSEIF Enemys(I).Status = 2 THEN
      Enemys(I).Object.AngOff = Enemys(I).Object.AngOff + 5
      O2D.Check Enemys(I).Object
      IF Enemys(I).Ft = 0 THEN O2D.Propell Enemys(I).Object, Enemys(I).Object.Speed
      O2D.MTScreen Enemys(I).Object
      IF Enemys(I).Hp <= 0 THEN Enemys(I).Status = 3
     ELSEIF Enemys(I).Status = 3 THEN
      DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT
     
      IF Enemys(I).Object.Scale = .5 THEN
        PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 30, 2, 0
        PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1
        IF DiffLev > 1 THEN
         ENMY.AddEnemy 2, INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), Enemys(I).Object.Ang + Enemys(I).Object.AngOff + 90
         ENMY.AddEnemy 2, INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), Enemys(I).Object.Ang + Enemys(I).Object.AngOff + 270
        END IF
        IF DiffLev = 3 THEN
         ENMY.AddEnemy 2, INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), Enemys(I).Object.Ang + Enemys(I).Object.AngOff + 180
         ENMY.AddEnemy 2, INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), Enemys(I).Object.Ang + Enemys(I).Object.AngOff
        END IF
       ELSE
        PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 15, 2, 0
        PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1
      END IF

      Enemys(I).Used = 0
    END IF
  CASE 2       '  Green ship type 1 :: waypoint thingy
  
    IF Enemys(I).Status = 1 THEN
      Enemys(I).Object.Speed = 2
      IF DiffLev = 3 THEN Enemys(I).Object.Speed = 3
      DO
       RANDOMIZE TIMER
       Enemys(I).E1.A = (RND * 220) + 50
       Enemys(I).E1.B = (RND * 150) + 25
       Enemys(I).E1.C = TIMER
      LOOP WHILE Distance(INT(Enemys(I).E1.A), INT(Enemys(I).E1.B), INT(Enemys(I).Object.Y), INT(Enemys(I).Object.X)) <= 50
      Enemys(I).Status = 2
    END IF
    IF Enemys(I).Status = 2 THEN
      d = Distance(INT(Enemys(I).E1.A), INT(Enemys(I).E1.B), INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y))
      IF TIMER - Enemys(I).E1.C > 5 THEN d = 0
     
      IF d > 50 THEN
        ENMY.Track I, INT(Enemys(I).E1.A), INT(Enemys(I).E1.B)
       ELSE
        Enemys(I).Status = 1
      END IF

      IF Enemys(I).Hp <> Enemys(I).MHp THEN Enemys(I).Ai = 3
        
      O2D.Check Enemys(I).Object
      IF Enemys(I).Ft = 0 THEN
       OldX = Enemys(I).Object.X: OldY = Enemys(I).Object.Y
       O2D.Propell Enemys(I).Object, Enemys(I).Object.Speed
      END IF
      O2D.MTScreen Enemys(I).Object
      IF Enemys(I).Hp <= 0 THEN Enemys(I).Status = 3
      ENMY.DoTrail I, OldX, OldY
     ELSEIF Enemys(I).Status = 3 THEN
      DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT
    
      PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 30, 2, 0
      PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1
     
      Enemys(I).Used = 0
    END IF
   
   CASE 3      ' Green ship thingy type 2 :: agresive

    IF Enemys(I).Status = 1 THEN
      Enemys(I).Object.Speed = 3
      IF DiffLev = 3 THEN Enemys(I).Object.Speed = 5
      Enemys(I).Status = 2
    END IF
    IF Enemys(I).Status = 2 THEN
       
      ENMY.Track I, INT(MyShip.X), INT(MyShip.Y)
       
      O2D.Check Enemys(I).Object
      IF Enemys(I).Ft = 0 THEN
       OldX = Enemys(I).Object.X: OldY = Enemys(I).Object.Y
       O2D.Propell Enemys(I).Object, Enemys(I).Object.Speed
      END IF
      O2D.MTScreen Enemys(I).Object
      ENMY.DoTrail I, OldX, OldY
      IF Enemys(I).Hp <= 0 THEN Enemys(I).Status = 3
     ELSEIF Enemys(I).Status = 3 THEN
      DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT
   
      PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 30, 2, 0
      PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1

      IF (INT(RND * 3) + 1) = 2 THEN PWRUP.AddPowerUp 1, INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y)

      Enemys(I).Used = 0
    END IF

   CASE 4      ' Blaster Turret

    IF Enemys(I).Status = 1 THEN Enemys(I).Status = 2: Enemys(I).E1.A = TIMER + (I MOD 5)
    IF Enemys(I).Status = 2 THEN

      ENMY.Track I, INT(MyShip.X), INT(MyShip.Y)
     
      O2D.Check Enemys(I).Object
     
      B = 2
      IF DiffLev = 3 THEN B = 1
  
      IF TIMER - Enemys(I).E1.A > B THEN
       DS4QB.PlaySoundEx 6, 30000, CURRENT, CURRENT, CURRENT
       PRJ.AddProjectile EnemyProjs(), Enemys(I).Object, LLaserTemplate, MAXENEMYPROJS
       Enemys(I).E1.A = TIMER
      END IF

      IF Enemys(I).Ft = 0 THEN O2D.Propell Enemys(I).Object, Enemys(I).Object.Speed
      O2D.MTScreen Enemys(I).Object
      IF Enemys(I).Hp <= 0 THEN Enemys(I).Status = 3
    
     ELSEIF Enemys(I).Status = 3 THEN
      DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT
  
      PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 30, 2, 0
      PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1

      PWRUP.AddPowerUp 2, INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y)

      Enemys(I).Used = 0
    END IF
 
  CASE 5       '  Blue missile ship
 
    IF Enemys(I).Status = 1 THEN
      Enemys(I).Object.Speed = 1
      IF DiffLev = 3 THEN Enemys(I).Object.Speed = 2
      Enemys(I).Status = 2
    END IF
    IF Enemys(I).Status = 2 THEN
     
      ENMY.Track I, INT(MyShip.X), INT(MyShip.Y)

      IF Enemys(I).Hp <> Enemys(I).MHp AND Enemys(I).E1.C <> Enemys(I).Hp THEN
       IF DiffLev = 3 THEN Enemys(I).Object.Speed = 4
       Enemys(I).E1.A = TIMER
       Enemys(I).E1.C = Enemys(I).Hp
      END IF
     
      IF TIMER - Enemys(I).E1.A > 10 THEN Enemys(I).Object.Speed = 1.5
       
      O2D.Check Enemys(I).Object
     
      IF (TIMER - Enemys(I).E1.B > 4 OR (Enemys(I).E3.C AND TIMER - Enemys(I).E1.B > 2)) AND Enemys(I).E3.B = 0 THEN
       
       IF TIMER - Enemys(I).E2.A > .5 THEN
        DS4QB.PlaySoundEx 5, 11025, CURRENT, CURRENT, CURRENT
        PRJ.AddProjectile EnemyProjs(), Enemys(I).Object, MissileTemplate, MAXENEMYPROJS
        Enemys(I).E2.A = TIMER
        Enemys(I).E2.B = Enemys(I).E2.B + 1
       END IF

       IF Enemys(I).E2.B = 2 THEN Enemys(I).E1.B = TIMER: Enemys(I).E2.B = 0
       
      END IF

      IF Enemys(I).Ft = 0 THEN
       OldX = Enemys(I).Object.X: OldY = Enemys(I).Object.Y
       IF Enemys(I).E3.C THEN Enemys(I).Object.Speed = .5
       O2D.Propell Enemys(I).Object, Enemys(I).Object.Speed
      END IF
      O2D.MTScreen Enemys(I).Object
      ENMY.DoTrail I, OldX, OldY
      IF Enemys(I).Hp <= 0 THEN Enemys(I).Status = 3
     ELSEIF Enemys(I).Status = 3 THEN
      DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT
   
      PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 30, 2, 0
      PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1

      PWRUP.AddPowerUp 1, INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y)
    
      Enemys(I).Used = 0
    END IF

   CASE 6      ' Green drill ship
 
    IF Enemys(I).Status = 1 THEN
     Enemys(I).Object.Speed = 8
     Enemys(I).Status = 2
     Enemys(I).E1.A = TIMER
     Enemys(I).E1.B = 0
    END IF
    IF Enemys(I).Status = 2 THEN

      B = 3
      IF DiffLev = 3 THEN B = 2

      IF TIMER - Enemys(I).E1.A > B THEN
       Enemys(I).E1.B = Enemys(I).E1.B + 10
       Enemys(I).Object.Ang = Enemys(I).Object.Ang + 10
       IF Enemys(I).E1.B >= 90 THEN Enemys(I).E1.B = 0: Enemys(I).E1.A = TIMER
      END IF

      O2D.Check Enemys(I).Object

      O2D.MTScreenAK Enemys(I).Object

      IF Enemys(I).Ft = 0 THEN
       OldX = Enemys(I).Object.X: OldY = Enemys(I).Object.Y
       O2D.Propell Enemys(I).Object, Enemys(I).Object.Speed
      END IF

      ENMY.DoTrail I, OldX, OldY
      IF Enemys(I).Hp <= 0 THEN Enemys(I).Status = 3
     ELSEIF Enemys(I).Status = 3 THEN
      DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT
   
      PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 30, 2, 0
      PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1

      Enemys(I).Used = 0
    END IF
  
   CASE 7      ' 1st boss

    IF Enemys(I).Status = 1 THEN
      Enemys(I).Object.Speed = 5
      IF DiffLev = 3 THEN Enemys(I).TurnSpeed = 40
      DO
       RANDOMIZE TIMER
       Enemys(I).E1.A = (RND * 220) + 50
       Enemys(I).E1.B = (RND * 150) + 25
       Enemys(I).E1.C = 0
       Enemys(I).E3.A = TIMER
      LOOP WHILE Distance(INT(Enemys(I).E1.A), INT(Enemys(I).E1.B), INT(Enemys(I).Object.Y), INT(Enemys(I).Object.X)) <= 50
      Enemys(I).Status = 2
    END IF
    IF Enemys(I).Status = 2 THEN
      d = Distance(INT(Enemys(I).E1.A), INT(Enemys(I).E1.B), INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y))
      IF TIMER - Enemys(I).E3.A > 5 THEN d = 0

      IF Enemys(I).Hp < 20 THEN d = 51
     
      IF d > 50 THEN
        IF Enemys(I).Hp >= 20 THEN ENMY.Track I, INT(Enemys(I).E1.A), INT(Enemys(I).E1.B)
        OldX = Enemys(I).Object.X: OldY = Enemys(I).Object.Y
        O2D.Propell Enemys(I).Object, Enemys(I).Object.Speed
        ENMY.DoTrail I, OldX, OldY
       ELSE
        IF Enemys(I).E1.C = 0 THEN
         Enemys(I).E1.C = TIMER
         Enemys(I).E2.A = TIMER
         Enemys(I).E2.B = TIMER
        END IF
        ENMY.Track I, INT(MyShip.X), INT(MyShip.Y)
        IF TIMER - Enemys(I).E1.C > 4 THEN Enemys(I).Status = 1
      END IF

      IF Enemys(I).Hp < 20 THEN
       ENMY.Track I, INT(MyShip.X), INT(MyShip.Y)
       Enemys(I).Object.Speed = 2.5
       IF INT(RND * 15) = 1 THEN PRT.CircleExp INT(Enemys(I).Object.X + ((RND * 50) - 25)), INT(Enemys(I).Object.Y + ((RND * 50) - 25)), 15, 2, 0: DS4QB.PlaySoundEx 1, 11000, CURRENT, CURRENT, CURRENT
      END IF
     
      IF d <= 50 OR Enemys(I).Hp < 20 THEN
       IF TIMER - Enemys(I).E2.B > .1 THEN
        DS4QB.PlaySoundEx 6, 30000, CURRENT, CURRENT, CURRENT
        
        temp = Enemys(I).Object: temp.Scale = .5
        temp.Ang = temp.Ang + 90: O2D.Check temp: O2D.Propell temp, 15
        temp.Ang = temp.Ang - 90: O2D.Check temp
        PRJ.AddProjectile EnemyProjs(), temp, LLaserTemplate, MAXENEMYPROJS
      
        temp = Enemys(I).Object: temp.Scale = .5
        temp.Ang = temp.Ang - 90: O2D.Check temp: O2D.Propell temp, 15
        temp.Ang = temp.Ang + 90: O2D.Check temp
        PRJ.AddProjectile EnemyProjs(), temp, LLaserTemplate, MAXENEMYPROJS
      
        Enemys(I).E2.B = TIMER
       END IF
       IF TIMER - Enemys(I).E2.A > .3 THEN
        DS4QB.PlaySoundEx 5, 11025, CURRENT, CURRENT, CURRENT
        PRJ.AddProjectile EnemyProjs(), Enemys(I).Object, MissileTemplate, MAXENEMYPROJS
        Enemys(I).E2.A = TIMER
       END IF
      END IF
     
      O2D.Check Enemys(I).Object
      O2D.MTScreen Enemys(I).Object
      IF Enemys(I).Hp <= 0 THEN Enemys(I).Status = 3
     ELSEIF Enemys(I).Status = 3 THEN
      DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT
   
      PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 100, 2, 0
      PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1
    
      Enemys(I).Used = 0
    END IF

   CASE 8      ' Blue warship

    IF Enemys(I).Status = 1 THEN
      Enemys(I).Object.Speed = 1
      IF DiffLev = 3 THEN Enemys(I).Object.Speed = 2
      Enemys(I).Status = 2
    END IF
    IF Enemys(I).Status = 2 THEN

      ENMY.Track I, INT(MyShip.X), INT(MyShip.Y)

      IF Enemys(I).Hp <> Enemys(I).MHp AND Enemys(I).E1.C <> Enemys(I).Hp THEN
       IF DiffLev = 3 THEN Enemys(I).Object.Speed = 5 ELSE Enemys(I).Object.Speed = 4
       Enemys(I).E1.A = TIMER
       Enemys(I).E1.C = Enemys(I).Hp
      END IF

      IF TIMER - Enemys(I).E1.A > 10 THEN Enemys(I).Object.Speed = 1.5

      O2D.Check Enemys(I).Object

      IF (TIMER - Enemys(I).E1.B > 4 OR (Enemys(I).E3.C AND TIMER - Enemys(I).E1.B > 2)) AND Enemys(I).E3.B = 0 THEN

       IF TIMER - Enemys(I).E2.A > .5 THEN
        DS4QB.PlaySoundEx 5, 11025, CURRENT, CURRENT, CURRENT
        temp = Enemys(I).Object: temp.Scale = .5
        temp.Ang = temp.Ang + 90: O2D.Check temp: O2D.Propell temp, 6
        temp.Ang = temp.Ang - 90: O2D.Check temp: O2D.Propell temp, -5
        PRJ.AddProjectile EnemyProjs(), temp, MissileTemplate, MAXENEMYPROJS
        temp = Enemys(I).Object: temp.Scale = .5
        temp.Ang = temp.Ang - 90: O2D.Check temp: O2D.Propell temp, 6
        temp.Ang = temp.Ang + 90: O2D.Check temp: O2D.Propell temp, -5
        PRJ.AddProjectile EnemyProjs(), temp, MissileTemplate, MAXENEMYPROJS
        Enemys(I).E2.A = TIMER
        Enemys(I).E2.B = Enemys(I).E2.B + 1
       END IF

       IF Enemys(I).E2.B = 2 THEN Enemys(I).E1.B = TIMER: Enemys(I).E2.B = 0

      END IF

      IF Enemys(I).Ft = 0 THEN
       OldX = Enemys(I).Object.X: OldY = Enemys(I).Object.Y
       IF Enemys(I).E3.C THEN Enemys(I).Object.Speed = .5
       O2D.Propell Enemys(I).Object, Enemys(I).Object.Speed
      END IF
      O2D.MTScreen Enemys(I).Object
      ENMY.DoTrail I, OldX, OldY
      IF Enemys(I).Hp <= 0 THEN Enemys(I).Status = 3
     ELSEIF Enemys(I).Status = 3 THEN
      DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT

      PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 30, 2, 0
      PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1

      PWRUP.AddPowerUp 1, INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y)

      Enemys(I).Used = 0
    END IF

   CASE 9      ' Double missile turret

    IF Enemys(I).Status = 1 THEN Enemys(I).Status = 2: Enemys(I).E1.A = TIMER + (I MOD 5)
    IF Enemys(I).Status = 2 THEN

      ENMY.Track I, INT(MyShip.X), INT(MyShip.Y)

      O2D.Check Enemys(I).Object

      B = 3
      IF DiffLev = 3 THEN B = 2

      IF TIMER - Enemys(I).E1.A > B THEN
       DS4QB.PlaySoundEx 5, 11025, CURRENT, CURRENT, CURRENT
       temp = Enemys(I).Object: temp.Scale = .5
       temp.Ang = temp.Ang + 90: O2D.Check temp: O2D.Propell temp, 3
       temp.Ang = temp.Ang - 90: O2D.Check temp: O2D.Propell temp, -5
       PRJ.AddProjectile EnemyProjs(), temp, MissileTemplate, MAXENEMYPROJS
       temp = Enemys(I).Object: temp.Scale = .5
       temp.Ang = temp.Ang - 90: O2D.Check temp: O2D.Propell temp, 3
       temp.Ang = temp.Ang + 90: O2D.Check temp: O2D.Propell temp, -5
       PRJ.AddProjectile EnemyProjs(), temp, MissileTemplate, MAXENEMYPROJS
       Enemys(I).E1.A = TIMER
      END IF

      IF Enemys(I).Ft = 0 THEN O2D.Propell Enemys(I).Object, Enemys(I).Object.Speed
      O2D.MTScreen Enemys(I).Object
      IF Enemys(I).Hp <= 0 THEN Enemys(I).Status = 3

     ELSEIF Enemys(I).Status = 3 THEN
      DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT

      PRT.CircleExp INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 30, 2, 0
      PRT.ImplodeText "BOOM!", INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y), 1

      PWRUP.AddPowerUp 3, INT(Enemys(I).Object.X), INT(Enemys(I).Object.Y)

      Enemys(I).Used = 0
    END IF

  END SELECT
 END IF
END SUB

SUB Explosions (St#)
 IF St# = 0 THEN St# = TIMER
 IF TIMER - St# > .3 AND INT(RND * 30) = 10 THEN
  PRT.CircleExp (RND * 320), (RND * 200), 40, 2, 0
  St# = TIMER
 END IF
END SUB

REM $DYNAMIC
SUB MENU.Main

 Sel = 1: OSel = Sel
 SelI = 8

 CSSetTimer 0, 25

 St# = TIMER
 DO
  CSWaitTimer 0
 
  CSClear EMS.Layer, 0

  DoStars

  PRT.MoveAndDrawParticles

  Explosions St#

  TX.Locate 8
  Text.Colour = 255

  BBoxF64 EMS.Layer, 84, 69, 234, 131, 143

  TX.CPrint "ž Menu ž"
  TX.CPrint "ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ"
  FOR d = 1 TO 7
   TX.CPrint "³                  ³"
  NEXT
  TX.CPrint "ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ"

  TX.Locate 10

  Text.Colour = 255: TX.CPrint " Start Game     "
                     TX.CPrint " Training       "
                     TX.CPrint " MultiPlayer    "
                     TX.CPrint " High scores    "
  Text.Colour = 220: TX.CPrint " Options        "
  Text.Colour = 255: TX.CPrint " Credits        "
                     TX.CPrint " Exit           "

  SelA = SelI + (8 * 8)

  BBoxF64 EMS.Layer, 92, SelA, 92 + 134, SelA + 7, 143

  K$ = ""
  FOR I = 1 TO 16
   G$ = UCASE$(INKEY$)
   IF G$ <> "" AND TIMER - pauseK# > .1 THEN K$ = G$: pauseK# = TIMER
  NEXT I

  IF Sel * 8 > SelI THEN SelI = SelI + (MenuStep + (7 * -((Sel + 1 <> OSel) AND (Sel - 1 <> OSel))))
  IF Sel * 8 < SelI THEN SelI = SelI - (MenuStep + (7 * -((Sel + 1 <> OSel) AND (Sel - 1 <> OSel))))
  
  SELECT CASE K$
   CASE CHR$(0) + "P"
    IF Sel * 8 = SelI THEN OSel = Sel: Sel = Sel + 1
    IF Sel > 7 THEN Sel = 1
    DS4QB.PlaySoundEx 7, 44050, CURRENT, CURRENT, CURRENT
   CASE CHR$(0) + "H"
    IF Sel * 8 = SelI THEN OSel = Sel: Sel = Sel - 1
    IF Sel < 1 THEN Sel = 7
    DS4QB.PlaySoundEx 7, 44050, CURRENT, CURRENT, CURRENT
   CASE CHR$(13), CHR$(32):
    SELECT CASE Sel
     CASE 1
      DS4QB.PlaySoundEx 4, 22050, CURRENT, CURRENT, CURRENT
      GAME.LevelMode
     CASE 2
      DS4QB.PlaySoundEx 4, 22050, CURRENT, CURRENT, CURRENT
      GAME.ScoreMode score&, KillEff#, TotalTime#: PRT.ClearAllParts
     CASE 3
      DS4QB.PlaySoundEx 4, 22050, CURRENT, CURRENT, CURRENT
      MENU.MultyPlayer
     CASE 4
      DS4QB.PlaySoundEx 4, 22050, CURRENT, CURRENT, CURRENT
      DoHighScores 0, DHS.VIEW
     CASE 6
      DS4QB.PlaySoundEx 4, 22050, CURRENT, CURRENT, CURRENT
      ScrollMessage "XGTXT03.TXT"
     CASE 7
      DS4QB.PlaySoundEx 4, 22050, CURRENT, CURRENT, CURRENT
      GOTO ExitProg
     CASE ELSE
      DS4QB.PlaySoundEx 6, 22050, CURRENT, CURRENT, CURRENT
     END SELECT
   CASE CHR$(27): IF (Sel * 8) = SelI THEN OSel = Sel: Sel = 7
  END SELECT

  SetCam 160, 100, 1
 
  CSPcopy EMS.Layer, &HA000
 LOOP

ExitProg:

END SUB

SUB PlaySound (SoundN)
 IF MUSICON THEN PlaySample SndLook(SoundN, friq& \ 1000)
END SUB

SUB PRJ.AddProjectile (Projs() AS Projectile, fObject AS Object2D, Template AS Projectile, MAXPROJS AS INTEGER)
 okflag = 0
 FOR I = 1 TO MAXPROJS
  IF Projs(I).Active = 0 THEN
   Projs(I) = Template
   ta = ((fObject.Ang + Projs(I).DepAng + 720) MOD 360)
   IF ta = 0 THEN ta = 1
   Projs(I).Object.X = fObject.X + ((fObject.Scale * fObject.Radius) * Cosine(ta))
   Projs(I).Object.Y = fObject.Y + ((fObject.Scale * fObject.Radius) * Sine(ta))
   Projs(I).Object.Ang = fObject.Ang
   IF Projs(I).Object.Speed THEN
    Projs(I).Object.Speed = Projs(I).Object.Speed + fObject.Speed
    IF Projs(I).Object.Speed < .1 THEN Projs(I).Object.Speed = .1
   END IF
   Projs(I).Active = 1
   okflag = -1
   EXIT SUB
  END IF
 NEXT I
END SUB

SUB PRJ.CheckCollide (Projs() AS Projectile, Object AS Object2D, MAXPROJS AS INTEGER, rx, ry, rj)
 rj = 0
 FOR J = 1 TO MAXPROJS
  IF Projs(J).Active THEN
   IF O2D.Collide(Projs(J).Object, Object, TX, Ty) THEN rj = J: rx = TX: ry = Ty: EXIT FOR
  END IF
 NEXT J
END SUB

SUB PRJ.ClearProjectiles (Projs() AS Projectile, MAXPROJS AS INTEGER)
 FOR I = 1 TO MAXPROJS
  Projs(I).Active = 0
 NEXT I
END SUB

SUB PRJ.MoveAndDrawProjectiles (Projs() AS Projectile, MAXPROJS AS INTEGER)
 DIM Test AS INTEGER
 DIM temp AS Object2D
 DIM Old AS Object2D
 DIM NewX AS SINGLE, NewY AS SINGLE, R AS SINGLE
 FOR I = 1 TO MAXPROJS
  IF Projs(I).Active = 1 THEN
   temp = Projs(I).Object
   IF Projs(I).Accelmult THEN temp.Speed = temp.Speed * 2
   IF Projs(I).Accelerate THEN temp.Speed = temp.Speed + Projs(I).Accelerate
   Old = temp
   O2D.Propell temp, temp.Speed
   O2D.Check temp
   IF NOT Projs(I).Invisible THEN O2D.Draw temp
   IF Projs(I).Rotate > 0 THEN temp.AngOff = ((temp.AngOff + Projs(I).Rotate) MOD 360)
   IF (temp.X <> Old.X OR temp.Y <> Old.Y) AND Projs(I).Trail THEN
    A = ((temp.Ang + temp.AngOff + 90 + 720) MOD 360)
    IF A = 0 THEN A = 1
    NewX = temp.X + (temp.X - Old.X) + ((temp.Radius * temp.Scale) * Cosine(A))
    NewY = temp.Y + (temp.Y - Old.Y) + ((temp.Radius * temp.Scale) * Sine(A))
    R = RND
    R = R - (R / 2)
    IF Projs(I).Trail = -2 THEN
      PRT.AddPart INT(NewX), INT(NewY), -((temp.X - Old.X) / 10) + R, -((temp.Y - Old.Y) / 10) + R, 3, 2, 0, Red, 0
     ELSE
      PRT.AddPart INT(NewX), INT(NewY), -((temp.X - Old.X) / 10) + R, -((temp.Y - Old.Y) / 10) + R, 3, 20, 0, Red, 0
    END IF
    AddLight INT(NewX), INT(NewY), 15, 32
   END IF
   IF (temp.X > (320 + (temp.Radius * temp.Scale))) OR (temp.Y > (200 + (temp.Radius * temp.Scale))) OR (temp.X < (1 - (temp.Radius * temp.Scale))) OR (temp.Y < (1 - (temp.Radius * temp.Scale))) THEN
    Projs(I).Active = 0:
   END IF
   Projs(I).Object = temp
  END IF
 NEXT I
END SUB

SUB TX.CLS
 Text.y = 0
 CSClear &HA000, 0
END SUB

SUB TX.CPrint (Text$)
 Text.x = 160 - (CSLen(Text$) / 2)
 CSPrint EMS.Layer, Text.x, Text.y, Text$, Text.Colour
 Text.y = Text.y + 8
END SUB

SUB TX.Locate (Y)
 Text.y = (Y * 8) - 8
END SUB

SUB TX.Print (Text$)
 CSPrint &HA000, Text.x, Text.y, Text$, Text.Colour
 Text.y = Text.y + 8
END SUB

