1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef PLAYER_HANDLER_H
4 #define PLAYER_HANDLER_H
5 
6 #include "System/creg/creg_cond.h"
7 
8 #include <cassert>
9 #include <vector>
10 
11 class CGameSetup;
12 class CPlayer;
13 
14 
15 class CPlayerHandler
16 {
17 public:
18 	CR_DECLARE_STRUCT(CPlayerHandler)
19 
20 	~CPlayerHandler();
21 
22 	void LoadFromSetup(const CGameSetup* setup);
23 
24 	/**
25 	 * @brief Player
26 	 * @param id index to fetch
27 	 * @return CPlayer pointer
28 	 *
29 	 * Accesses a CPlayer instance at a given index
30 	 */
Player(int id)31 	CPlayer* Player(int id) { assert(unsigned(id) < players.size()); return players[id]; }
32 
33 	/**
34 	 * @brief Player
35 	 * @param name name of the player
36 	 * @return his playernumber of -1 if not found
37 	 *
38 	 * Search a player by name.
39 	 */
40 	int Player(const std::string& name) const;
41 
42 	void PlayerLeft(int id, unsigned char reason);
43 
44 	/**
45 	 * @brief Number of players the game was created for
46 	 *
47 	 * Will change at runtime, for example if a new spectator joins
48 	 */
ActivePlayers()49 	int ActivePlayers() const { return players.size(); }
50 
51 	/**
52 	 * @brief Number of players in a team
53 	 *
54 	 * Will change during runtime (Connection lost, died, ...).
55 	 * This excludes spectators and AIs.
56 	 */
57 	std::vector<int> ActivePlayersInTeam(int teamId) const;
58 
59 	/**
60 	 * @brief is the supplied id a valid playerId?
61 	 *
62 	 * Will change during at runtime when a new spectator joins
63 	 */
IsValidPlayer(int id)64 	bool IsValidPlayer(int id) const {
65 		return ((id >= 0) && (id < ActivePlayers()));
66 	}
67 
68 	void GameFrame(int frameNum);
69 
70 	/**
71 	 * @brief Adds a new player for dynamic join
72 	 *
73 	 * This resizes the playerlist adding stubs if there's gaps to his playerNum
74 	 */
75 	void AddPlayer(const CPlayer& player);
76 
77 private:
78 	typedef std::vector<CPlayer*> playerVec;
79 	/**
80 	 * @brief players
81 	 *
82 	 * for all the players in the game
83 	 */
84 	playerVec players;
85 };
86 
87 extern CPlayerHandler* playerHandler;
88 
89 #endif // !PLAYER_HANDLER_H
90