Skip to content

Commit

Permalink
Const-correctness checks in loops, minor fixes in object memory manag…
Browse files Browse the repository at this point in the history
…ement

Signed-off-by: Pavel Solodovnikov <[email protected]>
  • Loading branch information
ManManson committed Dec 13, 2023
1 parent df80117 commit 5722d9d
Show file tree
Hide file tree
Showing 31 changed files with 128 additions and 160 deletions.
2 changes: 1 addition & 1 deletion src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/cmddroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,15 @@ static bool localPlayerHasSelection()
return false;
}

for (DROID* psDroid : apsDroidLists[selectedPlayer])
for (const DROID* psDroid : apsDroidLists[selectedPlayer])
{
if (psDroid->selected)
{
return true;
}
}

for (STRUCTURE* psStruct : apsStructLists[selectedPlayer])
for (const STRUCTURE* psStruct : apsStructLists[selectedPlayer])
{
if (psStruct->selected)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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)
{
Expand All @@ -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))
{
Expand Down
10 changes: 5 additions & 5 deletions src/display3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
20 changes: 10 additions & 10 deletions src/droid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/droid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 7 additions & 17 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/gamehistorylogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/hci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
22 changes: 10 additions & 12 deletions src/keybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void kf_TraceObject()
return; // no-op
}

for (DROID* psCDroid : apsDroidLists[selectedPlayer])
for (const DROID* psCDroid : apsDroidLists[selectedPlayer])
{
if (psCDroid->selected)
{
Expand All @@ -261,7 +261,7 @@ void kf_TraceObject()
return;
}
}
for (STRUCTURE* psCStruct : apsStructLists[selectedPlayer])
for (const STRUCTURE* psCStruct : apsStructLists[selectedPlayer])
{
if (psCStruct->selected)
{
Expand Down Expand Up @@ -359,7 +359,7 @@ void kf_DebugDroidInfo()
return; // no-op
}

for (DROID* psDroid : apsDroidLists[selectedPlayer])
for (const DROID* psDroid : apsDroidLists[selectedPlayer])
{
if (psDroid->selected)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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! */
Expand Down
Loading

0 comments on commit 5722d9d

Please sign in to comment.