2
0

CQuery.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * CQuery.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/GameConstants.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. struct CPack;
  14. VCMI_LIB_NAMESPACE_END
  15. class CObjectVisitQuery;
  16. class QueriesProcessor;
  17. class CQuery;
  18. class CGameHandler;
  19. using QueryPtr = std::shared_ptr<CQuery>;
  20. // This class represents any kind of prolonged interaction that may need to do something special after it is over.
  21. // It does not necessarily has to be "query" requiring player action, it can be also used internally within server.
  22. // Examples:
  23. // - all kinds of blocking dialog windows
  24. // - battle
  25. // - object visit
  26. // - hero movement
  27. // Queries can cause another queries, forming a stack of queries for each player. Eg: hero movement -> object visit -> dialog.
  28. class CQuery
  29. {
  30. public:
  31. std::vector<PlayerColor> players; //players that are affected (often "blocked") by query
  32. QueryID queryID;
  33. CQuery(CGameHandler * gh);
  34. virtual bool blocksPack(const CPack *pack) const; //query can block attempting actions by player. Eg. he can't move hero during the battle.
  35. virtual bool endsByPlayerAnswer() const; //query is removed after player gives answer (like dialogs)
  36. virtual void onAdding(PlayerColor color); //called just before query is pushed on stack
  37. virtual void onAdded(PlayerColor color); //called right after query is pushed on stack
  38. virtual void onRemoval(PlayerColor color); //called after query is removed from stack
  39. virtual void onExposure(QueryPtr topQuery);//called when query immediately above is removed and this is exposed (becomes top)
  40. virtual std::string toString() const;
  41. virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const;
  42. virtual void setReply(std::optional<int32_t> reply);
  43. virtual ~CQuery();
  44. protected:
  45. QueriesProcessor * owner;
  46. CGameHandler * gh;
  47. void addPlayer(PlayerColor color);
  48. bool blockAllButReply(const CPack * pack) const;
  49. };
  50. std::ostream &operator<<(std::ostream &out, const CQuery &query);
  51. std::ostream &operator<<(std::ostream &out, QueryPtr query);
  52. class CDialogQuery : public CQuery
  53. {
  54. public:
  55. CDialogQuery(CGameHandler * owner);
  56. bool endsByPlayerAnswer() const override;
  57. bool blocksPack(const CPack *pack) const override;
  58. void setReply(std::optional<int32_t> reply) override;
  59. protected:
  60. std::optional<ui32> answer;
  61. };
  62. class CGenericQuery : public CQuery
  63. {
  64. public:
  65. CGenericQuery(CGameHandler * gh, PlayerColor color, std::function<void(std::optional<int32_t>)> Callback);
  66. bool blocksPack(const CPack * pack) const override;
  67. bool endsByPlayerAnswer() const override;
  68. void onExposure(QueryPtr topQuery) override;
  69. void setReply(std::optional<int32_t> reply) override;
  70. void onRemoval(PlayerColor color) override;
  71. private:
  72. std::function<void(std::optional<int32_t>)> callback;
  73. std::optional<int32_t> reply;
  74. };