1 /***************************************************************************
2  *   Copyright (C) 2010 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 "battle_tower.h"
24 #include "battle.h"
25 #include "battle_arena.h"
26 #include "battle_board.h"
27 #include "battle_cell.h"
28 #include "castle.h"
29 #include "tools.h"
30 #include "translations.h"
31 
Tower(const Castle & castle,int twr,const Rand::DeterministicRandomGenerator & randomGenerator,const uint32_t uid)32 Battle::Tower::Tower( const Castle & castle, int twr, const Rand::DeterministicRandomGenerator & randomGenerator, const uint32_t uid )
33     : Unit( Troop( Monster::ARCHER, 0 ), -1, false, randomGenerator, uid )
34     , type( twr )
35     , color( castle.GetColor() )
36     , bonus( 0 )
37     , valid( true )
38 {
39     count += castle.CountBuildings();
40 
41     if ( count > 20 )
42         count = 20;
43     if ( TWR_CENTER != type )
44         count /= 2;
45     if ( count == 0 )
46         count = 1;
47     bonus = castle.GetLevelMageGuild();
48 
49     SetModes( CAP_TOWER );
50 }
51 
GetName(void) const52 const char * Battle::Tower::GetName( void ) const
53 {
54     switch ( type ) {
55     case TWR_LEFT:
56         return _( "Left Turret" );
57     case TWR_RIGHT:
58         return _( "Right Turret" );
59 
60     default:
61         break;
62     }
63 
64     return _( "Ballista" );
65 }
66 
isValid(void) const67 bool Battle::Tower::isValid( void ) const
68 {
69     return valid;
70 }
71 
GetType(void) const72 u32 Battle::Tower::GetType( void ) const
73 {
74     return type;
75 }
76 
GetBonus(void) const77 u32 Battle::Tower::GetBonus( void ) const
78 {
79     return bonus;
80 }
81 
GetAttack(void) const82 u32 Battle::Tower::GetAttack( void ) const
83 {
84     return Unit::GetAttack() + bonus;
85 }
86 
GetColor(void) const87 int Battle::Tower::GetColor( void ) const
88 {
89     return color;
90 }
91 
GetPortPosition(void) const92 fheroes2::Point Battle::Tower::GetPortPosition( void ) const
93 {
94     switch ( type ) {
95     case TWR_LEFT:
96         return fheroes2::Point( 410, 70 );
97         break;
98     case TWR_RIGHT:
99         return fheroes2::Point( 410, 320 );
100     case TWR_CENTER:
101         return fheroes2::Point( 560, 170 );
102     default:
103         break;
104     }
105 
106     return fheroes2::Point();
107 }
108 
SetDestroy(void)109 void Battle::Tower::SetDestroy( void )
110 {
111     switch ( type ) {
112     case TWR_LEFT:
113         Board::GetCell( Arena::CASTLE_TOP_ARCHER_TOWER_POS )->SetObject( 1 );
114         break;
115     case TWR_RIGHT:
116         Board::GetCell( Arena::CASTLE_BOTTOM_ARCHER_TOWER_POS )->SetObject( 1 );
117         break;
118     default:
119         break;
120     }
121     valid = false;
122 }
123 
GetInfo(const Castle & cstl)124 std::string Battle::Tower::GetInfo( const Castle & cstl )
125 {
126     std::vector<int> towers;
127     std::string msg;
128 
129     if ( cstl.isBuild( BUILD_CASTLE ) ) {
130         towers.push_back( TWR_CENTER );
131 
132         if ( cstl.isBuild( BUILD_LEFTTURRET ) )
133             towers.push_back( TWR_LEFT );
134         if ( cstl.isBuild( BUILD_RIGHTTURRET ) )
135             towers.push_back( TWR_RIGHT );
136 
137         const char * tmpl = _( "The %{name} fires with the strength of %{count} Archers" );
138         const char * addn = _( "each with a +%{attack} bonus to their attack skill." );
139 
140         for ( std::vector<int>::const_iterator it = towers.begin(); it != towers.end(); ++it ) {
141             Tower twr = Tower( cstl, *it, Rand::DeterministicRandomGenerator( 0 ), 0 );
142 
143             msg.append( tmpl );
144             StringReplace( msg, "%{name}", twr.GetName() );
145             StringReplace( msg, "%{count}", twr.GetCount() );
146 
147             if ( twr.GetBonus() ) {
148                 msg.append( ", " );
149                 msg.append( addn );
150                 StringReplace( msg, "%{attack}", twr.GetBonus() );
151             }
152             else {
153                 msg += '.';
154             }
155 
156             if ( ( it + 1 ) != towers.end() )
157                 msg.append( "\n \n" );
158         }
159     }
160 
161     return msg;
162 }
163