2023-10-19 16:19:09 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../../lib/GameConstants.h"
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2024-10-04 15:41:53 +02:00
|
|
|
struct CPackForServer;
|
2024-09-04 16:17:22 +02:00
|
|
|
class CGObjectInstance;
|
|
|
|
class CGHeroInstance;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
|
|
|
class CObjectVisitQuery;
|
|
|
|
class QueriesProcessor;
|
|
|
|
class CQuery;
|
|
|
|
class CGameHandler;
|
|
|
|
|
|
|
|
using QueryPtr = std::shared_ptr<CQuery>;
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
// - all kinds of blocking dialog windows
|
|
|
|
// - battle
|
|
|
|
// - 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
|
|
|
|
QueryID queryID;
|
|
|
|
|
|
|
|
CQuery(CGameHandler * gh);
|
|
|
|
|
2024-09-04 16:17:22 +02:00
|
|
|
/// query can block attempting actions by player. Eg. he can't move hero during the battle.
|
2024-10-04 15:41:53 +02:00
|
|
|
virtual bool blocksPack(const CPackForServer *pack) const;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-09-04 16:17:22 +02:00
|
|
|
/// query is removed after player gives answer (like dialogs)
|
|
|
|
virtual bool endsByPlayerAnswer() const;
|
|
|
|
|
|
|
|
/// called just before query is pushed on stack
|
|
|
|
virtual void onAdding(PlayerColor color);
|
|
|
|
|
|
|
|
/// called right after query is pushed on stack
|
|
|
|
virtual void onAdded(PlayerColor color);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-09-04 16:17:22 +02:00
|
|
|
/// called after query is removed from stack
|
|
|
|
virtual void onRemoval(PlayerColor color);
|
|
|
|
|
|
|
|
/// called when query immediately above is removed and this is exposed (becomes top)
|
|
|
|
virtual void onExposure(QueryPtr topQuery);
|
|
|
|
|
|
|
|
/// called when this query is being removed and must report its result to currently visited object
|
|
|
|
virtual void notifyObjectAboutRemoval(const CGObjectInstance * visitedObject, const CGHeroInstance * visitingHero) const;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
virtual void setReply(std::optional<int32_t> reply);
|
2024-09-04 16:17:22 +02:00
|
|
|
virtual std::string toString() const;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
virtual ~CQuery();
|
|
|
|
protected:
|
|
|
|
QueriesProcessor * owner;
|
|
|
|
CGameHandler * gh;
|
|
|
|
void addPlayer(PlayerColor color);
|
2024-10-04 15:41:53 +02:00
|
|
|
bool blockAllButReply(const CPackForServer * pack) const;
|
2023-10-19 16:19:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &out, const CQuery &query);
|
|
|
|
std::ostream &operator<<(std::ostream &out, QueryPtr query);
|
|
|
|
|
|
|
|
class CDialogQuery : public CQuery
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CDialogQuery(CGameHandler * owner);
|
2024-02-10 21:22:08 +02:00
|
|
|
bool endsByPlayerAnswer() const override;
|
2024-10-04 15:41:53 +02:00
|
|
|
bool blocksPack(const CPackForServer *pack) const override;
|
2023-10-19 16:19:09 +02:00
|
|
|
void setReply(std::optional<int32_t> reply) override;
|
|
|
|
protected:
|
|
|
|
std::optional<ui32> answer;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CGenericQuery : public CQuery
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CGenericQuery(CGameHandler * gh, PlayerColor color, std::function<void(std::optional<int32_t>)> Callback);
|
|
|
|
|
2024-10-04 15:41:53 +02:00
|
|
|
bool blocksPack(const CPackForServer * pack) const override;
|
2023-10-19 16:19:09 +02:00
|
|
|
bool endsByPlayerAnswer() const override;
|
|
|
|
void onExposure(QueryPtr topQuery) override;
|
|
|
|
void setReply(std::optional<int32_t> reply) override;
|
|
|
|
void onRemoval(PlayerColor color) override;
|
|
|
|
private:
|
|
|
|
std::function<void(std::optional<int32_t>)> callback;
|
|
|
|
std::optional<int32_t> reply;
|
|
|
|
};
|