File indexing completed on 2024-04-28 07:53:33

0001 /***************************************************************************
0002                           onu.h  -  description
0003                              -------------------
0004     begin                : Wed Jul 18 2001
0005     copyright            : (C) 2001 by Gael de Chalendar
0006     email                : Gael.de.Chalendar@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 #ifndef ONU_H
0024 #define ONU_H
0025 
0026 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
0027 #include <libkdegamesprivate/kgamesvgdocument.h>
0028 
0029 #include <QDataStream>
0030 #include <QPixmap>
0031 #include <QFont>
0032 #include <QSvgRenderer>
0033 #include <QTimer>
0034 #include <QObject>
0035 #include "country.h"
0036 #include "continent.h"
0037 #include "nationality.h"
0038 #include "kgamewin.h"
0039 #include "gameautomaton.h"
0040 #include <QTextStream>
0041 
0042 namespace Ksirk
0043 {
0044 
0045 namespace GameLogic
0046 {
0047 
0048 class Country;
0049 
0050 /**
0051   * Class ONU (Organisation des Nations Unies = UNO : United Nations 
0052   * Organization) is simply the list of the countries. The data definining 
0053   * each country is loaded from an XML configuration file located in the
0054   * current skin data directory
0055   */
0056 class ONU: public QObject
0057 {
0058   Q_OBJECT
0059 
0060 public:
0061 
0062   /**
0063     * Constructor
0064     * @param configFileName The name of the XML file defining this world. Built
0065     * from the current skin dir and a default file name.
0066     */
0067   ONU(GameAutomaton* automaton, const QString& configFileName);
0068 
0069   /** Default destructor */
0070   ~ONU() override;
0071 
0072   /** Zoom methods: */
0073     void applyZoomFactor(qreal zoomFactor);
0074     void applyZoomFactorFast(qreal zoomFactor);     //benj
0075 
0076   //{@
0077   /**
0078    * Accessors
0079    */
0080   const QString& skin() const {return m_skin;}
0081   const QString& name() const {return m_name;}
0082   const QString& description() const {return m_description;}
0083   const QString& mapFileName() const;
0084   const QString& getConfigFileName() const {return m_configFileName;}
0085   const QPixmap& map() const {return m_map;}
0086   const QPixmap& snapshot() const {return m_snapshot;}
0087   unsigned int width() const;
0088   unsigned int height() const;
0089   //@}
0090   
0091   /**
0092     * This method returns a pointer to the country that contains the given 
0093     * point. If there is no country there, the functions returns 0.
0094     * @param point The point where to search for a country in the map mask
0095     * @return The country at the given point or 0 if there is no country there.
0096     */
0097   Country* countryAt(const QPointF& point);
0098 
0099   /**
0100     * Calls its reset method for each country
0101     */
0102   void reset();
0103 
0104   /**
0105     * Return the countries list
0106     */
0107   QList<Country*>& getCountries();
0108   
0109   /**
0110     * Returns the nationalities list
0111     */
0112   QList<Nationality*>& getNationalities();
0113 
0114   //@{
0115   /** Read property of QList<Continent*> continents. */
0116   QList<Continent*>& getContinents();
0117   const QList<Continent*>& getContinents() const;
0118   //@}
0119 
0120   /**
0121     * Retrieves the continent with the given id
0122     * @param id The id of the continent to retrieve
0123     * @return A pointer to the retrieved continent or 0 if there is no 
0124     * continent with the given id.
0125     */
0126 //   const Continent* continentWithId(const unsigned int id) const;
0127   
0128   /** 
0129     * Returns the list of countries neighbours of the parameter country that 
0130     * belongs to the argument player.
0131     * @param country The country whose neighbours have to be tested
0132     * @param player The countries that belong to this player will be retrieved
0133     * @return A vector of pointers on countries neighbour to the given country 
0134     * and belonging to the given player.
0135     */
0136   QList<Country*> neighboursBelongingTo(const Country& country, const Player* player);
0137 
0138   /** Returns the list of countries neighbours of the parameter country that 
0139     * does not belong to the argument player.
0140     * @param country The country whose neighbours have to be tested
0141     * @param player The countries that do not belong to this player will be 
0142     * retrieved
0143     * @return A vector of pointers on countries neighbour to the given country 
0144     * and not belonging to the given player.
0145     */
0146     QList<Country*> neighboursNotBelongingTo(const Country& country, const Player* player);
0147 
0148   /** 
0149     * Returns the country named "name" ; 0 in case there is no such country.
0150     * @param name The name of the country to retrieve.
0151     * @return The country named name or 0 if there is no such country.
0152     */
0153   Country* countryNamed(const QString& name);
0154 
0155   /** 
0156     * Gets the number of countries in the world
0157     * @return The number of countries in the world 
0158     */
0159   unsigned int getNbCountries() const;
0160 
0161   unsigned int indexOfCountry(Country*) const;
0162   
0163   /**
0164     * Saves a XML representation of the world for game saving purpose
0165     * @param xmlStream The stream to write on
0166     */
0167   void saveXml(QTextStream& xmlStream);
0168   
0169   /** 
0170     * Returns the nation named "name" ; 0 in case there is no such nation 
0171     * @param name The name of the nation to retrieve.
0172     * @return The nation named name or 0 if there is no such nation.
0173     */
0174   Nationality* nationNamed(const QString& name);
0175   
0176   /**
0177     * Transmit countries data to all network clients of the game. Called once
0178     * during finalization of network game start.
0179     * @param stream The stream to write countries data on
0180     */
0181   void sendCountries(QDataStream& stream);
0182   
0183   /** 
0184     * Returns the continent named "name" ; 0 in case there is no such continent 
0185     * @param name The name of the continent to retrieve.
0186     * @return The continent named name or 0 if there is no such continent.
0187     */
0188   Continent* continentNamed(const QString& name);
0189 
0190   double zoom() const;
0191 
0192   QSvgRenderer* renderer();
0193 
0194   KGameSvgDocument* svgDom();
0195 
0196   inline const QImage& mask() const {return countriesMask;}
0197 
0198   inline void setZoomArena(double newZoom) {m_zoomArena = newZoom;}
0199 
0200   inline GameAutomaton* automaton() {return m_automaton;}
0201 private:
0202   /**
0203     * All data that have to be stored about the font to display countries names
0204     * in this world's skin
0205     */
0206   struct FontDesc
0207   {
0208     QString family;
0209     int size;
0210     QFont::Weight weight;
0211     bool italic;
0212     QString foregroundColor;
0213     QString backgroundColor;
0214   };
0215 
0216   GameAutomaton* m_automaton;
0217   
0218   /**
0219     * The name of the XML file containing the world's definition
0220     */
0221   QString m_configFileName;
0222   
0223   /**
0224     * The displayable name of the skin
0225     */
0226   QString m_name;
0227   
0228   /**
0229     * The displayable long description of the skin
0230     */
0231   QString m_description;
0232   
0233   /**
0234     * The map used by this skin, built at the proper size from its SVG source 
0235     * and decorated with countries names 
0236     */
0237   QPixmap m_map;
0238   
0239   /**
0240     * A snaphsot of a running game with this skin. Used at skin choice time.
0241     */
0242   QPixmap m_snapshot;
0243   
0244   //@{
0245   /**
0246     * The width and height of the map file (will be used as canvas size). These
0247     * measures do not take into account the zoom factor.
0248     */
0249   unsigned int m_width;
0250   unsigned int m_height;
0251   //@}
0252 
0253   /**
0254     * The list of countries
0255     */
0256   QList<Country*> countries;
0257 
0258   /**
0259     * The list of nationalities
0260     */
0261   QList<Nationality*> nationalities;
0262 
0263   /**
0264     * The continents of the world
0265     */
0266   QList<Continent*> m_continents;
0267 
0268   /**
0269     * This image stores the mask that defines the countries of the world.
0270     * The blue RGB component value of each pixel gives the index of the
0271     * country in the countries list.
0272     */
0273   QImage countriesMask;
0274   
0275   /**
0276     * The path to the skin ; relative to the ksirk data dir ; loaded from the 
0277     * XML file
0278     */
0279   QString m_skin;
0280   
0281   /** 
0282     * The description of the font used to draw countries names onto the map.
0283     */
0284   FontDesc m_font;
0285   
0286   /** 
0287     * Zoom factor
0288     */
0289   double m_zoom;
0290   
0291   /** 
0292     * Zoom arena factor
0293     */
0294   double m_zoomArena;
0295 
0296   /**
0297     * Counter to know how many zooms have been asked by user 
0298     */
0299     int m_nbZooms;
0300 
0301   /**
0302     * Zoom used to change size of map
0303     */
0304     qreal m_zoomFactorFinal;
0305 
0306   /** 
0307     * Timer for the ZoomfactorFast
0308     */
0309     QTimer * m_timerFast;
0310     
0311   /**
0312     * This SVG renderer stores the SVG file of the map, renders it at the
0313     * desired zoom factor and the result is used to build the map image.
0314     */
0315 //   QSvgRenderer m_renderer;
0316 
0317 //   KGameSvgDocument m_svgDom;
0318 
0319   /**
0320    * Build the map from it's stored image and the countries names
0321    */
0322   void buildMap();
0323 
0324  /** SLOTS */
0325 public Q_SLOTS:
0326   void changingZoom();
0327 
0328 };
0329 
0330 }
0331 }
0332 #endif // ONU_H
0333