Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ✨ Add RepairHome and RepairWormhole #366

Merged
merged 13 commits into from
May 16, 2024
20 changes: 14 additions & 6 deletions CAPI/cpp/API/include/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ILogic
[[nodiscard]] virtual std::shared_ptr<const THUAI7::GameInfo> GetGameInfo() const = 0;
[[nodiscard]] virtual std::vector<int64_t> GetPlayerGUIDs() const = 0;
[[nodiscard]] virtual THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual std::pair<int32_t, int32_t> GetConstructionState(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual std::optional<THUAI7::ConstructionState> GetConstructionState(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual int32_t GetWormholeHp(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual int32_t GetResourceState(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual int32_t GetHomeHp() const = 0;
Expand All @@ -64,6 +64,8 @@ class ILogic
virtual bool Move(int64_t time, double angle) = 0;
virtual bool Recover(int64_t recover) = 0;
virtual bool Produce() = 0;
virtual bool RepairWormhole() = 0;
virtual bool RepairHome() = 0;
virtual bool Rebuild(THUAI7::ConstructionType constructionType) = 0;
virtual bool Construct(THUAI7::ConstructionType constructionType) = 0;
virtual bool Attack(double angle) = 0;
Expand Down Expand Up @@ -97,7 +99,7 @@ class IAPI
[[nodiscard]] virtual std::vector<std::vector<THUAI7::PlaceType>> GetFullMap() const = 0;
[[nodiscard]] virtual std::shared_ptr<const THUAI7::GameInfo> GetGameInfo() const = 0;
[[nodiscard]] virtual THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual std::pair<int32_t, int32_t> GetConstructionState(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual std::optional<THUAI7::ConstructionState> GetConstructionState(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual int32_t GetWormholeHp(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual int32_t GetResourceState(int32_t cellX, int32_t cellY) const = 0;
[[nodiscard]] virtual int32_t GetHomeHp() const = 0;
Expand Down Expand Up @@ -139,6 +141,8 @@ class IShipAPI : public IAPI
virtual std::future<bool> Attack(double angleInRadian) = 0;
virtual std::future<bool> Recover(int64_t recover) = 0;
virtual std::future<bool> Produce() = 0;
virtual std::future<bool> RepairWormhole() = 0;
virtual std::future<bool> RepairHome() = 0;
virtual std::future<bool> Rebuild(THUAI7::ConstructionType constructionType) = 0;
virtual std::future<bool> Construct(THUAI7::ConstructionType constructionType) = 0;
virtual std::shared_ptr<const THUAI7::Ship> GetSelfInfo() const = 0;
Expand Down Expand Up @@ -195,6 +199,8 @@ class ShipAPI : public IShipAPI, public IGameTimer
std::future<bool> Attack(double angleInRadian) override;
std::future<bool> Recover(int64_t recover) override;
std::future<bool> Produce() override;
std::future<bool> RepairWormhole() override;
std::future<bool> RepairHome() override;
std::future<bool> Rebuild(THUAI7::ConstructionType constructionType) override;
std::future<bool> Construct(THUAI7::ConstructionType constructionType) override;

Expand All @@ -203,7 +209,7 @@ class ShipAPI : public IShipAPI, public IGameTimer
[[nodiscard]] std::vector<std::shared_ptr<const THUAI7::Bullet>> GetBullets() const override;
[[nodiscard]] std::vector<std::vector<THUAI7::PlaceType>> GetFullMap() const override;
[[nodiscard]] THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] std::pair<int32_t, int32_t> GetConstructionState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] std::optional<THUAI7::ConstructionState> GetConstructionState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] int32_t GetWormholeHp(int32_t x, int32_t y) const override;
[[nodiscard]] int32_t GetResourceState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] std::shared_ptr<const THUAI7::GameInfo> GetGameInfo() const override;
Expand Down Expand Up @@ -259,7 +265,7 @@ class TeamAPI : public ITeamAPI, public IGameTimer
[[nodiscard]] std::vector<std::shared_ptr<const THUAI7::Bullet>> GetBullets() const override;
[[nodiscard]] std::vector<std::vector<THUAI7::PlaceType>> GetFullMap() const override;
[[nodiscard]] THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] std::pair<int32_t, int32_t> GetConstructionState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] std::optional<THUAI7::ConstructionState> GetConstructionState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] int32_t GetWormholeHp(int32_t x, int32_t y) const override;
[[nodiscard]] int32_t GetResourceState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] int32_t GetHomeHp() const override;
Expand Down Expand Up @@ -312,6 +318,8 @@ class ShipDebugAPI : public IShipAPI, public IGameTimer
std::future<bool> Attack(double angleInRadian) override;
std::future<bool> Recover(int64_t recover) override;
std::future<bool> Produce() override;
std::future<bool> RepairWormhole() override;
std::future<bool> RepairHome() override;
std::future<bool> Rebuild(THUAI7::ConstructionType constructionType) override;
std::future<bool> Construct(THUAI7::ConstructionType constructionType) override;

