;================================================================
; [stage2.s] - (c) kr Software '05
;
;   This file contains "mostly everything" stage 2.
;   's1boss.s' got kinda big eventually so for my own sake
;   (and sanity) I've put the code for stage2 into this file.
;
;  All code herein is part of bank $12 (18).
;================================================================
;
; The mighty list of variables a.k.a what-where-huh?!?
; ----=============================================-----
;
; ENM_13_numfr     - used by the ball 'n chain parser
;
; ENM_1*_f*        - used for the level obstrive zones
; ENM_1*_patt
;
; freeForAll*      - constantly (ab)used by the mistress of the game -.-;;
;
; shipAnim         - IS_STANDING (bool)
; shipLastDir      - HITTING_LEFT (bool)
; shipVel          - HITTING_RIGHT (bool)
; shipAdvNumPix    - HITTING_BELOW (bool)
;
; shipAdvHalfFlag  - IS_JUMPING
; dec_vel_dir      - HAS_LANDED
; areWeAtFirstBoss - JUMP_VAL
;
; BUL_21_alive     - do not use!!! buggy for some reason
; BUL_21_time      - ditto
; BUL_21_delay     - ditto
; BUL_21_dir       - ditto
;
; ENM_13_numfr     - commonly used by the ball 'n chain parser
; ENM_14_numfr     - almighty all-purpose ca.., err, variable ;)
; ENM_15_numfr     - ditto
; ENM_16_numfr     - ditto
; ENM_17_numfr     - ditto (used in room03)
; ENM_18_numfr     - ditto (used in room03)
;================================================================


STAGE2TESTLESTEN

    jsl bH12_INIT_VIDEO_03
    lda #$80
    sta $2100
    lda #%00010011
    sta $212c

    LOAD_PAL S2_LEVEL_PAL
    wai
    LOAD_BLOCK2VRAM S2_ROOM_01_MAP,$0000,$2000
    wai
    wai
    LOAD_BLOCK2VRAM S2_ROOM_01_PIC,$1000,$0800
    wai
    wai
    LOAD_BLOCK2VRAM FONT2_TILES,$2000,$1800
    wai
    wai
    LOAD_BLOCK2VRAM S2_SPRSET1_PIC,$6000,$2000
    wai
    wai

    jsl TOTALLY_RESET_32x32_SPRITES
    jsl INITIALIZE_HUD_PAL

    jsl bH0_SPLSC_CLR
    jsl CUT_PLYFLD
    jsl bH13_BLACK_SIDES_MKAY
    jsl WRITE_STATUSBAR_LV1
    jsl ADD_BLACK_SIDES_MKAY
    jsl WRITE_FOOBAR_LV1
    jsl UPDATE_SCORE
    jsl WRITE_LIVES_REMAINING
    jsl WRITE_HP_REMAINING
    wai

    ldx #$0000
    stx MAPX
    ldx #$ffff
    stx MAPY

    jsr S2_ROOM01_SETUP

    lda #54
    sta SPRBUF1+SX
    lda #181
    sta SPRBUF1+SY
    lda #%00110100             ; no flips, max prior, spr.pal #2
    sta SPRBUF1+SPRIORITY
    stz SPRBUF1+SFRAME

    jsr S2_RECALC_ANG_X
    jsr S2_RECALC_ANG_Y
    jsr S2_RECALC_ANG_X2
    jsr S2_RECALC_ANG_Y2



;-----------------------
    stz ENM_13_hp              ; angela frame number
    stz ENM_14_hp              ; angela walk delay
    stz ENM_15_hp              ; angela duck frame #1 lockout
    stz ENM_16_hp              ; ball 'n chain interval

    stz a_byte                 ; "floating point" helper for Angela falling

    stz GOOD_ENDING            ; doesn't have anything to do with ending :-/
;-----------------------

    ; ball 'n chain setup
;   125,94 (innerste lenke) spr4
;   125,110 spr3
;   125,126 spr2
;   117,142 (ballen) spr1
.ENDASM
    ; THIS OUTCOMMENTED CLAUSE OF CODE SHOWS HOW TO
    ; PRESET THE BALL'N'CHAIN SPRITES POSITIONS

    lda #117
    sta SPRBUF1+SX1
;   lda #136 ; this does for a more addams/mario like swing, but
    lda #130 ; this one makes it look much more professional ;-)
    sta SPRBUF1+SY1
    lda #125
    sta SPRBUF1+SX2
    sta SPRBUF1+SX3
    sta SPRBUF1+SX4
    lda #125
    sta SPRBUF1+SY2
    lda #110
    sta SPRBUF1+SY3
    lda #94
    sta SPRBUF1+SY4

    lda #%00110010
    sta SPRBUF1+SPRIORITY1
    sta SPRBUF1+SPRIORITY2
    sta SPRBUF1+SPRIORITY3
    sta SPRBUF1+SPRIORITY4
    lda #$4c
    sta SPRBUF1+SFRAME1
    lda #$80
    sta SPRBUF1+SFRAME2
    sta SPRBUF1+SFRAME3
    sta SPRBUF1+SFRAME4
.ASM

    ; 1st time setup for room01
    ; sprites enabled: 0
    lda #%11111100
    sta SPRBUF2

    lda #$0f                   ; screen on, full brightness
    sta $2100

    ldy #$0000                 ; ball 'n chain data pointer
                               ; DO NOT USE THIS REGISTER TO ANYTHING
                               ; OTHER THAN THE BALL 'n CHAIN ROUTINE
                               ; OR ELSE YOU'LL GET FUNNY RESULTS =]
    jmp S2_testeLoopen

;=======================

s2tstLpn_r03dechu
    jmp S2_ACTION_SCRIPT_r03
    jmp s2tstLpn_actionErUnnagjort
s2tstLpn_r02dechu
    jmp S2_ACTION_SCRIPT_r02
    jmp s2tstLpn_actionErUnnagjort
s2tstLpn_r01dechu
    jmp S2_ACTION_SCRIPT_r01
S2_ACTION_SCRIPT_unnagjort
    jmp s2tstLpn_actionErUnnagjort

;------------

S2_testeLoopen
    wai

;   lda LEVEL_WAVE             ; process whatever it takes to make the current
;   cmp #$03                   ; room anything near game-like.
;   beq s2tstLpn_r03dechu

;s2tstLpn_actionErUnnagjort

    jsr S2_PROCESS_INPUT
    jsr S2_DRAW_ANGELA


    lda LEVEL_WAVE             ; process whatever it takes to make the current
    cmp #$03                   ; room anything near game-like.
    beq s2tstLpn_r03dechu
    cmp #$02
    beq s2tstLpn_r02dechu
    cmp #$01
    beq s2tstLpn_r01dechu

s2tstLpn_actionErUnnagjort

    jsr S2_my_BOXCOLL_LEFTSIDE
    jsr S2_my_BOXCOLL_RIGHTSIDE

    lda shipAdvHalfFlag        ; skip my_BOXCOLL_TOP if IS_JUMPING = TRUE
    cmp #$01
    beq s2_skipTopChk
    jsr S2_my_BOXCOLL_TOP

s2_skipTopChk

    lda shipAnim               ; IS_STANDING?
    cmp #$00
    bne s2_skipFalling
    stz dec_vel_dir            ; HAS_LANDED = FALSE
    inc SPRBUF1+SY


    ; The following clause of code is to Angela fall more "believable". :)
    ; err, that prolly didn't make sense. Simply put; it makes her fall
    ; a little quicker (+0.5).
.ENDASM
    lda shipAdvHalfFlag        ; skip this clause of code if IS_JUMPING
    cmp #$01
    beq s2_skipFalling
    lda a_byte                 ; don't proceed further through this clause
    cmp #$01                   ; if a_byte != 1.
    bne s2_skipFalling
    inc SPRBUF1+SY
.ASM

s2_skipFalling

    lda areWeAtFirstBoss       ; reached maximum JUMP_VAL?
    cmp #32                    ; if > skip the jumping part
    bpl s2_notJmpng

    lda shipAdvHalfFlag        ; if IS_JUMPING then
    cmp #$01                   ; decrease SPRBUF1+SY by 3
    bne s2_endOfMainLoop

    lda areWeAtFirstBoss       ; this makes for a smooth-fluid-
    cmp #32                    ; defying-gravity kinda max jump ;)
    beq s2_skpFlng_decBy1
    cmp #31
    beq s2_skpFlng_decBy1
    cmp #30
    beq s2_skpFlng_decBy1
    cmp #29
    beq s2_skpFlng_decBy1
    cmp #28
    beq s2_skpFlng_decBy1
    cmp #27
    beq s2_skpFlng_decBy2
    cmp #26
    beq s2_skpFlng_decBy2
    cmp #25
    beq s2_skpFlng_decBy2
    cmp #24
    beq s2_skpFlng_decBy2
;s2_skpFlng_decBy3
    dec SPRBUF1+SY
s2_skpFlng_decBy2
    dec SPRBUF1+SY
s2_skpFlng_decBy1
    dec SPRBUF1+SY
    inc areWeAtFirstBoss       ; JUMP_VAL++

    jsr S2_my_BOXCOLL_BELOW
    lda shipAdvNumPix          ; if HITTING_BELOW = TRUE
    cmp #$00                   ; then set IS_JUMPING to FALSE
    beq s2_endOfMainLoop
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE

    
s2_notJmpng
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE


s2_endOfMainLoop

    jsr S2_CHECK_ROOM_EXITS    ; check if Angela wants to leave the room

    lda LEVEL_WAVE             ; does the current room have a ball'n'chain?
    cmp #$02
    bne S2_tstLoopn_forts

    inc ENM_16_hp              ; ball 'n chain interval
    lda ENM_16_hp
    cmp #$02                   ; time for an update?
    bne S2_tstLoopn_forts      ; branch if now is not the time
    jsr S2_BALL_n_CHAIN_UPDATE
    stz ENM_16_hp

S2_tstLoopn_forts


    lda ENM_18_numfr           ; if(SECTION1_VERTTIME>0) --SECTION1_VERTTIME
    cmp #$00
    beq extraForR03_endeligWendelig
    dec ENM_18_numfr
    jmp extraForR03_endeligWendelig2

extraForR03_endeligWendelig
    stz ENM_17_numfr           ; SECTION1_VERTLOCK = FALSE
extraForR03_endeligWendelig2
    jmp S2_testeLoopen


;==============================


S2_PROCESS_INPUT

    inc a_byte
    lda a_byte
    cmp #$02
    bne s2_prcsInpt_aByteNotReset
    stz a_byte
s2_prcsInpt_aByteNotReset

;   if(HAS_LANDED) {
;       if(key[KEY_LCONTROL]) {
;           IS_JUMPING = TRUE;
;           HAS_LANDED = FALSE;
;           IS_STANDING = FALSE;
;       }
;   }
;   if(!key[KEY_LCONTROL])
;       IS_JUMPING = FALSE;

    lda dec_vel_dir            ; does HAS_LANDED
    cmp #$01                   ; equal TRUE?
    bne s2_prcsInpt_notLanded  ; branch if FALSE

    lda joy1+1                 ; check key B
    and #$80
    beq s2_prcsInpt_notLanded  ; branch if not pressed

    ; if key B is pressed do:
    lda LEVEL_WAVE
    cmp #$03
    bne s2_prcsInpt_r03hackNotNecessary
    stz ENM_17_numfr

s2_prcsInpt_r03hackNotNecessary
    lda #$01
    sta shipAdvHalfFlag        ; IS_JUMPING = TRUE
    stz dec_vel_dir            ; HAS_LANDED = FALSE
    stz shipAnim               ; IS_STANDING = FALSE

s2_prcsInpt_notLanded

    lda joy1+1
    and #$80
    beq s2_prcsInpt_setJumpFalse
    jmp s2_prcsInpt_notKeyB
s2_prcsInpt_setJumpFalse
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE



s2_prcsInpt_notKeyB
;==================
    lda joy1+1                 ; check down
    cmp #$04
    bne s2_prcsInpt_notDown

    lda ENM_15_hp
    cmp #$05
    beq s2_prcsInpt_df1locked
    inc ENM_15_hp
    lda #$06
    sta ENM_13_hp
    jmp s2_prcsInpt_ende
s2_prcsInpt_df1locked
    lda #$07
    sta ENM_13_hp              ; set angela ducking frame #2/2
    jmp s2_prcsInpt_ende


s2_prcsInpt_notDown
;==================
    stz ENM_15_hp              ; uncheck angela duck #1 frame lockout

    lda shipVel                ; if(HITTING_RIGHT) goto notLeft
    cmp #$01
    beq s2_prcsInpt_notLeft

    lda joy1+1                 ; check left
    and #$02
    beq s2_prcsInpt_notLeft

    lda ENM_13_hp              ; restrict the player from moving the
    cmp #$07                   ; character whilst ducking.
    beq s2_prcsInpt_ende

    jsr s2_angelaFrame_WALK_INC
    lda #%01110100
    sta SPRBUF1+SPRIORITY
    dec SPRBUF1+SX
    jmp s2_prcsInpt_ende

s2_prcsInpt_notLeft
;==================
    lda shipLastDir            ; if(HITTING_LEFT) goto noMovement
    cmp #$01
    beq s2_prcsInpt_noMovement

    lda joy1+1                 ; check right
    and #$01
    beq s2_prcsInpt_noMovement

    stz ENM_15_hp              ; uncheck angela duck #1 frame lockout

    lda ENM_13_hp              ; restrict the player from moving the
    cmp #$07                   ; character whilst ducking.
    beq s2_prcsInpt_ende

    jsr s2_angelaFrame_WALK_INC
    lda #%00110100
    sta SPRBUF1+SPRIORITY
    inc SPRBUF1+SX
    jmp s2_prcsInpt_ende

s2_prcsInpt_noMovement
;=====================
    stz ENM_13_hp              ; reset angela frame
    stz ENM_14_hp              ; reset angela walking frame delay
    stz ENM_15_hp              ; reset angela duck frame #1 lockout

s2_prcsInpt_ende
    rts



;------------------------------



S2_DRAW_ANGELA

    ; if(IS_JUMPING && !HAS_LANDED && !IS_STANDING)
    ;     do draw Angela jumping sprite

    lda shipAdvHalfFlag        ; IS_JUMPING
    cmp #$01
    bne s2_drw_ang_notJumping
    lda dec_vel_dir            ; HAS_LANDED
    cmp #$00
    bne s2_drw_ang_notJumping
    lda shipAnim               ; IS_STANDING
    cmp #$00
    bne s2_drw_ang_notJumping

    lda #$44                   ; angela jumping frame
    sta SPRBUF1+SFRAME
    jmp s2_drAng_ff

s2_drw_ang_notJumping
    ; if(!IS_JUMPING && !HAS_LANDED && !IS_STANDING)
    ;     do draw Angela falling sprite

    lda shipAdvHalfFlag        ; IS_JUMPING
    cmp #$00
    bne s2_drw_ang_notFalling
    lda dec_vel_dir            ; HAS_LANDED
    cmp #$00
    bne s2_drw_ang_notFalling
    lda shipAnim               ; IS_STANDING
    cmp #$00
    bne s2_drw_ang_notFalling

    ;-----
    ; little hack to make Angela stand still on the moving platform
    ; in room 3.
    lda LEVEL_WAVE
    cmp #$03
    bne s2_drw_ang_falling
    lda ENM_17_numfr
    cmp #$01
    beq s2_drw_ang_notFalling

s2_drw_ang_falling
    lda #$48                   ; angela falling frame
    sta SPRBUF1+SFRAME
    jmp s2_drAng_ff
    ;--------


s2_drw_ang_notFalling

    lda ENM_13_hp
    cmp #$00                   ; walking anims
    beq s2_drAng_00
    cmp #$01
    beq s2_drAng_01
    cmp #$02
    beq s2_drAng_00
    cmp #$03
    beq s2_drAng_02
    cmp #$04
    beq s2_drAng_00
    cmp #$05
    beq s2_drAng_01
    ;---
    cmp #$06
    beq s2_drAng_03
    cmp #$07
    beq s2_drAng_04
    jmp s2_drAng_ff
