1 /***************************************************************************
2  *   Copyright (C) 2009 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 #ifndef H2KINGDOM_H
23 #define H2KINGDOM_H
24 
25 #include "castle.h"
26 #include "heroes_recruits.h"
27 #include "mp2.h"
28 #include "pairs.h"
29 #include "puzzle.h"
30 
31 struct CapturedObjects;
32 
33 struct LastLoseHero
34 {
LastLoseHeroLastLoseHero35     LastLoseHero()
36         : id( Heroes::UNKNOWN )
37         , date( 0 )
38     {}
39     int id;
40     u32 date;
41 };
42 
43 struct KingdomCastles : public VecCastles
44 {};
45 
46 struct KingdomHeroes : public VecHeroes
47 {};
48 
49 class Kingdom : public BitModes, public Control
50 {
51 public:
52     enum
53     {
54         // UNDEF      = 0x0001,
55         IDENTIFYHERO = 0x0002,
56         // UNUSED = 0x0004,
57         KINGDOM_OVERVIEW_CASTLE_SELECTION = 0x0008
58     };
59 
60     Kingdom();
61     ~Kingdom() override = default;
62 
63     void Init( int color );
64     void clear( void );
65 
66     void openOverviewDialog();
67 
68     bool isPlay( void ) const;
69     bool isLoss( void ) const;
70     bool AllowPayment( const Funds & ) const;
71     bool AllowRecruitHero( bool check_payment, int level ) const;
72 
73     void SetLastLostHero( const Heroes & );
74     void ResetLastLostHero( void );
75 
76     void SetLastBattleWinHero( const Heroes & hero );
77 
78     Heroes * GetLastLostHero( void ) const;
79     Heroes * GetLastBattleWinHero() const;
80 
81     Heroes * GetBestHero();
82 
83     Monster GetStrongestMonster() const;
84 
85     int GetControl( void ) const override;
86     int GetColor( void ) const;
87     int GetRace( void ) const;
88 
GetFunds(void)89     const Funds & GetFunds( void ) const
90     {
91         return resource;
92     }
93     Funds GetIncome( int = INCOME_ALL ) const;
94 
95     double GetArmiesStrength( void ) const;
96 
97     void AddFundsResource( const Funds & );
98     void OddFundsResource( const Funds & );
99 
100     u32 GetCountCastle( void ) const;
101     u32 GetCountTown( void ) const;
102     u32 GetCountMarketplace( void ) const;
103     u32 GetLostTownDays( void ) const;
104     u32 GetCountNecromancyShrineBuild( void ) const;
105     u32 GetCountBuilding( u32 ) const;
106     uint32_t GetCountThievesGuild() const;
107 
108     uint32_t GetCountArtifacts() const;
109 
110     Recruits & GetRecruits( void );
111 
GetHeroes(void)112     const KingdomHeroes & GetHeroes( void ) const
113     {
114         return heroes;
115     }
GetCastles(void)116     const KingdomCastles & GetCastles( void ) const
117     {
118         return castles;
119     }
120 
GetHeroes(void)121     KingdomHeroes & GetHeroes( void )
122     {
123         return heroes;
124     }
GetCastles(void)125     KingdomCastles & GetCastles( void )
126     {
127         return castles;
128     }
129 
130     void AddHeroes( Heroes * );
131     void RemoveHeroes( const Heroes * );
132     void ApplyPlayWithStartingHero( void );
133 
134     void AddCastle( const Castle * );
135     void RemoveCastle( const Castle * );
136 
137     void ActionBeforeTurn( void );
138     void ActionNewDay( void );
139     void ActionNewWeek( void );
140     void ActionNewMonth( void );
141 
142     void SetVisited( s32 index, const MP2::MapObjectType objectType );
143     uint32_t CountVisitedObjects( const MP2::MapObjectType objectType ) const;
144     bool isVisited( const MP2::MapObjectType objectType ) const;
145     bool isVisited( const Maps::Tiles & ) const;
146     bool isVisited( s32, const MP2::MapObjectType objectType ) const;
147 
148     bool isValidKingdomObject( const Maps::Tiles & tile, const MP2::MapObjectType objectType ) const;
149 
150     bool HeroesMayStillMove( void ) const;
151 
152     Puzzle & PuzzleMaps( void );
153 
154     void SetVisitTravelersTent( int color );
155     bool IsVisitTravelersTent( int ) const;
156 
157     void UpdateRecruits( void );
158     void LossPostActions( void );
159 
160     bool IsTileVisibleFromCrystalBall( const int32_t dest ) const;
161 
162     static u32 GetMaxHeroes( void );
163 
164 private:
165     cost_t _getKingdomStartingResources( const int difficulty );
166 
167     friend StreamBase & operator<<( StreamBase &, const Kingdom & );
168     friend StreamBase & operator>>( StreamBase &, Kingdom & );
169 
170     int color;
171     int _lastBattleWinHeroID;
172     Funds resource;
173 
174     u32 lost_town_days;
175 
176     KingdomCastles castles;
177     KingdomHeroes heroes;
178 
179     Recruits recruits;
180     LastLoseHero lost_hero;
181 
182     std::list<IndexObject> visit_object;
183 
184     Puzzle puzzle_maps;
185     u32 visited_tents_colors;
186 
187     KingdomHeroes heroes_cond_loss;
188 
189     // Used to remember which item was selected in Kingdom View dialog.
190     int _topItemInKingdomView;
191 };
192 
193 class Kingdoms
194 {
195 public:
196     Kingdoms() = default;
197 
198     void Init( void );
199     void clear( void );
200 
201     void ApplyPlayWithStartingHero( void );
202 
203     void NewDay( void );
204     void NewWeek( void );
205     void NewMonth( void );
206 
207     Kingdom & GetKingdom( int color );
208     const Kingdom & GetKingdom( int color ) const;
209 
210     int GetNotLossColors( void ) const;
211     int FindWins( int ) const;
212 
213     void AddHeroes( const AllHeroes & );
214     void AddCastles( const AllCastles & );
215 
216     void AddTributeEvents( CapturedObjects & captureobj, const uint32_t day, const MP2::MapObjectType objectType );
217 
218 private:
219     friend StreamBase & operator<<( StreamBase &, const Kingdoms & );
220     friend StreamBase & operator>>( StreamBase &, Kingdoms & );
221 
222     static constexpr uint32_t _size = KINGDOMMAX + 1;
223     Kingdom kingdoms[_size];
224 };
225 
226 StreamBase & operator<<( StreamBase &, const Kingdom & );
227 StreamBase & operator>>( StreamBase &, Kingdom & );
228 
229 StreamBase & operator<<( StreamBase &, const Kingdoms & );
230 StreamBase & operator>>( StreamBase &, Kingdoms & );
231 
232 StreamBase & operator<<( StreamBase & sb, const LastLoseHero & hero );
233 StreamBase & operator>>( StreamBase & sb, LastLoseHero & hero );
234 
235 #endif
236