File indexing completed on 2024-05-05 04:05:12

0001 /* This file is part of KsirK.
0002  *   Copyright (C) 2005-2007 Gael de Chalendar (aka Kleag) <kleag@free.fr>
0003  *
0004  *   KsirK is free software; you can redistribute it and/or
0005  *   modify it under the terms of the GNU General Public
0006  *   License as published by the Free Software Foundation, either version 2
0007    of the License, or (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  *   General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the Free Software
0016  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017  *   02110-1301, USA
0018  */
0019 #include "goal.h"
0020 #include "onu.h"
0021 
0022 #include "ksirkskineditor_debug.h"
0023 #include <KLocalizedString>
0024 #include <KMessageBox>
0025 
0026 namespace KsirkSkinEditor
0027 {
0028 
0029 Goal::Goal() :
0030   m_type(NoGoal),
0031   m_description(QLatin1String("")),
0032   m_nbCountries(0),
0033   m_nbArmiesByCountry(0),
0034   m_continents(),
0035   m_players()
0036 {
0037 }
0038 
0039 Goal::Goal(const Goal& goal)
0040 {
0041   qCDebug(KSIRKSKINEDITOR_LOG) << "Goal copy constructor :";
0042   m_type = goal.m_type;
0043   qCDebug(KSIRKSKINEDITOR_LOG) << "  type="<< m_type ;
0044   m_description = goal.m_description;
0045   qCDebug(KSIRKSKINEDITOR_LOG) << "  description="<< m_description ;
0046   m_nbCountries = goal.m_nbCountries;
0047   qCDebug(KSIRKSKINEDITOR_LOG) << "  nbCountries="<< m_nbCountries ;
0048   m_nbArmiesByCountry = goal.m_nbArmiesByCountry;
0049   qCDebug(KSIRKSKINEDITOR_LOG) << "  nbArmiesByCountry="<< m_nbArmiesByCountry ;
0050   m_continents = goal.m_continents;
0051   qCDebug(KSIRKSKINEDITOR_LOG) << "  continents: "<< m_continents.size();
0052   m_players = goal.m_players;
0053   qCDebug(KSIRKSKINEDITOR_LOG) << "  players: "<< m_players.size();
0054 //   m_player = goal.m_player;
0055 //   qCDebug(KSIRKSKINEDITOR_LOG) << "  player: "<< m_player ;
0056 }
0057 
0058 Goal::~Goal()
0059 {
0060 }
0061 
0062 QString Goal::message(int displayType) const
0063 {
0064   qCDebug(KSIRKSKINEDITOR_LOG);
0065   KLocalizedString res;
0066 
0067 //   std::set<unsigned int>::const_iterator it, it_end, it_next;
0068   
0069   if(type()==NoGoal)
0070   {
0071     QString mes = (QString)QStringLiteral("You must conquer the World !");
0072     return mes;
0073   }
0074   else
0075   {
0076     if (displayType & GoalDesc)
0077     {
0078       res = ki18n(m_description.toUtf8().data());
0079 /*      if (m_player == 0)
0080       {
0081         res = res.subs("");
0082       }
0083       else
0084       {
0085         res = res.subs(m_player->name());
0086       }*/
0087       qCDebug(KSIRKSKINEDITOR_LOG) << "Goal type='" << m_type << "' mes = '" << res.toString() << "'";
0088     
0089       QList<unsigned int>::const_iterator it, it_end;
0090       switch (m_type)
0091       {
0092       case Goal::GoalPlayer :
0093         if (!m_players.empty())
0094         {
0095           qCDebug(KSIRKSKINEDITOR_LOG) << "  player name='" << *m_players.begin();
0096           res = res.subs(m_nbCountries);
0097         }
0098         else
0099         {
0100           res = res.subs(i18n("Error: no player to destroy"));
0101         }
0102         break;
0103       case Goal::Countries:
0104         qCDebug(KSIRKSKINEDITOR_LOG) << "  arg1 = '" << m_nbCountries << "'";
0105         res=res.subs(m_nbCountries);
0106         if (m_nbArmiesByCountry > 0)
0107         {
0108           qCDebug(KSIRKSKINEDITOR_LOG) << "  arg2 = '" << m_nbArmiesByCountry << "'";
0109           res=res.subs(m_nbArmiesByCountry);
0110         }
0111         break;
0112       case Goal::Continents:
0113 //         it = m_continents.begin(); it_end = m_continents.end();
0114 //         for (; it != it_end; it++)
0115 //         {
0116 //           if (*it != 0)
0117 //           {
0118 /*            qCDebug(KSIRKSKINEDITOR_LOG) << "  arg = '" << m_automaton->game()->theWorld()->continentWithId(*it)->name() << "'";
0119             res=res.subs(i18n(m_automaton->game()->theWorld()->continentWithId(*it)->name().toUtf8().data()));*/
0120 //           }
0121 //         }
0122         break;
0123       default:;
0124       }
0125     }
0126   }
0127 
0128   QString mes = res.toString();
0129   if (displayType & GoalAdvance)
0130   {
0131 //     int diff = 0;
0132     switch (m_type)
0133     {
0134     case Goal::GoalPlayer :
0135 /*      if (m_automaton->playerNamed(*m_players.begin()) != 0)
0136       {
0137         mes += i18n("<br>%1 is still alive...",*m_players.begin());
0138       }
0139       else
0140       {
0141         mes += i18n("<br>You now have to conquer %1",m_nbCountries);
0142       }*/
0143     break;
0144     case Goal::Countries:
0145 //       diff  = m_nbCountries-m_player->countries().size();
0146 //       if (diff > 0)
0147 //       {
0148 //         mes += i18n("<br>%1, you have still %2 countries to conquer...",m_player->name(),diff);
0149 //       }
0150 //       else
0151 //       {
0152 //         int nbOk = 0;
0153 //         for (unsigned int i = 0; i < m_player->countries().size();i++)
0154 //         {
0155 //           if (m_player->countries().at(i)->nbArmies() >= m_nbArmiesByCountry)
0156 //           {
0157 //             nbOk++;
0158 //           }
0159 //         }
0160 //         mes += i18n("<br>%1, you have enough countries but you still have to put more than %2 armies on %3 of them...",m_player->name(),m_nbArmiesByCountry,m_nbCountries-nbOk);
0161 //         
0162 //       }
0163       break;
0164     case Goal::Continents:
0165 /*      mes += i18n("<br>%1, you still have to conquer ",m_player->name());
0166       it = m_continents.begin(); it_end = m_continents.end();
0167       if (*it != 0)
0168       {
0169         Continent* continent = const_cast<Continent*>(m_automaton->game()->theWorld()->continentWithId(*it));
0170         int nb = continent->getMembers().size() - continent->countriesOwnedBy(m_player).size();
0171         mes += i18n("%1 countries in %2",nb,continent->name());
0172       }
0173       it++;
0174       while (it != it_end)
0175       {
0176         it_next = it;
0177         it_next++;
0178         QString joint;
0179         if (it_next==it_end)
0180         {
0181           joint = i18n(" and ");
0182         }
0183         else
0184         {
0185           joint = i18n(", ");
0186         }
0187         if (*it != 0)
0188         {
0189           Continent* continent = const_cast<Continent*>(m_automaton->game()->theWorld()->continentWithId(*it));
0190           int nb = continent->getMembers().size() - continent->countriesOwnedBy(m_player).size();
0191           mes += joint + i18nc("@info An element of the enumeration of the number of countries in the given continent", "%1 in %2",nb,continent->name());
0192         }
0193         it++;
0194       }
0195       mes += '.';*/
0196       break;
0197     default:
0198       break;
0199 ;
0200     }
0201   }
0202 
0203   return mes;
0204 }
0205   
0206 void Goal::show(int displayType)
0207 {
0208   qCDebug(KSIRKSKINEDITOR_LOG) << message(displayType);
0209 //   m_automaton->game()->showMessage(message(displayType),5, KGameWindow::ForceShowing);
0210   KMessageBox::information(
0211                             nullptr,
0212                             message(displayType), 
0213                             i18n("KsirK - Goal Display"));
0214 }
0215 
0216 QDataStream& operator<<(QDataStream& stream, const Goal& goal)
0217 {
0218   qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : type" << goal.type();
0219   stream << quint32(goal.type());
0220 /*  if (goal.player() != 0)
0221   {
0222     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : player " << goal.player()->id();
0223     stream << quint32(goal.player()->id());
0224   }
0225   else
0226   {
0227     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : player " << 0 ;
0228     stream << quint32(0);
0229   }*/
0230   qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : description " << goal.description();
0231   stream << goal.description();
0232   QList<QString>::ConstIterator it, it_end;
0233   QList<unsigned int>::const_iterator itc, itc_end;
0234   switch (goal.type())
0235   {
0236   case Goal::GoalPlayer :
0237     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : players " << goal.players().size();
0238     stream << quint32(goal.players().size());
0239     it = goal.players().constBegin(); it_end = goal.players().constEnd();
0240     for (; it != it_end; it++)
0241     {
0242       qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : player " << (*it);
0243       stream << *it;
0244     }
0245     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : nbCountries " << goal.nbCountries();
0246     stream << quint32(goal.nbCountries());
0247     break;
0248   case Goal::Countries:
0249     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : nbCountries " << goal.nbCountries();
0250     stream << quint32(goal.nbCountries());
0251     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : nbArmiesByCountry " << goal.nbArmiesByCountry();
0252     stream << quint32(goal.nbArmiesByCountry());
0253     break;
0254   case Goal::Continents:
0255     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : continents " << goal.continents().size();
0256     stream << quint32(goal.continents().size());
0257     it = goal.continents().begin(); it_end = goal.continents().end();
0258     for (; it != it_end; it++)
0259     {
0260       qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator<< : continent " << (*it);
0261       stream << (*it);
0262     }
0263     break;
0264   default: break;
0265   }
0266   return stream;
0267 }
0268 
0269 QDataStream& operator>>(QDataStream& stream, Goal& goal)
0270 {
0271   qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>>";
0272   quint32 type;
0273   QString description;
0274   QString playerName;
0275   quint32 nb, nbp;
0276   QString id;
0277   quint32 ownerId;
0278   stream >> type;
0279   qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> type: " << type ;
0280   stream >> ownerId;
0281   qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> ownerId: " << ownerId ;
0282   goal.setType(Goal::GoalType(type));
0283   stream >> description;
0284   qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> description: " << description ;
0285   goal.setDescription(description);
0286   switch (type)
0287   {
0288   case Goal::GoalPlayer :
0289     goal.players().clear();
0290     stream >> nbp;
0291     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> nbp: " << nbp ;
0292     for (quint32 i = 0; i < nbp; i++)
0293     {
0294       stream >> playerName;
0295       qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> player name: " << playerName ;
0296       goal.players().push_back(playerName);
0297     }
0298     stream >> nb;
0299     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> nbCountries: " << nb ;
0300     goal.setNbCountries(nb);
0301     break;
0302   case Goal::Countries:
0303     stream >> nb;
0304     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> nbCountries: " << nb ;
0305     goal.setNbCountries(nb);
0306     stream >> nb;
0307     goal.setNbArmiesByCountry(nb);
0308     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> nbArmiesByCountry: " << nb ;
0309     break;
0310   case Goal::Continents:
0311     stream >> nb;
0312     qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> nbContinents: " << nb ;
0313     goal.continents().clear();
0314     for (quint32 i = 0; i < nb; i++)
0315     {
0316       stream >> id;
0317       qCDebug(KSIRKSKINEDITOR_LOG) << "Goal operator>> continent: " << id ;
0318       goal.continents().push_back(id);
0319     }
0320     break;
0321   default:;
0322   }
0323   return stream;
0324 }
0325 
0326 void Goal::saveXml(std::ostream& xmlStream) const
0327 {
0328   
0329   xmlStream << "<goal player=\"";
0330 /*  if (m_player != 0)
0331   {
0332     xmlStream << m_player->name().toUtf8().data();
0333   }*/
0334   xmlStream << "\" type=\"" << m_type << "\" description=\"" << m_description.toUtf8().data();
0335   xmlStream << "\" nbCountries=\"" << m_nbCountries << "\" nbArmiesByCountry=\"" << m_nbArmiesByCountry << "\">\n";
0336   xmlStream << "<continents>\n";
0337   QList<QString>::const_iterator itc, itc_end;
0338   itc = continents().begin(); itc_end = continents().end();
0339   for (; itc != itc_end; itc++)
0340   {
0341 //     QString name = (*itc==0)?"":m_automaton->game()->theWorld()->continentWithId(*itc)->name();
0342 //     xmlStream << "<continent name=\"" << name.toUtf8().data() << "\"/>\n";
0343   }
0344   xmlStream << "</continents>\n";
0345   xmlStream << "<players>\n";
0346   QList<QString>::ConstIterator itp, itp_end;
0347   itp = m_players.constBegin(); itp_end = m_players.constEnd();
0348   for (; itp != itp_end; itp++)
0349   {
0350     xmlStream << "<player name=\"" << (*itp).toUtf8().data() << "\"/>\n";
0351   }
0352   xmlStream << "</players>\n";
0353   xmlStream << "</goal>\n";
0354   
0355     
0356 }
0357 
0358 }