1 // Copyright (C) 2000, 2001, 2002, 2003 Michael Bartl
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Ulf Lorenz
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
4 // Copyright (C) 2007 Ole Laursen
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Library General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #include <sigc++/trackable.h>
29 #include <sigc++/signal.h>
30 #include "game-parameters.h"
36 //! A list of all of the Player objects in a scenario.
38 * The Playerlist is implemented as a singleton class. The currently
39 * active player is designated, you can access players by name or id and the
40 * playerlist can check if there are more than one player remaining alive.
41 * This class also holds methods that affect all players.
44 class Playerlist : public std::list<Player*>, public sigc::trackable
48 //! The xml tag of this object in a saved-game file.
49 static std::string d_tag;
53 //! Set the winning human player.
54 void setWinningPlayer(Player *winner);
56 //! Set the player who's turn it is. should only be used by the editor.
57 void setActiveplayer(Player *p) {d_activeplayer = p;};
59 //! set the player who is looking at the bigmap and smallmap.
60 void setViewingplayer(Player *p) {viewingplayer = p;};
64 //! Returns the neutral player.
65 Player* getNeutral() const {return d_neutral;}
67 //! Return the player with the highest score.
68 Player *getWinningPlayer() const;
71 // Methods that operate on the class data and modify the class.
73 //! Sets the active player to the next player in the order.
77 * Checks if a player is alive and has no cities left. If not then
78 * this method marks the player as killed.
80 * @param Returns whether or not any players were marked as dead.
82 //! Kill any players that don't have cities left.
86 * Though the neutral player is located in the list of existing
87 * players, it is handled in a special way in many different cases.
89 * There can only be one neutral player per Playerlist.
91 * @param neutral The new neutral player.
93 //! Set the neutral player.
94 void setNeutral(Player* neutral) {d_neutral = neutral;}
97 * Swap out a player from the list and replace it with a new one.
98 * Specical care is taken to remove all references to the original
99 * player and replace it with a reference to the new player.
101 * The purpose of this method is to change a human player into a
102 * computer player and vice-versa.
104 * @param old_player A pointer to the player to replace.
105 * @param new_player A pointer to the new player to replace the
108 //! Replace a Player in the list with a new Player.
109 void swap(Player *old_player, Player *new_player);
112 * Erase a Player from the list, and free the contents of the Player.
114 * @param it The place in the Playerlist to erase.
116 * @return The place in the list that was erased.
118 //! Erase a player from the list.
119 iterator flErase(iterator it);
122 * This method is called when a round starts.
123 * The purpose of this method is to calculate who is winning, and
124 * it to negotiate diplomacy between players. This method also
125 * implements the computer players collectively surrendering to a
126 * final human player.
128 * @param diplomacy Whether or not we should negotiate diplomacy
130 * @param surrender_already_offered Tells the method if surrender
131 * has already been offered by the computer
132 * players. This needs to be kept track of
133 * because the computer players only offer
134 * surrender once. The method will change this
135 * value from false to true if it decided that
136 * the computer players collectively offer
139 //! Callback method to process all players at the start of a round.
140 void nextRound(bool diplomacy, bool *surrender_already_offered);
143 * The purpose of randomzing the Playerlist is to implement
145 * Note: This method does not set the active player.
147 //! Randomize the order of the players in the list.
148 void randomizeOrder();
151 * This method takes care of giving a player it's diplomatic
152 * ranking among all other players. The rank is determined by
153 * adding up all of the diplomatic scores, and then sorting them.
154 * Each rank has a title. There is always a Player who has the
155 * title of `Statesman', and there is always a Player who has the
156 * title of `Running Dog'. The other titles disappear as the other
159 //! Figure out who's winning diplomatically.
160 void calculateDiplomaticRankings();
162 //! Sync the playerlist.
164 * Sync the playerlist with the list of players given.
166 void syncPlayers(std::vector<GameParameters::Player> players);
168 //! Sync the given player with the playerlist
169 void syncPlayer(GameParameters::Player player);
171 //! Converts all of the human players into network players.
172 void turnHumansIntoNetworkPlayers();
174 //! Converts a given number of the human players into a type of player.
175 void turnHumansInto(Player::Type type, int num_players = -1);
177 //! Reorder the list according to the given order.
178 void reorder(std::list<guint32> order);
180 //! Perform the surrender of all computer players.
183 //! Add a player to the list. Use this instead of push_back.
184 void add(Player *player);
187 // Methods that operate on the class data but do not modify it.
190 * Scan the list of players for a Player with a given name.
192 * @param name The name of the Player to lookup.
194 * @return A pointer to the Player if it is found, or NULL if it isn't.
196 //! Lookup a Player by it's name.
197 Player* getPlayer(std::string name) const;
200 * Scan the list of players for a Player with a given Id.
202 * @param id The id of the Player to lookup.
204 * @return A pointer to the Player if it is found, or NULL if it isn't.
206 //! Lookup a Player by it's Id.
207 Player* getPlayer(guint32 id) const;
209 //! Returns the number of living players (neutral player excluded.)
210 guint32 getNoOfPlayers() const;
213 * Scan the list of players for the first Player that is alive.
214 * This method is used to determine the beginning of a round.
216 * @return A pointer to the first living Player.
218 //! Return the first living Player in the list.
219 Player* getFirstLiving() const;
221 //! Saves the playerlist to an opened saved-game file.
222 bool save(XML_Helper* helper) const;
224 //! Return the number of human players left alive in the list.
225 guint32 countHumanPlayersAlive() const;
227 //! Return the number of players left alive, not including neutral.
228 guint32 countPlayersAlive() const;
231 //! Return the list of activities that the given hero has accomplished.
232 std::list<History *>getHistoryForHeroId(guint32 id) const;
235 \brief Check to see if this is the end of the round or not.
237 bool isEndOfRound() const;
243 * @param player The player who has died.
245 //! Emitted when a player has died.
246 sigc::signal<void, Player*> splayerDead;
249 * Emitted when the computer players collectively offer surrender to
250 * a single remaining human player.
252 * @param player The human player who is being surrendered to.
254 //! Emitted when a surrender is offered.
255 sigc::signal<void, Player*> ssurrender;
260 //! Gets the singleton instance or creates a new one.
261 static Playerlist* getInstance();
264 * Load all Players in the Playerlist from a saved-game file.
266 * @param helper The opened saved-game file to read from.
268 * @return The loaded Playerlist.
270 //! Loads the playerlist from a saved-game file.
271 static Playerlist* getInstance(XML_Helper* helper);
273 //! Explicitly deletes the singleton instance.
274 static void deleteInstance();
276 //! Returns the active player (the Player whose turn it is).
277 static Player* getActiveplayer() {return d_activeplayer;}
279 //! Returns the viewing player (the Player who is looking at maps).
280 static Player *getViewingplayer() {return viewingplayer;}
283 int countFightsThisTurn() const;
284 int countMovesThisTurn() const;
287 //! Default constructor.
290 //! Loading constructor.
291 Playerlist(XML_Helper* helper);
297 //! Callback for loading the playerlist from an opened saved-game file.
298 bool load(std::string, XML_Helper* helper);
300 //! Comparison function to assist in sorting the list of players.
301 static bool randomly(const Player *lhs, const Player *rhs);
303 //! Comparison function to assist in sorting the list of players.
304 static bool inGivenOrder(const Player *lhs, const Player *rhs);
306 //! Comparison function to assist in sorting the list of players.
307 static bool inOrderOfId(const Player *lhs, const Player *rhs);
309 //! Calculate new scores for all players.
310 void calculateWinners();
312 //! Calculate new diplomatic states for all players.
313 void negotiateDiplomacy();
317 //! The pointer to the player whose turn it is in the list.
318 static Player* d_activeplayer;
320 //! The player that the smallmap and bigmap are being viewed as.
321 static Player *viewingplayer;
323 //! The pointer to the neutral player in the list.
326 typedef std::map<guint32, Player*> IdMap;
329 //! A static pointer for the singleton instance.
330 static Playerlist* s_instance;
334 #endif // PLAYERLIST_H