﻿

* = $0400
;-----------------------------------------------------------------------------------------------------
; SYS 2061 (PrepareGame)
; This launches the program from address $080d, i.e. PrepareGame.
;
; based on https://github.com/mwenge/llamaSource
; Released May 12, 2022
;-----------------------------------------------------------------------------------------------------
; $9E = SYS
; $32,$30,$36,$31,$00,$00 = 2071 ($080D in hex)
 !BYTE $00,$0B,$08,$0A,$00,$9E,$31,$30,$33,$37,$00,$00,$00

  LDA #12:
  STA 59468 ; upper case
 

; ===================================
; PRINT TITLE PAGE
; ==================================

 JSR title
 wait JSR $ffe4 
 CMP #$32
 BEQ joys1  ; joystick   
 CMP #$31
 BEQ kbd:  ; keyboard
 JMP  wait

 
 kbd LDA #0   ; use keyboard
 STA joy:
 JMP  xit
 joys1 LDA #1:   ; USE JOYSTICK
 STA joy

xit
; **** ZP ABSOLUTE ADRESSES **** 
;
currentXPosition     = $46
currentYPosition     = $47
currentCharacter     = $bd
screenLineLoPtr       = $01
screenLineHiPtr       = $02
clearScreenLineLoPtr = $3e
clearScreenLineHiPtr = $3f

; This will work on all PET models which have different keyboard scan values for run stop
lastKeyPressed    = $0223

podScreenLoPtr = $3e
podScreenHiPtr = $3f

;
; **** PREDEFINED LABELS **** 
;

GRID                          = $4c
LEFT_ZAPPER                   = $3e
BOTTOM_ZAPPER                 = $1e
SHIP                          = $58
BULLET_UP1                    = $f5
BOMB_DOWN                     = $2a
POD3                          = $b4  
POD6                          = $2a
DROID1                        = $51
EXPLOSION1                    = $5a
SPACE                         = $20
VERTICAL_LINE                 = $43
COLOR_RAM                     = $8800

WHITE                         = $01
RED                           = $02
CYAN                          = $03
BGREEN                         = $05
YELLOW                        = $07
ORANGE                        = $0C
LTGREEN                       = $0D
        JMP InitializeGame

      
;-------------------------------------------------------------------------
; CheckCurrentCharacterForShip
;-------------------------------------------------------------------------
CheckCurrentCharacterForShip
        JSR GetCharacterAtCurrentXYPos
        CMP #$58
        BEQ b8028
        RTS 

b8028   JMP JumpToDrawGridCharAtOldPosAndCheckCollisions

        
;---------------------------------------------------------------------------------
; CheckIfHasReachedBorder   
;---------------------------------------------------------------------------------
CheckIfHasReachedBorder   
        AND #$1F
        CMP #$18
        BPL b803A
j8036   STA explosionYPosArray,X
        RTS 

b803A   LDA previousYPosition
        JMP j8036

      
;-------------------------------------------------------------------------
; UpdateExplosionXPosArray
;-------------------------------------------------------------------------
UpdateExplosionXPosArray
        DEC explosionXPosArray,X
        LDA explosionXPosArray,X
        AND #$3F
        CMP #$27
        BPL b8050
        STA explosionXPosArray,X
        RTS 

b8050   LDA previousXPosition
        STA explosionXPosArray,X
        RTS 

;-------------------------------------------------------------------------
; UpdateExplosionYPosArray
;-------------------------------------------------------------------------
UpdateExplosionYPosArray
        DEC explosionYPosArray,X
        LDA explosionYPosArray,X
        JMP CheckIfHasReachedBorder

        
;---------------------------------------------------------------------------------
; WaitForScreen   
;
; GAME OVER HERE..................
;---------------------------------------------------------------------------------


WaitForScreen   

        jsr ishighscore       ; did we score in the charts?
        JMP DisplayTitleScreen

;-------------------------------------------------------------------------
; IncrementSelectedLevel
;-------------------------------------------------------------------------
IncrementSelectedLevel
        LDA selectedLevel
        CMP #$20
        BNE b80AA
        LDA #$01
        STA selectedLevel
b80AA   LDA #$30
        STA $80b9
        STA $80ba

        LDX selectedLevel
b80B4   INC $80ba
        LDA $80ba
        CMP #$3A
        BNE b80C6
        LDA #$30
        STA $80ba
        INC $80b9
b80C6   DEX 
        BNE b80B4

        LDX #$30
b80CB   JSR WasteSomeCycles
        DEX 
        BNE b80CB
        RTS 



;---------------------------------------------------------------------------------
; StartLevel   
;---------------------------------------------------------------------------------
StartLevel   
        LDA #$34 
        STA $8027
        LDX #$07
        LDA #$30
b80EE   STA $800e,X
        DEX 
        BNE b80EE
        JMP DisplayNewLevelInterstitial


;---------------------------------------------------------------------------------
; InitializeGame   
;---------------------------------------------------------------------------------
InitializeGame   
      
        LDA #$00
        TAX
        STA currentXPosition
        LDA #$80  
        STA currentYPosition
b8138   LDA currentXPosition
        STA screenLinesLoPtrArray,X
        LDA currentYPosition
        STA screenLinesHiPtrArray,X
        LDY #$00
        LDA #$20
b8146   STA (currentXPosition),Y
        INY 
        CPY #$28
        BNE b8146
        LDA currentXPosition
        CLC 
        ADC #$28
        STA currentXPosition
        LDA currentYPosition
        ADC #$00
        STA currentYPosition
        INX 
        CPX #$18
        BNE b8138

; intro show

   JSR DisplayGameBanner
   JMP JumpToDisplayTitleScreen


;-------------------------------------------------------------------------
; GetLinePtrForCurrentYPosition
;-------------------------------------------------------------------------
GetLinePtrForCurrentYPosition
        LDX currentYPosition
        LDY currentXPosition
        LDA screenLinesLoPtrArray,X
        STA screenLineLoPtr
        LDA screenLinesHiPtrArray,X
        STA screenLineHiPtr
        RTS 

;-------------------------------------------------------------------------
; WriteCurrentCharacterToCurrentXYPos
;-------------------------------------------------------------------------
WriteCurrentCharacterToCurrentXYPos
        JSR GetLinePtrForCurrentYPosition
        LDA currentCharacter
        STA (screenLineLoPtr),Y
        
        ; COLOR PET
        LDA screenLineHiPtr
        CLC 
        ADC #$08
        STA screenLineHiPtr
        LDA colorForCurrentCharacter
        STA (screenLineLoPtr),Y
        RTS 


;-------------------------------------------------------------------------
; GetCharacterAtCurrentXYPos
;-------------------------------------------------------------------------
GetCharacterAtCurrentXYPos
        JSR GetLinePtrForCurrentYPosition
        LDA (screenLineLoPtr),Y
        RTS 



gridXPos !byte 0
gridYPos !byte 0
;-------------------------------------------------------------------------
; DrawGrid
;-------------------------------------------------------------------------
DrawGrid
        LDA #$02
        STA gridXPos
        LDA #ORANGE
        STA colorForCurrentCharacter
        LDA #VERTICAL_LINE
        STA currentCharacter

        ; Draw the horizontal lines of the grid
DrawHorizontalLineLoop   
    

        LDA #$02
        STA gridYPos
b81BC   LDA gridYPos
        STA currentYPosition
        LDA gridXPos
        STA currentXPosition
        JSR WriteCurrentCharacterToCurrentXYPos
      
        
        INC gridYPos
        LDA gridYPos
        CMP #$16
        BNE b81BC
        JSR gridfx ; SOUND FX
       
        INC gridXPos
        LDA gridXPos
        CMP #$27
        BNE DrawHorizontalLineLoop

        ; Draw the full grid
        LDA #$02
        STA gridXPos
        LDA #GRID
        STA currentCharacter

DrawGridLoop
       LDA #$01
        STA gridYPos
b81F1   LDA gridYPos
        STA currentXPosition
        LDA gridXPos
        STA currentYPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        
        INC gridYPos;
        LDA gridYPos
        CMP #$27
        BNE b81F1

      
JSR gridfx
        INC gridXPos
        LDA gridXPos
        CMP #$16
        BNE DrawGridLoop

b821A   RTS 

;-------------------------------------------------------------------------
; CheckForPausePressed
;-------------------------------------------------------------------------
CheckForPausePressed
        
