1 // Copyright (C) 2008 Ben Asselstine
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include "GameScenario.h"
20 #include "pbm-game-client.h"
21 #include "playerlist.h"
22 #include "xmlhelper.h"
23 #include "NextTurnPbm.h"
24 #include "Configuration.h"
34 //turn all human players to networked
35 void pbm::turn_all_players_to_networked()
37 Playerlist *pl = Playerlist::getInstance();
38 for (Playerlist::iterator it = pl->begin(); it != pl->end(); it++)
40 if ((*it) == Playerlist::getInstance()->getNeutral())
42 if ((*it)->getType() != Player::HUMAN)
44 (*it)->setType(Player::NETWORKED);
48 void pbm::humanize_active_player()
50 Playerlist *pl = Playerlist::getInstance();
51 if (pl->getActiveplayer()->getType() == Player::NETWORKED)
52 pl->getActiveplayer()->setType(Player::HUMAN);
55 void pbm::playUntilFirstNetworkedPlayer(GameScenario *game_scenario)
57 //are we an ai player?
58 while (Playerlist::getActiveplayer()->isComputer())
60 NextTurnPbm *nextTurn;
61 nextTurn = new NextTurnPbm(game_scenario->getTurnmode(),
62 game_scenario->s_random_turns);
68 void pbm::init(std::string save_game_file)
71 GameScenario* game_scenario = new GameScenario(save_game_file, broken);
72 if (game_scenario == NULL)
74 game_scenario->setPlayMode(GameScenario::PLAY_BY_MAIL);
75 turn_all_players_to_networked();
76 playUntilFirstNetworkedPlayer(game_scenario);
77 humanize_active_player();
78 d_player_name = Playerlist::getActiveplayer()->getName();
79 broken = game_scenario->saveGame(save_game_file);
82 void pbm::run(std::string save_game_file, std::string turn_file)
85 PbmGameClient *pbm_game_client = PbmGameClient::getInstance();
86 GameScenario* game_scenario = new GameScenario(save_game_file, broken);
87 if (game_scenario == NULL)
89 Playerlist::getActiveplayer()->clearActionlist();
90 //now apply the actions in the turn file
91 //load the file, and decode them as we go.
92 XML_Helper helper(turn_file, std::ios::in,
93 Configuration::s_zipfiles);
94 NextTurnPbm *nextTurn;
95 nextTurn = new NextTurnPbm(game_scenario->getTurnmode(),
96 game_scenario->s_random_turns);
97 Player *player = Playerlist::getActiveplayer();
98 broken = pbm_game_client->loadWithHelper (helper, player);
102 turn_all_players_to_networked();
103 playUntilFirstNetworkedPlayer(game_scenario);
104 humanize_active_player();
105 d_player_name = Playerlist::getActiveplayer()->getName();
108 game_scenario->saveGame(save_game_file);
110 pbm_game_client->deleteInstance();