1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-04 09:42:40 +02:00
vcmi/client/battle/BattleInterface.cpp

767 lines
22 KiB
C++
Raw Normal View History

/*
* BattleInterface.cpp, part of VCMI engine
*
* 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
*
*/
2011-12-14 00:35:28 +03:00
#include "StdInc.h"
#include "BattleInterface.h"
2011-12-14 00:35:28 +03:00
#include "BattleAnimationClasses.h"
#include "BattleActionsController.h"
#include "BattleInterfaceClasses.h"
#include "CreatureAnimation.h"
#include "BattleProjectileController.h"
#include "BattleEffectsController.h"
#include "BattleObstacleController.h"
#include "BattleSiegeController.h"
#include "BattleFieldController.h"
#include "BattleWindow.h"
#include "BattleStacksController.h"
#include "BattleRenderer.h"
#include "../CGameInfo.h"
2011-12-14 00:35:28 +03:00
#include "../CMessage.h"
#include "../CMusicHandler.h"
#include "../CPlayerInterface.h"
#include "../gui/Canvas.h"
#include "../gui/CursorHandler.h"
#include "../gui/CGuiHandler.h"
#include "../windows/CAdvmapInterface.h"
2011-12-14 00:35:28 +03:00
#include "../../CCallback.h"
#include "../../lib/CStack.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/CGeneralTextHandler.h"
#include "../../lib/CHeroHandler.h"
2011-12-14 00:35:28 +03:00
#include "../../lib/CondSh.h"
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/NetPacks.h"
#include "../../lib/UnlockGuard.h"
#include "../../lib/TerrainHandler.h"
CondSh<BattleAction *> BattleInterface::givenCommand(nullptr);
BattleInterface::BattleInterface(const CCreatureSet *army1, const CCreatureSet *army2,
const CGHeroInstance *hero1, const CGHeroInstance *hero2,
std::shared_ptr<CPlayerInterface> att,
std::shared_ptr<CPlayerInterface> defen,
std::shared_ptr<CPlayerInterface> spectatorInt)
: attackingHeroInstance(hero1)
, defendingHeroInstance(hero2)
, attackerInt(att)
, defenderInt(defen)
, curInt(att)
, myTurn(false)
, moveSoundHander(-1)
{
for ( auto & event : animationEvents)
event.setn(false);
if(spectatorInt)
{
curInt = spectatorInt;
}
else if(!curInt)
{
//May happen when we are defending during network MP game -> attacker interface is just not present
curInt = defenderInt;
}
2010-01-28 19:23:01 +02:00
givenCommand.setn(nullptr);
2010-01-28 19:23:01 +02:00
//hot-seat -> check tactics for both players (defender may be local human)
if(attackerInt && attackerInt->cb->battleGetTacticDist())
2011-12-14 00:35:28 +03:00
tacticianInterface = attackerInt;
else if(defenderInt && defenderInt->cb->battleGetTacticDist())
2011-12-14 00:35:28 +03:00
tacticianInterface = defenderInt;
2010-01-28 19:23:01 +02:00
//if we found interface of player with tactics, then enter tactics mode
tacticsMode = static_cast<bool>(tacticianInterface);
2010-01-28 19:23:01 +02:00
2011-12-14 00:35:28 +03:00
//initializing armies
this->army1 = army1;
this->army2 = army2;
2022-12-13 13:58:16 +02:00
const CGTownInstance *town = curInt->cb->battleGetDefendedTown();
if(town && town->hasFort())
siegeController.reset(new BattleSiegeController(*this, town));
windowObject = std::make_shared<BattleWindow>(*this);
2022-12-13 13:58:16 +02:00
projectilesController.reset(new BattleProjectileController(*this));
stacksController.reset( new BattleStacksController(*this));
actionsController.reset( new BattleActionsController(*this));
effectsController.reset(new BattleEffectsController(*this));
obstacleController.reset(new BattleObstacleController(*this));
2011-12-14 00:35:28 +03:00
CCS->musich->stopMusic();
setAnimationCondition(EAnimationEvents::OPENING, true);
battleIntroSoundChannel = CCS->soundh->playSoundFromSet(CCS->soundh->battleIntroSounds);
auto onIntroPlayed = [this]()
{
if(LOCPLINT->battleInt)
{
boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim);
onIntroSoundPlayed();
}
};
GH.pushInt(windowObject);
windowObject->blockUI(true);
windowObject->updateQueue();
if (battleIntroSoundChannel != -1)
CCS->soundh->setCallback(battleIntroSoundChannel, onIntroPlayed);
else
onIntroSoundPlayed();
}
void BattleInterface::onIntroSoundPlayed()
{
setAnimationCondition(EAnimationEvents::OPENING, false);
CCS->musich->playMusicFromSet("battle", true, true);
if(tacticsMode)
tacticNextStack(nullptr);
activateStack();
battleIntroSoundChannel = -1;
}
BattleInterface::~BattleInterface()
{
CPlayerInterface::battleInt = nullptr;
givenCommand.cond.notify_all(); //that two lines should make any stacksController->getActiveStack() waiting thread to finish
2016-10-28 23:37:20 +02:00
if (adventureInt && adventureInt->selection)
2011-12-14 00:35:28 +03:00
{
//FIXME: this should be moved to adventureInt which should restore correct track based on selection/active player
const auto * terrain = LOCPLINT->cb->getTile(adventureInt->selection->visitablePos())->terType;
CCS->musich->playMusicFromSet("terrain", terrain->getJsonKey(), true, false);
2022-12-16 22:37:44 +02:00
}
// may happen if user decided to close game while in battle
if (getAnimationCondition(EAnimationEvents::ACTION) == true)
logGlobal->error("Shutting down BattleInterface during animation playback!");
setAnimationCondition(EAnimationEvents::ACTION, false);
}
void BattleInterface::setPrintCellBorders(bool set)
{
Settings cellBorders = settings.write["battle"]["cellBorders"];
cellBorders->Bool() = set;
fieldController->redrawBackgroundWithHexes();
2011-12-14 00:35:28 +03:00
GH.totalRedraw();
}
void BattleInterface::setPrintStackRange(bool set)
2011-12-14 00:35:28 +03:00
{
Settings stackRange = settings.write["battle"]["stackRange"];
stackRange->Bool() = set;
fieldController->redrawBackgroundWithHexes();
2011-12-14 00:35:28 +03:00
GH.totalRedraw();
}
void BattleInterface::setPrintMouseShadow(bool set)
{
Settings shadow = settings.write["battle"]["mouseShadow"];
shadow->Bool() = set;
2011-12-14 00:35:28 +03:00
}
void BattleInterface::stackReset(const CStack * stack)
2011-12-14 00:35:28 +03:00
{
stacksController->stackReset(stack);
}
void BattleInterface::stackAdded(const CStack * stack)
{
stacksController->stackAdded(stack, false);
2011-12-14 00:35:28 +03:00
}
void BattleInterface::stackRemoved(uint32_t stackID)
2011-12-14 00:35:28 +03:00
{
stacksController->stackRemoved(stackID);
fieldController->redrawBackgroundWithHexes();
windowObject->updateQueue();
2011-12-14 00:35:28 +03:00
}
void BattleInterface::stackActivated(const CStack *stack)
2011-12-14 00:35:28 +03:00
{
stacksController->stackActivated(stack);
2011-12-14 00:35:28 +03:00
}
void BattleInterface::stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance, bool teleport)
2011-12-14 00:35:28 +03:00
{
if (teleport)
stacksController->stackTeleported(stack, destHex, distance);
else
stacksController->stackMoved(stack, destHex, distance);
2011-12-14 00:35:28 +03:00
}
void BattleInterface::stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos)
2011-12-14 00:35:28 +03:00
{
stacksController->stacksAreAttacked(attackedInfos);
std::array<int, 2> killedBySide = {0, 0};
for(const StackAttackedInfo & attackedInfo : attackedInfos)
2009-11-08 16:44:58 +02:00
{
ui8 side = attackedInfo.defender->side;
killedBySide.at(side) += attackedInfo.amountKilled;
}
for(ui8 side = 0; side < 2; side++)
{
if(killedBySide.at(side) > killedBySide.at(1-side))
setHeroAnimation(side, EHeroAnimType::DEFEAT);
else if(killedBySide.at(side) < killedBySide.at(1-side))
setHeroAnimation(side, EHeroAnimType::VICTORY);
2009-11-08 16:44:58 +02:00
}
}
void BattleInterface::stackAttacking( const StackAttackInfo & attackInfo )
{
stacksController->stackAttacking(attackInfo);
2011-12-14 00:35:28 +03:00
}
void BattleInterface::newRoundFirst( int round )
2011-12-14 00:35:28 +03:00
{
waitForAnimationCondition(EAnimationEvents::OPENING, false);
2011-12-14 00:35:28 +03:00
}
void BattleInterface::newRound(int number)
2011-12-14 00:35:28 +03:00
{
console->addText(CGI->generaltexth->allTexts[412]);
2011-12-14 00:35:28 +03:00
}
void BattleInterface::giveCommand(EActionType action, BattleHex tile, si32 additional)
2011-12-14 00:35:28 +03:00
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
const CStack * actor = nullptr;
if(action != EActionType::HERO_SPELL && action != EActionType::RETREAT && action != EActionType::SURRENDER)
2011-12-14 00:35:28 +03:00
{
actor = stacksController->getActiveStack();
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
auto side = curInt->cb->playerToSide(curInt->playerID);
if(!side)
{
logGlobal->error("Player %s is not in battle", curInt->playerID.getStr());
return;
}
auto ba = new BattleAction(); //is deleted in CPlayerInterface::stacksController->getActiveStack()()
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
ba->side = side.get();
2011-12-14 00:35:28 +03:00
ba->actionType = action;
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
ba->aimToHex(tile);
ba->actionSubtype = additional;
sendCommand(ba, actor);
}
void BattleInterface::sendCommand(BattleAction *& command, const CStack * actor)
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
{
command->stackNumber = actor ? actor->unitId() : ((command->side == BattleSide::ATTACKER) ? -1 : -2);
if(!tacticsMode)
2010-03-02 13:40:29 +02:00
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
logGlobal->trace("Setting command for %s", (actor ? actor->nodeName() : "hero"));
2011-12-14 00:35:28 +03:00
myTurn = false;
stacksController->setActiveStack(nullptr);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
givenCommand.setn(command);
2011-12-14 00:35:28 +03:00
}
else
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
curInt->cb->battleMakeTacticAction(command);
vstd::clear_pointer(command);
stacksController->setActiveStack(nullptr);
//next stack will be activated when action ends
2010-03-02 13:40:29 +02:00
}
}
const CGHeroInstance * BattleInterface::getActiveHero()
2011-12-14 00:35:28 +03:00
{
const CStack *attacker = stacksController->getActiveStack();
if(!attacker)
{
return nullptr;
2011-12-14 00:35:28 +03:00
}
if(attacker->side == BattleSide::ATTACKER)
2011-12-14 00:35:28 +03:00
{
return attackingHeroInstance;
}
2011-12-14 00:35:28 +03:00
return defendingHeroInstance;
}
void BattleInterface::stackIsCatapulting(const CatapultAttack & ca)
{
if (siegeController)
siegeController->stackIsCatapulting(ca);
}
void BattleInterface::gateStateChanged(const EGateState state)
{
if (siegeController)
siegeController->gateStateChanged(state);
}
void BattleInterface::battleFinished(const BattleResult& br)
{
assert(getAnimationCondition(EAnimationEvents::ACTION) == false);
waitForAnimationCondition(EAnimationEvents::ACTION, false);
stacksController->setActiveStack(nullptr);
CCS->curh->set(Cursor::Map::POINTER);
curInt->waitWhileDialog();
2022-12-30 00:52:23 +02:00
if(settings["session"]["spectate"].Bool() && settings["session"]["spectate-skip-battle-result"].Bool())
{
windowObject->close();
return;
}
GH.pushInt(std::make_shared<BattleResultWindow>(br, *(this->curInt)));
curInt->waitWhileDialog(); // Avoid freeze when AI end turn after battle. Check bug #1897
CPlayerInterface::battleInt = nullptr;
}
void BattleInterface::spellCast(const BattleSpellCast * sc)
{
windowObject->blockUI(true);
2022-12-12 21:17:36 +02:00
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
const SpellID spellID = sc->spellID;
const CSpell * spell = spellID.toSpell();
auto targetedTile = sc->tile;
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
assert(spell);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
if(!spell)
return;
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
const std::string & castSoundPath = spell->getCastSound();
2015-02-07 10:13:24 +02:00
2016-10-28 23:37:20 +02:00
if (!castSoundPath.empty())
{
auto group = spell->animationInfo.projectile.empty() ?
EAnimationEvents::HIT:
EAnimationEvents::BEFORE_HIT;//FIXME: recheck whether this should be on projectile spawning
executeOnAnimationCondition(group, true, [=]() {
CCS->soundh->playSound(castSoundPath);
});
}
2014-11-27 20:06:11 +02:00
if ( sc->activeCast )
2014-11-27 20:06:11 +02:00
{
const CStack * casterStack = curInt->cb->battleGetStackByID(sc->casterStack);
if(casterStack != nullptr )
2014-11-27 20:06:11 +02:00
{
executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]()
{
2022-12-12 22:04:25 +02:00
stacksController->addNewAnim(new CastAnimation(*this, casterStack, targetedTile, curInt->cb->battleGetStackByPos(targetedTile), spell));
displaySpellCast(spell, casterStack->getPosition());
});
}
else
{
auto hero = sc->side ? defendingHero : attackingHero;
assert(hero);
2017-09-05 19:45:29 +02:00
executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]()
{
stacksController->addNewAnim(new HeroCastAnimation(*this, hero, targetedTile, curInt->cb->battleGetStackByPos(targetedTile), spell));
});
2014-11-26 23:27:38 +02:00
}
2015-03-22 11:18:58 +02:00
}
executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
displaySpellHit(spell, targetedTile);
});
2015-03-22 11:18:58 +02:00
//queuing affect animation
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
for(auto & elem : sc->affectedCres)
2014-11-26 23:27:38 +02:00
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
auto stack = curInt->cb->battleGetStackByID(elem, false);
assert(stack);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
if(stack)
{
executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
displaySpellEffect(spell, stack->getPosition());
});
}
}
2015-02-07 10:13:24 +02:00
for(auto & elem : sc->reflectedCres)
{
auto stack = curInt->cb->battleGetStackByID(elem, false);
assert(stack);
executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
effectsController->displayEffect(EBattleEffect::MAGIC_MIRROR, stack->getPosition());
});
}
2023-01-07 14:27:12 +02:00
if (!sc->resistedCres.empty())
{
executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
CCS->soundh->playSound("MAGICRES");
});
}
for(auto & elem : sc->resistedCres)
{
auto stack = curInt->cb->battleGetStackByID(elem, false);
assert(stack);
executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
effectsController->displayEffect(EBattleEffect::RESISTANCE, stack->getPosition());
});
2014-11-26 23:27:38 +02:00
}
2011-12-14 00:35:28 +03:00
//mana absorption
2016-10-28 23:37:20 +02:00
if (sc->manaGained > 0)
2011-12-14 00:35:28 +03:00
{
Point leftHero = Point(15, 30);
Point rightHero = Point(755, 30);
bool side = sc->side;
executeOnAnimationCondition(EAnimationEvents::AFTER_HIT, true, [=](){
stacksController->addNewAnim(new EffectAnimation(*this, side ? "SP07_A.DEF" : "SP07_B.DEF", leftHero));
stacksController->addNewAnim(new EffectAnimation(*this, side ? "SP07_B.DEF" : "SP07_A.DEF", rightHero));
});
}
}
void BattleInterface::battleStacksEffectsSet(const SetStackEffect & sse)
{
if(stacksController->getActiveStack() != nullptr)
fieldController->redrawBackgroundWithHexes();
2011-12-14 00:35:28 +03:00
}
2022-12-12 23:31:18 +02:00
void BattleInterface::setHeroAnimation(ui8 side, EHeroAnimType phase)
{
if(side == BattleSide::ATTACKER)
2017-10-07 23:04:43 +02:00
{
if(attackingHero)
attackingHero->setPhase(phase);
}
else
2017-10-07 23:04:43 +02:00
{
if(defendingHero)
defendingHero->setPhase(phase);
}
}
void BattleInterface::displayBattleLog(const std::vector<MetaString> & battleLog)
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
{
for(const auto & line : battleLog)
{
std::string formatted = line.toString();
boost::algorithm::trim(formatted);
appendBattleLog(formatted);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
}
}
void BattleInterface::displaySpellAnimationQueue(const CSpell * spell, const CSpell::TAnimationQueue & q, BattleHex destinationTile, bool isHit)
2015-03-22 15:47:20 +02:00
{
2021-02-20 03:57:50 +02:00
for(const CSpell::TAnimation & animation : q)
2015-03-22 15:47:20 +02:00
{
if(animation.pause > 0)
2022-12-12 22:04:25 +02:00
stacksController->addNewAnim(new DummyAnimation(*this, animation.pause));
if (!animation.effectName.empty())
{
const CStack * destStack = getCurrentPlayerInterface()->cb->battleGetStackByPos(destinationTile, false);
if (destStack)
stacksController->addNewAnim(new ColorTransformAnimation(*this, destStack, animation.effectName, spell ));
}
if(!animation.resourceName.empty())
{
int flags = 0;
if (isHit)
flags |= EffectAnimation::FORCE_ON_TOP;
if (animation.verticalPosition == VerticalPosition::BOTTOM)
flags |= EffectAnimation::ALIGN_TO_BOTTOM;
if (!destinationTile.isValid())
flags |= EffectAnimation::SCREEN_FILL;
if (!destinationTile.isValid())
stacksController->addNewAnim(new EffectAnimation(*this, animation.resourceName, flags));
else
stacksController->addNewAnim(new EffectAnimation(*this, animation.resourceName, destinationTile, flags));
}
}
2015-03-22 15:47:20 +02:00
}
void BattleInterface::displaySpellCast(const CSpell * spell, BattleHex destinationTile)
{
2021-02-20 03:57:50 +02:00
if(spell)
displaySpellAnimationQueue(spell, spell->animationInfo.cast, destinationTile, false);
2021-02-20 03:57:50 +02:00
}
void BattleInterface::displaySpellEffect(const CSpell * spell, BattleHex destinationTile)
2021-02-20 03:57:50 +02:00
{
if(spell)
displaySpellAnimationQueue(spell, spell->animationInfo.affect, destinationTile, false);
}
void BattleInterface::displaySpellHit(const CSpell * spell, BattleHex destinationTile)
{
2021-02-20 03:57:50 +02:00
if(spell)
displaySpellAnimationQueue(spell, spell->animationInfo.hit, destinationTile, true);
}
void BattleInterface::setAnimSpeed(int set)
2011-12-14 00:35:28 +03:00
{
Settings speed = settings.write["battle"]["animationSpeed"];
speed->Float() = float(set);
2011-12-14 00:35:28 +03:00
}
int BattleInterface::getAnimSpeed() const
2011-12-14 00:35:28 +03:00
{
if(settings["session"]["spectate"].Bool() && !settings["session"]["spectate-battle-speed"].isNull())
return static_cast<int>(vstd::round(settings["session"]["spectate-battle-speed"].Float() *100));
return static_cast<int>(vstd::round(settings["battle"]["animationSpeed"].Float() *100));
}
CPlayerInterface *BattleInterface::getCurrentPlayerInterface() const
{
return curInt.get();
}
void BattleInterface::trySetActivePlayer( PlayerColor player )
{
if ( attackerInt && attackerInt->playerID == player )
curInt = attackerInt;
if ( defenderInt && defenderInt->playerID == player )
curInt = defenderInt;
}
void BattleInterface::activateStack()
{
stacksController->activateStack();
2013-11-02 23:07:45 +03:00
const CStack * s = stacksController->getActiveStack();
if(!s)
return;
myTurn = true;
windowObject->updateQueue();
windowObject->blockUI(false);
fieldController->redrawBackgroundWithHexes();
actionsController->activateStack();
GH.fakeMouseMove();
2011-12-14 00:35:28 +03:00
}
void BattleInterface::endAction(const BattleAction* action)
{
2016-10-28 23:37:20 +02:00
const CStack *stack = curInt->cb->battleGetStackByID(action->stackNumber);
stacksController->endAction(action);
windowObject->updateQueue();
2011-12-14 00:35:28 +03:00
2022-12-12 21:17:36 +02:00
//stack ended movement in tactics phase -> select the next one
if (tacticsMode)
tacticNextStack(stack);
2022-12-12 21:17:36 +02:00
//we have activated next stack after sending request that has been just realized -> blockmap due to movement has changed
if(action->actionType == EActionType::HERO_SPELL)
fieldController->redrawBackgroundWithHexes();
}
void BattleInterface::appendBattleLog(const std::string & newEntry)
{
console->addText(newEntry);
}
void BattleInterface::startAction(const BattleAction* action)
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
if(action->actionType == EActionType::END_TACTIC_PHASE)
{
windowObject->tacticPhaseEnded();
2011-12-14 00:35:28 +03:00
return;
}
2011-12-14 00:35:28 +03:00
const CStack *stack = curInt->cb->battleGetStackByID(action->stackNumber);
2016-10-28 23:37:20 +02:00
if (stack)
{
windowObject->updateQueue();
}
2011-12-14 00:35:28 +03:00
else
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
assert(action->actionType == EActionType::HERO_SPELL); //only cast spell is valid action without acting stack number
}
stacksController->startAction(action);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
if(action->actionType == EActionType::HERO_SPELL) //when hero casts spell
2011-12-14 00:35:28 +03:00
return;
2016-10-28 23:37:20 +02:00
if (!stack)
{
2017-08-11 13:38:10 +02:00
logGlobal->error("Something wrong with stackNumber in actionStarted. Stack number: %d", action->stackNumber);
2011-12-14 00:35:28 +03:00
return;
}
2022-11-24 16:30:04 +02:00
effectsController->startAction(action);
2011-12-14 00:35:28 +03:00
}
void BattleInterface::tacticPhaseEnd()
{
stacksController->setActiveStack(nullptr);
2011-12-14 00:35:28 +03:00
tacticsMode = false;
}
2011-12-14 00:35:28 +03:00
static bool immobile(const CStack *s)
{
2011-12-14 00:35:28 +03:00
return !s->Speed(0, true); //should bound stacks be immobile?
}
void BattleInterface::tacticNextStack(const CStack * current)
{
2016-10-28 23:37:20 +02:00
if (!current)
current = stacksController->getActiveStack();
2012-03-01 14:57:38 +03:00
//no switching stacks when the current one is moving
assert(getAnimationCondition(EAnimationEvents::ACTION) == false);
waitForAnimationCondition(EAnimationEvents::ACTION, false);
2012-03-01 14:57:38 +03:00
2011-12-14 00:35:28 +03:00
TStacks stacksOfMine = tacticianInterface->cb->battleGetStacks(CBattleCallback::ONLY_MINE);
2016-10-28 23:37:20 +02:00
vstd::erase_if (stacksOfMine, &immobile);
if (stacksOfMine.empty())
{
tacticPhaseEnd();
return;
}
auto it = vstd::find(stacksOfMine, current);
2016-10-28 23:37:20 +02:00
if (it != stacksOfMine.end() && ++it != stacksOfMine.end())
2011-12-14 00:35:28 +03:00
stackActivated(*it);
else
stackActivated(stacksOfMine.front());
}
void BattleInterface::obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> oi)
{
obstacleController->obstaclePlaced(oi);
}
const CGHeroInstance *BattleInterface::currentHero() const
{
2022-12-13 13:58:16 +02:00
if (attackingHeroInstance && attackingHeroInstance->tempOwner == curInt->playerID)
return attackingHeroInstance;
2022-12-13 13:58:16 +02:00
if (defendingHeroInstance && defendingHeroInstance->tempOwner == curInt->playerID)
return defendingHeroInstance;
2022-12-13 13:58:16 +02:00
return nullptr;
}
InfoAboutHero BattleInterface::enemyHero() const
{
InfoAboutHero ret;
2016-10-28 23:37:20 +02:00
if (attackingHeroInstance->tempOwner == curInt->playerID)
curInt->cb->getHeroInfo(defendingHeroInstance, ret);
else
curInt->cb->getHeroInfo(attackingHeroInstance, ret);
return ret;
}
void BattleInterface::requestAutofightingAIToTakeAction()
{
assert(curInt->isAutoFightOn);
boost::thread aiThread([&]()
{
auto ba = std::make_unique<BattleAction>(curInt->autofightingAI->activeStack(stacksController->getActiveStack()));
2022-04-04 09:16:32 +02:00
if(curInt->cb->battleIsFinished())
{
return; // battle finished with spellcast
}
2016-10-28 23:37:20 +02:00
if (curInt->isAutoFightOn)
{
2016-10-28 23:37:20 +02:00
if (tacticsMode)
{
// Always end tactics mode. Player interface is blocked currently, so it's not possible that
// the AI can take any action except end tactics phase (AI actions won't be triggered)
//TODO implement the possibility that the AI will be triggered for further actions
//TODO any solution to merge tactics phase & normal phase in the way it is handled by the player and battle interface?
stacksController->setActiveStack(nullptr);
tacticsMode = false;
}
else
{
givenCommand.setn(ba.release());
}
}
else
{
2016-11-25 21:12:22 +02:00
boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim);
activateStack();
}
});
2013-06-24 17:35:27 +03:00
aiThread.detach();
}
void BattleInterface::castThisSpell(SpellID spellID)
{
actionsController->castThisSpell(spellID);
}
void BattleInterface::setAnimationCondition( EAnimationEvents event, bool state)
{
2022-12-16 22:37:44 +02:00
logAnim->debug("setAnimationCondition: %d -> %s", static_cast<int>(event), state ? "ON" : "OFF");
size_t index = static_cast<size_t>(event);
animationEvents[index].setn(state);
decltype(awaitingEvents) executingEvents;
for (auto it = awaitingEvents.begin(); it != awaitingEvents.end();)
{
if (it->event == event && it->eventState == state)
{
executingEvents.push_back(*it);
it = awaitingEvents.erase(it);
}
else
++it;
}
for (auto const & event : executingEvents)
event.action();
}
bool BattleInterface::getAnimationCondition( EAnimationEvents event)
{
size_t index = static_cast<size_t>(event);
return animationEvents[index].get();
}
void BattleInterface::waitForAnimationCondition( EAnimationEvents event, bool state)
{
auto unlockPim = vstd::makeUnlockGuard(*CPlayerInterface::pim);
size_t index = static_cast<size_t>(event);
animationEvents[index].waitUntil(state);
}
void BattleInterface::executeOnAnimationCondition( EAnimationEvents event, bool state, const AwaitingAnimationAction & action)
{
awaitingEvents.push_back({action, event, state});
}