b8221   LDA 151
        CMP #3
        BEQ PAUSE
        CMP #4
        BEQ PAUSE
        LDA $223
        CMP #$04
        BEQ PAUSE
        RTS
 
 ; COMBINE BOTH 2001 AND 3000/4000 MODELS INTO ONE RUN STOP PAUSE CHECK       
PAUSE   
        LDA 151:CLC:ADC $223:STA P3
        LDA 151:CLC:ADC $223:CMP P3:BEQ PAUSE
        RTS



;-------------------------------------------------------------------------
; MaterializeShip
;-------------------------------------------------------------------------
MaterializeShip
        LDA #$0F
        STA materializeShipOffset
      
        LDA #EXPLOSION1
        STA currentCharacter
MaterializeShipLoop

        LDA #WHITE
        STA colorForCurrentCharacter
    
        LDA #$15
        STA currentYPosition
        LDA #$14
        CLC 
        SBC materializeShipOffset
        STA currentXPosition
        JSR WriteCurrentCharacterToCurrentXYPos
    
        LDA #$14
        CLC 
        ADC materializeShipOffset
        STA currentXPosition
       JSR WriteCurrentCharacterToCurrentXYPos
        
        LDA currentYPosition
        CLC 
        SBC materializeShipOffset
        STA currentYPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        
        LDA #$14
        STA currentXPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        
        LDA #$14
        CLC
        SBC materializeShipOffset
        STA currentXPosition
        JSR WriteCurrentCharacterToCurrentXYPos  
        
      
        LDA #EXPLOSION1
b82AE   STA currentExplosionCharacter
        
        ;LDX materializeShipOffset
        LDX #$02
b82B2   JSR MaybeWasteSomeCycles
        DEX 
        BNE b82B2
        
        LDA #ORANGE         
        STA colorForCurrentCharacter
        LDA #GRID
        STA currentCharacter
      
        LDA #$14
        STA currentXPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        LDA #$14
        CLC 
        SBC materializeShipOffset
        STA currentXPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        LDA #$14
        CLC 
        ADC materializeShipOffset
        STA currentXPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        LDA #$15
        STA currentYPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        LDA #$14
        CLC 
        SBC materializeShipOffset
        STA currentXPosition
      
        JMP DrawMaterializeShip


DrawNewLevelScreen   
        
        JSR DORND
        
        JSR DrawGrid
        JSR MaterializeShip
        LDA #$14
        STA previousXPosition
        LDA #$15
        STA previousYPosition
        LDA #$FF
        STA currentBulletYPosition
        
        LDA SEED
        STA bottomZapperXPosition
        LDA #$02
        STA leftZapperYPosition
        LDA #$04
        STA zapperFrameRate
        LDA #$0A
        STA laserAndPodInterval
       
        STA laserShootInterval
        JSR ClearPodArray
        LDA #$00
        STA backgroundSoundParm1
        STA backgroundSoundParm2
        STA noOfDroidSquadsCurrentLevel
        LDA #DROID1
        STA currentDroidCharacter
        LDA #$20
        STA a28
        LDA sizeOfDroidSquadForLevel
        STA sizeOfDroidSquadForLevel
        STA sizeOfDroidSquadForLevel
        
        LDA #$00
        STA a29
      
        JMP MainGameLoop


;-------------------------------------------------------------------------
; GetJoystickInput
;-------------------------------------------------------------------------
GetJoystickInput

        LDA #$00
        STA 59459
        LDA 59471:
        STA joystickInput
        RTS 

;-------------------------------------------------------------------------
; MaybeWasteSomeCycles
;-------------------------------------------------------------------------
MaybeWasteSomeCycles
        LDA materializeShipOffset
        CMP #$00
        BEQ b8392

;-------------------------------------------------------------------------
; WasteSomeCycles
;-------------------------------------------------------------------------
WasteSomeCycles
        LDA #$00
;---------------------------------------------------------------------------------
; WasteAFewCycles   
;---------------------------------------------------------------------------------
WasteAFewCycles   
        STA cyclesToWasteCounter
b838A   DEC cyclesToWasteCounter
        BNE b838A
b838E   DEC cyclesToWasteCounter
        BNE b838E
b8392   RTS 

;---------------------------------------------------------------------------------
; MainGameLoop   
;---------------------------------------------------------------------------------
MainGameLoop   

        JSR UpdateShipPosition
        JSR CheckForPausePressed

        JSR DrawBullet
        JSR UpdateZappersPosition
        JSR DrawLaser
        JSR UpdatePods
        JSR DrawUpdatedPods
        JSR DrawDroids
        JSR ResetAnimationFrameRate
        JSR CheckLevelComplete
        
  
        LDX #$14
b83EA   DEX 
        BNE b83EA
        JMP MainGameLoop



;---------------------------------------------------------------------------------
; DrawMaterializeShip   
;---------------------------------------------------------------------------------
DrawMaterializeShip   
        JSR WriteCurrentCharacterToCurrentXYPos
        LDA currentExplosionCharacter
        STA currentCharacter
        DEC materializeShipOffset
        LDA materializeShipOffset
        CMP #$FF
        BEQ b841A
       
        JMP MaterializeShipLoop

b841A   LDA #SHIP
        STA currentCharacter
       
        LDA #LTGREEN
        STA colorForCurrentCharacter      ; color
        
        LDA #$15
        STA currentYPosition
        LDA #$14
        STA currentXPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        JSR pfx
        JMP pfx
        

;-------------------------------------------------------------------------
; UpdateShipPosition
;-------------------------------------------------------------------------
UpdateShipPosition
        DEC shipAnimationFrameRate
        BEQ b8475
        RTS 

b8475   JSR GetJoystickInput

        LDA previousXPosition
        STA currentXPosition
        LDA previousYPosition
        STA currentYPosition
        JSR GetCharacterAtCurrentXYPos
        CMP #$58
        BEQ b848A
        JSR CheckOverlapWithBulletOrGrid
b848A   
        LDA #GRID
        STA currentCharacter
        LDA #ORANGE
        STA colorForCurrentCharacter      ; color
        
        JSR WriteCurrentCharacterToCurrentXYPos

; ===============================
; MOVEMENT READ JOYSTICK Or KEYBOARD
; ===================================

  LDA joy
  BEQ rx    ; read keyboard
  JMP  readjoy  ; read joystick    

    rx
    LDA #$02
    STA $E810   ; keyboard matrix
    LDA $E812
    STA valu
    AND #64     ; 7 key
    BNE +
    LDA #250:STA joystickInput
    + LDA valu
    AND #128        ; 9 key
    BNE +
    LDA #246:STA joystickInput
    +                     
    LDA #$04:STA $E810
    LDA $E812
    STA valu
    AND  #64     ; 4 key`
    BNE +
    LDA #251:STA joystickInput
    + LDA valu
  AND #128      ; 6 key
  BNE +
LDA #247:STA joystickInput
                           
+ LDA #$06:STA $E810    
LDA $E812
STA valu
AND #64             ; 1 KEY
BNE +
LDA #249:STA joystickInput  ; 1 KEY


+ LDA valu       ; 3 KEY  
AND #128
BNE +
LDA #245:STA joystickInput   ; 3 key

+ LDA #$07:STA $E810   ;  2 key
LDA $E812
AND #64
BNE +
LDA #253:STA joystickInput

+
LDA #$03:STA $E810
LDA $E812
STA valu
AND #64
BNE readjoy
LDA #254:STA joystickInput  ; up

readjoy
        LDA joystickInput
        AND #$01  ; 00000001 = up
        BNE b84A7
        DEC currentYPosition
        LDA currentYPosition
        CMP #$0E
        BNE b84A7
        LDA #$0F
        STA currentYPosition
b84A7   LDA joystickInput     ; down
        AND #$02 ; 00000010
        BNE b84B9
        INC currentYPosition
        LDA currentYPosition
        CMP #$16
        BNE b84B9
        LDA #$15
        STA currentYPosition
b84B9   LDA joystickInput      ; left
        AND #$04
        BNE b84CB
        DEC currentXPosition
        LDA currentXPosition
        CMP #$00
        BNE b84CB
        LDA #$01
        STA currentXPosition
b84CB   LDA joystickInput  ; right
        AND #$08
        BNE b84DD
        INC currentXPosition
        LDA currentXPosition
        CMP #$27
        BNE b84DD
        LDA #$26
        STA currentXPosition
