<?xml version="1.0" encoding="utf-8" ?>
<sector name="ClientUpdate">
    
    <tGameStateOut>
        <!-- I am the other player. -->
        <rPlayer2Pos> tOtherPlayer.vTranslate </rPlayer2Pos>
        <rPlayer2Rot> tOtherPlayer.xRotate </rPlayer2Rot>
        <rPlayer2Mom> tOtherPlayer.vMomentum </rPlayer2Mom>
        <rPlayer2FV> vForward </rPlayer2FV>
        <rIWantToShoot> tBall.iP2WantsToShoot </rIWantToShoot>
    </tGameStateOut>
    
    <tUpdateGameState prototype="Entity">
        <!-- Flag for sending data. -->
        <iSendNetworkData>1</iSendNetworkData>
        
        <!-- Datums for ActionNetworkSend. -->
        <fTimePrevRcvd>0.0</fTimePrevRcvd>
        <fLocalTimePrevRcvd>0.0</fLocalTimePrevRcvd>
        <iSeqNum>1</iSeqNum>
        <sMsg>0</sMsg>
        <iPacketSizeOut>0</iPacketSizeOut>
        <Update>
            <iCond> 0 </iCond>
            <expr> iCond = ( iSendNetworkData == 1 ) </expr>
            <if>
                <rCondition> iCond </rCondition>
                <then>
                
                    <tNetReceive prototype="ActionNetworkReceive">
                        <rSocket>iSocket</rSocket>
                        <!-- sMsgData, iMsgDataSize, and iMsgDataType are hidden and updated if DATA; are sent as the header-->
                        <iUpdateType>updt</iUpdateType>
                        <iOverType>over</iOverType>
                        <iOldPlayer1Score>0</iOldPlayer1Score>
                        <iOldPlayer2Score>0</iOldPlayer2Score>
                        <iCond>0</iCond>
                        
                        <expr> iCond = ( iMsgDataType == iUpdateType ) </expr>
                        <if>
                            <rCondition>iCond</rCondition>
                            <then>
                                
                                <iTest>0</iTest>
                                <tTestAction prototype = "ActionUnpackNetMsg">
                                    <rMsgData> sMsgData </rMsgData>
                                    <rSize> iMsgDataSize </rSize>
                                    <!-- The order of this must match tGameStateOut from 
                                        ServerUpdate.xml. -->
                                    <rGameTime> tTimer.fTime </rGameTime>
                                    <rPlayer1Pos> tPlayer.vTranslate </rPlayer1Pos>
                                    <rPlayer1Rot> tPlayer.xRotate </rPlayer1Rot>
                                    <rPlayer1Mom> tPlayer.vMomentum </rPlayer1Mom>
                                    <rPlayer1Score> tScoreboard.iPlayer1Score </rPlayer1Score>
                                    <rPlayer2Score> tScoreboard.iPlayer2Score </rPlayer2Score>
                                    <rBallPos> tBall.vTranslate </rBallPos>
                                    <rBallOwner> tBall.iOwner </rBallOwner>
                                    <rNorthGoalPos> tNorthGoal.vTranslate </rNorthGoalPos>
                                    <rSouthGoalPos> tSouthGoal.vTranslate </rSouthGoalPos>
                                </tTestAction>
                                
                                <iCond1>0</iCond1>    
                                <iCond2>0</iCond2> 
                                <iCond3> 0 </iCond3>   
                                <expr> iCond1 = ( iOldPlayer1Score != tScoreboard . iPlayer1Score )</expr>
                                <expr> iCond2 = ( iOldPlayer2Score != tScoreboard . iPlayer2Score )</expr>
                                <expr> iCond3 = ( iCond1 OR iCond2 )</expr>
                                    <if>
                                        <rCondition>iCond3</rCondition>
                                        <then>
                                            <tActionEvent prototype="ActionEvent">
                                                <iSubtype>GOAL</iSubtype>
                                                <iDelay>0.0</iDelay>
                                            </tActionEvent>
                                        
                                        </then>
                                    
                                    </if>
                                <expr>iOldPlayer1Score = tScoreboard . iPlayer1Score</expr>
                                <expr>iOldPlayer2Score = tScoreboard . iPlayer2Score</expr>
                            </then>
                          <else>
                            <expr>iCond = ( iMsgDataType == iOverType )</expr>
                            <if>
                              <rCondition> iCond </rCondition>
                              <then>
                                <expr> iGameRunning = 0 </expr>
                                <expr> iGameOver = 1 </expr>

                                <tActionUnloadKeyBoard prototype="ActionUnloadSector">
                                  <sSectorName>GeneralInput</sSectorName>
                                </tActionUnloadKeyBoard>

                                <tThrowPauseGame prototype="ActionEvent">
                                  <iSubtype>paus</iSubtype>
                                  <fDelay>0.0</fDelay>
                                </tThrowPauseGame>


                                <tThrowEndGame prototype="ActionEvent">
                                  <iSubtype>over</iSubtype>
                                  <fDelay>4.0</fDelay>
                                </tThrowEndGame>

                                <tStopVehicleSound prototype="ActionStopSound">
                                  <sFilename> vehicle.wav </sFilename>
                                </tStopVehicleSound>

                                <tPlayWinSound prototype="ActionPlaySound">
                                  <sFilename> win.wav </sFilename>
                                </tPlayWinSound>
                                
                                <tStopDrumLoop prototype="ActionStopSound">
                                  <sFilename> drums.wav </sFilename>
                                </tStopDrumLoop>

                                <tActionLoad prototype ="ActionLoad">
                                  <sFilename>GameOver.xml</sFilename>
                                </tActionLoad>
                              </then>
                            </if>
                          </else>
                        </if>
                    </tNetReceive>

                    <tPackMsg prototype="ActionPackNetMsg">
                        <iPacketType>updt</iPacketType>
                        <rPacketTable>tGameStateOut</rPacketTable>
                        <rPacketMsg>sMsg</rPacketMsg>
                        <rPacketSizeOut>iPacketSizeOut</rPacketSizeOut>
                    </tPackMsg>
                  
                    <tGameStateSend prototype="ActionNetworkSend">
                        <rTimePrevRcvd>fTimePrevRcvd</rTimePrevRcvd>
                        <rLocalTimePrevRcvd>fLocalTimePrevRcvd</rLocalTimePrevRcvd>
                        <rSeqNum>iSeqNum</rSeqNum>
                        <rMsgData>sMsg</rMsgData>
                        <rMsgLengths>iPacketSizeOut</rMsgLengths>
                        <rSocket>iSocket</rSocket>
                        <rPeerPortNum>iPeerPort</rPeerPortNum>
                        <rPeerIPAddr>iChosenIPAddress</rPeerIPAddr>
                    </tGameStateSend>
                    <expr>tBall . iP2WantsToShoot = 0</expr>
            
                
                </then>
            
            </if>
        </Update>
                
        <!-- REACTION SECTION -->
        <tReactGameOver prototype="ReactionAttributed" iSynchronous="0">
            <iMatch>over</iMatch>
            <iMask>-1</iMask>
            
            <tActionList prototype="ActionList">
                <!-- Game ended, change flag to halt data stuff. -->
                <expr>iSendNetworkData = 0</expr>
            
                <tActionGameOver prototype="ActionUnloadSector">
                    <sSectorName>ClientUpdate</sSectorName>
                </tActionGameOver>
                <tUnload prototype="ActionUnloadSector">
                    <sSectorName>ServerSysCode</sSectorName>
                </tUnload>
                
                <!-- Send DISCONNECT message to server. -->
                <sMsg>"DISCONNECT;"</sMsg>
                <iMsgLength>-1</iMsgLength>
                <iSeqNum>0</iSeqNum>
                <tSendDisconnect prototype="ActionNetworkSend">
                    <rTimePrevRcvd>fTimePrevRcvd</rTimePrevRcvd>
                    <rLocalTimePrevRcvd>fLocalTimePrevRcvd</rLocalTimePrevRcvd>
                    <rSeqNum>iSeqNum</rSeqNum>
                    <rMsgData>sMsg</rMsgData>
                    <rMsgLengths>iMsgLength</rMsgLengths>
                    <rSocket>iSocket</rSocket>
                    <rPeerPortNum>iPeerPort</rPeerPortNum>
                    <rPeerIPAddr>iChosenIPAddress</rPeerIPAddr>
                </tSendDisconnect>
            </tActionList>
        </tReactGameOver>
        <!-- END REACTION SECTION -->
    </tUpdateGameState>
</sector>
