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

Unloading: extract transportee choice to a function #1740

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 23 additions & 38 deletions rts/Sim/Units/CommandAI/MobileCAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1887,12 +1887,26 @@ void CMobileCAI::UnloadUnits_LandFlood(Command& c)
StopMoveAndFinishCommand();
}

static constexpr CUnit* GetTransporteeFromUnloadOrder(const Command& c, const auto &transportees)
{
if (transportees.empty())
return nullptr;

if (c.GetNumParams() < 4)
return transportees[0].unit;

const int transporteeID = c.GetParam(3);
for (const auto &transportee : transportees)
if (transportee.unit->id == transporteeID)
return transportee.unit;

return nullptr;
}

void CMobileCAI::UnloadLand(Command& c)
{
RECOIL_DETAILED_TRACY_ZONE;
// default unload
CUnit* transportee = nullptr;
CHoverAirMoveType* am = nullptr;

float3 wantedPos = c.GetPos(0);
Expand All @@ -1901,25 +1915,10 @@ void CMobileCAI::UnloadLand(Command& c)

SetGoal(wantedPos, owner->pos);

if (c.GetNumParams() < 4) {
// unload the first transportee
transportee = transportees[0].unit;
} else {
const int unitID = c.GetParam(3);

// unload a specific transportee
for (const CUnit::TransportedUnit& tu: transportees) {
CUnit* carried = tu.unit;

if (unitID == carried->id) {
transportee = carried;
break;
}
}
if (transportee == nullptr) {
StopMoveAndFinishCommand();
return;
}
const auto transportee = GetTransporteeFromUnloadOrder(c, transportees);
if (transportee == nullptr) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

if (const auto* transportee = GetTransporteeFromUnloadOrder(c, transportees); transportee) {

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transportee is used a lot afterwards though (from line 1928/1927 until 1972/1971), its scope is the whole rest of the function and not limited just to this check.

StopMoveAndFinishCommand(); // FIXME: UnloadLandFlood() has just FinishCommand here, is the difference meaningful?
return;
}

if (wantedPos.SqDistance2D(owner->pos) >= Square(owner->unitDef->loadingRadius * 0.9f))
Expand Down Expand Up @@ -2024,31 +2023,17 @@ void CMobileCAI::UnloadLandFlood(Command& c)
{
RECOIL_DETAILED_TRACY_ZONE;
// land, then release all units at once
CUnit* transportee = nullptr;

float3 wantedPos = c.GetPos(0);

const auto& transportees = owner->transportedUnits;

SetGoal(wantedPos, owner->pos);

if (c.GetNumParams() < 4) {
transportee = transportees[0].unit;
} else {
const int unitID = c.GetParam(3);

for (const CUnit::TransportedUnit& tu: transportees) {
CUnit* carried = tu.unit;

if (unitID == carried->id) {
transportee = carried;
break;
}
}
if (transportee == nullptr) {
FinishCommand();
return;
}
const auto transportee = GetTransporteeFromUnloadOrder(c, transportees);
if (transportee == nullptr) {
FinishCommand(); // FIXME: UnloadLand() has StopMoveAndFinishCommand here, is the difference meaningful?
return;
}

if (wantedPos.SqDistance2D(owner->pos) < Square(owner->unitDef->loadingRadius * 0.9f)) {
Expand Down