b84DD   JSR GetCharacterAtCurrentXYPos
        BEQ b84E5
        JSR CheckIfBlockedByPod
b84E5   LDA currentXPosition
        STA previousXPosition
        LDA currentYPosition
        STA previousYPosition
        
        LDA #LTGREEN
        STA colorForCurrentCharacter  ; color
        
        LDA #SHIP
        STA currentCharacter
        JMP WriteCurrentCharacterToCurrentXYPos

;-------------------------------------------------------------------------
; DrawBullet
;-------------------------------------------------------------------------
DrawBullet
        DEC bulletAndLaserFrameRate
        BEQ b84FD
        RTS 

b84FD   LDA #$18
        STA bulletAndLaserFrameRate
        
        LDA currentBulletYPosition
        CMP #$FF
        BNE b8522
       
         
        LDA #$04:STA $E810
        LDA $E812
        AND #$01
        BEQ dofire ; 'a' button pressed for fire
        RTS
        
        dofire
        JSR firefx
        LDA previousXPosition
        STA currentBulletXPosition
        LDA previousYPosition
        STA currentBulletYPosition
        DEC currentBulletYPosition
        LDA #BULLET_UP1
        STA currentBulletCharacter
        LDA #$40
        STA bulletSoundControl
b8522   LDA currentBulletXPosition
        STA currentXPosition
        LDA currentBulletYPosition
        STA currentYPosition
        JSR GetCharacterAtCurrentXYPos
        CMP currentBulletCharacter
        BEQ b8538
        CMP #$4c 
        BEQ b8538
        JSR BulletCollidedWithPod
b8538   
        LDA #GRID
        STA currentCharacter
        LDA #ORANGE
        STA colorForCurrentCharacter  ; color
        
        JSR WriteCurrentCharacterToCurrentXYPos
 
        DEC currentBulletYPosition
        LDA currentBulletYPosition
        CMP #$02
        BNE b8558
        LDA #$FF
        STA currentBulletYPosition
        RTS 

b8558   LDA #BULLET_UP1
        STA currentBulletCharacter
        LDA currentBulletYPosition
        STA currentYPosition
        JSR GetCharacterAtCurrentXYPos
        BEQ b8568
        JSR BulletCollidedWithPod
b8568   LDA currentBulletCharacter
        STA currentCharacter
        
        LDA #WHITE
         STA colorForCurrentCharacter   ; color
        
         JMP WriteCurrentCharacterToCurrentXYPos



;-------------------------------------------------------------------------
; UpdateZappersPosition
;-------------------------------------------------------------------------
UpdateZappersPosition
        LDA shipAnimationFrameRate
        CMP #$01
        BEQ b85A2
b85A1   RTS 

b85A2   DEC zapperFrameRate
        BNE b85A1
        LDA #$02
        STA zapperFrameRate
        LDA #$00
        STA currentXPosition
        LDA leftZapperYPosition
        STA currentYPosition
        LDA #$20  ;SPACE
        STA currentCharacter
        JSR WriteCurrentCharacterToCurrentXYPos
        INC leftZapperYPosition
        LDA leftZapperYPosition
        CMP #$16
        BNE b85C5

        LDA #$03
        STA leftZapperYPosition
b85C5   LDA leftZapperYPosition
        STA currentYPosition
       
        LDA #WHITE
        STA colorForCurrentCharacter   ; color
        
        LDA #LEFT_ZAPPER
        STA currentCharacter
        JSR WriteCurrentCharacterToCurrentXYPos

        LDA #$16
        STA currentYPosition
        LDA bottomZapperXPosition
        STA currentXPosition
        LDA #$20
        JSR WriteAccumulatorToXYPos
        INC bottomZapperXPosition
        LDA bottomZapperXPosition
        CMP #$27
        BNE b85ED

        LDA #$01
        STA bottomZapperXPosition
b85ED   LDA bottomZapperXPosition
        STA currentXPosition
        LDA #BOTTOM_ZAPPER
        STA currentCharacter
        JMP DrawBottomZapperAndResetIfNecessary

;-------------------------------------------------------------------------
; WriteAccumulatorToXYPos
;-------------------------------------------------------------------------
WriteAccumulatorToXYPos
        STA currentCharacter
        JMP WriteCurrentCharacterToCurrentXYPos
    
;---------------------------------------------------------------------------------
; DrawBottomZapperAndResetIfNecessary   
;---------------------------------------------------------------------------------
DrawBottomZapperAndResetIfNecessary   
        JSR WriteCurrentCharacterToCurrentXYPos
        DEC laserAndPodInterval
        BEQ b8605
        RTS 

b8605   LDA laserFrameRate
        STA laserAndPodInterval
        JSR fx2
      
        LDA #$FF
        STA laserShootInterval
        LDA #$5c
        STA laserCurrentCharacter
        LDA leftZapperYPosition
        STA leftLaserYPosition
        LDA #$01
        STA leftLaserXPosition
        LDA #$15
        STA bottomLaserYPosition
        LDA bottomZapperXPosition
        STA bottomLaserXPosition
        RTS 

;-------------------------------------------------------------------------
; DrawLaser
;-------------------------------------------------------------------------
DrawLaser
        LDA bulletAndLaserFrameRate
        CMP #$05
        BEQ b863C
b863B   RTS 

b863C   LDA laserShootInterval
        CMP #$FF
        BNE b863B
        JSR UpdateLaserCharacter
        
       
        LDA #$d6
        STA laserCurrentCharacter

b864E   LDA #WHITE
        STA colorForCurrentCharacter  ; color
        LDA laserCurrentCharacter
        STA currentCharacter
        LDA #$15
        STA bottomLaserYPosition
b865A   LDA bottomLaserYPosition
        STA currentYPosition
        LDA bottomLaserXPosition
        STA currentXPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        DEC bottomLaserYPosition
        LDA bottomLaserYPosition
        CMP #$02
        BNE b865A
        
        LDA leftLaserYPosition
        STA currentYPosition
        LDA leftLaserXPosition
        STA currentXPosition
        JSR GetCharacterAtCurrentXYPos
        CMP laserCurrentCharacter
        BEQ b86A2
        LDA #GRID
        STA currentCharacter
        
       LDA #ORANGE
        STA colorForCurrentCharacter    ; color
        
        JSR WriteCurrentCharacterToCurrentXYPos
        INC leftLaserXPosition
        LDA leftLaserXPosition
        STA currentXPosition
        JSR CheckCurrentCharacterForShip
        CMP laserCurrentCharacter
        BEQ b86A2
        
        LDA #WHITE
        STA colorForCurrentCharacter    ; color
        
        LDA laserCurrentCharacter
        CLC 
        SBC #$01
        STA currentCharacter
        JMP WriteCurrentCharacterToCurrentXYPos

b86A2   LDA #$15
        STA currentYPosition
        LDA bottomLaserXPosition
        STA currentXPosition
        
        LDA #ORANGE
        STA colorForCurrentCharacter    ; color
        
        LDA #GRID
        STA currentCharacter
b86B2   JSR WriteCurrentCharacterToCurrentXYPos
        DEC currentYPosition
        LDA currentYPosition
        CMP #$02
        BNE b86B2
        LDA leftLaserYPosition
        STA currentYPosition
        
      LDA #YELLOW
        STA colorForCurrentCharacter
        LDA #POD3
        STA currentCharacter
        LDA #$00
        STA laserShootInterval
        JMP WriteCurrentCharacterToCurrentXYPos

;-------------------------------------------------------------------------
; UpdateLaserCharacter
;-------------------------------------------------------------------------
UpdateLaserCharacter
        DEC bulletAndLaserFrameRate
        INC laserCurrentCharacter
        LDA laserCurrentCharacter
        RTS 


;-------------------------------------------------------------------------
; UpdatePods
;-------------------------------------------------------------------------
UpdatePods 
        LDA laserAndPodInterval
        CMP #$05
        BEQ b86DE
        RTS 

b86DE   DEC laserAndPodInterval
        LDA #$80
        STA podScreenHiPtr
        LDA #$50
        STA podScreenLoPtr
        LDY #$00
b86EA   LDA (podScreenLoPtr),Y

        CMP #$4C
        BNE b86FB  ; not a grid object in scan of screen
       
