Browse Source

feat(CAPI): complete temp debug api

tags/0.1.0
DragonAura 3 years ago
parent
commit
9bb2a712bc
8 changed files with 415 additions and 186 deletions
  1. +53
    -146
      CAPI/API/include/API.h
  2. +7
    -13
      CAPI/API/include/logic.h
  3. +3
    -0
      CAPI/API/src/AI.cpp
  4. +10
    -0
      CAPI/API/src/API.cpp
  5. +15
    -15
      CAPI/API/src/Communication.cpp
  6. +249
    -0
      CAPI/API/src/DebugAPI.cpp
  7. +75
    -11
      CAPI/API/src/logic.cpp
  8. +3
    -1
      CAPI/API/src/main.cpp

+ 53
- 146
CAPI/API/include/API.h View File

@@ -45,23 +45,24 @@ public:
virtual bool HaveMessage() = 0;
virtual std::pair<int64_t, std::string> GetMessage() = 0;

virtual bool WaitThread() = 0;

virtual int GetCounter() const = 0;

// IHumanAPI使用的部分
virtual bool Escape() = 0;

// 说明:双向stream由三个函数共同实现,两个记录开始和结束,结果由Logic里的私有的成员变量记录,获得返回值则另调函数
virtual bool StartFixMachine() = 0;
virtual bool EndFixMachine() = 0;

virtual bool StartSaveHuman() = 0;
virtual bool EndSaveHuman() = 0;

// IButcherAPI使用的部分
virtual bool Attack(double angle) = 0;
virtual bool CarryHuman() = 0;
virtual bool ReleaseHuman() = 0;
virtual bool HangHuman() = 0;

virtual bool WaitThread() = 0;

virtual int GetCounter() = 0;
};

class IAPI
@@ -201,9 +202,7 @@ public:
[[nodiscard]] std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override;
[[nodiscard]] THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const override;

[[nodiscard]] const std::vector<int64_t> GetPlayerGUIDs() const override
{
}
[[nodiscard]] const std::vector<int64_t> GetPlayerGUIDs() const override;

std::future<bool> StartFixMachine() override;
std::future<bool> EndFixMachine() override;
@@ -262,9 +261,7 @@ public:
[[nodiscard]] std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override;
[[nodiscard]] THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const override;

[[nodiscard]] const std::vector<int64_t> GetPlayerGUIDs() const override
{
}
[[nodiscard]] const std::vector<int64_t> GetPlayerGUIDs() const override;

std::future<bool> Attack(double angleInRadian) override;
std::future<bool> CarryHuman() override;
@@ -291,91 +288,45 @@ public:
}
void Play(IAI& ai) override;

std::future<bool> Move(int64_t timeInMilliseconds, double angleInRadian) override
{
}
std::future<bool> Move(int64_t timeInMilliseconds, double angleInRadian) override;

[[nodiscard]] int GetFrameCount() const override
{
}

std::future<bool> MoveRight(int64_t timeInMilliseconds) override
{
}
std::future<bool> MoveUp(int64_t timeInMilliseconds) override
{
}
std::future<bool> MoveLeft(int64_t timeInMilliseconds) override
{
}
std::future<bool> MoveDown(int64_t timeInMilliseconds) override
{
}
std::future<bool> MoveRight(int64_t timeInMilliseconds) override;
std::future<bool> MoveUp(int64_t timeInMilliseconds) override;
std::future<bool> MoveLeft(int64_t timeInMilliseconds) override;
std::future<bool> MoveDown(int64_t timeInMilliseconds) override;

std::future<bool> PickProp(THUAI6::PropType prop) override
{
}
std::future<bool> UseProp() override
{
}
std::future<bool> UseSkill() override
{
}
std::future<bool> PickProp(THUAI6::PropType prop) override;
std::future<bool> UseProp() override;
std::future<bool> UseSkill() override;

std::future<bool> SendMessage(int64_t, std::string) override
{
}
[[nodiscard]] std::future<bool> HaveMessage() override
{
}
[[nodiscard]] std::future<std::pair<int64_t, std::string>> GetMessage() override
{
}
std::future<bool> SendMessage(int64_t, std::string) override;
[[nodiscard]] std::future<bool> HaveMessage() override;
[[nodiscard]] std::future<std::pair<int64_t, std::string>> GetMessage() override;

