Skip to content

Commit

Permalink
Use mutating_list_iterate more for droid/structure lists
Browse files Browse the repository at this point in the history
Replace while-based loops introduced by
#3572
with `mutating_list_iterate`.

Signed-off-by: Pavel Solodovnikov <[email protected]>
  • Loading branch information
ManManson committed Jan 7, 2024
1 parent e408010 commit 130d0c3
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 269 deletions.
13 changes: 5 additions & 8 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5330,18 +5330,16 @@ static bool loadSaveDroidPointers(const WzString &pFileName, PerPlayerDroidLists
continue; // special hack for campaign missions, cannot have targets
}

DroidList::iterator droidIt = (*ppsCurrentDroidLists)[player].begin(), droidItNext;
while (droidIt != (*ppsCurrentDroidLists)[player].end())
for (DROID* d : (*ppsCurrentDroidLists)[player])
{
droidItNext = std::next(droidIt);
psDroid = *droidIt;
if (psDroid->id == id)
if (d->id == id)
{
psDroid = d;
break;
}
if (isTransporter(psDroid) && psDroid->psGroup != nullptr) // Check for droids in the transporter.
if (isTransporter(d) && d->psGroup != nullptr) // Check for droids in the transporter.
{
for (DROID *psTrDroid : psDroid->psGroup->psList)
for (DROID *psTrDroid : d->psGroup->psList)
{
if (psTrDroid->id == id)
{
Expand All @@ -5350,7 +5348,6 @@ static bool loadSaveDroidPointers(const WzString &pFileName, PerPlayerDroidLists
}
}
}
droidIt = droidItNext;
}
foundDroid:
if (!psDroid)
Expand Down
24 changes: 11 additions & 13 deletions src/keybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,11 @@ void kf_CloneSelected(int limit)
return; // no-op
}

DroidList::iterator droidIt = apsDroidLists[selectedPlayer].begin(), droidItNext;
while (droidIt != apsDroidLists[selectedPlayer].end())
mutating_list_iterate(apsDroidLists[selectedPlayer], [limit, &sTemplate](DROID* d)
{
droidItNext = std::next(droidIt);
if ((*droidIt)->selected)
if (d->selected)
{
enumerateTemplates(selectedPlayer, [psDroid = *droidIt, &sTemplate](DROID_TEMPLATE * psTempl) {
enumerateTemplates(selectedPlayer, [psDroid = d, &sTemplate](DROID_TEMPLATE* psTempl) {
if (psTempl->name.compare(psDroid->aName) == 0)
{
sTemplate = psTempl;
Expand All @@ -402,15 +400,15 @@ void kf_CloneSelected(int limit)

if (!sTemplate)
{
debug(LOG_ERROR, "Cloning vat has been destroyed. We can't find the template for this droid: %s, id:%u, type:%d!", (*droidIt)->aName, (*droidIt)->id, (*droidIt)->droidType);
return;
debug(LOG_ERROR, "Cloning vat has been destroyed. We can't find the template for this droid: %s, id:%u, type:%d!", d->aName, d->id, d->droidType);
return IterationResult::BREAK_ITERATION;
}

// create a new droid army
for (int i = 0; i < limit; i++)
{
Vector2i pos = (*droidIt)->pos.xy() + iSinCosR(40503 * i, iSqrt(50 * 50 * (i + 1))); // 40503 = 65536/φ (A bit more than a right angle)
DROID *psNewDroid = buildDroid(sTemplate, pos.x, pos.y, (*droidIt)->player, false, nullptr);
Vector2i pos = d->pos.xy() + iSinCosR(40503 * i, iSqrt(50 * 50 * (i + 1))); // 40503 = 65536/φ (A bit more than a right angle)
DROID* psNewDroid = buildDroid(sTemplate, pos.x, pos.y, d->player, false, nullptr);
if (psNewDroid)
{
addDroid(psNewDroid, apsDroidLists);
Expand All @@ -421,15 +419,15 @@ void kf_CloneSelected(int limit)
debug(LOG_ERROR, "Cloning has failed for template:%s id:%d", getID(sTemplate), sTemplate->multiPlayerID);
}
}
std::string msg = astringf(_("Player %u is cheating a new droid army of: %d × %s."), selectedPlayer, limit, (*droidIt)->aName);
std::string msg = astringf(_("Player %u is cheating a new droid army of: %d × %s."), selectedPlayer, limit, d->aName);
sendInGameSystemMessage(msg.c_str());
Cheated = true;
audio_PlayTrack(ID_SOUND_NEXUS_LAUGH1);
return;
return IterationResult::BREAK_ITERATION;
}
debug(LOG_INFO, "Nothing was selected?");
droidIt = droidItNext;
}
return IterationResult::CONTINUE_ITERATION;
});
}

void kf_MakeMeHero()
Expand Down
46 changes: 17 additions & 29 deletions src/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,40 +542,28 @@ static void gameStateUpdate()
//update the current power available for a player
updatePlayerPower(i);

DroidList::iterator droidIt = apsDroidLists[i].begin(), droidItNext;
while (droidIt != apsDroidLists[i].end())
mutating_list_iterate(apsDroidLists[i], [](DROID* d)
{
// Copy the next pointer - not 100% sure if the droid could get destroyed but this covers us anyway
droidItNext = std::next(droidIt);
droidUpdate(*droidIt);
droidIt = droidItNext;
}

droidIt = mission.apsDroidLists[i].begin();
while (droidIt != mission.apsDroidLists[i].end())
droidUpdate(d);
return IterationResult::CONTINUE_ITERATION;
});
mutating_list_iterate(mission.apsDroidLists[i], [](DROID* d)
{
/* Copy the next pointer - not 100% sure if the droid could
get destroyed but this covers us anyway */
droidItNext = std::next(droidIt);
missionDroidUpdate(*droidIt);
droidIt = droidItNext;
}
missionDroidUpdate(d);
return IterationResult::CONTINUE_ITERATION;
});

// FIXME: These for-loops are code duplicationo
StructureList::iterator structIt = apsStructLists[i].begin(), structItNext;
while (structIt != apsStructLists[i].end())
// FIXME: These for-loops are code duplication
mutating_list_iterate(apsStructLists[i], [](STRUCTURE* s)
{
structItNext = std::next(structIt);
structureUpdate(*structIt, false);
structIt = structItNext;
}
structIt = mission.apsStructLists[i].begin();
while (structIt != mission.apsStructLists[i].end())
structureUpdate(s, false);
return IterationResult::CONTINUE_ITERATION;
});
mutating_list_iterate(mission.apsStructLists[i], [](STRUCTURE* s)
{
structItNext = std::next(structIt);
structureUpdate(*structIt, true); // update for mission
structIt = structItNext;
}
structureUpdate(s, true); // update for mission
return IterationResult::CONTINUE_ITERATION;
});
}

missionTimerUpdate();
Expand Down
Loading

0 comments on commit 130d0c3

Please sign in to comment.