2022-11-18 17:54:10 +02:00
/*
2022-12-11 23:16:23 +02:00
* BattleControlPanel . cpp , part of VCMI engine
2022-11-18 17:54:10 +02:00
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
# include "StdInc.h"
2022-12-09 13:38:46 +02:00
# include "BattleControlPanel.h"
2022-11-28 16:43:38 +02:00
2022-12-09 13:38:46 +02:00
# include "BattleInterface.h"
# include "BattleInterfaceClasses.h"
# include "BattleStacksController.h"
# include "BattleActionsController.h"
2022-11-28 16:43:38 +02:00
2022-11-18 17:54:10 +02:00
# include "../CGameInfo.h"
# include "../CPlayerInterface.h"
# include "../gui/CCursorHandler.h"
# include "../gui/CGuiHandler.h"
2022-12-20 01:28:20 +02:00
# include "../gui/CAnimation.h"
2022-11-18 17:54:10 +02:00
# include "../windows/CSpellWindow.h"
2022-11-28 16:43:38 +02:00
# include "../widgets/Buttons.h"
# include "../widgets/Images.h"
# include "../../CCallback.h"
# include "../../lib/CGeneralTextHandler.h"
# include "../../lib/mapObjects/CGHeroInstance.h"
2022-11-18 17:54:10 +02:00
# include "../../lib/CStack.h"
# include "../../lib/CConfigHandler.h"
2022-12-13 13:58:16 +02:00
BattleControlPanel : : BattleControlPanel ( BattleInterface & owner , const Point & position ) :
2022-11-18 17:54:10 +02:00
owner ( owner )
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE ;
2022-11-27 02:26:02 +02:00
pos + = position ;
2022-11-18 17:54:10 +02:00
//preparing buttons and console
2022-12-09 13:26:17 +02:00
bOptions = std : : make_shared < CButton > ( Point ( 3 , 5 ) , " icm003.def " , CGI - > generaltexth - > zelp [ 381 ] , std : : bind ( & BattleControlPanel : : bOptionsf , this ) , SDLK_o ) ;
bSurrender = std : : make_shared < CButton > ( Point ( 54 , 5 ) , " icm001.def " , CGI - > generaltexth - > zelp [ 379 ] , std : : bind ( & BattleControlPanel : : bSurrenderf , this ) , SDLK_s ) ;
bFlee = std : : make_shared < CButton > ( Point ( 105 , 5 ) , " icm002.def " , CGI - > generaltexth - > zelp [ 380 ] , std : : bind ( & BattleControlPanel : : bFleef , this ) , SDLK_r ) ;
bAutofight = std : : make_shared < CButton > ( Point ( 157 , 5 ) , " icm004.def " , CGI - > generaltexth - > zelp [ 382 ] , std : : bind ( & BattleControlPanel : : bAutofightf , this ) , SDLK_a ) ;
bSpell = std : : make_shared < CButton > ( Point ( 645 , 5 ) , " icm005.def " , CGI - > generaltexth - > zelp [ 385 ] , std : : bind ( & BattleControlPanel : : bSpellf , this ) , SDLK_c ) ;
bWait = std : : make_shared < CButton > ( Point ( 696 , 5 ) , " icm006.def " , CGI - > generaltexth - > zelp [ 386 ] , std : : bind ( & BattleControlPanel : : bWaitf , this ) , SDLK_w ) ;
bDefence = std : : make_shared < CButton > ( Point ( 747 , 5 ) , " icm007.def " , CGI - > generaltexth - > zelp [ 387 ] , std : : bind ( & BattleControlPanel : : bDefencef , this ) , SDLK_d ) ;
2022-12-20 01:28:20 +02:00
bSwitchAction = std : : make_shared < CButton > ( Point ( 589 , 5 ) , " icmalt00 " , CGI - > generaltexth - > zelp [ 387 ] , std : : bind ( & BattleControlPanel : : bSwitchActionf , this ) , SDLK_r ) ;
bConsoleUp = std : : make_shared < CButton > ( Point ( 578 , 5 ) , " ComSlide.def " , std : : make_pair ( " " , " " ) , std : : bind ( & BattleControlPanel : : bConsoleUpf , this ) , SDLK_UP ) ;
bConsoleDown = std : : make_shared < CButton > ( Point ( 578 , 24 ) , " ComSlide.def " , std : : make_pair ( " " , " " ) , std : : bind ( & BattleControlPanel : : bConsoleDownf , this ) , SDLK_DOWN ) ;
2022-11-18 17:54:10 +02:00
bDefence - > assignedKeys . insert ( SDLK_SPACE ) ;
bConsoleUp - > setImageOrder ( 0 , 1 , 0 , 0 ) ;
bConsoleDown - > setImageOrder ( 2 , 3 , 2 , 2 ) ;
2022-12-20 01:28:20 +02:00
console = std : : make_shared < BattleConsole > ( Rect ( 211 , 4 , 350 , 38 ) ) ;
2022-11-18 17:54:10 +02:00
GH . statusbar = console ;
2022-12-13 13:58:16 +02:00
if ( owner . tacticsMode )
2022-11-18 17:54:10 +02:00
tacticPhaseStarted ( ) ;
else
tacticPhaseEnded ( ) ;
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : show ( SDL_Surface * to )
2022-11-27 02:26:02 +02:00
{
//show menu before all other elements to keep it in background
menu - > show ( to ) ;
CIntObject : : show ( to ) ;
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : showAll ( SDL_Surface * to )
2022-11-27 02:26:02 +02:00
{
//show menu before all other elements to keep it in background
menu - > showAll ( to ) ;
CIntObject : : showAll ( to ) ;
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : tacticPhaseStarted ( )
2022-11-18 17:54:10 +02:00
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE ;
btactNext = std : : make_shared < CButton > ( Point ( 213 , 4 ) , " icm011.def " , std : : make_pair ( " " , " " ) , [ & ] ( ) { bTacticNextStack ( ) ; } , SDLK_SPACE ) ;
btactEnd = std : : make_shared < CButton > ( Point ( 419 , 4 ) , " icm012.def " , std : : make_pair ( " " , " " ) , [ & ] ( ) { bTacticPhaseEnd ( ) ; } , SDLK_RETURN ) ;
menu = std : : make_shared < CPicture > ( " COPLACBR.BMP " , 0 , 0 ) ;
2022-12-13 13:58:16 +02:00
menu - > colorize ( owner . curInt - > playerID ) ;
2022-11-27 02:26:02 +02:00
menu - > recActions & = ~ ( SHOWALL | UPDATE ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : tacticPhaseEnded ( )
2022-11-18 17:54:10 +02:00
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE ;
2022-11-20 22:56:42 +02:00
btactNext . reset ( ) ;
btactEnd . reset ( ) ;
2022-11-18 17:54:10 +02:00
menu = std : : make_shared < CPicture > ( " CBAR.BMP " , 0 , 0 ) ;
2022-12-13 13:58:16 +02:00
menu - > colorize ( owner . curInt - > playerID ) ;
2022-11-27 02:26:02 +02:00
menu - > recActions & = ~ ( SHOWALL | UPDATE ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bOptionsf ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . actionsController - > spellcastingModeActive ( ) )
2022-11-18 17:54:10 +02:00
return ;
CCS - > curh - > changeGraphic ( ECursor : : ADVENTURE , 0 ) ;
2022-12-09 13:26:17 +02:00
GH . pushIntT < BattleOptionsWindow > ( owner ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bSurrenderf ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . actionsController - > spellcastingModeActive ( ) )
2022-11-18 17:54:10 +02:00
return ;
2022-12-13 13:58:16 +02:00
int cost = owner . curInt - > cb - > battleGetSurrenderCost ( ) ;
2022-11-18 17:54:10 +02:00
if ( cost > = 0 )
{
2022-12-13 13:58:16 +02:00
std : : string enemyHeroName = owner . curInt - > cb - > battleGetEnemyHero ( ) . name ;
2022-11-18 17:54:10 +02:00
if ( enemyHeroName . empty ( ) )
{
logGlobal - > warn ( " Surrender performed without enemy hero, should not happen! " ) ;
enemyHeroName = " #ENEMY# " ;
}
std : : string surrenderMessage = boost : : str ( boost : : format ( CGI - > generaltexth - > allTexts [ 32 ] ) % enemyHeroName % cost ) ; //%s states: "I will accept your surrender and grant you and your troops safe passage for the price of %d gold."
2022-12-13 13:58:16 +02:00
owner . curInt - > showYesNoDialog ( surrenderMessage , [ this ] ( ) { reallySurrender ( ) ; } , nullptr ) ;
2022-11-18 17:54:10 +02:00
}
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bFleef ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . actionsController - > spellcastingModeActive ( ) )
2022-11-18 17:54:10 +02:00
return ;
2022-12-13 13:58:16 +02:00
if ( owner . curInt - > cb - > battleCanFlee ( ) )
2022-11-18 17:54:10 +02:00
{
2022-12-09 13:26:17 +02:00
CFunctionList < void ( ) > ony = std : : bind ( & BattleControlPanel : : reallyFlee , this ) ;
2022-12-13 13:58:16 +02:00
owner . curInt - > showYesNoDialog ( CGI - > generaltexth - > allTexts [ 28 ] , ony , nullptr ) ; //Are you sure you want to retreat?
2022-11-18 17:54:10 +02:00
}
else
{
std : : vector < std : : shared_ptr < CComponent > > comps ;
std : : string heroName ;
//calculating fleeing hero's name
2022-12-13 13:58:16 +02:00
if ( owner . attackingHeroInstance )
if ( owner . attackingHeroInstance - > tempOwner = = owner . curInt - > cb - > getMyColor ( ) )
heroName = owner . attackingHeroInstance - > name ;
if ( owner . defendingHeroInstance )
if ( owner . defendingHeroInstance - > tempOwner = = owner . curInt - > cb - > getMyColor ( ) )
heroName = owner . defendingHeroInstance - > name ;
2022-11-18 17:54:10 +02:00
//calculating text
auto txt = boost : : format ( CGI - > generaltexth - > allTexts [ 340 ] ) % heroName ; //The Shackles of War are present. %s can not retreat!
//printing message
2022-12-13 13:58:16 +02:00
owner . curInt - > showInfoDialog ( boost : : to_string ( txt ) , comps ) ;
2022-11-18 17:54:10 +02:00
}
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : reallyFlee ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
owner . giveCommand ( EActionType : : RETREAT ) ;
2022-11-18 17:54:10 +02:00
CCS - > curh - > changeGraphic ( ECursor : : ADVENTURE , 0 ) ;
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : reallySurrender ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . curInt - > cb - > getResourceAmount ( Res : : GOLD ) < owner . curInt - > cb - > battleGetSurrenderCost ( ) )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
owner . curInt - > showInfoDialog ( CGI - > generaltexth - > allTexts [ 29 ] ) ; //You don't have enough gold!
2022-11-18 17:54:10 +02:00
}
else
{
2022-12-13 13:58:16 +02:00
owner . giveCommand ( EActionType : : SURRENDER ) ;
2022-11-18 17:54:10 +02:00
CCS - > curh - > changeGraphic ( ECursor : : ADVENTURE , 0 ) ;
}
}
2022-12-20 01:28:20 +02:00
void BattleControlPanel : : showAlternativeActionIcon ( PossiblePlayerBattleAction action )
{
std : : string iconName = " icmalt00 " ;
switch ( action )
{
case PossiblePlayerBattleAction : : ATTACK :
iconName = " icmalt01 " ;
break ;
case PossiblePlayerBattleAction : : SHOOT :
iconName = " icmalt02 " ;
break ;
case PossiblePlayerBattleAction : : AIMED_SPELL_CREATURE :
iconName = " icmalt03 " ;
break ;
//case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
//iconName = "icmalt04";
//break;
case PossiblePlayerBattleAction : : ATTACK_AND_RETURN :
iconName = " icmalt05 " ;
break ;
case PossiblePlayerBattleAction : : WALK_AND_ATTACK :
iconName = " icmalt06 " ;
break ;
}
auto anim = std : : make_shared < CAnimation > ( iconName ) ;
bSwitchAction - > setImage ( anim , false ) ;
}
void BattleControlPanel : : setAlternativeActions ( const std : : list < PossiblePlayerBattleAction > & actions )
{
alternativeActions = actions ;
defaultAction = PossiblePlayerBattleAction : : INVALID ;
if ( alternativeActions . size ( ) > 1 )
defaultAction = alternativeActions . back ( ) ;
if ( ! alternativeActions . empty ( ) )
showAlternativeActionIcon ( alternativeActions . front ( ) ) ;
else
showAlternativeActionIcon ( defaultAction ) ;
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bAutofightf ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . actionsController - > spellcastingModeActive ( ) )
2022-11-18 17:54:10 +02:00
return ;
//Stop auto-fight mode
2022-12-13 13:58:16 +02:00
if ( owner . curInt - > isAutoFightOn )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
assert ( owner . curInt - > autofightingAI ) ;
owner . curInt - > isAutoFightOn = false ;
2022-11-18 17:54:10 +02:00
logGlobal - > trace ( " Stopping the autofight... " ) ;
}
2022-12-13 13:58:16 +02:00
else if ( ! owner . curInt - > autofightingAI )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
owner . curInt - > isAutoFightOn = true ;
2022-11-18 17:54:10 +02:00
blockUI ( true ) ;
auto ai = CDynLibHandler : : getNewBattleAI ( settings [ " server " ] [ " friendlyAI " ] . String ( ) ) ;
2022-12-23 14:40:45 +02:00
ai - > initBattleInterface ( owner . curInt - > env , owner . curInt - > cb ) ;
2022-12-13 13:58:16 +02:00
ai - > battleStart ( owner . army1 , owner . army2 , int3 ( 0 , 0 , 0 ) , owner . attackingHeroInstance , owner . defendingHeroInstance , owner . curInt - > cb - > battleGetMySide ( ) ) ;
owner . curInt - > autofightingAI = ai ;
owner . curInt - > cb - > registerBattleInterface ( ai ) ;
2022-11-18 17:54:10 +02:00
2022-12-13 13:58:16 +02:00
owner . requestAutofightingAIToTakeAction ( ) ;
2022-11-18 17:54:10 +02:00
}
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bSpellf ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . actionsController - > spellcastingModeActive ( ) )
2022-11-18 17:54:10 +02:00
return ;
2022-12-13 13:58:16 +02:00
if ( ! owner . myTurn )
2022-11-18 17:54:10 +02:00
return ;
2022-12-13 13:58:16 +02:00
auto myHero = owner . currentHero ( ) ;
2022-11-18 17:54:10 +02:00
if ( ! myHero )
return ;
CCS - > curh - > changeGraphic ( ECursor : : ADVENTURE , 0 ) ;
2022-12-13 13:58:16 +02:00
ESpellCastProblem : : ESpellCastProblem spellCastProblem = owner . curInt - > cb - > battleCanCastSpell ( myHero , spells : : Mode : : HERO ) ;
2022-11-18 17:54:10 +02:00
if ( spellCastProblem = = ESpellCastProblem : : OK )
{
2022-12-13 13:58:16 +02:00
GH . pushIntT < CSpellWindow > ( myHero , owner . curInt . get ( ) ) ;
2022-11-18 17:54:10 +02:00
}
else if ( spellCastProblem = = ESpellCastProblem : : MAGIC_IS_BLOCKED )
{
//TODO: move to spell mechanics, add more information to spell cast problem
//Handle Orb of Inhibition-like effects -> we want to display dialog with info, why casting is impossible
2022-12-13 13:58:16 +02:00
auto blockingBonus = owner . currentHero ( ) - > getBonusLocalFirst ( Selector : : type ( ) ( Bonus : : BLOCK_ALL_MAGIC ) ) ;
2022-11-18 17:54:10 +02:00
if ( ! blockingBonus )
return ;
if ( blockingBonus - > source = = Bonus : : ARTIFACT )
{
2022-12-01 23:58:01 +02:00
const auto artID = ArtifactID ( blockingBonus - > sid ) ;
2022-11-18 17:54:10 +02:00
//If we have artifact, put name of our hero. Otherwise assume it's the enemy.
//TODO check who *really* is source of bonus
2022-12-13 13:58:16 +02:00
std : : string heroName = myHero - > hasArt ( artID ) ? myHero - > name : owner . enemyHero ( ) . name ;
2022-11-18 17:54:10 +02:00
//%s wields the %s, an ancient artifact which creates a p dead to all magic.
LOCPLINT - > showInfoDialog ( boost : : str ( boost : : format ( CGI - > generaltexth - > allTexts [ 683 ] )
% heroName % CGI - > artifacts ( ) - > getByIndex ( artID ) - > getName ( ) ) ) ;
}
}
}
2022-12-20 01:28:20 +02:00
void BattleControlPanel : : bSwitchActionf ( )
{
if ( alternativeActions . empty ( ) )
return ;
if ( alternativeActions . front ( ) = = defaultAction )
{
alternativeActions . push_back ( alternativeActions . front ( ) ) ;
alternativeActions . pop_front ( ) ;
}
auto actions = owner . actionsController - > getPossibleActions ( ) ;
if ( ! actions . empty ( ) & & actions . front ( ) = = alternativeActions . front ( ) )
{
owner . actionsController - > removePossibleAction ( alternativeActions . front ( ) ) ;
showAlternativeActionIcon ( defaultAction ) ;
}
else
{
owner . actionsController - > pushFrontPossibleAction ( alternativeActions . front ( ) ) ;
showAlternativeActionIcon ( alternativeActions . front ( ) ) ;
}
alternativeActions . push_back ( alternativeActions . front ( ) ) ;
alternativeActions . pop_front ( ) ;
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bWaitf ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . actionsController - > spellcastingModeActive ( ) )
2022-11-18 17:54:10 +02:00
return ;
2022-12-13 13:58:16 +02:00
if ( owner . stacksController - > getActiveStack ( ) ! = nullptr )
owner . giveCommand ( EActionType : : WAIT ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bDefencef ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . actionsController - > spellcastingModeActive ( ) )
2022-11-18 17:54:10 +02:00
return ;
2022-12-13 13:58:16 +02:00
if ( owner . stacksController - > getActiveStack ( ) ! = nullptr )
owner . giveCommand ( EActionType : : DEFEND ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bConsoleUpf ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . actionsController - > spellcastingModeActive ( ) )
2022-11-18 17:54:10 +02:00
return ;
console - > scrollUp ( ) ;
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bConsoleDownf ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
if ( owner . actionsController - > spellcastingModeActive ( ) )
2022-11-18 17:54:10 +02:00
return ;
console - > scrollDown ( ) ;
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bTacticNextStack ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
owner . tacticNextStack ( nullptr ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : bTacticPhaseEnd ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
owner . tacticPhaseEnd ( ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-09 13:26:17 +02:00
void BattleControlPanel : : blockUI ( bool on )
2022-11-18 17:54:10 +02:00
{
bool canCastSpells = false ;
2022-12-13 13:58:16 +02:00
auto hero = owner . curInt - > cb - > battleGetMyHero ( ) ;
2022-11-18 17:54:10 +02:00
if ( hero )
{
2022-12-13 13:58:16 +02:00
ESpellCastProblem : : ESpellCastProblem spellcastingProblem = owner . curInt - > cb - > battleCanCastSpell ( hero , spells : : Mode : : HERO ) ;
2022-11-18 17:54:10 +02:00
//if magic is blocked, we leave button active, so the message can be displayed after button click
canCastSpells = spellcastingProblem = = ESpellCastProblem : : OK | | spellcastingProblem = = ESpellCastProblem : : MAGIC_IS_BLOCKED ;
}
2022-12-13 13:58:16 +02:00
bool canWait = owner . stacksController - > getActiveStack ( ) ? ! owner . stacksController - > getActiveStack ( ) - > waitedThisTurn : false ;
2022-11-18 17:54:10 +02:00
bOptions - > block ( on ) ;
2022-12-13 13:58:16 +02:00
bFlee - > block ( on | | ! owner . curInt - > cb - > battleCanFlee ( ) ) ;
bSurrender - > block ( on | | owner . curInt - > cb - > battleGetSurrenderCost ( ) < 0 ) ;
2022-11-18 17:54:10 +02:00
// block only if during enemy turn and auto-fight is off
// otherwise - crash on accessing non-exisiting active stack
2022-12-13 13:58:16 +02:00
bAutofight - > block ( ! owner . curInt - > isAutoFightOn & & ! owner . stacksController - > getActiveStack ( ) ) ;
2022-11-18 17:54:10 +02:00
2022-12-13 13:58:16 +02:00
if ( owner . tacticsMode & & btactEnd & & btactNext )
2022-11-18 17:54:10 +02:00
{
btactNext - > block ( on ) ;
btactEnd - > block ( on ) ;
}
else
{
bConsoleUp - > block ( on ) ;
bConsoleDown - > block ( on ) ;
}
2022-12-13 13:58:16 +02:00
bSpell - > block ( on | | owner . tacticsMode | | ! canCastSpells ) ;
bWait - > block ( on | | owner . tacticsMode | | ! canWait ) ;
bDefence - > block ( on | | owner . tacticsMode ) ;
2022-11-18 17:54:10 +02:00
}