b86EE   INC podScreenLoPtr
        BNE b86EA
        INC podScreenHiPtr
        LDA podScreenHiPtr
        CMP #$84
        BNE b86EA
        RTS           ; STOP SWEEPING FOR PODS ON SCREEN

b86FB   CMP #$20
        BEQ b86EE
   
      
        
        LDX #$04
b8701   CMP PodDecaySequence,X
        BEQ b870C
        DEX 
        BNE b8701
        JMP b86EE

b870C   CPX #$04
        BEQ b8719
        INX 
        LDA PodDecaySequence,X
        STA (podScreenLoPtr),Y
        JMP b86EE

b8719   JSR DrawBomb
        JMP b86EE



; This is the sequence in which the yellow pods decay before exploding.
; Each byte is a char representing a stage of decay, working from left to right.
PodDecaySequence
        !BYTE $00,$B4,$b3,$b2,$b1
    
;-------------------------------------------------------------------------
; DrawBomb
;-------------------------------------------------------------------------
DrawBomb 
        LDA #BOMB_DOWN
        STA (podScreenLoPtr),Y
        
        LDA podScreenLoPtr
        STA $6E
        LDA podScreenHiPtr
        CLC
        ADC #$08
        STA $6F
        
        LDA #ORANGE
        STA ($6E),y
        
        LDX #$18
b872E   LDA podHiPtrArray,X
        CMP #$FF
        BEQ b873D
        DEX 
        BNE b872E
     
        LDA #POD6
        STA (podScreenLoPtr),Y
        
        LDA podScreenLoPtr
        STA $6E
        LDA podScreenHiPtr
        CLC
        ADC #$08
        STA $6F
        LDA #ORANGE
        STA ($6E),y
        RTS 

b873D   LDA podScreenLoPtr
        STA podLoPtrArray,X
        LDA podScreenHiPtr
        STA podHiPtrArray,X
        RTS 

;-------------------------------------------------------------------------
; ClearPodArray
;-------------------------------------------------------------------------
ClearPodArray
        LDX #$20
        LDA #$FF
b874C   STA podHiPtrArray,X
        DEX 
        BNE b874C
        RTS 

;-------------------------------------------------------------------------
; DrawUpdatedPods
;-------------------------------------------------------------------------
DrawUpdatedPods
        DEC podUpdateRate
        BEQ b8758
        RTS 

b8758   LDA #$40
        STA podUpdateRate
        LDX #$18
b875E   LDA podHiPtrArray,X
        CMP #$FF
        BEQ b8768
        JSR DrawPods
b8768   DEX 
        BNE b875E
        RTS 

podLoPtr = currentXPosition
podHiPtr = currentYPosition
;-------------------------------------------------------------------------
; DrawPods
;-------------------------------------------------------------------------
DrawPods
        LDA podLoPtrArray,X
        STA podLoPtr
        LDA podHiPtrArray,X
        STA podHiPtr
        LDY #$00
        LDA (podLoPtr),Y
        CMP #SHIP
        BNE b8781
        JMP JumpToDrawGridCharAtOldPosAndCheckCollisions

b8781   
        LDA #GRID
        STA (podLoPtr),Y
      
        ; patch for color PET
        
        LDA podLoPtr
        STA $6E
        LDA podHiPtr
        CLC
        ADC #$08
        STA $6F
        
        LDA #ORANGE
        STA ($6E),y
        
        LDA podLoPtrArray,X
        CLC 
        ADC #$28
        STA podLoPtrArray,X
        LDA podHiPtrArray,X
        ADC #$00
        STA podHiPtrArray,X
        STA podHiPtr
        LDA podLoPtrArray,X
        STA podLoPtr
        
        LDA (podLoPtr),Y
        CMP #$1E
        BEQ OFF
        CMP #$20
        BNE b87B4
        OFF LDA #$FF
        STA podHiPtrArray,X
        RTS 

b87B4   CMP #SHIP
        BNE b87BB
        JMP JumpToDrawGridCharAtOldPosAndCheckCollisions

b87BB   LDA #$2a
        STA (podLoPtr),Y
        
        RTS 

;-------------------------------------------------------------------------
; BulletCollidedWithPod
;-------------------------------------------------------------------------
BulletCollidedWithPod
        LDX #$04
b87CD   CMP PodDecaySequence,X
        BEQ b87D9
        DEX 
        BNE b87CD
        JMP CheckIfBulletCollidedWithDroid

      
b87D9   DEX 
        BEQ b87EC
        LDA PodDecaySequence,X
        STA currentCharacter
        
       LDA #YELLOW
        STA colorForCurrentCharacter ; colorForCurrentCharacter
        LDA #$FF
        STA currentBulletYPosition
        JMP j8801

b87EC   LDA #GRID
        STA currentCharacter
        LDA #ORANGE
        STA colorForCurrentCharacter ; color
        
        LDA #$FF
        STA currentBulletYPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        JSR IncreaseScoreBy10Points
        PLA 
        PLA 
        RTS 

j8801   PLA 
        PLA 
        JMP WriteCurrentCharacterToCurrentXYPos

;-------------------------------------------------------------------------
; DisplayGameBanner
;-------------------------------------------------------------------------
DisplayGameBanner
        LDX #00
b8808   LDA screenHeaderText,X:
        STA $8000,X
        LDA screenHeaderColors,X
        STA COLOR_RAM ,X
        INX
        CPX #$28 
        BNE b8808
        
        LDX #$08
        LDY #$00
        - 
        LDA high,x
        STA $801b,y
        INX
        INY
        CPY #$07
        BNE -
        RTS 

screenHeaderText   

!byte $07,$12,$9,$4,$12,$15,$0e,$0e,$5,$12
!byte $20,$20,$10,$31,$20
 !byte $30,$30,$30,$30,$30,$30,$30,$20
 !byte $20,$08,$09,$20,$30,$30,$30,$30
 !byte $30,$30,$30,$20,$20,$58,$20,$20,$20

;---------------------------------------------------------------------------------
; IncrementPlayerScore   
;---------------------------------------------------------------------------------
IncrementPlayerScore   
        TXA 
        PHA 
b8872   INC $800f,X
        LDA $800f,X
        CMP #$3A
        BNE b8884
        LDA #$30
        STA $800f,X
        DEX 
        BNE b8872
b8884   PLA 
        TAX 
        DEY 
        BNE IncrementPlayerScore
        RTS 

;-------------------------------------------------------------------------
; IncreaseScoreBy10Points
;-------------------------------------------------------------------------
IncreaseScoreBy10Points
        LDX #$06
        LDY #$0A
        JSR IncrementPlayerScore
        LDA #$F0
        STA backgroundSoundParm1
        LDA #$03
        STA backgroundSoundParm2
        RTS 


;-------------------------------------------------------------------------
; DrawDroids
;-------------------------------------------------------------------------
DrawDroids
        DEC droidFrameRate
        BEQ b88CE
b88CD   RTS 

b88CE   LDA #$80
        STA droidFrameRate
        JSR UpdateDroidsRemaining
        LDA noOfDroidSquadsCurrentLevel
        BEQ b88CD
        TAX 
       
           
        LDA #DROID1
        STA currentDroidCharacter
DrawDroidsLoop
        STX currentDroidIndex

        ; Draw grid over the old position of the droid
        LDA droidXPositionArray,X
        STA currentXPosition
        LDA droidYPositionArray,X
        STA currentYPosition
      
        LDA #GRID
        STA currentCharacter
        LDA #ORANGE
        STA colorForCurrentCharacter  ; color
        
        JSR DrawDroidSegment

        LDX currentDroidIndex
        LDA droidStatusArray,X
        AND #$40
        BNE b892A

        ; Draw the droid segment at its new position.
        LDA droidXPositionArray - $01,X
        STA droidXPositionArray,X
        LDA droidYPositionArray - $01,X
        STA droidYPositionArray,X
        STA currentYPosition
        LDA droidXPositionArray,X
        STA currentXPosition
      
        LDA #DROID1
        STA currentCharacter
        LDA #10
        STA colorForCurrentCharacter 
        
        JSR WriteCurrentCharacterToCurrentXYPos

ResumeDrawingDroids
        LDX currentDroidIndex
        DEX 
        BNE DrawDroidsLoop
        RTS 

b892A   LDA droidStatusArray,X
        AND #$02
        BNE b8935
        INC currentXPosition
        INC currentXPosition
