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

834 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"
#include "../CMusicHandler.h"
#include "../CPlayerInterface.h"
#include "../gui/CursorHandler.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../render/Canvas.h"
#include "../adventureMap/AdventureMapInterface.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/gameState/InfoAboutArmy.h"
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/NetPacks.h"
#include "../../lib/UnlockGuard.h"
#include "../../lib/TerrainHandler.h"
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)
, battleOpeningDelayActive(true)
{
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
//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));
2023-03-01 17:20:05 +02:00
adventureInt->onAudioPaused();
ongoingAnimationsState.set(true);
2023-02-15 23:38:41 +02:00
GH.windows().pushWindow(windowObject);
2023-02-15 23:38:41 +02:00
windowObject->blockUI(true);
windowObject->updateQueue();
2023-02-18 17:35:07 +02:00
playIntroSoundAndUnlockInterface();
}
void BattleInterface::playIntroSoundAndUnlockInterface()
{
auto onIntroPlayed = [this]()
{
if(LOCPLINT->battleInt)
{
boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim);
onIntroSoundPlayed();
}
};
int battleIntroSoundChannel = CCS->soundh->playSoundFromSet(CCS->soundh->battleIntroSounds);
if (battleIntroSoundChannel != -1)
{
CCS->soundh->setCallback(battleIntroSoundChannel, onIntroPlayed);
if (settings["gameTweaks"]["skipBattleIntroMusic"].Bool())
openingEnd();
}
else // failed to play sound
{
onIntroSoundPlayed();
}
}
bool BattleInterface::openingPlaying()
{
return battleOpeningDelayActive;
}
void BattleInterface::onIntroSoundPlayed()
{
if (openingPlaying())
openingEnd();
CCS->musich->playMusicFromSet("battle", true, true);
}
void BattleInterface::openingEnd()
{
assert(openingPlaying());
if (!openingPlaying())
return;
onAnimationsFinished();
if(tacticsMode)
tacticNextStack(nullptr);
activateStack();
battleOpeningDelayActive = false;
}
BattleInterface::~BattleInterface()
{
CPlayerInterface::battleInt = nullptr;
2023-03-01 17:20:05 +02:00
if (adventureInt)
adventureInt->onAudioResumed();
2022-12-16 22:37:44 +02:00
awaitingEvents.clear();
onAnimationsFinished();
2022-12-16 22:37:44 +02:00
}
void BattleInterface::redrawBattlefield()
{
fieldController->redrawBackgroundWithHexes();
GH.windows().totalRedraw();
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->unitSide();
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
{
waitForAnimations();
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;
}
BattleAction ba;
ba.side = side.value();
ba.actionType = action;
ba.aimToHex(tile);
ba.actionSubtype = additional;
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
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);
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(!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"));
stacksController->setActiveStack(nullptr);
LOCPLINT->cb->battleMakeUnitAction(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);
stacksController->setActiveStack(nullptr);
//next stack will be activated when action ends
2010-03-02 13:40:29 +02:00
}
2023-02-02 14:36:58 +02:00
CCS->curh->set(Cursor::Combat::POINTER);
}
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->unitSide() == 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);
}
2023-04-06 17:34:07 +02:00
void BattleInterface::battleFinished(const BattleResult& br, QueryID queryID)
{
checkForAnimations();
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())
{
2023-04-06 17:34:07 +02:00
curInt->cb->selectionMade(0, queryID);
windowObject->close();
return;
}
2023-04-06 17:34:07 +02:00
auto wnd = std::make_shared<BattleResultWindow>(br, *(this->curInt));
wnd->resultCallback = [=](ui32 selection)
{
curInt->cb->selectionMade(selection, queryID);
};
GH.windows().pushWindow(wnd);
2023-04-06 17:34:07 +02:00
curInt->waitWhileDialog(); // Avoid freeze when AI end turn after battle. Check bug #1897
CPlayerInterface::battleInt = nullptr;
}
void BattleInterface::spellCast(const BattleSpellCast * sc)
{
// Do not deactivate anything in tactics mode
// This is battlefield setup spells
if(!tacticsMode)
{
windowObject->blockUI(true);
2022-12-12 21:17:36 +02:00
// Disable current active stack duing the cast
// Store the current activeStack to stackToActivate
stacksController->deactivateStack();
}
CCS->curh->set(Cursor::Combat::BLOCKED);
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
addToAnimationStage(group, [=]() {
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
{
addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]()
{
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
addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]()
{
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
}
addToAnimationStage(EAnimationEvents::HIT, [=](){
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)
{
addToAnimationStage(EAnimationEvents::HIT, [=](){
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);
addToAnimationStage(EAnimationEvents::HIT, [=](){
effectsController->displayEffect(EBattleEffect::MAGIC_MIRROR, stack->getPosition());
});
}
2023-01-07 14:27:12 +02:00
if (!sc->resistedCres.empty())
{
addToAnimationStage(EAnimationEvents::HIT, [=](){
2023-01-07 14:27:12 +02:00
CCS->soundh->playSound("MAGICRES");
});
}
for(auto & elem : sc->resistedCres)
{
auto stack = curInt->cb->battleGetStackByID(elem, false);
assert(stack);
addToAnimationStage(EAnimationEvents::HIT, [=](){
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;
addToAnimationStage(EAnimationEvents::AFTER_HIT, [=](){
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));
});
}
2023-08-03 13:20:54 +02:00
// animations will be executed by spell effects
}
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);
}
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;
windowObject->updateQueue();
windowObject->blockUI(false);
fieldController->redrawBackgroundWithHexes();
actionsController->activateStack();
GH.fakeMouseMove();
2011-12-14 00:35:28 +03:00
}
2023-01-21 16:15:20 +02:00
bool BattleInterface::makingTurn() const
{
return stacksController->getActiveStack() != nullptr;
}
void BattleInterface::endAction(const BattleAction* action)
{
// it is possible that tactics mode ended while opening music is still playing
waitForAnimations();
2016-10-28 23:37:20 +02:00
const CStack *stack = curInt->cb->battleGetStackByID(action->stackNumber);
// Activate stack from stackToActivate because this might have been temporary disabled, e.g., during spell cast
activateStack();
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;
2023-07-18 20:43:53 +02:00
auto side = tacticianInterface->cb->playerToSide(tacticianInterface->playerID);
auto action = BattleAction::makeEndOFTacticPhase(*side);
tacticianInterface->cb->battleMakeTacticAction(action);
}
2011-12-14 00:35:28 +03:00
static bool immobile(const CStack *s)
{
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
checkForAnimations();
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);
}
void BattleInterface::obstacleRemoved(const std::vector<ObstacleChanges> & obstacles)
{
obstacleController->obstacleRemoved(obstacles);
}
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);
2023-07-19 11:09:03 +02:00
if(curInt->cb->battleIsFinished())
{
2023-07-19 11:09:03 +02:00
return; // battle finished with spellcast
}
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?
tacticPhaseEnd();
stacksController->setActiveStack(nullptr);
}
else
{
const CStack* activeStack = stacksController->getActiveStack();
2022-04-04 09:16:32 +02:00
2023-07-19 11:09:03 +02:00
// If enemy is moving, activeStack can be null
if (activeStack)
{
stacksController->setActiveStack(nullptr);
2023-07-19 11:09:03 +02:00
// FIXME: unsafe
// Run task in separate thread to avoid UI lock while AI is making turn (which might take some time)
// HOWEVER this thread won't atttempt to lock game state, potentially leading to races
boost::thread aiThread([this, activeStack]()
{
curInt->autofightingAI->activeStack(activeStack);
2023-07-19 11:09:03 +02:00
});
aiThread.detach();
}
2023-07-19 11:09:03 +02:00
}
}
void BattleInterface::castThisSpell(SpellID spellID)
{
actionsController->castThisSpell(spellID);
}
void BattleInterface::executeStagedAnimations()
{
EAnimationEvents earliestStage = EAnimationEvents::COUNT;
for(const auto & event : awaitingEvents)
earliestStage = std::min(earliestStage, event.event);
if(earliestStage != EAnimationEvents::COUNT)
executeAnimationStage(earliestStage);
}
void BattleInterface::executeAnimationStage(EAnimationEvents event)
{
decltype(awaitingEvents) executingEvents;
for(auto it = awaitingEvents.begin(); it != awaitingEvents.end();)
{
if(it->event == event)
{
executingEvents.push_back(*it);
it = awaitingEvents.erase(it);
}
else
++it;
}
for(const auto & event : executingEvents)
event.action();
}
void BattleInterface::onAnimationsStarted()
{
ongoingAnimationsState.setn(true);
}
void BattleInterface::onAnimationsFinished()
{
ongoingAnimationsState.setn(false);
}
void BattleInterface::waitForAnimations()
{
{
auto unlockPim = vstd::makeUnlockGuard(*CPlayerInterface::pim);
ongoingAnimationsState.waitUntil(false);
}
assert(!hasAnimations());
assert(awaitingEvents.empty());
if (!awaitingEvents.empty())
{
logGlobal->error("Wait for animations finished but we still have awaiting events!");
awaitingEvents.clear();
}
}
bool BattleInterface::hasAnimations()
{
return ongoingAnimationsState.get();
}
void BattleInterface::checkForAnimations()
{
assert(!hasAnimations());
if(hasAnimations())
logGlobal->error("Unexpected animations state: expected all animations to be over, but some are still ongoing!");
waitForAnimations();
}
void BattleInterface::addToAnimationStage(EAnimationEvents event, const AwaitingAnimationAction & action)
{
awaitingEvents.push_back({action, event});
}
2023-02-15 23:38:41 +02:00
void BattleInterface::setBattleQueueVisibility(bool visible)
{
windowObject->hideQueue();
2023-02-15 23:38:41 +02:00
if(visible)
windowObject->showQueue();
2023-02-18 17:35:07 +02:00
}
void BattleInterface::setStickyHeroWindowsVisibility(bool visible)
{
windowObject->hideStickyHeroWindows();
if(visible)
windowObject->showStickyHeroWindows();
}