Advanced Strategic Command
newgame.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #include <sstream>
22 #include <pgimage.h>
23 #include <pglistbox.h>
24 #include <pglistboxitem.h>
25 
26 #include "newgame.h"
27 #include "editmapparam.h"
28 #include "alliancesetup.h"
29 #include "playersetup.h"
30 #include "fileselector.h"
31 #include "emailsetup.h"
32 #include "../loaders.h"
33 #include "../gamemap.h"
34 #include "../paradialog.h"
35 #include "../turncontrol.h"
36 #include "../viewcalculation.h"
37 #include "../networkinterface.h"
38 #include "../spfst.h"
39 #include "../spfst-legacy.h"
40 #include "../windowing.h"
41 #include "../controls.h"
42 #include "../sg.h"
43 #include "../gamedlg.h"
44 #include "../network/simple_file_transfer.h"
45 #include "../network/pbem-server.h"
46 #include "../textfile_evaluation.h"
47 #include "../textfileparser.h"
48 #include "../widgets/textrenderer.h"
49 #include "../mapdisplayinterface.h"
50 #include "../campaignactionrecorder.h"
51 
53 
54 const int pageCount = 11;
55 
56 class Campaign {
57  public:
61 
63  pc.addString ( "name", name);
64  pc.addString ( "description", description );
65  pc.addString ( "file", file );
66  }
67 };
68 
70  private:
71  bool startButton;
72  bool success;
73  GameMap* newMap;
74 
75  enum Pages { ModeSelection = 1, FilenameSelection, PlayerSetup, EmailSetup, AllianceSetup, MapParameterEditor, MultiPlayerOptions, PasswordSearch, PBEMServerSetup, CampaignChooser };
76  Pages page;
77 
78  enum Mode { NewCampaign, ContinueCampaign, Skirmish, Hotseat, PBEM, PBP, PBEM_Server };
79  int mode;
80 
81  static const char* buttonLabels[];
82 
83  ASCString filename;
84  ASCString PBEMfilename;
85  bool replay;
86  bool supervisorEnabled;
87  Password supervisorPassword;
88 
89  GameParameterEditorWidget* mapParameterEditor;
90  PG_Widget* mapParameterEditorParent;
91 
92  AllianceSetupWidget* allianceSetup;
93  PG_Widget* allianceSetupParent;
94  DirectAllianceSetupStrategy directAllianceSetupStrategy;
95 
96  PlayerSetupWidget* playerSetup;
97  PG_Widget* playerSetupParent;
98 
99  EmailSetupWidget* emailSetup;
100  PG_Widget* emailSetupParent;
101 
102  bool nextPage();
103  void showPage();
104  bool checkPlayerStat();
105 
106  ASCString getDefaultPBEM_Filename();
107  void setupNetwork();
108 
109  PBEMServer* pbemserver;
110 
111  void loadPBEMServerDefaults();
112 
114  Campaigns campaigns;
115  void loadCampaigns();
116  Campaign* getSelectedCampaign();
117  bool campaignSelected();
118 
119  void fileNameSelected( const ASCString& filename )
120  {
121  this->filename = filename;
122  if ( startButton )
123  start();
124  else
125  nextPage();
126  };
127 
128  void fileNameMarked( const ASCString& filename )
129  {
130  this->filename = filename;
131  };
132 
133  bool quickstart()
134  {
135  if ( Apply() )
136  start();
137  return true;
138  }
139 
140 
141  void showSupervisorWidgets()
142  {
143  if ( page == MultiPlayerOptions ) {
144  if ( supervisorEnabled )
145  show( "SuperVisor" );
146  else
147  hide( "SuperVisor" );
148  }
149  }
150 
151  bool togglesupervisor( bool on )
152  {
153  supervisorEnabled = on;
154  showSupervisorWidgets();
155  return true;
156  }
157 
158  void showButtons( bool start, bool quickstart, bool next )
159  {
160  startButton = start;
161 
162  assert ( start != next );
163  assert ( !(start && quickstart) );
164 
165  if ( next )
166  show("next");
167  else
168  hide("next");
169 
170  if ( quickstart )
171  show("quickstart");
172  else
173  hide("quickstart");
174 
175  if ( start )
176  show("start");
177  else
178  hide("start");
179  };
180 
181 
182  GameMap* searchForMap( const ASCString& password );
183 
184  protected:
185  void userHandler( const ASCString& label, PropertyReadingContainer& pc, PG_Widget* parent, WidgetParameters widgetParams );
186  bool start();
187  bool Apply();
188 
189  bool eventKeyDown(const SDL_KeyboardEvent *key){
190  if(key->keysym.sym == SDLK_ESCAPE) {
191  if ( page <= 2 )
192  QuitModal();
193  return true;
194  }
195  return false;
196  }
197 
198  public:
199  StartMultiplayerGame(PG_MessageObject* c);
201 
202  bool getSuccess()
203  {
204  return success;
205  }
206 
207 };
208 
209 const char* StartMultiplayerGame::buttonLabels[8] = {
210  "NewCampaign",
211  "ContinueCampaign",
212  "SinglePlayer",
213  "Hotseat",
214  "PBEM",
215  "PBP",
216  "PBEMServer",
217  NULL
218 };
219 
220 
221 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),
222  mapParameterEditor(NULL), mapParameterEditorParent(NULL),
223  allianceSetup(NULL), allianceSetupParent(NULL),
224  playerSetup(NULL), playerSetupParent(NULL),
225  emailSetup(NULL), emailSetupParent(NULL),
226  pbemserver(NULL)
227 {
228  setup();
229  sigClose.connect( sigc::mem_fun( *this, &StartMultiplayerGame::QuitModal ));
230 
231  PG_RadioButton* b1 = dynamic_cast<PG_RadioButton*>(FindChild(buttonLabels[0], true ));
232  if ( b1 )
233  b1->SetPressed();
234 
235  PG_Button* next = dynamic_cast<PG_Button*>(FindChild("next", true ));
236  if ( next )
237  next->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &StartMultiplayerGame::nextPage )));
238 
239  PG_Button* start = dynamic_cast<PG_Button*>(FindChild("start", true ));
240  if ( start )
241  start->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &StartMultiplayerGame::start )));
242 
243  PG_Button* qstart = dynamic_cast<PG_Button*>(FindChild("quickstart", true ));
244  if ( qstart )
245  qstart->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &StartMultiplayerGame::quickstart )));
246 
247  PG_CheckButton* supervisor = dynamic_cast<PG_CheckButton*>(FindChild("SupervisorOn", true ));
248  if ( supervisor ) {
249  if ( supervisorEnabled )
250  supervisor->SetPressed();
251  else
252  supervisor->SetUnpressed();
253  supervisor->sigClick.connect( sigc::mem_fun( *this, &StartMultiplayerGame::togglesupervisor ));
254  }
255 
256  PG_LineEdit* s1 = dynamic_cast<PG_LineEdit*>( FindChild( "SupervisorPlain", true ));
257  if ( s1 )
258  s1->SetPassHidden ('*');
259 
260 
261  PG_CheckButton* cb = dynamic_cast<PG_CheckButton*>( FindChild( "Replay", true ));
262  if ( cb ) {
263  if ( replay )
264  cb->SetPressed( );
265  else
266  cb->SetUnpressed();
267  }
268 
269  showPage();
270  showButtons(false,false,true);
271 
272  loadPBEMServerDefaults();
273  loadCampaigns();
274 }
275 
276 
278 {
279  if ( newMap )
280  delete newMap;
281 
282  if ( pbemserver )
283  delete pbemserver;
284 }
285 
286 bool StartMultiplayerGame::campaignSelected()
287 {
288 
289  TextRenderer* text = dynamic_cast<TextRenderer*>( FindChild( "CampaignDescription", true ));
290  if ( text ) {
291  Campaign* c = getSelectedCampaign();
292  if ( c )
293  text->SetText( c->description );
294  }
295  return true;
296 }
297 
298 void StartMultiplayerGame::loadCampaigns()
299 {
300  PG_ListBox* list = dynamic_cast<PG_ListBox*>( FindChild( "CampaignList", true ));
301  if ( !list )
302  return;
303 
304  list->sigSelectItem.connect( sigc::hide( sigc::mem_fun( *this, &StartMultiplayerGame::campaignSelected )));
305 
306 
307  tfindfile ff ( "*.asccampaign" );
309  while ( ff.getnextname( fi) ) {
310  try {
312  TextFormatParser parser(&f);
313 
314  PropertyReadingContainer prc ( "campaign", parser.run());
315  Campaign* c = new Campaign();
316  c->runTextIO(prc);
317 
318  campaigns.push_back ( c );
319 
320  if ( list )
321  new PG_ListBoxItem( list, 20, c->name );
322 
323  } catch ( ParsingError pe ) {
324  errorMessage( pe.getMessage( ));
325  }
326  }
327 }
328 
329 
330 Campaign* StartMultiplayerGame::getSelectedCampaign()
331 {
332  PG_ListBox* list = dynamic_cast<PG_ListBox*>( FindChild( "CampaignList", true ));
333 
334  if ( list )
335  for ( int i = 0; i < list->GetWidgetCount(); ++i ) {
336  PG_ListBoxBaseItem* bi = dynamic_cast<PG_ListBoxBaseItem*>(list->FindWidget(i));
337  if ( bi && bi->IsSelected() )
338  for ( Campaigns::iterator i = campaigns.begin(); i != campaigns.end(); ++i )
339  if ( (*i)->name == bi->GetText() )
340  return *i;
341  }
342 
343 
344  return NULL;
345 }
346 
347 
349 {
350  switch ( page ) {
351  case FilenameSelection: {
352  if ( !filename.empty() )
353  if ( exist( filename )) {
354  delete newMap;
356  if ( newMap ) {
357  if ( mode != NewCampaign && mode != ContinueCampaign )
358  newMap->campaign.avail = false;
359  else
360  replay = false;
361 
362  if ( mode == Skirmish )
363  replay = false;
364 
365  if ( checkPlayerStat() )
366  return true;
367  else {
368  delete newMap;
369  newMap = NULL;
370  return false;
371  }
372  }
373  }
374 
375  }
376  break;
377  case PlayerSetup:
378  if ( playerSetup->Valid() ) {
379  playerSetup->Apply();
380  return true;
381  }
382  break;
383  case EmailSetup:
384  emailSetup->Apply();
385  return true;
386 
387  case AllianceSetup:
388  allianceSetup->Apply();
389  return true;
390 
391  case MapParameterEditor:
392  if ( mapParameterEditor->Valid() ) {
393  mapParameterEditor->Apply();
394  return true;
395  }
396  break;
397  case MultiPlayerOptions:
398  {
399  PG_CheckButton* cb = dynamic_cast<PG_CheckButton*>( FindChild( "Replay", true ));
400  if ( cb )
401  replay = cb->GetPressed();
402 
403  PG_LineEdit* le = dynamic_cast<PG_LineEdit*>( FindChild( "Filename", true ));
404  if ( le && !le->GetText().empty() )
405  PBEMfilename = le->GetText();
406 
407  if ( supervisorEnabled ) {
408  PG_LineEdit* s1 = dynamic_cast<PG_LineEdit*>( FindChild( "SupervisorPlain", true ));
409  PG_LineEdit* s2 = dynamic_cast<PG_LineEdit*>( FindChild( "SupervisorEnc", true ));
410 
411  if ( s1 && s2 && !s1->GetText().empty() && !s2->GetText().empty()) {
412  Password pwd;
413  pwd.setUnencoded( s1->GetText() );
414 
415  Password pwd2;
416  pwd2.setEncoded( s2->GetText() );
417  if ( pwd != pwd2 ) {
418  warningMessage ( "Passwords don't match!");
419  return false;
420  }
421  }
422 
423  if ( s1 && !s1->GetText().empty() )
424  supervisorPassword.setUnencoded( s1->GetText() );
425 
426  if ( s2 && !s2->GetText().empty() )
427  supervisorPassword.setEncoded( s2->GetText() );
428  }
429  return true;
430  }
431  break;
432  case PBEMServerSetup:
433  {
434  if ( !pbemserver )
435  pbemserver = new PBEMServer();
436 
437  // @PBEMSERVER
438  // transferring the data from the dialog to the class that will be associated with the game
439 
440 
441  PG_LineEdit* le = dynamic_cast<PG_LineEdit*>( FindChild( "Server", true ));
442  if ( le && !le->GetText().empty() )
443  pbemserver->setServerAddress ( le->GetText());
444 
445  }
446  return true;
447  case PasswordSearch: {
448  PG_LineEdit* pw = dynamic_cast<PG_LineEdit*>( FindChild( "MapPassword", true ));
449  if ( pw ) {
450  if ( pw->GetText().empty() ) {
451  errorMessage("please enter a password");
452  return false;
453  }
454 
455  GameMap* map = searchForMap( pw->GetText() );
456  if ( !map ) {
457  errorMessage("No map found");
458  return false;
459  }
460  newMap = map;
461  replay = false;
462  // start();
463  return true;
464  }
465 
466  }
467  break;
468 
469  case CampaignChooser: {
470  Campaign* c =getSelectedCampaign();
471  if ( c ) {
472  if ( c->file.empty() )
473  return false;
474 
475  tfindfile ff ( c->file );
476  if ( ff.getFoundFileNum() < 1 )
477  return false;
478 
479  if ( ff.getFoundFileNum() == 1 ) {
481  if ( !ff.getnextname( fi) )
482  return false;
483 
484  filename = fi.name;
485  replay = false;
486  return true;
487  } else {
488  filename = selectFile( c->file, true );
489  if ( filename.empty() ) {
490  return false;
491  } else {
492  replay = false;
493  return true;
494  }
495  }
496  } else
497  return false;
498  }
499 
500  default:
501  break;
502  }
503 
504  return false;
505 }
506 
507 GameMap* StartMultiplayerGame::searchForMap( const ASCString& password )
508 {
509  StatusMessageWindowHolder smw = MessagingHub::Instance().infoMessageWindow( "please wait, searching for map...");
510 
511  tfindfile ff ( mapextension );
512  string filename = ff.getnextname();
513  while( !filename.empty() ) {
514  try {
515  tmaploaders loader;
516  GameMap* map = loader.loadmap( filename );
517  if ( map->codeWord == password )
518  return map;
519  else
520  delete map;
521  }
522  catch ( ASCexception ) {
523  } /* endcatch */
524 
525  filename = ff.getnextname();
526  }
527  return NULL;
528 }
529 
530 bool StartMultiplayerGame::nextPage()
531 {
532  int oldpage = page;
533  switch ( page ) {
534  case ModeSelection: {
535  int i = 0;
536  while ( buttonLabels[i] ) {
537  PG_RadioButton* b = dynamic_cast<PG_RadioButton*>(FindChild(buttonLabels[i], true ));
538  if ( b && b->GetPressed() ) {
539  mode = i;
540  break;
541  }
542  ++i;
543  }
544  if ( mode == ContinueCampaign )
545  page = PasswordSearch;
546  else
547  if ( mode == NewCampaign )
548  page = CampaignChooser;
549  else
550  page = FilenameSelection;
551  }
552  break;
553 
554  case FilenameSelection:
555  if ( Apply() )
556  page = PlayerSetup;
557  break;
558  case PlayerSetup:
559  if ( Apply() ) {
560  if ( mode == PBEM || mode == PBP || mode == PBEM_Server )
561  page = EmailSetup;
562  else
563  page = AllianceSetup;
564  }
565  break;
566  case EmailSetup:
567  if ( Apply() ) {
568  if ( mode == PBEM_Server )
569  page = PBEMServerSetup;
570  else
571  page = AllianceSetup;
572  }
573  break;
574 
575  case PBEMServerSetup:
576  if ( Apply() ) {
577  page = AllianceSetup;
578  }
579  break;
580 
581  case AllianceSetup:
582  if ( Apply() )
583  page = MapParameterEditor;
584  break;
585 
586  case MapParameterEditor:
587  if ( Apply() )
588  page = MultiPlayerOptions;
589  break;
590 
591  default:
592  break;
593  }
594 
595  if ( oldpage != page ) {
596  if ( page == PlayerSetup && playerSetupParent ) {
597  delete playerSetup;
598  if ( mode == Skirmish )
599  playerSetup = new PlayerSetupWidget( newMap, PlayerSetupWidget::AllEditableSinglePlayer, playerSetupParent, PG_Rect( 0, 0, playerSetupParent->Width(), playerSetupParent->Height() ));
600  else
601  playerSetup = new PlayerSetupWidget( newMap, PlayerSetupWidget::AllEditable, playerSetupParent, PG_Rect( 0, 0, playerSetupParent->Width(), playerSetupParent->Height() ));
602  }
603 
604  if ( page == AllianceSetup && allianceSetupParent ) {
605  delete allianceSetup;
606  allianceSetup = new AllianceSetupWidget( newMap, &directAllianceSetupStrategy, true, allianceSetupParent, PG_Rect( 0, 0, allianceSetupParent->Width(), allianceSetupParent->Height() ));
607  }
608 
609  if ( page == MapParameterEditor && mapParameterEditorParent ) {
610  if ( mode == PBP )
612  delete mapParameterEditor;
613  mapParameterEditor = new GameParameterEditorWidget( newMap, mapParameterEditorParent, PG_Rect( 0, 0, mapParameterEditorParent->Width(), mapParameterEditorParent->Height() ));
614  }
615 
616  if ( page == EmailSetup && emailSetupParent ) {
617  delete emailSetup;
618  emailSetup = new EmailSetupWidget( newMap, -1, emailSetupParent, PG_Rect( 0, 0, emailSetupParent->Width(), emailSetupParent->Height() ));
619  }
620 
621  if ( page == MultiPlayerOptions ) {
622  PG_LineEdit* le = dynamic_cast<PG_LineEdit*>( FindChild( "Filename", true ));
623  PBEMfilename = getDefaultPBEM_Filename();
624  if ( le )
625  le->SetText( PBEMfilename );
626  }
627 
628  showPage();
629  return true;
630  } else
631  return false;
632 }
633 
634 void StartMultiplayerGame::loadPBEMServerDefaults()
635 {
636  if ( !pbemserver )
637  pbemserver = new PBEMServer();
638 
639  // @PBEMSERVER
640  // transferring the data from the dialog to the class that will be associated with the game
641 
642  PG_LineEdit* le = dynamic_cast<PG_LineEdit*>( FindChild( "Server", true ));
643  if ( le )
644  le->SetText( PBEMServer::getDefaultServerAddress() );
645 
646 }
647 
648 
649 void StartMultiplayerGame::showPage()
650 {
651  for ( int i = 0; i < pageCount; ++i ) {
652  ASCString name = "Page" + ASCString::toString(i);
653  if ( page == i )
654  show( name );
655  else
656  hide( name );
657  }
658  showSupervisorWidgets();
659 
660  switch ( page ) {
661  case CampaignChooser:
662  showButtons(true,false,false);
663  break;
664 
665  case FilenameSelection:
666  if ( mode == NewCampaign || mode == ContinueCampaign )
667  showButtons(true,false,false);
668  else
669  showButtons(false,true,true);
670  break;
671 
672  case MapParameterEditor:
673  if ( mode != Hotseat && mode != PBEM )
674  showButtons(true,false,false);
675  else
676  showButtons(false,true,true);
677  break;
678  case MultiPlayerOptions:
679  showButtons(true,false,false);
680  break;
681  case PasswordSearch:
682  showButtons(true,false,false);
683  break;
684 
685  default:
686  break;
687  }
688 }
689 
690 
691 
692 void StartMultiplayerGame::userHandler( const ASCString& label, PropertyReadingContainer& pc, PG_Widget* parent, WidgetParameters widgetParams )
693 {
694  if ( label == "FileList" ) {
696  factory->filenameSelectedKeyb.connect ( sigc::mem_fun( *this, &StartMultiplayerGame::fileNameSelected ));
697  factory->filenameSelectedMouse.connect ( sigc::mem_fun( *this, &StartMultiplayerGame::fileNameMarked ));
698  factory->filenameMarked.connect ( sigc::mem_fun( *this, &StartMultiplayerGame::fileNameMarked ));
699  new ItemSelectorWidget( parent, PG_Rect(0, 0, parent->Width(), parent->Height()), factory );
700  }
701 
702  if ( label == "PlayerSetup" )
703  playerSetupParent = parent;
704 
705  if ( label == "AllianceSetup" )
706  allianceSetupParent = parent;
707 
708  if ( label == "GameParameters" )
709  mapParameterEditorParent = parent;
710 
711  if ( label == "EmailSetup" )
712  emailSetupParent = parent;
713 
714 }
715 
716 bool StartMultiplayerGame::checkPlayerStat()
717 {
718  ASCString msg;
719  if ( mode == NewCampaign || mode == ContinueCampaign ) {
720  bool humanFound = false;
721  for ( int i = 0; i < newMap->getPlayerCount(); ++i )
722  if ( newMap->player[i].exist() )
723  if ( newMap->player[i].stat == Player::human )
724  humanFound = true;
725 
726  if ( !humanFound ) {
727  warningMessage("no human players on map!");
728  return false;
729  }
730  return true;
731  }
732 
733  if ( mode == Skirmish ) {
734  bool humanFound = false;
735  for ( int i = 0; i < newMap->getPlayerCount(); ++i )
736  if ( newMap->player[i].exist() )
737  if ( newMap->player[i].stat == Player::human || newMap->player[i].stat == Player::supervisor ) {
738  if ( humanFound ) {
739  newMap->player[i].stat = Player::computer;
740  msg += newMap->player[i].getName() + " has been switch to AI\n";
741  } else
742  humanFound = true;
743  }
744  if ( !humanFound )
745  for ( int i = 0; i < newMap->getPlayerCount(); ++i )
746  if ( newMap->player[i].exist() )
747  if ( newMap->player[i].stat == Player::computer ) {
748  newMap->player[i].stat = Player::human;
749  msg += newMap->player[i].getName() + " has been switch to human\n";
750  humanFound = true;
751  break;
752  }
753 
754  bool aiFound = false;
755  for ( int i = 0; i < newMap->getPlayerCount(); ++i )
756  if ( newMap->player[i].exist() )
757  if ( newMap->player[i].stat == Player::computer )
758  aiFound = true;
759 
760  if ( !aiFound )
761  for ( int i = 0; i < newMap->getPlayerCount(); ++i )
762  if ( newMap->player[i].exist() )
763  if ( newMap->player[i].stat == Player::off ) {
764  newMap->player[i].stat = Player::computer;
765  msg += newMap->player[i].getName() + " has been switch to AI\n";
766  aiFound = true;
767  break;
768  }
769 
770 
771  /*
772  if ( !aiFound ) {
773  MessagingHub::Instance().error("Map has no opponents!");
774  return false;
775  }
776  */
777  }
778 
779  if ( mode == PBEM || mode == Hotseat ) {
780  int humanNum = 0;
781  for ( int i = 0; i < newMap->getPlayerCount(); ++i )
782  if ( newMap->player[i].exist() )
783  if ( newMap->player[i].stat == Player::human || newMap->player[i].stat == Player::supervisor )
784  ++humanNum;
785 
786  if ( humanNum <= 1 )
787  for ( int i = 0; i < newMap->getPlayerCount(); ++i )
788  if ( newMap->player[i].exist() )
789  if ( newMap->player[i].stat == Player::computer || newMap->player[i].stat == Player::off ) {
790  newMap->player[i].stat = Player::human;
791  msg += newMap->player[i].getName() + " has been switched to human\n";
792  ++humanNum;
793  }
794 
795 
796  if ( humanNum <= 1 ) {
797  MessagingHub::Instance().error("Not enough players on map for multiplayer game!");
798  return false;
799  }
800  }
801 
802  if ( mode == PBP ) {
803  newMap->player[0].stat = Player::supervisor;
804 
805  int humanNum = 0;
806  for ( int i = 1; i < newMap->getPlayerCount(); ++i )
807  if ( newMap->player[i].exist() )
808  if ( newMap->player[i].stat == Player::human || newMap->player[i].stat == Player::supervisor )
809  ++humanNum;
810 
811  if ( humanNum < 1 )
812  for ( int i = 1; i < newMap->getPlayerCount(); ++i )
813  if ( newMap->player[i].exist() )
814  if ( newMap->player[i].stat == Player::computer || newMap->player[i].stat == Player::off ) {
815  newMap->player[i].stat = Player::human;
816  msg += newMap->player[i].getName() + " has been switched to human\n";
817  ++humanNum;
818  }
819  }
820 
821  // if ( !msg.empty() )
822  // infoMessage( msg );
823  return true;
824 }
825 
826 ASCString StartMultiplayerGame::getDefaultPBEM_Filename()
827 {
828  ASCString filename = newMap->preferredFileNames.mapname[0];
829  if( filename.empty() )
830  filename = "game";
831 
832  if ( filename.find( '.') != ASCString::npos )
833  filename.erase( filename.find( '.'));
834 
835  filename += "-$p-$t";
836 
837  return filename;
838 }
839 
840 
841 void StartMultiplayerGame::setupNetwork()
842 {
843  if ( mode == PBP || mode == PBEM ) {
844  if ( newMap && (!newMap->network || newMap->network->getMechanismID() != FileTransfer::mechanismID()) ) {
845 
846  delete newMap->network;
847 
848  FileTransfer* ft = new FileTransfer();
849  newMap->network = ft;
850 
851  if ( !PBEMfilename.empty() )
852  ft->setup( PBEMfilename );
853  else
854  ft->setup( getDefaultPBEM_Filename() );
855  }
856  } else
857  if ( mode == PBEM_Server ) {
858  if ( newMap ) {
859  delete newMap->network;
860  newMap->network = pbemserver;
861 
862  pbemserver = NULL;
863  }
864  }
865 }
866 
867 
869 {
870  if ( !Apply() )
871  return false;
872 
873  if ( !newMap )
875 
876  if ( !newMap )
877  return false;
878 
879 // if ( !checkPlayerStat() )
880 // return false;
881 
882  if ( replay )
883  newMap->replayinfo = new GameMap::ReplayInfo;
884  else
885  if ( newMap->replayinfo ) {
886  delete newMap->replayinfo;
887  newMap->replayinfo = NULL;
888  }
889 
890  if ( supervisorEnabled )
891  newMap->supervisorpasswordcrc = supervisorPassword;
892 
893 
894  setupNetwork();
895 
896  delete actmap;
897  actmap = newMap;
898  newMap = NULL;
899  QuitModal();
900  Hide();
901  computeview( actmap );
903 
905  displaymap();
906  updateFieldInfo();
907  success = true;
908 
909  if ( mode == PBP || mode == PBEM )
911 
912  if ( (mode == NewCampaign || mode == ContinueCampaign) && CGameOptions::Instance()->recordCampaignMaps )
914 
915 
916  return true;
917 }
918 
919 
920 
922 {
923  bool res = false;
924  try {
925  StartMultiplayerGame smg(NULL);
926  smg.Show();
927  smg.RunModal();
928  res = smg.getSuccess();
929  smg.Hide();
930  }
931  catch ( ShutDownMap sdm ) {
932  warningMessage("No players found on map");
933  throw sdm;
934  }
935  /*
936  if ( res )
937  actmap->sigPlayerUserInteractionBegins( actmap->player[actmap->actplayer] );
938  */
939 
940  return res;
941 }
942 
943 
void displaymap()
Definition: spfst.cpp:58
ASCString codeWord
the codeword for accessing a map in a campaign
Definition: gamemap.h:210
ASCString description
Definition: newgame.cpp:59
MapDisplayInterface & getDefaultMapDisplay()
static ASCString getDefaultServerAddress()
int computeview(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
completely computes the view
sigc::signal< void, const ASCString & > filenameSelectedKeyb
Definition: fileselector.h:100
void runTextIO(PropertyContainer &pc)
Definition: newgame.cpp:62
ASCString getnextname(int *loc=NULL, bool *inContainer=NULL, ASCString *location=NULL)
Returns the next entry of the internal file list.
Definition: basestrm.cpp:2211
ASCString name
Definition: newgame.cpp:58
ASCString getName() const
returns the name of the player
Definition: player.cpp:274
sigc::signal< void, const ASCString & > filenameSelectedMouse
Definition: fileselector.h:99
Loki::Functor< GameMap *, LOKI_TYPELIST_1(const ASCString &) > MapLoadingFunction
Definition: loaders.h:57
bool eventKeyDown(const SDL_KeyboardEvent *key)
Definition: newgame.cpp:189
void warningMessage(const ASCString &str)
StartMultiplayerGame(PG_MessageObject *c)
Definition: newgame.cpp:221
static ASCString mechanismID()
void setUnencoded(const string &s)
Definition: password.cpp:27
static GameMap * loadmap(const ASCString &name)
Definition: loaders.cpp:1039
void setgameparameter(GameParameter num, int value)
Definition: gamemap.cpp:1058
A class that hols a MessageWindow. This class ensures that the window is removed if the last copy of ...
Definition: messaginghub.h:34
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
void sendGameParameterAsMail(GameMap *actmap)
Definition: gamedlg.cpp:481
void setEncoded(const string &s)
Definition: password.cpp:32
ASCString file
Definition: newgame.cpp:60
const int pageCount
Definition: newgame.cpp:54
struct GameMap::PreferredFilenames preferredFileNames
void next_turn(GameMap *gamemap, const NextTurnStrategy &nextTurnStrategy, MapDisplayInterface *display, int playerView)
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void SetText(const std::string &text)
sigc::signal< void, const ASCString & > filenameMarked
Definition: fileselector.h:101
ActionRecorder * actionRecorder
Definition: gamemap.h:391
bool startMultiplayerGame()
Definition: newgame.cpp:921
GameMap * mapLoadingExceptionChecker(const ASCString &filename, MapLoadingFunction loader)
Definition: loaders.cpp:1539
enum Player::PlayerStatus stat
const char * mapextension
Definition: loaders.cpp:79
virtual ASCString getMechanismID() const =0
Searching for files.
Definition: basestrm.h:484
ASCString name
Definition: basestrm.h:489
bool avail
is this a campaign map?
Definition: gamemap.h:221
const ASCString & getMessage(void) const
Definition: errors.h:42
sigc::signal< void > updateFieldInfo
Definition: spfst.cpp:47
void userHandler(const ASCString &label, PropertyReadingContainer &pc, PG_Widget *parent, WidgetParameters widgetParams)
Definition: newgame.cpp:692
void show(const ASCString &widgetName, PG_Widget *parent=NULL)
Definition: windowing.cpp:778
Player player[9]
Definition: gamemap.h:253
Parses a .ASCTXT file and returns a TextPropertyGroup.
void errorMessage(const ASCString &string)
static CGameOptions * Instance()
returns the only Instance
Definition: gameoptions.cpp:38
ASCString selectFile(const ASCString &ext, bool load, bool overwriteMessage)
Password supervisorpasswordcrc
Definition: gamemap.h:368
GameMap * actmap
Definition: spfst.cpp:64
GameTransferMechanism * network
Definition: gamemap.h:576
bool exist(const ASCString &s)
does a file s exist (wildcards allowed)
Definition: basestrm.cpp:2444
void hookGuiToMap(GameMap *map)
Definition: sg.cpp:287
void hide(const ASCString &widgetName, PG_Widget *parent=NULL)
Definition: windowing.cpp:768
int getFoundFileNum()
Definition: basestrm.h:546
void addString(const ASCString &name, ASCString &property)
void setServerAddress(const ASCString &address)
int getPlayerCount() const
Definition: gamemap.h:255
bool exist() const
does the player exist at all
Definition: player.cpp:313
ReplayInfo * replayinfo
Definition: gamemap.h:396
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
struct GameMap::Campaign campaign