~ubuntu-branches/ubuntu/trusty/lordsawar/trusty

« back to all changes in this revision

Viewing changes to src/playerlist.h

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2008-06-17 16:07:00 UTC
  • mto: (5.1.1 lenny) (1.1.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20080617160700-6d8ofoz0qkasxlnw
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2000, 2001, 2002, 2003 Michael Bartl
 
2
// Copyright (C) 2001, 2002, 2003, 2004, 2005 Ulf Lorenz
 
3
// Copyright (C) 2007, 2008 Ben Asselstine
 
4
// Copyright (C) 2007 Ole Laursen
 
5
//
1
6
//  This program is free software; you can redistribute it and/or modify
2
7
//  it under the terms of the GNU General Public License as published by
3
8
//  the Free Software Foundation; either version 2 of the License, or
10
15
//
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 
 
19
//  02110-1301, USA.
14
20
 
15
21
#ifndef PLAYERLIST_H
16
22
#define PLAYERLIST_H
17
23
 
18
24
#include <list>
19
25
#include <string>
 
26
#include <vector>
20
27
#include <sigc++/trackable.h>
21
28
#include <sigc++/signal.h>
 
29
#include "game-parameters.h"
22
30
 
23
31
#include "player.h"
24
32
 
25
 
/** List of all players of a scenario
26
 
  * 
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.
30
 
  */
 
33
//! A list of all of the Player objects in a scenario.
 
34
/** 
 
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.
 
39
 */
31
40
 
32
41
class Playerlist : public std::list<Player*>, public sigc::trackable
33
42
{
34
43
    public:
35
44
 
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();
38
47
 
39
 
        //! Loads the playerlist (including all players)
 
48
        /**
 
49
         * Load all Players in the Playerlist from a saved-game file.
 
50
         *
 
51
         * @param helper     The opened saved-game file to read from.
 
52
         *
 
53
         * @return The loaded Playerlist.
 
54
         */
 
55
        //! Loads the playerlist from a saved-game file.
40
56
        static Playerlist* getInstance(XML_Helper* helper);
41
57
 
42
 
        //! Explicitely deletes the singleton instance
 
58
        //! Explicitly deletes the singleton instance.
43
59
        static void deleteInstance();
44
60
 
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;}
47
63
        
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;}
50
66
 
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;}
53
69
        
54
 
        //! Sets the active player to the next player in the order
 
70
        //! Sets the active player to the next player in the order.
55
71
        void nextPlayer();
56
72
 
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.
60
 
          */
 
73
        /** 
 
74
         * Checks if a player is alive and has no cities left. If not then 
 
75
         * this method marks the player as killed.
 
76
         *
 
77
         * @param Returns whether or not any players were marked as dead.
 
78
         */
 
79
        //! Kill any players that don't have cities left.
61
80
        bool checkPlayers();
62
81
 
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.
68
 
          *
69
 
          * @param neutral          the new neutral player
70
 
          */
 
82
        /** 
 
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.
 
85
         *
 
86
         * There can only be one neutral player per Playerlist.
 
87
         *
 
88
         * @param neutral          The new neutral player.
 
89
         */
 
90
        //! Set the neutral player.
71
91
        void setNeutral(Player* neutral) {d_neutral = neutral;}
72
92
 
73
 
        //! Returns the neutral player
 
93
        //! Returns the neutral player.
74
94
        Player* getNeutral() const {return d_neutral;}
75
95
 
76
 
        //! Returns player named "name" or none if not existent
 
96
        /**
 
97
         * Scan the list of players for a Player with a given name.
 
98
         *
 
99
         * @param name      The name of the Player to lookup.
 
100
         *
 
101
         * @return A pointer to the Player if it is found, or NULL if it isn't.
 
102
         */
 
103
        //! Lookup a Player by it's name.
77
104
        Player* getPlayer(std::string name) const;
78
105
 
79
 
        //! Returns player with the specified id or none if not existent
 
106
        /**
 
107
         * Scan the list of players for a Player with a given Id.
 
108
         *
 
109
         * @param id        The id of the Player to lookup.
 
110
         *
 
111
         * @return A pointer to the Player if it is found, or NULL if it isn't.
 
112
         */
 
113
        //! Lookup a Player by it's Id.
80
114
        Player* getPlayer(Uint32 id) const;
81
115
 
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;
84
118
 
85
 
        /** Returns the first player alive (needed to determine the begin of
86
 
          * a new round).
87
 
          */
 
119
        /** 
 
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.
 
122
         *
 
123
         * @return A pointer to the first living Player.
 
124
         */
 
125
        //! Return the first living Player in the list.
88
126
        Player* getFirstLiving() const;