Expand All @@ -320,7 +328,7 @@ class ShipDebugAPI : public IShipAPI, public IGameTimer
[[nodiscard]] std::vector<std::shared_ptr<const THUAI7::Bullet>> GetBullets() const override;
[[nodiscard]] std::vector<std::vector<THUAI7::PlaceType>> GetFullMap() const override;
[[nodiscard]] THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] std::pair<int32_t, int32_t> GetConstructionState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] std::optional<THUAI7::ConstructionState> GetConstructionState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] int32_t GetWormholeHp(int32_t x, int32_t y) const override;
[[nodiscard]] int32_t GetResourceState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] int32_t GetHomeHp() const override;
Expand Down Expand Up @@ -366,7 +374,7 @@ class TeamDebugAPI : public ITeamAPI, public IGameTimer
[[nodiscard]] std::vector<std::shared_ptr<const THUAI7::Bullet>> GetBullets() const override;
[[nodiscard]] std::vector<std::vector<THUAI7::PlaceType>> GetFullMap() const override;
[[nodiscard]] THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] std::pair<int32_t, int32_t> GetConstructionState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] std::optional<THUAI7::ConstructionState> GetConstructionState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] int32_t GetWormholeHp(int32_t x, int32_t y) const override;
[[nodiscard]] int32_t GetResourceState(int32_t cellX, int32_t cellY) const override;
[[nodiscard]] int32_t GetHomeHp() const override;
Expand Down
2 changes: 2 additions & 0 deletions CAPI/cpp/API/include/Communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Communication
bool Move(int32_t playerID, int32_t teamID, int64_t time, double angle);
bool Recover(int32_t playerID, int64_t recover, int32_t teamID);
bool Produce(int32_t playerID, int32_t teamID);
bool RepairWormhole(int32_t playerID, int32_t teamID);
bool RepairHome(int32_t playerID, int32_t teamID);
bool Rebuild(int32_t playerID, int32_t teamID, THUAI7::ConstructionType constructionType);
bool Construct(int32_t playerID, int32_t teamID, THUAI7::ConstructionType constructionType);
bool Attack(int32_t playerID, int32_t teamID, double angle);
Expand Down
5 changes: 4 additions & 1 deletion CAPI/cpp/API/include/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <iostream>
#include <vector>
#include <optional>
#include <thread>
#include <mutex>
#include <condition_variable>
Expand Down Expand Up @@ -98,7 +99,7 @@ class Logic : public ILogic
[[nodiscard]] std::shared_ptr<const THUAI7::GameInfo> GetGameInfo() const;
[[nodiscard]] THUAI7::PlaceType GetPlaceType(int32_t cellX, int32_t cellY) const;
[[nodiscard]] std::vector<int64_t> GetPlayerGUIDs() const;
[[nodiscard]] std::pair<int32_t, int32_t> GetConstructionState(int32_t cellX, int32_t cellY) const;
[[nodiscard]] std::optional<THUAI7::ConstructionState> GetConstructionState(int32_t cellX, int32_t cellY) const;
[[nodiscard]] int32_t GetWormholeHp(int32_t cellX, int32_t cellY) const;
[[nodiscard]] int32_t GetResourceState(int32_t cellX, int32_t cellY) const;
[[nodiscard]] int32_t GetHomeHp() const;
Expand All @@ -117,6 +118,8 @@ class Logic : public ILogic
bool Move(int64_t time, double angle);
bool Recover(int64_t recover);
bool Produce();
bool RepairWormhole();
bool RepairHome();
bool Rebuild(THUAI7::ConstructionType constructionType);
bool Construct(THUAI7::ConstructionType constructionType);
bool Attack(double angle);
Expand Down
13 changes: 13 additions & 0 deletions CAPI/cpp/API/include/structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ namespace THUAI7
int32_t speed; // 子弹速度
};

