1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Moved all battle-related functionality of server into a new class

This commit is contained in:
Ivan Savenko 2023-07-24 00:00:37 +03:00
parent 4edee00a97
commit e8e6c02a4a
8 changed files with 2945 additions and 2826 deletions

2754
server/BattleProcessor.cpp Normal file

File diff suppressed because it is too large Load Diff

129
server/BattleProcessor.h Normal file
View File

@ -0,0 +1,129 @@
/*
* BattleProcessor.h, 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
*
*/
#pragma once
#include "../lib/GameConstants.h"
#include "../lib/NetPacks.h"
VCMI_LIB_NAMESPACE_BEGIN
class CStack;
struct SideInBattle;
namespace battle {
class CUnitState;
}
VCMI_LIB_NAMESPACE_END
class CBattleQuery;
struct CasualtiesAfterBattle
{
using TStackAndItsNewCount = std::pair<StackLocation, int>;
using TSummoned = std::map<CreatureID, TQuantity>;
enum {ERASE = -1};
const CArmedInstance * army;
std::vector<TStackAndItsNewCount> newStackCounts;
std::vector<ArtifactLocation> removedWarMachines;
TSummoned summoned;
ObjectInstanceID heroWithDeadCommander; //TODO: unify stack locations
CasualtiesAfterBattle(const SideInBattle & battleSide, const BattleInfo * bat);
void updateArmy(CGameHandler *gh);
};
struct FinishingBattleHelper
{
FinishingBattleHelper();
FinishingBattleHelper(std::shared_ptr<const CBattleQuery> Query, int RemainingBattleQueriesCount);
inline bool isDraw() const {return winnerSide == 2;}
const CGHeroInstance *winnerHero, *loserHero;
PlayerColor victor, loser;
ui8 winnerSide;
int remainingBattleQueriesCount;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & winnerHero;
h & loserHero;
h & victor;
h & loser;
h & winnerSide;
h & remainingBattleQueriesCount;
}
};
using FireShieldInfo = std::vector<std::pair<const CStack *, int64_t>>;
class BattleProcessor : boost::noncopyable
{
////used only in endBattle - don't touch elsewhere
bool visitObjectAfterVictory;
std::unique_ptr<boost::thread> battleThread;
std::unique_ptr<FinishingBattleHelper> finishingBattle;
void removeObstacle(const CObstacleInstance &obstacle);
void makeStackDoNothing(const CStack * next);
void updateGateState();
bool makeAutomaticAction(const CStack *stack, BattleAction &ba); //used when action is taken by stack without volition of player (eg. unguided catapult attack)
void stackEnchantedTrigger(const CStack * stack);
void stackTurnTrigger(const CStack *stack);
void engageIntoBattle( PlayerColor player );
void handleAttackBeforeCasting(bool ranged, const CStack * attacker, const CStack * defender);
void handleAfterAttackCasting(bool ranged, const CStack * attacker, const CStack * defender);
void attackCasting(bool ranged, BonusType attackMode, const battle::Unit * attacker, const battle::Unit * defender);
int moveStack(int stack, BattleHex dest); //returned value - travelled distance
void runBattle();
void makeAttack(const CStack * attacker, const CStack * defender, int distance, BattleHex targetHex, bool first, bool ranged, bool counter);
// damage, drain life & fire shield; returns amount of drained life
int64_t applyBattleEffects(BattleAttack & bat, std::shared_ptr<battle::CUnitState> attackerState, FireShieldInfo & fireShield, const CStack * def, int distance, bool secondary);
void sendGenericKilledLog(const CStack * defender, int32_t killed, bool multiple);
void addGenericKilledLog(BattleLogMessage & blm, const CStack * defender, int32_t killed, bool multiple);
void checkBattleStateChanges();
void setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance *heroes[2], bool creatureBank, const CGTownInstance *town);
void setBattleResult(BattleResult::EResult resultType, int victoriusSide);
public:
CGameHandler * gameHandler;
BattleProcessor(CGameHandler * gameHandler);
BattleProcessor();
~BattleProcessor();
void startBattlePrimary(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank = false, const CGTownInstance *town = nullptr); //use hero=nullptr for no hero
void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank = false); //if any of armies is hero, hero will be used
void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, bool creatureBank = false); //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle
void battleAfterLevelUp(const BattleResult &result);
bool makeBattleAction(BattleAction &ba);
bool makeCustomAction(BattleAction &ba);
void endBattle(int3 tile, const CGHeroInstance * hero1, const CGHeroInstance * hero2); //ends battle
void endBattleConfirm(const BattleInfo * battleInfo);
template <typename Handler> void serialize(Handler &h, const int version)
{
}
};

File diff suppressed because it is too large Load Diff

View File

