00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 );
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;
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;
00086 strm >> tmp;
00087 strm >> tmp;
00088 int seed;
00089 strm >> seed;
00090 sys_setRandSeed(seed);
00091
00092 overhead_init();
00093 mode = MainModes::overhead;
00094 }
00095 else if (action == COMMAND_CHAT)
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) )
00112 {
00113
00114 switchToMainMenu();
00115 step = Steps::enterName;
00116 return;
00117 }
00118
00119
00120 if( butMultiName.isContain(mouse) ) pTyper = &typerName;
00121 if( butMultiIP.isContain(mouse) ) pTyper = &typerIP;
00122 if( butMultiChatMsg.isContain(mouse) ) pTyper = &typerChatMsg;
00123
00124
00125 if(step==Steps::enterName && typerName.getString() != "Your Name") step = Steps::hostOrJoin;
00126 if( step==Steps::hostOrJoin )
00127 {
00128 if( butMultiHost.isContain(mouse) )
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) )
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
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
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
00171 if( step==Steps::enterName || step==Steps::hostOrJoin )
00172 if( pTyper==&typerName || pTyper==&typerIP )
00173 pTyper->input();
00174 if( step==Steps::readyToLaunch && pTyper==&typerChatMsg )
00175 pTyper->input();
00176
00177
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
00187 if( step==Steps::readyToLaunch ) net_acceptMorePlayers();
00188
00189 processIncomingMessages();
00190 }
00191
00192 void renderMultiplayMenu()
00193 {
00194 Vec2D mouse( input_getMouseX(), input_getMouseY() );
00195 overlay_image( Globals::artWork->mouse, mouse+Vec2D(0.025f,0.025f), Vec2D(0.05f,0.05f), BlendModes::KEY );
00196
00197
00198 overlay_image( artWork->main_multiplayTex, Vec2D(0.5f,0.5f), Vec2D(1.0f,1.0f) );
00199
00200
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
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
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
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
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