b8935   DEC currentXPosition
        JSR GetCharacterAtCurrentXYPos
       
        CMP #$4c
        BEQ b8990   ; on empty grid
       
        LDX currentDroidIndex
        JSR CheckForShipCollision
        STA currentXPosition
        INC currentYPosition
        LDA currentYPosition
        CMP #$16
        BNE b8962
        ; reached bottom
         
        LDA #$0E
        STA currentXPosition
        LDA #$02
        STA currentYPosition
        
        LDA droidStatusArray,X
        ORA #$01
        AND #$FD
        STA droidStatusArray,X
       
        JMP j896A

b8962   LDA droidStatusArray,X
        EOR #$03
        STA droidStatusArray,X

        ; Draw the droid segment
j896A   LDA currentXPosition
        STA droidXPositionArray,X
        LDA currentYPosition
        STA droidYPositionArray,X
        
        LDA #BGREEN
        STA colorForCurrentCharacter  ; color
        
        LDA currentDroidCharacter
        STA currentCharacter
        JSR WriteCurrentCharacterToCurrentXYPos
        LDX currentDroidIndex
        JMP ResumeDrawingDroids

        LDX #$01
        INC currentDroidCharacter
        RTS 

        LDX currentDroidIndex
        INX 
        CPX noOfDroidSquadsCurrentLevel
        RTS 

        NOP 
b8990   LDX currentDroidIndex
        JMP j896A

;-------------------------------------------------------------------------
; DrawDroidSegment
;-------------------------------------------------------------------------
DrawDroidSegment
        LDA droidStatusArray,X
        AND #$80
        BEQ b899F
        JMP WriteCurrentCharacterToCurrentXYPos

b899F   RTS 

;-------------------------------------------------------------------------
; ResetAnimationFrameRate
;-------------------------------------------------------------------------
ResetAnimationFrameRate
        LDA shipAnimationFrameRate
        CMP #$FF
        BNE b899F
        LDA #$80
        STA shipAnimationFrameRate
        RTS 

;-------------------------------------------------------------------------
; CheckIfBlockedByPod
;-------------------------------------------------------------------------
CheckIfBlockedByPod
        LDX #$07
b89AD   CMP PodDecaySequence,X
        BEQ b89B8
        DEX 
        BNE b89AD
        JMP CheckOverlapWithBulletOrGrid

b89B8   LDA previousXPosition
        STA currentXPosition
        LDA previousYPosition
        STA currentYPosition
        RTS 

;-------------------------------------------------------------------------
; UpdateDroidsRemaining
;-------------------------------------------------------------------------
UpdateDroidsRemaining
        LDA a29
        BNE b89E6
        DEC a28
        BEQ b8A0C
        RTS 

b89CA   LDA #$20
        STA a28
        LDA sizeOfDroidSquadForLevel
        STA a29
        INC noOfDroidSquadsCurrentLevel
        LDX noOfDroidSquadsCurrentLevel
        LDA #$0A
        STA droidXPositionArray,X
        LDA #$02
        STA droidYPositionArray,X
        LDA #$41
        STA droidStatusArray,X
        RTS 

b89E6   INC noOfDroidSquadsCurrentLevel
        LDX noOfDroidSquadsCurrentLevel
        LDA #$00
        STA droidStatusArray,X
        LDA #$02
        STA droidYPositionArray,X
        LDA #$0A
        STA droidXPositionArray,X
        DEC a29
        LDA a29
        CMP #$01
        BEQ b8A02
        RTS 

b8A02   DEC a29
        DEC droidsLeftToKill
        LDA #$80
        STA droidStatusArray,X
        RTS 

b8A0C   LDA droidsLeftToKill
        BNE b89CA
        RTS 

;---------------------------------------------------------------------------------
; CheckIfBulletCollidedWithDroid   
;---------------------------------------------------------------------------------
CheckIfBulletCollidedWithDroid   
        CMP #$51
        BEQ BulletColidedWithDroneDroid
       
        RTS 

BulletCollidedWithLeadDroid
        ; Increment score by 300 points
        LDX #$04
        LDY #$03
        JSR IncrementPlayerScore
        ;Returns

        
BulletColidedWithDroneDroid   
        ; Increment Score by 100 points
        LDX #$04
        LDY #$01
        JSR RegisterHit

        ; Figure out which drone droid the bullet hit.
        LDA #$FF
        STA currentBulletYPosition
        PLA 
        PLA 
        LDX noOfDroidSquadsCurrentLevel
b8A37   LDA droidXPositionArray,X
        CMP currentXPosition
        BEQ b8A42
b8A3E   DEX 
        BNE b8A37
        RTS 

b8A42   LDA droidYPositionArray,X
        CMP currentYPosition
        BNE b8A3E
        LDA droidStatusArray,X
        AND #$C0
        BNE b8A7A
        JSR UpdateDroidStatus

        ; Update droid arrays now that one has been killed
j8A53   LDA droidYPositionArray + $01,X
        STA droidYPositionArray,X
        LDA droidXPositionArray + $01,X
        STA droidXPositionArray,X
        LDA droidStatusArray + $01,X
        STA droidStatusArray,X
        CPX noOfDroidSquadsCurrentLevel
        BEQ b8A6D
        INX 
        JMP j8A53

b8A6D  LDA #YELLOW
        STA colorForCurrentCharacter  ; colorForCurrentCharacter
        
        LDA #POD3
        STA currentCharacter
        
        DEC noOfDroidSquadsCurrentLevel
        JMP WriteCurrentCharacterToCurrentXYPos

b8A7A   CMP #$C0
        BEQ j8A53
        CMP #$40
        BEQ b8A8D
        LDA droidStatusArray - $01,X
        ORA #$80
        STA droidStatusArray - $01,X
        JMP j8A53

b8A8D   LDA droidStatusArray + $01,X
        ORA droidStatusArray,X
        STA droidStatusArray + $01,X
        JMP j8A53

;-------------------------------------------------------------------------
; RegisterHit
;-------------------------------------------------------------------------
RegisterHit
        LDA #$F0
        STA backgroundSoundParm1
        LDA #$03
        STA backgroundSoundParm2
        JMP IncrementPlayerScore

;-------------------------------------------------------------------------
; UpdateDroidStatus
;-------------------------------------------------------------------------
UpdateDroidStatus
        STX currentDroidIndex
b8AA6   DEX 
        LDA droidStatusArray,X
        AND #$40
        BEQ b8AA6
        LDA droidStatusArray,X
        NOP 
        NOP 
        LDX currentDroidIndex
        JSR s8AC1
        LDA droidStatusArray - $01,X
        ORA #$80
        STA droidStatusArray - $01,X
        RTS 

;-------------------------------------------------------------------------
; s8AC1
;-------------------------------------------------------------------------
s8AC1
        ORA droidStatusArray + $01,X
        STA droidStatusArray + $01,X
        RTS 

;-------------------------------------------------------------------------
; CheckLevelComplete
;-------------------------------------------------------------------------
CheckLevelComplete
        LDA droidsLeftToKill
        BEQ b8ACD
b8ACC   RTS 

b8ACD   LDA noOfDroidSquadsCurrentLevel
        BNE b8ACC
        JMP DisplayNewLevelInterstitial
        ;Returns

;-------------------------------------------------------------------------
; Is this reached?
;-------------------------------------------------------------------------
        JSR GetCharacterAtCurrentXYPos
        CMP #SHIP
        BEQ JumpToDrawGridCharAtOldPosAndCheckCollisions
        JMP WriteCurrentCharacterToCurrentXYPos

;---------------------------------------------------------------------------------
; JumpToDrawGridCharAtOldPosAndCheckCollisions   
;---------------------------------------------------------------------------------
JumpToDrawGridCharAtOldPosAndCheckCollisions   
        LDX #$F6
        TXS 
        JMP DrawGridCharAtOldPosAndCheckCollisions

        RTS 

;-------------------------------------------------------------------------
; CheckForShipCollision
;-------------------------------------------------------------------------
CheckForShipCollision
        CMP #$58
        BEQ JumpToDrawGridCharAtOldPosAndCheckCollisions
        LDA droidXPositionArray,X
b8AF0   RTS 

       

;---------------------------------------------------------------------------------
; CheckForCollisions   
;---------------------------------------------------------------------------------
CheckForCollisions   
        LDA #$0F
        STA collisionSoundControl
        LDA previousXPosition
        LDX #$08
