diff --git a/src/action.cpp b/src/action.cpp index b2df763de6c..25198751168 100644 --- a/src/action.cpp +++ b/src/action.cpp @@ -2822,7 +2822,7 @@ bool actionVTOLLandingPos(DROID const *psDroid, Vector2i *p) } // clear blocking flags for all the other droids - for (DROID *psCurr : apsDroidLists[psDroid->player]) + for (const DROID *psCurr : apsDroidLists[psDroid->player]) { Vector2i t(0, 0); if (DROID_STOPPED(psCurr)) diff --git a/src/cmddroid.cpp b/src/cmddroid.cpp index ed8d25f53ba..5df095fabb8 100644 --- a/src/cmddroid.cpp +++ b/src/cmddroid.cpp @@ -165,7 +165,7 @@ SDWORD cmdDroidGetIndex(DROID *psCommander) return 0; } - for (DROID* psCurr : apsDroidLists[psCommander->player]) + for (const DROID* psCurr : apsDroidLists[psCommander->player]) { if (psCurr->droidType == DROID_COMMAND && psCurr->id < psCommander->id) diff --git a/src/display.cpp b/src/display.cpp index 96dfe773d9f..8cad6cc0b02 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -417,7 +417,7 @@ static bool localPlayerHasSelection() return false; } - for (DROID* psDroid : apsDroidLists[selectedPlayer]) + for (const DROID* psDroid : apsDroidLists[selectedPlayer]) { if (psDroid->selected) { @@ -425,7 +425,7 @@ static bool localPlayerHasSelection() } } - for (STRUCTURE* psStruct : apsStructLists[selectedPlayer]) + for (const STRUCTURE* psStruct : apsStructLists[selectedPlayer]) { if (psStruct->selected) { @@ -806,7 +806,7 @@ void processMouseClickInput() else if (selection == SC_DROID_REPAIR) { // We can't repair ourselves, so change it to a blocking cursor - for (DROID *psCurr : apsDroidLists[selectedPlayer]) + for (const DROID *psCurr : apsDroidLists[selectedPlayer]) { if (psCurr->selected) { @@ -2676,7 +2676,7 @@ bool repairDroidSelected(UDWORD player) { ASSERT_OR_RETURN(false, player < MAX_PLAYERS, "Invalid player (%" PRIu32 ")", player); - for (DROID* psCurr : apsDroidLists[player]) + for (const DROID* psCurr : apsDroidLists[player]) { if (psCurr->selected && ( psCurr->droidType == DROID_REPAIR || @@ -2714,7 +2714,7 @@ bool anyDroidSelected(UDWORD player) { ASSERT_OR_RETURN(false, player < MAX_PLAYERS, "Invalid player (%" PRIu32 ")", player); - for (DROID* psCurr : apsDroidLists[player]) + for (const DROID* psCurr : apsDroidLists[player]) { if (psCurr->selected) { @@ -2731,7 +2731,7 @@ bool cyborgDroidSelected(UDWORD player) { ASSERT_OR_RETURN(false, player < MAX_PLAYERS, "Invalid player (%" PRIu32 ")", player); - for (DROID* psCurr : apsDroidLists[player]) + for (const DROID* psCurr : apsDroidLists[player]) { if (psCurr->selected && cyborgDroid(psCurr)) { diff --git a/src/display3d.cpp b/src/display3d.cpp index f755b5eb165..544fed589a8 100644 --- a/src/display3d.cpp +++ b/src/display3d.cpp @@ -525,7 +525,7 @@ class BatchedObjectStatusRenderer SDWORD scrX, scrY; for (uint32_t i = 0; i < MAX_PLAYERS; i++) { - for (STRUCTURE* psStruct : apsStructLists[i]) + for (const STRUCTURE* psStruct : apsStructLists[i]) { /* If it's targetted and on-screen */ if (psStruct->flags.test(OBJECT_FLAG_TARGETED) @@ -784,7 +784,7 @@ static void showDroidPaths() return; // no-op for now } - for (DROID *psDroid : apsDroidLists[selectedPlayer]) + for (const DROID *psDroid : apsDroidLists[selectedPlayer]) { if (psDroid->selected && psDroid->sMove.Status != MOVEINACTIVE) { @@ -1098,7 +1098,7 @@ void draw3DScene() { int visibleDroids = 0; int undrawnDroids = 0; - for (DROID *psDroid : apsDroidLists[selectedPlayer]) + for (const DROID *psDroid : apsDroidLists[selectedPlayer]) { if (psDroid->sDisplay.frameNumber != currentGameFrame) { @@ -2183,7 +2183,7 @@ void displayBlueprints(const glm::mat4 &viewMatrix, const glm::mat4 &perspective continue; } STRUCT_STATES state = player == selectedPlayer ? SS_BLUEPRINT_PLANNED : SS_BLUEPRINT_PLANNED_BY_ALLY; - for (DROID *psDroid : apsDroidLists[player]) + for (const DROID *psDroid : apsDroidLists[player]) { if (psDroid->droidType == DROID_CONSTRUCT || psDroid->droidType == DROID_CYBORG_CONSTRUCT) { @@ -4009,7 +4009,7 @@ static void structureEffectsPlayer(UDWORD player) return; // Don't add effects this frame. } - for (STRUCTURE *psStructure : apsStructLists[player]) + for (const STRUCTURE *psStructure : apsStructLists[player]) { if (psStructure->status != SS_BUILT) { diff --git a/src/droid.cpp b/src/droid.cpp index 4199d22ec11..6ca50575c8c 100644 --- a/src/droid.cpp +++ b/src/droid.cpp @@ -570,7 +570,7 @@ bool removeDroidBase(DROID *psDel) if (tryingToGetLocation()) { int numSelectedConstructors = 0; - for (DROID *psDroid : apsDroidLists[psDel->player]) + for (const DROID *psDroid : apsDroidLists[psDel->player]) { numSelectedConstructors += psDroid->selected && isConstructionDroid(psDroid); } @@ -1341,7 +1341,7 @@ bool idfDroid(DROID *psDroid) } /* Return the type of a droid */ -DROID_TYPE droidType(DROID *psDroid) +DROID_TYPE droidType(const DROID *psDroid) { return psDroid->droidType; } @@ -2284,7 +2284,7 @@ UDWORD getNumDroidsForLevel(uint32_t player, UDWORD level) { continue; } - for (DROID* psDroid : *dList) + for (const DROID* psDroid : *dList) { if (getDroidLevel(psDroid) == level) { @@ -2354,7 +2354,7 @@ static bool oneDroidMax(UDWORD x, UDWORD y) // check each droid list for (i = 0; i < MAX_PLAYERS; i++) { - for (DROID* pD : apsDroidLists[i]) + for (const DROID* pD : apsDroidLists[i]) { if (map_coord(pD->pos.x) == x && map_coord(pD->pos.y) == y) @@ -2435,7 +2435,7 @@ static bool ThreatInRange(SDWORD player, SDWORD range, SDWORD rangeX, SDWORD ran } //check structures - for (STRUCTURE* psStruct : apsStructLists[i]) + for (const STRUCTURE* psStruct : apsStructLists[i]) { if (psStruct->visible[player] || psStruct->born == 2) // if can see it or started there { @@ -2464,11 +2464,11 @@ static bool ThreatInRange(SDWORD player, SDWORD range, SDWORD rangeX, SDWORD ran } //check droids - for (DROID* psDroid : apsDroidLists[i]) + for (const DROID* psDroid : apsDroidLists[i]) { if (psDroid->visible[player]) //can see this droid? { - if (!objHasWeapon((BASE_OBJECT *)psDroid)) + if (!objHasWeapon(psDroid)) { continue; } @@ -2554,7 +2554,7 @@ PICKTILE pickHalfATile(UDWORD *x, UDWORD *y, UBYTE numIterations) building the specified structure - returns true if finds one*/ bool checkDroidsBuilding(STRUCTURE *psStructure) { - for (DROID* psDroid : apsDroidLists[psStructure->player]) + for (const DROID* psDroid : apsDroidLists[psStructure->player]) { //check DORDER_BUILD, HELP_BUILD is handled the same BASE_OBJECT *const psStruct = orderStateObj(psDroid, DORDER_BUILD); @@ -2570,7 +2570,7 @@ bool checkDroidsBuilding(STRUCTURE *psStructure) demolishing the specified structure - returns true if finds one*/ bool checkDroidsDemolishing(STRUCTURE *psStructure) { - for (DROID* psDroid : apsDroidLists[psStructure->player]) + for (const DROID* psDroid : apsDroidLists[psStructure->player]) { //check DORDER_DEMOLISH BASE_OBJECT *const psStruct = orderStateObj(psDroid, DORDER_DEMOLISH); @@ -2756,7 +2756,7 @@ UBYTE checkCommandExist(UBYTE player) { UBYTE quantity = 0; - for (DROID *psDroid : apsDroidLists[player]) + for (const DROID *psDroid : apsDroidLists[player]) { if (psDroid->droidType == DROID_COMMAND) { diff --git a/src/droid.h b/src/droid.h index 5fbe1f29b80..ca59abada6c 100644 --- a/src/droid.h +++ b/src/droid.h @@ -163,7 +163,7 @@ bool droidRemove(DROID *psDroid, PerPlayerDroidLists& pList); bool droidTemplateShutDown(); /* Return the type of a droid */ -DROID_TYPE droidType(DROID *psDroid); +DROID_TYPE droidType(const DROID *psDroid); /* Return the type of a droid from it's template */ DROID_TYPE droidTemplateType(const DROID_TEMPLATE *psTemplate); diff --git a/src/game.cpp b/src/game.cpp index 1e73934573a..9f6d363ce79 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -5355,14 +5355,11 @@ static bool loadSaveDroidPointers(const WzString &pFileName, PerPlayerDroidLists foundDroid: if (!psDroid) { - auto missionDroidIt = std::find_if(mission.apsDroidLists[player].begin(), mission.apsDroidLists[player].end(), [id](DROID* d) - { - return d->id == id; - }); + DROID* d = (DROID*)getBaseObjFromId(mission.apsDroidLists[player], id); // FIXME - if (missionDroidIt != mission.apsDroidLists[player].end()) + if (d) { - debug(LOG_ERROR, "Droid %s (%d) was in wrong file/list (was in %s)...", objInfo(*missionDroidIt), id, pFileName.toUtf8().c_str()); + debug(LOG_ERROR, "Droid %s (%d) was in wrong file/list (was in %s)...", objInfo(d), id, pFileName.toUtf8().c_str()); } } ASSERT_OR_RETURN(false, psDroid, "Droid %d not found", id); @@ -5438,7 +5435,7 @@ static void loadSaveObject(WzConfig &ini, BASE_OBJECT *psObj) psObj->born = ini.value("born", 2).toInt(); } -static void writeSaveObject(WzConfig &ini, BASE_OBJECT *psObj) +static void writeSaveObject(WzConfig &ini, const BASE_OBJECT *psObj) { ini.setValue("id", psObj->id); setPlayer(ini, psObj->player); @@ -6528,7 +6525,7 @@ bool writeStructFile(const char *pFileName) for (int player = 0; player < MAX_PLAYERS; player++) { - for (STRUCTURE *psCurr : apsStructLists[player]) + for (const STRUCTURE *psCurr : apsStructLists[player]) { if (!psCurr->pStructureType) { @@ -6696,14 +6693,7 @@ bool loadSaveStructurePointers(const WzString& filename, PerPlayerStructureLists STRUCTURE *psStruct = nullptr; int player = getPlayer(ini); int id = ini.value("id", -1).toInt(); - auto structIt = std::find_if((*ppList)[player].begin(), (*ppList)[player].end(), [id](STRUCTURE* str) - { - return str->id == id; - }); - if (structIt != (*ppList)[player].end()) - { - psStruct = *structIt; - } + psStruct = (STRUCTURE*)getBaseObjFromId((*ppList)[player], id); if (!psStruct) { ini.endGroup(); @@ -7036,7 +7026,7 @@ bool writeFeatureFile(const char *pFileName) WzConfig ini(WzString::fromUtf8(pFileName), WzConfig::ReadAndWrite); int counter = 0; - for (FEATURE *psCurr : apsFeatureLists[0]) + for (const FEATURE *psCurr : apsFeatureLists[0]) { ini.beginGroup("feature_" + (WzString::number(counter++).leftPadToMinimumLength(WzUniCodepoint::fromASCII('0'), 10))); // Zero padded so that alphabetical sort works. ini.setValue("name", psCurr->psStats->id); diff --git a/src/gamehistorylogger.cpp b/src/gamehistorylogger.cpp index 62b7117bd06..f6b68b6619a 100644 --- a/src/gamehistorylogger.cpp +++ b/src/gamehistorylogger.cpp @@ -88,7 +88,7 @@ static uint32_t getNumOilRigs(uint32_t player) } uint32_t result = 0; - for (STRUCTURE *psStruct : apsStructLists[player]) + for (const STRUCTURE *psStruct : apsStructLists[player]) { if (!psStruct->died && (REF_RESOURCE_EXTRACTOR == psStruct->pStructureType->type)) diff --git a/src/geometry.cpp b/src/geometry.cpp index 135571bfda6..77ad64ed8bb 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -137,7 +137,7 @@ Vector2i positionInQuad(Vector2i const &pt, QUAD const &quad) } //----------------------------------------------------------------------------------- -bool objectOnScreen(BASE_OBJECT *object, SDWORD tolerance) +bool objectOnScreen(const BASE_OBJECT *object, SDWORD tolerance) { if (DrawnInLastFrame(object->sDisplay.frameNumber) == true) { diff --git a/src/geometry.h b/src/geometry.h index 7ab91608557..cff25f0ab01 100644 --- a/src/geometry.h +++ b/src/geometry.h @@ -32,7 +32,7 @@ uint16_t calcDirection(int32_t x0, int32_t y0, int32_t x1, int32_t y1); bool inQuad(const Vector2i *pt, const QUAD *quad); Vector2i positionInQuad(Vector2i const &pt, QUAD const &quad); DROID *getNearestDroid(UDWORD x, UDWORD y, bool bSelected); -bool objectOnScreen(BASE_OBJECT *object, SDWORD tolerance); +bool objectOnScreen(const BASE_OBJECT *object, SDWORD tolerance); static inline STRUCTURE *getTileStructure(UDWORD x, UDWORD y) { diff --git a/src/hci.cpp b/src/hci.cpp index 4f39e1c132c..e18aeebbe9e 100644 --- a/src/hci.cpp +++ b/src/hci.cpp @@ -2758,7 +2758,7 @@ static SDWORD intNumSelectedDroids(UDWORD droidType) } num = 0; - for (DROID* psDroid : apsDroidLists[selectedPlayer]) + for (const DROID* psDroid : apsDroidLists[selectedPlayer]) { if (psDroid->selected && psDroid->droidType == droidType) { diff --git a/src/keybind.cpp b/src/keybind.cpp index 78bdd430b2d..457b085559a 100644 --- a/src/keybind.cpp +++ b/src/keybind.cpp @@ -252,7 +252,7 @@ void kf_TraceObject() return; // no-op } - for (DROID* psCDroid : apsDroidLists[selectedPlayer]) + for (const DROID* psCDroid : apsDroidLists[selectedPlayer]) { if (psCDroid->selected) { @@ -261,7 +261,7 @@ void kf_TraceObject() return; } } - for (STRUCTURE* psCStruct : apsStructLists[selectedPlayer]) + for (const STRUCTURE* psCStruct : apsStructLists[selectedPlayer]) { if (psCStruct->selected) { @@ -359,7 +359,7 @@ void kf_DebugDroidInfo() return; // no-op } - for (DROID* psDroid : apsDroidLists[selectedPlayer]) + for (const DROID* psDroid : apsDroidLists[selectedPlayer]) { if (psDroid->selected) { @@ -740,7 +740,7 @@ void kf_ListDroids() } for (int i = 0; i < MAX_PLAYERS; i++) { - for (DROID *psDroid : apsDroidLists[i]) + for (const DROID *psDroid : apsDroidLists[i]) { const auto x = map_coord(psDroid->pos.x); const auto y = map_coord(psDroid->pos.y); @@ -1921,7 +1921,7 @@ void kf_KillEnemy() if (playerId != selectedPlayer && !aiCheckAlliances(selectedPlayer, playerId)) { // wipe out all the droids - for (DROID* psCDroid : apsDroidLists[playerId]) + for (const DROID* psCDroid : apsDroidLists[playerId]) { SendDestroyDroid(psCDroid); } @@ -1957,7 +1957,7 @@ void kf_KillSelected() audio_PlayTrack(ID_SOUND_COLL_DIE); Cheated = true; - for (DROID* psCDroid : apsDroidLists[selectedPlayer]) + for (const DROID* psCDroid : apsDroidLists[selectedPlayer]) { if (psCDroid->selected) { @@ -2144,18 +2144,16 @@ void kf_CentreOnBase() SPECTATOR_NO_OP(); /* Got through our buildings */ - auto structIt = apsStructLists[selectedPlayer].begin(); - while (structIt != apsStructLists[selectedPlayer].end()) + for (const STRUCTURE* pStruct : apsStructLists[selectedPlayer]) { /* Have we got a HQ? */ - if ((*structIt)->pStructureType->type == REF_HQ) + if (pStruct->pStructureType->type == REF_HQ) { bGotHQ = true; - xJump = (*structIt)->pos.x; - yJump = (*structIt)->pos.y; + xJump = pStruct->pos.x; + yJump = pStruct->pos.y; break; } - ++structIt; } /* If we found it, then jump to it! */ diff --git a/src/loop.cpp b/src/loop.cpp index 6387bc6a1fd..1d50b484044 100644 --- a/src/loop.cpp +++ b/src/loop.cpp @@ -439,7 +439,7 @@ void countUpdate(bool synch) break; } } - for (DROID *psCurr : apsLimboDroids[i]) + for (const DROID *psCurr : apsLimboDroids[i]) { // count the type of units switch (psCurr->droidType) @@ -457,7 +457,7 @@ void countUpdate(bool synch) } // FIXME: These for-loops are code duplicationo setLasSatExists(false, i); - for (STRUCTURE *psCBuilding : apsStructLists[i]) + for (const STRUCTURE *psCBuilding : apsStructLists[i]) { if (psCBuilding->pStructureType->type == REF_SAT_UPLINK && psCBuilding->status == SS_BUILT) { @@ -469,7 +469,7 @@ void countUpdate(bool synch) setLasSatExists(true, i); } } - for (STRUCTURE *psCBuilding : mission.apsStructLists[i]) + for (const STRUCTURE *psCBuilding : mission.apsStructLists[i]) { if (psCBuilding->pStructureType->type == REF_SAT_UPLINK && psCBuilding->status == SS_BUILT) { diff --git a/src/mission.cpp b/src/mission.cpp index aea3c797e5d..616ce8d7e08 100644 --- a/src/mission.cpp +++ b/src/mission.cpp @@ -678,7 +678,7 @@ static void saveMissionData() if (psStruct->status == SS_BEING_BUILT) { //find a droid working on it - for (DROID* psDroid : apsDroidLists[selectedPlayer]) + for (const DROID* psDroid : apsDroidLists[selectedPlayer]) { if ((psStructBeingBuilt = (STRUCTURE *)orderStateObj(psDroid, DORDER_BUILD)) && psStructBeingBuilt == psStruct) @@ -1637,7 +1637,6 @@ static void missionResetDroids() if (pickRes == NO_FREE_TILE) { ASSERT(false, "missionResetUnits: Unable to find a free location"); - psStruct = nullptr; } else { @@ -1650,7 +1649,7 @@ static void missionResetDroids() } else // if couldn't find the factory - try to place near HQ instead { - for (STRUCTURE* psStructure : apsStructLists[psDroid->player]) + for (const STRUCTURE* psStructure : apsStructLists[psDroid->player]) { if (psStructure->pStructureType->type == REF_HQ) { @@ -1661,7 +1660,6 @@ static void missionResetDroids() if (pickRes == NO_FREE_TILE) { ASSERT(false, "missionResetUnits: Unable to find a free location"); - psStructure = nullptr; } else { @@ -3048,7 +3046,7 @@ bool getPlayCountDown() bool missionDroidsRemaining(UDWORD player) { ASSERT_OR_RETURN(false, player < MAX_PLAYERS, "invalid player: %" PRIu32 "", player); - for (DROID *psDroid : apsDroidLists[player]) + for (const DROID *psDroid : apsDroidLists[player]) { if (!isTransporter(psDroid)) { diff --git a/src/multigifts.cpp b/src/multigifts.cpp index bf51eea34dd..a76cf68c287 100644 --- a/src/multigifts.cpp +++ b/src/multigifts.cpp @@ -294,7 +294,7 @@ static void recvGiftDroids(uint8_t from, uint8_t to, uint32_t droidID) // \param to :player that should be getting the droid static void sendGiftDroids(uint8_t from, uint8_t to) { - DroidList::iterator psD; + DroidList::const_iterator psD; uint8_t giftType = DROID_GIFT; uint8_t totalToSend; @@ -501,7 +501,7 @@ void breakAlliance(uint8_t p1, uint8_t p2, bool prop, bool allowAudio) // Make sure p1's structures are no longer considered "our buildings" to their former allies // For unit pathing - for (STRUCTURE* psStructure : apsStructLists[p1]) + for (const STRUCTURE* psStructure : apsStructLists[p1]) { StructureBounds b = getStructureBounds(psStructure); @@ -515,7 +515,7 @@ void breakAlliance(uint8_t p1, uint8_t p2, bool prop, bool allowAudio) } } // Do the same for p2's stuff - for (STRUCTURE* psStructure : apsStructLists[p2]) + for (const STRUCTURE* psStructure : apsStructLists[p2]) { StructureBounds b = getStructureBounds(psStructure); @@ -590,7 +590,7 @@ void formAlliance(uint8_t p1, uint8_t p2, bool prop, bool allowAudio, bool allow } // Properly mark all of p1's structures as allied buildings for unit pathing - for (STRUCTURE* psStructure : apsStructLists[p1]) + for (const STRUCTURE* psStructure : apsStructLists[p1]) { StructureBounds b = getStructureBounds(psStructure); @@ -612,7 +612,7 @@ void formAlliance(uint8_t p1, uint8_t p2, bool prop, bool allowAudio, bool allow } } // Do the same for p2's stuff - for (STRUCTURE* psStructure : apsStructLists[p2]) + for (const STRUCTURE* psStructure : apsStructLists[p2]) { StructureBounds b = getStructureBounds(psStructure); diff --git a/src/multiplay.cpp b/src/multiplay.cpp index 55f80cbe32a..e1db693e2b5 100644 --- a/src/multiplay.cpp +++ b/src/multiplay.cpp @@ -392,23 +392,19 @@ DROID *IdToDroid(UDWORD id, UDWORD player) { for (int i = 0; i < MAX_PLAYERS; i++) { - for (DROID *d : apsDroidLists[i]) + DROID* d = (DROID*)getBaseObjFromId(apsDroidLists[i], id); + if (d) { - if (d->id == id) - { - return d; - } + return d; } } } else if (player < MAX_PLAYERS) { - for (DROID *d : apsDroidLists[player]) + DROID* d = (DROID*)getBaseObjFromId(apsDroidLists[player], id); + if (d) { - if (d->id == id) - { - return d; - } + return d; } } return nullptr; @@ -421,23 +417,19 @@ DROID *IdToMissionDroid(UDWORD id, UDWORD player) { for (int i = 0; i < MAX_PLAYERS; i++) { - for (DROID *d : mission.apsDroidLists[i]) + DROID* d = (DROID*)getBaseObjFromId(mission.apsDroidLists[i], id); + if (d) { - if (d->id == id) - { - return d; - } + return d; } } } else if (player < MAX_PLAYERS) { - for (DROID *d : mission.apsDroidLists[player]) + DROID* d = (DROID*)getBaseObjFromId(mission.apsDroidLists[player], id); + if (d) { - if (d->id == id) - { - return d; - } + return d; } } return nullptr; @@ -447,21 +439,15 @@ static STRUCTURE* _IdToStruct(UDWORD id, UDWORD beginPlayer, UDWORD endPlayer) { for (int i = beginPlayer; i < endPlayer; ++i) { - auto it = std::find_if(apsStructLists[i].begin(), apsStructLists[i].end(), [id](STRUCTURE* s) - { - return s->id == id; - }); - if (it != apsStructLists[i].end()) + STRUCTURE* s = (STRUCTURE*)getBaseObjFromId(apsStructLists[i], id); + if (s) { - return *it; + return s; } - it = std::find_if(mission.apsStructLists[i].begin(), mission.apsStructLists[i].end(), [id](STRUCTURE* s) + s = (STRUCTURE*)getBaseObjFromId(mission.apsStructLists[i], id); + if (s) { - return s->id == id; - }); - if (it != mission.apsStructLists[i].end()) - { - return *it; + return s; } } return nullptr; @@ -490,11 +476,7 @@ STRUCTURE *IdToStruct(UDWORD id, UDWORD player) FEATURE *IdToFeature(UDWORD id, UDWORD player) { (void)player; // unused, all features go into player 0 - auto feat = std::find_if(apsFeatureLists[0].begin(), apsFeatureLists[0].end(), [id](FEATURE* f) - { - return f->id == id; - }); - return feat != apsFeatureLists[0].end() ? *feat : nullptr; + return (FEATURE*)getBaseObjFromId(apsFeatureLists[0], id); } // //////////////////////////////////////////////////////////////////////////// diff --git a/src/objmem.cpp b/src/objmem.cpp index 5cbfe596e62..3f7e2e46ada 100644 --- a/src/objmem.cpp +++ b/src/objmem.cpp @@ -98,7 +98,7 @@ static bool checkReferences(BASE_OBJECT *psVictim) { for (int plr = 0; plr < MAX_PLAYERS; ++plr) { - for (STRUCTURE *psStruct : apsStructLists[plr]) + for (const STRUCTURE *psStruct : apsStructLists[plr]) { if (psStruct == psVictim) { @@ -110,7 +110,7 @@ static bool checkReferences(BASE_OBJECT *psVictim) ASSERT_OR_RETURN(false, psStruct->psTarget[i] != psVictim, BADREF(psStruct->targetFunc[i], psStruct->targetLine[i])); } } - for (DROID *psDroid : apsDroidLists[plr]) + for (const DROID *psDroid : apsDroidLists[plr]) { if (psDroid == psVictim) { @@ -699,16 +699,6 @@ void checkFactoryFlags() /************************** OBJECT ACCESS FUNCTIONALITY ********************************/ -template -BASE_OBJECT* getBaseObjFromId(const std::list& list, unsigned id) -{ - auto objIt = std::find_if(list.begin(), list.end(), [id](ObjectType* obj) - { - return obj->id == id; - }); - return objIt != list.end() ? *objIt : nullptr; -} - static BASE_OBJECT* getBaseObjFromDroidId(const DroidList& list, unsigned id) { for (DROID* psObj : list) @@ -886,7 +876,7 @@ static void objListIntegCheck() for (player = 0; player < MAX_PLAYERS; player += 1) { - for (DROID* psCurr : apsDroidLists[player]) + for (const DROID* psCurr : apsDroidLists[player]) { ASSERT(psCurr->type == OBJ_DROID && (SDWORD)psCurr->player == player, @@ -896,7 +886,7 @@ static void objListIntegCheck() } for (player = 0; player < MAX_PLAYERS; player += 1) { - for (STRUCTURE* psStruct : apsStructLists[player]) + for (const STRUCTURE* psStruct : apsStructLists[player]) { ASSERT(psStruct->type == OBJ_STRUCTURE && (SDWORD)psStruct->player == player, @@ -904,7 +894,7 @@ static void objListIntegCheck() objInfo(psStruct), (void*)psStruct, player, (int)psStruct->player); } } - for (BASE_OBJECT* obj : apsFeatureLists[0]) + for (const BASE_OBJECT* obj : apsFeatureLists[0]) { ASSERT(obj->type == OBJ_FEATURE, "objListIntegCheck: misplaced object in the feature list"); @@ -924,12 +914,12 @@ void objCount(int *droids, int *structures, int *features) for (int i = 0; i < MAX_PLAYERS; i++) { - for (DROID *psDroid : apsDroidLists[i]) + for (const DROID *psDroid : apsDroidLists[i]) { (*droids)++; if (isTransporter(psDroid) && psDroid->psGroup && psDroid->psGroup->psList) { - DROID *psTrans = psDroid->psGroup->psList; + const DROID *psTrans = psDroid->psGroup->psList; for (psTrans = psTrans->psGrpNext; psTrans != nullptr; psTrans = psTrans->psGrpNext) { diff --git a/src/objmem.h b/src/objmem.h index 7a63d53fe74..a1a61f55521 100644 --- a/src/objmem.h +++ b/src/objmem.h @@ -132,6 +132,16 @@ void freeAllFlagPositions(); void addFlagPositionToList(FLAG_POSITION* psFlagPosToAdd, PerPlayerFlagPositionLists& list); // Find a base object from it's id +template +BASE_OBJECT* getBaseObjFromId(const std::list& list, unsigned id) +{ + auto objIt = std::find_if(list.begin(), list.end(), [id](ObjectType* obj) + { + return obj->id == id; + }); + return objIt != list.end() ? *objIt : nullptr; +} + BASE_OBJECT *getBaseObjFromData(unsigned id, unsigned player, OBJECT_TYPE type); BASE_OBJECT *getBaseObjFromId(UDWORD id); diff --git a/src/order.cpp b/src/order.cpp index de058005a1b..14695312f36 100644 --- a/src/order.cpp +++ b/src/order.cpp @@ -1645,7 +1645,7 @@ void orderDroidBase(DROID *psDroid, DROID_ORDER_DATA *psOrder) } break; case DORDER_RTB: - for (STRUCTURE* psStruct : apsStructLists[psDroid->player]) + for (const STRUCTURE* psStruct : apsStructLists[psDroid->player]) { if (psStruct->pStructureType->type == REF_HQ) { @@ -2832,7 +2832,7 @@ static void orderPlayOrderObjAudio(UDWORD player, BASE_OBJECT *psObj) ASSERT_PLAYER_OR_RETURN(, player); /* loop over selected droids */ - for (DROID *psDroid : apsDroidLists[player]) + for (const DROID *psDroid : apsDroidLists[player]) { if (psDroid->selected) { @@ -3941,7 +3941,7 @@ static SECONDARY_STATE secondaryGetAverageGroupState(UDWORD player, UDWORD group // count the number of units for each state numStates = 0; memset(aStateCount, 0, sizeof(aStateCount)); - for (DROID* psCurr : apsDroidLists[player]) + for (const DROID* psCurr : apsDroidLists[player]) { if (psCurr->group == group) { diff --git a/src/power.cpp b/src/power.cpp index 656d83d795f..8e72722ca71 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -144,7 +144,7 @@ void delPowerRequest(STRUCTURE *psStruct) } } -static int64_t checkPrecisePowerRequest(STRUCTURE *psStruct) +static int64_t checkPrecisePowerRequest(const STRUCTURE *psStruct) { ASSERT_NOT_NULLPTR_OR_RETURN(-1, psStruct); PlayerPower const *p = &asPower[psStruct->player]; @@ -166,7 +166,7 @@ static int64_t checkPrecisePowerRequest(STRUCTURE *psStruct) return -1; } -int32_t checkPowerRequest(STRUCTURE *psStruct) +int32_t checkPowerRequest(const STRUCTURE *psStruct) { int64_t power = checkPrecisePowerRequest(psStruct); return power != -1 ? power / FP_ONE : -1; diff --git a/src/power.h b/src/power.h index ac9db5153ed..80cbb73fdaa 100644 --- a/src/power.h +++ b/src/power.h @@ -38,7 +38,7 @@ void delPowerRequest(STRUCTURE *psStruct); /// Checks how much power must be accumulated, before the power request from this structure can be satisfied. /// Returns -1 if there is no power request or if there is enough power already. -int32_t checkPowerRequest(STRUCTURE *psStruct); +int32_t checkPowerRequest(const STRUCTURE *psStruct); bool requestPowerFor(STRUCTURE *psStruct, int32_t amount); bool requestPrecisePowerFor(STRUCTURE *psStruct, int64_t amount); diff --git a/src/radar.cpp b/src/radar.cpp index 549c2b7b912..373c1d6a335 100644 --- a/src/radar.cpp +++ b/src/radar.cpp @@ -514,7 +514,7 @@ static void DrawRadarObjects() flashCol = flashColours[getPlayerColour(clan)]; /* Go through all droids */ - for (DROID* psDroid : apsDroidLists[clan]) + for (const DROID* psDroid : apsDroidLists[clan]) { if (psDroid->pos.x < world_coord(scrollMinX) || psDroid->pos.y < world_coord(scrollMinY) || psDroid->pos.x >= world_coord(scrollMaxX) || psDroid->pos.y >= world_coord(scrollMaxY)) diff --git a/src/research.cpp b/src/research.cpp index bef604afe90..a63ab6c75d2 100644 --- a/src/research.cpp +++ b/src/research.cpp @@ -1519,7 +1519,7 @@ void researchReward(UBYTE losingPlayer, UBYTE rewardPlayer) UDWORD topicIndex = 0, researchPoints = 0, rewardID = 0; //look through the losing players structures to find a research facility - for (STRUCTURE *psStruct : apsStructLists[losingPlayer]) + for (const STRUCTURE *psStruct : apsStructLists[losingPlayer]) { if (psStruct->pStructureType->type == REF_RESEARCH) { @@ -1728,7 +1728,7 @@ std::vector const &listAllyResearch(unsigned ref) } // Check each research facility to see if they are doing this topic. (As opposed to having started the topic, but stopped researching it.) - for (STRUCTURE *psStruct : apsStructLists[player]) + for (const STRUCTURE *psStruct : apsStructLists[player]) { RESEARCH_FACILITY *res = (RESEARCH_FACILITY *)psStruct->pFunctionality; if (psStruct->pStructureType->type != REF_RESEARCH || res->psSubject == nullptr) diff --git a/src/scores.cpp b/src/scores.cpp index d69db4f2cbf..e2a9a6eb480 100644 --- a/src/scores.cpp +++ b/src/scores.cpp @@ -308,7 +308,7 @@ END_GAME_STATS_DATA collectEndGameStatsData() { continue; } - for (DROID* psDroid : *dList) + for (const DROID* psDroid : *dList) { if (isTransporter(psDroid)) { @@ -621,7 +621,7 @@ void stdOutGameSummary(UDWORD realTimeThrottleSeconds, bool flush_output /* = tr uint32_t numFactories = 0; uint32_t numResearch = 0; uint32_t numFactoriesThatCanProduceConstructionUnits = 0; - for (STRUCTURE *psStruct : apsStructLists[n]) + for (const STRUCTURE *psStruct : apsStructLists[n]) { if (psStruct->status != SS_BUILT || psStruct->died != 0) { diff --git a/src/selection.cpp b/src/selection.cpp index 4db5cb5781f..8cfcd8516a3 100644 --- a/src/selection.cpp +++ b/src/selection.cpp @@ -161,7 +161,7 @@ unsigned int selNumSelected(unsigned int player) unsigned int count = 0; if (player >= MAX_PLAYERS) { return 0; } - for (DROID *psDroid : apsDroidLists[player]) + for (const DROID *psDroid : apsDroidLists[player]) { if (psDroid->selected) { @@ -173,7 +173,7 @@ unsigned int selNumSelected(unsigned int player) } -std::vector buildComponentsFromDroid(DROID* psDroid) +std::vector buildComponentsFromDroid(const DROID* psDroid) { std::vector components; uint32_t stat = 0; @@ -532,7 +532,7 @@ static bool droidIsCommanderNum(DROID *psDroid, SDWORD n) } int numLess = 0; - for (DROID *psCurr : apsDroidLists[psDroid->player]) + for (const DROID *psCurr : apsDroidLists[psDroid->player]) { if ((psCurr->droidType == DROID_COMMAND) && (psCurr->id < psDroid->id)) { diff --git a/src/selection.h b/src/selection.h index 7bccbc4fb24..25e3a055a9d 100644 --- a/src/selection.h +++ b/src/selection.h @@ -62,6 +62,6 @@ void selNextSpecifiedBuilding(STRUCTURE_TYPE structType, bool jump); void selNextSpecifiedUnit(DROID_TYPE unitType); // select the n'th command droid void selCommander(int n); -std::vector buildComponentsFromDroid(DROID* psDroid); +std::vector buildComponentsFromDroid(const DROID* psDroid); #endif // __INCLUDED_SRC_SELECTION_H__ diff --git a/src/structure.cpp b/src/structure.cpp index 1ac3f336033..ebd2fe4ec18 100644 --- a/src/structure.cpp +++ b/src/structure.cpp @@ -321,7 +321,7 @@ void resetFactoryNumFlag() factoryNumFlag[i][type].clear(); } //look through the list of structures to see which have been used - for (STRUCTURE *psStruct : apsStructLists[i]) + for (const STRUCTURE *psStruct : apsStructLists[i]) { FLAG_TYPE type; switch (psStruct->pStructureType->type) @@ -829,7 +829,7 @@ void structureBuild(STRUCTURE *psStruct, DROID *psDroid, int buildPoints, int bu { for (unsigned player = 0; player < MAX_PLAYERS; player++) { - for (DROID *psCurr : apsDroidLists[player]) + for (const DROID *psCurr : apsDroidLists[player]) { // An enemy droid is blocking it if ((STRUCTURE *) orderStateObj(psCurr, DORDER_BUILD) == psStruct @@ -1665,7 +1665,7 @@ STRUCTURE *buildStructureDir(STRUCTURE_STATS *pStructureType, UDWORD x, UDWORD y StructureBounds bounds = getStructureBounds(psBuilding); for (unsigned playerNum = 0; playerNum < MAX_PLAYERS; ++playerNum) { - for (STRUCTURE *psStruct : apsStructLists[playerNum]) + for (const STRUCTURE *psStruct : apsStructLists[playerNum]) { FLAG_POSITION *fp = nullptr; if (StructIsFactory(psStruct)) @@ -2301,7 +2301,7 @@ static bool structClearTile(UWORD x, UWORD y) /* Check for a droid */ for (player = 0; player < MAX_PLAYERS; player++) { - for (DROID* psCurr : apsDroidLists[player]) + for (const DROID* psCurr : apsDroidLists[player]) { if (map_coord(psCurr->pos.x) == x && map_coord(psCurr->pos.y) == y) @@ -2641,7 +2641,7 @@ bool structureExists(int player, STRUCTURE_TYPE type, bool built, bool isMission } StructureList* pList = isMission ? &mission.apsStructLists[player] : &apsStructLists[player]; - for (STRUCTURE *psCurr : *pList) + for (const STRUCTURE *psCurr : *pList) { if (psCurr->pStructureType->type == type && (!built || (built && psCurr->status == SS_BUILT))) { @@ -2918,11 +2918,11 @@ static void aiUpdateStructure(STRUCTURE *psStructure, bool isMission) { // This isn't supposed to happen, and really shouldn't be possible - if this happens, maybe a structure is being updated twice? int count1 = 0, count2 = 0; - for (STRUCTURE* s : apsStructLists[psStructure->player]) + for (const STRUCTURE* s : apsStructLists[psStructure->player]) { count1 += s == psStructure; } - for (STRUCTURE* s : mission.apsStructLists[psStructure->player]) + for (const STRUCTURE* s : mission.apsStructLists[psStructure->player]) { count2 += s == psStructure; } @@ -3920,7 +3920,7 @@ std::vector fillStructureList(UDWORD _selectedPlayer, UDWORD //if currently on a mission can't build factory/research/power/derricks if (!missionIsOffworld()) { - for (STRUCTURE* psCurr : apsStructLists[_selectedPlayer]) + for (const STRUCTURE* psCurr : apsStructLists[_selectedPlayer]) { if (psCurr->pStructureType->type == REF_RESEARCH && psCurr->status == SS_BUILT) { @@ -4749,7 +4749,7 @@ bool checkSpecificStructExists(UDWORD structInc, UDWORD player) { ASSERT_OR_RETURN(false, structInc < numStructureStats, "Invalid structure inc"); - for (STRUCTURE *psStructure : apsStructLists[player]) + for (const STRUCTURE *psStructure : apsStructLists[player]) { if (psStructure->status == SS_BUILT) { @@ -5080,7 +5080,7 @@ uint16_t countPlayerUnusedDerricks() if (selectedPlayer >= MAX_PLAYERS) { return 0; } - for (STRUCTURE *psStruct : apsExtractorLists[selectedPlayer]) + for (const STRUCTURE *psStruct : apsExtractorLists[selectedPlayer]) { if (psStruct->status == SS_BUILT && psStruct->pStructureType->type == REF_RESOURCE_EXTRACTOR) { @@ -6714,14 +6714,14 @@ void ensureRearmPadClear(STRUCTURE *psStruct, DROID *psDroid) // return whether a rearm pad has a vtol on it -bool vtolOnRearmPad(STRUCTURE *psStruct, DROID *psDroid) +bool vtolOnRearmPad(const STRUCTURE *psStruct, DROID *psDroid) { SDWORD tx, ty; tx = map_coord(psStruct->pos.x); ty = map_coord(psStruct->pos.y); - for (DROID* psCurr : apsDroidLists[psStruct->player]) + for (const DROID* psCurr : apsDroidLists[psStruct->player]) { if (psCurr != psDroid && map_coord(psCurr->pos.x) == tx diff --git a/src/structure.h b/src/structure.h index f9f74802db7..671c3c3eca3 100644 --- a/src/structure.h +++ b/src/structure.h @@ -311,7 +311,7 @@ bool clearRearmPad(const STRUCTURE *psStruct); void ensureRearmPadClear(STRUCTURE *psStruct, DROID *psDroid); // return whether a rearm pad has a vtol on it -bool vtolOnRearmPad(STRUCTURE *psStruct, DROID *psDroid); +bool vtolOnRearmPad(const STRUCTURE *psStruct, DROID *psDroid); /* Just returns true if the structure's present body points aren't as high as the original*/ bool structIsDamaged(STRUCTURE *psStruct); @@ -462,9 +462,9 @@ static inline STRUCTURE const *castStructure(SIMPLE_OBJECT const *psObject) return isStructure(psObject) ? (STRUCTURE const *)psObject : (STRUCTURE const *)nullptr; } -static inline int getBuildingResearchPoints(STRUCTURE *psStruct) +static inline int getBuildingResearchPoints(const STRUCTURE *psStruct) { - auto &upgrade = psStruct->pStructureType->upgrade[psStruct->player]; + const auto &upgrade = psStruct->pStructureType->upgrade[psStruct->player]; return upgrade.research + upgrade.moduleResearch * psStruct->capacity; } diff --git a/src/visibility.cpp b/src/visibility.cpp index 089c0023ac9..522401b7c03 100644 --- a/src/visibility.cpp +++ b/src/visibility.cpp @@ -860,7 +860,7 @@ void processVisibility() processVisibilityVision(psObj); } } - for (BASE_OBJECT *psObj : apsSensorList[0]) + for (const BASE_OBJECT *psObj : apsSensorList[0]) { if (objRadarDetector(psObj)) { diff --git a/src/warcam.cpp b/src/warcam.cpp index e0e5e728893..4ec0f480f8f 100644 --- a/src/warcam.cpp +++ b/src/warcam.cpp @@ -453,7 +453,7 @@ static uint16_t getAverageTrackAngle(unsigned groupNumber, bool bCheckOnScreen) } /* Got thru' all droids */ - for (DROID* psDroid : apsDroidLists[selectedPlayer]) + for (const DROID* psDroid : apsDroidLists[selectedPlayer]) { /* Is he worth selecting? */ if (groupNumber == GROUP_SELECTED ? psDroid->selected : psDroid->group == groupNumber) @@ -482,7 +482,7 @@ static void getTrackingConcerns(SDWORD *x, SDWORD *y, SDWORD *z, UDWORD groupNum return; } - for (DROID* psDroid : apsDroidLists[selectedPlayer]) + for (const DROID* psDroid : apsDroidLists[selectedPlayer]) { if (groupNumber == GROUP_SELECTED ? psDroid->selected : psDroid->group == groupNumber) { diff --git a/src/wzapi.cpp b/src/wzapi.cpp index f266bdd5125..15b99aa5edd 100644 --- a/src/wzapi.cpp +++ b/src/wzapi.cpp @@ -1120,7 +1120,7 @@ std::vector wzapi::enumFeature(WZAPI_PARAMS(int playerFilter, o } std::vector matches; - for (FEATURE *psFeat : apsFeatureLists[0]) + for (const FEATURE *psFeat : apsFeatureLists[0]) { if ((playerFilter == ALL_PLAYERS || psFeat->visible[playerFilter]) && !psFeat->died @@ -1142,7 +1142,7 @@ std::vector wzapi::enumBlips(WZAPI_PARAMS(int player)) { SCRIPT_ASSERT_PLAYER({}, context, player); std::vector matches; - for (BASE_OBJECT *psSensor : apsSensorList[0]) + for (const BASE_OBJECT *psSensor : apsSensorList[0]) { if (psSensor->visible[player] > 0 && psSensor->visible[player] < UBYTE_MAX) { @@ -1965,7 +1965,7 @@ wzapi::returned_nullable_ptr wzapi::addFeature(WZAPI_PARAMS(std:: { int feature = getFeatureStatFromName(WzString::fromUtf8(featureName)); FEATURE_STATS *psStats = &asFeatureStats[feature]; - for (FEATURE *psFeat : apsFeatureLists[0]) + for (const FEATURE *psFeat : apsFeatureLists[0]) { SCRIPT_ASSERT(nullptr, context, map_coord(psFeat->pos.x) != x || map_coord(psFeat->pos.y) != y, "Building feature on tile already occupied");