File indexing completed on 2024-12-01 03:48:16
0001 /* This file is part of KsirK. 0002 Copyright (C) 2001-2007 Gaƫl de Chalendar <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 0020 /* begin : Wed Jul 18 2001 */ 0021 0022 #ifndef PLAYER_H 0023 #define PLAYER_H 0024 0025 #include "KsirkGlobalDefinitions.h" 0026 #include "GameLogic/distributiondata.h" 0027 #include "GameLogic/gameautomaton.h" 0028 #include "GameLogic/goal.h" 0029 #include "nationality.h" 0030 #include "Sprites/animsprite.h" 0031 0032 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API 0033 #include <libkdegamesprivate/kgame/kplayer.h> 0034 #include <libkdegamesprivate/kgame/kgame.h> 0035 #include <libkdegamesprivate/kgame/kgameproperty.h> 0036 #include <libkdegamesprivate/kgame/kgamepropertyhandler.h> 0037 0038 #include <QString> 0039 #include <QMutex> 0040 0041 namespace Ksirk 0042 { 0043 0044 // class Goal; 0045 0046 namespace GameLogic 0047 { 0048 0049 class Nationality; 0050 0051 /** 0052 * The class Player represents a player and its associated data 0053 */ 0054 class Player : public KPlayer 0055 { 0056 Q_OBJECT 0057 public: 0058 0059 /** 0060 * Constructor with simple initializations 0061 */ 0062 Player(GameAutomaton* automaton, const QString &nomPlayer, unsigned int nbArmies, Nationality* myNation); 0063 0064 ~Player() override {} 0065 0066 /** 0067 * The idendification of the player. Overwrite this in 0068 * classes inherting KPlayer to run time identify them. 0069 * 0070 * @return 0 for default KPlayer. 0071 */ 0072 int rtti() const override {return 1;} 0073 0074 /** 0075 * comparison on the players' names 0076 */ 0077 virtual bool operator==(const Player& player) const; 0078 0079 //@{ 0080 /** 0081 * Accessors to the variables 0082 */ 0083 void setNbAvailArmies(unsigned int nb, bool transmit = true); 0084 unsigned int getNbAvailArmies(); 0085 void setNbAttack(unsigned int nb); 0086 unsigned int getNbAttack(); 0087 void setNbDefense(unsigned int nb); 0088 unsigned int getNbDefense(); 0089 void setNbCountries(unsigned int nb); 0090 unsigned int getNbCountries() const; 0091 //@} 0092 0093 //@{ 0094 /** 0095 * Add/Remove nb armies to the number of available armies (defaults to 1) 0096 */ 0097 void incrNbAvailArmies(unsigned int nb=1); 0098 void decrNbAvailArmies(unsigned int nb=1); 0099 //@} 0100 void putArmiesInto(int nb, int country); 0101 void removeArmiesFrom(int nb, int country); 0102 bool canRemoveArmiesFrom(int nb, int country); 0103 0104 //@{ 0105 /** 0106 * Add/Remove nb countries to the player (defaults to 1) 0107 */ 0108 void incrNbCountries(unsigned int nb=1); 0109 void decrNbCountries(unsigned int nb=1); 0110 //@} 0111 0112 /** Returns the flag associated to the nationality of the player */ 0113 const AnimSprite* getFlag() const; 0114 0115 /** Returns the file name of the flag sprite associated to the nationality of 0116 * the player */ 0117 const QString& flagFileName() const; 0118 0119 /** 0120 * This function is called whenever the player should choose an action 0121 * (attack, defence, etc.). If the player is human, this method do nothing 0122 * and so is empty. Its inherited version, in AIPlayer will have an activity 0123 */ 0124 virtual void actionChoice(GameLogic::GameAutomaton::GameState /*state*/) {} 0125 0126 /** 0127 * Returns false (a Player is not an AI) 0128 */ 0129 virtual bool isAI() const; 0130 0131 /** 0132 * Saves this player as XML. Used in game saving. 0133 * @param xmlStream The stream on which to write the XML 0134 */ 0135 virtual void saveXml(QTextStream& xmlStream); 0136 0137 //@{ 0138 /** Functions used to write and read data to and from a stream for network 0139 * transmission */ 0140 bool load (QDataStream &stream) override; 0141 bool save (QDataStream &stream) override; 0142 //@} 0143 0144 //@{ 0145 /** Accessors for this player's nationality */ 0146 Nationality* getNation(); 0147 void setNation(const QString& nationName); 0148 //@} 0149 0150 //@{ 0151 /** Accessors for this player's password */ 0152 inline void setPassword(const QString& password) {m_password = password;} 0153 inline const QString& getPassword() const {return m_password.value();} 0154 //@} 0155 0156 //@{ 0157 /** Accessors for this player's goal */ 0158 inline const Goal& goal() const {return m_goal;} 0159 inline Goal& goal() {return m_goal;} 0160 void goal(const Goal& goal); 0161 //@} 0162 0163 /** 0164 * Checks if this player's goal is reached or not. 0165 * @return true if the goal of this player is fulfilled, false otherwise 0166 */ 0167 bool checkGoal(); 0168 0169 /** 0170 * @return The list of the countries owned by this player 0171 */ 0172 QList<Country*> countries() const; 0173 0174 #define AUTHOR "Kleag" 0175 /** Method added during the porting of the AIColsonPlayer AI from XFrisk */ 0176 virtual QString author() {return AUTHOR;} 0177 0178 /** 0179 * Called once when all players are created/loaded/joined and when the 0180 * game can start. Allows to initialize AIs with public data about other 0181 * players. 0182 */ 0183 virtual void finalize() {} 0184 0185 /** 0186 * Called by the game automaton to acknowledge the reception of a message 0187 * by the master 0188 * @param ack an id to acknowledge if it is the waited one 0189 * @return true if the ack received was the waited one; false otherwise. 0190 */ 0191 bool acknowledge(const QString& ack); 0192 0193 void reset(); 0194 0195 protected: 0196 /** 0197 * Saving of private data 0198 * @param xmlStream The stream on which to write the XML 0199 */ 0200 void innerSaveXml(QTextStream& xmlStream); 0201 0202 GameAutomaton* m_automaton; 0203 /** 0204 * Number of armies used for an attack 0205 * ( 0 <> 3, < nbArmies of the country ) 0206 */ 0207 KGamePropertyUInt m_nbAttack; 0208 0209 /** 0210 * Number of countries owned by the player 0211 */ 0212 KGamePropertyUInt m_nbCountries; 0213 0214 /** 0215 * Number of armies the player can distribute 0216 */ 0217 // KGamePropertyUInt m_nbAvailArmies; 0218 // unsigned int m_nbAvailArmies; 0219 0220 /** 0221 * Number of armies used for defense 0222 * ( 0 <> 2, <= nbArmies used for the attack, < nbArmies of the country ) 0223 */ 0224 KGamePropertyUInt m_nbDefense; 0225 0226 /** 0227 * The nation chosen by the player 0228 */ 0229 Nationality *m_nation; 0230 0231 /** 0232 * The encrypted password of the player 0233 */ 0234 KGamePropertyQString m_password; 0235 0236 /** 0237 * The goal to be reached by this player 0238 */ 0239 Goal m_goal; 0240 0241 /** 0242 * Allows to initialize the nation name of the player even if the nation 0243 * object is not already initialized 0244 */ 0245 QString m_delayedInitNationName; 0246 0247 QString m_waitedAck; 0248 0249 QMutex m_waitedAckMutex; 0250 0251 private: 0252 void setFlag(); 0253 0254 AnimSprite* m_flag; 0255 0256 DistributionData m_distributionData; 0257 }; 0258 0259 typedef KGame::KGamePlayerList PlayersArray; 0260 0261 /** 0262 * A structure to store data concerning a player. Used during loading when the 0263 * actual player is waited from the network. 0264 */ 0265 struct PlayerMatrix 0266 { 0267 PlayerMatrix(GameLogic::GameAutomaton* automaton): goal(automaton){} 0268 0269 QString name; 0270 0271 unsigned int nbAttack; 0272 0273 unsigned int nbCountries; 0274 0275 unsigned int nbAvailArmies; 0276 0277 unsigned int nbDefense; 0278 0279 QString nation; 0280 0281 QString password; 0282 0283 QList<QString> countries; 0284 0285 GameLogic::GameAutomaton::GameState state; 0286 0287 bool isAI; 0288 0289 Goal goal; 0290 }; 0291 0292 QDataStream& operator<<(QDataStream& stream, PlayerMatrix& p); 0293 0294 QDataStream& operator>>(QDataStream& stream, PlayerMatrix& p); 0295 0296 } // closing namespace GameLogic 0297 0298 } // closing namespace Ksirk 0299 0300 #endif // PLAYER_H