1 /***************************************************************************
2 * Copyright (C) 2012 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 #include <algorithm>
24
25 #include "agg.h"
26 #include "castle.h"
27 #include "game.h"
28 #include "game_interface.h"
29 #include "heroes.h"
30 #include "kingdom.h"
31 #include "mus.h"
32 #include "settings.h"
33 #include "world.h"
34
SetFocus(Heroes * hero)35 void Interface::Basic::SetFocus( Heroes * hero )
36 {
37 Player * player = Settings::Get().GetPlayers().GetCurrent();
38
39 if ( player ) {
40 Focus & focus = player->GetFocus();
41
42 if ( focus.GetHeroes() && focus.GetHeroes() != hero ) {
43 focus.GetHeroes()->SetMove( false );
44 focus.GetHeroes()->ShowPath( false );
45 }
46
47 hero->RescanPath();
48 hero->ShowPath( true );
49 focus.Set( hero );
50
51 GetButtonsArea().Redraw();
52
53 iconsPanel.Select( hero );
54 gameArea.SetCenter( hero->GetCenter() );
55 statusWindow.SetState( StatusType::STATUS_ARMY );
56
57 const int heroIndexPos = hero->GetIndex();
58 if ( Game::UpdateSoundsOnFocusUpdate() && heroIndexPos >= 0 ) {
59 Game::EnvironmentSoundMixer();
60 AGG::PlayMusic( MUS::FromGround( world.GetTiles( heroIndexPos ).GetGround() ), true, true );
61 }
62 }
63 }
64
SetFocus(Castle * castle)65 void Interface::Basic::SetFocus( Castle * castle )
66 {
67 Player * player = Settings::Get().GetPlayers().GetCurrent();
68
69 if ( player ) {
70 Focus & focus = player->GetFocus();
71
72 if ( focus.GetHeroes() ) {
73 focus.GetHeroes()->SetMove( false );
74 focus.GetHeroes()->ShowPath( false );
75 }
76
77 focus.Set( castle );
78
79 GetButtonsArea().Redraw();
80
81 iconsPanel.Select( castle );
82 gameArea.SetCenter( castle->GetCenter() );
83 statusWindow.SetState( StatusType::STATUS_FUNDS );
84
85 if ( Game::UpdateSoundsOnFocusUpdate() ) {
86 Game::EnvironmentSoundMixer();
87 AGG::PlayMusic( MUS::FromGround( world.GetTiles( castle->GetIndex() ).GetGround() ), true, true );
88 }
89 }
90 }
91
ResetFocus(int priority)92 void Interface::Basic::ResetFocus( int priority )
93 {
94 Player * player = Settings::Get().GetPlayers().GetCurrent();
95
96 if ( player ) {
97 Focus & focus = player->GetFocus();
98 Kingdom & myKingdom = world.GetKingdom( player->GetColor() );
99
100 iconsPanel.ResetIcons();
101
102 switch ( priority ) {
103 case GameFocus::FIRSTHERO: {
104 const KingdomHeroes & heroes = myKingdom.GetHeroes();
105 // skip sleeping
106 KingdomHeroes::const_iterator it = std::find_if( heroes.begin(), heroes.end(), []( const Heroes * hero ) { return !hero->Modes( Heroes::SLEEPER ); } );
107
108 if ( it != heroes.end() )
109 SetFocus( *it );
110 else
111 ResetFocus( GameFocus::CASTLE );
112 break;
113 }
114
115 case GameFocus::HEROES:
116 if ( focus.GetHeroes() && focus.GetHeroes()->GetColor() == player->GetColor() )
117 SetFocus( focus.GetHeroes() );
118 else if ( !myKingdom.GetHeroes().empty() )
119 SetFocus( myKingdom.GetHeroes().front() );
120 else if ( !myKingdom.GetCastles().empty() ) {
121 iconsPanel.SetRedraw( ICON_HEROES );
122 SetFocus( myKingdom.GetCastles().front() );
123 }
124 else
125 focus.Reset();
126 break;
127
128 case GameFocus::CASTLE:
129 if ( focus.GetCastle() && focus.GetCastle()->GetColor() == player->GetColor() )
130 SetFocus( focus.GetCastle() );
131 else if ( !myKingdom.GetCastles().empty() )
132 SetFocus( myKingdom.GetCastles().front() );
133 else if ( !myKingdom.GetHeroes().empty() ) {
134 iconsPanel.SetRedraw( ICON_CASTLES );
135 SetFocus( myKingdom.GetHeroes().front() );
136 }
137 else
138 focus.Reset();
139 break;
140
141 default:
142 focus.Reset();
143 break;
144 }
145 }
146 }
147
GetFocusType(void)148 int Interface::GetFocusType( void )
149 {
150 Player * player = Settings::Get().GetPlayers().GetCurrent();
151
152 if ( player ) {
153 Focus & focus = player->GetFocus();
154
155 if ( focus.GetHeroes() )
156 return GameFocus::HEROES;
157 else if ( focus.GetCastle() )
158 return GameFocus::CASTLE;
159 }
160
161 return GameFocus::UNSEL;
162 }
163
GetFocusCastle(void)164 Castle * Interface::GetFocusCastle( void )
165 {
166 Player * player = Settings::Get().GetPlayers().GetCurrent();
167
168 return player ? player->GetFocus().GetCastle() : nullptr;
169 }
170
GetFocusHeroes(void)171 Heroes * Interface::GetFocusHeroes( void )
172 {
173 Player * player = Settings::Get().GetPlayers().GetCurrent();
174
175 return player ? player->GetFocus().GetHeroes() : nullptr;
176 }
177
GetFocusCenter(void)178 fheroes2::Point Interface::GetFocusCenter( void )
179 {
180 Player * player = Settings::Get().GetPlayers().GetCurrent();
181
182 if ( player ) {
183 Focus & focus = player->GetFocus();
184
185 if ( focus.GetHeroes() )
186 return focus.GetHeroes()->GetCenter();
187 else if ( focus.GetCastle() )
188 return focus.GetCastle()->GetCenter();
189 }
190
191 return fheroes2::Point( world.w() / 2, world.h() / 2 );
192 }
193
RedrawFocus(void)194 void Interface::Basic::RedrawFocus( void )
195 {
196 int type = GetFocusType();
197
198 if ( type != FOCUS_HEROES && iconsPanel.IsSelected( ICON_HEROES ) ) {
199 iconsPanel.ResetIcons( ICON_HEROES );
200 iconsPanel.SetRedraw();
201 }
202 else if ( type == FOCUS_HEROES && !iconsPanel.IsSelected( ICON_HEROES ) ) {
203 iconsPanel.Select( GetFocusHeroes() );
204 iconsPanel.SetRedraw();
205 }
206
207 if ( type != FOCUS_CASTLE && iconsPanel.IsSelected( ICON_CASTLES ) ) {
208 iconsPanel.ResetIcons( ICON_CASTLES );
209 iconsPanel.SetRedraw();
210 }
211 else if ( type == FOCUS_CASTLE && !iconsPanel.IsSelected( ICON_CASTLES ) ) {
212 iconsPanel.Select( GetFocusCastle() );
213 iconsPanel.SetRedraw();
214 }
215
216 SetRedraw( REDRAW_GAMEAREA | REDRAW_RADAR );
217
218 if ( type == FOCUS_HEROES )
219 iconsPanel.SetRedraw( ICON_HEROES );
220 else if ( type == FOCUS_CASTLE )
221 iconsPanel.SetRedraw( ICON_CASTLES );
222
223 statusWindow.SetRedraw();
224 }
225