~armagetronad-dev/armagetronad/armagetronad-old-glancing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*

*************************************************************************

ArmageTron -- Just another Tron Lightcycle Game in 3D.
Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)

**************************************************************************

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***************************************************************************

*/

#ifndef ArmageTron_TEAM_H
#define ArmageTron_TEAM_H

#include "ePlayer.h"
#include "nNetObject.h"
#include "tList.h"
#include "eSpawn.h"
#include <vector>

namespace Engine{ class TeamSync; }
class eLadderLogWriter;

tString & operator << ( tString&, const eTeam&);
std::ostream & operator << ( std::ostream&, const eTeam&);

template<class T> class nConfItem;

class eTeam: public nNetObject{
protected:							// protected attributes
    int colorID;					// ID of the team predefined color
    int listID; 					// ID in the list of all teams
    int score;						// score the team has accumulated
    int lastScore_;                 //!< score from the beginning of the round

    bool lastEmpty_;                //!< flag indicating whether the team was empty on the last call to UpdateAppearance
    bool lastWasCustomTeamName_;    //!< Was the last team name a custom set team name from the last call to UpdateAppearance?

    int numHumans;					// number of human players on the team
    int numAIs;						// number of AI players on the team

    tList<ePlayerNetID> players;    // players in this team

    int maxPlayersLocal;			// maximum number of players allowed in this team
    int maxImbalanceLocal;			// maximum imbalance allowed here

    int roundsPlayed;               //!< number of rounds played

    tShortColor color;	            // team color
    tString	name;					// our name
    tString logName;                // our name for logs, sorting and admin UI

    bool locked_;                   //!< if set, only invited players may join

    eSpawnPoint * spawnPoint;

    static void UpdateStaticFlags();// update all internal information

public:							// public configuration options
    static int  minTeams;			// minimum nuber of teams
    static int  maxTeams;    		// maximum nuber of teams
    static int  maxPlayers;   		// maximum number of players per team
    static int  minPlayers;   		// maximum number of players per team
    static int  maxImbalance;		// maximum difference of player numbers allowed
    static bool balanceWithAIs;	// use AI players to balance the teams?
    static bool enforceRulesOnQuit; // if the quitting of one player unbalances the teams, enforce the rules by redistributing

    static tList<eTeam> teams;		//  list of all teams

    int RoundsPlayed() const;       //!< number of rounds played (updated right after spawning, so it includes the current round)
    void PlayRound();               //!< increase round counter

    void UpdateProperties();		// update internal properties ( player count )
    void UpdateAppearance();		// update name and color
    void Update();					// update all properties

    void SetLocked( bool locked );  // sets the lock status (whether invitations are required)
    bool IsLocked() const;          // returns the lock status
    bool IsLockedFor( const ePlayerNetID * p ) const;   // returns if a team is locked to join by a certain player (due to ACCESS_LEVEL_PLAY)

    void Invite( ePlayerNetID * player );                // invite the player to join
    void UnInvite( ePlayerNetID * player );              // revoke an invitation
    bool IsInvited( ePlayerNetID const * player ) const; // check if a player is invited
    std::vector< const ePlayerNetID * > InterestingInvitedPlayers() const;

    static bool Enemies( eTeam const * team, ePlayerNetID const * player ); //!< determines whether the player is an enemy of the team
    static bool Enemies( eTeam const * team1, eTeam const * team2 ); //!< determines whether two teams are enemies

    static void Enforce( int minTeams, int maxTeams, int maxImbalance );
    
    static void WritePlayers( eLadderLogWriter & writer, const eTeam *team );
    static void WriteOnlinePlayers();
public:												// public methods
    static void	EnforceConstraints();					// make sure the limits on team number and such are met

    static void SortByScore();							// brings the teams into the right order

    static void SwapTeamsNo(int a,int b);             	// swaps the teams a and b

    static tString Ranking( int MAX = 6, bool cut = true );				// return ranking information
    static float RankingGraph( float y, int MAX = 6 );				// print ranking information

    bool			NameTeamAfterColor ( bool wish );	// set the ability to use a color as a team name, return status

    bool			TeamNamedAfterColor () const { return colorID >= 0; } //!< returns whether the team is currently named after a color

    void 			AddPlayer   	( ePlayerNetID* player );				// register a player
    void 			AddPlayerDirty 	( ePlayerNetID* player );				// register a player without calling UpdateProperties
    void 	 		RemovePlayer	( ePlayerNetID* player );				// deregister a player
    virtual bool	PlayerMayJoin	( const ePlayerNetID* player ) const;		// see if the given player may join this team
    static bool 	NewTeamAllowed	();											// is it allowed to create a new team?

