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

MultiplayableBattleMode.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 
00023 #include "WinMainSelector.h"
00024 
00025 #ifdef COMPILE_DEMOMULTIPLAYBATTLE
00026 
00027 #include "..\AEngine\AEngine.h"
00028 #include "globals.h"
00029 
00030 #include "BattleEntry.h"
00031 #include "BattleUnitData.h"
00032 
00033 using namespace Globals;
00034 
00035 BattleEntry *pBattle;
00036 FLOAT           screenSize;
00037 bool            notInGame;
00038 int                     lastHeardTick;
00039 
00040 string& stripComments(string& str)
00041 {
00042   size_t start;
00043   size_t end;
00044   while( (start = str.find("//")) != string::npos )
00045   {
00046     end = str.find("\n", start)+1;
00047     str.erase(start, end-start);
00048   }
00049   return str;
00050 }
00051 
00052 void initData()
00053 {
00054   ifstream fileIn("data.txt");
00055   ostringstream fileDump;
00056 
00057   fileDump << fileIn.rdbuf();
00058   string file = stripComments(fileDump.str());
00059 
00060   size_t start = file.find(BattleUnitData::TOK_BEGIN_DATA)+BattleUnitData::TOK_BEGIN_DATA.size();
00061   size_t len = file.find(BattleUnitData::TOK_END_DATA) - start;
00062   BattleUnitData::getObj().initData(istringstream(file.substr(start, len)));
00063 }
00064 
00065 //------------------------------------------------------------------------------------
00066 void init()
00067 {       
00068   initData();
00069         sys_showConsole(false); 
00070         Globals::artWork = new ArtWork();
00071         pBattle = NULL; 
00072         screenSize = 1.0f;
00073         sys_setSkipRate(0);
00074         notInGame = true;
00075         //sys_enableConsoleLog("console.log");
00076 }
00077 //------------------------------------------------------------------------------------
00078 void update()
00079 {       
00080         // host or join a game
00081         if( notInGame && input_isKeyPressed(KeyCodes::key_H)) { net_runAsServer("155.101.73.116","UsitS",8);    notInGame=false; }
00082         if( notInGame && input_isKeyPressed(KeyCodes::key_J)) { net_connectToServer("155.101.73.116","UsitC");  notInGame=false; }
00083         if( notInGame && input_isKeyPressed(KeyCodes::key_F)) { net_runAsFakeServer("UsitF",8);                                 notInGame=false; }
00084 
00085         if( net_amIServer() && input_isKeyPressed(KeyCodes::key_SPACE) && pBattle==NULL )
00086         {
00087                 net_send("BeginBattle");                
00088         }
00089 
00090         
00091         string msg;
00092         while( (msg=net_recv()) != "" )
00093         {
00094                 sys_console() << "[" << msg << "]" << endl;                     // for debugging
00095                 
00096                 if( msg=="BeginBattle" )
00097                 {                       
00098                         sys_console() << "Starting Battle" << endl;
00099                         pBattle = new BattleEntry( Colors::blue, Colors::black, Vec2D(0.0f,0.0f) );
00100 
00101                         pBattle->teamAGroups.push_back( Int2Tuple( UnitTypes::LIGHT_TANK, 30 ) );
00102                         pBattle->teamAGroups.push_back( Int2Tuple( UnitTypes::HEAVY_TANK, 16 ) );
00103                         pBattle->teamAGroups.push_back( Int2Tuple( UnitTypes::LAUNCHER, 24 ) );
00104                         pBattle->teamAGroups.push_back( Int2Tuple( UnitTypes::ARTILLERY, 18 ) );
00105 
00106                         pBattle->teamBGroups.push_back( Int2Tuple( UnitTypes::LIGHT_TANK, 30 ) );
00107                         pBattle->teamBGroups.push_back( Int2Tuple( UnitTypes::LIGHT_TANK, 30 ) );
00108                         pBattle->teamBGroups.push_back( Int2Tuple( UnitTypes::LAUNCHER, 24 ) );
00109 
00110                         screenSize = 0.01f;
00111 
00112                         // reset game tick, random seed
00113                         gameTick = 0;
00114                         sys_setRandSeed(0);
00115                         lastHeardTick = 0;
00116                 }
00117                 else
00118                         if(pBattle) pBattle->enQueueInput(msg);
00119         }
00120 
00121         if(pBattle)
00122         {
00123                 pBattle->input();
00124                 pBattle->update();
00125                 if(pBattle->isBattleFinished())
00126                 {
00127                         pBattle = NULL;
00128                         sys_console() << "Battle Terminates at GameTick = " << gameTick << endl;
00129                 }
00130         }
00131 
00132         if(screenSize<1.0f) screenSize = (screenSize + 0.004f) * 1.025f;
00133         if(screenSize>1.0f) screenSize = 1.0f;
00134         camera_setScreenSize(screenSize);
00135 }
00136 //------------------------------------------------------------------------------------
00137 void render()
00138 {
00139         if(pBattle)
00140         {
00141                 pBattle->render();
00142         }
00143         else
00144         {
00145                 overlay_text() << "DemoBattleMode : Main" << endl;
00146                 if(notInGame)
00147                 {
00148                         overlay_text() << "Press [H] to host a game" << endl;
00149                         overlay_text() << "Press [J] to join a game" << endl;
00150                         overlay_text() << "Press [F] to fake-host a game" << endl;
00151                 }
00152                 else if( net_amIServer() )
00153                         overlay_text() << "Press [SPACE] to begin battle" << endl;
00154 
00155                 overlay_textOut( Vec2D(0.0f,0.0f) );
00156         }
00157 }
00158 //------------------------------------------------------------------------------------
00159 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR, INT )
00160 {
00161         sys_init(hInst,800,600,32,true);
00162         sys_setInitFunc(init);
00163         sys_setUpdateFunc(update);
00164         sys_setRenderFunc(render);
00165         sys_go();
00166         return 0;
00167 }
00168 //------------------------------------------------------------------------------------
00169 #endif

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