11
16
// You should have received a copy of the GNU General Public License
12
17
// along with this program; if not, write to the Free Software
13
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
21
#ifndef PLAYERLIST_H
16
22
#define PLAYERLIST_H
20
27
#include <sigc++/trackable.h>
21
28
#include <sigc++/signal.h>
29
#include "game-parameters.h"
23
31
#include "player.h"
25
/** List of all players of a scenario
27
* The playerlist is also implemented as a singleton function. The currently
28
* active player is designated, you can access players by name or id and the
29
* playerlist can check if there are more than one player remaining alive.
33
//! A list of all of the Player objects in a scenario.
35
* The Playerlist is implemented as a singleton class. The currently
36
* active player is designated, you can access players by name or id and the
37
* playerlist can check if there are more than one player remaining alive.
38
* This class also holds methods that affect all players.
32
41
class Playerlist : public std::list<Player*>, public sigc::trackable
36
//! Gets the singleton instance or creates a new one
45
//! Gets the singleton instance or creates a new one.
37
46
static Playerlist* getInstance();
39
//! Loads the playerlist (including all players)
49
* Load all Players in the Playerlist from a saved-game file.
51
* @param helper The opened saved-game file to read from.
53
* @return The loaded Playerlist.
55
//! Loads the playerlist from a saved-game file.
40
56
static Playerlist* getInstance(XML_Helper* helper);
42
//! Explicitely deletes the singleton instance
58
//! Explicitly deletes the singleton instance.
43
59
static void deleteInstance();
45
//! Returns the active player of the singleton instance
61
//! Returns the active player (the Player whose turn it is).
46
62
static Player* getActiveplayer() {return d_activeplayer;}
48
//! Returns whether the game has been finished. I didn't know a better place...
64
//! Returns whether all of the players are finished playing the game.
49
65
static bool isFinished() {return s_finish;}
51
//! set the game to a finished state
67
//! Set whether or not all players are finished the game.
52
68
static void finish() {s_finish = true;}
54
//! Sets the active player to the next player in the order
70
//! Sets the active player to the next player in the order.
57
/** Checks if a player has died (==doesn't own a city). If so, it
58
* marks the player as killed, so he is ignored the next time.
59
* Returns whether or not any players were marked as dead.
74
* Checks if a player is alive and has no cities left. If not then
75
* this method marks the player as killed.
77
* @param Returns whether or not any players were marked as dead.
79
//! Kill any players that don't have cities left.
61
80
bool checkPlayers();
63
/** Sets the neutral player. Though the neutral player is located in the
64
* list of existing players, it is handled in a special way. His colors
65
* are e.g. used for stacks not owned by a player (such as monsters
66
* infesting ruins), which is why we keep track of the neutral player
67
* with a special pointer.
69
* @param neutral the new neutral player
83
* Though the neutral player is located in the list of existing
84
* players, it is handled in a special way in many different cases.
86
* There can only be one neutral player per Playerlist.
88
* @param neutral The new neutral player.
90
//! Set the neutral player.
71
91
void setNeutral(Player* neutral) {d_neutral = neutral;}
73
//! Returns the neutral player
93
//! Returns the neutral player.
74
94
Player* getNeutral() const {return d_neutral;}
76
//! Returns player named "name" or none if not existent
97
* Scan the list of players for a Player with a given name.
99
* @param name The name of the Player to lookup.
101
* @return A pointer to the Player if it is found, or NULL if it isn't.
103
//! Lookup a Player by it's name.
77
104
Player* getPlayer(std::string name) const;
79
//! Returns player with the specified id or none if not existent
107
* Scan the list of players for a Player with a given Id.
109
* @param id The id of the Player to lookup.
111
* @return A pointer to the Player if it is found, or NULL if it isn't.
113
//! Lookup a Player by it's Id.
80
114
Player* getPlayer(Uint32 id) const;
82
//! Returns the number of living players (neutral player excluded)
83
int getNoOfPlayers() const;
116
//! Returns the number of living players (neutral player excluded.)
117
Uint32 getNoOfPlayers() const;
85
/** Returns the first player alive (needed to determine the begin of
120
* Scan the list of players for the first Player that is alive.
121
* This method is used to determine the beginning of a round.
123
* @return A pointer to the first living Player.
125
//! Return the first living Player in the list.
88
126
Player* getFirstLiving() const;
91
/** Replace old_player with new_player everywhere.
129
* Swap out a player from the list and replace it with a new one.
130
* Specical care is taken to remove all references to the original
131
* player and replace it with a reference to the new player.
133
* The purpose of this method is to change a human player into a
134
* computer player and vice-versa.
136
* @param old_player A pointer to the player to replace.
137
* @param new_player A pointer to the new player to replace the
140
//! Replace a Player in the list with a new Player.
93
141
void swap(Player *old_player, Player *new_player);
95
//! Saves the playerlist. See XML_Helper for further docu.
143
//! Saves the playerlist to an opened saved-game file.
96
144
bool save(XML_Helper* helper) const;
98
/** Erase an item. This needs to be implemented, because we have to free
99
* the pointer. Else it behaves like the STL erase member functions.
147
* Erase a Player from the list, and free the contents of the Player.
149
* @param it The place in the Playerlist to erase.
151
* @return The place in the list that was erased.
153
//! Erase a player from the list.
101
154
iterator flErase(iterator it);
103
//! This signal is emitted when a player has died.
104
sigc::signal<void, Player*> splayerDead;
106
//! This signal is emitted when a surrender is offered
107
sigc::signal<void, Player*> ssurrender;
157
* This method is called when a round starts.
158
* The purpose of this method is to calculate who is winning, and
159
* it to negotiate diplomacy between players. This method also
160
* implements the computer players collectively surrendering to a
161
* final human player.
163
* @param diplomacy Whether or not we should negotiate diplomacy
165
* @param surrender_already_offered Tells the method if surrender
166
* has already been offered by the computer
167
* players. This needs to be kept track of
168
* because the computer players only offer
169
* surrender once. The method will change this
170
* value from false to true if it decided that
171
* the computer players collectively offer
174
//! Callback method to process all players at the start of a round.
109
175
void nextRound(bool diplomacy, bool *surrender_already_offered);
177
//! Return the number of human players left alive in the list.
111
178
Uint32 countHumanPlayersAlive();
112
Uint32 countPlayersAlive(); //does not include neutral player
180
//! Return the number of players left alive, not including neutral.
181
Uint32 countPlayersAlive();
184
* The purpose of randomzing the Playerlist is to implement
187
//! Randomize the order of the players in the list.
114
188
void randomizeOrder();
191
* This method takes care of giving a player it's diplomatic
192
* ranking among all other players. The rank is determined by
193
* adding up all of the diplomatic scores, and then sorting them.
194
* Each rank has a title. There is always a Player who has the
195
* title of `Statesman', and there is always a Player who has the
196
* title of `Running Dog'. The other titles disappear as the other
199
//! Figure out who's winning diplomatically.
116
200
void calculateDiplomaticRankings();
202
//! Sync the playerlist.
204
* Sync the playerlist with the list of players given.
206
void syncPlayers(std::vector<GameParameters::Player> players);
208
//! Sync the given player with the playerlist
209
void syncPlayer(GameParameters::Player player);
212
* @param player The player who has died.
214
//! Emitted when a player has died.
215
sigc::signal<void, Player*> splayerDead;
218
* Emitted when the computer players collectively offer surrender to
219
* a single remaining human player.
221
* @param player The human player who is being surrendered to.
223
//! Emitted when a surrender is offered.
224
sigc::signal<void, Player*> ssurrender;
227
//! Default constructor.
229
//! Loading constructor.
121
230
Playerlist(XML_Helper* helper);
125
//! Callback for loading the playerlist.
235
//! Callback for loading the playerlist from an opened saved-game file.
126
236
bool load(std::string, XML_Helper* helper);
238
//! Comparison function to assist in sorting the list of players.
128
239
static bool randomly(const Player *lhs, const Player *rhs);
130
//! end of round stuff
241
//! Comparison function to assist in sorting the list of players.
242
static bool inOrderOfId(const Player *lhs, const Player *rhs);
244
//! Calculate new scores for all players.
131
245
void calculateWinners();
247
//! Calculate new diplomatic states for all players.
132
248
void negotiateDiplomacy();
134
//static pointers for the singleton
135
static Playerlist* s_instance;
251
//! The pointer to the player whose turn it is in the list.
138
252
static Player* d_activeplayer;
254
//! Whether or not the game is over.
139
255
static bool s_finish;
257
//! The pointer to the neutral player in the list.
140
258
Player* d_neutral;
260
//! A static pointer for the singleton instance.
261
static Playerlist* s_instance;
143
265
#endif // PLAYERLIST_H