initial commit, lordsawar source, slightly modified
[lordsawar] / src / pbm / pbm.cpp
1 // Copyright (C) 2008 Ben Asselstine
2 //
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.
7 //
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.
12 //
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 
16 //  02110-1301, USA.
17 #include "pbm.h"
18 #include "defs.h"
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"
25
26 pbm::pbm()
27 {
28 }
29
30 pbm::~pbm()
31 {
32 }
33
34 //turn all human players to networked
35 void pbm::turn_all_players_to_networked()
36 {
37   Playerlist *pl = Playerlist::getInstance();
38   for (Playerlist::iterator it = pl->begin(); it != pl->end(); it++)
39     {
40       if ((*it) == Playerlist::getInstance()->getNeutral())
41         continue;
42       if ((*it)->getType() != Player::HUMAN)
43         continue;
44       (*it)->setType(Player::NETWORKED);
45     }
46 }
47
48 void pbm::humanize_active_player()
49 {
50   Playerlist *pl = Playerlist::getInstance();
51   if (pl->getActiveplayer()->getType() == Player::NETWORKED)
52     pl->getActiveplayer()->setType(Player::HUMAN);
53 }
54
55 void pbm::playUntilFirstNetworkedPlayer(GameScenario *game_scenario)
56 {
57   //are we an ai player?
58   while (Playerlist::getActiveplayer()->isComputer())
59     {
60       NextTurnPbm *nextTurn;
61       nextTurn = new NextTurnPbm(game_scenario->getTurnmode(),
62                                  game_scenario->s_random_turns);
63       nextTurn->start();
64       delete nextTurn;
65     }
66 }
67
68 void pbm::init(std::string save_game_file)
69 {
70   bool broken = false;
71   GameScenario* game_scenario = new GameScenario(save_game_file, broken);
72   if (game_scenario == NULL)
73     return;
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);
80 }
81
82 void pbm::run(std::string save_game_file, std::string turn_file)
83 {
84   bool broken = false;
85   PbmGameClient *pbm_game_client = PbmGameClient::getInstance();
86   GameScenario* game_scenario = new GameScenario(save_game_file, broken);
87   if (game_scenario == NULL)
88     return;
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);
99   helper.close();
100   delete nextTurn;
101
102   turn_all_players_to_networked();
103   playUntilFirstNetworkedPlayer(game_scenario);
104   humanize_active_player();
105   d_player_name = Playerlist::getActiveplayer()->getName();
106   if (!broken)
107     {
108       game_scenario->saveGame(save_game_file);
109     }
110   pbm_game_client->deleteInstance();
111 }