89
 
        
90
127
 
91
 
        /** Replace old_player with new_player everywhere.
 
128
        /** 
 
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.
 
132
         *
 
133
         * The purpose of this method is to change a human player into a
 
134
         * computer player and vice-versa.
 
135
         *
 
136
         * @param old_player   A pointer to the player to replace.
 
137
         * @param new_player   A pointer to the new player to replace the 
 
138
         *                     original player.
92
139
         */
 
140
        //! Replace a Player in the list with a new Player.
93
141
        void swap(Player *old_player, Player *new_player);
94
142
        
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;
97
145
 
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.
100
 
          */
 
146
        /** 
 
147
         * Erase a Player from the list, and free the contents of the Player.
 
148
         *
 
149
         * @param it   The place in the Playerlist to erase.
 
150
         *
 
151
         * @return The place in the list that was erased.
 
152
         */
 
153
        //! Erase a player from the list.
101
154
        iterator flErase(iterator it);
102
155
 
103
 
        //! This signal is emitted when a player has died.
104
 
        sigc::signal<void, Player*> splayerDead;
105
 
    
106
 
        //! This signal is emitted when a surrender is offered
107
 
        sigc::signal<void, Player*> ssurrender;
108
 
    
 
156
        /**
 
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.
 
162
         *
 
163
         * @param diplomacy     Whether or not we should negotiate diplomacy
 
164
         *                      between players.
 
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 
 
172
         *                      surrender.
 
173
         */
 
174
        //! Callback method to process all players at the start of a round.
109
175
        void nextRound(bool diplomacy, bool *surrender_already_offered);
110
176
 
 
177
        //! Return the number of human players left alive in the list.
111
178
        Uint32 countHumanPlayersAlive();
112
 
        Uint32 countPlayersAlive(); //does not include neutral player
113
 
 
 
179
 
 
180
        //! Return the number of players left alive, not including neutral.
 
181
        Uint32 countPlayersAlive();
 
182
 
 
183
        /**
 
184
         * The purpose of randomzing the Playerlist is to implement
 
185
         * random turns.
 
186
         */
 
187
        //! Randomize the order of the players in the list.
114
188
        void randomizeOrder();
115
189
 
 
190
        /**
 
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 
 
197
         * players die off.
 
198
         */
 
199
        //! Figure out who's winning diplomatically.
116
200
        void calculateDiplomaticRankings();
117
201
 
 
202
        //! Sync the playerlist.
 
203
        /**
 
204
         * Sync the playerlist with the list of players given.
 
205
         */
 
206
        void syncPlayers(std::vector<GameParameters::Player> players);
 
207
 
 
208
        //! Sync the given player with the playerlist
 
209
        void syncPlayer(GameParameters::Player player);
 
210
 
 
211
        /**
 
212
         * @param player  The player who has died.
 
213
         */
 
214
        //! Emitted when a player has died.
 
215
        sigc::signal<void, Player*> splayerDead;
 
216
    
 
217
        /**
 
218
         * Emitted when the computer players collectively offer surrender to 
 
219
         * a single remaining human player.
 
220
         *
 
221
         * @param player  The human player who is being surrendered to.
 
222
         */
 
223
        //! Emitted when a surrender is offered.
 
224
        sigc::signal<void, Player*> ssurrender;
 
225
    
118
226
    protected:
119
 
        // CREATORS
 
227
        //! Default constructor.
120
228
        Playerlist();
 
229
        //! Loading constructor.
121
230
        Playerlist(XML_Helper* helper);
 
231
        //! Destructor.
122
232
        ~Playerlist();
123
233
        
124
234
    private:
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);
127
237
 
 
238
        //! Comparison function to assist in sorting the list of players.
128
239
        static bool randomly(const Player *lhs, const Player *rhs);
129
240
 
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);
 
243
 
 
244
        //! Calculate new scores for all players.
131
245
        void calculateWinners();
 
246
 
 
247
        //! Calculate new diplomatic states for all players.
132
248
        void negotiateDiplomacy();
133
249
 
134
 
        //static pointers for the singleton
135
 
        static Playerlist* s_instance;
136
 
        
137
250
        // DATA
 
251
        //! The pointer to the player whose turn it is in the list.
138
252
        static Player* d_activeplayer;
 
253
 
 
254
        //! Whether or not the game is over.
139
255
        static bool s_finish;
 
256
 
 
257
        //! The pointer to the neutral player in the list.
140
258
        Player* d_neutral;
 
259
 
 
260
        //! A static pointer for the singleton instance.
 
261
        static Playerlist* s_instance;
 
262
        
141
263
};
142
264
 
143
265
#endif // PLAYERLIST_H