You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

API.h 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #pragma once
  2. #ifndef API_H
  3. #define API_H
  4. #ifdef _MSC_VER
  5. #pragma warning(disable : 4996)
  6. #endif
  7. #include <Message2Server.pb.h>
  8. #include <Message2Clients.pb.h>
  9. #include <MessageType.pb.h>
  10. #include <Message2Server.grpc.pb.h>
  11. #include <Message2Clients.grpc.pb.h>
  12. #include <MessageType.grpc.pb.h>
  13. #include <future>
  14. #include <iostream>
  15. #include <vector>
  16. #include "structures.h"
  17. const constexpr int num_of_grid_per_cell = 1000;
  18. class ILogic
  19. {
  20. // API中依赖Logic的部分
  21. public:
  22. // 获取服务器发来的所有消息,要注意线程安全问题
  23. virtual protobuf::MessageToClient GetFullMessage() = 0;
  24. // 供IAPI使用的操作相关的部分
  25. virtual bool Move(protobuf::MoveMsg) = 0;
  26. virtual bool PickProp(protobuf::PickMsg) = 0;
  27. virtual bool UseProp(protobuf::IDMsg) = 0;
  28. virtual bool UseSkill(protobuf::IDMsg) = 0;
  29. virtual void SendMessage(protobuf::SendMsg) = 0;
  30. virtual bool HaveMessage(protobuf::IDMsg) = 0;
  31. virtual protobuf::MsgRes GetMessage(protobuf::IDMsg) = 0;
  32. virtual bool Escape(protobuf::IDMsg) = 0;
  33. // 说明:双向stream由三个函数共同实现,两个记录开始和结束,结果由Logic里的私有的成员变量记录,获得返回值则另调函数
  34. virtual bool StartFixMachine(protobuf::IDMsg) = 0;
  35. virtual bool EndFixMachine(protobuf::IDMsg) = 0;
  36. virtual bool GetFixStatus() = 0;
  37. virtual bool StartSaveHuman(protobuf::IDMsg) = 0;
  38. virtual bool EndSaveHuman(protobuf::IDMsg) = 0;
  39. virtual bool GetSaveStatus() = 0;
  40. virtual bool Attack(protobuf::AttackMsg) = 0;
  41. virtual bool CarryHuman(protobuf::IDMsg) = 0;
  42. virtual bool ReleaseHuman(protobuf::IDMsg) = 0;
  43. virtual bool HangHuman(protobuf::IDMsg) = 0;
  44. virtual bool WaitThread() = 0;
  45. virtual int GetCounter() = 0;
  46. };
  47. class IAPI
  48. {
  49. public:
  50. // 选手可执行的操作,应当保证所有函数的返回值都应当为std::future,例如下面的移动函数:
  51. // 指挥本角色进行移动,`timeInMilliseconds` 为移动时间,单位为毫秒;`angleInRadian` 表示移动的方向,单位是弧度,使用极坐标——竖直向下方向为 x 轴,水平向右方向为 y 轴
  52. virtual std::future<bool> Move(uint32_t timeInMilliseconds, double angleInRadian) = 0;
  53. // 向特定方向移动
  54. virtual std::future<bool> MoveRight(uint32_t timeInMilliseconds) = 0;
  55. virtual std::future<bool> MoveUp(uint32_t timeInMilliseconds) = 0;
  56. virtual std::future<bool> MoveLeft(uint32_t timeInMilliseconds) = 0;
  57. virtual std::future<bool> MoveDown(uint32_t timeInMilliseconds) = 0;
  58. // 捡道具、使用技能
  59. virtual std::future<bool> PickProp() = 0;
  60. virtual std::future<bool> UseProp() = 0;
  61. virtual std::future<bool> UseSkill() = 0;
  62. // 发送信息、接受信息
  63. virtual std::future<bool> SendMessage(int, std::string) = 0;
  64. [[nodiscard]] virtual std::future<bool> HaveMessage() = 0;
  65. [[nodiscard]] virtual std::future<std::pair<int, std::string>> GetMessage() = 0;
  66. // 等待下一帧
  67. virtual std::future<bool> Wait() = 0;
  68. // 获取视野内可见的人类/屠夫的信息
  69. [[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI6::Human>> GetHuman() const = 0;
  70. [[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI6::Buthcer>> GetButcher() const = 0;
  71. // 获取视野内可见的道具信息
  72. [[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const = 0;
  73. // 获取地图信息,视野外的地图统一为Land
  74. [[nodiscard]] virtual std::array<std::array<THUAI6::PlaceType, 50>, 50> GetFullMap() const = 0;
  75. [[nodiscard]] virtual THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const = 0;
  76. // 获取所有玩家的GUID
  77. [[nodiscard]] virtual const std::vector<int64_t> GetPlayerGUIDs() const = 0;
  78. // 获取游戏目前所进行的帧数
  79. [[nodiscard]] virtual int GetFrameCount() const = 0;
  80. /*****人类阵营的特定函数*****/
  81. virtual std::future<bool> StartFixMachine() = 0;
  82. virtual std::future<bool> EndFixMachine() = 0;
  83. virtual std::future<bool> GetFixStatus() = 0;
  84. virtual std::future<bool> StartSaveHuman() = 0;
  85. virtual std::future<bool> EndSaveHuman() = 0;
  86. virtual std::future<bool> GetSaveStatus() = 0;
  87. virtual std::future<bool> Escape() = 0;
  88. [[nodiscard]] virtual std::shared_ptr<const THUAI6::Human> HumanGetSelfInfo() const = 0;
  89. /*****屠夫阵营的特定函数*****/
  90. virtual std::future<bool> Attack(double angleInRadian) = 0;
  91. virtual std::future<bool> CarryHuman() = 0;
  92. virtual std::future<bool> ReleaseHuman() = 0;
  93. virtual std::future<bool> HangHuman() = 0;
  94. [[nodiscard]] virtual std::shared_ptr<const THUAI6::Buthcer> ButcherGetSelfInfo() const = 0;
  95. /*****选手可能用的辅助函数*****/
  96. // 获取指定格子中心的坐标
  97. [[nodiscard]] static inline int CellToGrid(int cell) noexcept
  98. {
  99. return cell * num_of_grid_per_cell + num_of_grid_per_cell / 2;
  100. }
  101. // 获取指定坐标点所位于的格子的 X 序号
  102. [[nodiscard]] static inline int GridToCell(int grid) noexcept
  103. {
  104. return grid / num_of_grid_per_cell;
  105. }
  106. IAPI(ILogic& logic) :
  107. logic(logic)
  108. {
  109. }
  110. virtual ~IAPI()
  111. {
  112. }
  113. protected:
  114. ILogic& logic;
  115. };
  116. // 给Logic使用的IAPI接口,为了保证面向对象的设计模式
  117. class IAPIForLogic : public IAPI
  118. {
  119. public:
  120. IAPIForLogic(ILogic& logic) :
  121. IAPI(logic)
  122. {
  123. }
  124. virtual void StartTimer() = 0;
  125. virtual void EndTimer() = 0;
  126. };
  127. class HumanAPI : public IAPIForLogic
  128. {
  129. public:
  130. HumanAPI(ILogic& logic) :
  131. IAPIForLogic(logic)
  132. {
  133. }
  134. };
  135. class ButcherAPI : public IAPIForLogic
  136. {
  137. public:
  138. ButcherAPI(ILogic& logic) :
  139. IAPIForLogic(logic)
  140. {
  141. }
  142. };
  143. // class IHumanAPI : public IAPIForLogic
  144. // {
  145. // public:
  146. // IHumanAPI(ILogic& logic) :
  147. // IAPIForLogic(logic)
  148. // {
  149. // }
  150. // virtual std::future<bool> StartFixMachine() = 0;
  151. // virtual std::future<bool> EndFixMachine() = 0;
  152. // virtual std::future<bool> GetFixStatus() = 0;
  153. // virtual std::future<bool> StartSaveHuman() = 0;
  154. // virtual std::future<bool> EndSaveHuman() = 0;
  155. // virtual std::future<bool> GetSaveStatus() = 0;
  156. // virtual std::future<bool> Escape() = 0;
  157. // [[nodiscard]] virtual std::shared_ptr<const THUAI6::Human> GetSelfInfo() const = 0;
  158. // };
  159. // class IButcherAPI : public IAPIForLogic
  160. // {
  161. // public:
  162. // IButcherAPI(Logic& logic) :
  163. // IAPIForLogic(logic)
  164. // {
  165. // }
  166. // virtual std::future<bool> Attack(double angleInRadian) = 0;
  167. // virtual std::future<bool> CarryHuman() = 0;
  168. // virtual std::future<bool> ReleaseHuman() = 0;
  169. // virtual std::future<bool> HangHuman() = 0;
  170. // [[nodiscard]] virtual std::shared_ptr<const THUAI6::Buthcer> GetSelfInfo() const = 0;
  171. // };
  172. // class HumanAPI : public IHumanAPI
  173. // {
  174. // public:
  175. // HumanAPI(Logic& logic) :
  176. // IHumanAPI(logic)
  177. // {
  178. // }
  179. // };
  180. // class DebugHumanAPI : public IHumanAPI
  181. // {
  182. // public:
  183. // DebugHumanAPI(Logic& logic) :
  184. // IHumanAPI(logic)
  185. // {
  186. // }
  187. // };
  188. // class ButhcerAPI : public IButcherAPI
  189. // {
  190. // public:
  191. // ButhcerAPI(Logic& logic) :
  192. // IButcherAPI(logic)
  193. // {
  194. // }
  195. // };
  196. // class DebugButcherAPI : public IButcherAPI
  197. // {
  198. // public:
  199. // DebugButcherAPI(Logic& logic) :
  200. // IButcherAPI(logic)
  201. // {
  202. // }
  203. // };
  204. #endif