1 /***************************************************************************
2  *   Copyright (C) 2011 by Andrey Afletdinov <fheroes2@gmail.com>          *
3  *                                                                         *
4  *   Part of the Free Heroes2 Engine:                                      *
5  *   http://sourceforge.net/projects/fheroes2                              *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21  ***************************************************************************/
22 
23 #ifndef H2PLAYERS_H
24 #define H2PLAYERS_H
25 
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 #include "bitmodes.h"
31 #include "color.h"
32 
33 namespace Maps
34 {
35     struct FileInfo;
36 }
37 
38 namespace AI
39 {
40     class Base;
41 }
42 
43 class Castle;
44 class Heroes;
45 
46 // control_t
47 enum
48 {
49     CONTROL_NONE = 0,
50     CONTROL_HUMAN = 1,
51     CONTROL_AI = 4,
52     CONTROL_REMOTE = 2 /*, CONTROL_LOCAL = CONTROL_AI | CONTROL_HUMAN */
53 };
54 enum
55 {
56     FOCUS_UNSEL = 0,
57     FOCUS_HEROES = 1,
58     FOCUS_CASTLE = 2
59 };
60 
61 struct Focus : std::pair<int, void *>
62 {
FocusFocus63     Focus()
64         : std::pair<int, void *>( FOCUS_UNSEL, nullptr )
65     {}
66 
ResetFocus67     void Reset( void )
68     {
69         first = FOCUS_UNSEL;
70         second = nullptr;
71     }
SetFocus72     void Set( Castle * ptr )
73     {
74         first = FOCUS_CASTLE;
75         second = ptr;
76     }
SetFocus77     void Set( Heroes * ptr )
78     {
79         first = FOCUS_HEROES;
80         second = ptr;
81     }
82 
GetCastleFocus83     Castle * GetCastle( void )
84     {
85         return first == FOCUS_CASTLE && second ? static_cast<Castle *>( second ) : nullptr;
86     }
GetHeroesFocus87     Heroes * GetHeroes( void )
88     {
89         return first == FOCUS_HEROES && second ? static_cast<Heroes *>( second ) : nullptr;
90     }
91 };
92 
93 struct Control
94 {
95     virtual int GetControl( void ) const = 0;
96     virtual ~Control() = default;
97 
98     bool isControlAI( void ) const;
99     bool isControlHuman( void ) const;
100     bool isControlRemote( void ) const;
101     bool isControlLocal( void ) const;
102 };
103 
104 class Player : public BitModes, public Control
105 {
106 public:
107     explicit Player( int col = Color::NONE );
108     ~Player() override = default;
109 
110     bool isColor( int ) const;
111     bool isPlay( void ) const;
112 
113     void SetColor( int );
114     void SetRace( int );
115     void SetControl( int );
116     void SetPlay( bool );
117     void SetFriends( int );
118     void SetName( const std::string & );
119 
120     int GetControl( void ) const override;
121     int GetColor( void ) const;
122     int GetRace( void ) const;
123     int GetFriends( void ) const;
124 
125     std::string GetDefaultName() const;
126     const std::string & GetName( void ) const;
127 
128     std::string GetPersonalityString() const;
129 
130     Focus & GetFocus( void );
131     const Focus & GetFocus( void ) const;
132 
133 protected:
134     friend StreamBase & operator<<( StreamBase &, const Player & );
135     friend StreamBase & operator>>( StreamBase &, Player & );
136 
137     int control;
138     int color;
139     int race;
140     int friends;
141     std::string name;
142     u32 id;
143     Focus focus;
144     std::shared_ptr<AI::Base> _ai;
145 };
146 
147 StreamBase & operator<<( StreamBase &, const Player & );
148 StreamBase & operator>>( StreamBase &, Player & );
149 
150 class Players : public std::vector<Player *>
151 {
152 public:
153     Players();
154     Players( const Players & ) = delete;
155 
156     ~Players();
157 
158     Players & operator=( const Players & ) = delete;
159 
160     void Init( int colors );
161     void Init( const Maps::FileInfo & );
162     void clear( void );
163 
164     void SetStartGame( void );
165     int GetColors( int control = 0xFF, bool strong = false ) const;
166     int GetActualColors( void ) const;
167     std::string String( void ) const;
168 
169     Player * GetCurrent( void );
170     const Player * GetCurrent( void ) const;
171 
172     static void Set( const int color, Player * player );
173     static Player * Get( int color );
174     static int GetPlayerControl( int color );
175     static int GetPlayerRace( int color );
176     static int GetPlayerFriends( int color );
177     static bool GetPlayerInGame( int color );
178     static bool isFriends( int player, int colors );
179     static void SetPlayerRace( int color, int race );
180     static void SetPlayerControl( int color, int ctrl );
181     static void SetPlayerInGame( int color, bool );
182     static int HumanColors( void );
183     static int FriendColors( void );
184 
185     int current_color;
186 };
187 
188 StreamBase & operator<<( StreamBase &, const Players & );
189 StreamBase & operator>>( StreamBase &, Players & );
190 
191 #endif
192