2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CQuery.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
|
|
|
|
*
|
|
|
|
*/
|
2013-04-20 14:34:01 +03:00
|
|
|
#pragma once
|
2013-04-21 15:49:26 +03:00
|
|
|
#include "../lib/GameConstants.h"
|
|
|
|
#include "../lib/int3.h"
|
|
|
|
#include "../lib/NetPacks.h"
|
2023-03-20 17:08:18 +02:00
|
|
|
#include "JsonNode.h"
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
class CGObjectInstance;
|
|
|
|
class CGHeroInstance;
|
|
|
|
class CArmedInstance;
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
class CGameHandler;
|
|
|
|
class CObjectVisitQuery;
|
|
|
|
class CQuery;
|
2017-06-06 06:53:51 +02:00
|
|
|
class Queries;
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
typedef std::shared_ptr<CQuery> QueryPtr;
|
2013-04-20 14:34:01 +03:00
|
|
|
|
|
|
|
// This class represents any kind of prolonged interaction that may need to do something special after it is over.
|
|
|
|
// It does not necessarily has to be "query" requiring player action, it can be also used internally within server.
|
|
|
|
// Examples:
|
2017-06-06 06:53:51 +02:00
|
|
|
// - all kinds of blocking dialog windows
|
|
|
|
// - battle
|
2013-04-20 14:34:01 +03:00
|
|
|
// - object visit
|
|
|
|
// - hero movement
|
|
|
|
// Queries can cause another queries, forming a stack of queries for each player. Eg: hero movement -> object visit -> dialog.
|
|
|
|
class CQuery
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::vector<PlayerColor> players; //players that are affected (often "blocked") by query
|
2013-05-27 13:53:28 +03:00
|
|
|
QueryID queryID;
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CQuery(Queries * Owner);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
virtual bool blocksPack(const CPack *pack) const; //query can block attempting actions by player. Eg. he can't move hero during the battle.
|
|
|
|
|
|
|
|
virtual bool endsByPlayerAnswer() const; //query is removed after player gives answer (like dialogs)
|
2017-06-06 06:53:51 +02:00
|
|
|
virtual void onAdding(PlayerColor color); //called just before query is pushed on stack
|
|
|
|
virtual void onAdded(PlayerColor color); //called right after query is pushed on stack
|
|
|
|
virtual void onRemoval(PlayerColor color); //called after query is removed from stack
|
|
|
|
virtual void onExposure(QueryPtr topQuery);//called when query immediately above is removed and this is exposed (becomes top)
|
2013-04-20 14:34:01 +03:00
|
|
|
virtual std::string toString() const;
|
|
|
|
|
|
|
|
virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const;
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
virtual void setReply(const JsonNode & reply);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2018-01-13 10:43:26 +02:00
|
|
|
virtual ~CQuery();
|
2017-06-06 06:53:51 +02:00
|
|
|
protected:
|
|
|
|
Queries * owner;
|
|
|
|
void addPlayer(PlayerColor color);
|
|
|
|
bool blockAllButReply(const CPack * pack) const;
|
2013-04-20 14:34:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &out, const CQuery &query);
|
|
|
|
std::ostream &operator<<(std::ostream &out, QueryPtr query);
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
class CGhQuery : public CQuery
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CGhQuery(CGameHandler * owner);
|
|
|
|
protected:
|
|
|
|
CGameHandler * gh;
|
|
|
|
};
|
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
//Created when hero visits object.
|
|
|
|
//Removed when query above is resolved (or immediately after visit if no queries were created)
|
2017-06-06 06:53:51 +02:00
|
|
|
class CObjectVisitQuery : public CGhQuery
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
const CGObjectInstance *visitedObject;
|
|
|
|
const CGHeroInstance *visitingHero;
|
|
|
|
int3 tile; //may be different than hero pos -> eg. visit via teleport
|
2013-05-14 16:42:52 +03:00
|
|
|
bool removeObjectAfterVisit;
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CObjectVisitQuery(CGameHandler * owner, const CGObjectInstance *Obj, const CGHeroInstance *Hero, int3 Tile);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
virtual bool blocksPack(const CPack *pack) const override;
|
2017-06-06 06:53:51 +02:00
|
|
|
virtual void onRemoval(PlayerColor color) override;
|
|
|
|
virtual void onExposure(QueryPtr topQuery) override;
|
2013-04-20 14:34:01 +03:00
|
|
|
};
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
class CBattleQuery : public CGhQuery
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::array<const CArmedInstance *,2> belligerents;
|
2023-04-06 19:19:46 +02:00
|
|
|
std::array<int, 2> initialHeroMana;
|
2013-04-20 14:34:01 +03:00
|
|
|
|
|
|
|
const BattleInfo *bi;
|
|
|
|
boost::optional<BattleResult> result;
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CBattleQuery(CGameHandler * owner);
|
|
|
|
CBattleQuery(CGameHandler * owner, const BattleInfo * Bi); //TODO
|
2013-06-26 14:18:27 +03:00
|
|
|
virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const override;
|
|
|
|
virtual bool blocksPack(const CPack *pack) const override;
|
2017-06-06 06:53:51 +02:00
|
|
|
virtual void onRemoval(PlayerColor color) override;
|
2013-04-20 14:34:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//Created when hero attempts move and something happens
|
|
|
|
//(not necessarily position change, could be just an object interaction).
|
2017-06-06 06:53:51 +02:00
|
|
|
class CHeroMovementQuery : public CGhQuery
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TryMoveHero tmh;
|
|
|
|
bool visitDestAfterVictory; //if hero moved to guarded tile and it should be visited once guard is defeated
|
|
|
|
const CGHeroInstance *hero;
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
virtual void onExposure(QueryPtr topQuery) override;
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CHeroMovementQuery(CGameHandler * owner, const TryMoveHero & Tmh, const CGHeroInstance * Hero, bool VisitDestAfterVictory = false);
|
|
|
|
virtual void onAdding(PlayerColor color) override;
|
|
|
|
virtual void onRemoval(PlayerColor color) override;
|
2013-04-20 14:34:01 +03:00
|
|
|
};
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
class CDialogQuery : public CGhQuery
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
public:
|
2017-06-06 06:53:51 +02:00
|
|
|
CDialogQuery(CGameHandler * owner);
|
2013-06-26 14:18:27 +03:00
|
|
|
virtual bool endsByPlayerAnswer() const override;
|
|
|
|
virtual bool blocksPack(const CPack *pack) const override;
|
2017-06-06 06:53:51 +02:00
|
|
|
void setReply(const JsonNode & reply) override;
|
|
|
|
protected:
|
|
|
|
boost::optional<ui32> answer;
|
2013-04-20 14:34:01 +03:00
|
|
|
};
|
|
|
|
|
2013-05-27 13:53:28 +03:00
|
|
|
class CGarrisonDialogQuery : public CDialogQuery //used also for hero exchange dialogs
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::array<const CArmedInstance *,2> exchangingArmies;
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CGarrisonDialogQuery(CGameHandler * owner, const CArmedInstance *up, const CArmedInstance *down);
|
2013-06-26 14:18:27 +03:00
|
|
|
virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const override;
|
|
|
|
virtual bool blocksPack(const CPack *pack) const override;
|
2013-04-20 14:34:01 +03:00
|
|
|
};
|
|
|
|
|
2023-04-06 17:34:07 +02:00
|
|
|
class CBattleDialogQuery : public CDialogQuery
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CBattleDialogQuery(CGameHandler * owner, const BattleInfo * Bi);
|
|
|
|
|
|
|
|
const BattleInfo * bi;
|
|
|
|
|
|
|
|
virtual void onRemoval(PlayerColor color) override;
|
|
|
|
};
|
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
//yes/no and component selection dialogs
|
|
|
|
class CBlockingDialogQuery : public CDialogQuery
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BlockingDialog bd; //copy of pack... debug purposes
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CBlockingDialogQuery(CGameHandler * owner, const BlockingDialog &bd);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const override;
|
2013-04-20 14:34:01 +03:00
|
|
|
};
|
|
|
|
|
2015-03-08 15:37:33 +02:00
|
|
|
class CTeleportDialogQuery : public CDialogQuery
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TeleportDialog td; //copy of pack... debug purposes
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CTeleportDialogQuery(CGameHandler * owner, const TeleportDialog &td);
|
2015-03-08 15:37:33 +02:00
|
|
|
|
|
|
|
virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const override;
|
|
|
|
};
|
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
class CHeroLevelUpDialogQuery : public CDialogQuery
|
|
|
|
{
|
|
|
|
public:
|
2018-03-10 21:19:55 +02:00
|
|
|
CHeroLevelUpDialogQuery(CGameHandler * owner, const HeroLevelUp &Hlu, const CGHeroInstance * Hero);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
virtual void onRemoval(PlayerColor color) override;
|
2013-06-26 14:18:27 +03:00
|
|
|
virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const override;
|
2013-04-20 14:34:01 +03:00
|
|
|
|
|
|
|
HeroLevelUp hlu;
|
2018-03-10 21:19:55 +02:00
|
|
|
const CGHeroInstance * hero;
|
2013-04-20 14:34:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class CCommanderLevelUpDialogQuery : public CDialogQuery
|
|
|
|
{
|
|
|
|
public:
|
2018-03-10 21:19:55 +02:00
|
|
|
CCommanderLevelUpDialogQuery(CGameHandler * owner, const CommanderLevelUp &Clu, const CGHeroInstance * Hero);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
virtual void onRemoval(PlayerColor color) override;
|
2013-06-26 14:18:27 +03:00
|
|
|
virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const override;
|
2013-04-20 14:34:01 +03:00
|
|
|
|
|
|
|
CommanderLevelUp clu;
|
2018-03-10 21:19:55 +02:00
|
|
|
const CGHeroInstance * hero;
|
2013-04-20 14:34:01 +03:00
|
|
|
};
|
|
|
|
|
2017-07-03 20:09:27 +02:00
|
|
|
class CGenericQuery : public CQuery
|
2017-06-06 06:53:51 +02:00
|
|
|
{
|
|
|
|
public:
|
2017-07-03 20:09:27 +02:00
|
|
|
CGenericQuery(Queries * Owner, PlayerColor color, std::function<void(const JsonNode &)> Callback);
|
2017-06-06 06:53:51 +02:00
|
|
|
|
|
|
|
bool blocksPack(const CPack * pack) const override;
|
|
|
|
bool endsByPlayerAnswer() const override;
|
|
|
|
void onExposure(QueryPtr topQuery) override;
|
2017-07-03 20:09:27 +02:00
|
|
|
void setReply(const JsonNode & reply) override;
|
2023-03-20 17:08:18 +02:00
|
|
|
void onRemoval(PlayerColor color) override;
|
2017-07-03 20:09:27 +02:00
|
|
|
private:
|
|
|
|
std::function<void(const JsonNode &)> callback;
|
2023-03-20 17:08:18 +02:00
|
|
|
JsonNode reply;
|
2017-06-06 06:53:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Queries
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
void addQuery(PlayerColor player, QueryPtr query);
|
|
|
|
void popQuery(PlayerColor player, QueryPtr query);
|
|
|
|
|
|
|
|
std::map<PlayerColor, std::vector<QueryPtr>> queries; //player => stack of queries
|
|
|
|
|
|
|
|
public:
|
|
|
|
static boost::mutex mx;
|
|
|
|
|
|
|
|
void addQuery(QueryPtr query);
|
|
|
|
void popQuery(const CQuery &query);
|
|
|
|
void popQuery(QueryPtr query);
|
|
|
|
void popIfTop(const CQuery &query); //removes this query if it is at the top (otherwise, do nothing)
|
|
|
|
void popIfTop(QueryPtr query); //removes this query if it is at the top (otherwise, do nothing)
|
|
|
|
|
|
|
|
QueryPtr topQuery(PlayerColor player);
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
std::vector<std::shared_ptr<const CQuery>> allQueries() const;
|
2020-10-19 21:39:57 +02:00
|
|
|
std::vector<QueryPtr> allQueries();
|
|
|
|
QueryPtr getQuery(QueryID queryID);
|
2013-04-20 14:34:01 +03:00
|
|
|
//void removeQuery
|
2013-04-21 15:49:26 +03:00
|
|
|
};
|