diff --git a/src/game.cpp b/src/game.cpp index 2141a27733c..44b1f05c147 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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) { @@ -5350,7 +5348,6 @@ static bool loadSaveDroidPointers(const WzString &pFileName, PerPlayerDroidLists } } } - droidIt = droidItNext; } foundDroid: if (!psDroid) diff --git a/src/keybind.cpp b/src/keybind.cpp index 920ffe7c186..de6a210560d 100644 --- a/src/keybind.cpp +++ b/src/keybind.cpp @@ -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; @@ -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); @@ -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() diff --git a/src/loop.cpp b/src/loop.cpp index bd1ebb7f80b..8297ca2f4d8 100644 --- a/src/loop.cpp +++ b/src/loop.cpp @@ -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(); diff --git a/src/mission.cpp b/src/mission.cpp index eb6b5276d53..8818d70a37b 100644 --- a/src/mission.cpp +++ b/src/mission.cpp @@ -580,7 +580,7 @@ void addTransporterTimerInterface() void missionFlyTransportersIn(SDWORD iPlayer, bool bTrackTransporter) { UWORD iX, iY, iZ; - SDWORD iLandX, iLandY, iDx, iDy; + SDWORD iLandX, iLandY; ASSERT_OR_RETURN(, iPlayer < MAX_PLAYERS, "Flying nonexistent player %d's transporters in", iPlayer); @@ -590,11 +590,9 @@ void missionFlyTransportersIn(SDWORD iPlayer, bool bTrackTransporter) iZ = (UWORD)(map_Height(iX, iY) + OFFSCREEN_HEIGHT); //get the droids for the mission - DroidList::iterator droidIt = mission.apsDroidLists[iPlayer].begin(), droidItNext; - while (droidIt != mission.apsDroidLists[iPlayer].end()) + mutating_list_iterate(mission.apsDroidLists[iPlayer], [iPlayer, bTrackTransporter, iX, iY, iZ, iLandX, iLandY](DROID* psTransporter) { - droidItNext = std::next(droidIt); - DROID* psTransporter = *droidIt; + SDWORD iDx, iDy; if (psTransporter->droidType == DROID_SUPERTRANSPORTER) { @@ -647,18 +645,17 @@ void missionFlyTransportersIn(SDWORD iPlayer, bool bTrackTransporter) audio_PlayObjDynamicTrack(psTransporter, ID_SOUND_BLIMP_FLIGHT, moveCheckDroidMovingAndVisible); //only want to fly one transporter in at a time now - AB 14/01/99 - break; + return IterationResult::BREAK_ITERATION; } } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } /* Saves the necessary data when moving from a home base Mission to an OffWorld mission */ static void saveMissionData() { UDWORD inc; - STRUCTURE *psStructBeingBuilt; bool bRepairExists; debug(LOG_SAVE, "called"); @@ -670,18 +667,16 @@ static void saveMissionData() bRepairExists = false; //set any structures currently being built to completed for the selected player - StructureList::iterator structIt = apsStructLists[selectedPlayer].begin(), structItNext; - while (structIt != apsStructLists[selectedPlayer].end()) + mutating_list_iterate(apsStructLists[selectedPlayer], [&bRepairExists](STRUCTURE* psStruct) { - structItNext = std::next(structIt); - STRUCTURE* psStruct = *structIt; + STRUCTURE* psStructBeingBuilt; if (psStruct->status == SS_BEING_BUILT) { //find a droid working on it for (const DROID* psDroid : apsDroidLists[selectedPlayer]) { - if ((psStructBeingBuilt = (STRUCTURE *)orderStateObj(psDroid, DORDER_BUILD)) - && psStructBeingBuilt == psStruct) + if ((psStructBeingBuilt = (STRUCTURE*)orderStateObj(psDroid, DORDER_BUILD)) + && psStructBeingBuilt == psStruct) { // just give it all its build points structureBuild(psStruct, nullptr, structureBuildPointsToCompletion(*psStruct)); @@ -695,8 +690,8 @@ static void saveMissionData() { bRepairExists = true; } - structIt = structItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); //repair all droids back at home base if have a repair facility if (bRepairExists) @@ -713,6 +708,7 @@ static void saveMissionData() //clear droid orders for all droids except constructors still building for (DROID* psDroid : apsDroidLists[selectedPlayer]) { + STRUCTURE* psStructBeingBuilt; if ((psStructBeingBuilt = (STRUCTURE *)orderStateObj(psDroid, DORDER_BUILD))) { if (psStructBeingBuilt->status == SS_BUILT) @@ -882,17 +878,14 @@ void saveMissionLimboData() processPreviousCampDroids(); // move droids properly - does all the clean up code - DroidList::iterator psDroid = apsDroidLists[selectedPlayer].begin(), psNext; - while (psDroid != apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(apsDroidLists[selectedPlayer], [](DROID* psDroid) { - psNext = std::next(psDroid); - DROID* psDroidRaw = *psDroid; - if (droidRemove(psDroidRaw, apsDroidLists)) + if (droidRemove(psDroid, apsDroidLists)) { - addDroid(psDroidRaw, mission.apsDroidLists); + addDroid(psDroid, mission.apsDroidLists); } - psDroid = psNext; - } + return IterationResult::CONTINUE_ITERATION; + }); apsDroidLists[selectedPlayer].clear(); // any selectedPlayer's factories/research need to be put on holdProduction/holdresearch @@ -912,19 +905,16 @@ void saveMissionLimboData() //this is called via a script function to place the Limbo droids once the mission has started void placeLimboDroids() { - UDWORD droidX, droidY; - PICKTILE pickRes; - debug(LOG_SAVE, "called"); ASSERT(selectedPlayer < MAX_PLAYERS, "selectedPlayer %" PRIu32 " exceeds MAX_PLAYERS", selectedPlayer); // Copy the droids across for the selected Player - DroidList::iterator droidIt = apsLimboDroids[selectedPlayer].begin(), droidItNext; - while (droidIt != apsLimboDroids[selectedPlayer].end()) + mutating_list_iterate(apsLimboDroids[selectedPlayer], [](DROID* psDroid) { - droidItNext = std::next(droidIt); - DROID* psDroid = *droidIt; + UDWORD droidX, droidY; + PICKTILE pickRes; + if (droidRemove(psDroid, apsLimboDroids)) { addDroid(psDroid, apsDroidLists); @@ -932,7 +922,7 @@ void placeLimboDroids() if (isTransporter(psDroid)) { vanishDroid(psDroid); - continue; + return IterationResult::CONTINUE_ITERATION; } //set up location for each of the droids droidX = map_coord(getLandingX(LIMBO_LANDING)); @@ -959,8 +949,8 @@ void placeLimboDroids() { ASSERT(false, "placeLimboUnits: Unable to remove unit from Limbo list"); } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } /*restores the necessary data on completion of a Limbo Expand mission*/ @@ -972,11 +962,8 @@ void restoreMissionLimboData() /*the droids stored in the mission droid list need to be added back into the current droid list*/ - DroidList::iterator droidIt = mission.apsDroidLists[selectedPlayer].begin(), droidItNext; - while (droidIt != mission.apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(mission.apsDroidLists[selectedPlayer], [](DROID* psDroid) { - droidItNext = std::next(droidIt); - DROID* psDroid = *droidIt; //remove out of stored list and add to current Droid list if (droidRemove(psDroid, mission.apsDroidLists)) { @@ -985,8 +972,8 @@ void restoreMissionLimboData() orderDroid(psDroid, DORDER_STOP, ModeImmediate); //the location of the droid should be valid! } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); ASSERT(mission.apsDroidLists[selectedPlayer].empty(), "list should be empty"); } @@ -1002,15 +989,12 @@ void saveCampaignData() if (getDroidsToSafetyFlag()) { // Move any Transporters into the mission list - DroidList::iterator droidIt = apsDroidLists[selectedPlayer].begin(), droidItNext; - while (droidIt != apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(apsDroidLists[selectedPlayer], [](DROID* psDroid) { - droidItNext = std::next(droidIt); - DROID* psDroid = *droidIt; if (isTransporter(psDroid)) { // Empty the transporter into the mission list - ASSERT_OR_RETURN(, psDroid->psGroup != nullptr, "Transporter does not have a group"); + ASSERT_OR_RETURN(IterationResult::CONTINUE_ITERATION, psDroid->psGroup != nullptr, "Transporter does not have a group"); mutating_list_iterate(psDroid->psGroup->psList, [psDroid](DROID* psCurr) { @@ -1037,8 +1021,8 @@ void saveCampaignData() addDroid(psDroid, mission.apsDroidLists); } } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } else { @@ -1061,19 +1045,13 @@ void saveCampaignData() mission.apsDroidLists[selectedPlayer].reverse(); //find the *first* transporter - DroidList::iterator droidIt = mission.apsDroidLists[selectedPlayer].begin(), droidItNext; - while (droidIt != mission.apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(mission.apsDroidLists[selectedPlayer], [](DROID* psDroid) { - droidItNext = std::next(droidIt); - DROID* psDroid = *droidIt; if (isTransporter(psDroid)) { //fill it with droids from the mission list - DroidList::iterator safeDroidIt = mission.apsDroidLists[selectedPlayer].begin(), safeDroidItNext; - while (safeDroidIt != mission.apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(mission.apsDroidLists[selectedPlayer], [psDroid](DROID* psSafeDroid) { - safeDroidItNext = std::next(safeDroidIt); - DROID* psSafeDroid = *safeDroidIt; if (psSafeDroid != psDroid) { //add to the Transporter, checking for when full @@ -1086,28 +1064,26 @@ void saveCampaignData() } else { - break; + return IterationResult::BREAK_ITERATION; } } - safeDroidIt = safeDroidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); //only want to fill one transporter - break; + return IterationResult::BREAK_ITERATION; } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } //clear all other droids for (int inc = 0; inc < MAX_PLAYERS; inc++) { - DroidList::iterator droidIt = apsDroidLists[inc].begin(), droidItNext; - while (droidIt != apsDroidLists[inc].end()) + mutating_list_iterate(apsDroidLists[inc], [](DROID* d) { - droidItNext = std::next(droidIt); - vanishDroid(*droidIt); - droidIt = droidItNext; - } + vanishDroid(d); + return IterationResult::CONTINUE_ITERATION; + }); } //clear out the audio @@ -1259,21 +1235,17 @@ static void clearCampaignUnits() /*This deals with droids at the end of an offworld mission*/ static void processMission() { - UDWORD droidX, droidY; - PICKTILE pickRes; - ASSERT(selectedPlayer < MAX_PLAYERS, "selectedPlayer %" PRIu32 " exceeds MAX_PLAYERS", selectedPlayer); //and the rest on the mission map - for now? - DroidList::iterator droidIt = apsDroidLists[selectedPlayer].begin(), droidItNext; - while (droidIt != apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(apsDroidLists[selectedPlayer], [](DROID* psDroid) { - droidItNext = std::next(droidIt); - DROID* psDroid = *droidIt; + UDWORD droidX, droidY; + PICKTILE pickRes; //reset order - do this to all the droids that are returning from offWorld orderDroid(psDroid, DORDER_STOP, ModeImmediate); // clean up visibility - visRemoveVisibility((BASE_OBJECT *)psDroid); + visRemoveVisibility((BASE_OBJECT*)psDroid); //remove out of stored list and add to current Droid list if (droidRemove(psDroid, apsDroidLists)) { @@ -1298,8 +1270,8 @@ static void processMission() // This is mainly for VTOLs setDroidBase(psDroid, nullptr); } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } @@ -1313,11 +1285,8 @@ void processMissionLimbo() ASSERT(selectedPlayer < MAX_PLAYERS, "selectedPlayer %" PRIu32 " exceeds MAX_PLAYERS", selectedPlayer); //all droids (for selectedPlayer only) are placed into the limbo list - DroidList::iterator droidIt = apsDroidLists[selectedPlayer].begin(), droidItNext; - while (droidIt != apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(apsDroidLists[selectedPlayer], [&numDroidsAddedToLimboList](DROID* psDroid) { - droidItNext = std::next(droidIt); - DROID* psDroid = *droidIt; //KILL OFF TRANSPORTER - should never be one but.... if (isTransporter(psDroid)) { @@ -1345,8 +1314,8 @@ void processMissionLimbo() } } } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } /*switch the pointers for the map and droid lists so that droid placement @@ -1576,48 +1545,43 @@ static void missionResetDroids() for (unsigned int player = 0; player < MAX_PLAYERS; player++) { - DroidList::iterator droidIt = apsDroidLists[player].begin(), droidItNext; - while (droidIt != apsDroidLists[player].end()) + mutating_list_iterate(apsDroidLists[player], [](DROID* d) { - droidItNext = std::next(droidIt); // Reset order - unless constructor droid that is mid-build - if (((*droidIt)->droidType == DROID_CONSTRUCT - || (*droidIt)->droidType == DROID_CYBORG_CONSTRUCT) - && orderStateObj((*droidIt), DORDER_BUILD)) + if ((d->droidType == DROID_CONSTRUCT + || d->droidType == DROID_CYBORG_CONSTRUCT) + && orderStateObj(d, DORDER_BUILD)) { // Need to set the action time to ignore the previous mission time - (*droidIt)->actionStarted = gameTime; + d->actionStarted = gameTime; } else { - orderDroid(*droidIt, DORDER_STOP, ModeImmediate); + orderDroid(d, DORDER_STOP, ModeImmediate); } //KILL OFF TRANSPORTER - if (isTransporter(*droidIt)) + if (isTransporter(d)) { - vanishDroid(*droidIt); + vanishDroid(d); } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } - DroidList::iterator droidIt = apsDroidLists[selectedPlayer].begin(), droidItNext; - while (droidIt != apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(apsDroidLists[selectedPlayer], [](DROID* psDroid) { - droidItNext = std::next(droidIt); - DROID* psDroid = *droidIt; bool placed = false; //for all droids that have never left home base if (psDroid->pos.x == INVALID_XY && psDroid->pos.y == INVALID_XY) { - STRUCTURE *psStruct = psDroid->psBaseStruct; - FACTORY *psFactory = nullptr; + STRUCTURE* psStruct = psDroid->psBaseStruct; + FACTORY* psFactory = nullptr; if (psStruct && StructIsFactory(psStruct)) { - psFactory = (FACTORY *)psStruct->pFunctionality; + psFactory = (FACTORY*)psStruct->pFunctionality; } //find a location next to the factory if (psFactory) @@ -1681,13 +1645,13 @@ static void missionResetDroids() // Do all the things in build droid that never did when it was built! // check the droid is a reasonable distance from the edge of the map if (psDroid->pos.x <= world_coord(EDGE_SIZE) || - psDroid->pos.y <= world_coord(EDGE_SIZE) || - psDroid->pos.x >= world_coord(mapWidth - EDGE_SIZE) || - psDroid->pos.y >= world_coord(mapHeight - EDGE_SIZE)) + psDroid->pos.y <= world_coord(EDGE_SIZE) || + psDroid->pos.x >= world_coord(mapWidth - EDGE_SIZE) || + psDroid->pos.y >= world_coord(mapHeight - EDGE_SIZE)) { debug(LOG_ERROR, "missionResetUnits: unit too close to edge of map - removing"); vanishDroid(psDroid); - continue; + return IterationResult::CONTINUE_ITERATION; } // People always stand upright @@ -1705,8 +1669,8 @@ static void missionResetDroids() vanishDroid(psDroid); } } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } /*unloads the Transporter passed into the mission at the specified x/y @@ -2922,36 +2886,29 @@ void missionDestroyObjects() { // AI player, clear out old data - DroidList::iterator droidIt = apsDroidLists[Player].begin(), droidItNext; - while (droidIt != apsDroidLists[Player].end()) + mutating_list_iterate(apsDroidLists[Player], [](DROID* d) { - droidItNext = std::next(droidIt); - removeDroidBase(*droidIt); - droidIt = droidItNext; - } + removeDroidBase(d); + return IterationResult::CONTINUE_ITERATION; + }); //clear out the mission lists as well to make sure no Transporters exist apsDroidLists[Player] = std::move(mission.apsDroidLists[Player]); - droidIt = apsDroidLists[Player].begin(); - while (droidIt != apsDroidLists[Player].end()) + mutating_list_iterate(apsDroidLists[Player], [](DROID* psDroid) { - droidItNext = std::next(droidIt); - DROID* psDroid = *droidIt; //make sure its died flag is not set since we've swapped the apsDroidList pointers over psDroid->died = false; removeDroidBase(psDroid); - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); mission.apsDroidLists[Player].clear(); - StructureList::iterator structIt = apsStructLists[Player].begin(), structItNext; - while (structIt != apsStructLists[Player].end()) + mutating_list_iterate(apsStructLists[Player], [](STRUCTURE* s) { - structItNext = std::next(structIt); - removeStruct(*structIt, true); - structIt = structItNext; - } + removeStruct(s, true); + return IterationResult::CONTINUE_ITERATION; + }); } } @@ -3010,11 +2967,8 @@ void processPreviousCampDroids() // See if any are left if (!mission.apsDroidLists[selectedPlayer].empty()) { - DroidList::iterator droidIt = mission.apsDroidLists[selectedPlayer].begin(), droidItNext; - while (droidIt != mission.apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(mission.apsDroidLists[selectedPlayer], [](DROID* psDroid) { - droidItNext = std::next(droidIt); - DROID* psDroid = *droidIt; // We want to kill off all droids now! - AB 27/01/99 // KILL OFF TRANSPORTER if (droidRemove(psDroid, mission.apsDroidLists)) @@ -3022,8 +2976,8 @@ void processPreviousCampDroids() addDroid(psDroid, apsDroidLists); vanishDroid(psDroid); } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } } @@ -3231,11 +3185,8 @@ void emptyTransporters(bool bOffWorld) ASSERT_OR_RETURN(, selectedPlayer < MAX_PLAYERS, "selectedPlayer %" PRIu32 " >= MAX_PLAYERS", selectedPlayer); //see if there are any Transporters in the world - DroidList::iterator transIt = apsDroidLists[selectedPlayer].begin(), transItNext; - while (transIt != apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(apsDroidLists[selectedPlayer], [bOffWorld](DROID* psTransporter) { - transItNext = std::next(transIt); - DROID* psTransporter = *transIt; if (isTransporter(psTransporter)) { //if flying in, empty the contents @@ -3286,14 +3237,12 @@ void emptyTransporters(bool bOffWorld) vanishDroid(psTransporter); } } - transIt = transItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); + //deal with any transporters that are waiting to come over - transIt = mission.apsDroidLists[selectedPlayer].begin(); - while (transIt != mission.apsDroidLists[selectedPlayer].end()) + mutating_list_iterate(mission.apsDroidLists[selectedPlayer], [](DROID* psTransporter) { - transItNext = std::next(transIt); - DROID* psTransporter = *transIt; if (isTransporter(psTransporter)) { //for each droid within the transporter... @@ -3312,8 +3261,8 @@ void emptyTransporters(bool bOffWorld) }); } //don't need to destroy the transporter here - it is dealt with by the endMission process - transIt = transItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); } /*bCheating = true == start of cheat diff --git a/src/multijoin.cpp b/src/multijoin.cpp index 31f17bdb9bb..1502448f5ed 100644 --- a/src/multijoin.cpp +++ b/src/multijoin.cpp @@ -134,39 +134,35 @@ void destroyPlayerResources(UDWORD player, bool quietly) } debug(LOG_DEATH, "killing off all droids for player %d", player); - DroidList::iterator droidIt = apsDroidLists[player].begin(), droidItNext; - while (droidIt != apsDroidLists[player].end()) // delete all droids + // delete all droids + mutating_list_iterate(apsDroidLists[player], [quietly](DROID* d) { - droidItNext = std::next(droidIt); if (quietly) // don't show effects { - killDroid(*droidIt); + killDroid(d); } else // show effects { - destroyDroid(*droidIt, gameTime); + destroyDroid(d, gameTime); } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); debug(LOG_DEATH, "killing off all structures for player %d", player); - StructureList::iterator psStructIt = apsStructLists[player].begin(), psNextIt; - while (psStructIt != apsStructLists[player].end()) // delete all structs + // delete all structs + mutating_list_iterate(apsStructLists[player], [quietly](STRUCTURE* psStruct) { - psNextIt = std::next(psStructIt); - // FIXME: look why destroyStruct() doesn't put back the feature like removeStruct() does - if (quietly || (*psStructIt)->pStructureType->type == REF_RESOURCE_EXTRACTOR) // don't show effects + if (quietly || psStruct->pStructureType->type == REF_RESOURCE_EXTRACTOR) // don't show effects { - removeStruct(*psStructIt, true); + removeStruct(psStruct, true); } else // show effects { - destroyStruct(*psStructIt, gameTime); + destroyStruct(psStruct, gameTime); } - - psStructIt = psNextIt; - } + return IterationResult::CONTINUE_ITERATION; + }); return; } @@ -174,27 +170,23 @@ void destroyPlayerResources(UDWORD player, bool quietly) static bool destroyMatchingStructs(UDWORD player, std::function cmp, bool quietly) { bool destroyedAnyStructs = false; - StructureList::iterator psStructIt = apsStructLists[player].begin(), psNextIt; - while (psStructIt != apsStructLists[player].end()) + mutating_list_iterate(apsStructLists[player], [quietly, &cmp, &destroyedAnyStructs](STRUCTURE* psStruct) { - psNextIt = std::next(psStructIt); - - if (cmp(*psStructIt)) + if (cmp(psStruct)) { // FIXME: look why destroyStruct() doesn't put back the feature like removeStruct() does - if (quietly || (*psStructIt)->pStructureType->type == REF_RESOURCE_EXTRACTOR) // don't show effects + if (quietly || psStruct->pStructureType->type == REF_RESOURCE_EXTRACTOR) // don't show effects { - removeStruct(*psStructIt, true); + removeStruct(psStruct, true); } else // show effects { - destroyStruct(*psStructIt, gameTime); + destroyStruct(psStruct, gameTime); } destroyedAnyStructs = true; } - - psStructIt = psNextIt; - } + return IterationResult::CONTINUE_ITERATION; + }); return destroyedAnyStructs; } @@ -261,16 +253,14 @@ bool splitResourcesAmongTeam(UDWORD player) return a.itemsRecv < b.itemsRecv; }); }; - DroidList::iterator droidIt = apsDroidLists[player].begin(), droidItNext; - while (droidIt != apsDroidLists[player].end()) + mutating_list_iterate(apsDroidLists[player], [&droidsGiftedPerTarget, &incrRecvItem](DROID* d) { - droidItNext = std::next(droidIt); bool transferredDroid = false; - if (!isDead(*droidIt)) + if (!isDead(d)) { for (size_t i = 0; i < droidsGiftedPerTarget.size(); ++i) { - if (giftSingleDroid(*droidIt, droidsGiftedPerTarget[i].player, false)) + if (giftSingleDroid(d, droidsGiftedPerTarget[i].player, false)) { transferredDroid = true; incrRecvItem(i); @@ -282,10 +272,10 @@ bool splitResourcesAmongTeam(UDWORD player) if (!transferredDroid) { - destroyDroid(*droidIt, gameTime); + destroyDroid(d, gameTime); } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); auto distributeMatchingStructs = [&](std::function cmp) { @@ -301,19 +291,15 @@ bool splitResourcesAmongTeam(UDWORD player) }); }; - StructureList::iterator psStructIt = apsStructLists[player].begin(), psNextIt; - while (psStructIt != apsStructLists[player].end()) + mutating_list_iterate(apsStructLists[player], [&cmp, &structsGiftedPerTarget, &incrRecvStruct](STRUCTURE* psStruct) { - psNextIt = std::next(psStructIt); - - if (cmp(*psStructIt)) + if (cmp(psStruct)) { - giftSingleStructure(*psStructIt, structsGiftedPerTarget.front().player, false); + giftSingleStructure(psStruct, structsGiftedPerTarget.front().player, false); incrRecvStruct(0); } - - psStructIt = psNextIt; - } + return IterationResult::CONTINUE_ITERATION; + }); }; // Distribute key structures diff --git a/src/multilimit.cpp b/src/multilimit.cpp index d5c9e2df55e..89e027669c7 100644 --- a/src/multilimit.cpp +++ b/src/multilimit.cpp @@ -380,19 +380,15 @@ bool applyLimitSet() { while (asStructureStats[id].curCount[player] > asStructureStats[id].upgrade[player].limit) { - StructureList::iterator structIt = apsStructLists[player].begin(), structItNext; - while (structIt != apsStructLists[player].end()) + mutating_list_iterate(apsStructLists[player], [id](STRUCTURE* psStruct) { - structItNext = std::next(structIt); - STRUCTURE* psStruct = *structIt; if (psStruct->pStructureType->type == asStructureStats[id].type) { removeStruct(psStruct, true); - break; + return IterationResult::BREAK_ITERATION; } - structIt = structItNext; - } - + return IterationResult::CONTINUE_ITERATION; + }); } } } diff --git a/src/multiplay.cpp b/src/multiplay.cpp index 8e821864017..3c4a0419fe3 100644 --- a/src/multiplay.cpp +++ b/src/multiplay.cpp @@ -2431,47 +2431,41 @@ bool makePlayerSpectator(uint32_t playerIndex, bool removeAllStructs, bool quiet // Destroy all droids debug(LOG_DEATH, "killing off all droids for player %d", playerIndex); - DroidList::iterator droidIt = apsDroidLists[playerIndex].begin(), droidItNext; - while (droidIt != apsDroidLists[playerIndex].end()) // delete all droids + mutating_list_iterate(apsDroidLists[playerIndex], [quietly](DROID* d) { - droidItNext = std::next(droidIt); if (quietly) // don't show effects { - killDroid(*droidIt); + killDroid(d); } else // show effects { - destroyDroid(*droidIt, gameTime); + destroyDroid(d, gameTime); } - droidIt = droidItNext; - } + return IterationResult::CONTINUE_ITERATION; + }); // Destroy structs debug(LOG_DEATH, "killing off structures for player %d", playerIndex); - StructureList::iterator psStructIt = apsStructLists[playerIndex].begin(), psNextIt; - while (psStructIt != apsStructLists[playerIndex].end()) // delete structs + mutating_list_iterate(apsStructLists[playerIndex], [quietly, removeAllStructs](STRUCTURE* psStruct) { - psNextIt = std::next(psStructIt); - if (removeAllStructs - || (*psStructIt)->pStructureType->type == REF_POWER_GEN - || (*psStructIt)->pStructureType->type == REF_RESEARCH - || (*psStructIt)->pStructureType->type == REF_COMMAND_CONTROL - || StructIsFactory((*psStructIt))) + || psStruct->pStructureType->type == REF_POWER_GEN + || psStruct->pStructureType->type == REF_RESEARCH + || psStruct->pStructureType->type == REF_COMMAND_CONTROL + || StructIsFactory(psStruct)) { // FIXME: look why destroyStruct() doesn't put back the feature like removeStruct() does - if (quietly || (*psStructIt)->pStructureType->type == REF_RESOURCE_EXTRACTOR) // don't show effects + if (quietly || psStruct->pStructureType->type == REF_RESOURCE_EXTRACTOR) // don't show effects { - removeStruct(*psStructIt, true); + removeStruct(psStruct, true); } else // show effects { - destroyStruct(*psStructIt, gameTime); + destroyStruct(psStruct, gameTime); } } - - psStructIt = psNextIt; - } + return IterationResult::CONTINUE_ITERATION; + }); } if (!quietly)