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

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