CQuery.h 3.2 KB

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