s2_drAng_00                    ; angela still frame
    stz SPRBUF1+SFRAME
    jmp s2_drAng_ff
s2_drAng_01                    ; angela walking frame #1/2
    lda #$04
    sta SPRBUF1+SFRAME
    jmp s2_drAng_ff
s2_drAng_02                    ; angela walking frame #2/2
    lda #$08
    sta SPRBUF1+SFRAME
    jmp s2_drAng_ff
s2_drAng_03                    ; angela ducking frame #1/2
    lda #$0c
    sta SPRBUF1+SFRAME
    jmp s2_drAng_ff
s2_drAng_04                    ; angela ducking frame #2/2
    lda #$40
    sta SPRBUF1+SFRAME
s2_drAng_ff
    rts


s2_angelaFrame_WALK_INC
    inc ENM_14_hp
    lda ENM_14_hp
    cmp #$05
    bne s2_angFrWLKINC_ende
    stz ENM_14_hp
    inc ENM_13_hp
    lda ENM_13_hp
    cmp #$06
    bne s2_angFrWLKINC_ende
    stz ENM_13_hp
s2_angFrWLKINC_ende
    rts






;----------------------------------------------------------------
;
; B A L L 'n C H A I N
;                       H A N D L I N G   C O D E
; H O I P O I ! !
;
;----------------------------------------------------------------

; (ab)uses: 'ENM_13_leftRight' for holding the instruction byte,
;           and 'ENM_13_numfr' for amount of pixels to dec/inc.
;
; the sprites used for ball 'n chain are 1, 2, 3 and 4 whereas of
; the first one is the ball and the three remaining for the chain.
;
; the ball 'n chain movement pattern table resides at the very
; end of 's1boss.s'.

S2_BALL_n_CHAIN_UPDATE

    jsr s2_BLnCHN_fetchChunk
    lda ENM_13_leftRight
    cmp #$ff
    beq s2_blchn_chain1Y
    cmp #$a0
    bne s2_blchn_chain1xINC
    lda SPRBUF1+SX4
    jsr s2_BLnCHN_DEC
    sta SPRBUF1+SX4
    jmp s2_blchn_chain1Y
s2_blchn_chain1xINC
    lda SPRBUF1+SX4
    jsr s2_BLnCHN_INC
    sta SPRBUF1+SX4

s2_blchn_chain1Y
    jsr s2_BLnCHN_fetchChunk
    lda ENM_13_leftRight
    cmp #$ff
    beq s2_blchn_chain2X
    cmp #$a0
    bne s2_blchn_chain1yINC
    lda SPRBUF1+SY4
    jsr s2_BLnCHN_DEC
    sta SPRBUF1+SY4
    jmp s2_blchn_chain2X
s2_blchn_chain1yINC
    lda SPRBUF1+SY4
    jsr s2_BLnCHN_INC
    sta SPRBUF1+SY4

s2_blchn_chain2X
    jsr s2_BLnCHN_fetchChunk
    lda ENM_13_leftRight
    cmp #$ff
    beq s2_blchn_chain2Y
    cmp #$a0
    bne s2_blchn_chain2xINC
    lda SPRBUF1+SX3
    jsr s2_BLnCHN_DEC
    sta SPRBUF1+SX3
    jmp s2_blchn_chain2Y
s2_blchn_chain2xINC
    lda SPRBUF1+SX3
    jsr s2_BLnCHN_INC
    sta SPRBUF1+SX3

s2_blchn_chain2Y
    jsr s2_BLnCHN_fetchChunk
    lda ENM_13_leftRight
    cmp #$ff
    beq s2_blchn_chain3X
    cmp #$a0
    bne s2_blchn_chain2yINC
    lda SPRBUF1+SY3
    jsr s2_BLnCHN_DEC
    sta SPRBUF1+SY3
    jmp s2_blchn_chain3X
s2_blchn_chain2yINC
    lda SPRBUF1+SY3
    jsr s2_BLnCHN_INC
    sta SPRBUF1+SY3

s2_blchn_chain3X
    jsr s2_BLnCHN_fetchChunk
    lda ENM_13_leftRight
    cmp #$ff
    beq s2_blchn_chain3Y
    cmp #$a0
    bne s2_blchn_chain3xINC
    lda SPRBUF1+SX2
    jsr s2_BLnCHN_DEC
    sta SPRBUF1+SX2
    jmp s2_blchn_chain3Y
s2_blchn_chain3xINC
    lda SPRBUF1+SX2
    jsr s2_BLnCHN_INC
    sta SPRBUF1+SX2

s2_blchn_chain3Y
    jsr s2_BLnCHN_fetchChunk
    lda ENM_13_leftRight
    cmp #$ff
    beq s2_blchn_chain4X
    cmp #$a0
    bne s2_blchn_chain3yINC
    lda SPRBUF1+SY2
    jsr s2_BLnCHN_DEC
    sta SPRBUF1+SY2
    jmp s2_blchn_chain4X
s2_blchn_chain3yINC
    lda SPRBUF1+SY2
    jsr s2_BLnCHN_INC
    sta SPRBUF1+SY2

s2_blchn_chain4X
    jsr s2_BLnCHN_fetchChunk
    lda ENM_13_leftRight
    cmp #$ff
    beq s2_blchn_chain4Y
    cmp #$a0
    bne s2_blchn_chain4xINC
    lda SPRBUF1+SX1
    jsr s2_BLnCHN_DEC
    sta SPRBUF1+SX1
    jmp s2_blchn_chain4Y
s2_blchn_chain4xINC
    lda SPRBUF1+SX1
    jsr s2_BLnCHN_INC
    sta SPRBUF1+SX1

s2_blchn_chain4Y
    jsr s2_BLnCHN_fetchChunk
    lda ENM_13_leftRight
    cmp #$ff
    beq s2_blchn_chainENDE
    cmp #$a0
    bne s2_blchn_chain4yINC
    lda SPRBUF1+SY1
    jsr s2_BLnCHN_DEC
    sta SPRBUF1+SY1
    jmp s2_blchn_chainENDE
s2_blchn_chain4yINC
    lda SPRBUF1+SY1
    jsr s2_BLnCHN_INC
    sta SPRBUF1+SY1

s2_blchn_chainENDE
    rts

    



s2_BLnCHN_fetchChunk
    jsr s2_BLnCHN_getDataByte  ; get instruction byte
    sta ENM_13_leftRight       ; store it
    jsr s2_BLnCHN_getDataByte  ; get value byte
    sta ENM_13_numfr           ; store it
    rts

s2_BLnCHN_getDataByte
    phb
    lda #:PATTERN_BALL_n_CHAIN
    pha
    plb
    lda PATTERN_BALL_n_CHAIN, y
    plb
    iny
    cpy #560
    beq s2_blchn_gDB_RES
    cpy #1120
    bne s2_blchn_gDB_RTS
    ldy #$0000
s2_blchn_gDB_RES               ; reset to original coords
    pha
    lda #112
    sta SPRBUF1+SX1
    lda #146
    sta SPRBUF1+SY1
    lda #120
    sta SPRBUF1+SX2
    sta SPRBUF1+SX3
    sta SPRBUF1+SX4
    lda #141
    sta SPRBUF1+SY2
    lda #126
    sta SPRBUF1+SY3
    lda #110
    sta SPRBUF1+SY4
    pla
s2_blchn_gDB_RTS
    rts

s2_BLnCHN_DEC
    ldx #$0000
    ldx ENM_13_numfr
    cpx #$0000
    beq s2_BLnCHN_DECINCRTS
s2_blchn_decLoop
    dea
    dex
    cpx #$0000
    bne s2_blchn_decLoop
    rts

s2_BLnCHN_INC
    ldx #$0000
    ldx ENM_13_numfr
    cpx #$0000
    beq s2_BLnCHN_DECINCRTS
s2_blchn_incLoop
    ina
    dex
    cpx #$0000
    bne s2_blchn_incLoop
s2_BLnCHN_DECINCRTS
    rts







;----------------------------------------------------------------
; L E V E L   B O U N D A R Y   C O L L I S I O N   R O U T I N E S ! ! !
;----------------------------------------------------------------



; pseudo->angela variable translation:
;
;  ANG_X  = freeForAll1
;  ANG_Y  = freeForAll2
;  ANG_X2 = freeForAll3
;  ANG_Y2 = freeForAll4
;
;  IS_STANDING   = shipAnim
;  HITTING_LEFT  = shipLastDir
;  HITTING_RIGHT = shipVel
;  HITTING_BELOW = shipAdvNumPix
;
;  IS_JUMPING = shipAdvHalfFlag
;  HAS_LANDED = dec_vel_dir
;  JUMP_VAL = areWeAtFirstBoss
;
;
;  SECTION1_VERTLOCK = BUL_21_alive
;  SECTION2_VERTLOCK = free to use at your own will
;  SECTION3_VERTLOCK = ditto
;  SECTION4_VERTLOCK = ditto
;
;
;  SECTION1_X  = ENM_13_f1
;  SECTION1_Y  = ENM_13_f2
;  SECTION1_X2 = ENM_13_f3
;  SECTION1_Y2 = ENM_14_f1
;
;  SECTION2_X  = ENM_14_f2
;  SECTION2_Y  = ENM_14_f3
;  SECTION2_X2 = ENM_15_f1
;  SECTION2_Y2 = ENM_15_f2
;
;  SECTION3_X  = ENM_15_f3
;  SECTION3_Y  = ENM_16_f1
;  SECTION3_X2 = ENM_16_f2
;  SECTION3_Y2 = ENM_16_f3
;
;  SECTION4_X  = ENM_17_f1
;  SECTION4_Y  = ENM_17_f2
;  SECTION4_X2 = ENM_17_f3
;  SECTION4_Y2 = ENM_18_f1
;
;  SECTION5_X  = ENM_18_f2
;  SECTION5_Y  = ENM_18_f3
;  SECTION5_X2 = ENM_13_patt
;  SECTION5_Y2 = ENM_14_patt
;
;  SECTION6_X  = ENM_15_patt
;  SECTION6_Y  = ENM_16_patt
;  SECTION6_X2 = ENM_17_patt
;  SECTION6_Y2 = ENM_18_patt
;---------------------------


S2_OUTSIDE_SETUP

    ; solid sections setup
    ; left part grassy tiles
    lda #$0e
    sta ENM_13_f1              ; SECTION1_X
    lda #208
    sta ENM_13_f2              ; SECTION1_Y
    lda #127
    sta ENM_13_f3              ; SECTION1_X2
    lda #223
    sta ENM_14_f1              ; SECTION1_Y2
    ; right part grassy tiles
    lda #124
    sta ENM_14_f2              ; SECTION2_X
    lda #208
    sta ENM_14_f3              ; SECTION2_Y
    lda #244
    sta ENM_15_f1              ; SECTION2_X2
    lda #223
    sta ENM_15_f2              ; SECTION2_Y2
    ; up arrow box thing
    lda #96  
    sta ENM_15_f3              ; SECTION3_X
    lda #144
    sta ENM_16_f1              ; SECTION3_Y
    lda #111
    sta ENM_16_f2              ; SECTION3_X2
    lda #159
    sta ENM_16_f3              ; SECTION3_Y2
    ; left screen border
    stz ENM_17_f1              ; SECTION4_X
    lda #104
    sta ENM_17_f2              ; SECTION4_Y
    lda #$0f
    sta ENM_17_f3              ; SECTION4_X2
    lda #223
    sta ENM_18_f1              ; SECTION4_Y1
    ; wooden door
    lda #158
    sta ENM_15_patt            ; SECTION6_X
    lda #110
    sta ENM_16_patt            ; SECTION6_Y
    lda #226
    sta ENM_17_patt            ; SECTION6_X2
    lda #213
    sta ENM_18_patt            ; SECTION6_Y2

    ; zeroify everything else for great justice
    stz ENM_18_f2              ; SECTION5_X
    stz ENM_18_f3              ; SECTION5_Y
    stz ENM_13_patt            ; SECTION5_X2
    stz ENM_14_patt            ; SECTION5_Y2

    lda #$ff
    sta LEVEL_WAVE             ; set room number

    stz shipAnim               ; IS_STANDING = FALSE
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE
    stz dec_vel_dir            ; HAS_LANDED = FALSE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    rts


;---------------------------


S2_ROOM01_SETUP

    ; solid sections setup
    lda #32
    sta ENM_13_f1              ; SECTION1_X
    lda #96 
    sta ENM_13_f2              ; SECTION1_Y
    lda #47
    sta ENM_13_f3              ; SECTION1_X2
    lda #207
    sta ENM_14_f1              ; SECTION1_Y2

    lda #32
    sta ENM_14_f2              ; SECTION2_X
    lda #208
    sta ENM_14_f3              ; SECTION2_Y
    lda #111
    sta ENM_15_f1              ; SECTION2_X2
    lda #223
    sta ENM_15_f2              ; SECTION2_Y2

    lda #96
    sta ENM_15_f3              ; SECTION3_X
    lda #160
    sta ENM_16_f1              ; SECTION3_Y
    lda #127
    sta ENM_16_f2              ; SECTION3_X2
    lda #175
    sta ENM_16_f3              ; SECTION3_Y2

    lda #144
    sta ENM_17_f1              ; SECTION4_X
    lda #208
    sta ENM_17_f2              ; SECTION4_Y
    lda #223
    sta ENM_17_f3              ; SECTION4_X2
    sta ENM_18_f1              ; SECTION4_Y2

    lda #208
    sta ENM_18_f2              ; SECTION5_X
    lda #80
    sta ENM_18_f3              ; SECTION5_Y
    lda #223
    sta ENM_13_patt            ; SECTION5_X2
    lda #175
    sta ENM_14_patt            ; SECTION5_Y2

    lda #64
    sta ENM_15_patt            ; SECTION6_X
    lda #64
    sta ENM_16_patt            ; SECTION6_Y
    lda #191
    sta ENM_17_patt            ; SECTION6_X2
    lda #79
    sta ENM_18_patt            ; SECTION6_Y2


    stz shipAnim               ; IS_STANDING = FALSE
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE
    stz dec_vel_dir            ; HAS_LANDED = FALSE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    lda #$01
    sta LEVEL_WAVE             ; set room number

    ; sprites enabled: 0 only
    lda #%11111100
    sta SPRBUF2
    lda #$ff
    sta SPRBUF2+1
    sta SPRBUF2+2
    sta SPRBUF2+3

    rts



;---------------------



S2_ROOM02_SETUP

    ; solid sections setup
    lda #32
    sta ENM_13_f1              ; SECTION1_X
    lda #64
    sta ENM_13_f2              ; SECTION1_Y
    lda #47
    sta ENM_13_f3              ; SECTION1_X2
    lda #175
    sta ENM_14_f1              ; SECTION1_Y2

    lda #32
    sta ENM_14_f2              ; SECTION2_X
    lda #208
    sta ENM_14_f3              ; SECTION2_Y
    lda #47
    sta ENM_15_f1              ; SECTION2_X2
    lda #223
    sta ENM_15_f2              ; SECTION2_Y2

    lda #96
    sta ENM_15_f3              ; SECTION3_X
    lda #208
    sta ENM_16_f1              ; SECTION3_Y
    lda #208
    sta ENM_16_f2              ; SECTION3_X2
    lda #223
    sta ENM_16_f3              ; SECTION3_Y23

    lda #208
    sta ENM_17_f1              ; SECTION4_X
    lda #176
    sta ENM_17_f2              ; SECTION4_Y
    lda #223
    sta ENM_17_f3              ; SECTION4_X2
    lda #210
    sta ENM_18_f1              ; SECTION4_Y2

    lda #208
    sta ENM_18_f2              ; SECTION5_X
    lda #80
    sta ENM_18_f3              ; SECTION5_Y
    lda #223
    sta ENM_13_patt            ; SECTION5_X2
    lda #143
    sta ENM_14_patt            ; SECTION5_Y2

    lda #112
    sta ENM_15_patt            ; SECTION6_X
    lda #100
    sta ENM_16_patt            ; SECTION6_Y
    lda #143
    sta ENM_17_patt            ; SECTION6_X2
    lda #115
    sta ENM_18_patt            ; SECTION6_Y2


    stz shipAnim               ; IS_STANDING = FALSE
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE
    stz dec_vel_dir            ; HAS_LANDED = FALSE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    lda #$02
    sta LEVEL_WAVE             ; set room number

    ; sprites enabled: 0,1,2,3,4
    stz SPRBUF2
    lda #%11111100
    sta SPRBUF2+1
    lda #$ff
    sta SPRBUF2+2
    sta SPRBUF2+3

    rts



