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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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;
  40. mutable std::mutex mtxAI;
  41. mutable std::mutex mtxState;
  42. mutable std::mutex mtxBuffer;
  43. std::condition_variable cvBuffer;
  44. std::condition_variable cvAI;
  45. // 信息队列目前可能会不用?具体待定
  46. // 存储状态,分别是现在的状态和缓冲区的状态。
  47. State state[2];
  48. State* currentState;
  49. State* bufferState;
  50. // 是否应该执行player()
  51. std::atomic_bool AILoop = true;
  52. // buffer是否更新完毕
  53. bool bufferUpdated = true;
  54. // 是否可以启用当前状态
  55. bool currentStateAccessed = false;
  56. // 是否应当启动AI
  57. bool AIStart = false;
  58. // 控制内容更新的变量
  59. std::atomic_bool freshed = false;
  60. // 提供给API使用的函数
  61. // 获取服务器发来的所有消息,要注意线程安全问题
  62. protobuf::MessageToClient GetFullMessage() override;
  63. // 供IAPI使用的操作相关的部分
  64. bool Move(protobuf::MoveMsg) override
  65. {
  66. }
  67. bool PickProp(protobuf::PickMsg) override
  68. {
  69. }
  70. bool UseProp(protobuf::IDMsg) override
  71. {
  72. }
  73. bool UseSkill(protobuf::IDMsg) override
  74. {
  75. }
  76. void SendMessage(protobuf::SendMsg) override
  77. {
  78. }
  79. bool HaveMessage(protobuf::IDMsg) override
  80. {
  81. }
  82. protobuf::MsgRes GetMessage(protobuf::IDMsg) override
  83. {
  84. }
  85. bool Escape(protobuf::IDMsg) override
  86. {
  87. }
  88. // 说明:双向stream由三个函数共同实现,两个记录开始和结束,结果由Logic里的私有的成员变量记录,获得返回值则另调函数
  89. bool StartFixMachine(protobuf::IDMsg) override
  90. {
  91. }
  92. bool EndFixMachine(protobuf::IDMsg) override
  93. {
  94. }
  95. bool GetFixStatus() override
  96. {
  97. }
  98. bool StartSaveHuman(protobuf::IDMsg) override
  99. {
  100. }
  101. bool EndSaveHuman(protobuf::IDMsg) override
  102. {
  103. }
  104. bool GetSaveStatus() override
  105. {
  106. }
  107. bool Attack(protobuf::AttackMsg) override
  108. {
  109. }
  110. bool CarryHuman(protobuf::IDMsg) override
  111. {
  112. }
  113. bool ReleaseHuman(protobuf::IDMsg) override
  114. {
  115. }
  116. bool HangHuman(protobuf::IDMsg) override
  117. {
  118. }
  119. bool WaitThread() override
  120. {
  121. }
  122. int GetCounter() override
  123. {
  124. }
  125. bool TryConnection();
  126. // 执行AI线程
  127. void PlayerWrapper(std::function<void()> player);
  128. // THUAI5中的一系列用于处理信息的函数可能也不会再用
  129. // 将信息加载到buffer
  130. void LoadBuffer(std::shared_ptr<protobuf::MessageToClient>);
  131. // 解锁状态更新线程
  132. void UnBlockBuffer();
  133. // 解锁AI线程
  134. void UnBlockAI();
  135. // 更新状态
  136. void Update() noexcept;
  137. // 等待
  138. void Wait() noexcept;
  139. public:
  140. // 构造函数还需要传更多参数,有待补充
  141. Logic(THUAI6::PlayerType type, int playerID, THUAI6::ButcherType butcher, THUAI6::HumanType human);
  142. ~Logic() = default;
  143. // Main函数同上
  144. void Main(CreateAIFunc createAI, std::string IP, std::string port);
  145. };
  146. #endif