DECLARE SUB AddLight (X AS INTEGER, Y AS INTEGER, Size AS INTEGER, Colour AS INTEGER)
DECLARE SUB AddPxLight (X%, Y%, Size%, Colour%)
DEFINT A-Z
'$DYNAMIC

DECLARE SUB Delay (Time AS DOUBLE)
DECLARE SUB DoStars ()
DECLARE SUB LOAD.InitVideo ()
DECLARE SUB O2D.Check (Object AS ANY)
DECLARE SUB O2D.Draw (Object AS ANY)
DECLARE SUB PRJ.MoveAndDrawProjectiles (Projs() AS ANY, MAXPROJS 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 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 ENMY.DoAi (I%)
DECLARE SUB ENMY.DrawEnemys (I%)
DECLARE SUB PlaySound (SoundN%)
DECLARE SUB ScrollMessage (FileName AS STRING)
DECLARE SUB GoRetro ()
DECLARE SUB DEB.Clear ()
DECLARE SUB DEB.MoveDraw ()
DECLARE SUB DEB.Add (X%, Y%, Angle%, Speed%, Scale%)
DECLARE SUB InMessage (FileName AS STRING)
DECLARE SUB DoShat ()

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

DIM SHARED exflag AS INTEGER, DsDouble AS INTEGER

REM $STATIC
SUB DoShat
 CSWaitTimer 0
 CSClear EMS.Layer, 0

 DoStars
 IF DsDouble THEN DoStars: DsDouble = 0

 PRT.MoveAndDrawParticles
END SUB

SUB GoRetro
 DIM cc AS DOUBLE
 exflag = 0
 st# = TIMER
 cc = 0
 DO
  DoShat

  cc = cc + .3

  CSBoxF EMS.Layer, 0, 0, 319, INT(cc), 0
  CSBoxF EMS.Layer, 0, 199 - INT(cc), 319, 199, 0

  CSPcopy EMS.Layer, &HA000
  IF INKEY$ = CHR$(27) THEN exflag = -1: EXIT SUB
 LOOP UNTIL cc >= 20

END SUB

SUB InMessage (FileName AS STRING)

 CSClear &HA000, 0: CSClear EMS.Layer, 0
 PRT.ClearAllParts
 LOAD.InitVideo

 FOR j = 1 TO 2
  FOR I = 1 TO 2
   CSClear EMS.Layer, 0
   CSPcopy EMS.Layer, &HA000: Delay .3
   TX.Locate 13: Text.Colour = 255: TX.CPrint "-Incomming Message-"
   DS4QB.PlaySoundEx 3, 44050, CURRENT, CURRENT, CURRENT
   CSPcopy EMS.Layer, &HA000
   Delay .3
  NEXT I
  Delay 2.2
 NEXT j

 DS4QB.PlaySoundEx 4, 22050, CURRENT, CURRENT, CURRENT
 
 CSSetTimer 0, 35

 FOR I = 3 TO 95 STEP 4
  CSWaitTimer 0
  CSClear EMS.Layer, 0
  CSBox EMS.Layer, 160 - (I * 1.6), 100 - I, 160 + (I * 1.6), 100 + I, 150
  CSBox EMS.Layer, 160 - (I * 1.6), 100 - I, 160 + (I * 1.6), 100 + I, 191
  CSPcopy EMS.Layer, &HA000
 NEXT I

 CSPcopy &HA000, EMS.Layer

 F = FREEFILE
 OPEN "Text\" + FileName FOR INPUT AS #F
  t.y = 20
  DO
   INPUT #F, sText$
   IF sText$ = "*pause*" THEN sText$ = "": t.y = t.y - 10: Delay 1
   t.x = 8
   FOR I = 1 TO LEN(sText$)
    t.x = t.x + 8
    CSPrint &HA000, t.x, t.y, MID$(sText$, I, 1), 255
    IF MID$(sText$, I, 1) <> CHR$(32) THEN
     DS4QB.PlaySoundEx 7, 44050, CURRENT, CURRENT, CURRENT
     Delay .01
    END IF
   NEXT I
   Delay .1
   t.y = t.y + 10
  LOOP UNTIL EOF(F)
 CLOSE #F

 WHILE LEN(INKEY$): WEND
 DO: LOOP UNTIL LEN(INKEY$)

 DS4QB.PlaySoundEx 4, 22050, CURRENT, CURRENT, CURRENT
 
 FOR I = 95 TO 3 STEP -4
  CSWaitTimer 0
  CSClear EMS.Layer, 0
  CSBox EMS.Layer, 160 - (I * 1.6), 100 - I, 160 + (I * 1.6), 100 + I, 150
  CSBox EMS.Layer, 160 - (I * 1.6), 100 - I, 160 + (I * 1.6), 100 + I, 191
  CSPcopy EMS.Layer, &HA000
 NEXT I

 CSClear &HA000, 0

 Delay 2

END SUB

SUB Intro
END SUB

SUB LMMid
 exflag = 0: maxobj = 20
 DIM obj(1 TO maxobj) AS Object2D, temp AS Object2D: temp = MyShip
 CSSetTimer 0, 35: PRT.ClearAllParts

 InMessage ("XGTXT04.TXT")
 IF exflag THEN MyShip = temp: EXIT SUB

 MyShip = temp
END SUB

SUB LMStart

 exflag = 0

 maxobj = 20

 DIM obj(1 TO maxobj) AS Object2D, temp AS Object2D
 temp = MyShip

 obj(1).Model = dIs(28)
 obj(1).Radius = 50
 obj(1).FillRadius = 20
 obj(1).Colour = 230
 obj(1).Scale = .75
 obj(1).X = 500
 obj(1).Y = 100

 CSSetTimer 0, 35

 PRT.ClearAllParts

 ScrollMessage ("XGTXT02.TXT")
 IF exflag THEN MyShip = temp: EXIT SUB
 
 GoRetro
 IF exflag THEN MyShip = temp: EXIT SUB

 cc = 194

 st# = TIMER
 DO
  IF obj(1).X > 160 THEN obj(1).X = obj(1).X - 2: DsDouble = -1: st# = TIMER

  DoShat

  O2D.Check obj(1)
  O2D.Draw obj(1)
  
  IF obj(1).X <= 160 THEN
   IF TIMER - st# < 4 THEN
     cc = cc + 1
     IF cc > 255 THEN cc = 255
    ELSE
     cc = cc - 1
   END IF
   Text.Colour = cc
   TX.Locate 13: TX.CPrint "Supply Outpost 7"
  END IF

  OldX = MyShip.X: OldY = MyShip.Y

  obj(1).Ang = obj(1).Ang + 1

  A = ((MyShip.Ang + MyShip.AngOff + 90 + 720) MOD 360)
  newx = MyShip.X + (MyShip.X - OldX) + ((MyShip.Radius * MyShip.Scale * .8) * Cosine(A))
  newy = MyShip.Y + (MyShip.Y - OldY) + ((MyShip.Radius * MyShip.Scale * .8) * Sine(A))
  R = RND: R = R - (R / 2)
  PRT.AddPart INT(newx), INT(newy), -((MyShip.X - OldX) / 5) + R, -((MyShip.Y - OldY) / 5) + R, 3, 10, 0, Red, 0
  AddLight INT(newx), INT(newy), 15, 32
  A = ((MyShip.Ang + MyShip.AngOff + 270 + 720) MOD 360)
  newx = MyShip.X + (MyShip.X - OldX) + ((MyShip.Radius * MyShip.Scale * .8) * Cosine(A))
  newy = MyShip.Y + (MyShip.Y - OldY) + ((MyShip.Radius * MyShip.Scale * .8) * Sine(A))
  R = RND: R = R - (R / 2)
  PRT.AddPart INT(newx), INT(newy), -((MyShip.X - OldX) / 5) + R, -((MyShip.Y - OldY) / 5) + R, 3, 10, 0, Red, 0
  AddLight INT(newx), INT(newy), 15, 32

  CSBoxF EMS.Layer, 0, 0, 319, 20, 0
  CSBoxF EMS.Layer, 0, 199 - 20, 319, 199, 0

  CSPcopy EMS.Layer, &HA000
  MyShip = obj(1)
  IF INKEY$ = CHR$(27) THEN MyShip = temp: EXIT SUB
 LOOP UNTIL cc = 193

 FOR I = 1 TO 4
  Enemys(I) = EnemyTemplate(5)
  Enemys(I).Used = -1
  Enemys(I).Status = 1
 NEXT I

 Enemys(1).Object.X = 0: Enemys(1).Object.Y = 0
 Enemys(2).Object.X = 319: Enemys(2).Object.Y = 0: Enemys(2).Object.Ang = 180
 Enemys(3).Object.X = 0: Enemys(3).Object.Y = 199
 Enemys(4).Object.X = 319: Enemys(4).Object.Y = 199: Enemys(4).Object.Ang = 180

 stah = 23

 st# = 0
 DO
  DoShat
  
  OldX = MyShip.X
  OldY = MyShip.Y

  obj(1).Ang = obj(1).Ang + 1

  O2D.Check obj(1)
  IF stah THEN O2D.Draw obj(1)

  FOR I = 1 TO MAXENEMYS
   Enemys(I).E3.C = -1
   IF stah = 0 THEN Enemys(I).E3.B = -1
   ENMY.DoAi I
  NEXT I
  
  IF stah THEN
   FOR I = 1 TO MAXENEMYPROJS
    IF EnemyProjs(I).ACTIVE THEN
     IF O2D.RadiusCollide(obj(1), EnemyProjs(I).Object) THEN
      CSCircle EMS.Layer, (obj(1).X), (obj(1).Y), (obj(1).Radius * 1.2), 150
      DS4QB.PlaySoundEx 2, CURRENT, CURRENT, CURRENT, CURRENT
      PRT.CircleExp (EnemyProjs(I).Object.X), (EnemyProjs(I).Object.Y), 10, 2, 0
      stah = stah - 1
      IF stah = 0 THEN
       DS4QB.PlaySoundEx 1, CURRENT, CURRENT, CURRENT, CURRENT
       PRT.CircleExp INT(obj(1).X), INT(obj(1).Y), 100, 2, 0
       A# = 0
       astep# = 360 / 10
       FOR shat = 1 TO 10
        A# = A# + astep#
        DEB.Add INT(obj(1).X), INT(obj(1).Y), INT(A#), INT((RND * 2) + 2), .8
       NEXT
       st# = TIMER
       EXIT FOR
      END IF
      EnemyProjs(I).ACTIVE = 0
     END IF
    END IF
   NEXT I
  END IF

  IF (TIMER - st#) > 6 AND st# THEN
   FOR j = 0 TO 255
    CSGetCol j, R, B, G
    R = R - 1: B = B - 1: G = G - 1
    IF R < 0 THEN R = 0
    IF B < 0 THEN B = 0
    IF G < 0 THEN G = 0
    CSSetCol j, R, B, G
   NEXT j
  END IF

  IF stah = 0 THEN obj(1).Y = obj(1).Y + .5

  DEB.MoveDraw
  
  MyShip = obj(1)

  IF stah THEN
   A = ((MyShip.Ang + MyShip.AngOff + 90 + 720) MOD 360)
   newx = MyShip.X + (MyShip.X - OldX) + ((MyShip.Radius * MyShip.Scale * .8) * Cosine(A))
   newy = MyShip.Y + (MyShip.Y - OldY) + ((MyShip.Radius * MyShip.Scale * .8) * Sine(A))
   R = RND: R = R - (R / 2)
   PRT.AddPart INT(newx), INT(newy), -((MyShip.X - OldX) / 5) + R, -((MyShip.Y - OldY) / 5) + R, 3, 10, 0, Red, 0
   AddLight INT(newx), INT(newy), 15, 32
   A = ((MyShip.Ang + MyShip.AngOff + 270 + 720) MOD 360)
   newx = MyShip.X + (MyShip.X - OldX) + ((MyShip.Radius * MyShip.Scale * .8) * Cosine(A))
   newy = MyShip.Y + (MyShip.Y - OldY) + ((MyShip.Radius * MyShip.Scale * .8) * Sine(A))
   R = RND: R = R - (R / 2)
   PRT.AddPart INT(newx), INT(newy), -((MyShip.X - OldX) / 5) + R, -((MyShip.Y - OldY) / 5) + R, 3, 10, 0, Red, 0
   AddLight INT(newx), INT(newy), 15, 32
  END IF
  PRJ.MoveAndDrawProjectiles EnemyProjs(), MAXENEMYPROJS

  CSBoxF EMS.Layer, 0, 0, 319, 20, 0
  CSBoxF EMS.Layer, 0, 199 - 20, 319, 199, 0
  
  CSPcopy EMS.Layer, &HA000
  IF INKEY$ = CHR$(27) THEN MyShip = temp: EXIT SUB
 LOOP UNTIL (TIMER - st#) > 12 AND st#

 InMessage ("XGTXT01.TXT")

 MyShip = temp

END SUB

SUB ScrollMessage (FileName AS STRING)

 exflag = 0

 DIM sText(101) AS STRING, I AS LONG

 CSClear EMS.Layer, 0
 PRT.ClearAllParts

 LOAD.InitVideo

 CSSetTimer 0, 35

 Handle = FREEFILE

 Count = 16

 OPEN "Text\" + FileName FOR INPUT AS #Handle
  DO
   Count = Count + 1
   INPUT #Handle, sText(Count)
  LOOP UNTIL EOF(Handle)
 CLOSE

 Count = (Count + 2) * 16

 Text.Colour = 255
 FOR I = 1 TO Count
  DoShat
  CM = (CM + 1) AND 1: I = I - CM
  FOR Y = 1 TO 26
   Text.y = ((INT(I / 16#) * 16#) - I) + (Y * 16#) - 16#
   tpos = INT(I / 16#) - 1 + Y
   IF tpos > 0 AND tpos < 101 THEN
    TX.CPrint LTRIM$(RTRIM$(sText(tpos)))
   END IF
  NEXT
  IF INKEY$ = CHR$(27) THEN exflag = -1: EXIT SUB
  CSPcopy EMS.Layer, &HA000
 NEXT

END SUB

