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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include "Communication.h"
  23. // 封装了通信组件和对AI对象进行操作
  24. class Logic : public ILogic
  25. {
  26. private:
  27. // gRPC客户端的stub,所有与服务端之间的通信操作都需要基于stub完成。
  28. std::unique_ptr<Communication> pComm;
  29. // ID、阵营记录
  30. int64_t 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; // 用于运行AI的线程
  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. std::vector<std::shared_ptr<const THUAI6::Human>> GetHumans() const override;
  65. std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override;
  66. std::shared_ptr<const THUAI6::Human> HumanGetSelfInfo() const override;
  67. std::shared_ptr<const THUAI6::Butcher> ButcherGetSelfInfo() const override;
  68. std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override;
  69. THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const override;
  70. // 供IAPI使用的操作相关的部分
  71. bool Move(int64_t time, double angle) override;
  72. bool PickProp(THUAI6::PropType prop) override;
  73. bool UseProp() override;
  74. bool UseSkill() override;
  75. bool SendMessage(int64_t toID, std::string message) override;
  76. bool HaveMessage() override;
  77. std::pair<int64_t, std::string> GetMessage() override;
  78. bool Escape() override;
  79. // 说明:双向stream由三个函数共同实现,两个记录开始和结束,结果由Logic里的私有的成员变量记录,获得返回值则另调函数
  80. void StartFixMachine() override;
  81. void EndFixMachine() override;
  82. bool GetFixStatus() override;
  83. void StartSaveHuman() override;
  84. void EndSaveHuman() override;
  85. bool GetSaveStatus() override;
  86. bool Attack(double angle) override;
  87. bool CarryHuman() override;
  88. bool ReleaseHuman() override;
  89. bool HangHuman() override;
  90. bool WaitThread() override
  91. {
  92. }
  93. int GetCounter() override
  94. {
  95. }
  96. bool TryConnection();
  97. // 执行AI线程
  98. void PlayerWrapper(std::function<void()> player);
  99. // THUAI5中的一系列用于处理信息的函数可能也不会再用
  100. void ProcessMessage();
  101. // 将信息加载到buffer
  102. void LoadBuffer(protobuf::MessageToClient&);
  103. // 解锁状态更新线程
  104. void UnBlockBuffer();
  105. // 解锁AI线程
  106. void UnBlockAI();
  107. // 更新状态
  108. void Update() noexcept;
  109. // 等待
  110. void Wait() noexcept;
  111. public:
  112. // 构造函数还需要传更多参数,有待补充
  113. Logic(THUAI6::PlayerType type, int64_t ID, THUAI6::ButcherType butcher, THUAI6::HumanType human);
  114. ~Logic()
  115. {
  116. }
  117. // Main函数同上
  118. void Main(CreateAIFunc createAI, std::string IP, std::string port);
  119. };
  120. #endif