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 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // 获取指定格子中心的坐标
  82. [[nodiscard]] static inline int CellToGrid(int cell) noexcept
  83. {
  84. return cell * num_of_grid_per_cell + num_of_grid_per_cell / 2;
  85. }
  86. // 获取指定坐标点所位于的格子的 X 序号
  87. [[nodiscard]] static inline int GridToCell(int grid) noexcept
  88. {
  89. return grid / num_of_grid_per_cell;
  90. }
  91. IAPI(ILogic& logic) :
  92. logic(logic)
  93. {
  94. }
  95. virtual ~IAPI()
  96. {
  97. }
  98. protected:
  99. ILogic& logic;
  100. };
  101. // 给Logic使用的IAPI接口,为了保证面向对象的设计模式
  102. class IAPIForLogic : public IAPI
  103. {
  104. IAPIForLogic(ILogic& logic) :
  105. IAPI(logic)
  106. {
  107. }
  108. virtual void StartTimer() = 0;
  109. virtual void EndTimer() = 0;
  110. };
  111. class IHumanAPI : public IAPIForLogic
  112. {
  113. public:
  114. IHumanAPI(ILogic& logic) :
  115. IAPIForLogic(logic)
  116. {
  117. }
  118. virtual std::future<bool> StartFixMachine() = 0;
  119. virtual std::future<bool> EndFixMachine() = 0;
  120. virtual std::future<bool> GetFixStatus() = 0;
  121. virtual std::future<bool> StartSaveHuman() = 0;
  122. virtual std::future<bool> EndSaveHuman() = 0;
  123. virtual std::future<bool> GetSaveStatus() = 0;
  124. virtual std::future<bool> Escape() = 0;
  125. [[nodiscard]] virtual std::shared_ptr<const THUAI6::Human> GetSelfInfo() const = 0;
  126. };
  127. class IButcherAPI : public IAPIForLogic
  128. {
  129. public:
  130. IButcherAPI(Logic& logic) :
  131. IAPIForLogic(logic)
  132. {
  133. }
  134. virtual std::future<bool> Attack(double angleInRadian) = 0;
  135. virtual std::future<bool> CarryHuman() = 0;
  136. virtual std::future<bool> ReleaseHuman() = 0;
  137. virtual std::future<bool> HangHuman() = 0;
  138. [[nodiscard]] virtual std::shared_ptr<const THUAI6::Buthcer> GetSelfInfo() const = 0;
  139. };
  140. class HumanAPI : public IHumanAPI
  141. {
  142. public:
  143. HumanAPI(Logic& logic) :
  144. IHumanAPI(logic)
  145. {
  146. }
  147. };
  148. class DebugHumanAPI : public IHumanAPI
  149. {
  150. public:
  151. DebugHumanAPI(Logic& logic) :
  152. IHumanAPI(logic)
  153. {
  154. }
  155. };
  156. class ButhcerAPI : public IButcherAPI
  157. {
  158. public:
  159. ButhcerAPI(Logic& logic) :
  160. IButcherAPI(logic)
  161. {
  162. }
  163. };
  164. class DebugButcherAPI : public IButcherAPI
  165. {
  166. public:
  167. DebugButcherAPI(Logic& logic) :
  168. IButcherAPI(logic)
  169. {
  170. }
  171. };
  172. #endif