b8B00   STA explosionXPosArray,X
        DEX 
        BNE b8B00
        LDA previousYPosition
        LDX #$08
b8B0A   STA explosionYPosArray,X
        DEX 
        BNE b8B0A
      
        LDA #$5A  ; 5a but was 16
        STA currentShipExplosionCharacter
        ; Falls through

;---------------------------------------------------------------------------------
; PlayExplosion   
;---------------------------------------------------------------------------------
PlayExplosion   

        
        LDX #$08
        LDA #ORANGE
        STA colorForCurrentCharacter ; color
        
        LDA #GRID
        STA currentCharacter

b8B3D   LDA explosionXPosArray,X
        STA currentXPosition
        LDA explosionYPosArray,X
        STA currentYPosition
        STX currentDroidIndex
        JSR GetCharacterAtCurrentXYPos
        CMP currentShipExplosionCharacter
        BNE b8B53
        JSR WriteCurrentCharacterToCurrentXYPos
b8B53   LDX currentDroidIndex
        DEX 
        BNE b8B3D

        LDA #$14
b8B5A   JMP AnimateShipExplosion
        DEX 
        BNE b8B5A

;---------------------------------------------------------------------------------
; AnimateShipExplosion   
;---------------------------------------------------------------------------------
AnimateShipExplosion   
 

        LDA #EXPLOSION1
        STA currentShipExplosionCharacter

        LDX #$08
b8B6E   LDA explosionYPosArrayControl,X
        CMP #$80
        BEQ b8B7F
        CMP #$00
        BEQ b8B7C
        INC explosionXPosArray,X
b8B7C   INC explosionXPosArray,X

b8B7F   JSR UpdateExplosionXPosArray
        LDA explosionXPosArrayControl,X
        BEQ b8B8E

        CMP #$80
        BEQ b8B91

        INC explosionYPosArray,X
b8B8E   INC explosionYPosArray,X
b8B91   JSR UpdateExplosionYPosArray
        LDA explosionXPosArray,X
        STA currentXPosition
        LDA explosionYPosArray,X
        STA currentYPosition
        
        LDA #WHITE
        STA colorForCurrentCharacter  ; color
       
        LDA currentShipExplosionCharacter
        STA currentCharacter
        JSR GetCharAtCurrentPosition
       CMP #$4c: BNE b8BAE
        JSR WriteCurrentCharacterToCurrentXYPos
        JSR diefx                                                      
b8BAE   JMP MaybeRestartLevel


;---------------------------------------------------------------------------------
; PlayExplosionAndRestartLevel   
;---------------------------------------------------------------------------------
PlayExplosionAndRestartLevel   
        DEC collisionSoundControl
        BMI b8BB8
        JMP PlayExplosion

b8BB8   JMP ClearScreenAndRestartLevel

;-------------------------------------------------------------------------
; GetCharAtCurrentPosition
;-------------------------------------------------------------------------
GetCharAtCurrentPosition
        STX currentDroidIndex
        JMP GetCharacterAtCurrentXYPos

  
explosionYPosArrayControl  
        !byte $00,$01,$01,$01,$00,$80,$80,$80
explosionXPosArrayControl  
         !byte $80,$80,$00,$01,$01,$01,$00,$80
     
;---------------------------------------------------------------------------------
; MaybeRestartLevel   
;---------------------------------------------------------------------------------
MaybeRestartLevel   
        LDX currentDroidIndex
        DEX 
        BNE b8B6E
        LDX #$10
b8BD9   JSR WasteSomeCycles
        DEX 
        BNE b8BD9
        
        JMP PlayExplosionAndRestartLevel

;-------------------------------------------------------------------------
; CheckOverlapWithBulletOrGrid
;-------------------------------------------------------------------------
CheckOverlapWithBulletOrGrid
        CMP #$F5
        BEQ b8BFB
        CMP #$b4
        BEQ b8BFB  ; was 09
        CMP #$00
        BEQ b8BFB
        CMP #$4c:BEQ b8BFB
        JMP JumpToDrawGridCharAtOldPosAndCheckCollisions

b8BFB   RTS 

;-------------------------------------------------------------------------
; ClearScreen
;-------------------------------------------------------------------------
ClearScreen
        LDA #$50
        STA clearScreenLineLoPtr
        LDA #$80  ; $8050
        STA clearScreenLineHiPtr
        LDY #$00
b8C06   LDA #$20 ; SPACE
b8C08   STA (clearScreenLineLoPtr),Y
        INC clearScreenLineLoPtr
        BNE b8C08
        INC clearScreenLineHiPtr
        LDA clearScreenLineHiPtr
        CMP #$84
        BNE b8C06
        
        RTS 

;---------------------------------------------------------------------------------
; ClearScreenAndRestartLevel   
;---------------------------------------------------------------------------------
ClearScreenAndRestartLevel   
        JSR ClearScreen
      
        DEC $8027
        LDA $8027
        CMP #$30
        BEQ gover
        JMP UpdateLivesAndRestartLevel
gmsg 
!byte 7,1,13,5,$20,$f,$16,5,$12


;---------------------------------------------------------------------------------
; Game Over
;---------------------------------------------------------------------------------

gover

 LDX #$00
o9 LDA gmsg,x
   STA $819F,x
   LDA #$03
   STA COLOR_RAM + $019F,x
   INX
   CPX #$09
   BNE o9

; GAME OVER
LDA #$10:STA $e84b
LDA #85:STA 59466
LDA #50:STA tmp
dd3 LDA tmp:STA 59464
ldy #0:sty x1:sty x2

- INC x1:
  BNE -
  INC x2
  LDA x2:
  CMP #3
  BNE -
  INC tmp
  BNE dd3
  LDA #0:STA $E84B
  JMP WaitForScreen

;---------------------------------------------------------------------------------
; DisplayNewLevelInterstitial   
;---------------------------------------------------------------------------------
DisplayNewLevelInterstitial   

        LDX #$F6
        TXS 
        JSR ClearScreen
        LDX #$00
b8C35   LDA txtBattleStations,X
        STA $80fb,X
        
        LDA #$0E
        STA COLOR_RAM + $00FB,X
        
        LDA txtEnterGridArea,X
        STA $814B,X
        LDA #$01
        STA COLOR_RAM + $014B,X
        INX
       CPX #$12 
        BNE b8C35
        
        JMP PrepareNextLevel

txtBattleStations   
        !byte $20,$2,$01,$14,$14,$0c,$05,$20
        !byte $20,$13,$14,$01,$14,9,$0f,$0e
        !byte $13,$20,$20
txtEnterGridArea 
        !byte 5,$e,$14,5,$12,$20,7,$12,9,4,$20,1,$12,5,1,$20
        !byte $30,$30
;---------------------------------------------------------------------------------
; PrepareNextLevel   
;---------------------------------------------------------------------------------
PrepareNextLevel   
        INC $8027
        LDA $8027
        CMP #$3A
        BNE b8C82
        DEC $8027
b8C82   INC selectedLevel
        LDA selectedLevel
        CMP #$20
        BNE b8C8C
        DEC selectedLevel
b8C8C   LDX selectedLevel
        LDA noOfDroidSquadsForLevel,X
        STA droidsLeftToKill
        LDA sizeOfDroidSquadsForLevels,X
        STA sizeOfDroidSquadForLevel
        LDA laserFrameRateForLevel,X
        STA laserFrameRate

IncrementLevel 

        INC $815C
        LDA $815C
        CMP #$3A
        BNE b8CAF
        LDA #$30
        STA $815C
        INC $815B
b8CAF   DEX 
        BNE IncrementLevel
        JMP SetVolumeAndPlaySounds

;-------------------------------------------------------------------------
; SoundEffect
;-------------------------------------------------------------------------
SoundEffect
        STX currentDroidIndex
        LDA #$40
        ROR
        SBC currentDroidIndex
        STA $e848
        RTS 

noOfDroidSquadsForLevel   
        !byte $01,$02,$02,$03,$03,$03,$04,$04
        !byte $04,$04,$05,$05,$10,$06,$06,$06
        !byte $06,$06,$06,$06,$06,$06,$06,$06
        !byte $06,$06,$07,$07,$07,$07,$07,$07
