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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "ConcurrentQueue.hpp"
  28. // 封装了通信组件和对AI对象进行操作
  29. class Logic : public ILogic
  30. {
  31. private:
  32. // 日志组件
  33. std::unique_ptr<spdlog::logger> logger;
  34. // 通信组件
  35. std::unique_ptr<Communication> pComm;
  36. // ID、阵营记录
  37. THUAI6::PlayerType playerType;
  38. int64_t playerID;
  39. // 类型记录
  40. THUAI6::TrickerType trickerType;
  41. THUAI6::StudentType studentType;
  42. std::unique_ptr<IGameTimer> timer;
  43. std::thread tAI; // 用于运行AI的线程
  44. mutable std::mutex mtxAI;
  45. mutable std::mutex mtxState;
  46. mutable std::mutex mtxBuffer;
  47. std::condition_variable cvBuffer;
  48. std::condition_variable cvAI;
  49. // 信息队列
  50. ConcurrentQueue<std::pair<int64_t, std::string>> messageQueue;
  51. // 存储状态,分别是现在的状态和缓冲区的状态。
  52. State state[2];
  53. State* currentState;
  54. State* bufferState;
  55. // 保存缓冲区数
  56. int counterState = 0;
  57. int counterBuffer = 0;
  58. THUAI6::GameState gameState = THUAI6::GameState::NullGameState;
  59. // 是否应该执行player()
  60. std::atomic_bool AILoop = true;
  61. // buffer是否更新完毕
  62. bool bufferUpdated = true;
  63. // 是否应当启动AI
  64. bool AIStart = false;
  65. // asynchronous = true 时控制内容更新的变量
  66. std::atomic_bool freshed = false;
  67. // 提供给API使用的函数
  68. [[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Tricker>> GetTrickers() const override;
  69. [[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Student>> GetStudents() const override;
  70. [[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override;
  71. [[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Bullet>> GetBullets() const override;
  72. [[nodiscard]] std::shared_ptr<const THUAI6::Student> StudentGetSelfInfo() const override;
  73. [[nodiscard]] std::shared_ptr<const THUAI6::Tricker> TrickerGetSelfInfo() const override;
  74. [[nodiscard]] THUAI6::HiddenGateState GetHiddenGateState(int32_t cellX, int32_t cellY) const override;
  75. [[nodiscard]] std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override;
  76. [[nodiscard]] THUAI6::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const override;
  77. [[nodiscard]] bool IsDoorOpen(int32_t cellX, int32_t cellY) const override;
  78. [[nodiscard]] int32_t GetDoorProgress(int32_t cellX, int32_t cellY) const override;
  79. [[nodiscard]] int32_t GetClassroomProgress(int32_t cellX, int32_t cellY) const override;
  80. [[nodiscard]] int32_t GetChestProgress(int32_t cellX, int32_t cellY) const override;
  81. [[nodiscard]] int32_t GetGateProgress(int32_t cellX, int32_t cellY) const override;
  82. [[nodiscard]] std::shared_ptr<const THUAI6::GameInfo> GetGameInfo() const override;
  83. // 供IAPI使用的操作相关的部分
  84. bool Move(int64_t time, double angle) override;
  85. bool PickProp(THUAI6::PropType prop) override;
  86. bool UseProp(THUAI6::PropType prop) override;
  87. bool ThrowProp(THUAI6::PropType prop) override;
  88. bool UseSkill(int32_t skillID) override;
  89. bool SendMessage(int64_t toID, std::string message) override;
  90. bool HaveMessage() override;
  91. std::pair<int64_t, std::string> GetMessage() override;
  92. bool Graduate() override;
  93. bool StartLearning() override;
  94. bool StartEncourageMate(int64_t mateID) override;
  95. bool StartRouseMate(int64_t mateID) override;
  96. bool Attack(double angle) override;
  97. bool OpenDoor() override;
  98. bool CloseDoor() override;
  99. bool SkipWindow() override;
  100. bool StartOpenGate() override;
  101. bool StartOpenChest() override;
  102. bool EndAllAction() override;
  103. bool WaitThread() override;
  104. int GetCounter() const override;
  105. std::vector<int64_t> GetPlayerGUIDs() const override;
  106. bool TryConnection();
  107. void ProcessMessage();
  108. // 将信息加载到buffer
  109. void LoadBufferSelf(const protobuf::MessageToClient& message);
  110. void LoadBufferCase(const protobuf::MessageOfObj& item);
  111. void LoadBuffer(const protobuf::MessageToClient& message);
  112. // 解锁AI线程
  113. void UnBlockAI();
  114. // 更新状态
  115. void Update() noexcept;
  116. // 等待
  117. void Wait() noexcept;
  118. [[nodiscard]] bool HaveView(int gridX, int gridY, int selfX, int selfY, int viewRange) const override;
  119. public:
  120. // 构造函数还需要传更多参数,有待补充
  121. Logic(THUAI6::PlayerType type, int64_t ID, THUAI6::TrickerType tricker, THUAI6::StudentType student);
  122. ~Logic()
  123. {
  124. }
  125. // Main函数同上
  126. void Main(CreateAIFunc createAI, std::string IP, std::string port, bool file, bool print, bool warnOnly);
  127. };
  128. #endif