~keith-penguin/kdegames/trunk

« back to all changes in this revision

Viewing changes to ksirk/ksirk/SaveLoad/ksirkgamexmlhandler.cpp

  • Committer: Keith Worrell
  • Date: 2009-03-18 05:35:28 UTC
  • Revision ID: keith.worrell@gmail.com-20090318053528-mx6x9c0ngmg0kg6p
imported project

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of KsirK.
 
2
   Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
 
3
 
 
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.
 
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 GNU
 
11
   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
*/
 
18
 
 
19
/*    begin                : Mon Feb 07 2005 */
 
20
 
 
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"
 
27
 
 
28
#include <kdebug.h>
 
29
#include <klocale.h>
 
30
#include <kmessagebox.h>
 
31
 
 
32
namespace Ksirk
 
33
{
 
34
using namespace GameLogic;
 
35
 
 
36
namespace SaveLoad
 
37
{
 
38
 
 
39
bool GameXmlHandler::startDocument()
 
40
 
41
  kDebug() << "startDocument" << endl;
 
42
  return true;
 
43
}
 
44
 
 
45
bool GameXmlHandler::startElement( const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts ) 
 
46
{
 
47
  Q_UNUSED(namespaceURI);
 
48
  Q_UNUSED(qName);
 
49
  kDebug() << "startElement " << localName << " / " << qName << endl;
 
50
  if (localName == "ksirkSavedGame")
 
51
  {
 
52
    QString fv =atts.value("formatVersion");
 
53
    QString wfv =SAVE_GAME_FILE_FORMAT_VERSION;
 
54
    if (fv!=wfv)
 
55
    {
 
56
      KMessageBox::sorry(0, 
 
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 !"));
 
59
 
 
60
      return false;
 
61
    }
 
62
  }
 
63
  else if (localName == "game")
 
64
  {
 
65
    kDebug() << "GameXmlHandler stored game state is: " << atts.value("state") << endl;
 
66
    
 
67
    m_game.automaton()->skin(atts.value("skin"));
 
68
    
 
69
    m_savedState = GameLogic::GameAutomaton::GameState(atts.value("state").toInt());
 
70
    m_game.automaton()->savedState(m_savedState);
 
71
  }
 
72
  else if (localName == "players" && !m_inGoal)
 
73
  {
 
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);
 
78
  }
 
79
  else if (localName == "player" && !m_inGoal)
 
80
  {
 
81
    kDebug() << "Reading a player" << endl;
 
82
    m_playersNumber++;
 
83
    unsigned int nbAvailArmies = atts.value("nbAvailArmies").toInt();
 
84
    
 
85
    unsigned int nbCountries = atts.value("nbCountries").toInt();
 
86
    
 
87
    QString name = atts.value("name");
 
88
    
 
89
    QString nationName = atts.value("nation");
 
90
    
 
91
    unsigned int nbAttack = atts.value("nbAttack").toInt();
 
92
    
 
93
    unsigned int nbDefense = atts.value("nbDefense").toInt();
 
94
    
 
95
    bool isAi = false;
 
96
    if (atts.value("ai") == "true") isAi = true;
 
97
    
 
98
    QString password = atts.value("password");
 
99
    
 
100
    bool isLocal = true; // local player by default
 
101
    if (atts.value("local") == "false") isLocal = false;
 
102
 
 
103
    if (isLocal)
 
104
    {
 
105
      kDebug() << "Adding the read player " << name << endl;
 
106
      m_game.addPlayer(name, nbAvailArmies, nbCountries, nationName,
 
107
                        isAi, password, nbAttack, nbDefense);
 
108
    }
 
109
    else
 
110
    {
 
111
      kDebug() << "Player" << name << "stored in matrix";
 
112
      PlayerMatrix pm(m_game.automaton());
 
113
      pm.name = name;
 
114
      pm.nbAttack = nbAttack;
 
115
      pm.nbCountries = nbCountries;
 
116
      pm.nbAvailArmies = nbAvailArmies;
 
117
      pm.nbDefense = nbDefense;
 
118
      pm.nation = nationName;
 
119
      pm.password = password;
 
120
      pm.isAI = isAi;
 
121
      foreach (const QString& k, m_ownersMap.keys())
 
122
      {
 
123
        if ( m_ownersMap[k] == name )
 
124
        {
 
125
          pm.countries.push_back(k);
 
126
        }
 
127
      }
 
128
      m_waitedPlayers.push_back(pm);
 
129
    }
 
130
  }
 
131
  else if (localName == "currentPlayer")
 
132
  {
 
133
    Player* currentPlayer = m_game.automaton()->playerNamed(atts.value("name"));
 
134
    if (currentPlayer)
 
135
    {
 
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);
 
141
      QByteArray buffer;
 
142
      QDataStream stream(&buffer, QIODevice::WriteOnly);
 
143
      stream << currentPlayer->name();
 
144
      m_game.automaton()->sendMessage(buffer,SetBarFlagButton);
 
145
    }
 
146
    m_game.automaton()->savedPlayer(atts.value("name"));
 
147
  }
 
148
  else if (localName == "ONU")
 
149
  {
 
150
    kDebug() << "GameXmlHandler starts new game with ONU file: " << atts.value("file") << endl;
 
151
    if (!(m_game.automaton()->playerList()->isEmpty()))
 
152
    {
 
153
      m_game.automaton()->playerList()->clear();
 
154
      m_game.automaton()->currentPlayer(0);
 
155
      kDebug() << "  playerList size = " << m_game.automaton()->playerList()->count() << endl;
 
156
    }
 
157
    m_game.automaton()->game()->newSkin(atts.value("file"));
 
158
  }
 
159
  else if (localName == "country")
 
160
  {
 
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);
 
165
    
 
166
    kDebug() << "Storing" << atts.value("owner") << "as owner of" << atts.value("name");
 
167
    m_ownersMap.insert(atts.value("name"), atts.value("owner"));
 
168
  }
 