sizeOfDroidSquadsForLevels    
        !byte $06,$06,$06,$07,$07,$08,$08
        !byte $09,$0C,$0C,$0A,$0A,$03,$0F,$10
        !byte $10,$11,$12,$13,$14,$14,$14,$15
        !byte $15,$16,$16,$16,$17,$03,$18,$18,$19
laserFrameRateForLevel    
        !byte $10,$10,$10,$0F,$0E,$0D,$0C
        !byte $0B,$0A,$09,$09,$09,$09,$09,$09
        !byte $09,$08,$08,$08,$08,$07,$07,$07
        !byte $07,$07,$07,$07,$07,$07,$06,$06
        !byte $05
    


;-------------------------------------------------------------------------
; Waste20Cycles
;-------------------------------------------------------------------------
Waste20Cycles
        LDA #$20
        JMP WasteAFewCycles

;---------------------------------------------------------------------------------
; JumpToDisplayTitleScreen   
;---------------------------------------------------------------------------------
JumpToDisplayTitleScreen   

        LDA selectedLevel
        CLC:ADC #$01
        STA selectedLevel
        JMP DisplayTitleScreen

;---------------------------------------------------------------------------------
; UpdateLivesAndRestartLevel   
;---------------------------------------------------------------------------------
UpdateLivesAndRestartLevel   
        DEC $8027
        DEC selectedLevel
        JMP DisplayNewLevelInterstitial

SetVolumeAndPlaySounds   
       
;---------------------------------------------------------------------------------
; PlayNewLevelSounds   
;---------------------------------------------------------------------------------
 
        LDA #$30
        STA soundEffectControl
LDA #$10:STA 59467
LDA #$f0:STA 59466

b8D1A   LDA soundEffectControl
        STA COLOR_RAM + $015B
        STA COLOR_RAM + $015C
        LDX soundEffectControl
b8D24   JSR Waste20Cycles
        JSR SoundEffect
       
        LDA $815B:eor #$80:STA $815B
        LDA $815C:eor #$80:STA $815C
        DEX 
        BNE b8D24
        DEC soundEffectControl:DEC soundEffectControl:
        
        BNE b8D1A
        LDA #$00:STA 59467:STA 59466
        
        LDA #0:STA dl1:STA dl2
        - inc dl1:BNE -:inc dl2:LDA dl2:CMP #210:BNE -
        JMP DrawNewLevelScreen

soundEffectControl !byte 0



;---------------------------------------------------------------------------------
; DrawGridCharAtOldPosAndCheckCollisions 
;---------------------------------------------------------------------------------
DrawGridCharAtOldPosAndCheckCollisions 
        LDA #GRID
        STA currentCharacter
        
        LDA #ORANGE
        STA colorForCurrentCharacter  ; color
        
        LDA previousXPosition
        STA currentXPosition
        LDA previousYPosition
        STA currentYPosition
        JSR WriteCurrentCharacterToCurrentXYPos
        JMP CheckForCollisions

;---------------------------------------------------------------------------------
; DisplayTitleScreen   
;---------------------------------------------------------------------------------
DisplayTitleScreen 

  LDX #$00  
  LDA #$20
  - STA $8028,X
  STA $8128,X
  STA $8228,x
  STA $8338-63,X
  INX
  BNE -
  
  LDA #$01
  STA selectedLevel
  LDX #$00
- LDA tit2,x
  BEQ JoystickInputLoop
  JSR $ffd2
  INX
  JMP  -

JoystickInputLoop
 
JSR showhi

LDA #$00:STA $9e ; CLEAR KEYS

l3
JSR $ffe4
CMP #$41  ; "a"
BEQ FirePressed
CMP #"+"
BEQ uplv
JMP  l3
  
uplv    INC selectedLevel
        JSR IncrementSelectedLevel
        JMP l3

FirePressed   
        DEC selectedLevel
        JMP StartLevel

 


; ================================
; SETUP SCREEN
; ==================================

title

LDA #147:
JSR $ffd2
LDA #<tx:
 STA $01
LDA #>tx:
STA $02

ldy #0:
v1 LDA ($01),y
BEQ end
JSR $ffd2:
inc $1:
BNE v1:
inc $2
 JMP  v1

; ==================
; ROM DETECT
; ==================

end LDA $c000
CMP #$1d:BEQ o1
CMP #$40:BEQ o2
CMP #$4c:BEQ o3
RTS


o1 LDA #<str1: STA $1:LDA #>str1:STA $2:LDA #1:STA model:JMP doprint
o2 LDA #<str2: STA $1:LDA #>str2:STA $2:LDA #2:STA model:JMP doprint
o3 LDA #<str3: STA $1:LDA #>str3:STA $2:LDA #3:STA model

doprint ldy #$00
w1 LDA ($01),y
BEQ end2
JSR $ffd2:iny: JMP  w1

end2 
   LDA #$0B
   LDX #$00
    - STA $8800,X
   STA $8900,X
   STA $8A00,X
   STA $8B00,X
   INX
   BNE -
 
LDA #<tx2:STA 1
LDA #>tx2:STA 2

ldy #0:v2 LDA ($01),y:BEQ end3
JSR $ffd2:inc $1:BNE v2:inc $2: JMP  v2

end3 RTS


firefx 
LDA #$10:STA $e84b
LDA #$0f:STA $e84a
LDA #$30:STA tmp
dd LDA tmp
 STA 59464
clc:adc #15:STA tmp
ldy #$00:sty x1:sty x2
- inc x1:BNE -:inc x2:LDA x2:CMP #3:BNE -
LDA tmp:CMP #$f0:bcc dd
LDA #$00:STA $e84b
RTS


fx2
LDA #$10:STA $e84b:LDA #$f0:STA $e84a
LDA #$01:f5 ldx #$e0:
f4 ldy #$50
- dey:BNE -
stx $e848
dex
BNE f4
tax
dex
txa
BNE f5

RTS

gridfx
sty aa
LDA #$10:STA 59467
LDA #$f0:STA 59466
ldy #0
ddd LDA val,y
STA $e848
- inc f5f:LDA f5f:CMP #70:BNE -
LDA #0:STA f5f
iny: cpy #16:BNE ddd
LDA #0:STA $e848
ldy aa
RTS

pfx LDA #$10:STA 59467
LDA #$f0:STA 59466
ldy #$10
ed3 LDA val2,y:STA $e848
- inc f5f:LDA f5f:BNE -:inc f6:LDA f6:CMP #2:BNE -
LDA #0:STA f5f:STA f6
dey
bpl ed3
LDA #0:STA $e848

RTS


diefx
LDA #$20:STA vll
ldx #$40
LDA #$10:
STA $e84b
LDA #$e3:
STA $e84a
v4 LDA vll
clc:adc #$08
STA vll

LDA vll:ASL
STA $e848:ROL $E848

LDA #0:STA fg

dex
BNE v4
LDA #$00
STA $e84b
RTS:




showhi

; ===================================
; SHOW HIGH SCORES
; ====================================
 LDX #$00
  LDA #$0D
  - STA $8828,x
  STA $8900,x
  STA $8A00,x
  STA $8B00,x
  inx
  bne -
  
  LDX #$00
 - LDA hbanner,x:
   BEQ +: 
   JSR $ffd2
   INX
   JMP  -
+
  LDA #$ec
  STA $1
  LDA #$81
  STA $2

ldx #$00
ld ldy #$00

lp LDA high,x:BEQ fi ; end of scores
AND #$3F
STA ($01),y:

non inx:iny
cpy #15:BNE lp

; next line
LDA $1:clc:adc #80:STA $1
LDA $2:adc #0:STA $2
 JMP  ld
fi rts

; ========================================
; DO WE GET A HIGH SCORE 
; =========================================


ishighscore
LDA #$00
STA POINTER
STA NOPUSH  ; DONT PUSH DOWN SCORES FLAG (USED IF YOU GET EXACT SAME SCORE AS HIGH)

- JSR $FFE4
BNE -

SORT LDX #$00
LDY POINTER
LDA VALS,Y
TAY
; SCAN TOP TO BOTTOM
lda high,y
- 
 LDA $800F,x  ;  GET SCORE FROM SCREEN RAM
 cmp high,y   ; COMPARE IT TO RANKS
 BEQ SKP   ; same digit
 BMI NOHI  ; no high 
 BCS YES
 SKP
 INX
 INY
 CPX #$7:
 BNE -
 INC NOPUSH
 JMP YES   ; SAME OR HIGHER

