Skip to content

Commit

Permalink
Merge branch 'dev' of ssh://ssh.github.com:443/hhzzff/THUAI7 into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hhzzff committed Apr 15, 2024
2 parents 0642f54 + 99df575 commit c6afd7d
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 46 deletions.
27 changes: 27 additions & 0 deletions CAPI/cpp/API/include/structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,33 @@ namespace THUAI7
{BulletType::Arc, "Arc"},
};

inline std::map<ConstructionType, std::string> constructionDict{
{ConstructionType::NullConstructionType, "NullConstructionType"},
{ConstructionType::Community, "Community"},
{ConstructionType::Fort, "Fort"},
{ConstructionType::Factory, "Factory"},
};

inline std::map<ModuleType, std::string> moduleTypeDict{
{ModuleType::NullModuleType, "NullModuleType"},
{ModuleType::ModuleProducer1, "ModuleProducer1"},
{ModuleType::ModuleProducer2, "ModuleProducer2"},
{ModuleType::ModuleProducer3, "ModuleProducer3"},
{ModuleType::ModuleConstructor1, "ModuleConstructor1"},
{ModuleType::ModuleConstructor2, "ModuleConstructor2"},
{ModuleType::ModuleConstructor3, "ModuleConstructor3"},
{ModuleType::ModuleArmor1, "ModuleArmor1"},
{ModuleType::ModuleArmor2, "ModuleArmor2"},
{ModuleType::ModuleArmor3, "ModuleArmor3"},
{ModuleType::ModuleShield1, "ModuleShield1"},
{ModuleType::ModuleShield2, "ModuleShield2"},
{ModuleType::ModuleShield3, "ModuleShield3"},
{ModuleType::ModuleLaserGun, "ModuleLaserGun"},
{ModuleType::ModulePlasmaGun, "ModulePlasmaGun"},
{ModuleType::ModuleShellGun, "ModuleShellGun"},
{ModuleType::ModuleMissileGun, "ModuleMissileGun"},
{ModuleType::ModuleArcGun, "ModuleArcGun"}};

