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 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // 通信组件
  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. 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. std::queue<std::pair<int64_t, std::string>> messageQueue;
  47. // 存储状态,分别是现在的状态和缓冲区的状态。
  48. State state[2];
  49. State* currentState;
  50. State* bufferState;
  51. // 保存缓冲区数
  52. int counterState = 0;
  53. int counterBuffer = 0;
  54. THUAI6::GameState gameState = THUAI6::GameState::NullGameState;
  55. // 是否应该执行player()
  56. std::atomic_bool AILoop = true;
  57. // buffer是否更新完毕
  58. bool bufferUpdated = true;
  59. // 是否应当启动AI
  60. bool AIStart = false;
  61. // asynchronous = true 时控制内容更新的变量
  62. std::atomic_bool freshed = false;
  63. // 提供给API使用的函数
  64. std::vector<std::shared_ptr<const THUAI6::Butcher>> GetButchers() const override;
  65. std::vector<std::shared_ptr<const THUAI6::Human>> GetHumans() const override;
  66. std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override;
  67. std::shared_ptr<const THUAI6::Human> HumanGetSelfInfo() const override;
  68. std::shared_ptr<const THUAI6::Butcher> ButcherGetSelfInfo() const override;
  69. std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override;
  70. THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const override;
  71. // 供IAPI使用的操作相关的部分
  72. bool Move(int64_t time, double angle) override;
  73. bool PickProp(THUAI6::PropType prop) override;
  74. bool UseProp() override;
  75. bool UseSkill() override;
  76. bool SendMessage(int64_t toID, std::string message) override;
  77. bool HaveMessage() override;
  78. std::optional<std::pair<int64_t, std::string>> GetMessage() override;
  79. bool Escape() override;
  80. bool StartFixMachine() override;
  81. bool EndFixMachine() override;
  82. bool StartSaveHuman() override;
  83. bool EndSaveHuman() override;
  84. bool Attack(double angle) override;
  85. bool CarryHuman() override;
  86. bool ReleaseHuman() override;
  87. bool HangHuman() override;
  88. bool WaitThread() override;
  89. int GetCounter() const override;
  90. const std::vector<int64_t> GetPlayerGUIDs() const override;
  91. bool TryConnection();
  92. void ProcessMessage();
  93. // 将信息加载到buffer
  94. void LoadBuffer(protobuf::MessageToClient&);
  95. // 解锁状态更新线程
  96. void UnBlockBuffer();
  97. // 解锁AI线程
  98. void UnBlockAI();
  99. // 更新状态
  100. void Update() noexcept;
  101. // 等待
  102. void Wait() noexcept;
  103. public:
  104. // 构造函数还需要传更多参数,有待补充
  105. Logic(THUAI6::PlayerType type, int64_t ID, THUAI6::ButcherType butcher, THUAI6::HumanType human);
  106. ~Logic()
  107. {
  108. }
  109. // Main函数同上
  110. void Main(CreateAIFunc createAI, std::string IP, std::string port, bool level, std::string filename);
  111. };
  112. #endif