CQuery.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 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(QueriesProcessor * Owner);
  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(const JsonNode & reply);
  43. virtual ~CQuery();
  44. protected:
  45. QueriesProcessor * owner;
  46. void addPlayer(PlayerColor color);
  47. bool blockAllButReply(const CPack * pack) const;
  48. };
  49. std::ostream &operator<<(std::ostream &out, const CQuery &query);
  50. std::ostream &operator<<(std::ostream &out, QueryPtr query);
  51. class CGhQuery : public CQuery
  52. {
  53. public:
  54. CGhQuery(CGameHandler * owner);
  55. protected:
  56. CGameHandler * gh;
  57. };
  58. class CDialogQuery : public CGhQuery
  59. {
  60. public:
  61. CDialogQuery(CGameHandler * owner);
  62. virtual bool endsByPlayerAnswer() const override;
  63. virtual bool blocksPack(const CPack *pack) const override;
  64. void setReply(const JsonNode & reply) override;
  65. protected:
  66. std::optional<ui32> answer;
  67. };
  68. class CGenericQuery : public CQuery
  69. {
  70. public:
  71. CGenericQuery(QueriesProcessor * Owner, PlayerColor color, std::function<void(const JsonNode &)> Callback);
  72. bool blocksPack(const CPack * pack) const override;
  73. bool endsByPlayerAnswer() const override;
  74. void onExposure(QueryPtr topQuery) override;
  75. void setReply(const JsonNode & reply) override;
  76. void onRemoval(PlayerColor color) override;
  77. private:
  78. std::function<void(const JsonNode &)> callback;
  79. JsonNode reply;
  80. };