inline std::map<MessageOfObj, std::string> messageOfObjDict{
{MessageOfObj::NullMessageOfObj, "NullMessageOfObj"},
{MessageOfObj::ShipMessage, "ShipMessage"},
Expand Down
23 changes: 12 additions & 11 deletions CAPI/cpp/API/src/AI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ extern const bool asynchronous = false;

// 选手需要依次将player1到player4的船类型在这里定义
extern const std::array<THUAI7::ShipType, 4> ShipTypeDict = {
THUAI7::ShipType::CivilianShip,
THUAI7::ShipType::MilitaryShip,
THUAI7::ShipType::CivilianShip,
THUAI7::ShipType::MilitaryShip,
THUAI7::ShipType::FlagShip,
};
Expand All @@ -22,31 +22,32 @@ void AI::play(IShipAPI& api)
{
if (this->playerID == 1)
{
api.Move(10, 2.1);
api.PrintSelfInfo();
api.Attack(1.1);
}
else if (this->playerID == 2)
{
api.Move(10, 2.1);
api.MoveDown(100);
std::this_thread::sleep_for(std::chrono::seconds(1));
api.MoveLeft(100);
api.PrintSelfInfo();
api.PrintShip();
api.Recover(10);
std::this_thread::sleep_for(std::chrono::seconds(1));
api.Attack(1.5);
}

else if (this->playerID == 3)
{
api.Move(10, 2.1);
api.PrintSelfInfo();
}
else if (this->playerID == 4)
{
api.Move(10, 2.1);
api.PrintSelfInfo();
api.MoveDown(100);
std::this_thread::sleep_for(std::chrono::seconds(1));
api.MoveLeft(100);
}
}

void AI::play(ITeamAPI& api) // 默认team playerID 为0
{
api.PrintSelfInfo();
api.InstallModule(1, THUAI7::ModuleType::ModuleArcGun);
api.Recycle(1);
api.InstallModule(2, THUAI7::ModuleType::ModuleLaserGun);
}
2 changes: 2 additions & 0 deletions CAPI/cpp/API/src/Communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ bool Communication::Move(int32_t playerID, int32_t teamID, int64_t time, double
auto request = THUAI72Proto::THUAI72ProtobufMoveMsg(playerID, teamID, time, angle);
auto status = THUAI7Stub->Move(&context, request, &moveResult);
if (status.ok())
{
return moveResult.act_success();
}
else
return false;
}
Expand Down
36 changes: 30 additions & 6 deletions CAPI/cpp/API/src/DebugAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,40 @@ std::future<bool> ShipDebugAPI::Attack(double angleInRadian)

std::future<bool> ShipDebugAPI::Recover(int64_t recover)
{
logger->info("Recover: recover={}, called at {}ms", recover, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ return logic.Recover(recover); });
{ auto result= logic.Recover(recover);
if(!result)
logger->warn("Recover: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> ShipDebugAPI::Produce()
{
logger->info("Produce: called at {}ms", Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ return logic.Produce(); });
{ auto result= logic.Produce();
if(!result)
logger->warn("Produce: 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));
return std::async(std::launch::async, [=]()
{ return logic.Rebuild(constructionType); });
{ auto result= logic.Rebuild(constructionType);
if(!result)
logger->warn("Rebuild: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

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

std::vector<std::shared_ptr<const THUAI7::Ship>> ShipDebugAPI::GetShips() const
Expand Down Expand Up @@ -426,8 +442,12 @@ int32_t TeamDebugAPI::GetScore() const

std::future<bool> TeamDebugAPI::InstallModule(int32_t playerID, THUAI7::ModuleType moduleType)
{
logger->info("InstallModule: install {} for ship {}, called at {}ms", THUAI7::moduleTypeDict[moduleType], playerID, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ return logic.InstallModule(playerID, moduleType); });
{ auto result= logic.InstallModule(playerID, moduleType);
if(!result)
logger->info("InstallModule: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

std::future<bool> TeamDebugAPI::Recycle(int32_t playerID)
Expand All @@ -442,8 +462,12 @@ std::future<bool> TeamDebugAPI::Recycle(int32_t playerID)

std::future<bool> TeamDebugAPI::BuildShip(THUAI7::ShipType ShipType, int32_t birthIndex)
{
logger->info("BuildShip: build {} at birthIndex {}, called at {}ms", THUAI7::shipTypeDict[ShipType], birthIndex, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ return logic.BuildShip(ShipType, birthIndex); });
{ auto result=logic.BuildShip(ShipType, birthIndex);
if(!result)
logger->warn("BuildShip: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

void TeamDebugAPI::PrintSelfInfo() const
Expand Down
6 changes: 4 additions & 2 deletions CAPI/cpp/API/src/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Logic::Logic(int32_t pID, int32_t tID, THUAI7::PlayerType pType, THUAI7::ShipTyp
bufferState->mapInfo = std::make_shared<THUAI7::GameMap>();
if (teamID == 0)
playerTeam = THUAI7::PlayerTeam::Red;
if (teamID == 1)
else if (teamID == 1)
playerTeam = THUAI7::PlayerTeam::Blue;
else
playerTeam = THUAI7::PlayerTeam::NullTeam;
Expand Down Expand Up @@ -303,7 +303,6 @@ void Logic::ProcessMessage()
{
case THUAI7::GameState::GameStart:
logger->info("Game Start!");

// 读取地图
for (const auto& item : clientMsg.obj_message())
{
Expand Down Expand Up @@ -831,7 +830,10 @@ void Logic::LoadBuffer(const protobuf::MessageToClient& message)
LoadBufferSelf(message);
// 确保这是一个活着的船,否则会使用空指针
if (playerType == THUAI7::PlayerType::Ship && !bufferState->shipSelf)
{
logger->info("exit for nullSelf");
return;
}
for (const auto& item : message.obj_message())
LoadBufferCase(item);
}
Expand Down
30 changes: 27 additions & 3 deletions CAPI/python/PyAPI/AI.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,38 @@ def ShipPlay(self, api: IShipAPI) -> None:
# 公共操作
if self.__playerID == 1:
api.PrintSelfInfo()
api.Move(100, 1.1)
api.Attack(10.0)
api.MoveLeft(100)
time.sleep(1)
api.MoveRight(100)
time.sleep(1)
api.MoveDown(100)
time.sleep(1)
api.MoveUp(100)
time.sleep(1)
# player1的操作
return
elif self.__playerID == 2:
api.PrintSelfInfo()
api.MoveLeft(100)
time.sleep(1)
api.MoveRight(100)
time.sleep(1)
api.MoveDown(100)
time.sleep(1)
api.MoveUp(100)
time.sleep(1)
# player2的操作
return
elif self.__playerID == 3:
api.PrintSelfInfo()
api.MoveLeft(100)
time.sleep(1)
api.MoveRight(100)
time.sleep(1)
api.MoveDown(100)
time.sleep(1)
api.MoveUp(100)
time.sleep(1)
# player3的操作
return
elif self.__playerID == 4:
Expand All @@ -52,6 +75,7 @@ def ShipPlay(self, api: IShipAPI) -> None:

def TeamPlay(self, api: ITeamAPI) -> None:
assert self.__playerID == 0, "Team's playerID must be 0"
# api.BuildShip(shipType=THUAI7.ShipType.CivilianShip)
api.BuildShip(shipType=THUAI7.ShipType.MilitaryShip,birthIndex=0)
time.sleep(2)
# 操作
return
8 changes: 4 additions & 4 deletions CAPI/python/PyAPI/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,8 @@ def __LoadBufferCase(self, item: Message2Clients.MessageOfObj) -> None:
# self.__bufferState.bombedBullets.append(Proto2THUAI7.Protobuf2THUAI7BombedBullet(item.bombed_bullet_message))
# self.__logger.debug('Add Bombed Bullet!')

else:
self.__logger.error("Unknown message!")
# else:
# self.__logger.error("Unknown message!")
elif self.__playerType == THUAI7.PlayerType.Team:

def HaveOverView(targetX: int, targetY: int):
Expand Down Expand Up @@ -788,8 +788,8 @@ def HaveOverView(targetX: int, targetY: int):
# self.__bufferState.bombedBullets.append(Proto2THUAI7.Protobuf2THUAI7BombedBullet(item.bombed_bullet_message))
# self.__logger.debug('Add Bombed Bullet!')

else:
self.__logger.error("Unknown message!")
# else:
# self.__logger.error("Unknown message!")

def __UnBlockAI(self) -> None:
with self.__cvAI:
Expand Down
2 changes: 2 additions & 0 deletions logic/Gaming/ShipManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public bool ActivateShip(Ship ship, XY pos)
long stateNum = ship.SetShipState(RunningStateType.RunningActively, ShipStateType.Null);
ship.ResetShipState(stateNum);
ship.CanMove.SetROri(true);
ship.IsRemoved.SetROri(false);
ship.MoneyPool.SubMoney(activateCost);
Debugger.Output(ship, " is activated!");
return true;
}
Expand Down
Loading

0 comments on commit c6afd7d

Please sign in to comment.