;-----------------------------------------------------------;
;                                                           ;
; Invaded                                                   ;
; Version 1.0                                               ;
; Enemy AI Scripts                                          ;
;                                                           ;
;-----------------------------------------------------------;

;------------------------------------------------
; aiNone - Blank AI script
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiNone:
        ret

;------------------------------------------------
; aiZigZagLeft - Move in a zig zag sorta pattern to the left
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiZigZagLeft:
        dec     (ix+E_X)
        ld      hl,(aiCnt)
        ld      h,0
        ld      de,zigZagTable
        add     hl,de
        ld      a,(hl)
        add     a,(ix+E_Y)
        ld      (ix+E_Y),a
        ld      hl,aiCnt
        inc     (hl)
        ld      a,(hl)
        cp      ZIGZAGMAX
        ret     nz
        ld      (hl),0
        ret

;------------------------------------------------
; aiHoverUpDown - Hover up & down
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiHoverUpDown:
        ld      a,(ix+E_X)
        cp      64
        jr      c,doHoverUpDown
        dec     a
        ld      (ix+E_X),a
doHoverUpDown:
        ld      hl,(aiCnt)
        ld      h,0
        ld      de,hoverUpDownTable
        add     hl,de
        ld      a,(hl)
        add     a,(ix+E_Y)
        ld      (ix+E_Y),a
        ld      hl,aiCnt
        inc     (hl)
        ld      a,(hl)
        cp      HOVERUPDOWNMAX
        ret     nz
        ld      (hl),0
        ret

;------------------------------------------------
; aiNormalShoot - Shoot at the player
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiNormalShoot:
        ld      b,50
        call    ionRandom
        cp      45
        ret     c
        ld      bc,1*256+255                    ; Create bullet of type 1, calculate angle
        jp      newEBullet

;------------------------------------------------
; aiMoveLeft - Simply move left
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiMoveLeft:
        dec     (ix+E_X)
        ret

;------------------------------------------------
; aiPowerShootLeft - Shoot powerful bullet to the left
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiPowerShootLeft:
        ld      hl,aiCnt
        inc     (hl)
        ld      a,(hl)
        and     $0F
        ret     nz
        ld      bc,2*256+14                     ; Create bullet of type 2 that moves left
        jp      newEBullet

;------------------------------------------------
; aiDownThenLeft - Move down to the center of the screen then move left
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiDownThenLeft:
        ld      a,(frame)
        and     $03
        jr      nz,doDownThenLeft
        dec     (ix+E_X)
doDownThenLeft:
        ld      hl,aiCnt
        ld      a,(hl)
        cp      DOWNLEFTMAX
        jr      nc,downThenLeftMoveLeft
        ld      c,(hl)
        inc     (hl)
        ex      de,hl
        ld      b,0
        ld      hl,downThenLeftTable
        add     hl,bc
        ld      a,(hl)
        add     a,(ix+E_Y)
        ld      (ix+E_Y),a
        ex      de,hl
downThenLeftMoveLeft:
        ld      a,(hl)
        cp      DOWNLEFTMOVELEFT
        ret     c
        dec     (ix+E_X)
        ret

;------------------------------------------------
; aiDiagonalUL - Move diagonally up and left
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiDiagonalUL:
        dec     (ix+E_Y)
        dec     (ix+E_X)
        ret

;------------------------------------------------
; aiDiagonalUR - Move diagonally up and right
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiDiagonalUR:
        dec     (ix+E_Y)
        inc     (ix+E_X)
        ret

;------------------------------------------------
; aiDiagonalDL - Move diagonally down and left
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiDiagonalDL:
        inc     (ix+E_Y)
        dec     (ix+E_X)
        ret

;------------------------------------------------
; aiDiagonalDR - Move diagonally down and right
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiDiagonalDR:
        inc     (ix+E_Y)
        inc     (ix+E_X)
        ret

;------------------------------------------------
; aiUpDownFollowPlayer - Move up and down whilst following player horizontally
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiUpDownFollowPlayer:
        ld      hl,aiCnt
        bit     1,(hl)
        jr      z,doUpDownFPIni
        ld      a,(pScreenX)
        cp      (ix+E_X)
        jr      z,doUpDown
        jr      nc,upDownFPMoveRight
        dec     (ix+E_X)
        jr      doUpDown
upDownFPMoveRight:
        inc     (ix+E_X)
doUpDown:
        bit     0,(hl)
        jr      z,doUpDownMoveDown
        dec     (ix+E_Y)
        ld      a,(ix+E_Y)
        cp      8
        ret     nc
        ld      (hl),$02
        ret
doUpDownMoveDown:
        inc     (ix+E_Y)
        ld      a,(ix+E_Y)
        cp      56-16
        ret     c
        ld      (hl),$03
        ret
doUpDownFPIni:
        dec     (ix+E_Y)
        dec     (ix+E_Y)
        ld      a,(ix+E_Y)
        cp      8
        jr      c,upDownFPIniDone
        cp      240
        ret     c
upDownFPIniDone:
        ld      (hl),$02
        ret