struct ConstructionState
{
int32_t teamID;
int32_t hp;
ConstructionType constructionType;
ConstructionState(std::pair<int32_t, int32_t> teamHP, ConstructionType type) :
teamID(teamHP.first),
hp(teamHP.second),
constructionType(type)
{
}
};

// struct BombedBullet
// {
// BulletType bulletType,
Expand Down
17 changes: 14 additions & 3 deletions CAPI/cpp/API/src/API.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <optional>
#include "AI.h"
#include "API.h"

Expand Down Expand Up @@ -140,12 +139,12 @@ THUAI7::PlaceType TeamAPI::GetPlaceType(int32_t cellX, int32_t cellY) const
return logic.GetPlaceType(cellX, cellY);
}

std::pair<int32_t, int32_t> ShipAPI::GetConstructionState(int32_t cellX, int32_t cellY) const
std::optional<THUAI7::ConstructionState> ShipAPI::GetConstructionState(int32_t cellX, int32_t cellY) const
{
return logic.GetConstructionState(cellX, cellY);
}

std::pair<int32_t, int32_t> TeamAPI::GetConstructionState(int32_t cellX, int32_t cellY) const
std::optional<THUAI7::ConstructionState> TeamAPI::GetConstructionState(int32_t cellX, int32_t cellY) const
{
return logic.GetConstructionState(cellX, cellY);
}
Expand Down Expand Up @@ -275,6 +274,18 @@ std::future<bool> ShipAPI::Produce()
{ return logic.Produce(); });
}

std::future<bool> ShipAPI::RepairWormhole()
{
return std::async(std::launch::async, [=]()
{ return logic.RepairWormhole(); });
}

std::future<bool> ShipAPI::RepairHome()
{
return std::async(std::launch::async, [=]()
{ return logic.RepairHome(); });
}