@ -11,28 +11,15 @@
#include <vcmi/Environment.h>
#include "../lib/FunctionList.h"
#include "../lib/IGameCallback.h"
#include "../lib/battle/CBattleInfoCallback.h"
#include "../lib/battle/BattleAction.h"
#include "../lib/ScriptHandler.h"
#include "CQuery.h"
VCMI_LIB_NAMESPACE_BEGIN
class CGameState;
struct StartInfo;
struct BattleResult;
struct SideInBattle;
struct BattleAttack;
struct BattleStackAttacked;
struct CPack;
struct Query;
struct SetResources;
struct NewStructures;
class CGHeroInstance;
class IMarket;
class SpellCastEnvironment;
#if SCRIPTING_ENABLED
@ -42,16 +29,15 @@ namespace scripting
}
#endif
template<typename T> class CApplier;
VCMI_LIB_NAMESPACE_END
class HeroPoolProcessor;
class CGameHandler;
class CVCMIServer;
class CBaseForGHApply;
class PlayerMessageProcessor;
class BattleProcessor;
struct PlayerStatus
{
@ -80,33 +66,17 @@ public:
}
};
struct CasualtiesAfterBattle
{
using TStackAndItsNewCount = std::pair<StackLocation, int>;
using TSummoned = std::map<CreatureID, TQuantity>;
enum {ERASE = -1};
const CArmedInstance * army;
std::vector<TStackAndItsNewCount> newStackCounts;
std::vector<ArtifactLocation> removedWarMachines;
TSummoned summoned;
ObjectInstanceID heroWithDeadCommander; //TODO: unify stack locations
CasualtiesAfterBattle(const SideInBattle & battleSide, const BattleInfo * bat);
void updateArmy(CGameHandler *gh);
};
class CGameHandler : public IGameCallback, public CBattleInfoCallback, public Environment
{
CVCMIServer * lobby;
std::shared_ptr<CApplier<CBaseForGHApply>> applier;
std::unique_ptr<boost::thread> battleThread;
public:
boost::recursive_mutex battleActionMutex;
using CCallbackBase::setBattle;
std::unique_ptr<HeroPoolProcessor> heroPool;
std::unique_ptr<BattleProcessor> battles;
using FireShieldInfo = std::vector<std::pair<const CStack *, int64_t>>;
//use enums as parameters, because doMove(sth, true, false, true) is not readable
enum EGuardLook {CHECK_FOR_GUARDS, IGNORE_GUARDS};
enum EVisitDest {VISIT_DEST, DONT_VISIT_DEST};
@ -135,26 +105,6 @@ public:
bool isBlockedByQueries(const CPack *pack, PlayerColor player);
bool isAllowedExchange(ObjectInstanceID id1, ObjectInstanceID id2);
void giveSpells(const CGTownInstance *t, const CGHeroInstance *h);
int moveStack(int stack, BattleHex dest); //returned value - travelled distance
void runBattle();
////used only in endBattle - don't touch elsewhere
bool visitObjectAfterVictory;
//
void endBattle(int3 tile, const CGHeroInstance * hero1, const CGHeroInstance * hero2); //ends battle
void endBattleConfirm(const BattleInfo * battleInfo);
void makeAttack(const CStack * attacker, const CStack * defender, int distance, BattleHex targetHex, bool first, bool ranged, bool counter);
// damage, drain life & fire shield; returns amount of drained life
int64_t applyBattleEffects(BattleAttack & bat, std::shared_ptr<battle::CUnitState> attackerState, FireShieldInfo & fireShield, const CStack * def, int distance, bool secondary);
void sendGenericKilledLog(const CStack * defender, int32_t killed, bool multiple);
void addGenericKilledLog(BattleLogMessage & blm, const CStack * defender, int32_t killed, bool multiple);
void checkBattleStateChanges();
void setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance *heroes[2], bool creatureBank, const CGTownInstance *town);
void setBattleResult(BattleResult::EResult resultType, int victoriusSide);
CGameHandler() = default;
CGameHandler(CVCMIServer * lobby);
@ -242,14 +192,6 @@ public:
PlayerColor getPlayerAt(std::shared_ptr<CConnection> c) const;
bool hasPlayerAt(PlayerColor player, std::shared_ptr<CConnection> c) const;
void updateGateState();
bool makeBattleAction(BattleAction &ba);
bool makeAutomaticAction(const CStack *stack, BattleAction &ba); //used when action is taken by stack without volition of player (eg. unguided catapult attack)
bool makeCustomAction(BattleAction &ba);
void stackEnchantedTrigger(const CStack * stack);
void stackTurnTrigger(const CStack *stack);
void removeObstacle(const CObstacleInstance &obstacle);
bool queryReply( QueryID qid, const JsonNode & answer, PlayerColor player );
bool buildBoat( ObjectInstanceID objid, PlayerColor player );
bool setFormation( ObjectInstanceID hid, ui8 formation );
@ -284,7 +226,6 @@ public:
bool complain(const std::string &problem); //sends message to all clients, prints on the logs and return true
void objectVisited( const CGObjectInstance * obj, const CGHeroInstance * h );
void objectVisitEnded(const CObjectVisitQuery &query);
void engageIntoBattle( PlayerColor player );
bool dig(const CGHeroInstance *h);
void moveArmy(const CArmedInstance *src, const CArmedInstance *dst, bool allowMerging);
@ -292,7 +233,7 @@ public:
{
h & QID;
h & states;
h & finishingBattle;
h & battles;
h & heroPool;
h & getRandomGenerator();
h & playerMessages;
@ -324,39 +265,8 @@ public:
void throwAndComplain(CPackForServer * pack, std::string txt);
bool isPlayerOwns(CPackForServer * pack, ObjectInstanceID id);
struct FinishingBattleHelper
{
FinishingBattleHelper();
FinishingBattleHelper(std::shared_ptr<const CBattleQuery> Query, int RemainingBattleQueriesCount);
inline bool isDraw() const {return winnerSide == 2;}
const CGHeroInstance *winnerHero, *loserHero;
PlayerColor victor, loser;
ui8 winnerSide;
int remainingBattleQueriesCount;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & winnerHero;
h & loserHero;
h & victor;
h & loser;
h & winnerSide;
h & remainingBattleQueriesCount;
}
};
std::unique_ptr<FinishingBattleHelper> finishingBattle;
void battleAfterLevelUp(const BattleResult &result);
void run(bool resume);
void newTurn();
void handleAttackBeforeCasting(bool ranged, const CStack * attacker, const CStack * defender);
void handleAfterAttackCasting(bool ranged, const CStack * attacker, const CStack * defender);
void attackCasting(bool ranged, BonusType attackMode, const battle::Unit * attacker, const battle::Unit * defender);
bool sacrificeArtifact(const IMarket * m, const CGHeroInstance * hero, const std::vector<ArtifactPosition> & slot);
void spawnWanderingMonsters(CreatureID creatureID);
@ -384,8 +294,6 @@ private:
void reinitScripting();
void deserializationFix();
void makeStackDoNothing(const CStack * next);
void getVictoryLossMessage(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult, InfoWindow & out) const;
const std::string complainNoCreatures;

