Skip to content
Permalink
Browse files

e5666f add.

  • Loading branch information...
rtri
rtri committed Oct 11, 2019
1 parent cd46e8e commit 639a473a57b7284586e30fe993d83ad67f2c8a4f
@@ -366,7 +366,7 @@ int CAICallback::GiveOrder(int unitId, Command* c)
if (unit->team != team)
return -5;

clientNet->Send(CBaseNetProtocol::Get().SendAICommand(gu->myPlayerNum, skirmishAIHandler.GetCurrentAIID(), unitId, c->GetID(false), c->GetID(true), c->GetTimeOut(), c->GetOpts(), c->GetNumParams(), c->GetParams()));
clientNet->Send(CBaseNetProtocol::Get().SendAICommand(gu->myPlayerNum, skirmishAIHandler.GetCurrentAIID(), team, unitId, c->GetID(false), c->GetID(true), c->GetTimeOut(), c->GetOpts(), c->GetNumParams(), c->GetParams()));
return 0;
}

@@ -720,21 +720,26 @@ void CSelectedUnitsHandler::ClearNetSelect(int playerId)
netSelected[playerId].clear();
}

// handles NETMSG_AICOMMAND{S}'s
void CSelectedUnitsHandler::AINetOrder(int unitID, int playerID, const Command& c)
// handles NETMSG_AICOMMAND{S}'s sent by AICallback / LuaUnsyncedCtrl (!)
void CSelectedUnitsHandler::AINetOrder(int unitID, int aiTeamID, int playerID, const Command& c)
{
CUnit* unit = unitHandler.GetUnit(unitID);

if (unit == nullptr)
return;

const CPlayer* player = playerHandler.Player(playerID);

if (player == nullptr)
return;

// no warning; will result in false bug reports due to latency between
// time of giving valid orders on units which then change team through
// e.g. LuaRules
if (!player->CanControlTeam(unit->team))
// AI's are hosted by players, but do not have any Player representation
// themselves and should not be automatically controllable by their host
// on the other hand they should always be able to control their OWN team
if ((aiTeamID == MAX_TEAMS && !player->CanControlTeam(unit->team)) || (aiTeamID != MAX_TEAMS && aiTeamID != unit->team))
return;

// always pulled from net, synced command by definition
@@ -993,6 +998,7 @@ void CSelectedUnitsHandler::SendCommand(const Command& c)
}