std::future<bool> Wait() override
{
}

[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Human>> GetHuman() const override
{
}
[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Butcher>> GetButcher() const override
{
}
[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Human>> GetHuman() const override;
[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Butcher>> GetButcher() const override;

[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override
{
}
[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override;

[[nodiscard]] std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override
{
}
[[nodiscard]] THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const override
{
}
[[nodiscard]] std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override;
[[nodiscard]] THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const override;

[[nodiscard]] const std::vector<int64_t> GetPlayerGUIDs() const override
{
}
[[nodiscard]] const std::vector<int64_t> GetPlayerGUIDs() const override;

std::future<bool> StartFixMachine() override
{
}
std::future<bool> EndFixMachine() override
{
}
std::future<bool> StartSaveHuman() override
{
}
std::future<bool> EndSaveHuman() override
{
}
std::future<bool> Escape() override
{
}
[[nodiscard]] virtual std::shared_ptr<const THUAI6::Human> GetSelfInfo() const override
{
}
std::future<bool> StartFixMachine() override;
std::future<bool> EndFixMachine() override;
std::future<bool> StartSaveHuman() override;
std::future<bool> EndSaveHuman() override;
std::future<bool> Escape() override;
[[nodiscard]] virtual std::shared_ptr<const THUAI6::Human> GetSelfInfo() const override;

private:
ILogic& logic;
@@ -396,88 +347,44 @@ public:
}
void Play(IAI& ai) override;

std::future<bool> Move(int64_t timeInMilliseconds, double angleInRadian) override
{
}
std::future<bool> Move(int64_t timeInMilliseconds, double angleInRadian) override;

[[nodiscard]] int GetFrameCount() const override
{
}

std::future<bool> MoveRight(int64_t timeInMilliseconds) override
{
}
std::future<bool> MoveUp(int64_t timeInMilliseconds) override
{
}
std::future<bool> MoveLeft(int64_t timeInMilliseconds) override
{
}
std::future<bool> MoveDown(int64_t timeInMilliseconds) override
{
}
std::future<bool> MoveRight(int64_t timeInMilliseconds) override;
std::future<bool> MoveUp(int64_t timeInMilliseconds) override;
std::future<bool> MoveLeft(int64_t timeInMilliseconds) override;
std::future<bool> MoveDown(int64_t timeInMilliseconds) override;

std::future<bool> PickProp(THUAI6::PropType prop) override
{
}
std::future<bool> UseProp() override
{
}
std::future<bool> UseSkill() override
{
}
std::future<bool> PickProp(THUAI6::PropType prop) override;
std::future<bool> UseProp() override;
std::future<bool> UseSkill() override;

std::future<bool> SendMessage(int64_t, std::string) override
{
}
[[nodiscard]] std::future<bool> HaveMessage() override
{
}
[[nodiscard]] std::future<std::pair<int64_t, std::string>> GetMessage() override
{
}
std::future<bool> SendMessage(int64_t, std::string) override;
[[nodiscard]] std::future<bool> HaveMessage() override;
[[nodiscard]] std::future<std::pair<int64_t, std::string>> GetMessage() override;

std::future<bool> Wait() override
{
}

[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Human>> GetHuman() const override
{
}
[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Butcher>> GetButcher() const override
{
}
[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Human>> GetHuman() const override;
[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Butcher>> GetButcher() const override;

[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override
{
}
[[nodiscard]] std::vector<std::shared_ptr<const THUAI6::Prop>> GetProps() const override;

[[nodiscard]] std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override
{
}
[[nodiscard]] THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const override
{
}
[[nodiscard]] std::vector<std::vector<THUAI6::PlaceType>> GetFullMap() const override;
[[nodiscard]] THUAI6::PlaceType GetPlaceType(int32_t CellX, int32_t CellY) const override;

[[nodiscard]] const std::vector<int64_t> GetPlayerGUIDs() const override
{
}
[[nodiscard]] const std::vector<int64_t> GetPlayerGUIDs() const override;

std::future<bool> Attack(double angleInRadian) override
{
}
std::future<bool> CarryHuman() override
{
}
std::future<bool> ReleaseHuman() override
{
}
std::future<bool> HangHuman() override
{
}
[[nodiscard]] std::shared_ptr<const THUAI6::Butcher> GetSelfInfo() const override
{
}
std::future<bool> Attack(double angleInRadian) override;
std::future<bool> CarryHuman() override;
std::future<bool> ReleaseHuman() override;
std::future<bool> HangHuman() override;
[[nodiscard]] std::shared_ptr<const THUAI6::Butcher> GetSelfInfo() const override;

private:
ILogic& logic;


+ 7
- 13
CAPI/API/include/logic.h View File

@@ -64,19 +64,20 @@ private:
State* currentState;
State* bufferState;

// 保存缓冲区数
int counterState = 0;
int counterBuffer = 0;

// 是否应该执行player()
std::atomic_bool AILoop = true;

// buffer是否更新完毕
bool bufferUpdated = true;

// 是否可以启用当前状态
bool currentStateAccessed = false;

// 是否应当启动AI
bool AIStart = false;

// 控制内容更新的变量
// asynchronous = true 时控制内容更新的变量
std::atomic_bool freshed = false;

// 提供给API使用的函数
@@ -114,19 +115,12 @@ private:
bool ReleaseHuman() override;
bool HangHuman() override;

bool WaitThread() override
{
}
bool WaitThread() override;

int GetCounter() override
{
}
int GetCounter() const override;

bool TryConnection();

// 执行AI线程
void PlayerWrapper(std::function<void()> player);

// THUAI5中的一系列用于处理信息的函数可能也不会再用

void ProcessMessage();


+ 3
- 0
CAPI/API/src/AI.cpp View File

@@ -2,6 +2,9 @@
#include <thread>
#include "AI.h"

// 为假则play()期间确保游戏状态不更新,为真则只保证游戏状态在调用相关方法时不更新
extern const bool asynchronous = false;

// 选手必须定义该变量来选择自己的阵营
extern const THUAI6::PlayerType playerType = THUAI6::PlayerType::HumanPlayer;



+ 10
- 0
CAPI/API/src/API.cpp View File

@@ -176,6 +176,16 @@ std::vector<std::vector<THUAI6::PlaceType>> ButcherAPI::GetFullMap() const
return logic.GetFullMap();
}

const std::vector<int64_t> HumanAPI::GetPlayerGUIDs() const
{
// todo
}

const std::vector<int64_t> ButcherAPI::GetPlayerGUIDs() const
{
// todo
}

std::future<bool> HumanAPI::StartFixMachine()
{
std::async(std::launch::async, [&]()


+ 15
- 15
CAPI/API/src/Communication.cpp View File

@@ -78,11 +78,11 @@ bool Communication::HaveMessage(int64_t playerID)
protobuf::BoolRes haveMessageResult;
ClientContext context;
auto request = THUAI62Proto::THUAI62ProtobufID(playerID);
auto status = THUAI6Stub->HaveMessage(&context, request, &haveMessageResult);
if (status.ok())
return haveMessageResult.act_success();
else
return false;
// auto status = THUAI6Stub->HaveMessage(&context, request, &haveMessageResult);
// if (status.ok())
// return haveMessageResult.act_success();
// else
// return false;
}

std::pair<int64_t, std::string> Communication::GetMessage(int64_t playerID)
@@ -90,16 +90,16 @@ std::pair<int64_t, std::string> Communication::GetMessage(int64_t playerID)
protobuf::MsgRes getMessageResult;
ClientContext context;
auto request = THUAI62Proto::THUAI62ProtobufID(playerID);
auto status = THUAI6Stub->GetMessage(&context, request, &getMessageResult);
if (status.ok())
{
if (getMessageResult.have_message())
return std::make_pair(getMessageResult.from_player_id(), getMessageResult.message_received());
else
return std::make_pair(-1, "");
}
else
return std::make_pair(-1, "");
// auto status = THUAI6Stub->GetMessage(&context, request, &getMessageResult);
// if (status.ok())
// {
// if (getMessageResult.have_message())
// return std::make_pair(getMessageResult.from_player_id(), getMessageResult.message_received());
// else
// return std::make_pair(-1, "");
// }
// else
// return std::make_pair(-1, "");
}

bool Communication::Escape(int64_t playerID)


+ 249
- 0
CAPI/API/src/DebugAPI.cpp View File

@@ -1,5 +1,254 @@
#include "AI.h"
#include "API.h"
#define PI 3.14159265358979323846

std::future<bool> HumanDebugAPI::Move(int64_t timeInMilliseconds, double angleInRadian)
{
return std::async(std::launch::async, [&]()
{ return logic.Move(timeInMilliseconds, angleInRadian); });
}

std::future<bool> HumanDebugAPI::MoveDown(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, 0);
}

std::future<bool> HumanDebugAPI::MoveRight(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, PI * 0.5);
}

std::future<bool> HumanDebugAPI::MoveUp(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, PI);
}

std::future<bool> HumanDebugAPI::MoveLeft(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, PI * 1.5);
}

std::future<bool> ButcherDebugAPI::Move(int64_t timeInMilliseconds, double angleInRadian)
{
return std::async(std::launch::async, [&]()
{ return logic.Move(timeInMilliseconds, angleInRadian); });
}

std::future<bool> ButcherDebugAPI::MoveDown(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, 0);
}

std::future<bool> ButcherDebugAPI::MoveRight(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, PI * 0.5);
}

std::future<bool> ButcherDebugAPI::MoveUp(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, PI);
}

std::future<bool> ButcherDebugAPI::MoveLeft(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, PI * 1.5);
}

std::future<bool> HumanDebugAPI::PickProp(THUAI6::PropType prop)
{
return std::async(std::launch::async, [&]()
{ return logic.PickProp(prop); });
}

std::future<bool> HumanDebugAPI::UseProp()
{
return std::async(std::launch::async, [&]()
{ return logic.UseProp(); });
}

std::future<bool> ButcherDebugAPI::PickProp(THUAI6::PropType prop)
{
return std::async(std::launch::async, [&]()
{ return logic.PickProp(prop); });
}

std::future<bool> ButcherDebugAPI::UseProp()
{
return std::async(std::launch::async, [&]()
{ return logic.UseProp(); });
}

std::future<bool> HumanDebugAPI::UseSkill()
{
return std::async(std::launch::async, [&]()
{ return logic.UseSkill(); });
}

std::future<bool> ButcherDebugAPI::UseSkill()
{
return std::async(std::launch::async, [&]()
{ return logic.UseSkill(); });
}

std::future<bool> HumanDebugAPI::SendMessage(int64_t toID, std::string message)
{
return std::async(std::launch::async, [&]()
{ return logic.SendMessage(toID, message); });
}

std::future<bool> ButcherDebugAPI::SendMessage(int64_t toID, std::string message)
{
return std::async(std::launch::async, [&]()
{ return logic.SendMessage(toID, message); });
}

std::future<bool> HumanDebugAPI::HaveMessage()
{
return std::async(std::launch::async, [&]()
{ return logic.HaveMessage(); });
}

std::future<bool> ButcherDebugAPI::HaveMessage()
{
return std::async(std::launch::async, [&]()
{ return logic.HaveMessage(); });
}

std::future<std::pair<int64_t, std::string>> HumanDebugAPI::GetMessage()
{
return std::async(std::launch::async, [&]()
{ return logic.GetMessage(); });
}

std::future<std::pair<int64_t, std::string>> ButcherDebugAPI::GetMessage()
{
return std::async(std::launch::async, [&]()
{ return logic.GetMessage(); });
}

std::vector<std::shared_ptr<const THUAI6::Butcher>> HumanDebugAPI::GetButcher() const
{
return logic.GetButchers();
}

std::vector<std::shared_ptr<const THUAI6::Human>> HumanDebugAPI::GetHuman() const
{
return logic.GetHumans();
}

std::vector<std::shared_ptr<const THUAI6::Butcher>> ButcherDebugAPI::GetButcher() const
{
return logic.GetButchers();
}

std::vector<std::shared_ptr<const THUAI6::Human>> ButcherDebugAPI::GetHuman() const
{
return logic.GetHumans();
}

std::vector<std::shared_ptr<const THUAI6::Prop>> HumanDebugAPI::GetProps() const
{
return logic.GetProps();
}

std::vector<std::shared_ptr<const THUAI6::Prop>> ButcherDebugAPI::GetProps() const
{
return logic.GetProps();
}

std::vector<std::vector<THUAI6::PlaceType>> HumanDebugAPI::GetFullMap() const
{
return logic.GetFullMap();
}

THUAI6::PlaceType HumanDebugAPI::GetPlaceType(int32_t CellX, int32_t CellY) const
{
return logic.GetPlaceType(CellX, CellY);
}

THUAI6::PlaceType ButcherDebugAPI::GetPlaceType(int32_t CellX, int32_t CellY) const
{
return logic.GetPlaceType(CellX, CellY);
}

std::vector<std::vector<THUAI6::PlaceType>> ButcherDebugAPI::GetFullMap() const
{
return logic.GetFullMap();
}

const std::vector<int64_t> HumanDebugAPI::GetPlayerGUIDs() const
{
// todo
}

const std::vector<int64_t> ButcherDebugAPI::GetPlayerGUIDs() const
{
// todo
}

std::future<bool> HumanDebugAPI::StartFixMachine()
{
std::async(std::launch::async, [&]()
{ return logic.StartFixMachine(); });
}

std::future<bool> HumanDebugAPI::EndFixMachine()
{
std::async(std::launch::async, [&]()
{ return logic.EndFixMachine(); });
}

std::future<bool> HumanDebugAPI::StartSaveHuman()
{
std::async(std::launch::async, [&]()
{ return logic.StartSaveHuman(); });
}

std::future<bool> HumanDebugAPI::EndSaveHuman()
{
std::async(std::launch::async, [&]()
{ return logic.EndSaveHuman(); });
}

std::future<bool> HumanDebugAPI::Escape()
{
return std::async(std::launch::async, [&]()
{ return logic.Escape(); });
}

std::shared_ptr<const THUAI6::Human> HumanDebugAPI::GetSelfInfo() const
{
return logic.HumanGetSelfInfo();
}

std::future<bool> ButcherDebugAPI::Attack(double angleInRadian)
{
return std::async(std::launch::async, [&]()
{ return logic.Attack(angleInRadian); });
}

std::future<bool> ButcherDebugAPI::CarryHuman()
{
return std::async(std::launch::async, [&]()
{ return logic.CarryHuman(); });
}

std::future<bool> ButcherDebugAPI::ReleaseHuman()
{
return std::async(std::launch::async, [&]()
{ return logic.ReleaseHuman(); });
}

std::future<bool> ButcherDebugAPI::HangHuman()
{
return std::async(std::launch::async, [&]()
{ return logic.HangHuman(); });
}

std::shared_ptr<const THUAI6::Butcher> ButcherDebugAPI::GetSelfInfo() const
{
return logic.ButcherGetSelfInfo();
}

void HumanDebugAPI::Play(IAI& ai)
{


+ 75
- 11
CAPI/API/src/logic.cpp View File

@@ -5,9 +5,7 @@
#include "utils.hpp"
#include "Communication.h"

// using grpc::ClientContext;
// using grpc::Status;

extern const bool asynchronous;
extern const THUAI6::PlayerType playerType;

Logic::Logic(THUAI6::PlayerType type, int64_t ID, THUAI6::ButcherType butcher, THUAI6::HumanType human) :
@@ -22,7 +20,7 @@ Logic::Logic(THUAI6::PlayerType type, int64_t ID, THUAI6::ButcherType butcher, T

std::vector<std::shared_ptr<const THUAI6::Butcher>> Logic::GetButchers() const
{
std::lock_guard<std::mutex> lock(mtxState);
std::lock_guard<std::mutex> lock(mtxBuffer);
std::vector<std::shared_ptr<const THUAI6::Butcher>> temp;
temp.assign(currentState->butchers.begin(), currentState->butchers.end());
return temp;
@@ -30,7 +28,7 @@ std::vector<std::shared_ptr<const THUAI6::Butcher>> Logic::GetButchers() const

std::vector<std::shared_ptr<const THUAI6::Human>> Logic::GetHumans() const
{
std::lock_guard<std::mutex> lock(mtxState);
std::unique_lock<std::mutex> lock(mtxBuffer);
std::vector<std::shared_ptr<const THUAI6::Human>> temp;
temp.assign(currentState->humans.begin(), currentState->humans.end());
return temp;
@@ -38,7 +36,7 @@ std::vector<std::shared_ptr<const THUAI6::Human>> Logic::GetHumans() const

std::vector<std::shared_ptr<const THUAI6::Prop>> Logic::GetProps() const
{
std::lock_guard<std::mutex> lock(mtxState);
std::unique_lock<std::mutex> lock(mtxBuffer);
std::vector<std::shared_ptr<const THUAI6::Prop>> temp;
temp.assign(currentState->props.begin(), currentState->props.end());
return temp;
@@ -46,25 +44,25 @@ std::vector<std::shared_ptr<const THUAI6::Prop>> Logic::GetProps() const

std::shared_ptr<const THUAI6::Human> Logic::HumanGetSelfInfo() const
{
std::lock_guard<std::mutex> lock(mtxState);
std::unique_lock<std::mutex> lock(mtxBuffer);
return currentState->humanSelf;
}

std::shared_ptr<const THUAI6::Butcher> Logic::ButcherGetSelfInfo() const
{
std::lock_guard<std::mutex> lock(mtxState);
std::unique_lock<std::mutex> lock(mtxBuffer);
return currentState->butcherSelf;
}

std::vector<std::vector<THUAI6::PlaceType>> Logic::GetFullMap() const
{
std::lock_guard<std::mutex> lock(mtxState);
std::unique_lock<std::mutex> lock(mtxBuffer);
return currentState->gamemap;
}

THUAI6::PlaceType Logic::GetPlaceType(int32_t CellX, int32_t CellY) const
{
std::lock_guard<std::mutex> lock(mtxState);
std::unique_lock<std::mutex> lock(mtxBuffer);
return currentState->gamemap[CellX][CellY];
}

@@ -148,6 +146,12 @@ bool Logic::HangHuman()
return pComm->HangHuman(playerID);
}

bool Logic::WaitThread()
{
Update();
return true;
}

void Logic::ProcessMessage()
{
auto messageThread = [&]()
@@ -207,12 +211,70 @@ void Logic::LoadBuffer(protobuf::MessageToClient& message)
}

bufferState->gamemap = Proto2THUAI6::Protobuf2THUAI6Map(message.map_message());
cvBuffer.notify_one();
if (asynchronous)
{
{
std::lock_guard<std::mutex> lock(mtxState);
std::swap(currentState, bufferState);
}
freshed = true;
}
else
bufferUpdated = true;
counterBuffer++;
}
// 唤醒其他线程
cvBuffer.notify_one();
}

void Logic::Update() noexcept
{
if (!asynchronous)
{
std::unique_lock<std::mutex> lock(mtxBuffer);

// 缓冲区被更新之后才可以使用
cvBuffer.wait(lock, [&]()
{ return bufferUpdated; });

std::swap(currentState, bufferState);
bufferUpdated = false;
counterState = counterBuffer;
}
}

void Logic::Wait() noexcept
{
freshed = false;
{
std::unique_lock<std::mutex> lock(mtxBuffer);
cvBuffer.wait(lock, [&]()
{ return freshed.load(); });
}
}

void Logic::UnBlockAI()
{
{
std::lock_guard<std::mutex> lock(mtxAI);
AIStart = true;
}
cvAI.notify_one();
}

void Logic::UnBlockBuffer()
{
{
std::lock_guard<std::mutex> lock(mtxBuffer);
bufferUpdated = true;
}
cvBuffer.notify_one();
}

int Logic::GetCounter() const
{
std::unique_lock<std::mutex> lock(mtxState);
return counterState;
}

bool Logic::TryConnection()
@@ -244,6 +306,7 @@ void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port)
cvAI.wait(lock, [this]()
{ return AIStart; });
}
std::cout << "AI Start!" << std::endl;
auto ai = createAI();
ProcessMessage();
while (AILoop)
@@ -265,6 +328,7 @@ void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port)
{
std::cout << "Join the AI thread." << std::endl;
AIStart = true;
cvAI.notify_one();
tAI.join();
}
}


+ 3
- 1
CAPI/API/src/main.cpp View File

@@ -6,11 +6,13 @@
#pragma warning(disable : 4996)
#endif

extern const bool asynchronous;

int THUAI6Main(CreateAIFunc AIBuilder)
{
// 仅供调试使用
int playerID = 123;
std::string sIP = "183.172.208.226";
std::string sIP = "183.172.208.84";
std::string sPort = "8888";
extern const THUAI6::PlayerType playerType;
extern const THUAI6::ButcherType butcherType;


Loading…
Cancel
Save