View File

@ -1,6 +1,7 @@
set(server_SRCS
StdInc.cpp
BattleProcessor.cpp
CGameHandler.cpp
HeroPoolProcessor.cpp
PlayerMessageProcessor.cpp
@ -14,6 +15,7 @@ set(server_SRCS
set(server_HEADERS
StdInc.h
BattleProcessor.h
CGameHandler.h
HeroPoolProcessor.h
PlayerMessageProcessor.h

View File

@ -10,6 +10,7 @@
#include "StdInc.h"
#include "CQuery.h"
#include "CGameHandler.h"
#include "BattleProcessor.h"
#include "../lib/battle/BattleInfo.h"
#include "../lib/mapObjects/MiscObjects.h"
#include "../lib/serializer/Cast.h"
@ -326,7 +327,7 @@ bool CBattleQuery::blocksPack(const CPack * pack) const
void CBattleQuery::onRemoval(PlayerColor color)
{
if(result)
gh->battleAfterLevelUp(*result);
gh->battles->battleAfterLevelUp(*result);
}
void CGarrisonDialogQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
@ -418,7 +419,7 @@ void CBattleDialogQuery::onRemoval(PlayerColor color)
}
else
{
gh->endBattleConfirm(bi);
gh->battles->endBattleConfirm(bi);
}
}

View File

@ -8,13 +8,13 @@
*
*/
#pragma once
#include "../lib/GameConstants.h"
#include "../lib/int3.h"
#include "../lib/NetPacks.h"
#include "JsonNode.h"
VCMI_LIB_NAMESPACE_BEGIN
class JsonNode;
class CGObjectInstance;
class CGHeroInstance;
class CArmedInstance;

View File

@ -11,6 +11,7 @@
#include "ServerNetPackVisitors.h"
#include "CGameHandler.h"
#include "BattleProcessor.h"
#include "HeroPoolProcessor.h"
#include "PlayerMessageProcessor.h"
@ -280,8 +281,6 @@ void ApplyGhNetPackVisitor::visitQueryReply(QueryReply & pack)
void ApplyGhNetPackVisitor::visitMakeAction(MakeAction & pack)
{
boost::unique_lock lock(gh.battleActionMutex);
const BattleInfo * b = gs.curB;
if(!b)
gh.throwAndComplain(&pack, "Can not make action - there is no battle ongoing!");
@ -304,13 +303,11 @@ void ApplyGhNetPackVisitor::visitMakeAction(MakeAction & pack)
gh.throwAndComplain(&pack, "Can not make actions in battles you are not part of!");
}
result = gh.makeBattleAction(pack.ba);
result = gh.battles->makeBattleAction(pack.ba);
}
void ApplyGhNetPackVisitor::visitMakeCustomAction(MakeCustomAction & pack)
{
boost::unique_lock lock(gh.battleActionMutex);
const BattleInfo * b = gs.curB;
if(!b)
gh.throwNotAllowedAction(&pack);
@ -325,7 +322,7 @@ void ApplyGhNetPackVisitor::visitMakeCustomAction(MakeCustomAction & pack)
if(pack.ba.actionType != EActionType::HERO_SPELL)
gh.throwNotAllowedAction(&pack);
result = gh.makeCustomAction(pack.ba);
result = gh.battles->makeCustomAction(pack.ba);
}
void ApplyGhNetPackVisitor::visitDigWithHero(DigWithHero & pack)