1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/server/queries/BattleQueries.cpp

115 lines
2.7 KiB
C++
Raw Normal View History

2023-07-23 23:46:29 +02:00
/*
* BattleQueries.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
*
*/
#include "StdInc.h"
#include "BattleQueries.h"
#include "MapQueries.h"
2023-09-05 16:22:11 +02:00
#include "QueriesProcessor.h"
2023-07-23 23:46:29 +02:00
#include "../CGameHandler.h"
#include "../battles/BattleProcessor.h"
#include "../../lib/battle/IBattleState.h"
2024-01-26 00:44:41 +02:00
#include "../../lib/battle/SideInBattle.h"
#include "../../lib/CPlayerState.h"
#include "../../lib/mapObjects/CGObjectInstance.h"
2024-02-28 23:25:14 +02:00
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/networkPacks/PacksForServer.h"
#include "../../lib/serializer/Cast.h"
2023-07-23 23:46:29 +02:00
2024-03-04 21:34:43 +02:00
void CBattleQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
{
assert(result);
if(result)
2024-03-04 21:34:43 +02:00
objectVisit.visitedObject->battleFinished(objectVisit.visitingHero, *result);
}
CBattleQuery::CBattleQuery(CGameHandler * owner, const IBattleInfo * bi):
CQuery(owner),
battleID(bi->getBattleID())
2023-07-23 23:46:29 +02:00
{
belligerents[0] = bi->getSideArmy(0);
belligerents[1] = bi->getSideArmy(1);
2023-07-23 23:46:29 +02:00
addPlayer(bi->getSidePlayer(0));
addPlayer(bi->getSidePlayer(1));
2023-07-23 23:46:29 +02:00
}
CBattleQuery::CBattleQuery(CGameHandler * owner):
CQuery(owner)
2023-07-23 23:46:29 +02:00
{
belligerents[0] = belligerents[1] = nullptr;
}
bool CBattleQuery::blocksPack(const CPack * pack) const
{
if(dynamic_ptr_cast<MakeAction>(pack) != nullptr)
return false;
if(dynamic_ptr_cast<GamePause>(pack) != nullptr)
return false;
return true;
2023-07-23 23:46:29 +02:00
}
void CBattleQuery::onRemoval(PlayerColor color)
{
2023-09-05 16:22:11 +02:00
assert(result);
2023-07-23 23:46:29 +02:00
if(result)
gh->battles->battleAfterLevelUp(battleID, *result);
2023-07-23 23:46:29 +02:00
}
2023-09-05 16:22:11 +02:00
void CBattleQuery::onExposure(QueryPtr topQuery)
{
// this method may be called in two cases:
// 1) when requesting battle replay (but before replay starts -> no valid result)
// 2) when aswering on levelup queries after accepting battle result -> valid result
if(result)
owner->popQuery(*this);
}
2024-02-28 23:25:14 +02:00
CBattleDialogQuery::CBattleDialogQuery(CGameHandler * owner, const IBattleInfo * bi, std::optional<BattleResult> Br):
CDialogQuery(owner),
2024-02-28 23:25:14 +02:00
bi(bi),
result(Br)
2023-07-23 23:46:29 +02:00
{
addPlayer(bi->getSidePlayer(0));
addPlayer(bi->getSidePlayer(1));
2023-07-23 23:46:29 +02:00
}
void CBattleDialogQuery::onRemoval(PlayerColor color)
{
// answer to this query was already processed when handling 1st player
// this removal call for 2nd player which can be safely ignored
if (resultProcessed)
2024-01-26 00:44:41 +02:00
return;
2023-07-23 23:46:29 +02:00
assert(answer);
if(*answer == 1)
{
2023-09-05 16:22:11 +02:00
gh->battles->restartBattlePrimary(
bi->getBattleID(),
bi->getSideArmy(0),
bi->getSideArmy(1),
bi->getLocation(),
bi->getSideHero(0),
bi->getSideHero(1),
bi->isCreatureBank(),
bi->getDefendedTown()
);
2023-07-23 23:46:29 +02:00
}
else
{
gh->battles->endBattleConfirm(bi->getBattleID());
2023-07-23 23:46:29 +02:00
}
resultProcessed = true;
2023-07-23 23:46:29 +02:00
}