CQuery.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "../../lib/JsonNode.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. struct CPack;
  15. VCMI_LIB_NAMESPACE_END
  16. class CObjectVisitQuery;
  17. class QueriesProcessor;
  18. class CQuery;
  19. class CGameHandler;
  20. using QueryPtr = std::shared_ptr<CQuery>;
  21. // This class represents any kind of prolonged interaction that may need to do something special after it is over.
  22. // It does not necessarily has to be "query" requiring player action, it can be also used internally within server.
  23. // Examples:
  24. // - all kinds of blocking dialog windows
  25. // - battle
  26. // - object visit
  27. // - hero movement
  28. // Queries can cause another queries, forming a stack of queries for each player. Eg: hero movement -> object visit -> dialog.
  29. class CQuery
  30. {
  31. public:
  32. std::vector<PlayerColor> players; //players that are affected (often "blocked") by query
  33. QueryID queryID;
  34. CQuery(QueriesProcessor * Owner);
  35. virtual bool blocksPack(const CPack *pack) const; //query can block attempting actions by player. Eg. he can't move hero during the battle.
  36. virtual bool endsByPlayerAnswer() const; //query is removed after player gives answer (like dialogs)
  37. virtual void onAdding(PlayerColor color); //called just before query is pushed on stack
  38. virtual void onAdded(PlayerColor color); //called right after query is pushed on stack
  39. virtual void onRemoval(PlayerColor color); //called after query is removed from stack
  40. virtual void onExposure(QueryPtr topQuery);//called when query immediately above is removed and this is exposed (becomes top)
  41. virtual std::string toString() const;
  42. virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const;
  43. virtual void setReply(const JsonNode & reply);
  44. virtual ~CQuery();
  45. protected:
  46. QueriesProcessor * owner;
  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 CGhQuery : public CQuery
  53. {
  54. public:
  55. CGhQuery(CGameHandler * owner);
  56. protected:
  57. CGameHandler * gh;
  58. };
  59. class CDialogQuery : public CGhQuery
  60. {
  61. public:
  62. CDialogQuery(CGameHandler * owner);
  63. virtual bool endsByPlayerAnswer() const override;
  64. virtual bool blocksPack(const CPack *pack) const override;
  65. void setReply(const JsonNode & reply) override;
  66. protected:
  67. std::optional<ui32> answer;
  68. };
  69. class CGenericQuery : public CQuery
  70. {
  71. public:
  72. CGenericQuery(QueriesProcessor * Owner, PlayerColor color, std::function<void(const JsonNode &)> Callback);
  73. bool blocksPack(const CPack * pack) const override;
  74. bool endsByPlayerAnswer() const override;
  75. void onExposure(QueryPtr topQuery) override;
  76. void setReply(const JsonNode & reply) override;
  77. void onRemoval(PlayerColor color) override;
  78. private:
  79. std::function<void(const JsonNode &)> callback;
  80. JsonNode reply;
  81. };