Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

MainMultiplay.cpp

Go to the documentation of this file.
00001 /*
00002 CS Senior Project 2003
00003 Team : Leftfield
00004 Project : ModernWarfare
00005 Members :
00006 - Russ Christensen              <rchriste@cs.utah.edu>
00007 - Todd Smith                    <tcsmith@cs.utah.edu>
00008 - Usit Duongsaa                 <duongsaa@cs.utah.edu>
00009 Copyright 2003 Russ Christensen, Usit Duongsaa, and Todd Smith. All rights reserved.
00010 
00011 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00012 
00013 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
00014 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
00015 THIS SOFTWARE IS PROVIDED BY RUSS CHRISTENSEN, USIT DUONGSAA, AND TODD SMITH ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RUSS, USIT, TODD OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00016 */
00017 
00018 
00023 #include "Network.h"
00024 #include "Sound.h"
00025 #include "Typer.h"
00026 #include "..\AEngine\AEngine.h"
00027 #include "Overhead.h"
00028 #include "MainFuncs.h"
00029 #include "Globals.h"
00030 
00031 using namespace Overhead;
00032 using namespace NetworkMessages;
00033 
00034 //------------------------------------------------------------------------------------
00035 Typer *pTyper;
00036 Typer typerName, typerIP, typerChatMsg;
00037 
00038 Box butMultiName           ( 0.117f, 0.168f, 0.466f, 0.229f );          // multiplayer menu
00039 Box butMultiHost           ( 0.120f, 0.258f, 0.324f, 0.305f );
00040 Box butMultiJoin           ( 0.119f, 0.349f, 0.325f, 0.402f );
00041 Box butMultiIP             ( 0.375f, 0.345f, 0.723f, 0.405f );
00042 Box butMultiChatMsg        ( 0.394f, 0.845f, 0.825f, 0.905f );
00043 Box butMultiChat           ( 0.852f, 0.845f, 0.953f, 0.900f );
00044 Box butMultiLaunch         ( 0.332f, 0.921f, 0.588f, 0.981f );
00045 Box butMultiAbort          ( 0.667f, 0.921f, 0.794f, 0.981f );
00046 
00047 int step;
00048 
00049 namespace Steps
00050 {
00051         const int enterName                     =       0;
00052         const int hostOrJoin            =       1;
00053         const int readyToLaunch         =       2;
00054 }
00055 
00056 list<string> chatLog;
00057 
00058 //------------------------------------------------------------------------------------
00059 void switchToMultiplayMenu()
00060 {
00061         mode = MainModes::multiplayMenu;
00062 
00063         typerName = Typer("Your Name");
00064         typerIP   = Typer("Host IP");
00065         typerChatMsg = Typer("Hello!");
00066         pTyper = &typerName;    
00067 
00068         step = Steps::enterName;
00069 }
00070 //------------------------------------------------------------------------------------
00071 void processIncomingMessages()
00072 {
00073         if( step != Steps::readyToLaunch ) return;              // no network connection yet
00074 
00075         string cmd;
00076         while( (cmd=net_recv()).length() > 0 )
00077         {
00078                 istringstream strm(cmd.c_str());
00079                 
00080                 string action;
00081                 strm >> action;
00082                 if (action == COMMAND_START_GAME)
00083                 {                       
00084                         string tmp;
00085                         strm >> tmp; //commitTime string
00086                         strm >> tmp; //commitTime int
00087                         strm >> tmp; //RANDOM_SEED_VALUE
00088                         int seed;
00089                         strm >> seed;
00090                         sys_setRandSeed(seed);
00091 
00092                         overhead_init();                                // switch to overhead
00093                         mode = MainModes::overhead;
00094                 }
00095                 else if (action == COMMAND_CHAT)                                // a chat msg
00096                 {
00097                         string msg;
00098                         getline(strm,msg);
00099                         chatLog.push_back( msg );
00100                 }
00101                 else
00102                         NetworkSubsystem::enQueueMsg(cmd);
00103         }
00104 }
00105 //------------------------------------------------------------------------------------
00106 void updateMultiplayMenu()
00107 {
00108         Vec2D mouse( input_getMouseX(), input_getMouseY() );
00109         if( input_isMouseLBClicked() )
00110         {
00111                 if( butMultiAbort.isContain(mouse) )                    // abort
00112                 {
00113                         //net_disconnect();
00114                         switchToMainMenu();
00115                         step = Steps::enterName;
00116                         return;
00117                 }
00118 
00119                 // select typer
00120                 if( butMultiName.isContain(mouse) )             pTyper = &typerName;
00121                 if( butMultiIP.isContain(mouse) )               pTyper = &typerIP;
00122                 if( butMultiChatMsg.isContain(mouse) )  pTyper = &typerChatMsg;
00123 
00124                 // host or join
00125                 if(step==Steps::enterName && typerName.getString() != "Your Name") step = Steps::hostOrJoin;
00126                 if( step==Steps::hostOrJoin )
00127                 {
00128                         if( butMultiHost.isContain(mouse) )                             // Host Game
00129                         {
00130                                 step = Steps::readyToLaunch;                            
00131                                 net_runAsServer( net_getSelfIP(), typerName.getString(), 8 );
00132                                 chatLog.push_back( "Hosting Process Complete.  IP = " + net_getSelfIP() );
00133                         }
00134 
00135                         if( butMultiJoin.isContain(mouse) )                             // Join Game
00136                         {                               
00137                                 if( !net_connectToServer( typerIP.getString(), typerName.getString() ))
00138                                 {
00139                                         chatLog.push_back( "Cannot Join with That Host" );
00140                                         return;
00141                                 }
00142                                 chatLog.push_back( "Joining Process Complete." );
00143                                 step = Steps::readyToLaunch;
00144                         }
00145                 }
00146 
00147                 // launch
00148                 if( step==Steps::readyToLaunch && butMultiLaunch.isContain(mouse) )
00149                         if( net_amIServer() )
00150                         {                               
00151                                 currGameTick = 0;
00152                                 stringstream sstr;
00153                                 
00154                                 if( net_amIServer() ) net_finalizeGame();
00155 
00156                                 sstr << COMMAND_START_GAME << " " << COMMIT_TIME << " " << currGameTick << " " << RANDOM_SEED_VALUE << " " << sys_randInt();
00157                                 net_send(sstr.str());
00158                         }
00159 
00160                 // chat by clicking Chat button
00161                 if( step==Steps::readyToLaunch && butMultiChat.isContain(mouse) )
00162                 {
00163                         ostringstream strm;
00164                         strm << COMMAND_CHAT << " [" << typerName.getString() << "] " << typerChatMsg.getString();
00165                         net_send(strm.str());
00166                         typerChatMsg = Typer();
00167                 }
00168         }
00169 
00170         // type to InputString Typer Box
00171         if( step==Steps::enterName || step==Steps::hostOrJoin )                 // type name or ip
00172                 if( pTyper==&typerName || pTyper==&typerIP )
00173                         pTyper->input();
00174         if( step==Steps::readyToLaunch && pTyper==&typerChatMsg )               // type chat
00175                 pTyper->input();
00176 
00177         // chat by pressing enter while chat box is active
00178         if( step==Steps::readyToLaunch && input_isKeyPressed(KeyCodes::key_RETURN) && pTyper==&typerChatMsg )
00179         {
00180                 ostringstream strm;
00181                 strm << COMMAND_CHAT << " [" << typerName.getString() << "] " << typerChatMsg.getString();
00182                 net_send(strm.str());
00183                 typerChatMsg = Typer();
00184         }
00185 
00186         // check for more players
00187         if( step==Steps::readyToLaunch ) net_acceptMorePlayers();
00188 
00189         processIncomingMessages();
00190 }
00191 //------------------------------------------------------------------------------------
00192 void renderMultiplayMenu()
00193 {
00194         Vec2D mouse( input_getMouseX(), input_getMouseY() );                                            // Mouse Cursor
00195         overlay_image( Globals::artWork->mouse, mouse+Vec2D(0.025f,0.025f), Vec2D(0.05f,0.05f), BlendModes::KEY );
00196 
00197         // main menu screen
00198         overlay_image( artWork->main_multiplayTex, Vec2D(0.5f,0.5f), Vec2D(1.0f,1.0f) );
00199 
00200         // highlight the button that the mouse is currently over
00201         if( butMultiName.isContain(mouse) )             drawBox( butMultiName, Colors::gray );
00202         if( butMultiHost.isContain(mouse) )             drawBox( butMultiHost, Colors::yellow );
00203         if( butMultiJoin.isContain(mouse) )             drawBox( butMultiJoin, Colors::yellow );
00204         if( butMultiIP.isContain(mouse) )               drawBox( butMultiIP, Colors::gray );
00205         if( butMultiChatMsg.isContain(mouse) )  drawBox( butMultiChatMsg, Colors::gray );
00206         if( butMultiChat.isContain(mouse) )             drawBox( butMultiChat, Colors::yellow );
00207         if( butMultiLaunch.isContain(mouse) )   drawBox( butMultiLaunch, Colors::yellow );
00208         if( butMultiAbort.isContain(mouse) )    drawBox( butMultiAbort, Colors::yellow );
00209 
00210         // typer strings
00211         overlay_text() << typerName.getString();
00212         overlay_textOut( Vec2D( butMultiName.left+0.008f, butMultiName.top+0.017f), 1.0f, Colors::white );
00213         overlay_text() << typerIP.getString();
00214         overlay_textOut( Vec2D( butMultiIP.left+0.008f, butMultiIP.top+0.017f), 1.0f, Colors::white );
00215         overlay_text() << typerChatMsg.getString();
00216         overlay_textOut( Vec2D( butMultiChatMsg.left+0.008f, butMultiChatMsg.top+0.017f), 1.0f, Colors::white );
00217 
00218         // selected typer
00219         if( pTyper==&typerName )                drawBox( butMultiName, Colors::white );
00220         if( pTyper==&typerIP )                  drawBox( butMultiIP, Colors::white );
00221         if( pTyper==&typerChatMsg )             drawBox( butMultiChatMsg, Colors::white );
00222 
00223         // player list
00224         if( step==Steps::readyToLaunch )
00225         {
00226                 vector<string> &playersList = net_getAllPlayers();
00227                 for( UINT i=0; i<playersList.size(); i++ )
00228                         overlay_text() << playersList[i] << endl;
00229                 overlay_textOut( Vec2D(0.065f, 0.522f) );
00230         }
00231 
00232         // chat log
00233         while( chatLog.size()>9 ) chatLog.pop_front();
00234         for( list<string>::iterator iter=chatLog.begin(); iter!=chatLog.end(); iter++ ) 
00235                 overlay_text() << (*iter) << endl;
00236         overlay_textOut( Vec2D( 0.440f, 0.527f ), 1.0f, Colors::white );        
00237 }
00238 //------------------------------------------------------------------------------------

Generated on Wed Apr 23 05:50:15 2003 for Modern Warfare by doxygen1.3-rc2