1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/server/queries/BattleQueries.cpp

116 lines
3.0 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/battle/BattleLayout.h"
2024-01-26 00:44:41 +02:00
#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"
2023-07-23 23:46:29 +02:00
void CBattleQuery::notifyObjectAboutRemoval(const CGObjectInstance * visitedObject, const CGHeroInstance * visitingHero) const
2024-03-04 21:34:43 +02:00
{
assert(result);
if(result)
visitedObject->battleFinished(visitingHero, *result);
2024-03-04 21:34:43 +02:00
}
CBattleQuery::CBattleQuery(CGameHandler * owner, const IBattleInfo * bi):
CQuery(owner),
battleID(bi->getBattleID())
2023-07-23 23:46:29 +02:00
{
belligerents[BattleSide::ATTACKER] = bi->getSideArmy(BattleSide::ATTACKER);
belligerents[BattleSide::DEFENDER] = bi->getSideArmy(BattleSide::DEFENDER);
2023-07-23 23:46:29 +02:00
addPlayer(bi->getSidePlayer(BattleSide::ATTACKER));
addPlayer(bi->getSidePlayer(BattleSide::DEFENDER));
2023-07-23 23:46:29 +02:00
}
CBattleQuery::CBattleQuery(CGameHandler * owner):
CQuery(owner)
2023-07-23 23:46:29 +02:00
{
belligerents[BattleSide::ATTACKER] = nullptr;
belligerents[BattleSide::DEFENDER] = nullptr;
2023-07-23 23:46:29 +02:00
}
bool CBattleQuery::blocksPack(const CPackForServer * pack) const
2023-07-23 23:46:29 +02:00
{
if(dynamic_cast<const MakeAction*>(pack) != nullptr)
return false;
if(dynamic_cast<const 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(BattleSide::ATTACKER));
addPlayer(bi->getSidePlayer(BattleSide::DEFENDER));
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)
{
gh->battles->restartBattle(
2023-09-05 16:22:11 +02:00
bi->getBattleID(),
bi->getSideArmy(BattleSide::ATTACKER),
bi->getSideArmy(BattleSide::DEFENDER),
bi->getLocation(),
bi->getSideHero(BattleSide::ATTACKER),
bi->getSideHero(BattleSide::DEFENDER),
bi->getLayout(),
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
}