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

110 lines
2.5 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"
#include "../../lib/networkPacks/PacksForServer.h"
#include "../../lib/serializer/Cast.h"
2023-07-23 23:46:29 +02:00
void CBattleQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
{
2023-09-05 16:22:11 +02:00
assert(result);
2023-07-23 23:46:29 +02:00
if(result)
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);
}
CBattleDialogQuery::CBattleDialogQuery(CGameHandler * owner, const IBattleInfo * bi):
CDialogQuery(owner),
bi(bi)
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)
{
2024-01-26 00:44:41 +02:00
if (!gh->getPlayerState(color)->isHuman())
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
}
}