169
  else if (localName == "goal")
 
170
  {
 
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);
 
184
    
 
185
    m_inGoal = true;
 
186
  }
 
187
  else if (localName == "player" && m_inGoal)
 
188
  {
 
189
    m_goal->players().push_back(atts.value("name"));
 
190
  }
 
191
  else if (localName == "continent" && m_inGoal)
 
192
  {
 
193
//     kDebug() << "Getting id of continent named " << atts.value("name") << endl;
 
194
    QString id;
 
195
    if (!atts.value("name").isEmpty())
 
196
        id = atts.value("name");
 
197
    m_goal->continents().push_back(id);
 
198
  }
 
199
  return true;
 
200
}
 
201
 
 
202
bool GameXmlHandler::endElement(const QString& namespaceURI, const QString& localName, const QString& qName)
 
203
{
 
204
  Q_UNUSED(namespaceURI);
 
205
  Q_UNUSED(qName);
 
206
//   kDebug() << "endElement " << localName << " / " << qName << endl;
 
207
  if (localName == "game")
 
208
  {
 
209
    foreach (const QString& k, m_ownersMap.keys())
 
210
    {
 
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]);
 
214
      if (owner)
 
215
      {
 
216
//         kDebug() << "Setting owner of " << country->name() << " to " << owner->name() << endl;
 
217
        country-> owner(owner);
 
218
      }
 
219
      else
 
220
      {
 
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++)
 
225
        {
 
226
          if ( (*itw).name == m_ownersMap[k] )
 
227
          {
 
228
            (*itw).countries.push_back(k);
 
229
            break;
 
230
          }
 
231
        }
 
232
      }
 
233
    }
 
234
    if (!m_waitedPlayers.empty())
 
235
    {
 
236
//       kDebug() << "There is waited players: does not change state nor run game..." << endl;
 
237
      m_waitedPlayers[0].state = m_savedState;
 
238
    }
 
239
    else
 
240
    {
 
241
//       kDebug() << "GameXmlHandler set game state to: " << m_savedState << endl;
 
242
      m_game.automaton()->state(m_savedState);
 
243
    }
 
244
  }
 
245
  else if (localName == "goal")
 
246
  {
 
247
    m_inGoal = false;
 
248
    if (m_goal)
 
249
    {
 
250
      if (m_goal->player())
 
251
      {
 
252
        m_goal->player()->goal(*m_goal);
 
253
      }
 
254
      else
 
255
      {
 
256
        QList<GameLogic::PlayerMatrix>::iterator itw,itw_end;
 
257
        itw = m_waitedPlayers.begin(); itw_end = m_waitedPlayers.end();
 
258
        for (; itw != itw_end; itw++)
 
259
        {
 
260
          if ( (*itw).name == m_goalPlayerName )
 
261
          {
 
262
            (*itw).goal = *m_goal;
 
263
            break;
 
264
          }
 
265
        }
 
266
      }
 
267
      delete m_goal;
 
268
    }
 
269
    m_goal = 0;
 
270
  }
 
271
  return true;
 
272
}
 
273
 
 
274
 
 
275
} // closing namespace SaveLoad
 
276
} // closing namespace Ksirk
 
277
 
 
278