1 /******************************************************************************
2  *  Warmux is a convivial mass murder game.
3  *  Copyright (C) 2001-2011 Warmux Team.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18  ******************************************************************************
19  * Handle team list
20  *****************************************************************************/
21 
22 #ifndef TEAMS_LIST_H
23 #define TEAMS_LIST_H
24 //-----------------------------------------------------------------------------
25 #include <list>
26 #include <vector>
27 #include <WARMUX_singleton.h>
28 //-----------------------------------------------------------------------------
29 
30 // Forward declarations
31 class Team;
32 class ConfigTeam;
33 class Character;
34 class WeaponsList;
35 
36 class TeamsList : public Singleton<TeamsList>
37 {
38 public:
39   typedef std::list<Team *>::iterator full_iterator;
40   typedef std::vector<Team *>::iterator iterator;
41   std::list<Team *> full_list;
42   std::vector<Team*> playing_list;
43 
44 private:
45   typedef std::list<uint>::iterator selection_iterator;
46   std::list<uint> selection;
47   std::vector<Team*>::iterator active_team;
48 
49   bool LoadOneTeam(const std::string &dir, const std::string &file);
50   void LoadList();
51 
52   void AddTeam(Team* the_team, int pos, const ConfigTeam& the_team_cfg, bool is_local);
53   void UpdateTeam(Team* the_team, const ConfigTeam &the_team_cfg);
54   void DelTeam(Team* the_team);
55   TeamsList();
56 
57 protected:
58   friend class Singleton<TeamsList>;
59 
60 public:
61   ~TeamsList();
62 
63   friend TeamsList &GetTeamsList(void);
64   void NextTeam();
65   Team* GetNextTeam();
66   Team& ActiveTeam();
67   void LoadGamingData(WeaponsList * weapons_list);
68   void UnloadGamingData();
Clear()69   void Clear() { selection.clear(); playing_list.clear(); }
70   void RandomizeFirstPlayer();
71 
72   // Add a new team to playing, and change active team
73   void AddTeam(const ConfigTeam& the_team_cfg, bool is_local, bool generate_error = true);
74   Team* UpdateTeam(const std::string &old_team_id, const ConfigTeam& the_team_cfg);
75   void DelTeam(const std::string &id);
76   void SetActive(const std::string &id);
77   void InitList(const std::list<ConfigTeam> &lst);
78   void InitEnergy();
79   void RefreshEnergy(); //Refresh energy bar
80   void RefreshSort(); //Refresh energy bar position
81   void ChangeSelection(const std::list<uint>& liste);
82   bool IsSelected(uint index);
IsLoaded()83   static bool IsLoaded() { return singleton != NULL; }
84 
85   // Find a team by its id or index (in full_list)
86   Team* FindById (const std::string &id, int &pos);
87   Team* FindByIndex (uint index);
88   // Find a team by its id or index (in playing full_list)
89   Team* FindPlayingById(const std::string &id, int &index);
FindPlayingByIndex(uint index)90   Team* FindPlayingByIndex(uint index)
91   {
92     ASSERT(index < playing_list.size());
93     return playing_list[index];
94   }
95 
GetPlayingList()96   std::vector<Team*>& GetPlayingList() { return playing_list; }
SetPlayingList(const std::vector<Team * > & list)97   void SetPlayingList(const std::vector<Team*>& list)
98   {
99     playing_list = list;
100     active_team = playing_list.begin();
101   }
102 };
103 
GetTeamsList(void)104 inline TeamsList &GetTeamsList(void) { return TeamsList::GetRef(); };
105 
106 // current active team
ActiveTeam()107 inline Team& ActiveTeam() { return GetTeamsList().ActiveTeam(); }
108 
109 // current active character
110 Character& ActiveCharacter();
111 
112 bool compareTeams(const Team *a, const Team *b);
113 
114 #endif
115