;---------------------



S2_ROOM03_SETUP

    ; solid sections setup
    lda #00 ; the blue movable block/platform
    sta ENM_13_f1              ; SECTION1_X
    lda #00 
    sta ENM_13_f2              ; SECTION1_Y
    lda #00
    sta ENM_13_f3              ; SECTION1_X2
    lda #00
    sta ENM_14_f1              ; SECTION1_Y2

    lda #112
    sta ENM_14_f2              ; SECTION2_X
    lda #112
    sta ENM_14_f3              ; SECTION2_Y
    lda #128
    sta ENM_15_f1              ; SECTION2_X2
    lda #220
    sta ENM_15_f2              ; SECTION2_Y2

    lda #32
    sta ENM_15_f3              ; SECTION3_X
    lda #64
    sta ENM_16_f1              ; SECTION3_Y
    lda #47
    sta ENM_16_f2              ; SECTION3_X2
    lda #143
    sta ENM_16_f3              ; SECTION3_Y23

    lda #32
    sta ENM_17_f1              ; SECTION4_X
    lda #176
    sta ENM_17_f2              ; SECTION4_Y
    lda #47
    sta ENM_17_f3              ; SECTION4_X2
    lda #223
    sta ENM_18_f1              ; SECTION4_Y2

    lda #96
    sta ENM_18_f2              ; SECTION5_X
    lda #128
    sta ENM_18_f3              ; SECTION5_Y
    lda #112
    sta ENM_13_patt            ; SECTION5_X2
    lda #220
    sta ENM_14_patt            ; SECTION5_Y2

    lda #48
    sta ENM_15_patt            ; SECTION6_X
    lda #64
    sta ENM_16_patt            ; SECTION6_Y
    lda #173
    sta ENM_17_patt            ; SECTION6_X2
    lda #80
    sta ENM_18_patt            ; SECTION6_Y2



    stz shipAnim               ; IS_STANDING = FALSE
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE
    stz dec_vel_dir            ; HAS_LANDED = FALSE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    stz ENM_16_numfr           ; block movement delay

    lda #$03
    sta LEVEL_WAVE             ; set room number

    ; sprites enabled: 0 & 1 (#0 = Angela, #1 = moving block/platform)
    lda #%11110000
    sta SPRBUF2
    lda #$ff
    sta SPRBUF2+1
    sta SPRBUF2+2
    sta SPRBUF2+3

    lda #64
    sta SPRBUF1+SX1
    lda #128
    sta SPRBUF1+SY1
    lda #$30
    sta SPRBUF1+SPRIORITY1
    lda #$84
    sta SPRBUF1+SFRAME1        ; use the 'blue smiley block' sprite

    rts



;---------------------



S2_RECALC_ANG_X

    lda LEVEL_WAVE
    cmp #$ff
    beq s2rclc_angXint

    lda SPRBUF1+SX
    sta freeForAll1            ; ANG_X
    ldx #$000c
s2_r_ang_x_loop
    inc freeForAll1            ; ANG_X
    dex
    cpx #$0000
    bne s2_r_ang_x_loop
    rts

s2rclc_angXint

    lda SPRBUF1+SX1
    sta freeForAll1
    ldx #$000c
s2_rang_x_loop
    inc freeForAll1
    dex
    cpx #$0000
    bne s2_rang_x_loop
    rts






S2_RECALC_ANG_Y

    lda LEVEL_WAVE
    cmp #$ff
    beq s2rclc_angYint

    lda SPRBUF1+SY
    sta freeForAll2            ; ANG_Y
    inc freeForAll2
    inc freeForAll2
    rts
s2rclc_angYint
    lda SPRBUF1+SY1
    sta freeForAll2            ; ANG_Y
    inc freeForAll2
    inc freeForAll2
    rts







S2_RECALC_ANG_X2

    lda LEVEL_WAVE
    cmp #$ff
    beq s2rclc_angX2int

    lda SPRBUF1+SX
    sta freeForAll3            ; ANG_X2
    ldx #$0012
s2_r_ang_x2_loop
    inc freeForAll3            ; ANG_X2
    dex
    cpx #$0000
    bne s2_r_ang_x2_loop
    rts

s2rclc_angX2int

    lda SPRBUF1+SX1
    sta freeForAll3
    ldx #$0012
s2_rang_x2_loop
    inc freeForAll3
    dex
    cpx #$0000
    bne s2_rang_x2_loop
    rts






S2_RECALC_ANG_Y2

    lda LEVEL_WAVE
    cmp #$ff
    beq s2rclc_angY2int

    lda SPRBUF1+SY
    sta freeForAll4            ; ANG_Y2
    ldx #$001b
s2_r_ang_y2_loop
    inc freeForAll4            ; ANG_Y2
    dex
    cpx #$0000
    bne s2_r_ang_y2_loop
    rts

s2rclc_angY2int

    lda SPRBUF1+SY1
    sta freeForAll4
    ldx #$001b
s2_rang_y2_loop
    inc freeForAll4
    dex
    cpx #$0000
    bne s2_rang_y2_loop
    rts


;---------




S2_RECALC_SPR_ANG_X

    lda LEVEL_WAVE
    cmp #$ff
    beq s2rclc_sprangXint

    lda freeForAll1            ; ANG_X
    sta a_byte2
    ldx #$000c
s2_r_spr_ang_x_loop
    dec a_byte2
    dex
    cpx #$0000
    bne s2_r_spr_ang_x_loop
    lda a_byte2
    sta SPRBUF1+SX
    rts

s2rclc_sprangXint

    lda freeForAll1
    sta a_byte2
    ldx #$000c
s2_r_sprang_x_loop
    dec a_byte2
    dex
    cpx #$0000
    bne s2_r_sprang_x_loop
    lda a_byte2
    sta SPRBUF1+SX1
    rts






S2_RECALC_SPR_ANG_Y

    lda LEVEL_WAVE
    cmp #$ff
    beq s2rclc_sprangYint

    lda freeForAll2            ; ANG_Y
    sta a_byte2
    dec a_byte2
    dec a_byte2
    lda a_byte2
    sta SPRBUF1+SY
    rts

s2rclc_sprangYint

    lda freeForAll2
    sta a_byte2
    dec a_byte2
    dec a_byte2
    lda a_byte2
    sta SPRBUF1+SY1
    rts






S2_RECALC_SPR_ANG_X2

    lda LEVEL_WAVE
    cmp #$ff
    beq s2rclc_sprangX2int

    lda freeForAll3            ; ANG_X2
    sta a_byte2
    ldx #$0012
s2_r_spr_ang_x2_loop
    dec a_byte2
    dex
    cpx #$0000
    bne s2_r_spr_ang_x2_loop ;fixed added _spr_
    lda a_byte2
    sta SPRBUF1+SX
    rts

s2rclc_sprangX2int

    lda freeForAll3
    sta a_byte2
    ldx #$0012
s2_r_sprang_x2_loop
    dec a_byte2
    dex
    cpx #$0000
    bne s2_r_sprang_x2_loop
    lda a_byte2
    sta SPRBUF1+SX1
    rts





S2_RECALC_SPR_ANG_Y2

    lda LEVEL_WAVE
    cmp #$ff
    beq s2rclc_sprangY2int

    lda freeForAll4            ; ANG_Y2
    sta a_byte2
    ldx #$001b
s2_r_spr_ang_y2_loop
    dec a_byte2
    dex
    cpx #$0000
    bne s2_r_spr_ang_y2_loop ;fixed added _spr_
    lda a_byte2
    sta SPRBUF1+SY
    sta freeForAll2
    rts

s2rclc_sprangY2int

    lda freeForAll4
    sta a_byte2
    ldx #$001b
s2rclc_sprangY2_loop
    dec a_byte2
    dex
    cpx #$0000
    bne s2rclc_sprangY2_loop
    lda a_byte2
    sta SPRBUF1+SY1
    sta freeForAll2
    rts



;=====================



; DESTROYS a_byte2

S2_my_BOXCOLL_RIGHTSIDE

;   if(ANG_Y2 > (SECTION1_Y+1) && ANG_Y < SECTION1_Y2) {
;       if(ANG_X == SECTION1_X2) {
;           HITTING_RIGHT = TRUE;
;           return;
;       }
;   }

    jsr S2_RECALC_ANG_X
    jsr S2_RECALC_ANG_Y
    jsr S2_RECALC_ANG_X2
    jsr S2_RECALC_ANG_Y2

    lda ENM_13_f2              ; increment SECTION1_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION1_Y+1
    bpl s2_m_b_rgtsd_01_0a     ; branch if(ANG_Y2 > (SECTION1_Y+1))
    jmp s2_m_b_rgtsd_02        ; ELSE check next section

s2_m_b_rgtsd_01_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_14_f1              ; against SECTION1_Y2
    bmi s2_m_b_rgtsd_01_0b     ; branch if(ANG_Y < SECTION1_Y2)
    jmp s2_m_b_rgtsd_02        ; ELSE check next section

s2_m_b_rgtsd_01_0b

    lda ENM_13_f3              ; copy SECTION1_X2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll1            ; ANG_X compare
    cmp ENM_13_f3              ; against SECTION1_X2
    beq s2_m_b_rgtsd_01_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION1_X2+1
    beq s2_m_b_rgtsd_01_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll1            ; ANG_X compare
    cmp a_byte2                ; against SECTION1_X2+2
    beq s2_m_b_rgtsd_01_0c     ; branch if equal
    jmp s2_m_b_rgtsd_02        ; ELSE check next section

s2_m_b_rgtsd_01_0c

    lda #$01
    sta shipVel                ; HITTING_RIGHT = TRUE

    lda ENM_13_f3              ; copy SECTION1_X2
    sta freeForAll1            ; to ANG_X
    jsr S2_RECALC_SPR_ANG_X

    jmp s2_m_b_rgtsd_FF



;--------------
s2_m_b_rgtsd_02
;--------------

    lda ENM_14_f3              ; increment SECTION2_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION2_Y+1
    bpl s2_m_b_rgtsd_02_0a     ; branch if(ANG_Y2 > (SECTION2_Y+1))
    jmp s2_m_b_rgtsd_03        ; ELSE check next section

s2_m_b_rgtsd_02_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_15_f2              ; against SECTION2_Y2
    bmi s2_m_b_rgtsd_02_0b     ; branch if(ANG_Y < SECTION2_Y2)
    jmp s2_m_b_rgtsd_03        ; ELSE check next section

s2_m_b_rgtsd_02_0b

    lda ENM_15_f1              ; copy SECTION2_X2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll1            ; ANG_X compare
    cmp ENM_15_f1              ; against SECTION2_X2
    beq s2_m_b_rgtsd_02_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION2_X2+1
    beq s2_m_b_rgtsd_02_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll1            ; ANG_X compare
    cmp a_byte2                ; against SECTION2_X2+2
    beq s2_m_b_rgtsd_02_0c     ; branch if equal
    jmp s2_m_b_rgtsd_03        ; ELSE check next section

s2_m_b_rgtsd_02_0c

    lda #$01
    sta shipVel                ; HITTING_RIGHT = TRUE

    lda ENM_15_f1              ; copy SECTION2_X2
    sta freeForAll1            ; to ANG_X
    jsr S2_RECALC_SPR_ANG_X

    jmp s2_m_b_rgtsd_FF


; mitsuki
;--------------
s2_m_b_rgtsd_03
;--------------

    lda ENM_16_f1              ; increment SECTION3_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION3_Y+1
    bpl s2_m_b_rgtsd_03_0a     ; branch if(ANG_Y2 > (SECTION3_Y+1))
    jmp s2_m_b_rgtsd_04        ; ELSE check next section

s2_m_b_rgtsd_03_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_16_f3              ; against SECTION3_Y2
    bmi s2_m_b_rgtsd_03_0b     ; branch if(ANG_Y < SECTION3_Y2)
    jmp s2_m_b_rgtsd_04        ; ELSE check next section

s2_m_b_rgtsd_03_0b

    lda ENM_16_f2              ; copy SECTION3_X2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll1            ; ANG_X compare
    cmp ENM_16_f2              ; against SECTION3_X2
    beq s2_m_b_rgtsd_03_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION3_X2+1
    beq s2_m_b_rgtsd_03_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll1            ; ANG_X compare
    cmp a_byte2                ; against SECTION3_X2+2
    beq s2_m_b_rgtsd_03_0c     ; branch if equal
    jmp s2_m_b_rgtsd_04        ; ELSE check next section

s2_m_b_rgtsd_03_0c

    lda #$01
    sta shipVel                ; HITTING_RIGHT = TRUE

    lda ENM_16_f2              ; copy SECTION3_X2
    sta freeForAll1            ; to ANG_X
    jsr S2_RECALC_SPR_ANG_X

    jmp s2_m_b_rgtsd_FF



;--------------
s2_m_b_rgtsd_04
;--------------

    lda ENM_17_f2              ; increment SECTION4_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION4_Y+1
    bpl s2_m_b_rgtsd_04_0a     ; branch if(ANG_Y2 > (SECTION4_Y+1))
    jmp s2_m_b_rgtsd_05        ; ELSE check next section

s2_m_b_rgtsd_04_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_18_f1              ; against SECTION4_Y2
    bmi s2_m_b_rgtsd_04_0b     ; branch if(ANG_Y < SECTION4_Y2)
    jmp s2_m_b_rgtsd_05        ; ELSE check next section

s2_m_b_rgtsd_04_0b

    lda ENM_17_f3              ; copy SECTION4_X2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll1            ; ANG_X compare
    cmp ENM_17_f3              ; against SECTION4_X2
    beq s2_m_b_rgtsd_04_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION4_X2+1
    beq s2_m_b_rgtsd_04_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll1            ; ANG_X compare
    cmp a_byte2                ; against SECTION4_X2+2
    beq s2_m_b_rgtsd_04_0c     ; branch if equal
    jmp s2_m_b_rgtsd_05        ; ELSE check next section

s2_m_b_rgtsd_04_0c

    lda #$01
    sta shipVel                ; HITTING_RIGHT = TRUE

    lda ENM_17_f3              ; copy SECTION4_X2
    sta freeForAll1            ; to ANG_X
    jsr S2_RECALC_SPR_ANG_X

    jmp s2_m_b_rgtsd_FF



;--------------
s2_m_b_rgtsd_05
;--------------

    lda ENM_18_f3              ; increment SECTION5_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION5_Y+1
    bpl s2_m_b_rgtsd_05_0a     ; branch if(ANG_Y2 > (SECTION5_Y+1))
    jmp s2_m_b_rgtsd_06        ; ELSE check next section

s2_m_b_rgtsd_05_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_14_patt            ; against SECTION5_Y2
    bmi s2_m_b_rgtsd_05_0b     ; branch if(ANG_Y < SECTION5_Y2)
    jmp s2_m_b_rgtsd_06        ; ELSE check next section

s2_m_b_rgtsd_05_0b

    lda ENM_13_patt            ; copy SECTION5_X2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll1            ; ANG_X compare
    cmp ENM_13_patt            ; against SECTION5_X2
    beq s2_m_b_rgtsd_05_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION5_X2+1
    beq s2_m_b_rgtsd_05_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll1            ; ANG_X compare
    cmp a_byte2                ; against SECTION5_X2+2
    beq s2_m_b_rgtsd_05_0c     ; branch if equal
    jmp s2_m_b_rgtsd_06        ; ELSE check next section

s2_m_b_rgtsd_05_0c

    lda #$01
    sta shipVel                ; HITTING_RIGHT = TRUE

    lda ENM_13_patt            ; copy SECTION5_X2
    sta freeForAll1            ; to ANG_X
    jsr S2_RECALC_SPR_ANG_X

    jmp s2_m_b_rgtsd_FF



;--------------
s2_m_b_rgtsd_06
;--------------

    lda ENM_16_patt            ; increment SECTION6_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION6_Y+1
    bpl s2_m_b_rgtsd_06_0a     ; branch if(ANG_Y2 > (SECTION6_Y+1))
    jmp s2_m_b_rgtsd_FF_pre    ; ELSE check next section

s2_m_b_rgtsd_06_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_18_patt            ; against SECTION6_Y2
    bmi s2_m_b_rgtsd_06_0b     ; branch if(ANG_Y < SECTION6_Y2)
    jmp s2_m_b_rgtsd_FF_pre    ; ELSE check next section

s2_m_b_rgtsd_06_0b

    lda ENM_17_patt            ; copy SECTION6_X2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll1            ; ANG_X compare
    cmp ENM_17_patt            ; against SECTION6_X2
    beq s2_m_b_rgtsd_06_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION6_X2+1
    beq s2_m_b_rgtsd_06_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll1            ; ANG_X compare
    cmp a_byte2                ; against SECTION6_X2+2
    beq s2_m_b_rgtsd_06_0c     ; branch if equal

s2_m_b_rgtsd_FF_pre

    stz shipVel                ; HITTING_RIGHT = FALSE
    jmp s2_m_b_rgtsd_FF

s2_m_b_rgtsd_06_0c

    lda #$01
    sta shipVel                ; HITTING_RIGHT = TRUE

    lda ENM_17_patt            ; copy SECTION6_X2
    sta freeForAll1            ; to ANG_X
    jsr S2_RECALC_SPR_ANG_X



;--------------
s2_m_b_rgtsd_FF
;--------------
    rts



;=====================



; DESTROYS a_byte2

S2_my_BOXCOLL_LEFTSIDE

;   if(ANG_Y2 > SECTION1_Y && ANG_Y < SECTION1_Y2) {
;       if(ANG_X2 == SECTION1_X) {
;           HITTING_LEFT = TRUE;
;           return;
;       }
;   }

    jsr S2_RECALC_ANG_X
    jsr S2_RECALC_ANG_Y
    jsr S2_RECALC_ANG_X2
    jsr S2_RECALC_ANG_Y2

    lda ENM_13_f2              ; increment SECTION1_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION1_Y+1
    bpl s2_m_b_lftsd_01_0a     ; branch if(ANG_Y2 > (SECTION1_Y+1))
    jmp s2_m_b_lftsd_02        ; ELSE check next section

s2_m_b_lftsd_01_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_14_f1              ; against SECTION1_Y2
    bmi s2_m_b_lftsd_01_0b     ; branch if(ANG_Y < SECTION1_Y2)
    jmp s2_m_b_lftsd_02        ; ELSE check next section

s2_m_b_lftsd_01_0b

    lda ENM_13_f1              ; copy SECTION1_X
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_13_f1              ; against SECTION1_X
    beq s2_m_b_lftsd_01_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION1_X+1
    beq s2_m_b_lftsd_01_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll3            ; ANG_X2 compare
    cmp a_byte2                ; against SECTION1_X+2
    beq s2_m_b_lftsd_01_0c     ; branch if equal
    jmp s2_m_b_lftsd_02        ; ELSE check next section

s2_m_b_lftsd_01_0c

    lda #$01
    sta shipLastDir            ; HITTING_LEFT = TRUE

    lda ENM_13_f1              ; copy SECTION1_X
    sta freeForAll3            ; to ANG_X2
    jsr S2_RECALC_SPR_ANG_X2

    jmp s2_m_b_lftsd_FF



;--------------
s2_m_b_lftsd_02
;--------------

    lda ENM_14_f3              ; increment SECTION2_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION2_Y+1
    bpl s2_m_b_lftsd_02_0a     ; branch if(ANG_Y2 > (SECTION2_Y+1))
    jmp s2_m_b_lftsd_03        ; ELSE check next section

s2_m_b_lftsd_02_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_15_f2              ; against SECTION2_Y2
    bmi s2_m_b_lftsd_02_0b     ; branch if(ANG_Y < SECTION2_Y2)
    jmp s2_m_b_lftsd_03        ; ELSE check next section

s2_m_b_lftsd_02_0b

    lda ENM_14_f2              ; copy SECTION2_X
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_14_f2              ; against SECTION2_X
    beq s2_m_b_lftsd_02_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION2_X+1
    beq s2_m_b_lftsd_02_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll3            ; ANG_X2 compare
    cmp a_byte2                ; against SECTION2_X+2
    beq s2_m_b_lftsd_02_0c     ; branch if equal
    jmp s2_m_b_lftsd_03        ; ELSE check next section

s2_m_b_lftsd_02_0c

    lda #$01
    sta shipLastDir            ; HITTING_LEFT = TRUE

    lda ENM_14_f2              ; copy SECTION2_X
    sta freeForAll3            ; to ANG_X2
    jsr S2_RECALC_SPR_ANG_X2

    jmp s2_m_b_lftsd_FF



;--------------
s2_m_b_lftsd_03
;--------------

    lda ENM_16_f1              ; increment SECTION3_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION3_Y+1
    bpl s2_m_b_lftsd_03_0a     ; branch if(ANG_Y2 > (SECTION3_Y+1))
    jmp s2_m_b_lftsd_04        ; ELSE check next section

s2_m_b_lftsd_03_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_16_f3              ; against SECTION3_Y2
    bmi s2_m_b_lftsd_03_0b     ; branch if(ANG_Y < SECTION3_Y2)
    jmp s2_m_b_lftsd_04        ; ELSE check next section

s2_m_b_lftsd_03_0b

    lda ENM_15_f3              ; copy SECTION3_X
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_15_f3              ; against SECTION3_X
    beq s2_m_b_lftsd_03_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION3_X+1
    beq s2_m_b_lftsd_03_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll3            ; ANG_X2 compare
    cmp a_byte2                ; against SECTION3_X+2
    beq s2_m_b_lftsd_03_0c     ; branch if equal
    jmp s2_m_b_lftsd_04        ; ELSE check next section

s2_m_b_lftsd_03_0c

    lda #$01
    sta shipLastDir            ; HITTING_LEFT = TRUE

    lda ENM_15_f3              ; copy SECTION3_X
    sta freeForAll3            ; to ANG_X2
    jsr S2_RECALC_SPR_ANG_X2

    jmp s2_m_b_lftsd_FF



;--------------
s2_m_b_lftsd_04
;--------------

    lda ENM_17_f2              ; increment SECTION4_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION4_Y+1
    bpl s2_m_b_lftsd_04_0a     ; branch if(ANG_Y2 > (SECTION4_Y+1))
    jmp s2_m_b_lftsd_05        ; ELSE check next section

s2_m_b_lftsd_04_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_18_f1              ; against SECTION4_Y2
    bmi s2_m_b_lftsd_04_0b     ; branch if(ANG_Y < SECTION4_Y2)
    jmp s2_m_b_lftsd_05        ; ELSE check next section

s2_m_b_lftsd_04_0b

    lda ENM_17_f1              ; copy SECTION4_X
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_17_f1              ; against SECTION4_X
    beq s2_m_b_lftsd_04_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION4_X+1
    beq s2_m_b_lftsd_04_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll3            ; ANG_X2 compare
    cmp a_byte2                ; against SECTION4_X+2
    beq s2_m_b_lftsd_04_0c     ; branch if equal
    jmp s2_m_b_lftsd_05        ; ELSE check next section

s2_m_b_lftsd_04_0c

    lda #$01
    sta shipLastDir            ; HITTING_LEFT = TRUE

    lda ENM_17_f1              ; copy SECTION4_X
    sta freeForAll3            ; to ANG_X2
    jsr S2_RECALC_SPR_ANG_X2

    jmp s2_m_b_lftsd_FF



;--------------
s2_m_b_lftsd_05
;--------------

    lda ENM_18_f3              ; increment SECTION5_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION5_Y+1
    bpl s2_m_b_lftsd_05_0a     ; branch if(ANG_Y2 > (SECTION5_Y+1))
    jmp s2_m_b_lftsd_06        ; ELSE check next section

s2_m_b_lftsd_05_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_14_patt            ; against SECTION5_Y2
    bmi s2_m_b_lftsd_05_0b     ; branch if(ANG_Y < SECTION5_Y2)
    jmp s2_m_b_lftsd_06        ; ELSE check next section

s2_m_b_lftsd_05_0b

    lda ENM_18_f2              ; copy SECTION5_X
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_18_f2              ; against SECTION5_X
    beq s2_m_b_lftsd_05_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION5_X+1
    beq s2_m_b_lftsd_05_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll3            ; ANG_X2 compare
    cmp a_byte2                ; against SECTION5_X+2
    beq s2_m_b_lftsd_05_0c     ; branch if equal
    jmp s2_m_b_lftsd_06        ; ELSE check next section

s2_m_b_lftsd_05_0c

    lda #$01
    sta shipLastDir            ; HITTING_LEFT = TRUE

    lda ENM_18_f2              ; copy SECTION5_X
    sta freeForAll3            ; to ANG_X2
    jsr S2_RECALC_SPR_ANG_X2

    jmp s2_m_b_lftsd_FF



;--------------
s2_m_b_lftsd_06
;--------------

    lda ENM_16_patt            ; increment SECTION6_Y by one
    sta a_byte2                ; and store new value in a_byte2
    inc a_byte2

    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION6_Y+1
    bpl s2_m_b_lftsd_06_0a     ; branch if(ANG_Y2 > (SECTION6_Y+1))
    jmp s2_m_b_lftsd_FF_pre    ; ELSE check next section

s2_m_b_lftsd_06_0a

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_18_patt            ; against SECTION6_Y2
    bmi s2_m_b_lftsd_06_0b     ; branch if(ANG_Y < SECTION6_Y2)
    jmp s2_m_b_lftsd_FF_pre    ; ELSE check next section

s2_m_b_lftsd_06_0b

    lda ENM_15_patt            ; copy SECTION6_X
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_15_patt            ; against SECTION6_X
    beq s2_m_b_lftsd_06_0c     ; branch if equal
    cmp a_byte2                ; compare against SECTION6_X+1
    beq s2_m_b_lftsd_06_0c     ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll3            ; ANG_X2 compare
    cmp a_byte2                ; against SECTION6_X+2
    beq s2_m_b_lftsd_06_0c     ; branch if equal

s2_m_b_lftsd_FF_pre

    stz shipLastDir            ; HITTING_LEFT = FALSE
    jmp s2_m_b_lftsd_FF

s2_m_b_lftsd_06_0c

    lda #$01
    sta shipLastDir            ; HITTING_LEFT = TRUE

    lda ENM_15_patt            ; copy SECTION6_X
    sta freeForAll3            ; to ANG_X2
    jsr S2_RECALC_SPR_ANG_X2



;--------------
s2_m_b_lftsd_FF
;--------------
    rts



;=====================



; DESTROYS a_byte2

S2_my_BOXCOLL_BELOW

;   if(ANG_X2 > SECTION1_X && ANG_X < SECTION1_X2) {
;       if(ANG_Y == SECTION1_Y2 || ANG_Y == (SECTION1_Y2-1) || ANG_Y == (SECTION1_Y2-2)) {
;           HITTING_BELOW = TRUE;
;           return;
;       }
;   }

    jsr S2_RECALC_ANG_X
    jsr S2_RECALC_ANG_Y
    jsr S2_RECALC_ANG_X2
    jsr S2_RECALC_ANG_Y2

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_13_f1              ; against SECTION1_X
    bpl s2_m_b_blw_01_0a       ; branch if(ANG_X2 > SECTION1_X)
    jmp s2_m_b_blw_02          ; ELSE check next section

s2_m_b_blw_01_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_13_f3              ; against SECTION1_X2
    bmi s2_m_b_blw_01_0b       ; branch if(ANG_X < SECTION1_X2)
    jmp s2_m_b_blw_02          ; ELSE check next section

s2_m_b_blw_01_0b

    lda ENM_14_f1              ; copy SECTION1_Y2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_14_f1              ; against SECTION1_Y2
    beq s2_m_b_blw_01_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION1_Y2+1
    beq s2_m_b_blw_01_0c       ; branch if equal
    dec a_byte2                ; increase a_byte2 once more
    lda freeForAll2            ; ANG_Y compare
    cmp a_byte2                ; against SECTION1_Y2+2
    beq s2_m_b_blw_01_0c       ; branch if equal
    jmp s2_m_b_blw_02          ; ELSE check next section

s2_m_b_blw_01_0c

    lda #$01
    sta shipAdvNumPix          ; HITTING_BELOW = TRUE

    lda ENM_14_f1              ; copy SECTION1_Y2
    sta freeForAll2            ; to ANG_Y
    jsr S2_RECALC_SPR_ANG_Y

    jsl sym_SFX_DEEP_THUMP
    jmp s2_m_b_blw_FF



;------------
s2_m_b_blw_02
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_14_f2              ; against SECTION2_X
    bpl s2_m_b_blw_02_0a       ; branch if(ANG_X2 > SECTION2_X)
    jmp s2_m_b_blw_03          ; ELSE check next section

s2_m_b_blw_02_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_15_f1              ; against SECTION2_X2
    bmi s2_m_b_blw_02_0b       ; branch if(ANG_X < SECTION2_X2)
    jmp s2_m_b_blw_03          ; ELSE check next section

s2_m_b_blw_02_0b

    lda ENM_15_f2              ; copy SECTION2_Y
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_15_f2              ; against SECTION2_Y2
    beq s2_m_b_blw_02_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION2_Y2+1
    beq s2_m_b_blw_02_0c       ; branch if equal
    dec a_byte2                ; increase a_byte2 once more
    lda freeForAll2            ; ANG_Y compare
    cmp a_byte2                ; against SECTION2_Y2+2
    beq s2_m_b_blw_02_0c       ; branch if equal
    jmp s2_m_b_blw_03          ; ELSE check next section

s2_m_b_blw_02_0c

    lda #$01
    sta shipAdvNumPix          ; HITTING_BELOW = TRUE

    lda ENM_15_f2              ; copy SECTION2_Y2
    sta freeForAll2            ; to ANG_Y
    jsr S2_RECALC_SPR_ANG_Y

    jsl sym_SFX_DEEP_THUMP
    jmp s2_m_b_blw_FF



;------------
s2_m_b_blw_03
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_15_f3              ; against SECTION3_X
    bpl s2_m_b_blw_03_0a       ; branch if(ANG_X2 > SECTION3_X)
    jmp s2_m_b_blw_04          ; ELSE check next section

s2_m_b_blw_03_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_16_f2              ; against SECTION3_X2
    bmi s2_m_b_blw_03_0b       ; branch if(ANG_X < SECTION3_X2)
    jmp s2_m_b_blw_04          ; ELSE check next section

s2_m_b_blw_03_0b

    lda ENM_16_f3              ; copy SECTION3_Y
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_16_f3              ; against SECTION3_Y2
    beq s2_m_b_blw_03_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION3_Y2+1
    beq s2_m_b_blw_03_0c       ; branch if equal
    dec a_byte2                ; increase a_byte2 once more
    lda freeForAll2            ; ANG_Y compare
    cmp a_byte2                ; against SECTION3_Y2+2
    beq s2_m_b_blw_03_0c       ; branch if equal
    jmp s2_m_b_blw_04          ; ELSE check next section

s2_m_b_blw_03_0c

    lda LEVEL_WAVE
    cmp #$ff                   ; if outside castle do cheap 'mario box' effect
    bne s2_m_b_blw_03_0c1
    jsr s2int_DO_greenBox

s2_m_b_blw_03_0c1
    lda #$01
    sta shipAdvNumPix          ; HITTING_BELOW = TRUE

    lda ENM_16_f3              ; copy SECTION3_Y2
    sta freeForAll2            ; to ANG_Y
    jsr S2_RECALC_SPR_ANG_Y

    lda LEVEL_WAVE
    cmp #$ff
    bne asdiogjn
    jsl sym_SFX_LASER_FIRE
    jmp s2_m_b_blw_FF
asdiogjn
    jsl sym_SFX_DEEP_THUMP

    jmp s2_m_b_blw_FF



;------------
s2_m_b_blw_04
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_17_f1              ; against SECTION4_X
    bpl s2_m_b_blw_04_0a       ; branch if(ANG_X2 > SECTION4_X)
    jmp s2_m_b_blw_05          ; ELSE check next section

s2_m_b_blw_04_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_17_f3              ; against SECTION4_X2
    bmi s2_m_b_blw_04_0b       ; branch if(ANG_X < SECTION4_X2)
    jmp s2_m_b_blw_05          ; ELSE check next section

s2_m_b_blw_04_0b

    lda ENM_18_f1              ; copy SECTION4_Y2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_18_f1              ; against SECTION4_Y2
    beq s2_m_b_blw_04_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION4_Y2+1
    beq s2_m_b_blw_04_0c       ; branch if equal
    dec a_byte2                ; increase a_byte2 once more
    lda freeForAll2            ; ANG_Y compare
    cmp a_byte2                ; against SECTION4_Y2+2
    beq s2_m_b_blw_04_0c       ; branch if equal
    jmp s2_m_b_blw_05          ; ELSE check next section

s2_m_b_blw_04_0c

    lda #$01
    sta shipAdvNumPix          ; HITTING_BELOW = TRUE

    lda ENM_18_f1              ; copy SECTION4_Y2
    sta freeForAll2            ; to ANG_Y
    jsr S2_RECALC_SPR_ANG_Y

    jsl sym_SFX_DEEP_THUMP
    jmp s2_m_b_blw_FF



;------------
s2_m_b_blw_05
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_18_f2              ; against SECTION5_X
    bpl s2_m_b_blw_05_0a       ; branch if(ANG_X2 > SECTION5_X)
    jmp s2_m_b_blw_06          ; ELSE check next section

s2_m_b_blw_05_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_13_patt            ; against SECTION5_X2
    bmi s2_m_b_blw_05_0b       ; branch if(ANG_X < SECTION5_X2)
    jmp s2_m_b_blw_06          ; ELSE check next section

s2_m_b_blw_05_0b

    lda ENM_14_patt            ; copy SECTION5_Y2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_14_patt            ; against SECTION5_Y2
    beq s2_m_b_blw_05_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION5_Y2+1
    beq s2_m_b_blw_05_0c       ; branch if equal
    dec a_byte2                ; increase a_byte2 once more
    lda freeForAll2            ; ANG_Y compare
    cmp a_byte2                ; against SECTION5_Y2+2
    beq s2_m_b_blw_05_0c       ; branch if equal
    jmp s2_m_b_blw_06          ; ELSE check next section

s2_m_b_blw_05_0c

    lda #$01
    sta shipAdvNumPix          ; HITTING_BELOW = TRUE

    lda ENM_14_patt            ; copy SECTION5_Y2
    sta freeForAll2            ; to ANG_Y
    jsr S2_RECALC_SPR_ANG_Y

    jsl sym_SFX_DEEP_THUMP
    jmp s2_m_b_blw_FF



;------------
s2_m_b_blw_06
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_15_patt            ; against SECTION6_X
    bpl s2_m_b_blw_06_0a       ; branch if(ANG_X2 > SECTION6_X)
    jmp s2_m_b_blw_FF_pre      ; ELSE check next section

s2_m_b_blw_06_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_17_patt            ; against SECTION6_X2
    bmi s2_m_b_blw_06_0b       ; branch if(ANG_X < SECTION6_X2)
    jmp s2_m_b_blw_FF_pre      ; ELSE check next section

s2_m_b_blw_06_0b

    lda ENM_18_patt            ; copy SECTION6_Y2
    sta a_byte2                ; to a_byte2
    dec a_byte2                ; decrease a_byte2 by one

    lda freeForAll2            ; ANG_Y compare
    cmp ENM_18_patt            ; against SECTION6_Y2
    beq s2_m_b_blw_06_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION6_Y2+1
    beq s2_m_b_blw_06_0c       ; branch if equal
    dec a_byte2                ; increase a_byte2 once more
    lda freeForAll2            ; ANG_Y compare
    cmp a_byte2                ; against SECTION6_Y2+2
    beq s2_m_b_blw_06_0c       ; branch if equal

s2_m_b_blw_FF_pre

    stz shipAdvNumPix          ; HITTING_BELOW = FALSE
    jmp s2_m_b_blw_FF          ; leave this routine

s2_m_b_blw_06_0c

    lda #$01
    sta shipAdvNumPix          ; HITTING_BELOW = TRUE

    lda ENM_18_patt            ; copy SECTION6_Y2
    sta freeForAll2            ; to ANG_Y
    jsr S2_RECALC_SPR_ANG_Y

    jsl sym_SFX_DEEP_THUMP


;------------
s2_m_b_blw_FF
;------------
    rts                        ; END OF S2_my_BOXCOLL_TOP





;=====================




; DESTROYS a_byte2

S2_my_BOXCOLL_TOP

;   if(ANG_X2 > SECTION1_X && ANG_X < SECTION1_X2) {
;       if(ANG_Y2 == SECTION1_Y || ANG_Y2 == (SECTION1_Y+1) || ANG_Y2 == (SECTION1_Y+2)) {
;           ANG_Y2 = SECTION1_Y;
;           IS_STANDING = TRUE;
;           HAS_LANDED = TRUE;
;           SECTION1_VERTLOCK = TRUE;
;           JUMP_VAL = 0;
;           return;
;       }
;   }

    jsr S2_RECALC_ANG_X
    jsr S2_RECALC_ANG_Y
    jsr S2_RECALC_ANG_X2
    jsr S2_RECALC_ANG_Y2

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_13_f1              ; against SECTION1_X
    bpl s2_m_b_top_01_0a       ; branch if(ANG_X2 > SECTION1_X)
    jmp s2_m_b_top_02          ; ELSE check next section

s2_m_b_top_01_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_13_f3              ; against SECTION1_X2
    bmi s2_m_b_top_01_0b       ; branch if(ANG_X < SECTION1_X2)
    jmp s2_m_b_top_02          ; ELSE check next section

s2_m_b_top_01_0b

    lda ENM_13_f2              ; copy SECTION1_Y
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll4            ; ANG_Y2 compare
    cmp ENM_13_f2              ; against SECTION1_Y
    beq s2_m_b_top_01_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION1_Y+1
    beq s2_m_b_top_01_0c       ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION1_Y+2
    beq s2_m_b_top_01_0c       ; branch if equal
    jmp s2_m_b_top_02          ; ELSE check next section

s2_m_b_top_01_0c

    lda #$01
    sta shipAnim               ; IS_STANDING = TRUE
    sta dec_vel_dir            ; HAS_LANDED = TRUE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    jsr s2ChkIfVRTLCK1isNeeded
    lda #$01
    sta ENM_17_numfr

    jmp s2_m_b_top_FF



;------------
s2_m_b_top_02
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_14_f2              ; against SECTION2_X
    bpl s2_m_b_top_02_0a       ; branch if(ANG_X2 > SECTION2_X)
    jmp s2_m_b_top_03          ; ELSE check next section

s2_m_b_top_02_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_15_f1              ; against SECTION2_X2
    bmi s2_m_b_top_02_0b       ; branch if(ANG_X < SECTION2_X2)
    jmp s2_m_b_top_03          ; ELSE check next section

s2_m_b_top_02_0b

    lda ENM_14_f3              ; copy SECTION2_Y
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll4            ; ANG_Y2 compare
    cmp ENM_14_f3              ; against SECTION2_Y
    beq s2_m_b_top_02_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION2_Y+1
    beq s2_m_b_top_02_0c       ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION2_Y+2
    beq s2_m_b_top_02_0c       ; branch if equal
    jmp s2_m_b_top_03          ; ELSE check next section

s2_m_b_top_02_0c

    lda #$01
    sta shipAnim               ; IS_STANDING = TRUE
    sta dec_vel_dir            ; HAS_LANDED = TRUE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    jsr s2ChkIfVRTLCK2isNeeded

    lda ENM_14_f3              ; copy SECTION2_Y
    sta freeForAll4            ; to ANG_Y2
    jsr S2_RECALC_SPR_ANG_Y2

    jmp s2_m_b_top_FF



;------------
s2_m_b_top_03
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_15_f3              ; against SECTION3_X
    bpl s2_m_b_top_03_0a       ; branch if(ANG_X2 > SECTION3_X)
    jmp s2_m_b_top_04          ; ELSE check next section

s2_m_b_top_03_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_16_f2              ; against SECTION3_X2
    bmi s2_m_b_top_03_0b       ; branch if(ANG_X < SECTION3_X2)
    jmp s2_m_b_top_04          ; ELSE check next section

s2_m_b_top_03_0b

    lda ENM_16_f1              ; copy SECTION3_Y
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll4            ; ANG_Y2 compare
    cmp ENM_16_f1              ; against SECTION3_Y
    beq s2_m_b_top_03_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION3_Y+1
    beq s2_m_b_top_03_0c       ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION3_Y+2
    beq s2_m_b_top_03_0c       ; branch if equal
    jmp s2_m_b_top_04          ; ELSE check next section

s2_m_b_top_03_0c

    lda #$01
    sta shipAnim               ; IS_STANDING = TRUE
    sta dec_vel_dir            ; HAS_LANDED = TRUE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    lda ENM_16_f1              ; copy SECTION3_Y
    sta freeForAll4            ; to ANG_Y2
    jsr S2_RECALC_SPR_ANG_Y2

    jmp s2_m_b_top_FF



;------------
s2_m_b_top_04
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_17_f1              ; against SECTION4_X
    bpl s2_m_b_top_04_0a       ; branch if(ANG_X2 > SECTION4_X)
    jmp s2_m_b_top_05          ; ELSE check next section

s2_m_b_top_04_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_17_f3              ; against SECTION4_X2
    bmi s2_m_b_top_04_0b       ; branch if(ANG_X < SECTION4_X2)
    jmp s2_m_b_top_05          ; ELSE check next section

s2_m_b_top_04_0b

    lda ENM_17_f2              ; copy SECTION4_Y
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll4            ; ANG_Y2 compare
    cmp ENM_17_f2              ; against SECTION4_Y
    beq s2_m_b_top_04_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION4_Y+1
    beq s2_m_b_top_04_0c       ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION4_Y+2
    beq s2_m_b_top_04_0c       ; branch if equal
    jmp s2_m_b_top_05          ; ELSE check next section

s2_m_b_top_04_0c

    lda #$01
    sta shipAnim               ; IS_STANDING = TRUE
    sta dec_vel_dir            ; HAS_LANDED = TRUE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    lda ENM_17_f2              ; copy SECTION4_Y
    sta freeForAll4            ; to ANG_Y2
    jsr S2_RECALC_SPR_ANG_Y2

    jmp s2_m_b_top_FF



;------------
s2_m_b_top_05
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_18_f2              ; against SECTION5_X
    bpl s2_m_b_top_05_0a       ; branch if(ANG_X2 > SECTION5_X)
    jmp s2_m_b_top_06          ; ELSE check next section

s2_m_b_top_05_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_13_patt            ; against SECTION5_X2
    bmi s2_m_b_top_05_0b       ; branch if(ANG_X < SECTION5_X2)
    jmp s2_m_b_top_06          ; ELSE check next section

s2_m_b_top_05_0b

    lda ENM_18_f3              ; copy SECTION5_Y
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll4            ; ANG_Y2 compare
    cmp ENM_18_f3              ; against SECTION5_Y
    beq s2_m_b_top_05_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION5_Y+1
    beq s2_m_b_top_05_0c       ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION5_Y+2
    beq s2_m_b_top_05_0c       ; branch if equal
    jmp s2_m_b_top_06          ; ELSE check next section

s2_m_b_top_05_0c

    lda #$01
    sta shipAnim               ; IS_STANDING = TRUE
    sta dec_vel_dir            ; HAS_LANDED = TRUE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    lda ENM_18_f3              ; copy SECTION5_Y
    sta freeForAll4            ; to ANG_Y2
    jsr S2_RECALC_SPR_ANG_Y2

    jmp s2_m_b_top_FF



;------------
s2_m_b_top_06
;------------

    lda freeForAll3            ; ANG_X2 compare
    cmp ENM_15_patt            ; against SECTION6_X
    bpl s2_m_b_top_06_0a       ; branch if(ANG_X2 > SECTION6_X)
    jmp s2_m_b_top_FF_pre      ; ELSE check next section

s2_m_b_top_06_0a

    lda freeForAll1            ; ANG_X compare
    cmp ENM_17_patt            ; against SECTION6_X2
    bmi s2_m_b_top_06_0b       ; branch if(ANG_X < SECTION6_X2)
    jmp s2_m_b_top_FF_pre      ; ELSE check next section

s2_m_b_top_06_0b

    lda ENM_16_patt            ; copy SECTION6_Y
    sta a_byte2                ; to a_byte2
    inc a_byte2                ; increase a_byte2 by one

    lda freeForAll4            ; ANG_Y2 compare
    cmp ENM_16_patt            ; against SECTION6_Y
    beq s2_m_b_top_06_0c       ; branch if equal
    cmp a_byte2                ; compare against SECTION6_Y+1
    beq s2_m_b_top_06_0c       ; branch if equal
    inc a_byte2                ; increase a_byte2 once more
    lda freeForAll4            ; ANG_Y2 compare
    cmp a_byte2                ; against SECTION6_Y+2
    beq s2_m_b_top_06_0c       ; branch if equal

s2_m_b_top_FF_pre

    stz shipAnim               ; IS_STANDING = FALSE
    stz dec_vel_dir            ; HAS_LANDED = FALSE

    lda ENM_18_numfr           ; if(!SECTION1_VERTTIME) stz SECTION1_VERTLOCK
    cmp #$00                   ; else jump to the end of the routine
    bne s2_m_b_top_FF
    stz ENM_17_numfr           ; SECTION1_VERTLOCK

    jmp s2_m_b_top_FF          ; leave this routine

s2_m_b_top_06_0c

    lda #$01
    sta shipAnim               ; IS_STANDING = TRUE
    sta dec_vel_dir            ; HAS_LANDED = TRUE
    stz areWeAtFirstBoss       ; JUMP_VAL = 0

    lda ENM_16_patt            ; copy SECTION6_Y
    sta freeForAll4            ; to ANG_Y2
    jsr S2_RECALC_SPR_ANG_Y2



;------------
s2_m_b_top_FF
;------------
    rts                        ; END OF S2_my_BOXCOLL_TOP

;----------------------------------------------------------------

s2ChkIfVRTLCK1isNeeded

    lda LEVEL_WAVE
    cmp #$03
    bne s2CIVRTLCK_false

    lda #$03
    sta ENM_18_numfr           ; SECTION1_VERTTIME
    lda #$01
    sta ENM_17_numfr           ; SECTION1_VERTLOCK = TRUE
    sta shipAnim               ; IS_STANDING = TRUE
    sta dec_vel_dir            ; HAS_LANDED = TRUE
    ; Legg til IS_STANDING = TRUE
;   sta shipAdvHalfFlag        ; IS_JUMPING = TRUE

    lda ENM_13_f2              ; copy SECTION1_Y
    sta freeForAll4            ; to ANG_Y2
    jsr S2_RECALC_SPR_ANG_Y2
    jmp s2CIVRTLCK_rts

s2CIVRTLCK_false
;   stz ENM_17_numfr           ; SECTION1_VERTLOCK = FALSE
s2CIVRTLCK_rts
    rts



s2ChkIfVRTLCK2isNeeded
;   stz BUL_21_time            ; SECTION2_VERTLOCK = FALSE
    rts

;================================================================



S2_CHECK_ROOM_EXITS
;==================

    lda LEVEL_WAVE             ; load current room number
    cmp #$ff
    beq s2_chk_r_exts_ff_sym
    cmp #$01
    beq s2_chk_r_exts_01_sym
    cmp #$02
    beq s2_chk_r_exts_02_sym
    cmp #$03
    beq s2_chk_r_exts_03_sym

s2_chk_r_exts_ff_sym
    jmp s2_chk_r_exts_ff
s2_chk_r_exts_01_sym
    jmp s2_chk_r_exts_01
s2_chk_r_exts_02_sym
    jmp s2_chk_r_exts_02
s2_chk_r_exts_03_sym
    jmp s2_chk_r_exts_03

;----
; shiori
s2_chk_r_exts_ff
    jmp s2_chk_r_exts_ENDE

;=======

s2_chk_r_exts_01

    lda SPRBUF1+SX             ; if(Angela.X == 205) do load ROOM_02
    cmp #205
    beq s2_chk_r_exts_01_0a
    jmp s2_chk_r_exts_ENDE

s2_chk_r_exts_01_0a

    lda #$02
    sta GOOD_ENDING

    jsr s2_chk_r_exts_scrnOFF
    jsr S2_ROOM02_SETUP

    lda #22
    sta SPRBUF1+SX
    lda #181
    sta SPRBUF1+SY
    jsr s2_chk_r_exts_calcAngCoords

    LOAD_BLOCK2VRAM S2_ROOM_02_MAP,$0000,$2000
    wai
    wai
    LOAD_BLOCK2VRAM S2_ROOM_02_PIC,$1000,$0800
    wai
    wai

    ; ball 'n chain setup
    stz ENM_16_hp              ; ball 'n chain interval
    ldy #$0000                 ; ball 'n chain data pointer
                               ; DO NOT USE THIS REGISTER TO ANYTHING
                               ; OTHER THAN THE BALL 'n CHAIN ROUTINE
                               ; OR ELSE YOU'LL GET (NOT) FUNNY RESULTS =]
    lda #112
    sta SPRBUF1+SX1
    lda #146
    sta SPRBUF1+SY1
    lda #120
    sta SPRBUF1+SX2
    sta SPRBUF1+SX3
    sta SPRBUF1+SX4
    lda #141
    sta SPRBUF1+SY2
    lda #126
    sta SPRBUF1+SY3
    lda #110
    sta SPRBUF1+SY4

    lda #%00110010
    sta SPRBUF1+SPRIORITY1
    sta SPRBUF1+SPRIORITY2
    sta SPRBUF1+SPRIORITY3
    sta SPRBUF1+SPRIORITY4
    lda #$4c
    sta SPRBUF1+SFRAME1
    lda #$80
    sta SPRBUF1+SFRAME2
    sta SPRBUF1+SFRAME3
    sta SPRBUF1+SFRAME4
    wai


    jsr s2_chk_r_exts_scrnON
    jmp s2_chk_r_exts_ENDE

;=======

s2_chk_r_exts_02

    lda SPRBUF1+SX             ; if(Angela.X == 19) do load ROOM_01
    cmp #19
    beq s2_chk_r_exts_02_0a

    lda SPRBUF1+SX
    cmp #205
    beq s2_chk_r_exts_02_0b    ; if(Angela.X == 205) do load ROOM_03
    jmp s2_chk_r_exts_ENDE

s2_chk_r_exts_02_0a

    lda #$01
    sta GOOD_ENDING

    jsr s2_chk_r_exts_scrnOFF
    jsr S2_ROOM01_SETUP

    lda #199
    sta SPRBUF1+SX
    lda #181
    sta SPRBUF1+SY
    jsr s2_chk_r_exts_calcAngCoords

    LOAD_BLOCK2VRAM S2_ROOM_01_MAP,$0000,$2000
    wai
    wai
    LOAD_BLOCK2VRAM S2_ROOM_01_PIC,$1000,$0800
    wai
    wai

    jsr s2_chk_r_exts_scrnON
    jmp s2_chk_r_exts_ENDE
;----
s2_chk_r_exts_02_0b

    lda #$04
    sta GOOD_ENDING

    jsr s2_chk_r_exts_scrnOFF
    jsr S2_ROOM03_SETUP

    lda #22
    sta SPRBUF1+SX
    lda #149
    sta SPRBUF1+SY
    jsr s2_chk_r_exts_calcAngCoords

    LOAD_BLOCK2VRAM S2_ROOM_03_MAP,$0000,$2000
    wai
    wai
    LOAD_BLOCK2VRAM S2_ROOM_03_PIC,$1000,$0800
    wai
    wai

    jsr s2_chk_r_exts_scrnON
    jmp s2_chk_r_exts_ENDE


;========


s2_chk_r_exts_03

    lda SPRBUF1+SX             ; if(Angela.X == 19) do load ROOM_02
    cmp #19
    beq s2_chk_r_exts_03_0a
    jmp s2_chk_r_exts_ENDE

s2_chk_r_exts_03_0a

    lda #$03
    sta GOOD_ENDING

    jsr s2_chk_r_exts_scrnOFF
    jsr S2_ROOM02_SETUP

    lda #199
    sta SPRBUF1+SX
    lda #149
    sta SPRBUF1+SY
    jsr s2_chk_r_exts_calcAngCoords

    LOAD_BLOCK2VRAM S2_ROOM_02_MAP,$0000,$2000
    wai
    wai
    LOAD_BLOCK2VRAM S2_ROOM_02_PIC,$1000,$0800
    wai
    wai

    ; ball 'n chain setup
    stz ENM_16_hp              ; ball 'n chain interval
    ldy #$0000                 ; ball 'n chain data pointer
                               ; DO NOT USE THIS REGISTER TO ANYTHING
                               ; OTHER THAN THE BALL 'n CHAIN ROUTINE
                               ; OR ELSE YOU'LL GET (NOT) FUNNY RESULTS =]
    lda #112
    sta SPRBUF1+SX1
    lda #146
    sta SPRBUF1+SY1
    lda #120
    sta SPRBUF1+SX2
    sta SPRBUF1+SX3
    sta SPRBUF1+SX4
    lda #141
    sta SPRBUF1+SY2
    lda #126
    sta SPRBUF1+SY3
    lda #110
    sta SPRBUF1+SY4

    lda #%00110010
    sta SPRBUF1+SPRIORITY1
    sta SPRBUF1+SPRIORITY2
    sta SPRBUF1+SPRIORITY3
    sta SPRBUF1+SPRIORITY4
    lda #$4c
    sta SPRBUF1+SFRAME1
    lda #$80
    sta SPRBUF1+SFRAME2
    sta SPRBUF1+SFRAME3
    sta SPRBUF1+SFRAME4
    wai

    jsr s2_chk_r_exts_scrnON
    jmp s2_chk_r_exts_ENDE


;========

s2_chk_r_exts_ENDE
    rts
s2_chk_r_exts_scrnOFF
    lda #$80
    sta $2100
    rts
s2_chk_r_exts_scrnON
    lda #$0f
    sta $2100
    rts
s2_chk_r_exts_calcAngCoords
    jsr S2_RECALC_ANG_X
    jsr S2_RECALC_ANG_Y
    jsr S2_RECALC_ANG_X2
    jsr S2_RECALC_ANG_Y2
    rts



;==============================================================
; ROOM "ACTION-SCRIPT" HANDLER SYSTEM v0.0.1 final
;==============================================================

S2_ACTION_SCRIPT_r01
;-------------------
    lda SPRBUF1+SY
    cmp #187
    beq ACTION_angelaDecLife1
    cmp #188
    beq ACTION_angelaDecLife1
    cmp #189
    beq ACTION_angelaDecLife1

    lda SPRBUF1+SX
    cmp #96
    bpl sleeping_with_the_dishes

    lda SPRBUF1+SY
    cmp #90
    beq ACTION_angelaDecLife1
    cmp #89
    beq ACTION_angelaDecLife1
    cmp #88
    beq ACTION_angelaDecLife1
    cmp #87
    beq ACTION_angelaDecLife1
    cmp #86
    beq ACTION_angelaDecLife1
    cmp #85
    beq ACTION_angelaDecLife1
    cmp #84
    beq ACTION_angelaDecLife1
    cmp #83
    beq ACTION_angelaDecLife1
    cmp #82
    beq ACTION_angelaDecLife1
sleeping_with_the_dishes
    jmp S2_ACTION_SCRIPT_unnagjort
ACTION_angelaDecLife1
    jsr S2_ANGELA_MEETS_SPIKE

    lda ang_lives
    cmp #$00
    beq S2_GOVER_r01

    lda #%00001111
    sta $2106
    lda #$0f
    sta $2100
    jmp sleeping_with_the_dishes

S2_GOVER_r01
    jml GAME_OVER

;===================

S2_ACTION_SCRIPT_r02
;-------------------
    lda SPRBUF1+SY
    cmp #187
    beq ACTION_angelaDecLife2
    cmp #188
    beq ACTION_angelaDecLife2
    cmp #189
    beq ACTION_angelaDecLife2
tulleball
    jmp S2_ACTION_SCRIPT_unnagjort
ACTION_angelaDecLife2
    jsr S2_ANGELA_MEETS_SPIKE

    lda ang_lives
    cmp #$00
    beq S2_GOVER_r02

    lda #%00001111
    sta $2106
    lda #$0f
    sta $2100
    jmp tulleball

S2_GOVER_r02
    jml GAME_OVER

;===================


S2_ACTION_SCRIPT_r03
;-------------------
    ; ENM_14_numfr = what direction. up/down (0|1)
    ; ENM_15_numfr = loop counter
    ; ENM_16_numfr = block movement delay
    inc ENM_16_numfr
    lda ENM_16_numfr
    cmp #$02
    bne +
    stz ENM_16_numfr

+   lda ENM_14_numfr
    cmp #$00
    bne s2actnScrptR03_moveDownNotUp

;---
    lda ENM_16_numfr
    cmp #$01
    bne +
;   lda #$88
;   sta SPRBUF1+SFRAME1        ; the moving block is ANGRY 
    lda #$84
    sta SPRBUF1+SFRAME1        ; the moving block is HAPPY
    dec SPRBUF1+SY1
;---
+   lda SPRBUF1+SY1
    cmp #193
    beq s2actnScrptR03_Ouch
    cmp #194
    beq s2actnScrptR03_Ouch
    cmp #195
    beq s2actnScrptR03_Ouch
    cmp #196
    beq s2actnScrptR03_Ouch
    cmp #197
    beq s2actnScrptR03_Ouch
    cmp #198
    beq s2actnScrptR03_Ouch
    cmp #199
    beq s2actnScrptR03_Ouch
    cmp #200
    beq s2actnScrptR03_Ouch

    cmp #108
    beq s2actnScrptR03_changeToDOWN
    jmp s2actnScrptR03_ende

s2actnScrptR03_moveDownNotUp
;---
    lda ENM_16_numfr
    cmp #$01
    bne +
    lda #$84
    sta SPRBUF1+SFRAME1        ; the moving block is HAPPY
    inc SPRBUF1+SY1
;---
+   lda SPRBUF1+SY1
    cmp #193
    beq s2actnScrptR03_Ouch
    cmp #194
    beq s2actnScrptR03_Ouch
    cmp #195
    beq s2actnScrptR03_Ouch
    cmp #196
    beq s2actnScrptR03_Ouch
    cmp #197
    beq s2actnScrptR03_Ouch
    cmp #198
    beq s2actnScrptR03_Ouch
    cmp #199
    beq s2actnScrptR03_Ouch
    cmp #200
    beq s2actnScrptR03_Ouch

    cmp #201
    beq s2actnScrptR03_changeToUP
    jmp s2actnScrptR03_ende

s2actnScrptR03_changeToUP
    lda #$c0
    sta SPRBUF1+SFRAME1
    stz ENM_14_numfr
    jmp s2actnScrptR03_ende
s2actnScrptR03_changeToDOWN
    lda #$01
    sta ENM_14_numfr
    jmp s2actnScrptR03_ende
s2actnScrptR03_Ouch
    lda #$c0
    sta SPRBUF1+SFRAME1

s2actnScrptR03_ende

    ; sprite->section update
    lda SPRBUF1+SX1
    sta ENM_13_f1              ; SECTION1_X
    sta ENM_13_f3              ; SECTION1_X2
    lda SPRBUF1+SY1
    sta ENM_13_f2              ; SECTION1_Y
    sta ENM_14_f1              ; SECTION1_Y2

    ; add 15 to SECTION1_X2 and SECTION1_Y2
    lda #$0f
    sta ENM_15_numfr           ; set loop counter
s2actnScrptR03_addLoop
    inc ENM_13_f3
    inc ENM_14_f1
    dec ENM_15_numfr
    lda ENM_15_numfr
    cmp #$00
    bne s2actnScrptR03_addLoop


    lda ENM_17_numfr           ; has SECTION1_VERTLOCK been toggled?
    cmp #$00
    beq s2actnScrptR03_endeligEnde
    lda ENM_13_f2              ; Angela.Y2 = SECTION1_Y
    sta freeForAll4
    jsr S2_RECALC_SPR_ANG_Y2

    lda #$01
    sta ENM_17_numfr           ; SECTION1_VERTLOCK = TRUE
    sta shipAnim               ; IS_STANDING = TRUE
    sta dec_vel_dir            ; HAS_LANDED = TRUE
s2actnScrptR03_endeligEnde

    ;----------STAGE 2 END---------------------
    lda SPRBUF1+SY
    cmp #220
    beq S2_END_REACHED
    cmp #221
    beq S2_END_REACHED
    cmp #222
    beq S2_END_REACHED
    cmp #223
    beq S2_END_REACHED
    jmp S2_END_NOT_REACHED
S2_END_REACHED
    wai
    wai
    wai
    lda #$80
    sta $2100                  ; screen off, lowest brightness
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    lda #%00000010
    sta $212c                  ; bg#2 only

    LOAD_PAL THE_END_PAL
    wai
    LOAD_BLOCK2VRAM THE_END_MAP,$0000,$2000
    wai
    wai
    LOAD_BLOCK2VRAM THE_END_PIC,$1000,$0800
    wai
    wai
    LOAD_BLOCK2VRAM FONT2_TILES,$2000,$1800
    wai
    wai
    jsl sym_MUS_SONG_AXELAY

    jsl INITIALIZE_HUD_PAL
    stz $2121
    lda #$17
    sta $2122
    stz $2122
    lda #$1f
    sta $2122
    stz $2122
    lda #$17
    sta $2122
    stz $2122

    jsl bH0_SPLSC_CLR

    ldx #$0000
    stx MAPX
    ldx #$ffff
    stx MAPY

    phy
    ldy #$001e
konosekai
    ldx MAPY
    dex
    stx MAPY
    dey
    cpy #$0000
    bne konosekai
    ply
    wai
    wai
    wai

    GOTOXY 3,7
    jsl bH0_PRINTZ_END_00
    GOTOXY 4,7
    jsl bH0_PRINTZ_END_00a

    lda #$0f
    sta $2100

    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony

    jsr myFirstPony
    GOTOXY 7,2
    jsl bH0_PRINTZ_END_01
    jsr myFirstPony
    GOTOXY 9,2
    jsl bH0_PRINTZ_END_02
    jsr myFirstPony
    GOTOXY 11,2
    jsl bH0_PRINTZ_END_03
    jsr myFirstPony
    GOTOXY 13,2
    jsl bH0_PRINTZ_END_04
    jsr myFirstPony
    GOTOXY 15,2
    jsl bH0_PRINTZ_END_05
    jsr myFirstPony
    GOTOXY 17,2
    jsl bH0_PRINTZ_END_06
    jsr myFirstPony
    GOTOXY 19,2
    jsl bH0_PRINTZ_END_07

    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony

    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony

    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony

    lda #%00000011
    sta $212c                  ; re-enable bg#1
    jsr msgTHE_END_fadeIn

    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony
    jsr myFirstPony

    jml GAME_OVER

myFirstPony
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    wai
    rts

mySecondPony
    wai
    wai
    wai
    wai
    rts

msgTHE_END_fadeIn
    lda #%11110001
    sta $2106
    jsr mySecondPony
    lda #%11100001
    sta $2106
    jsr mySecondPony
    lda #%11010001
    sta $2106
    jsr mySecondPony
    lda #%11000001
    sta $2106
    jsr mySecondPony
    lda #%10110001
    sta $2106
    jsr mySecondPony
    lda #%10100001
    sta $2106
    jsr mySecondPony
    lda #%10010001
    sta $2106
    jsr mySecondPony
    lda #%10000001
    sta $2106
    jsr mySecondPony
    lda #%01110001
    sta $2106
    jsr mySecondPony
    lda #%01100001
    sta $2106
    jsr mySecondPony
    lda #%01010001
    sta $2106
    jsr mySecondPony
    lda #%01000001
    sta $2106
    jsr mySecondPony
    lda #%00110001
    sta $2106
    jsr mySecondPony
    lda #%00100001
    sta $2106
    jsr mySecondPony
    lda #%00010001
    sta $2106
    jsr mySecondPony
    lda #%00001111
    sta $2106
    jsr mySecondPony
    rts


S2_END_NOT_REACHED

    ;----------ROOM 03 SPIKES KILL-------------
    lda SPRBUF1+SX
    cmp #185
    beq ACTION_angelaDecLife3
    cmp #186
    beq ACTION_angelaDecLife3
    cmp #187
    beq ACTION_angelaDecLife3

    lda SPRBUF1+SX
    cmp #64
    bmi farang_dingdong
    jmp creative_name_jump ; ..or the lack of creativity :-/
creative_name_jump
    lda SPRBUF1+SX
    cmp #176
    bpl sex_on_top_of_little_fishes
farang_dingdong
    lda SPRBUF1+SY
    cmp #187
    beq ACTION_angelaDecLife3
    cmp #188
    beq ACTION_angelaDecLife3
    cmp #189
    beq ACTION_angelaDecLife3
    cmp #190
    beq ACTION_angelaDecLife3
    cmp #191
    beq ACTION_angelaDecLife3
    cmp #192
    beq ACTION_angelaDecLife3
    cmp #193
    beq ACTION_angelaDecLife3
    cmp #194
    beq ACTION_angelaDecLife3
    cmp #195
    beq ACTION_angelaDecLife3
    cmp #196
    beq ACTION_angelaDecLife3
    cmp #197
    beq ACTION_angelaDecLife3
    cmp #198
    beq ACTION_angelaDecLife3
sex_on_top_of_little_fishes
    jmp S2_ACTION_SCRIPT_unnagjort
ACTION_angelaDecLife3
    jsr S2_ANGELA_MEETS_SPIKE

    lda ang_lives
    cmp #$00
    beq S2_GOVER_r03

    lda #%00001111
    sta $2106
    lda #$0f
    sta $2100

    jmp sex_on_top_of_little_fishes

S2_GOVER_r03
    jml GAME_OVER



;==============================================================

; elin


STAGE2_INTRO_SEQUENCE

    jsl bH12_INIT_VIDEO_04
    lda #$80
    sta $2100
    lda #$03
    sta $212c                  ; disable all, but bg#1 and bg#2
    jsl sym_MUS_NO_SONG
    ldx #$0000
    stx MAPX
    stx MAPY

    LOAD_PAL S2AMBLOOD_PAL
    wai
    LOAD_BLOCK2VRAM S2AMBLOOD_MAP,$0000,$2000
    wai
    wai
    LOAD_BLOCK2VRAM S2AMBLOOD_PIC,$2000,$6000
    wai
    wai
    LOAD_BLOCK2VRAM S1AM_S1B_SEQ_FONT,$5000,$0800
    wai
    wai

    jsl bH0_SPLSC_CLR

    stz a_byte                 ; "A" button animation var

    jsl bH13_FADE_IN_MOSAIC_macro
    ldx #$6f
    jsl bH12_WAIT_FOR_THE_HOLY_WAI

    jsl bH0_PRINTZ_S2AMCHAT_01
    ldx #$34
    jsl bH12_WAIT_FOR_THE_HOLY_WAI
    jsl bH0_PRINTZ_S2AMCHAT_02
    ldx #$68
    jsl bH12_WAIT_FOR_THE_HOLY_WAI
    jsl bH0_PRINTZ_S2AMCHAT_03
    ldx #$6e
    jsl bH12_WAIT_FOR_THE_HOLY_WAI

    ; Big eyed Manko-chan, please!
    jsl bH0_PRINTZ_S2AMCHAT_BIGEYES
    ldx #$0a
    jsl bH12_WAIT_FOR_THE_HOLY_WAI

    ldy #$000a

stg2intseq_blnkArrwsLOOP

    jsl bH0_PRINTZ_S2AMCHAT_ARROWS
    jsl sym_SFX_PING_LOW
    ldx #$0c
    jsl bH12_WAIT_FOR_THE_HOLY_WAI
    jsl bH0_PRINTZ_S2AMCHAT_undoARROWS
    ldx #$0c
    jsl bH12_WAIT_FOR_THE_HOLY_WAI
    dey
    cpy #$0000
    bne stg2intseq_blnkArrwsLOOP

    ldx #$0a
    jsl bH12_WAIT_FOR_THE_HOLY_WAI
    jsl bH0_PRINTZ_S2AMCHAT_undoBIGEYES
    ldx #$16
    jsl bH12_WAIT_FOR_THE_HOLY_WAI

    jsl bH0_PRINTZ_S2AMCHAT_04
    ldx #$1a
    jsl bH12_WAIT_FOR_THE_HOLY_WAI
    jsl bH0_PRINTZ_S2AMCHAT_05
    ldx #$34
    jsl bH12_WAIT_FOR_THE_HOLY_WAI

stg2intseq_AbuttonLoop
    wai
    wai
    wai
    wai
    wai
    wai

    inc a_byte
    lda a_byte
    cmp #$04
    bne stg2intseq_drwPROCEED
    stz a_byte

stg2intseq_drwPROCEED
    jsl bH0_PRINTZ_angelManko_AbuttAnim
    lda joy1                   ; loop until the 'A' button gets pressed
    and #$80
    beq stg2intseq_AbuttonLoop ; loop if button !pressed

    GOTOXY 24,30
    jsl bH0_PRINTZ_singleEraser
    stz a_byte


    jsl bH13_FADE_OUT_MOSAIC_macro
    lda #$80
    sta $2100
    jsl bH0_SPLSC_CLR

;--------------------
;----- SUPER MARIO WORLD SPOOF BEHOLD, PLEASE!
    lda #%00010001
    sta $212c                  ; bg#1 and oam enable

    ldx #$0000
    stx MAPX
    ldx #$ffff
    stx MAPY

    stz anm_flag0              ; '666' fade counter
    stz anm_flag1              ; '666' fade delay

    stz anm_timer              ; green box timer
    stz ENM_bulletHitENM       ; door anim (0-31 whereas 0 is shut closed)
    stz ENM_killCounter        ; "door restore" delay
    stz what_fade              ; time to wait before closing door

    stz ENM_13_hp              ; angela frame number
    stz ENM_14_hp              ; angela walk delay
    stz ENM_15_hp              ; angela duck frame #1 lockout
    stz ENM_16_hp              ; ball 'n chain interval

    stz a_byte                 ; "floating point" helper for Angela falling

    jsl TOTALLY_RESET_32x32_SPRITES
    stz SPRBUF2
    stz SPRBUF2+1


    LOAD_PAL S2INT_OUTSIDE_CASTLE_PAL
    wai
    LOAD_BLOCK2VRAM S2INT_OUTSIDE_CASTLE_MAP,$0000,$2000
    wai
    wai
    wai
    LOAD_BLOCK2VRAM S2INT_OUTSIDE_CASTLE_PIC,$2000,$6000
    wai
    wai
    wai
    LOAD_BLOCK2VRAM S2INT_OUTSIDE_SPRSET,$6000,$2000
    wai
    wai
    wai


    jsr S2_OUTSIDE_SETUP


    lda #216                   ; 'angela-enter-castle-over-lap-sprite' ;)
    sta SPRBUF1+SX
    lda #180
    sta SPRBUF1+SY
    lda #$84
    sta SPRBUF1+SFRAME
    stz SPRBUF1+SPRIORITY

    lda #24                    ; Angela
    sta SPRBUF1+SX1
    lda #181
    sta SPRBUF1+SY1
    stz SPRBUF1+SFRAME1
    lda #%00110100
    sta SPRBUF1+SPRIORITY1

    lda #168                   ; next two sprites are there to overlap
    sta SPRBUF1+SX2            ; the big wooden door.
    lda #112
    sta SPRBUF1+SY2
    lda #$84
    sta SPRBUF1+SFRAME2
    stz SPRBUF1+SPRIORITY2

    lda #200
    sta SPRBUF1+SX3
    lda #112
    sta SPRBUF1+SY3
    lda #$84
    sta SPRBUF1+SFRAME3
    stz SPRBUF1+SPRIORITY3

    lda #168                   ; these four sprites are the wooden door.
    sta SPRBUF1+SX4
    lda #144
    sta SPRBUF1+SY4
    lda #$4c
    sta SPRBUF1+SFRAME4
    lda #$30
    sta SPRBUF1+SPRIORITY4

    lda #200
    sta SPRBUF1+SX5
    lda #144
    sta SPRBUF1+SY5
    lda #$80
    sta SPRBUF1+SFRAME5
    lda #$30
    sta SPRBUF1+SPRIORITY5

    lda #168
    sta SPRBUF1+SX6
    lda #176
    sta SPRBUF1+SY6
    lda #$c0
    sta SPRBUF1+SFRAME6
    lda #$30
    sta SPRBUF1+SPRIORITY6

    lda #200
    sta SPRBUF1+SX7
    lda #176
    sta SPRBUF1+SY7
    lda #$c4
    sta SPRBUF1+SFRAME7
    lda #$30
    sta SPRBUF1+SPRIORITY7

    jsl sym_MUS_SONG_AWOLF

    jsr S2_RECALC_ANG_X
    jsr S2_RECALC_ANG_Y
    jsr S2_RECALC_ANG_X2
    jsr S2_RECALC_ANG_Y2

    wai
    lda #$0f
    sta $2100

dloop

    jmp s2int_testeLoopen      ; strange naming, no!?
    rtl

;-------

s2int_BLNK_3x6
    inc anm_flag1
    lda anm_flag1
    cmp #$08
    beq blnk3x6_OK
    jmp blnk3x6_fin
blnk3x6_OK
    stz anm_flag1

    lda #$4f                   ; cgram pal node #79
    sta $2121

    lda anm_flag0
    cmp #$00
    beq blnk3x6_0
    cmp #$01
    beq blnk3x6_1
    cmp #$02
    beq blnk3x6_2
    cmp #$03
    beq blnk3x6_3
    cmp #$04
    beq blnk3x6_4
    cmp #$05
    beq blnk3x6_5
    cmp #$06
    beq blnk3x6_6
    cmp #$07
    beq blnk3x6_5
    cmp #$08
    beq blnk3x6_4
    cmp #$09
    beq blnk3x6_3
    cmp #$0a
    beq blnk3x6_2
    cmp #$0b
    beq blnk3x6_1

blnk3x6_continues
    stz $2122
    inc anm_flag0
    lda anm_flag0
    cmp #$0c
    bne blnk3x6_fin
    stz anm_flag0
    jmp blnk3x6_fin


blnk3x6_0
    lda #%01111100
    sta $2122
    jmp blnk3x6_continues
blnk3x6_1
    lda #%00101100
    sta $2122
    jmp blnk3x6_continues
blnk3x6_2
    lda #%01010100
    sta $2122
    jmp blnk3x6_continues
blnk3x6_3
    lda #%00000100
    sta $2122
    jmp blnk3x6_continues
blnk3x6_4
    lda #%01101000
    sta $2122
    jmp blnk3x6_continues
blnk3x6_5
    lda #%00110000
    sta $2122
    jmp blnk3x6_continues
blnk3x6_6
    lda #%01000000
    sta $2122
    jmp blnk3x6_continues
blnk3x6_fin
    rts


;-------


s2int_testeLoopen
    wai
    jsr s2int_BLNK_3x6
    jsr s2int_UNDO_greenBox
    jsr s2int_PROCESS_INPUT
    jsr s2int_DRAW_ANGELA
    jsr S2_my_BOXCOLL_LEFTSIDE
    jsr S2_my_BOXCOLL_RIGHTSIDE

    lda shipAdvHalfFlag        ; skip my_BOXCOLL_TOP if IS_JUMPING = TRUE
    cmp #$01
    beq is2_skipTopChk
    jsr S2_my_BOXCOLL_TOP

is2_skipTopChk

    lda shipAnim               ; IS_STANDING?
    cmp #$00
    bne is2_skipFalling
    stz dec_vel_dir            ; HAS_LANDED = FALSE
    inc SPRBUF1+SY1

is2_skipFalling

    lda areWeAtFirstBoss       ; reached maximum JUMP_VAL?
    cmp #32                    ; if > skip the jumping part
    bpl is2_notJmpng

    lda shipAdvHalfFlag        ; if IS_JUMPING then
    cmp #$01                   ; decrease SPRBUF1+SY by 3
    bne is2_endOfMainLoop

    lda areWeAtFirstBoss       ; this makes for a smooth-fluid-
    cmp #32                    ; defying-gravity kinda max jump ;)
    beq is2_skpFlng_decBy1
    cmp #31
    beq is2_skpFlng_decBy1
    cmp #30
    beq is2_skpFlng_decBy1
    cmp #29
    beq is2_skpFlng_decBy1
    cmp #28
    beq is2_skpFlng_decBy1
    cmp #27
    beq is2_skpFlng_decBy2
    cmp #26
    beq is2_skpFlng_decBy2
    cmp #25
    beq is2_skpFlng_decBy2
    cmp #24
    beq is2_skpFlng_decBy2
;is2_skpFlng_decBy3
    dec SPRBUF1+SY1
is2_skpFlng_decBy2
    dec SPRBUF1+SY1
is2_skpFlng_decBy1
    dec SPRBUF1+SY1
    inc areWeAtFirstBoss       ; JUMP_VAL++

    jsr S2_my_BOXCOLL_BELOW
    lda shipAdvNumPix          ; if HITTING_BELOW = TRUE
    cmp #$00                   ; then set IS_JUMPING to FALSE
    beq is2_endOfMainLoop
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE

    
is2_notJmpng
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE


is2_endOfMainLoop

    lda SPRBUF1+SX1
    cmp #$b4
    bmi is2_doNotSyncY
    lda SPRBUF1+SY1            ; makes Angela's overlap sprite (past the door)
    sta SPRBUF1+SY             ; keeping up with Angela's y-value.
is2_doNotSyncY
    jsr S2_CHECK_ROOM_EXITS    ; check if Angela wants to leave the room
    jmp s2int_testeLoopen


;---------


s2int_DRAW_ANGELA

    ; if(IS_JUMPING && !HAS_LANDED && !IS_STANDING)
    ;     do draw Angela jumping sprite

    lda shipAdvHalfFlag        ; IS_JUMPING
    cmp #$01
    bne is2_drw_ang_notJumping
    lda dec_vel_dir            ; HAS_LANDED
    cmp #$00
    bne is2_drw_ang_notJumping
    lda shipAnim               ; IS_STANDING
    cmp #$00
    bne is2_drw_ang_notJumping

    lda #$44                   ; angela jumping frame
    sta SPRBUF1+SFRAME1
    jmp is2_drAng_ff

is2_drw_ang_notJumping
    ; if(!IS_JUMPING && !HAS_LANDED && !IS_STANDING)
    ;     do draw Angela falling sprite

    lda shipAdvHalfFlag        ; IS_JUMPING
    cmp #$00
    bne is2_drw_ang_notFalling
    lda dec_vel_dir            ; HAS_LANDED
    cmp #$00
    bne is2_drw_ang_notFalling
    lda shipAnim               ; IS_STANDING
    cmp #$00
    bne is2_drw_ang_notFalling


is2_drw_ang_falling
    lda #$48                   ; angela falling frame
    sta SPRBUF1+SFRAME1
    jmp is2_drAng_ff
    ;--------


is2_drw_ang_notFalling

    lda ENM_13_hp
    cmp #$00                   ; walking anims
    beq is2_drAng_00
    cmp #$01
    beq is2_drAng_01
    cmp #$02
    beq is2_drAng_00
    cmp #$03
    beq is2_drAng_02
    cmp #$04
    beq is2_drAng_00
    cmp #$05
    beq is2_drAng_01
    ;---
    cmp #$06
    beq is2_drAng_03
    cmp #$07
    beq is2_drAng_04
    jmp is2_drAng_ff
is2_drAng_00                   ; angela still frame
    stz SPRBUF1+SFRAME1
    jmp is2_drAng_ff
is2_drAng_01                   ; angela walking frame #1/2
    lda #$04
    sta SPRBUF1+SFRAME1
    jmp is2_drAng_ff
is2_drAng_02                   ; angela walking frame #2/2
    lda #$08
    sta SPRBUF1+SFRAME1
    jmp is2_drAng_ff
is2_drAng_03                   ; angela ducking frame #1/2
    lda #$0c
    sta SPRBUF1+SFRAME1
    jmp is2_drAng_ff
is2_drAng_04                   ; angela ducking frame #2/2
    lda #$40
    sta SPRBUF1+SFRAME1
is2_drAng_ff
    rts


;---------


s2int_PROCESS_INPUT

    lda SPRBUF1+SX1
    cmp #214
    beq is2_castleEntered
    cmp #215
    beq is2_castleEntered
    cmp #216
    beq is2_castleEntered
    jmp is2_castleNotEntered
is2_castleEntered
    lda #$c8
    sta SPRBUF1+SFRAME1
    wai
is2_DOORCLOSE
    wai
    jsl sym_SFX_LASER_FIRE
    jsr s2int_BLNK_3x6
    lda what_fade              ; time to wait before closing door
    cmp #$00
    beq is2_pI_DRESTORE
    dec what_fade
    jmp is2_prcsInpt_skipDoorClose
is2_pI_DRESTORE
    inc ENM_killCounter        ; "door restore" delay
    lda ENM_killCounter
    cmp #$06
    bne is2_prcsInpt_skipDoorClose
    stz ENM_killCounter
    lda ENM_bulletHitENM       ; door anim
    cmp #$00
    beq is2_prcsInpt_skipDoorClose2
    dec ENM_bulletHitENM
    jsr iDoorAnim
is2_prcsInpt_skipDoorClose
    jmp is2_DOORCLOSE
is2_prcsInpt_skipDoorClose2
    jsl bH13_FADE_OUT_macro
    jsl TOTALLY_RESET_32x32_SPRITES
    wai
    jmp is2_GETOUTTA_S2INTSEQ
is2_castleNotEntered

    inc a_byte
    lda a_byte
    cmp #$02
    bne is2_prcsInpt_aByteNotReset
    stz a_byte
is2_prcsInpt_aByteNotReset

    lda dec_vel_dir            ; does HAS_LANDED
    cmp #$01                   ; equal TRUE?
    bne is2_prcsInpt_notLanded ; branch if FALSE

    lda joy1+1                 ; check key B
    and #$80
    beq is2_prcsInpt_notLanded ; branch if not pressed

    ; if key B is pressed do:
    lda #$01
    sta shipAdvHalfFlag        ; IS_JUMPING = TRUE
    stz dec_vel_dir            ; HAS_LANDED = FALSE
    stz shipAnim               ; IS_STANDING = FALSE

is2_prcsInpt_notLanded

    lda joy1+1
    and #$80
    beq is2_prcsInpt_setJumpFalse
    jmp is2_prcsInpt_notKeyB
is2_prcsInpt_setJumpFalse
    stz shipAdvHalfFlag        ; IS_JUMPING = FALSE



is2_prcsInpt_notKeyB
;==================
    lda joy1+1                 ; check down
    cmp #$04
    bne is2_prcsInpt_notDown

    lda ENM_15_hp
    cmp #$05
    beq is2_prcsInpt_df1locked
    inc ENM_15_hp
    lda #$06
    sta ENM_13_hp
    jmp is2_prcsInpt_ende
is2_prcsInpt_df1locked
    lda #$07
    sta ENM_13_hp              ; set angela ducking frame #2/2
    jmp is2_prcsInpt_ende


is2_prcsInpt_notDown
;==================
    stz ENM_15_hp              ; uncheck angela duck #1 frame lockout

    lda shipVel                ; if(HITTING_RIGHT) goto notLeft
    cmp #$01
    beq is2_prcsInpt_notLeft

    lda joy1+1                 ; check left
    and #$02
    beq is2_prcsInpt_notLeft

    lda ENM_13_hp              ; restrict the player from moving the
    cmp #$07                   ; character whilst ducking.
    beq is2_prcsInpt_ende

    jsr s2_angelaFrame_WALK_INC
    lda #%01110100
    sta SPRBUF1+SPRIORITY1
    dec SPRBUF1+SX1
    jmp is2_prcsInpt_ende

is2_prcsInpt_notLeft
;==================
    lda shipLastDir            ; if(HITTING_LEFT) goto noMovement
    cmp #$01
    beq is2_prcsInpt_noMovement

    lda joy1+1                 ; check right
    and #$01
    beq is2_prcsInpt_noMovement

    stz ENM_15_hp              ; uncheck angela duck #1 frame lockout

    lda ENM_13_hp              ; restrict the player from moving the
    cmp #$07                   ; character whilst ducking.
    beq is2_prcsInpt_ende

    jsr s2_angelaFrame_WALK_INC
    lda #%00110100
    sta SPRBUF1+SPRIORITY1
    inc SPRBUF1+SX1
    jmp is2_prcsInpt_ende

is2_prcsInpt_noMovement
;=====================
    stz ENM_13_hp              ; reset angela frame
    stz ENM_14_hp              ; reset angela walking frame delay
    stz ENM_15_hp              ; reset angela duck frame #1 lockout

is2_prcsInpt_ende
    rts



;---------------



s2int_DO_greenBox

    jsl sym_SFX_LASER_FIRE

    lda #$05
    sta anm_timer              ; green box timer

    lda #96
    sta SPRBUF1+SX
    lda #124
    sta SPRBUF1+SY
    lda #$88
    sta SPRBUF1+SFRAME
    lda #$30
    sta SPRBUF1+SPRIORITY

    lda #$2c
    sta what_fade              ; time to wait before closing door
    inc ENM_bulletHitENM       ; door anim
    inc ENM_bulletHitENM
    inc ENM_bulletHitENM
    lda ENM_bulletHitENM
    cmp #32
    bmi iDOGBcnt
    lda #31
    sta ENM_bulletHitENM

iDOGBcnt
    jsr iDoorAnim

    lda #$4a
    sta $2121
    jsr iDO_grBblck
    lda #$4b
    sta $2121
    jsr iDO_grBblck
    lda #$4c
    sta $2121
    jsr iDO_grBblck
    lda #$4d
    sta $2121
    jsr iDO_grBblck
    rts
iDO_grBblck
    stz $2122
    stz $2122
    rts



;---------------



s2int_UNDO_greenBox

    lda anm_timer
    cmp #$00
    beq iUNDO_grB
    dec anm_timer
    jmp iUNDO_ende


iUNDO_grB
    lda SPRBUF1+SX
    cmp #96
    bne iUNDO_ende
    lda #216
    sta SPRBUF1+SX
    lda #180
    sta SPRBUF1+SY
    lda #$84
    sta SPRBUF1+SFRAME
    stz SPRBUF1+SPRIORITY

    lda #$4a
    sta $2121
    lda #$24
    sta $2122
    lda #$13
    sta $2122
    lda #$4b
    sta $2121
    lda #$c0
    sta $2122
    lda #$02
    sta $2122
    lda #$4c
    sta $2121
    lda #$00
    sta $2122
    lda #$02
    sta $2122
    lda #$4d
    sta $2121
    lda #$80
    sta $2122
    lda #$01
    sta $2122

iUNDO_ende
    rts

;----------------


iDoorAnim

    lda ENM_bulletHitENM       ; door anim
    cmp #31
    beq iDAsym_31
    cmp #30
    beq iDAsym_30
    cmp #29
    beq iDAsym_29
    cmp #28
    beq iDAsym_28
    cmp #27
    beq iDAsym_27
    cmp #26
    beq iDAsym_26
    cmp #25
    beq iDAsym_25
    cmp #24
    beq iDAsym_24
    cmp #23
    beq iDAsym_23
    cmp #22
    beq iDAsym_22
    cmp #21
    beq iDAsym_21
    cmp #20
    beq iDAsym_20
    cmp #19
    beq iDAsym_19
    cmp #18
    beq iDAsym_18
    cmp #17
    beq iDAsym_17
    cmp #16
    beq iDAsym_16
    cmp #15
    beq iDAsym_15
    cmp #14
    beq iDAsym_14
    cmp #13
    beq iDAsym_13
    cmp #12
    beq iDAsym_12
    cmp #11
    beq iDAsym_11
    cmp #10
    beq iDAsym_10
    cmp #09
    beq iDAsym_09
    cmp #08
    beq iDAsym_08
    cmp #07
    beq iDAsym_07
    cmp #06
    beq iDAsym_06
    cmp #05
    beq iDAsym_05
    cmp #04
    beq iDAsym_04
    cmp #03
    beq iDAsym_03
    cmp #02
    beq iDAsym_02
    cmp #01
    beq iDAsym_01
    cmp #00
    beq iDAsym_00

iDAsym_31
    jmp iDA_31
iDAsym_30
    jmp iDA_30
iDAsym_29
    jmp iDA_29
iDAsym_28
    jmp iDA_28
iDAsym_27
    jmp iDA_27
iDAsym_26
    jmp iDA_26
iDAsym_25
    jmp iDA_25
iDAsym_24
    jmp iDA_24
iDAsym_23
    jmp iDA_23
iDAsym_22
    jmp iDA_22
iDAsym_21
    jmp iDA_21
iDAsym_20
    jmp iDA_20
iDAsym_19
    jmp iDA_19
iDAsym_18
    jmp iDA_18
iDAsym_17
    jmp iDA_17
iDAsym_16
    jmp iDA_16
iDAsym_15
    jmp iDA_15
iDAsym_14
    jmp iDA_14
iDAsym_13
    jmp iDA_13
iDAsym_12
    jmp iDA_12
iDAsym_11
    jmp iDA_11
iDAsym_10
    jmp iDA_10
iDAsym_09
    jmp iDA_09
iDAsym_08
    jmp iDA_08
iDAsym_07
    jmp iDA_07
iDAsym_06
    jmp iDA_06
iDAsym_05
    jmp iDA_05
iDAsym_04
    jmp iDA_04
iDAsym_03
    jmp iDA_03
iDAsym_02
    jmp iDA_02
iDAsym_01
    jmp iDA_01
iDAsym_00
    jmp iDA_00

iDA_31
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-62
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_30
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-60
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_29
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-58
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_28
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-56
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_27
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-54
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_26
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-52
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_25
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-50
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_24
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-48
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_23
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-46
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_22
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-44
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_21
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-42
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_20
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-40
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_19
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-38
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_18
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-36
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_17
    lda #112
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-34
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_16
    lda #144-32
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-32
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_15
    lda #144-30
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-30
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_14
    lda #144-28
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-28
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_13
    lda #144-26
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-26
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_12
    lda #144-24
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-24
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_11
    lda #144-22
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-22
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_10
    lda #144-20
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-20
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_09
    lda #144-18
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-18
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_08
    lda #144-16
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-16
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_07
    lda #144-14
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-14
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_06
    lda #144-12
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-12
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_05
    lda #144-10
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-10
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_04
    lda #144-8
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-8
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_03
    lda #144-6
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-6
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_02
    lda #144-4
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-4
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_01
    lda #144-2
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176-2
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
    jmp iDArts
iDA_00
    lda #144
    sta SPRBUF1+SY4
    sta SPRBUF1+SY5
    lda #176
    sta SPRBUF1+SY6
    sta SPRBUF1+SY7
iDArts
    lda SPRBUF1+SY6
    phx
    ldx #$0022
idarts38Xloop
    ina
    dex
    cpx #$0000
    bne idarts38Xloop
    plx
    sta ENM_18_patt            ; SECTION6_Y2
    rts


;==============================================================


S2_ANGELA_MEETS_SPIKE          ; Anglela loses a try when touching spikes

    stz ang_hp
    jsl WRITE_HP_REMAINING
    dec ang_lives

    jsl sym_SFX_INTERFERENCE

    stz $2121                  ; pal idx.#0 = pure white
    lda #$ff
    sta $2122
    sta $2122
    lda #%00011110
    sta $212c                  ; bg#1 disable

    lda #$a1   ; make angela RED
    sta $2121
    lda #%00011111
    phx
    ldx #$000f
makeAngREDloop
    sta $2122
    stz $2122
    dex
    cpx #$0000
    bne makeAngREDloop
    plx

    phx
    ldx #$0020
spikey_wait_loop
    wai
    dex
    cpx #$0000
    bne spikey_wait_loop
    plx
    lda ang_lives
    cmp #$00
    beq asdoij
    jsl WRITE_LIVES_REMAINING
asdoij
    phx
    ldx #$0020
spikey_wait_loop2
    wai
    dex
    cpx #$0000
    bne spikey_wait_loop2
    plx
    jsl bH13_FADE_OUT_MOSAIC_macro

    lda GOOD_ENDING
    cmp #$00
    beq GE_00
    cmp #$01
    beq GE_01
    cmp #$02
    beq GE_02
    cmp #$03
    beq GE_03
    cmp #$04
    beq GE_04

GE_00
    lda #54
    sta SPRBUF1+SX
    lda #181
    sta SPRBUF1+SY
    lda #%00110100             ; no flips, max prior, spr.pal #2
    sta SPRBUF1+SPRIORITY
    stz SPRBUF1+SFRAME
    jmp spikey_meets_ende
GE_01
    lda #199
    sta SPRBUF1+SX
    lda #181
    sta SPRBUF1+SY
    lda #%01110100             ; h-flip, max prior. spr.pal #2
    sta SPRBUF1+SPRIORITY
    stz SPRBUF1+SFRAME
    jmp spikey_meets_ende
GE_02
    lda #22
    sta SPRBUF1+SX
    lda #181
    sta SPRBUF1+SY
    lda #%00110100             ; no flips, max prior, spr.pal #2
    sta SPRBUF1+SPRIORITY
    stz SPRBUF1+SFRAME
    jmp spikey_meets_ende
GE_03
    lda #199
    sta SPRBUF1+SX
    lda #149
    sta SPRBUF1+SY
    lda #%01110100             ; h-flip, max prior. spr.pal #2
    sta SPRBUF1+SPRIORITY
    stz SPRBUF1+SFRAME
    jmp spikey_meets_ende
GE_04
    lda #22
    sta SPRBUF1+SX
    lda #149
    sta SPRBUF1+SY
    lda #%00110100             ; no flips, max prior, spr.pal #2
    sta SPRBUF1+SPRIORITY
    stz SPRBUF1+SFRAME
    jmp spikey_meets_ende


spikey_meets_ende
    jsr S2_RECALC_ANG_X
    jsr S2_RECALC_ANG_Y
    jsr S2_RECALC_ANG_X2
    jsr S2_RECALC_ANG_Y2

    lda #$03
    sta ang_hp
    jsl WRITE_HP_REMAINING

    LOAD_PAL S2_LEVEL_PAL      ; reload the palette
    jsl INITIALIZE_HUD_PAL
    lda #%00011111
    sta $212c                  ; re-enable bg#1
    wai
    rts