    static void     SwapPlayers     ( ePlayerNetID* player1, ePlayerNetID *player2 ); //!< swaps the team positions of the two players (same team or not)

    void            Shuffle         ( int startID, int stopID ); //!< shuffles the player at team postion startID to stopID

    virtual bool BalanceThisTeam() const {
        return true;    // care about this team when balancing teams
    }
    virtual bool IsHuman() const {
        return true;    // does this team consist of humans?
    }

    int				TeamID			( void  ) const {
        return listID;
    }

    int				Score			(		) const {
        return score;
    }
    void			AddScore		( int s );
    void			ResetScore		(		);
    void			SetScore		( int s );

    void 			AddScore		( int points,
                        const tOutput& reasonwin,
                        const tOutput& reasonlose,
                        const tOutput& reasonfree=tOutput());

    eSpawnPoint * SpawnPoint() {
        return spawnPoint;
    }

    void SetSpawnPoint(eSpawnPoint * sp)
    {
        spawnPoint = sp;
    }

    static void ResetScoreDifferences(); //<! Resets the last stored score so ScoreDifferences takes this as a reference time
    static void LogScoreDifferences();   //<! Logs accumulated scores of all players since the last call to ResetScoreDifferences() to ladderlog.txt
    void LogScoreDifference();           //<! Logs accumulated scores since the last call to ResetScoreDifferences() to ladderlog.txt

    // player inquiry
    int	 			NumPlayers(		) const {
        return players.Len();    // total number of players, includes players that quit the team during the current round
    }
    ePlayerNetID*	Player			( int i ) const {
        return players(i); 	   // player of index i
    }
    // Export a list of pointers to all the players in this team
    std::vector < ePlayerNetID * > 	GetAllMembers () const {
        std::vector <ePlayerNetID *> tmp;
        for (int i=0; i<players.Len(); i++) {
            tmp.push_back( players(i) );
        }
        return tmp;
    }


    int	 			NumHumanPlayers	(		) const; 							//!< number of human players
    int	 			NumAIPlayers	(		) const; 							//!< number of human players
    int             NumActivePlayers(       ) const;                            //!< how many active players are there right now that can spawn next round?
    int	 			AlivePlayers	(		) const;							//!< how many of the current players are currently alive?
    ePlayerNetID*	OldestPlayer	(		) const;							//!< the oldest player
    ePlayerNetID*	OldestHumanPlayer(		) const;							//!< the oldest human player
    ePlayerNetID*	OldestAIPlayer	(		) const;							//!< the oldest AI player
    ePlayerNetID*	YoungestPlayer	(		) const;							//!< the youngest player
    ePlayerNetID*	YoungestHumanPlayer(	) const;							//!< the youngest human player
    ePlayerNetID*	YoungestAIPlayer(		) const;							//!< the youngest AI player
    bool			Alive			(		) const;							//!< is any of the players currently alive?
    bool            IsReady         (       ) const;                            //!< is the team ready to play?

    // color
    tShortColor const & Color() const
    {
        return color;
    }
    
    // name and color
    unsigned short	R() 	const {
        return color.r_;
    }
    unsigned short	G() 	const {
        return color.g_;
    }
    unsigned short	B() 	const {
        return color.b_;
    }
    const tString& 	Name() 	const {
        return name;
    }

    tColoredString GetColoredName(void) const;
    const tString& GetLogName() const { return logName; }

    virtual void PrintName(tString &s) const;					// print out an understandable name in to s


    virtual bool ClearToTransmit(int user) const;		// we must not transmit an object that contains pointers to non-transmitted objects. this function is supposed to check that.

    //! creates a netobject form sync data
    eTeam( Engine::TeamSync const &, nSenderInfo const & );
    //! reads incremental sync data. Returns false if sync was invalid or old.
    void ReadSync( Engine::TeamSync const &, nSenderInfo const & );
    //! writes sync data (and initialization data if flat is set)
    void WriteSync( Engine::TeamSync &, bool init );
    //! returns the descriptor responsible for this class
    virtual nNetObjectDescriptorBase const & DoGetDescriptor() const;

    // shall the server accept sync messages from the clients?
    virtual bool AcceptClientSync() const	{
        return false;
    }

    // con/desstruction
    eTeam();											// default constructor
    ~eTeam();											// destructor

private:
    void 	 		RemovePlayerDirty( ePlayerNetID* player );				// just remove a player from the player list, no messages, no balancing
    void LogScoreDifference( const tString & teamName );
};

#endif