1
/* This file is part of KsirK.
2
Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
4
KsirK is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public
6
License as published by the Free Software Foundation, version 2.
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 GNU
11
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
/* begin : Mon Feb 07 2005 */
21
#include "ksirkgamexmlhandler.h"
22
#include "KsirkGlobalDefinitions.h"
23
#include "GameLogic/country.h"
24
#include "GameLogic/onu.h"
25
#include "GameLogic/goal.h"
26
#include "GameLogic/KMessageParts.h"
30
#include <kmessagebox.h>
34
using namespace GameLogic;
39
bool GameXmlHandler::startDocument()
41
kDebug() << "startDocument" << endl;
45
bool GameXmlHandler::startElement( const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts )
47
Q_UNUSED(namespaceURI);
49
kDebug() << "startElement " << localName << " / " << qName << endl;
50
if (localName == "ksirkSavedGame")
52
QString fv =atts.value("formatVersion");
53
QString wfv =SAVE_GAME_FILE_FORMAT_VERSION;
57
i18n("Wrong save game format. Waited %1 and got %2 !",QString(SAVE_GAME_FILE_FORMAT_VERSION),atts.value("formatVersion")),
58
i18n("KsirK - Cannot load !"));
63
else if (localName == "game")
65
kDebug() << "GameXmlHandler stored game state is: " << atts.value("state") << endl;
67
m_game.automaton()->skin(atts.value("skin"));
69
m_savedState = GameLogic::GameAutomaton::GameState(atts.value("state").toInt());
70
m_game.automaton()->savedState(m_savedState);
72
else if (localName == "players" && !m_inGoal)
74
int nb = atts.value("nb").toInt();
75
kDebug() << "Setting min-max players to " << nb << endl;
76
m_game.automaton()->setMinPlayers(nb);
77
m_game.automaton()->setMaxPlayers(nb);
79
else if (localName == "player" && !m_inGoal)
81
kDebug() << "Reading a player" << endl;
83
unsigned int nbAvailArmies = atts.value("nbAvailArmies").toInt();
85
unsigned int nbCountries = atts.value("nbCountries").toInt();
87
QString name = atts.value("name");
89
QString nationName = atts.value("nation");
91
unsigned int nbAttack = atts.value("nbAttack").toInt();
93
unsigned int nbDefense = atts.value("nbDefense").toInt();
96
if (atts.value("ai") == "true") isAi = true;
98
QString password = atts.value("password");
100
bool isLocal = true; // local player by default
101
if (atts.value("local") == "false") isLocal = false;
105
kDebug() << "Adding the read player " << name << endl;
106
m_game.addPlayer(name, nbAvailArmies, nbCountries, nationName,
107
isAi, password, nbAttack, nbDefense);
111
kDebug() << "Player" << name << "stored in matrix";
112
PlayerMatrix pm(m_game.automaton());
114
pm.nbAttack = nbAttack;
115
pm.nbCountries = nbCountries;
116
pm.nbAvailArmies = nbAvailArmies;
117
pm.nbDefense = nbDefense;
118
pm.nation = nationName;
119
pm.password = password;
121
foreach (const QString& k, m_ownersMap.keys())
123
if ( m_ownersMap[k] == name )
125
pm.countries.push_back(k);
128
m_waitedPlayers.push_back(pm);
131
else if (localName == "currentPlayer")
133
Player* currentPlayer = m_game.automaton()->playerNamed(atts.value("name"));
136
// kDebug() << "Setting current player to " << atts.value("name") << " / " << currentPlayer << endl;
137
m_game.automaton()->currentPlayer(currentPlayer);
138
KMessageParts messageParts;
139
messageParts << I18N_NOOP("Current player is: %1") << currentPlayer->name();
140
m_game.broadcastChangeItem(messageParts, ID_STATUS_MSG2);
142
QDataStream stream(&buffer, QIODevice::WriteOnly);
143
stream << currentPlayer->name();
144
m_game.automaton()->sendMessage(buffer,SetBarFlagButton);
146
m_game.automaton()->savedPlayer(atts.value("name"));
148
else if (localName == "ONU")
150
kDebug() << "GameXmlHandler starts new game with ONU file: " << atts.value("file") << endl;
151
if (!(m_game.automaton()->playerList()->isEmpty()))
153
m_game.automaton()->playerList()->clear();
154
m_game.automaton()->currentPlayer(0);
155
kDebug() << " playerList size = " << m_game.automaton()->playerList()->count() << endl;
157
m_game.automaton()->game()->newSkin(atts.value("file"));
159
else if (localName == "country")
161
// kDebug() << "GameXmlHandler loads country: " << atts.value("name") << endl;
162
Country* country = m_game.theWorld()->countryNamed(atts.value("name"));
163
unsigned int gotNbArmies = atts.value("nbArmies").toInt();
164
country->nbArmies(gotNbArmies);
166
kDebug() << "Storing" << atts.value("owner") << "as owner of" << atts.value("name");
167
m_ownersMap.insert(atts.value("name"), atts.value("owner"));
169
else if (localName == "goal")
171
kDebug() << "loads goal for: " << atts.value("player") << endl;
172
m_goal = new GameLogic::Goal(m_game.automaton());
173
m_goalPlayerName = atts.value("player");
174
Player* player = m_game.automaton()->playerNamed(atts.value("player").toUtf8().data());
175
// kDebug() << "Got player pointer " << player << endl;
176
m_goal->player(player);
177
unsigned int type = atts.value("type").toInt();
178
m_goal->type(GameLogic::Goal::GoalType(type));
179
m_goal->description(atts.value("description"));
180
unsigned int nbCountries = atts.value("nbCountries").toInt();
181
m_goal->nbCountries(nbCountries);
182
unsigned int nbArmiesByCountry = atts.value("nbArmiesByCountry").toInt();
183
m_goal->nbArmiesByCountry(nbArmiesByCountry);
187
else if (localName == "player" && m_inGoal)
189
m_goal->players().push_back(atts.value("name"));
191
else if (localName == "continent" && m_inGoal)
193
// kDebug() << "Getting id of continent named " << atts.value("name") << endl;
195
if (!atts.value("name").isEmpty())
196
id = atts.value("name");
197
m_goal->continents().push_back(id);
202
bool GameXmlHandler::endElement(const QString& namespaceURI, const QString& localName, const QString& qName)
204
Q_UNUSED(namespaceURI);
206
// kDebug() << "endElement " << localName << " / " << qName << endl;
207
if (localName == "game")
209
foreach (const QString& k, m_ownersMap.keys())
211
// kDebug() << "Setting owner of " << k << " to " << m_ownersMap[k] << endl;
212
Country* country = m_game.theWorld()->countryNamed(k);
213
Player* owner = m_game.automaton()->playerNamed(m_ownersMap[k]);
216
// kDebug() << "Setting owner of " << country->name() << " to " << owner->name() << endl;
217
country-> owner(owner);
221
// kDebug() << "Player" << m_ownersMap[k] << "not found";
222
QList<GameLogic::PlayerMatrix>::iterator itw,itw_end;
223
itw = m_waitedPlayers.begin(); itw_end = m_waitedPlayers.end();
224
for (; itw != itw_end; itw++)
226
if ( (*itw).name == m_ownersMap[k] )
228
(*itw).countries.push_back(k);
234
if (!m_waitedPlayers.empty())
236
// kDebug() << "There is waited players: does not change state nor run game..." << endl;
237
m_waitedPlayers[0].state = m_savedState;
241
// kDebug() << "GameXmlHandler set game state to: " << m_savedState << endl;
242
m_game.automaton()->state(m_savedState);
245
else if (localName == "goal")
250
if (m_goal->player())
252
m_goal->player()->goal(*m_goal);
256
QList<GameLogic::PlayerMatrix>::iterator itw,itw_end;
257
itw = m_waitedPlayers.begin(); itw_end = m_waitedPlayers.end();
258
for (; itw != itw_end; itw++)
260
if ( (*itw).name == m_goalPlayerName )
262
(*itw).goal = *m_goal;
275
} // closing namespace SaveLoad
276
} // closing namespace Ksirk