2022-11-18 17:54:10 +02:00
/*
2022-12-21 17:06:47 +02:00
* BattleWindow . 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-21 17:06:47 +02:00
# include "BattleWindow.h"
2022-11-28 16:43:38 +02:00
2022-12-09 13:38:46 +02:00
# include "BattleInterface.h"
# include "BattleInterfaceClasses.h"
2022-12-21 17:02:53 +02:00
# include "BattleFieldController.h"
2022-12-09 13:38:46 +02:00
# 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"
2022-12-21 17:02:53 +02:00
# include "../CMusicHandler.h"
2023-01-05 19:34:37 +02:00
# include "../gui/CursorHandler.h"
2022-11-18 17:54:10 +02:00
# include "../gui/CGuiHandler.h"
# include "../windows/CSpellWindow.h"
2022-11-28 16:43:38 +02:00
# include "../widgets/Buttons.h"
# include "../widgets/Images.h"
2023-02-01 20:42:06 +02:00
# include "../windows/CMessage.h"
# include "../render/CAnimation.h"
# include "../render/Canvas.h"
# include "../adventureMap/CInGameConsole.h"
2022-11-28 16:43:38 +02:00
# 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-25 12:22:07 +02:00
# include "../../lib/filesystem/ResourceID.h"
2022-11-18 17:54:10 +02:00
2023-01-30 19:55:32 +02:00
# include <SDL_surface.h>
# include <SDL_events.h>
2022-12-21 17:06:47 +02:00
BattleWindow : : BattleWindow ( BattleInterface & owner ) :
2022-11-18 17:54:10 +02:00
owner ( owner )
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE ;
2022-12-21 17:02:53 +02:00
pos . w = 800 ;
pos . h = 600 ;
pos = center ( ) ;
2023-01-04 16:20:53 +02:00
REGISTER_BUILDER ( " battleConsole " , & BattleWindow : : buildBattleConsole ) ;
2022-12-25 12:22:07 +02:00
2023-01-04 16:20:53 +02:00
const JsonNode config ( ResourceID ( " config/widgets/BattleWindow.json " ) ) ;
2022-12-25 12:22:07 +02:00
2023-01-04 16:20:53 +02:00
addCallback ( " options " , std : : bind ( & BattleWindow : : bOptionsf , this ) ) ;
addCallback ( " surrender " , std : : bind ( & BattleWindow : : bSurrenderf , this ) ) ;
addCallback ( " flee " , std : : bind ( & BattleWindow : : bFleef , this ) ) ;
addCallback ( " autofight " , std : : bind ( & BattleWindow : : bAutofightf , this ) ) ;
addCallback ( " spellbook " , std : : bind ( & BattleWindow : : bSpellf , this ) ) ;
addCallback ( " wait " , std : : bind ( & BattleWindow : : bWaitf , this ) ) ;
addCallback ( " defence " , std : : bind ( & BattleWindow : : bDefencef , this ) ) ;
addCallback ( " consoleUp " , std : : bind ( & BattleWindow : : bConsoleUpf , this ) ) ;
addCallback ( " consoleDown " , std : : bind ( & BattleWindow : : bConsoleDownf , this ) ) ;
addCallback ( " tacticNext " , std : : bind ( & BattleWindow : : bTacticNextStack , this ) ) ;
addCallback ( " tacticEnd " , std : : bind ( & BattleWindow : : bTacticPhaseEnd , this ) ) ;
addCallback ( " alternativeAction " , std : : bind ( & BattleWindow : : bSwitchActionf , this ) ) ;
2022-12-25 12:22:07 +02:00
build ( config ) ;
console = widget < BattleConsole > ( " console " ) ;
2023-01-04 16:20:53 +02:00
2022-11-18 17:54:10 +02:00
GH . statusbar = console ;
2022-12-21 17:02:53 +02:00
owner . console = console ;
owner . fieldController . reset ( new BattleFieldController ( owner ) ) ;
owner . fieldController - > createHeroes ( ) ;
//create stack queue and adjust our own position
bool embedQueue ;
std : : string queueSize = settings [ " battle " ] [ " queueSize " ] . String ( ) ;
if ( queueSize = = " auto " )
embedQueue = screen - > h < 700 ;
else
embedQueue = screen - > h < 700 | | queueSize = = " small " ;
queue = std : : make_shared < StackQueue > ( embedQueue , owner ) ;
if ( ! embedQueue & & settings [ " battle " ] [ " showQueue " ] . Bool ( ) )
{
//re-center, taking into account stack queue position
pos . y - = queue - > pos . h ;
pos . h + = queue - > pos . h ;
pos = center ( ) ;
}
2022-11-18 17:54:10 +02:00
2022-12-13 13:58:16 +02:00
if ( owner . tacticsMode )
2022-11-18 17:54:10 +02:00
tacticPhaseStarted ( ) ;
else
tacticPhaseEnded ( ) ;
2022-12-21 17:02:53 +02:00
addUsedEvents ( RCLICK | KEYBOARD ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-21 18:04:19 +02:00
BattleWindow : : ~ BattleWindow ( )
{
CPlayerInterface : : battleInt = nullptr ;
}
2023-01-04 16:20:53 +02:00
std : : shared_ptr < BattleConsole > BattleWindow : : buildBattleConsole ( const JsonNode & config ) const
2022-11-27 02:26:02 +02:00
{
2023-01-04 16:20:53 +02:00
auto rect = readRect ( config [ " rect " ] ) ;
auto offset = readPosition ( config [ " imagePosition " ] ) ;
auto background = widget < CPicture > ( " menuBattle " ) ;
return std : : make_shared < BattleConsole > ( background , rect . topLeft ( ) , offset , rect . dimensions ( ) ) ;
2022-11-27 02:26:02 +02:00
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : hideQueue ( )
2022-11-27 02:26:02 +02:00
{
2022-12-21 17:02:53 +02:00
Settings showQueue = settings . write [ " battle " ] [ " showQueue " ] ;
showQueue - > Bool ( ) = false ;
queue - > disable ( ) ;
if ( ! queue - > embedded )
{
2023-01-04 16:40:38 +02:00
//re-center, taking into account stack queue position
pos . y + = queue - > pos . h ;
pos . h - = queue - > pos . h ;
pos = center ( ) ;
2022-12-21 17:02:53 +02:00
GH . totalRedraw ( ) ;
}
2022-11-27 02:26:02 +02:00
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : showQueue ( )
2022-11-27 02:26:02 +02:00
{
2022-12-21 17:02:53 +02:00
Settings showQueue = settings . write [ " battle " ] [ " showQueue " ] ;
showQueue - > Bool ( ) = true ;
queue - > enable ( ) ;
if ( ! queue - > embedded )
{
2023-01-04 16:40:38 +02:00
//re-center, taking into account stack queue position
pos . y - = queue - > pos . h ;
pos . h + = queue - > pos . h ;
pos = center ( ) ;
2022-12-21 17:02:53 +02:00
GH . totalRedraw ( ) ;
}
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : updateQueue ( )
2022-12-21 17:02:53 +02:00
{
queue - > update ( ) ;
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : activate ( )
2022-12-21 17:02:53 +02:00
{
2022-12-30 12:32:36 +02:00
GH . statusbar = console ;
2022-12-21 17:02:53 +02:00
CIntObject : : activate ( ) ;
LOCPLINT - > cingconsole - > activate ( ) ;
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : deactivate ( )
2022-12-21 17:02:53 +02:00
{
CIntObject : : deactivate ( ) ;
LOCPLINT - > cingconsole - > deactivate ( ) ;
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : keyPressed ( const SDL_KeyboardEvent & key )
2022-12-21 17:02:53 +02:00
{
if ( key . keysym . sym = = SDLK_q & & key . state = = SDL_PRESSED )
{
if ( settings [ " battle " ] [ " showQueue " ] . Bool ( ) ) //hide queue
hideQueue ( ) ;
else
showQueue ( ) ;
}
else if ( key . keysym . sym = = SDLK_f & & key . state = = SDL_PRESSED )
{
owner . actionsController - > enterCreatureCastingMode ( ) ;
}
else if ( key . keysym . sym = = SDLK_ESCAPE )
{
if ( owner . getAnimationCondition ( EAnimationEvents : : OPENING ) = = true )
CCS - > soundh - > stopSound ( owner . battleIntroSoundChannel ) ;
else
owner . actionsController - > endCastingSpell ( ) ;
}
2022-11-27 02:26:02 +02:00
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : clickRight ( tribool down , bool previousState )
2022-12-21 17:02:53 +02:00
{
if ( ! down )
owner . actionsController - > endCastingSpell ( ) ;
}
2022-11-27 02:26:02 +02:00
2022-12-21 17:06:47 +02:00
void BattleWindow : : tacticPhaseStarted ( )
2022-11-18 17:54:10 +02:00
{
2023-01-04 16:20:53 +02:00
auto menuBattle = widget < CIntObject > ( " menuBattle " ) ;
auto console = widget < CIntObject > ( " console " ) ;
auto menuTactics = widget < CIntObject > ( " menuTactics " ) ;
auto tacticNext = widget < CIntObject > ( " tacticNext " ) ;
auto tacticEnd = widget < CIntObject > ( " tacticEnd " ) ;
2022-12-21 17:02:53 +02:00
menuBattle - > disable ( ) ;
console - > disable ( ) ;
2022-11-18 17:54:10 +02:00
2022-12-21 17:02:53 +02:00
menuTactics - > enable ( ) ;
2023-01-04 16:20:53 +02:00
tacticNext - > enable ( ) ;
tacticEnd - > enable ( ) ;
2023-01-09 17:25:49 +02:00
redraw ( ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-21 17:02:53 +02:00
2022-12-21 17:06:47 +02:00
void BattleWindow : : tacticPhaseEnded ( )
2022-11-18 17:54:10 +02:00
{
2023-01-04 16:20:53 +02:00
auto menuBattle = widget < CIntObject > ( " menuBattle " ) ;
auto console = widget < CIntObject > ( " console " ) ;
auto menuTactics = widget < CIntObject > ( " menuTactics " ) ;
auto tacticNext = widget < CIntObject > ( " tacticNext " ) ;
auto tacticEnd = widget < CIntObject > ( " tacticEnd " ) ;
2022-12-21 17:02:53 +02:00
menuBattle - > enable ( ) ;
console - > enable ( ) ;
2022-11-20 22:56:42 +02:00
2022-12-21 17:02:53 +02:00
menuTactics - > disable ( ) ;
2023-01-04 16:20:53 +02:00
tacticNext - > disable ( ) ;
tacticEnd - > disable ( ) ;
2023-01-09 17:25:49 +02:00
redraw ( ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : 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 ;
2022-12-18 22:32:07 +02:00
CCS - > curh - > set ( Cursor : : Map : : POINTER ) ;
2022-11-18 17:54:10 +02:00
2022-12-09 13:26:17 +02:00
GH . pushIntT < BattleOptionsWindow > ( owner ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : 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-21 17:06:47 +02:00
void BattleWindow : : 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-21 17:06:47 +02:00
CFunctionList < void ( ) > ony = std : : bind ( & BattleWindow : : 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 ( ) )
2023-01-02 13:27:03 +02:00
heroName = owner . attackingHeroInstance - > getNameTranslated ( ) ;
2022-12-13 13:58:16 +02:00
if ( owner . defendingHeroInstance )
if ( owner . defendingHeroInstance - > tempOwner = = owner . curInt - > cb - > getMyColor ( ) )
2023-01-02 13:27:03 +02:00
heroName = owner . defendingHeroInstance - > getNameTranslated ( ) ;
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-21 17:06:47 +02:00
void BattleWindow : : reallyFlee ( )
2022-11-18 17:54:10 +02:00
{
2022-12-13 13:58:16 +02:00
owner . giveCommand ( EActionType : : RETREAT ) ;
2022-12-18 22:32:07 +02:00
CCS - > curh - > set ( Cursor : : Map : : POINTER ) ;
2022-11-18 17:54:10 +02:00
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : 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-12-18 22:32:07 +02:00
CCS - > curh - > set ( Cursor : : Map : : POINTER ) ;
2022-11-18 17:54:10 +02:00
}
}
2023-01-04 16:20:53 +02:00
void BattleWindow : : showAlternativeActionIcon ( PossiblePlayerBattleAction action )
2022-12-20 01:28:20 +02:00
{
2022-12-25 12:22:07 +02:00
auto w = widget < CButton > ( " alternativeAction " ) ;
if ( ! w )
return ;
std : : string iconName = variables [ " actionIconDefault " ] . String ( ) ;
2022-12-20 01:28:20 +02:00
switch ( action )
{
case PossiblePlayerBattleAction : : ATTACK :
2022-12-25 12:22:07 +02:00
iconName = variables [ " actionIconAttack " ] . String ( ) ;
2022-12-20 01:28:20 +02:00
break ;
case PossiblePlayerBattleAction : : SHOOT :
2022-12-25 12:22:07 +02:00
iconName = variables [ " actionIconShoot " ] . String ( ) ;
2022-12-20 01:28:20 +02:00
break ;
case PossiblePlayerBattleAction : : AIMED_SPELL_CREATURE :
2022-12-25 12:22:07 +02:00
iconName = variables [ " actionIconSpell " ] . String ( ) ;
2022-12-20 01:28:20 +02:00
break ;
2022-12-25 12:22:07 +02:00
//TODO: figure out purpose of this icon
//case PossiblePlayerBattleAction::???:
//iconName = variables["actionIconWalk"].String();
2022-12-20 01:28:20 +02:00
//break;
case PossiblePlayerBattleAction : : ATTACK_AND_RETURN :
2022-12-25 12:22:07 +02:00
iconName = variables [ " actionIconReturn " ] . String ( ) ;
2022-12-20 01:28:20 +02:00
break ;
case PossiblePlayerBattleAction : : WALK_AND_ATTACK :
2022-12-25 12:22:07 +02:00
iconName = variables [ " actionIconNoReturn " ] . String ( ) ;
2022-12-20 01:28:20 +02:00
break ;
}
auto anim = std : : make_shared < CAnimation > ( iconName ) ;
2022-12-25 12:22:07 +02:00
w - > setImage ( anim , false ) ;
2022-12-20 01:28:20 +02:00
}
2023-01-04 16:20:53 +02:00
void BattleWindow : : setAlternativeActions ( const std : : list < PossiblePlayerBattleAction > & actions )
2022-12-20 01:28:20 +02:00
{
alternativeActions = actions ;
defaultAction = PossiblePlayerBattleAction : : INVALID ;
if ( alternativeActions . size ( ) > 1 )
defaultAction = alternativeActions . back ( ) ;
if ( ! alternativeActions . empty ( ) )
showAlternativeActionIcon ( alternativeActions . front ( ) ) ;
else
showAlternativeActionIcon ( defaultAction ) ;
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : 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-21 17:06:47 +02:00
void BattleWindow : : 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 ;
2023-01-21 16:15:20 +02:00
if ( ! owner . makingTurn ( ) )
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 ;
2022-12-18 22:32:07 +02:00
CCS - > curh - > set ( Cursor : : Map : : POINTER ) ;
2022-11-18 17:54:10 +02:00
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
2023-01-02 13:27:03 +02:00
std : : string heroName = myHero - > hasArt ( artID ) ? myHero - > getNameTranslated ( ) : 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 ] )
2023-01-02 15:58:56 +02:00
% heroName % CGI - > artifacts ( ) - > getByIndex ( artID ) - > getNameTranslated ( ) ) ) ;
2022-11-18 17:54:10 +02:00
}
}
}
2023-01-04 16:20:53 +02:00
void BattleWindow : : bSwitchActionf ( )
2022-12-20 01:28:20 +02:00
{
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-21 17:06:47 +02:00
void BattleWindow : : 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-21 17:06:47 +02:00
void BattleWindow : : 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-21 17:06:47 +02:00
void BattleWindow : : 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-21 17:06:47 +02:00
void BattleWindow : : 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-21 17:06:47 +02:00
void BattleWindow : : 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-21 17:06:47 +02:00
void BattleWindow : : 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-21 17:06:47 +02:00
void BattleWindow : : 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
2022-12-25 12:22:07 +02:00
if ( auto w = widget < CButton > ( " options " ) )
w - > block ( on ) ;
if ( auto w = widget < CButton > ( " flee " ) )
w - > block ( on | | ! owner . curInt - > cb - > battleCanFlee ( ) ) ;
if ( auto w = widget < CButton > ( " surrender " ) )
w - > block ( on | | owner . curInt - > cb - > battleGetSurrenderCost ( ) < 0 ) ;
if ( auto w = widget < CButton > ( " cast " ) )
w - > block ( on | | owner . tacticsMode | | ! canCastSpells ) ;
if ( auto w = widget < CButton > ( " wait " ) )
w - > block ( on | | owner . tacticsMode | | ! canWait ) ;
if ( auto w = widget < CButton > ( " defence " ) )
w - > block ( on | | owner . tacticsMode ) ;
if ( auto w = widget < CButton > ( " alternativeAction " ) )
w - > block ( on | | owner . tacticsMode ) ;
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-25 12:22:07 +02:00
if ( auto w = widget < CButton > ( " options " ) )
w - > block ( ! owner . curInt - > isAutoFightOn & & ! owner . stacksController - > getActiveStack ( ) ) ;
2022-11-18 17:54:10 +02:00
2022-12-25 12:22:07 +02:00
auto btactEnd = widget < CButton > ( " tacticEnd " ) ;
auto btactNext = widget < CButton > ( " tacticNext " ) ;
if ( owner . tacticsMode & & btactEnd & & btactNext )
2022-11-18 17:54:10 +02:00
{
btactNext - > block ( on ) ;
btactEnd - > block ( on ) ;
}
else
{
2022-12-25 12:22:07 +02:00
auto bConsoleUp = widget < CButton > ( " consoleUp " ) ;
auto bConsoleDown = widget < CButton > ( " consoleDown " ) ;
if ( bConsoleUp & & bConsoleDown )
{
bConsoleUp - > block ( on ) ;
bConsoleDown - > block ( on ) ;
}
2022-11-18 17:54:10 +02:00
}
}
2022-12-21 17:02:53 +02:00
2022-12-21 17:06:47 +02:00
void BattleWindow : : showAll ( SDL_Surface * to )
2022-12-21 17:02:53 +02:00
{
CIntObject : : showAll ( to ) ;
if ( screen - > w ! = 800 | | screen - > h ! = 600 )
CMessage : : drawBorder ( owner . curInt - > playerID , to , pos . w + 28 , pos . h + 29 , pos . x - 14 , pos . y - 15 ) ;
}
2022-12-21 17:06:47 +02:00
void BattleWindow : : show ( SDL_Surface * to )
2022-12-21 17:02:53 +02:00
{
CIntObject : : show ( to ) ;
LOCPLINT - > cingconsole - > show ( to ) ;
}
2023-01-04 16:20:53 +02:00
void BattleWindow : : close ( )
{
if ( GH . topInt ( ) . get ( ) ! = this )
logGlobal - > error ( " Only top interface must be closed " ) ;
GH . popInts ( 1 ) ;
}