; =================================
; NO HIGH SCORE SO MOVE TO NEXT VALUE
; =================================
NOHI 
INC POINTER
LDY POINTER
CPY #$5
BNE SORT

RTS  ; no high score

high !text "MILA    0012560TPUG    0000400JEFF    0000300 C64    0000200 C64    0000100",0


POINTER !byte 0
VALS !byte 8,23,38,53,68
SROW !byte 0
STOP !byte 0

YES

; ========================
; PUSH DOWN SCORES
; ==========================
LDA NOPUSH
BNE DO

LDA POINTER   ; what position are we
CMP #$4
BEQ DO        ; lowest position so no sort needed
STA STOP      ; stop at this row
TAY

LDA #$03    ; select second last score
STA SROW      ; score row

 
REPEAT LDA SROW
    TAY
    LDA VALS,y
    SEC
    SBC #$08    ; push back to get name
    TAY

    LDX #$00
    - 
    LDA high,y
    sta high+15,y ; push down one row
    INX
    INY
    CPX #15
    BNE -

LDA SROW
CMP STOP
BEQ DO
DEC SROW
JMP REPEAT
 
DO 
 LDX #$00
  LDA #$09
  - STA $8828,x
  STA $8900,x
  STA $8A00,x
  STA $8B00,x
  inx
  bne -
  
 LDX #$00
   v6 LDA HMSG,X
     BEQ XX
     JSR $FFD2
    INX
    JMP v6

; =============================================
;  Enter name
; =============================================


XX

LDA #$00
STA SLEN    ; string length

KI JSR CURSOR     ; flash cursor
   JSR $FFE4
   BEQ KI
   CMP #$0D       ; return
   BEQ EXIT1
   CMP #20    ; delete
   BEQ DEL1 
   
   LDX #$00
   - CMP INPUT,x
   BEQ VALID
   INX
   CPX #44
   BNE -
   JMP KI     ; not a valid key
   
   
   DEL1
   LDA SLEN
   BEQ KI    ; nothing to delete
   DEC SLEN
   LDX SLEN
   INX
   LDA #$20
   STA $82BA,X
   JMP KI
   
   EXIT1 
   
; ===========================
; WRITE NAME & SCORE TO TABLE
; ===========================
LDY POINTER
LDA VALS,Y
TAX

; WRITE SCORE

LDY #$00
W1 LDA $800F,Y
CMP #$A0
BNE +
LDA #$20
+
STA high,X
INX
INY
CPY #$07
BNE W1   

; WRITE NAME
LDY POINTER
LDA VALS,Y
TAX
TXA
SEC
SBC #$08
TAX
LDY #$00
- LDA $82BA,Y
STA high,X
INX
INY
CPY #$04
BNE -
RTS

   VALID 
   
   and #$3f
   LDX SLEN
   CPX #4:BEQ +
   
   ; LITTLE JINGLE
   PHA
   JSR pfx
   PLA
   
   STA $82BA,X
   INC SLEN
   + JMP KI
   
   CURSOR INC DLA1   ; introduce delay
          BNE +
          INC DLA2
          LDA DLA2
          CMP #$2
          BNE +
          LDA #$00
          STA DLA2
          LDX SLEN
          
          LDA #$A0
          STA $82BA,x
          + RTS
;-------------------------------------------------------------------------
; Variables
;-------------------------------------------------------------------------

INPUT !text "0123456789+*-ABCDEFGHIJKLMNOPQRSTUVWXYZ "
SLEN !byte 0
DLA1 !byte 0
DLA2 !byte 0

HMSG !byte 19,13,13,13,13,13,13,13,13,13,13,13,13
!text "         ",18,"YOU GOT A HIGH SCORE!",146,13,13,13
!text "            ENTER YOUR NAME:",13,13,13
!text "                  ----",0

hbanner !byte 13,13
!text "           ",18,"-= TOP RUNNERS =-",146,13,13,0


screenLinesLoPtrArray         
!fill 90
screenLinesHiPtrArray
!fill 90

podLoPtrArray   
!fill 128
podHiPtrArray
!fill 128
droidXPositionArray 
!fill 128
droidYPositionArray
!fill 128
droidStatusArray
!fill 128

explosionXPosArray    
!fill 256
explosionYPosArray
!fill 256

currentExplosionCharacter     !byte 0
materializeShipOffset         !byte 0
previousXPosition             !byte 0
previousYPosition             !byte 0
shipAnimationFrameRate        !byte 0
joystickInput                 !byte 0
bulletAndLaserFrameRate       !byte 0
currentBulletXPosition        !byte 0
currentBulletYPosition        !byte 0
currentBulletCharacter        !byte 0
bulletSoundControl            !byte 0
zapperFrameRate               !byte 0
leftZapperYPosition           !byte 0
bottomZapperXPosition         !byte 0
laserAndPodInterval           !byte 0
laserShootInterval            !byte 0
laserCurrentCharacter         !byte 0
leftLaserYPosition            !byte 0
leftLaserXPosition            !byte 0
bottomLaserXPosition          !byte 0
bottomLaserYPosition          !byte 0
podUpdateRate                 !byte 0
backgroundSoundParm1          !byte 0
backgroundSoundParm2          !byte 0
noOfDroidSquadsCurrentLevel   !byte 0
droidFrameRate                !byte 0
currentDroidCharacter         !byte 0
currentDroidIndex             !byte 0
droidsLeftToKill              !byte 0
sizeOfDroidSquadForLevel      !byte 0
currentShipExplosionCharacter !byte 0
cyclesToWasteCounter          !byte 0
collisionSoundControl         !byte 0
laserFrameRate                !byte 0
selectedLevel                 !byte 0

a28 !byte 0
a29 !byte 0
colorForCurrentCharacter   !byte 0
fg !byte 0

dl1 !byte 0
dl2 !byte 0
valu !byte 0
model !byte 0
aa !byte 0:f5f !byte 0:f6 !byte 0
val !byte $0,$21,$25,$29,$2d,$31,$41,$5f,$7d,$21,$25,$29,$2d
val2 !byte $ff,$e1,$c3,$a5,$87,$69,$4b,$2d,$0f,$ff,$e1,$c3,$a5,$87,$69,$4b
vll !byte 0


str1 !text "ROM 2.0 (2001 SERIES)",0
str2 !text "ROM 3.0 (3000 SERIES)",0
str3 !text "ROM 4.0 (4000 SERIES)",0


tx !text "               GRIDRUNNER",8,13,13
!text "  CONVERTED TO THE C= PET BY MILASOFT",13
!text "         MILASOFT64@OUTLOOK.COM",13,13
!text "   COPYRIGHT JEFF MINTER (LLAMASOFT)",13,13
!text "         THIS GAME IS FREEWARE",13,13,13,18
!text "DETECTED: ",0

tx2 !byte 146,13,13

!text "        PRESS ",18,"1",146," FOR NUMERIC KEYS",13,13
!text  "     PRESS ",18,"2",146," TO USE JOYSTICK PORT 1",13,13
!byte 13,13
!byte 0

tit2 
!byte 19,13,13:!text "             BY JEFF MINTER",13,13
!text "             ENTER LEVEL 01",13,13
!text "     PRESS + TO ADJUST LEVEL NOW",13,13

!text "  (C) LLAMASOFT  PRESS FIRE TO BEGIN":!byte 0

joy !byte 0
tmp !byte 0
x1 !byte 0
x2 !byte 0


screenHeaderColors  
        !byte $03,$03,$03,$03,$04,$04,$04
        !byte $04,$04,$04,$01,$01,$07,$07,$01
        !byte $03,$03,$03,$03,$03,$03,$03,$01
        !byte $01,$07,$07,$01,$0E,$0E,$0E,$0E
        !byte $0E,$0E,$0E,$01,$01,$0D,$01,$01
        !byte $04    
        
 SEED !byte 0
 P3 !byte 0
 NOPUSH !BYTE 0 
;-------------------------------------------------------------------------
; Random Number
;------------------------------------------------------------------------- 
 DORND
 
LDX #$00
STX SEED
- LDA $E800,x
CLC
ADC SEED
STA SEED
INX
BNE -  
LDA SEED
AND #$23
STA SEED
CMP #$14    ; DONT ZAP SHIP COLUMN FIRST THING
BEQ DORND   ; FETCH ANOTHER VALUE
RTS