;------------------------------------------------
; aiLeftWithLevel - Move left in sync with level scrolling
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiLeftWithLevel:
        ld      a,(scrolled)
        or      a
        ret     z
        ld      a,(aiCnt)
        or      a
        jr      z,firstLeftWithLevel
        dec     (ix+E_X)
        ret
firstLeftWithLevel:
        inc     a
        ld      (aiCnt),a
        ret

;------------------------------------------------
; ai3WayPowerball - Shoot Powerball bullet in 3 directions
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
ai3WayPowerball:
        ld      hl,aiCnt
        inc     (hl)
        ld      a,(hl)
        and     $0F
        ret     nz
        ld      bc,3*256+14                     ; Create bullet of type 2 that moves left
        call    newEBullet
        ld      bc,3*256+10
        call    newEBullet
        ld      bc,3*256+4
        jp      newEBullet

;------------------------------------------------
; aiRandomUpPowerball - Shoot Powerballs in random up directions
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiRandomUpPowerball:
        ld      hl,aiCnt
        inc     (hl)
        ld      a,(hl)
        and     $0F
        ret     nz
        ld      b,7
        call    ionRandom
        add     a,6
        ld      c,a
        ld      b,3
        jp      newEBullet

;------------------------------------------------
; aiShootThroughWalls - Shoot at the player with bullets that go through walls
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiShootThroughWalls:
        ld      b,50
        call    ionRandom
        cp      45
        ret     c
        ld      bc,4*256+255                    ; Create bullet of type 1, calculate angle
        jp      newEBullet

;------------------------------------------------
; aiHoverHorizontallyFL - Hover left and right after entering from the left of screen
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiHoverHorizontallyFL:
        ld      hl,aiCnt
        ld      a,(hl)
        or      a
        jr      z,hhFLRight
        dec     (ix+E_X)
        ld      a,(ix+E_X)
        cp      8
        ret     nc
        ld      (hl),0
        ret
hhFLRight:
        inc     (ix+E_X)
        ld      a,(ix+E_X)
        cp      96-16
        ret     c
        cp      220
        ret     nc
        ld      (hl),1
        ret

;------------------------------------------------
; aiHoverHorizontallyFR - Hover left and right after entering from the right of screen
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiHoverHorizontallyFR:
        ld      hl,aiCnt
        ld      a,(hl)
        or      a
        jr      z,hhFRLeft
        inc     (ix+E_X)
        ld      a,(ix+E_X)
        cp      96-16
        ret     c
        ld      (hl),0
        ret
hhFRLeft:
        dec     (ix+E_X)
        ld      a,(ix+E_X)
        cp      8
        ret     nc
        ld      (hl),1
        ret

;------------------------------------------------
; aiShootTwinLasers - Shoot Twin Laser bullets left
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiShootTwinLasers:
        ld      b,50
        call    ionRandom
        cp      45
        ret     c
        ld      bc,5*256+14
        jp      newEBullet

;------------------------------------------------
; aiFollowPlayer - Follow the player
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiFollowPlayer:
        ld      a,(ix+E_X)
        cp      220
        jr      nc,followPlayerRight
        ld      hl,pScreenX
        cp      (hl)
        jr      z,followPlayerY
        jr      nc,followPlayerLeft
followPlayerRight:
        inc     (ix+E_X)
        jr      followPlayerY
followPlayerLeft:
        dec     (ix+E_X)
followPlayerY:
        ld      a,(ix+E_Y)
        cp      220
        jr      nc,followPlayerDown
        ld      hl,y
        cp      (hl)
        ret     z
        jr      nc,followPlayerUp
followPlayerDown:
        inc     (ix+E_Y)
        ret
followPlayerUp:
        dec     (ix+E_Y)
        ret

;------------------------------------------------
; aiMoveLeftFP - Move left and follow player in terms of Y coord
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiMoveLeftFP:
        dec     (ix+E_X)
        ld      a,(frame)
        and     $01
        ret     nz
        ld      a,(y)
        cp      (ix+E_Y)
        ret     z
        jr      nc,mlFPDown
        dec     (ix+E_Y)
        ret
mlFPDown:
        inc     (ix+E_Y)
        ret

;------------------------------------------------
; aiUpThenLeft - Move up to the center of the screen then move left
;
; Input:    IX => Entry of enemy in enemyArray
; Output:   None
;------------------------------------------------
aiUpThenLeft:
        ld      a,(scrolled)
        or      a
        jr      z,doUpThenLeft
        dec     (ix+E_X)
doUpThenLeft:
        ld      hl,aiCnt
        ld      a,(hl)
        cp      UPLEFTMAX
        jr      nc,upThenLeftMoveLeft
        ld      c,(hl)
        inc     (hl)
        ex      de,hl
        ld      b,0
        ld      hl,upThenLeftTable
        add     hl,bc
        ld      a,(hl)
        add     a,(ix+E_Y)
        ld      (ix+E_Y),a
        ex      de,hl
upThenLeftMoveLeft:
        ld      a,(hl)
        cp      UPLEFTMOVELEFT
        ret     c
        dec     (ix+E_X)
        ret

.end
