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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. int64_t 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. 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. std::vector<std::shared_ptr<const THUAI6::Butcher>> GetButchers() const override;
  63. std::vector<std::shared_ptr<const THUAI6::Human>> GetHumans() const override;
  64. std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override;
  65. std::shared_ptr<const THUAI6::Human> HumanGetSelfInfo() const override;
  66. std::shared_ptr<const THUAI6::Butcher> ButcherGetSelfInfo() const override;
  67. std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override;
  68. // 供IAPI使用的操作相关的部分
  69. bool Move(int64_t time, double angle) override;
  70. bool PickProp(THUAI6::PropType prop) override;
  71. bool UseProp() override;
  72. bool UseSkill() override;
  73. bool SendMessage(int64_t toID, std::string message) override;
  74. bool HaveMessage() override;
  75. std::pair<std::string, int64_t> GetMessage() override;
  76. bool Escape() override;
  77. // 说明:双向stream由三个函数共同实现,两个记录开始和结束,结果由Logic里的私有的成员变量记录,获得返回值则另调函数
  78. bool StartFixMachine() override
  79. {
  80. }
  81. bool EndFixMachine() override
  82. {
  83. }
  84. bool GetFixStatus() override
  85. {
  86. }
  87. bool StartSaveHuman() override
  88. {
  89. }
  90. bool EndSaveHuman() override
  91. {
  92. }
  93. bool GetSaveStatus() override
  94. {
  95. }
  96. bool Attack(double angle) override
  97. {
  98. }
  99. bool CarryHuman() override
  100. {
  101. }
  102. bool ReleaseHuman() override
  103. {
  104. }
  105. bool HangHuman() override
  106. {
  107. }
  108. bool WaitThread() override
  109. {
  110. }
  111. int GetCounter() override
  112. {
  113. }
  114. bool TryConnection();
  115. // 执行AI线程
  116. void PlayerWrapper(std::function<void()> player);
  117. // THUAI5中的一系列用于处理信息的函数可能也不会再用
  118. void ProcessMessage();
  119. // 将信息加载到buffer
  120. void LoadBuffer(protobuf::MessageToClient&);
  121. // 解锁状态更新线程
  122. void UnBlockBuffer();
  123. // 解锁AI线程
  124. void UnBlockAI();
  125. // 更新状态
  126. void Update() noexcept;
  127. // 等待
  128. void Wait() noexcept;
  129. public:
  130. // 构造函数还需要传更多参数,有待补充
  131. Logic(THUAI6::PlayerType type, int64_t ID, THUAI6::ButcherType butcher, THUAI6::HumanType human);
  132. ~Logic()
  133. {
  134. }
  135. // Main函数同上
  136. void Main(CreateAIFunc createAI, std::string IP, std::string port);
  137. };
  138. #endif