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

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