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.

logic.h 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #pragma once
  2. #ifndef LOGIC_H
  3. #define LOGIC_H
  4. #ifdef _MSC_VER
  5. #pragma warning(disable : 4996)
  6. #endif
  7. #include <iostream>
  8. #include <vector>
  9. #include <thread>
  10. #include <mutex>
  11. #include <condition_variable>
  12. #include <atomic>
  13. #include "Message2Server.pb.h"
  14. #include "Message2Clients.pb.h"
  15. #include "MessageType.pb.h"
  16. #include "Services.grpc.pb.h"
  17. #include "Services.pb.h"
  18. #include "API.h"
  19. #include "AI.h"
  20. #include "structures.h"
  21. #include "state.h"
  22. // 封装了通信组件和对AI对象进行操作
  23. class Logic : public ILogic
  24. {
  25. private:
  26. // gRPC客户端的stub,所有与服务端之间的通信操作都需要基于stub完成。
  27. std::unique_ptr<protobuf::AvailableService::Stub> THUAI6Stub;
  28. // ID、阵营记录
  29. int playerID;
  30. THUAI6::PlayerType playerType;
  31. // 类型记录
  32. THUAI6::HumanType humanType;
  33. THUAI6::ButcherType butcherType;
  34. // GUID信息
  35. std::vector<int64_t> playerGUIDs;
  36. // THUAI5中的通信组件可以完全被我们的stub取代,故无须再写
  37. std::unique_ptr<IAI> pAI;
  38. std::unique_ptr<IGameTimer> timer;
  39. std::thread tAI; // 用于运行AI的线程
  40. std::thread tMessage; // 用于读取服务器发来消息的线程
  41. mutable std::mutex mtxAI;
  42. mutable std::mutex mtxState;
  43. mutable std::mutex mtxBuffer;
  44. std::condition_variable cvBuffer;
  45. std::condition_variable cvAI;
  46. // 信息队列目前可能会不用?具体待定
  47. // 存储状态,分别是现在的状态和缓冲区的状态。
  48. State state[2];
  49. State* currentState;
  50. State* bufferState;
  51. // 是否应该执行player()
  52. std::atomic_bool AILoop = true;
  53. // buffer是否更新完毕
  54. bool bufferUpdated = true;
  55. // 是否可以启用当前状态
  56. bool currentStateAccessed = false;
  57. // 是否应当启动AI
  58. bool AIStart = false;
  59. // 控制内容更新的变量
  60. std::atomic_bool freshed = false;
  61. // 提供给API使用的函数
  62. // 获取服务器发来的消息
  63. std::vector<std::shared_ptr<const THUAI6::Butcher>> GetButchers() const override
  64. {
  65. }
  66. std::vector<std::shared_ptr<const THUAI6::Human>> GetHumans() const override
  67. {
  68. }
  69. std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override
  70. {
  71. }
  72. std::shared_ptr<const THUAI6::Human> HumanGetSelfInfo() const override
  73. {
  74. }
  75. std::shared_ptr<const THUAI6::Butcher> ButcherGetSelfInfo() const override
  76. {
  77. }
  78. std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override
  79. {
  80. }
  81. // 供IAPI使用的操作相关的部分
  82. bool Move(protobuf::MoveMsg) override
  83. {
  84. }
  85. bool PickProp(protobuf::PickMsg) override
  86. {
  87. }
  88. bool UseProp(protobuf::IDMsg) override
  89. {
  90. }
  91. bool UseSkill(protobuf::IDMsg) override
  92. {
  93. }
  94. void SendMessage(protobuf::SendMsg) override
  95. {
  96. }
  97. bool HaveMessage(protobuf::IDMsg) override
  98. {
  99. }
  100. protobuf::MsgRes GetMessage(protobuf::IDMsg) override
  101. {
  102. }
  103. bool Escape(protobuf::IDMsg) override
  104. {
  105. }
  106. // 说明:双向stream由三个函数共同实现,两个记录开始和结束,结果由Logic里的私有的成员变量记录,获得返回值则另调函数
  107. bool StartFixMachine(protobuf::IDMsg) override
  108. {
  109. }
  110. bool EndFixMachine(protobuf::IDMsg) override
  111. {
  112. }
  113. bool GetFixStatus() override
  114. {
  115. }
  116. bool StartSaveHuman(protobuf::IDMsg) override
  117. {
  118. }
  119. bool EndSaveHuman(protobuf::IDMsg) override
  120. {
  121. }
  122. bool GetSaveStatus() override
  123. {
  124. }
  125. bool Attack(protobuf::AttackMsg) override
  126. {
  127. }
  128. bool CarryHuman(protobuf::IDMsg) override
  129. {
  130. }
  131. bool ReleaseHuman(protobuf::IDMsg) override
  132. {
  133. }
  134. bool HangHuman(protobuf::IDMsg) override
  135. {
  136. }
  137. bool WaitThread() override
  138. {
  139. }
  140. int GetCounter() override
  141. {
  142. }
  143. bool TryConnection();
  144. // 执行AI线程
  145. void PlayerWrapper(std::function<void()> player);
  146. // THUAI5中的一系列用于处理信息的函数可能也不会再用
  147. void ProcessMessage();
  148. // 将信息加载到buffer
  149. void LoadBuffer(protobuf::MessageToClient&);
  150. // 解锁状态更新线程
  151. void UnBlockBuffer();
  152. // 解锁AI线程
  153. void UnBlockAI();
  154. // 更新状态
  155. void Update() noexcept;
  156. // 等待
  157. void Wait() noexcept;
  158. public:
  159. // 构造函数还需要传更多参数,有待补充
  160. Logic(THUAI6::PlayerType type, int ID, THUAI6::ButcherType butcher, THUAI6::HumanType human);
  161. ~Logic()
  162. {
  163. }
  164. // Main函数同上
  165. void Main(CreateAIFunc createAI, std::string IP, std::string port);
  166. };
  167. #endif