File indexing completed on 2024-09-29 06:39:18

0001 /***************************************************************************
0002                           continent.cpp  -  description
0003                              -------------------
0004     begin                : sam sep 7 2002
0005     copyright            : (C) 2002 by Gael de Chalendar
0006     email                : kleag@free.fr
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either either version 2
0014    of the License, or (at your option) any later version.of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  *   You should have received a copy of the GNU General Public License
0018  *   along with this program; if not, write to the Free Software
0019  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0020  *   02110-1301, USA
0021  ***************************************************************************/
0022 
0023 #include "continent.h"
0024 
0025 #include "player.h"
0026 
0027 namespace Ksirk
0028 {
0029 
0030 namespace GameLogic
0031 {
0032 
0033 /** The constructor-initializer */
0034 Continent::Continent (const QString &myName, const QList<Country*>& myCountries, const int myBonus) :
0035   m_members(myCountries), m_name(myName), bonus(myBonus)
0036 {
0037   foreach (Country* c, myCountries)
0038   {
0039     if (c)
0040     {
0041       c->setContinent(this);
0042     }
0043   }
0044 }
0045 
0046 Continent::~Continent()
0047 {
0048 }
0049 
0050 /** Read property of QList<Country> m_members. */
0051 const QList<Country*>& Continent::getMembers() const
0052 {
0053 //   qCDebug(KSIRK_LOG) << "There is " << m_members.size() << " countries in " << name();
0054   return m_members;
0055 }
0056 
0057 /** Read property of QString name. */
0058 const QString& Continent::name() const
0059 {
0060     return m_name;
0061 }
0062 
0063 /** Read property of int bonus. */
0064 const int& Continent::getBonus() const
0065 {
0066     return bonus;
0067 }
0068 
0069 /**
0070   * Returns the player that owns all the countries of this continent. 0 if none
0071   */
0072 const Player* Continent::owner() const
0073 {
0074    qCDebug(KSIRK_LOG) << "Continent::owner for "  << m_name;
0075     /** The owner of the first country is the owner if there is any one*/
0076   QList<Country*>::const_iterator it = m_members.constBegin();
0077   const Country* firstOne = *(it);
0078   const Player* owner = firstOne-> owner();
0079   qCDebug(KSIRK_LOG) << "\t"  << firstOne-> name()  << " is owned by "  << owner-> name();
0080 
0081   for (it++; it != m_members.end(); it++)
0082   {
0083     qCDebug(KSIRK_LOG) << "\t"  << (*it)-> name() << " is owned by "  << (*it)-> owner()-> name();
0084     /** if the owner of the current country is not the owner of th first
0085       * one, then there is two different owners and the function should
0086       * return 0
0087       */
0088     if ((*it)-> owner() != owner)
0089     {
0090       qCDebug(KSIRK_LOG) << "Nobody owns " << m_name ;
0091       return nullptr;
0092     }
0093   }
0094   /** There is only one owner for all the countries ; lets return it */
0095   qCDebug(KSIRK_LOG) << "The owner of " << m_name << " is "  << owner-> name();
0096   return owner;
0097 }
0098 
0099 void Continent::saveXml(QTextStream& xmlStream)
0100 {
0101   QString name = m_name;
0102   name = name.replace("&","&amp;");
0103   name = name.replace("<","&lt;");
0104   name = name.replace(">","&gt;");
0105   xmlStream << "<continent name=\""<<name<<"\" bonus=\""<<bonus<<"\" >";
0106   QList< Country* >::const_iterator it, it_end;
0107   it = m_members.constBegin(); it_end = m_members.constEnd();
0108   for (; it != it_end; it++)
0109   {
0110     (*it)->saveXml(xmlStream);
0111   }
0112 
0113   xmlStream << "</continent>";
0114 }
0115 
0116 /** Returns the list of countries owned by @ref player */
0117 QList<Country*> Continent::countriesOwnedBy(const Player* player)
0118 {
0119   QList<Country*> res;
0120   foreach (Country*c, m_members)
0121   {    
0122     if ( c->owner() == player )
0123     {
0124       res.push_back(c);
0125     }
0126   }
0127   qCDebug(KSIRK_LOG) << player->name() << " owns " << res.size() << " in " << name();
0128   return res;
0129 }
0130 
0131 } // closing namespace GameLogic
0132 } // closing namespace Ksirk