std::future<bool> ShipAPI::Rebuild(THUAI7::ConstructionType constructionType)
{
return std::async(std::launch::async, [=]()
Expand Down
38 changes: 38 additions & 0 deletions CAPI/cpp/API/src/Communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,44 @@ bool Communication::Produce(int32_t playerID, int32_t teamID)
return false;
}

bool Communication::RepairWormhole(int32_t playerID, int32_t teamID)
{
{
std::lock_guard<std::mutex> lock(mtxLimit);
if (counter >= limit || counterMove >= moveLimit)
return false;
counter++;
counterMove++;
}
protobuf::BoolRes repairWormholeResult;
ClientContext context;
auto request = THUAI72Proto::THUAI72ProtobufIDMsg(playerID, teamID);
auto status = THUAI7Stub->RepairWormhole(&context, request, &repairWormholeResult);
if (status.ok())
return repairWormholeResult.act_success();
else
return false;
}

bool Communication::RepairHome(int32_t playerID, int32_t teamID)
{
{
std::lock_guard<std::mutex> lock(mtxLimit);
if (counter >= limit || counterMove >= moveLimit)
return false;
counter++;
counterMove++;
}
protobuf::BoolRes repairHomeResult;
ClientContext context;
auto request = THUAI72Proto::THUAI72ProtobufIDMsg(playerID, teamID);
auto status = THUAI7Stub->RepairHome(&context, request, &repairHomeResult);
if (status.ok())
return repairHomeResult.act_success();
else
return false;
}

bool Communication::Rebuild(int32_t playerID, int32_t teamID, THUAI7::ConstructionType constructionType)
{
{
Expand Down
29 changes: 25 additions & 4 deletions CAPI/cpp/API/src/DebugAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,27 @@ std::future<bool> ShipDebugAPI::Produce()
logger->warn("Produce: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

std::future<bool> ShipDebugAPI::RepairWormhole()
{
logger->info("RepairWormhole: called at {}ms", Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result= logic.RepairWormhole();
if(!result)
logger->warn("RepairWormhole: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

std::future<bool> ShipDebugAPI::RepairHome()
{
logger->info("RepairHome: called at {}ms", Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result= logic.RepairHome();
if(!result)
logger->warn("RepairHome: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

std::future<bool> ShipDebugAPI::Rebuild(THUAI7::ConstructionType constructionType)
{
logger->info("Rebuild: rebuilding {}, called at {}ms", THUAI7::constructionDict[constructionType], Time::TimeSinceStart(startPoint));
Expand Down Expand Up @@ -201,7 +222,7 @@ THUAI7::PlaceType ShipDebugAPI::GetPlaceType(int32_t cellX, int32_t cellY) const
return logic.GetPlaceType(cellX, cellY);
}

std::pair<int32_t, int32_t> ShipDebugAPI::GetConstructionState(int32_t cellX, int32_t cellY) const
std::optional<THUAI7::ConstructionState> ShipDebugAPI::GetConstructionState(int32_t cellX, int32_t cellY) const
{
return logic.GetConstructionState(cellX, cellY);
}
Expand Down Expand Up @@ -395,7 +416,7 @@ THUAI7::PlaceType TeamDebugAPI::GetPlaceType(int32_t cellX, int32_t cellY) const
return logic.GetPlaceType(cellX, cellY);
}

std::pair<int32_t, int32_t> TeamDebugAPI::GetConstructionState(int32_t cellX, int32_t cellY) const
std::optional<THUAI7::ConstructionState> TeamDebugAPI::GetConstructionState(int32_t cellX, int32_t cellY) const
{
return logic.GetConstructionState(cellX, cellY);
}
Expand Down Expand Up @@ -446,7 +467,7 @@ std::future<bool> TeamDebugAPI::InstallModule(int32_t playerID, THUAI7::ModuleTy
return std::async(std::launch::async, [=]()
{ auto result= logic.InstallModule(playerID, moduleType);
if(!result)
logger->info("InstallModule: failed at {}ms",Time::TimeSinceStart(startPoint));
logger->warn("InstallModule: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

Expand Down Expand Up @@ -474,7 +495,7 @@ void TeamDebugAPI::PrintSelfInfo() const
{
auto Team = logic.TeamGetSelfInfo();
logger->info("******Self Info******");
logger->info("playerID={}, teamID={}, score={}, energy={}", Team->playerID, Team->teamID, Team->score, Team->energy);
logger->info("teamID={}, playerID={}, score={}, energy={}", Team->teamID, Team->playerID, Team->score, Team->energy);
logger->info("*********************\n");
}

Expand Down
22 changes: 17 additions & 5 deletions CAPI/cpp/API/src/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ THUAI7::PlaceType Logic::GetPlaceType(int32_t cellX, int32_t cellY) const
return currentState->gameMap[cellX][cellY];
}

std::pair<int32_t, int32_t> Logic::GetConstructionState(int32_t cellX, int32_t cellY) const
std::optional<THUAI7::ConstructionState> Logic::GetConstructionState(int32_t cellX, int32_t cellY) const
{
std::unique_lock<std::mutex> lock(mtxState);
logger->debug("Called GetConstructionState");
Expand All @@ -101,16 +101,16 @@ std::pair<int32_t, int32_t> Logic::GetConstructionState(int32_t cellX, int32_t c
auto it3 = currentState->mapInfo->fortState.find(pos);
if (it != currentState->mapInfo->factoryState.end())
{
return currentState->mapInfo->factoryState[pos];
return std::make_optional<THUAI7::ConstructionState>(currentState->mapInfo->factoryState[pos], THUAI7::ConstructionType::Factory);
}
else if (it2 != currentState->mapInfo->communityState.end())
return currentState->mapInfo->communityState[pos];
return std::make_optional<THUAI7::ConstructionState>(currentState->mapInfo->communityState[pos], THUAI7::ConstructionType::Community);
else if (it3 != currentState->mapInfo->fortState.end())
return currentState->mapInfo->fortState[pos];
return std::make_optional<THUAI7::ConstructionState>(currentState->mapInfo->fortState[pos], THUAI7::ConstructionType::Fort);
else
{
logger->warn("Construction not found");
return std::make_pair(-1, -1);
return std::nullopt;
}
}

Expand Down Expand Up @@ -261,6 +261,18 @@ bool Logic::Produce()
return pComm->Produce(playerID, teamID);
}

bool Logic::RepairWormhole()
{
logger->debug("Called RepairWormhole");
return pComm->RepairWormhole(playerID, teamID);
}

bool Logic::RepairHome()
{
logger->debug("Called RepairHome");
return pComm->RepairHome(playerID, teamID);
}

bool Logic::Rebuild(THUAI7::ConstructionType constructionType)
{
logger->debug("Called Rebuild");
Expand Down
10 changes: 8 additions & 2 deletions CAPI/python/PyAPI/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def Recover(self, recover: int) -> Future[bool]:
def Produce(self) -> Future[bool]:
return self.__pool.submit(self.__logic.Produce)

def RepairWormhole(self) -> Future[bool]:
return self.__pool.submit(self.__logic.RepairWormhole)

def RepairHome(self) -> Future[bool]:
return self.__pool.submit(self.__logic.RepairHome)

def Rebuild(self, constructionType: THUAI7.ConstructionType) -> Future[bool]:
return self.__pool.submit(self.__logic.Rebuild, constructionType)

Expand Down Expand Up @@ -76,7 +82,7 @@ def GetFullMap(self) -> List[List[THUAI7.PlaceType]]:
def GetPlaceType(self, cellX: int, cellY: int) -> THUAI7.PlaceType:
return self.__logic.GetPlaceType(cellX, cellY)

def GetConstructionState(self, cellX: int, cellY: int) -> Tuple[int, int]:
def GetConstructionState(self, cellX: int, cellY: int) -> THUAI7.ConstructionState | None:
return self.__logic.GetConstructionState(cellX, cellY)

def GetWormholeHp(self, cellX: int, cellY: int) -> int:
Expand Down Expand Up @@ -184,7 +190,7 @@ def GetFullMap(self) -> List[List[THUAI7.PlaceType]]:
def GetPlaceType(self, cellX: int, cellY: int) -> THUAI7.PlaceType:
return self.__logic.GetPlaceType(cellX, cellY)

def GetConstructionState(self, cellX: int, cellY: int) -> Tuple[int, int]:
def GetConstructionState(self, cellX: int, cellY: int) -> THUAI7.ConstructionState | None:
return self.__logic.GetConstructionState(cellX, cellY)

def GetWormholeHp(self, cellX: int, cellY: int) -> int:
Expand Down
Loading
Loading