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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <queue>
  14. #include "Message2Server.pb.h"
  15. #include "Message2Clients.pb.h"
  16. #include "MessageType.pb.h"
  17. #include "Services.grpc.pb.h"
  18. #include "Services.pb.h"
  19. #include "API.h"
  20. #include "AI.h"
  21. #include "structures.h"
  22. #include "state.h"
  23. #include "Communication.h"
  24. // 封装了通信组件和对AI对象进行操作
  25. class Logic : public ILogic
  26. {
  27. private:
  28. // gRPC客户端的stub,所有与服务端之间的通信操作都需要基于stub完成。
  29. std::unique_ptr<Communication> pComm;
  30. // ID、阵营记录
  31. int64_t playerID;
  32. THUAI6::PlayerType playerType;
  33. // 类型记录
  34. THUAI6::HumanType humanType;
  35. THUAI6::ButcherType butcherType;
  36. // GUID信息
  37. std::vector<int64_t> playerGUIDs;
  38. // THUAI5中的通信组件可以完全被我们的stub取代,故无须再写
  39. std::unique_ptr<IAI> pAI;
  40. std::unique_ptr<IGameTimer> timer;
  41. std::thread tAI; // 用于运行AI的线程
  42. mutable std::mutex mtxAI;
  43. mutable std::mutex mtxState;
  44. mutable std::mutex mtxBuffer;
  45. std::condition_variable cvBuffer;
  46. std::condition_variable cvAI;
  47. // 信息队列
  48. std::queue<std::pair<int64_t, std::string>> messageQueue;
  49. // 存储状态,分别是现在的状态和缓冲区的状态。
  50. State state[2];
  51. State* currentState;
  52. State* bufferState;
  53. // 保存缓冲区数
  54. int counterState = 0;
  55. int counterBuffer = 0;
  56. // 是否应该执行player()
  57. std::atomic_bool AILoop = true;
  58. // buffer是否更新完毕
  59. bool bufferUpdated = true;
  60. // 是否应当启动AI
  61. bool AIStart = false;
  62. // asynchronous = true 时控制内容更新的变量
  63. std::atomic_bool freshed = false;
  64. // 提供给API使用的函数
  65. // 获取服务器发来的消息
  66. std::vector<std::shared_ptr<const THUAI6::Butcher>> GetButchers() const override;
  67. std::vector<std::shared_ptr<const THUAI6::Human>> GetHumans() const override;
  68. std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override;
  69. std::shared_ptr<const THUAI6::Human> HumanGetSelfInfo() const override;
  70. std::shared_ptr<const THUAI6::Butcher> ButcherGetSelfInfo() const override;
  71. std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override;
  72. THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const override;
  73. // 供IAPI使用的操作相关的部分
  74. bool Move(int64_t time, double angle) override;
  75. bool PickProp(THUAI6::PropType prop) override;
  76. bool UseProp() override;
  77. bool UseSkill() override;
  78. bool SendMessage(int64_t toID, std::string message) override;
  79. bool HaveMessage() override;
  80. std::pair<int64_t, std::string> GetMessage() override;
  81. bool Escape() override;
  82. bool StartFixMachine() override;
  83. bool EndFixMachine() override;
  84. bool StartSaveHuman() override;
  85. bool EndSaveHuman() override;
  86. bool Attack(double angle) override;
  87. bool CarryHuman() override;
  88. bool ReleaseHuman() override;
  89. bool HangHuman() override;
  90. bool WaitThread() override;
  91. int GetCounter() const override;
  92. bool TryConnection();
  93. // THUAI5中的一系列用于处理信息的函数可能也不会再用
  94. void ProcessMessage();
  95. // 将信息加载到buffer
  96. void LoadBuffer(protobuf::MessageToClient&);
  97. // 解锁状态更新线程
  98. void UnBlockBuffer();
  99. // 解锁AI线程
  100. void UnBlockAI();
  101. // 更新状态
  102. void Update() noexcept;
  103. // 等待
  104. void Wait() noexcept;
  105. public:
  106. // 构造函数还需要传更多参数,有待补充
  107. Logic(THUAI6::PlayerType type, int64_t ID, THUAI6::ButcherType butcher, THUAI6::HumanType human);
  108. ~Logic()
  109. {
  110. }
  111. // Main函数同上
  112. void Main(CreateAIFunc createAI, std::string IP, std::string port);
  113. };
  114. #endif