;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; File Name    : invset.asm
; Purpose      : Space Invaders Application Set State Manager
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;DO NOT MODIFY THE FOLLOWING SUBROUTINE DEFINITION;;;;;;;;;;;;;;;;;

                IF @DEF('SUBROUTINE')
                    UNDEF SUBROUTINE
                ENDIF
                DEFINE  SUBROUTINE      "'invset'"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

                GLOBAL  invSetStateManager

invSetStateManager:


                ;**************************************************************
                ; The "You Rock" state is a generic display, designed to inform 
                ;   the user that the crown was pulled to a "Set" position when 
                ;   the "Set" position is invalid (for example, pulling the 
                ;   crown out in a mode lacking a setting state). The state is 
                ;   generic because it operates in the same fashion for all modes, 
                ;   hence the need for only one state. The only input that can get 
                ;   the user out of this state is to push the crown to the "Home" 
                ;   position. This returns the user to the default state.
                ; 
                ; If the crown is pulled out during a popup, the same condition 
                ;   occurs, i.e., the "Set" position is invalid, so the "You Rock" 
                ;   message is displayed. In this case, when the user pushes the 
                ;   crown to the "Home" position, we do not return to the default 
                ;   state of the current mode, but to the mode and state which 
                ;   was "popped over".
                ;**************************************************************

                ;--------------------------------------------------------------
                ; Check what event was passed to the "You Rock" manager.
                ;--------------------------------------------------------------
                ld      A, [CORECurrentEvent]

                ; Check if state entry event.
                cp      A, #COREEVENT_STATEENTRY
                jr      NZ, xxxutlYouRockStMgrCheckEvent2

                ;**************************************************************
                ;
                ;                         STATE ENTRY
                ;
                ;**************************************************************

                ; Suspend popups, since the crown is out and we do NOT want anything 
                ;   "popping over" this warning message.
                CORE_SUSPEND_POPUPS

                ; Prohibit the switch and ring events to be passed to this state.
                CORE_MASK_KEYS (bCORESwitch1 | bCORESwitch2 | bCORESwitch3 | bCORECWSwitch | bCORECCWSwitch | bCOREELSwitch)

                ; Display "YOU ROCK" warning message, and exit.
                LCD_DISP_SMALL_DM_MSG_YOU_ROCK

                ret


xxxutlYouRockStMgrCheckEvent2:

                ; Check if crown home event.
                cp      A, #COREEVENT_CROWN_HOME
                jr      NZ, xxxutlYouRockStMgrExit

                ;**************************************************************
                ;
                ;                         CROWN HOME
                ;
                ;**************************************************************

                ;==============================================================
                ; The user has acknowledged the warning and we should now 
                ;   return control back to the default state if we are not 
                ;   in a popup, or to the "popped over" mode and state if 
                ;   we are.
                ;==============================================================
            
                ; Check if we are in a popup.
                ld      A, [CORESystemFlag4]
                bit     A, #bCOREPopupMode
                jr      Z, xxxutlYouRockGoToDefaultState

                ; We ARE in a popup.
                ; Ask the core to return to the "popped over" state.
                CORE_REQ_POPDOWN

                ret

xxxutlYouRockGoToDefaultState:
                
                ; We are NOT in a popup.
                ; Ask core to return to default state.


				; check which state to return...
				; if ships are still available, return to default state...
				; if all ships are lost, return to the game over state...
                ld      B, #COREDEFAULTSTATE
                ld      HL, [CORECurrentASDAddress]
                add     HL, #INVSHIPAVAILABLEOFFSET
                cp      [HL], #0
                jr      NZ, invcontinuegame

                ; go to game over state
                ld      B, #INVGAMEOVERSTATE

invcontinuegame:

                CORE_REQ_STATE_CHANGE
                ret



























                ld      B, #COREDEFAULTSTATE
                CORE_REQ_STATE_CHANGE

xxxutlYouRockStMgrExit:

                ret
