Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

newgame.cpp

Go to the documentation of this file.
00001 /*
00002      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003      Copyright (C) 1994-1999  Martin Bickel  and  Marc Schellenberger
00004  
00005      This program is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published by
00007      the Free Software Foundation; either version 2 of the License, or
00008      (at your option) any later version.
00009  
00010      This program is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013      GNU General Public License for more details.
00014  
00015      You should have received a copy of the GNU General Public License
00016      along with this program; see the file COPYING. If not, write to the 
00017      Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00018      Boston, MA  02111-1307  USA
00019 */
00020 
00021 #include <sstream>
00022 #include <pgimage.h>
00023 #include <pglistbox.h>
00024 #include <pglistboxitem.h>
00025 
00026 #include "newgame.h"
00027 #include "editmapparam.h"
00028 #include "alliancesetup.h"
00029 #include "playersetup.h"
00030 #include "fileselector.h"
00031 #include "emailsetup.h"
00032 #include "../loaders.h"
00033 #include "../gamemap.h"
00034 #include "../paradialog.h"
00035 #include "../turncontrol.h"
00036 #include "../viewcalculation.h"
00037 #include "../networkinterface.h"
00038 #include "../spfst.h"
00039 #include "../windowing.h"
00040 #include "../controls.h"
00041 #include "../sg.h"
00042 #include "../gamedlg.h"
00043 #include "../network/simple_file_transfer.h"
00044 #include "../network/pbem-server.h"
00045 #include "../textfile_evaluation.h"
00046 #include "../textfileparser.h"
00047 #include "../widgets/textrenderer.h"
00048 
00049 class GameParameterEditorWidget;
00050 
00051 const int pageCount = 11;
00052 
00053 class Campaign {
00054    public:
00055       ASCString name;
00056       ASCString description;
00057       ASCString file;
00058       
00059       void runTextIO ( PropertyContainer& pc ) {
00060          pc.addString ( "name", name);
00061          pc.addString ( "description", description );
00062          pc.addString ( "file", file );
00063       }
00064 };
00065 
00066 class StartMultiplayerGame: public ConfigurableWindow {
00067    private:
00068       bool startButton;
00069       bool success;
00070       GameMap* newMap;
00071       
00072       enum Pages { ModeSelection = 1, FilenameSelection, PlayerSetup, EmailSetup, AllianceSetup, MapParameterEditor, MultiPlayerOptions, PasswordSearch, PBEMServerSetup, CampaignChoser }; 
00073       Pages page;
00074      
00075       enum Mode { NewCampaign, ContinueCampaign, Skirmish, Hotseat, PBEM, PBP, PBEM_Server };
00076       int mode;
00077       
00078       static const char* buttonLabels[];
00079       
00080       ASCString filename;
00081       ASCString PBEMfilename;
00082       bool replay;
00083       bool supervisorEnabled;
00084       Password supervisorPassword;
00085       
00086       GameParameterEditorWidget* mapParameterEditor;
00087       PG_Widget* mapParameterEditorParent;
00088       
00089       AllianceSetupWidget* allianceSetup;
00090       PG_Widget* allianceSetupParent;
00091       
00092       PlayerSetupWidget* playerSetup;
00093       PG_Widget* playerSetupParent;
00094       
00095       EmailSetupWidget* emailSetup;
00096       PG_Widget* emailSetupParent;
00097    
00098       bool nextPage(PG_Button* button = NULL);
00099       void showPage();
00100       bool checkPlayerStat();
00101       
00102       ASCString getDefaultPBEM_Filename();
00103       void setupNetwork();
00104       
00105       PBEMServer* pbemserver;
00106       
00107       void loadPBEMServerDefaults();
00108 
00109       typedef deallocating_vector<Campaign*> Campaigns;
00110       Campaigns campaigns;
00111       void loadCampaigns();
00112       Campaign* getSelectedCampaign();
00113       bool campaignSelected();
00114       
00115       void fileNameSelected( const ASCString& filename )
00116       {
00117          this->filename = filename;
00118          if ( startButton ) 
00119             start();
00120          else
00121             nextPage();
00122       };   
00123 
00124       void fileNameMarked( const ASCString& filename )
00125       {
00126          this->filename = filename;
00127       };   
00128       
00129       bool quickstart()
00130       {
00131          if ( Apply() )
00132             start();
00133          return true;
00134       }
00135 
00136 
00137       void showSupervisorWidgets()
00138       {
00139          if ( page == MultiPlayerOptions )
00140             if ( supervisorEnabled )
00141                show( "SuperVisor" );
00142             else
00143                hide( "SuperVisor" );
00144 
00145       }
00146       
00147       bool togglesupervisor( bool on )
00148       {
00149          supervisorEnabled = on;
00150          showSupervisorWidgets();
00151          return true;
00152       }
00153       
00154       void showButtons( bool start, bool quickstart, bool next )
00155       {
00156          startButton = start;
00157 
00158          assert ( start != next );
00159          assert ( !(start && quickstart) );
00160          
00161          if ( next )
00162             show("next");
00163          else
00164             hide("next");   
00165             
00166          if ( quickstart )
00167             show("quickstart");
00168          else
00169             hide("quickstart");   
00170 
00171          if ( start )
00172             show("start");
00173          else
00174             hide("start");   
00175       };
00176 
00177 
00178       GameMap* searchForMap( const ASCString& password );
00179 
00180    protected:   
00181       void userHandler( const ASCString& label, PropertyReadingContainer& pc, PG_Widget* parent, WidgetParameters widgetParams ); 
00182       bool start();
00183       bool Apply();
00184       
00185       bool eventKeyDown(const SDL_KeyboardEvent *key){
00186          if(key->keysym.sym == SDLK_ESCAPE) {
00187             if ( page <= 2 )
00188                QuitModal();
00189             return true;
00190          }
00191          return false;
00192       }
00193       
00194    public:
00195       StartMultiplayerGame(PG_MessageObject* c);
00196       ~StartMultiplayerGame();
00197 
00198       bool getSuccess()
00199       {
00200          return success;
00201       }
00202    
00203 };
00204 
00205 const char* StartMultiplayerGame::buttonLabels[8] = {
00206    "NewCampaign",
00207    "ContinueCampaign",
00208    "SinglePlayer",
00209    "Hotseat",
00210    "PBEM",
00211    "PBP",
00212    "PBEMServer",
00213    NULL
00214 };
00215 
00216 
00217 StartMultiplayerGame::StartMultiplayerGame(PG_MessageObject* c): ConfigurableWindow( NULL, PG_Rect::null, "newmultiplayergame", false ), startButton(false), success(false), newMap(NULL), page(ModeSelection), mode ( 0 ), replay(true), supervisorEnabled(false),
00218    mapParameterEditor(NULL), mapParameterEditorParent(NULL),
00219    allianceSetup(NULL), allianceSetupParent(NULL),
00220    playerSetup(NULL), playerSetupParent(NULL),
00221    emailSetup(NULL), emailSetupParent(NULL),
00222    pbemserver(NULL)
00223 {
00224     setup();
00225     sigClose.connect( SigC::slot( *this, &StartMultiplayerGame::QuitModal ));
00226     
00227     PG_RadioButton* b1 = dynamic_cast<PG_RadioButton*>(FindChild(buttonLabels[0], true ));
00228     if ( b1 ) 
00229       b1->SetPressed();
00230 
00231     PG_Button* next = dynamic_cast<PG_Button*>(FindChild("next", true ));
00232     if ( next )
00233       next->sigClick.connect( SigC::slot( *this, &StartMultiplayerGame::nextPage ));
00234 
00235     PG_Button* start = dynamic_cast<PG_Button*>(FindChild("start", true ));
00236     if ( start )
00237       start->sigClick.connect( SigC::slot( *this, &StartMultiplayerGame::start ));
00238       
00239     PG_Button* qstart = dynamic_cast<PG_Button*>(FindChild("quickstart", true ));
00240     if ( qstart )
00241       qstart->sigClick.connect( SigC::slot( *this, &StartMultiplayerGame::quickstart ));
00242       
00243     PG_CheckButton* supervisor = dynamic_cast<PG_CheckButton*>(FindChild("SupervisorOn", true ));
00244     if ( supervisor ) {
00245        if ( supervisorEnabled )
00246          supervisor->SetPressed();
00247        else
00248           supervisor->SetUnpressed();
00249        supervisor->sigClick.connect( SigC::slot( *this, &StartMultiplayerGame::togglesupervisor ));
00250     }
00251 
00252     PG_LineEdit* s1 = dynamic_cast<PG_LineEdit*>( FindChild( "SupervisorPlain", true ));
00253     if ( s1 )
00254        s1->SetPassHidden ('*');
00255     
00256     
00257     PG_CheckButton* cb = dynamic_cast<PG_CheckButton*>( FindChild( "Replay", true ));
00258     if ( cb )
00259        if ( replay )
00260          cb->SetPressed( );
00261        else
00262          cb->SetUnpressed();
00263 
00264     
00265     showPage();
00266     showButtons(false,false,true);
00267     
00268     loadPBEMServerDefaults();
00269     loadCampaigns();
00270 }
00271 
00272 
00273 StartMultiplayerGame::~StartMultiplayerGame()
00274 {
00275    if ( newMap )
00276       delete newMap;
00277    
00278    if ( pbemserver )
00279       delete pbemserver;
00280 }      
00281 
00282 bool StartMultiplayerGame::campaignSelected()
00283 {
00284    
00285    TextRenderer* text = dynamic_cast<TextRenderer*>( FindChild( "CampaignDescription", true ));
00286    if ( text ) {
00287       Campaign* c = getSelectedCampaign();
00288       if ( c )
00289          text->SetText( c->description );
00290    }
00291    return true;
00292 }
00293 
00294 void StartMultiplayerGame::loadCampaigns()
00295 {
00296    PG_ListBox* list = dynamic_cast<PG_ListBox*>( FindChild( "CampaignList", true ));
00297    if ( !list ) 
00298       return;
00299    
00300    list->sigSelectItem.connect( SigC::slot( *this, &StartMultiplayerGame::campaignSelected ));
00301    
00302    
00303    tfindfile ff ( "*.asccampaign" );
00304    tfindfile::FileInfo fi;
00305    while ( ff.getnextname( fi) ) {
00306       try {
00307          tnfilestream f ( fi.name, tnstream::reading );
00308          TextFormatParser parser(&f);
00309          
00310          PropertyReadingContainer prc ( "campaign", parser.run());
00311          Campaign* c = new Campaign();
00312          c->runTextIO(prc);
00313    
00314          campaigns.push_back ( c );
00315          
00316          if ( list )
00317             new PG_ListBoxItem( list, 20, c->name );
00318          
00319       } catch ( ParsingError pe ) {
00320          errorMessage( pe.getMessage( ));
00321       }
00322    }
00323 }
00324 
00325 
00326 Campaign* StartMultiplayerGame::getSelectedCampaign()
00327 {
00328    PG_ListBox* list = dynamic_cast<PG_ListBox*>( FindChild( "CampaignList", true ));
00329       
00330    if ( list )
00331       for ( int i = 0; i < list->GetWidgetCount(); ++i ) {
00332          PG_ListBoxBaseItem* bi = dynamic_cast<PG_ListBoxBaseItem*>(list->FindWidget(i));
00333          if ( bi && bi->IsSelected() )
00334             for ( Campaigns::iterator i = campaigns.begin(); i != campaigns.end(); ++i )
00335                if ( (*i)->name == bi->GetText() )
00336                   return *i;
00337       }
00338 
00339    
00340    return NULL;
00341 }
00342 
00343 
00344 bool StartMultiplayerGame::Apply()
00345 {
00346    switch ( page )  {
00347       case FilenameSelection: {
00348             if ( !filename.empty() )
00349                if ( exist( filename )) {
00350                   delete newMap;
00351                   newMap = mapLoadingExceptionChecker( filename, MapLoadingFunction( tmaploaders::loadmap ));
00352                   if ( newMap ) {
00353                      if ( mode != NewCampaign && mode != ContinueCampaign )
00354                         newMap->campaign.avail = false;
00355                      else
00356                         replay = false;
00357 
00358                      if ( mode == Skirmish )
00359                         replay = false; 
00360 
00361                      if ( checkPlayerStat() )
00362                         return true;
00363                      else {
00364                         delete newMap;
00365                         newMap = NULL;
00366                         return false;
00367                      }      
00368                   }
00369                 }
00370                
00371          }
00372          break;
00373       case PlayerSetup: 
00374             if ( playerSetup->Valid() ) {
00375                playerSetup->Apply();
00376                return true;
00377             }   
00378             break;       
00379       case EmailSetup:
00380             emailSetup->Apply();      
00381             return true;
00382               
00383       case AllianceSetup: 
00384          allianceSetup->Apply();
00385          return true;
00386          
00387       case MapParameterEditor: 
00388             if ( mapParameterEditor->Valid() ) {
00389                mapParameterEditor->Apply();
00390                return true;
00391             }   
00392             break;   
00393       case MultiPlayerOptions:
00394          {
00395             PG_CheckButton* cb = dynamic_cast<PG_CheckButton*>( FindChild( "Replay", true ));
00396             if ( cb )
00397                replay = cb->GetPressed();
00398             
00399             PG_LineEdit* le = dynamic_cast<PG_LineEdit*>( FindChild( "Filename", true ));
00400             if ( le &&  !le->GetText().empty() ) 
00401                PBEMfilename = le->GetText();
00402                
00403             if ( supervisorEnabled ) {
00404                PG_LineEdit* s1 = dynamic_cast<PG_LineEdit*>( FindChild( "SupervisorPlain", true ));
00405                PG_LineEdit* s2 = dynamic_cast<PG_LineEdit*>( FindChild( "SupervisorEnc", true ));
00406 
00407                if ( s1 && s2 && !s1->GetText().empty() && !s2->GetText().empty()) {
00408                   Password pwd;
00409                   pwd.setUnencoded( s1->GetText() );
00410                   
00411                   Password pwd2;
00412                   pwd2.setEncoded( s2->GetText() );
00413                   if ( pwd != pwd2 ) {
00414                      warning ( "Passwords don't match!");
00415                      return false;
00416                   }
00417                }
00418 
00419                if ( s1 && !s1->GetText().empty() )
00420                   supervisorPassword.setUnencoded( s1->GetText() );
00421 
00422                if ( s2 && !s2->GetText().empty() )
00423                   supervisorPassword.setEncoded( s2->GetText() );
00424             }
00425             return true;
00426          }      
00427          break;
00428       case PBEMServerSetup:
00429           {
00430             if ( !pbemserver )
00431                pbemserver = new PBEMServer();
00432             
00433             // @PBEMSERVER
00434             // transferring the data from the dialog to the class that will be associated with the game
00435             
00436             
00437             PG_LineEdit* le = dynamic_cast<PG_LineEdit*>( FindChild( "Server", true ));
00438             if ( le &&  !le->GetText().empty() ) 
00439                pbemserver->setServerAddress ( le->GetText());
00440             
00441          }
00442          return true;
00443       case PasswordSearch: {
00444                PG_LineEdit* pw = dynamic_cast<PG_LineEdit*>( FindChild( "MapPassword", true ));
00445                if ( pw ) {
00446                   if ( pw->GetText().empty() ) {
00447                      errorMessage("please enter a password");
00448                      return false;
00449                   }
00450                   
00451                   GameMap* map = searchForMap( pw->GetText() );
00452                   if ( !map  ) {
00453                      errorMessage("No map found");
00454                      return false;
00455                   }
00456                   newMap = map;
00457                   // start();
00458                   return true;
00459                }
00460                            
00461          }
00462          break;
00463 
00464       case CampaignChoser: {
00465          Campaign* c =getSelectedCampaign();
00466          if ( c ) {
00467             if ( c->file.empty() )
00468                return false;
00469             
00470             tfindfile ff ( c->file );
00471             if ( ff.getFoundFileNum() < 1 )
00472                return false;
00473             
00474             if ( ff.getFoundFileNum() == 1 ) {
00475                tfindfile::FileInfo fi;
00476                if ( !ff.getnextname( fi) )
00477                   return false;
00478                
00479                filename = fi.name;
00480                return true;
00481             } else {
00482                filename =  selectFile( c->file, true );
00483                return !filename.empty();
00484             }
00485          } else 
00486             return false;
00487       }
00488          
00489       default: 
00490            break;
00491    }
00492    
00493    return false;
00494 }
00495 
00496 GameMap* StartMultiplayerGame::searchForMap( const ASCString& password )
00497 {
00498    StatusMessageWindowHolder smw = MessagingHub::Instance().infoMessageWindow( "please wait, searching for map...");
00499 
00500    tfindfile ff ( mapextension );
00501    string filename = ff.getnextname();
00502    while( !filename.empty() ) {
00503        try {
00504           tmaploaders loader;
00505           GameMap* map = loader.loadmap( filename );
00506           if ( map->codeWord == password )
00507              return map;
00508           else
00509              delete map;
00510       }
00511       catch ( ASCexception ) {
00512       } /* endcatch */
00513 
00514       filename = ff.getnextname();
00515    }
00516    return NULL;
00517 }
00518 
00519 bool StartMultiplayerGame::nextPage(PG_Button* button)
00520 {
00521    int oldpage = page;
00522    switch ( page )  {
00523       case ModeSelection: {
00524             int i = 0;
00525             while ( buttonLabels[i] ) {
00526                PG_RadioButton* b = dynamic_cast<PG_RadioButton*>(FindChild(buttonLabels[i], true ));
00527                if ( b && b->GetPressed() ) {
00528                   mode = i;
00529                   break;
00530                }
00531                ++i;
00532             }   
00533             if ( mode == ContinueCampaign )
00534                page = PasswordSearch;
00535             else
00536                if ( mode == NewCampaign )
00537                   page = CampaignChoser;
00538                else
00539                   page = FilenameSelection;
00540          }
00541          break;
00542          
00543       case FilenameSelection: 
00544             if ( Apply() ) 
00545                page = PlayerSetup;
00546             break;
00547       case PlayerSetup: 
00548             if ( Apply() )
00549                if ( mode == PBEM || mode == PBP || mode == PBEM_Server )
00550                   page = EmailSetup;
00551                else
00552                   page = AllianceSetup;
00553                   
00554             break;       
00555       case EmailSetup: 
00556             if ( Apply() ) {
00557                if ( mode == PBEM_Server )
00558                   page = PBEMServerSetup;
00559                else
00560                   page = AllianceSetup;
00561             }
00562             break;
00563               
00564       case PBEMServerSetup:
00565             if ( Apply() ) {
00566                page = AllianceSetup;
00567             }
00568             break;
00569 
00570       case AllianceSetup: 
00571             if ( Apply() )
00572                page = MapParameterEditor;
00573             break;
00574               
00575       case MapParameterEditor: 
00576             if ( Apply() )
00577                page = MultiPlayerOptions;
00578             break;   
00579             
00580       default: 
00581            break;
00582    }
00583    
00584    if ( oldpage != page ) {
00585       if ( page == PlayerSetup && playerSetupParent ) {
00586          delete playerSetup;
00587          if ( mode == Skirmish )
00588             playerSetup = new PlayerSetupWidget( newMap, PlayerSetupWidget::AllEditableSinglePlayer, playerSetupParent, PG_Rect( 0, 0, playerSetupParent->Width(), playerSetupParent->Height() ));
00589          else  
00590             playerSetup = new PlayerSetupWidget( newMap, PlayerSetupWidget::AllEditable, playerSetupParent, PG_Rect( 0, 0, playerSetupParent->Width(), playerSetupParent->Height() ));
00591       }   
00592       
00593       if ( page == AllianceSetup && allianceSetupParent ) {
00594          delete allianceSetup;
00595          allianceSetup = new AllianceSetupWidget( newMap, true, allianceSetupParent, PG_Rect( 0, 0, allianceSetupParent->Width(), allianceSetupParent->Height() ));
00596       }   
00597 
00598       if ( page == MapParameterEditor && mapParameterEditorParent ) {
00599          if ( mode == PBP )
00600             newMap->setgameparameter(cgp_superVisorCanSaveMap,1);
00601          delete mapParameterEditor;
00602          mapParameterEditor = new GameParameterEditorWidget( newMap, mapParameterEditorParent, PG_Rect( 0, 0, mapParameterEditorParent->Width(), mapParameterEditorParent->Height() ));
00603       }   
00604 
00605       if ( page == EmailSetup && emailSetupParent ) {
00606          delete emailSetup;
00607          emailSetup = new EmailSetupWidget( newMap, -1, emailSetupParent, PG_Rect( 0, 0, emailSetupParent->Width(), emailSetupParent->Height() ));
00608       }   
00609       
00610       if ( page == MultiPlayerOptions ) {
00611          PG_LineEdit* le = dynamic_cast<PG_LineEdit*>( FindChild( "Filename", true ));
00612          PBEMfilename = getDefaultPBEM_Filename();
00613          if ( le )
00614             le->SetText( PBEMfilename );         
00615       }
00616                
00617       showPage();
00618       return true;
00619    } else
00620       return false;
00621 }
00622 
00623 void StartMultiplayerGame::loadPBEMServerDefaults()
00624 {
00625       if ( !pbemserver )
00626          pbemserver = new PBEMServer();
00627       
00628       // @PBEMSERVER
00629       // transferring the data from the dialog to the class that will be associated with the game
00630    
00631       PG_LineEdit* le = dynamic_cast<PG_LineEdit*>( FindChild( "Server", true ));
00632       if ( le )
00633          le->SetText( PBEMServer::getDefaultServerAddress() );
00634    
00635 }
00636 
00637 
00638 void StartMultiplayerGame::showPage()
00639 {
00640    for ( int i = 0; i < pageCount; ++i ) {
00641       ASCString name = "Page" + ASCString::toString(i);
00642       if ( page == i )
00643          show( name );
00644       else   
00645          hide( name );
00646    }
00647    showSupervisorWidgets();
00648    
00649    switch ( page ) {
00650       case CampaignChoser:
00651          showButtons(true,false,false);
00652          break;
00653    
00654       case FilenameSelection: 
00655          if ( mode == NewCampaign || mode == ContinueCampaign )
00656             showButtons(true,false,false);
00657          else
00658             showButtons(false,true,true);
00659          break;
00660          
00661       case MapParameterEditor: 
00662          if ( mode != Hotseat && mode != PBEM )
00663             showButtons(true,false,false);
00664          else   
00665             showButtons(false,true,true);
00666          break;
00667       case MultiPlayerOptions: 
00668          showButtons(true,false,false);
00669          break;
00670       case PasswordSearch:
00671          showButtons(true,false,false);
00672          break;
00673 
00674       default:
00675          break;   
00676     }     
00677 }
00678 
00679 
00680 
00681 void StartMultiplayerGame::userHandler( const ASCString& label, PropertyReadingContainer& pc, PG_Widget* parent, WidgetParameters widgetParams )
00682 {
00683    if ( label == "FileList" ) {
00684       FileSelectionItemFactory* factory = new FileSelectionItemFactory( mapextension );
00685       factory->filenameSelectedKeyb.connect ( SigC::slot( *this, &StartMultiplayerGame::fileNameSelected ));
00686       factory->filenameSelectedMouse.connect ( SigC::slot( *this, &StartMultiplayerGame::fileNameMarked ));
00687       factory->filenameMarked.connect   ( SigC::slot( *this, &StartMultiplayerGame::fileNameMarked ));
00688       new ItemSelectorWidget( parent, PG_Rect(0, 0, parent->Width(), parent->Height()), factory );
00689    }
00690    
00691    if ( label == "PlayerSetup" ) 
00692       playerSetupParent = parent;
00693       
00694    if ( label == "AllianceSetup" ) 
00695       allianceSetupParent = parent;
00696       
00697    if ( label == "GameParameters" )
00698       mapParameterEditorParent = parent; 
00699       
00700    if ( label == "EmailSetup" )
00701       emailSetupParent = parent;
00702   
00703 }
00704 
00705 bool StartMultiplayerGame::checkPlayerStat()
00706 {
00707    ASCString msg;
00708    if ( mode == NewCampaign || mode == ContinueCampaign ) {
00709       bool humanFound = false;
00710       for ( int i = 0; i < newMap->getPlayerCount(); ++i )
00711          if ( newMap->player[i].exist() )
00712             if ( newMap->player[i].stat == Player::human )
00713                humanFound = true;
00714 
00715       if ( !humanFound ) {
00716          warning("no human players on map!");
00717          return false;
00718       }
00719       return true;
00720    }
00721 
00722    if ( mode == Skirmish ) {
00723       bool humanFound = false;
00724       for ( int i = 0; i < newMap->getPlayerCount(); ++i )
00725          if ( newMap->player[i].exist() )
00726             if ( newMap->player[i].stat == Player::human || newMap->player[i].stat == Player::supervisor )
00727                if ( humanFound ) {
00728                   newMap->player[i].stat = Player::computer;
00729                   msg += newMap->player[i].getName() + " has been switch to AI\n";
00730                } else
00731                   humanFound = true;   
00732                   
00733       if ( !humanFound )            
00734          for ( int i = 0; i < newMap->getPlayerCount(); ++i )
00735             if ( newMap->player[i].exist() )
00736                if ( newMap->player[i].stat == Player::computer ) {
00737                   newMap->player[i].stat = Player::human;
00738                   msg += newMap->player[i].getName() + " has been switch to human\n";
00739                   humanFound = true;   
00740                   break;
00741                }   
00742 
00743       bool aiFound = false;
00744       for ( int i = 0; i < newMap->getPlayerCount(); ++i )
00745          if ( newMap->player[i].exist() )
00746             if ( newMap->player[i].stat == Player::computer )
00747                aiFound = true;   
00748                   
00749       if ( !aiFound )            
00750          for ( int i = 0; i < newMap->getPlayerCount(); ++i )
00751             if ( newMap->player[i].exist() )
00752                if ( newMap->player[i].stat == Player::off ) {
00753                   newMap->player[i].stat = Player::computer;
00754                   msg += newMap->player[i].getName() + " has been switch to AI\n";
00755                   aiFound = true;   
00756                   break;
00757                }   
00758 
00759 
00760        /*
00761       if ( !aiFound ) {
00762          MessagingHub::Instance().error("Map has no opponents!");
00763          return false;
00764       }   
00765       */
00766    }
00767 
00768    if ( mode == PBEM || mode == Hotseat ) {
00769       int humanNum = 0;
00770       for ( int i = 0; i < newMap->getPlayerCount(); ++i )
00771          if ( newMap->player[i].exist() )
00772             if ( newMap->player[i].stat == Player::human || newMap->player[i].stat == Player::supervisor )
00773                ++humanNum;   
00774             
00775       if ( humanNum <= 1 )
00776          for ( int i = 0; i < newMap->getPlayerCount(); ++i )
00777             if ( newMap->player[i].exist() )
00778                if ( newMap->player[i].stat == Player::computer || newMap->player[i].stat == Player::off ) {
00779                   newMap->player[i].stat = Player::human;
00780                   msg += newMap->player[i].getName() + " has been switched to human\n";
00781                   ++humanNum;
00782                }
00783             
00784             
00785       if ( humanNum <= 1 ) {
00786          MessagingHub::Instance().error("Not enough players on map for multiplayer game!");
00787          return false;
00788       }   
00789    }
00790    
00791    if ( mode == PBP  ) {
00792       newMap->player[0].stat = Player::supervisor;
00793       
00794       int humanNum = 0;
00795       for ( int i = 1; i < newMap->getPlayerCount(); ++i )
00796          if ( newMap->player[i].exist() )
00797             if ( newMap->player[i].stat == Player::human || newMap->player[i].stat == Player::supervisor )
00798                ++humanNum;   
00799             
00800       if ( humanNum < 1 )
00801          for ( int i = 1; i < newMap->getPlayerCount(); ++i )
00802             if ( newMap->player[i].exist() )
00803                if ( newMap->player[i].stat == Player::computer || newMap->player[i].stat == Player::off ) {
00804                   newMap->player[i].stat = Player::human;
00805                   msg += newMap->player[i].getName() + " has been switched to human\n";
00806                   ++humanNum;
00807                }
00808    }
00809    
00810   // if ( !msg.empty() )
00811   //    infoMessage( msg );
00812    return true;
00813 }
00814 
00815 ASCString StartMultiplayerGame::getDefaultPBEM_Filename()
00816 {
00817    ASCString filename = newMap->preferredFileNames.mapname[0];
00818    if( filename.empty() )
00819       filename = "game";   
00820    
00821    if ( filename.find( '.') != ASCString::npos )
00822       filename.erase( filename.find( '.'));
00823       
00824    filename += "-$p-$t";
00825    
00826    return filename;
00827 }
00828 
00829 
00830 void StartMultiplayerGame::setupNetwork()
00831 {
00832    if ( mode == PBP || mode == PBEM ) {
00833       if ( newMap && (!newMap->network || newMap->network->getMechanismID() != FileTransfer::mechanismID()) ) {
00834          
00835          delete newMap->network;
00836          
00837          FileTransfer* ft = new FileTransfer();
00838          newMap->network = ft;
00839          
00840          if ( !PBEMfilename.empty() )
00841             ft->setup( PBEMfilename );
00842          else   
00843             ft->setup( getDefaultPBEM_Filename() );
00844       }
00845    } else
00846    if ( mode == PBEM_Server ) {
00847       if ( newMap ) {
00848          delete newMap->network;
00849          newMap->network = pbemserver;
00850          
00851          pbemserver = NULL;
00852       }
00853    }
00854 }
00855 
00856 
00857 bool StartMultiplayerGame::start()
00858 {
00859    if ( !Apply() )
00860       return false;
00861    
00862    if ( !newMap ) 
00863       newMap = mapLoadingExceptionChecker( filename, MapLoadingFunction( tmaploaders::loadmap ));
00864      
00865    if ( !newMap )
00866       return false;
00867    
00868 //   if ( !checkPlayerStat() )
00869 //      return false;
00870 
00871    if ( replay )
00872       newMap->replayinfo = new GameMap::ReplayInfo;
00873    else
00874       if ( newMap->replayinfo ) {
00875          delete newMap->replayinfo;
00876          newMap->replayinfo = NULL;
00877       }
00878 
00879    if ( supervisorEnabled )
00880       newMap->supervisorpasswordcrc = supervisorPassword;
00881       
00882    
00883    setupNetwork();
00884   
00885    delete actmap;
00886    actmap = newMap;
00887    newMap = NULL;
00888    QuitModal();
00889    Hide();
00890    computeview( actmap );
00891    hookGuiToMap(actmap);
00892   
00893    next_turn();
00894    displaymap();
00895    updateFieldInfo();
00896    moveparams.movestatus = 0;
00897    success = true;
00898 
00899    if ( mode == PBP || mode == PBEM ) 
00900       sendGameParameterAsMail ( actmap );
00901 
00902    
00903    return true;
00904 }
00905 
00906 
00907 
00908 bool startMultiplayerGame()
00909 {
00910    bool res = false;
00911    try {
00912     StartMultiplayerGame smg(NULL);
00913     smg.Show();
00914     smg.RunModal();
00915     res = smg.getSuccess();
00916     smg.Hide();
00917    }
00918    catch ( ShutDownMap sdm ) {
00919       warning("No players found on map");
00920       throw sdm;
00921    }
00922     /*
00923     if ( res )
00924        actmap->sigPlayerUserInteractionBegins( actmap->player[actmap->actplayer] );
00925     */
00926     
00927     return res;
00928 }
00929 
00930 

Generated on Tue Jun 24 01:27:46 2008 for Advanced Strategic Command by  doxygen 1.4.2