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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <Message2Server.grpc.pb.h>
  17. #include <Message2Clients.grpc.pb.h>
  18. #include <MessageType.grpc.pb.h>
  19. #include "API.h"
  20. #include "AI.h"
  21. #include "structures.h"
  22. #include "state.h"
  23. // 封装了通信组件和对AI对象进行操作
  24. class Logic : public ILogic
  25. {
  26. private:
  27. // gRPC客户端的stub,所有与服务端之间的通信操作都需要基于stub完成。
  28. std::unique_ptr<protobuf::AvailableService::Stub> THUAI6Stub;
  29. // ID、阵营记录
  30. int playerID;
  31. THUAI6::PlayerType playerType;
  32. // 类型记录
  33. THUAI6::HumanType humanType;
  34. THUAI6::ButcherType butcherType;
  35. // GUID信息
  36. std::vector<int64_t> playerGUIDs;
  37. // THUAI5中的通信组件可以完全被我们的stub取代,故无须再写
  38. std::unique_ptr<IAI> pAI;
  39. std::unique_ptr<IGameTimer> timer;
  40. std::thread tAI;
  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. protobuf::MessageToClient GetFullMessage() override;
  64. // 供IAPI使用的操作相关的部分
  65. bool Move(protobuf::MoveMsg) override
  66. {
  67. }
  68. bool PickProp(protobuf::PickMsg) override
  69. {
  70. }
  71. bool UseProp(protobuf::IDMsg) override
  72. {
  73. }
  74. bool UseSkill(protobuf::IDMsg) override
  75. {
  76. }
  77. void SendMessage(protobuf::SendMsg) override
  78. {
  79. }
  80. bool HaveMessage(protobuf::IDMsg) override
  81. {
  82. }
  83. protobuf::MsgRes GetMessage(protobuf::IDMsg) override
  84. {
  85. }
  86. bool Escape(protobuf::IDMsg) override
  87. {
  88. }
  89. // 说明:双向stream由三个函数共同实现,两个记录开始和结束,结果由Logic里的私有的成员变量记录,获得返回值则另调函数
  90. bool StartFixMachine(protobuf::IDMsg) override
  91. {
  92. }
  93. bool EndFixMachine(protobuf::IDMsg) override
  94. {
  95. }
  96. bool GetFixStatus() override
  97. {
  98. }
  99. bool StartSaveHuman(protobuf::IDMsg) override
  100. {
  101. }
  102. bool EndSaveHuman(protobuf::IDMsg) override
  103. {
  104. }
  105. bool GetSaveStatus() override
  106. {
  107. }
  108. bool Attack(protobuf::AttackMsg) override
  109. {
  110. }
  111. bool CarryHuman(protobuf::IDMsg) override
  112. {
  113. }
  114. bool ReleaseHuman(protobuf::IDMsg) override
  115. {
  116. }
  117. bool HangHuman(protobuf::IDMsg) override
  118. {
  119. }
  120. bool WaitThread() override
  121. {
  122. }
  123. int GetCounter() override
  124. {
  125. }
  126. bool TryConnection();
  127. // 执行AI线程
  128. void PlayerWrapper(std::function<void()> player);
  129. // THUAI5中的一系列用于处理信息的函数可能也不会再用
  130. // 将信息加载到buffer
  131. void LoadBuffer(std::shared_ptr<protobuf::MessageToClient>);
  132. // 解锁状态更新线程
  133. void UnBlockBuffer();
  134. // 解锁AI线程
  135. void UnBlockAI();
  136. // 更新状态
  137. void Update() noexcept;
  138. // 等待
  139. void Wait() noexcept;
  140. public:
  141. // 构造函数还需要传更多参数,有待补充
  142. Logic(THUAI6::PlayerType type, int playerID, THUAI6::ButcherType butcher, THUAI6::HumanType human);
  143. ~Logic() = default;
  144. // Main函数同上
  145. void Main(CreateAIFunc createAI, std::string IP, std::string port);
  146. };
  147. #endif