// despite the NETMSG_AICOMMANDS packet-id, this only services LuaUnsyncedCtrl
void CSelectedUnitsHandler::SendCommandsToUnits(const std::vector<int>& unitIDs, const std::vector<Command>& commands, bool pairwise)
{
// do not waste bandwidth (units can be selected
@@ -1046,7 +1052,7 @@ void CSelectedUnitsHandler::SendCommandsToUnits(const std::vector<int>& unitIDs,
*packet << static_cast<uint8_t>(NETMSG_AICOMMANDS)
<< static_cast<uint16_t>(msgLen)
<< static_cast<uint8_t>(gu->myPlayerNum)
<< skirmishAIHandler.GetCurrentAIID()
<< MAX_AIS
<< static_cast<uint8_t>(pairwise)
<< static_cast<uint32_t>(sameCmdID)
<< static_cast<uint8_t>(sameCmdOpt)
@@ -20,9 +20,9 @@ class CSelectedUnitsHandler : public CObject
public:
void Init(unsigned numPlayers);
void SelectGroup(int num);
void AINetOrder(int unitID, int playerID, const Command& c);
void AINetOrder(int unitID, int aiTeamID, int playerID, const Command& c);
int GetDefaultCmd(const CUnit* unit, const CFeature* feature);
bool CommandsChanged() const { return possibleCommandsChanged; }

void NetOrder(Command& c, int playerId);
void NetSelect(std::vector<int>& s, int playerId);
void ClearNetSelect(int playerId);
@@ -53,6 +53,7 @@ class CSelectedUnitsHandler : public CObject
void SendCommand(const Command& c);
void SendCommandsToUnits(const std::vector<int>& unitIDs, const std::vector<Command>& commands, bool pairwise = false);

bool CommandsChanged() const { return possibleCommandsChanged; }
bool IsUnitSelected(const CUnit* unit) const;
bool IsUnitSelected(const int unitID) const;
bool AutoAddBuiltUnitsToFactoryGroup() const { return autoAddBuiltUnitsToFactoryGroup; }
@@ -2457,7 +2457,7 @@ int LuaUnsyncedCtrl::GiveOrderToUnit(lua_State* L)

const Command cmd = LuaUtils::ParseCommand(L, __func__, 2);

clientNet->Send(CBaseNetProtocol::Get().SendAICommand(gu->myPlayerNum, skirmishAIHandler.GetCurrentAIID(), unit->id, cmd.GetID(false), cmd.GetID(true), cmd.GetTimeOut(), cmd.GetOpts(), cmd.GetNumParams(), cmd.GetParams()));
clientNet->Send(CBaseNetProtocol::Get().SendAICommand(gu->myPlayerNum, MAX_AIS, MAX_TEAMS, unit->id, cmd.GetID(false), cmd.GetID(true), cmd.GetTimeOut(), cmd.GetOpts(), cmd.GetNumParams(), cmd.GetParams()));

lua_pushboolean(L, true);
return 1;
@@ -693,15 +693,16 @@ void CGame::ClientReadNet()
try {
netcode::UnpackPacket pckt(packet, 1);

int16_t psize; pckt >> psize;
int16_t pktLen; pckt >> pktLen;
uint8_t player; pckt >> player;
uint8_t aiID; pckt >> aiID;
int16_t unitID;

uint8_t aiInstID; pckt >> aiInstID;
uint8_t aiTeamID; pckt >> aiTeamID;
int16_t unitID; pckt >> unitID;

if (!playerHandler.IsValidPlayer(player))
throw netcode::UnpackPacketException("Invalid player number");

pckt >> unitID;
if (unitID < 0 || static_cast<size_t>(unitID) >= unitHandler.MaxUnits())
throw netcode::UnpackPacketException("Invalid unit ID");

@@ -731,7 +732,7 @@ void CGame::ClientReadNet()
}

// command originating from SkirmishAI or LuaUnsyncedCtrl
selectedUnitsHandler.AINetOrder(unitID, player, c);
selectedUnitsHandler.AINetOrder(unitID, aiTeamID, player, c);
AddTraffic(player, packetCode, dataLength);
} catch (const netcode::UnpackPacketException& ex) {
LOG_L(L_ERROR, "[Game::%s][NETMSG_AICOMMAND*] exception \"%s\"", __func__, ex.what());
@@ -743,7 +744,7 @@ void CGame::ClientReadNet()
netcode::UnpackPacket pckt(packet, 3);

uint8_t player;
uint8_t aiID;
uint8_t aiInstID;
uint8_t pairwise;
uint32_t sameCmdID;
uint8_t sameCmdOpt;
@@ -757,7 +758,7 @@ void CGame::ClientReadNet()
if (!playerHandler.IsValidPlayer(player))
throw netcode::UnpackPacketException("Invalid player number");

pckt >> aiID;
pckt >> aiInstID;
pckt >> pairwise;
pckt >> sameCmdID;
pckt >> sameCmdOpt;
@@ -801,15 +802,17 @@ void CGame::ClientReadNet()
commands.push_back(cmd);
}

// apply the commands
assert(aiInstID == MAX_AIS);

// apply the "AI" commands (which actually originate from LuaUnsyncedCtrl)
if (pairwise) {
for (int16_t x = 0; x < std::min(unitCount, commandCount); ++x) {
selectedUnitsHandler.AINetOrder(unitIDs[x], player, commands[x]);
selectedUnitsHandler.AINetOrder(unitIDs[x], aiInstID, player, commands[x]);
}
} else {
for (int16_t c = 0; c < commandCount; c++) {
for (int16_t u = 0; u < unitCount; u++) {
selectedUnitsHandler.AINetOrder(unitIDs[u], player, commands[c]);
selectedUnitsHandler.AINetOrder(unitIDs[u], aiInstID, player, commands[c]);
}
}
}
@@ -139,7 +139,8 @@ PacketType CBaseNetProtocol::SendCommand(

PacketType CBaseNetProtocol::SendAICommand(
uint8_t playerNum,
uint8_t aiID,
uint8_t aiInstID,
uint8_t aiTeamID,
int16_t unitID,
int32_t commandID,
int32_t aiCommandID,
@@ -151,7 +152,7 @@ PacketType CBaseNetProtocol::SendAICommand(
const int32_t commandTypeID = (aiCommandID != -1)? NETMSG_AICOMMAND_TRACKED: NETMSG_AICOMMAND;

const uint32_t payloadSize =
sizeof(playerNum) + sizeof(aiID) + sizeof(unitID) +
sizeof(playerNum) + sizeof(aiInstID) + sizeof(aiTeamID) + sizeof(unitID) +
sizeof(commandID) + sizeof(timeout) + sizeof(options) + sizeof(numParams) +
(sizeof(commandTypeID) * (commandTypeID == NETMSG_AICOMMAND_TRACKED)) + (numParams * sizeof(float));
const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
@@ -162,7 +163,7 @@ PacketType CBaseNetProtocol::SendAICommand(
throw netcode::PackPacketException("[BaseNetProto::SendAICommand] maximum packet-size exceeded");

PackPacket* packet = new PackPacket(packetSize, commandTypeID);
*packet << static_cast<uint16_t>(packetSize) << playerNum << aiID << unitID;
*packet << static_cast<uint16_t>(packetSize) << playerNum << aiInstID << aiTeamID << unitID;
*packet << commandID << timeout << options << numParams;

if (commandTypeID == NETMSG_AICOMMAND_TRACKED)
@@ -55,7 +55,7 @@ class CBaseNetProtocol
PacketType SendPause(uint8_t playerNum, uint8_t bPaused);

PacketType SendCommand(uint8_t playerNum, int32_t commandID, int32_t timeout, uint8_t options, uint32_t numParams, const float* params);
PacketType SendAICommand(uint8_t playerNum, uint8_t aiID, int16_t unitID, int32_t commandID, int32_t aiCommandID, int32_t timeout, uint8_t options, uint32_t numParams, const float* params);
PacketType SendAICommand(uint8_t playerNum, uint8_t aiInstID, uint8_t aiTeamID, int16_t unitID, int32_t commandID, int32_t aiCommandID, int32_t timeout, uint8_t options, uint32_t numParams, const float* params);
PacketType SendAIShare(uint8_t playerNum, uint8_t aiID, uint8_t sourceTeam, uint8_t destTeam, float metal, float energy, const std::vector<int16_t>& unitIDs);

PacketType SendUserSpeed(uint8_t playerNum, float userSpeed);

0 comments on commit 639a473

Please sign in to comment.
You can’t perform that action at this time.