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

Convert more C-style lists to std::list #3588

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/display3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2284,7 +2284,7 @@ static void displayProximityMsgs(const glm::mat4& viewMatrix, const glm::mat4 &p
if (selectedPlayer >= MAX_PLAYERS) { return; /* no-op */ }

/* Go through all the proximity Displays*/
for (PROXIMITY_DISPLAY *psProxDisp = apsProxDisp[selectedPlayer]; psProxDisp != nullptr; psProxDisp = psProxDisp->psNext)
for (PROXIMITY_DISPLAY* psProxDisp : apsProxDisp[selectedPlayer])
{
if (!(psProxDisp->psMessage->read))
{
Expand Down
63 changes: 29 additions & 34 deletions src/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@
#include "random.h"

/* The statistics for the features */
FEATURE_STATS *asFeatureStats;
UDWORD numFeatureStats;
std::vector<FEATURE_STATS> asFeatureStats;

//Value is stored for easy access to this feature in destroyDroid()/destroyStruct()
FEATURE_STATS *oilResFeature = nullptr;

void featureInitVars()
{
asFeatureStats = nullptr;
numFeatureStats = 0;
asFeatureStats.clear();
oilResFeature = nullptr;
}

Expand All @@ -70,70 +68,69 @@ bool loadFeatureStats(WzConfig &ini)
{
ASSERT(ini.isAtDocumentRoot(), "WzConfig instance is in the middle of traversal");
std::vector<WzString> list = ini.childGroups();
asFeatureStats = new FEATURE_STATS[list.size()];
numFeatureStats = list.size();
asFeatureStats.reserve(list.size());
for (int i = 0; i < list.size(); ++i)
{
ini.beginGroup(list[i]);
asFeatureStats[i] = FEATURE_STATS(STAT_FEATURE + i);
FEATURE_STATS *p = &asFeatureStats[i];
p->name = ini.string(WzString::fromUtf8("name"));
p->id = list[i];
asFeatureStats.emplace_back(STAT_FEATURE + i);
FEATURE_STATS& p = asFeatureStats.at(i);
p.name = ini.string(WzString::fromUtf8("name"));
p.id = list[i];
WzString subType = ini.value("type").toWzString();
if (subType == "TANK WRECK")
{
p->subType = FEAT_TANK;
p.subType = FEAT_TANK;
}
else if (subType == "GENERIC ARTEFACT")
{
p->subType = FEAT_GEN_ARTE;
p.subType = FEAT_GEN_ARTE;
}
else if (subType == "OIL RESOURCE")
{
p->subType = FEAT_OIL_RESOURCE;
p.subType = FEAT_OIL_RESOURCE;
}
else if (subType == "BOULDER")
{
p->subType = FEAT_BOULDER;
p.subType = FEAT_BOULDER;
}
else if (subType == "VEHICLE")
{
p->subType = FEAT_VEHICLE;
p.subType = FEAT_VEHICLE;
}
else if (subType == "BUILDING")
{
p->subType = FEAT_BUILDING;
p.subType = FEAT_BUILDING;
}
else if (subType == "OIL DRUM")
{
p->subType = FEAT_OIL_DRUM;
p.subType = FEAT_OIL_DRUM;
}
else if (subType == "TREE")
{
p->subType = FEAT_TREE;
p.subType = FEAT_TREE;
}
else if (subType == "SKYSCRAPER")
{
p->subType = FEAT_SKYSCRAPER;
p.subType = FEAT_SKYSCRAPER;
}
else
{
ASSERT(false, "Unknown feature type: %s", subType.toUtf8().c_str());
}
p->psImd = modelGet(ini.value("model").toWzString());
p->baseWidth = ini.value("width", 1).toInt();
p->baseBreadth = ini.value("breadth", 1).toInt();
p->tileDraw = ini.value("tileDraw", 1).toInt();
p->allowLOS = ini.value("lineOfSight", 1).toInt();
p->visibleAtStart = ini.value("startVisible", 1).toInt();
p->damageable = ini.value("damageable", 1).toInt();
p->body = ini.value("hitpoints", 1).toInt();
p->armourValue = ini.value("armour", 1).toInt();
p.psImd = modelGet(ini.value("model").toWzString());
p.baseWidth = ini.value("width", 1).toInt();
p.baseBreadth = ini.value("breadth", 1).toInt();
p.tileDraw = ini.value("tileDraw", 1).toInt();
p.allowLOS = ini.value("lineOfSight", 1).toInt();
p.visibleAtStart = ini.value("startVisible", 1).toInt();
p.damageable = ini.value("damageable", 1).toInt();
p.body = ini.value("hitpoints", 1).toInt();
p.armourValue = ini.value("armour", 1).toInt();

//and the oil resource - assumes only one!
if (asFeatureStats[i].subType == FEAT_OIL_RESOURCE)
if (p.subType == FEAT_OIL_RESOURCE)
{
oilResFeature = &asFeatureStats[i];
oilResFeature = &p;
}
ini.endGroup();
}
Expand All @@ -144,9 +141,7 @@ bool loadFeatureStats(WzConfig &ini)
/* Release the feature stats memory */
void featureStatsShutDown()
{
delete[] asFeatureStats;
asFeatureStats = nullptr;
numFeatureStats = 0;
asFeatureStats.clear();
}

/** Deals with damage to a feature
Expand Down Expand Up @@ -539,7 +534,7 @@ SDWORD getFeatureStatFromName(const WzString &name)
{
FEATURE_STATS *psStat;

for (unsigned inc = 0; inc < numFeatureStats; inc++)
for (unsigned inc = 0, size = asFeatureStats.size(); inc < size; inc++)
{
psStat = &asFeatureStats[inc];
if (psStat->id.compare(name) == 0)
Expand Down
3 changes: 1 addition & 2 deletions src/feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
#include "lib/framework/wzconfig.h"

/* The statistics for the features */
extern FEATURE_STATS *asFeatureStats;
extern UDWORD numFeatureStats;
extern std::vector<FEATURE_STATS> asFeatureStats;

//Value is stored for easy access to this feature in destroyDroid()/destroyStruct()
extern FEATURE_STATS *oilResFeature;
Expand Down
52 changes: 22 additions & 30 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2537,7 +2537,7 @@ bool loadGame(const char *pGameToLoad, bool keepObjects, bool freeMem, bool User
apsFeatureLists[player].clear();
apsFlagPosLists[player].clear();
//clear all the messages?
apsProxDisp[player] = nullptr;
apsProxDisp[player].clear();
apsSensorList[0].clear();
apsExtractorLists[player].clear();
}
Expand Down 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 Expand Up @@ -6789,7 +6786,7 @@ bool loadSaveFeature(char *pFileData, UDWORD filesize)
FEATURE_SAVEHEADER *psHeader;
SAVE_FEATURE_V14 *psSaveFeature;
FEATURE *pFeature;
UDWORD count, i, statInc;
UDWORD count, i;
FEATURE_STATS *psStats = nullptr;
bool found;
UDWORD sizeOfSaveFeature;
Expand Down Expand Up @@ -6852,12 +6849,12 @@ bool loadSaveFeature(char *pFileData, UDWORD filesize)
//get the stats for this feature
found = false;

for (statInc = 0; statInc < numFeatureStats; statInc++)
for (FEATURE_STATS& stat : asFeatureStats)
{
psStats = asFeatureStats + statInc;
//loop until find the same name
if (psStats->id.compare(psSaveFeature->name) == 0)
if (stat.id.compare(psSaveFeature->name) == 0)
{
psStats = &stat;
found = true;
break;
}
Expand Down Expand Up @@ -6905,8 +6902,8 @@ static bool loadWzMapFeature(WzMap::Map &wzMap, std::unordered_map<UDWORD, UDWOR

for (auto &feature : *pFeatures)
{
auto psStats = std::find_if(asFeatureStats, asFeatureStats + numFeatureStats, [&](FEATURE_STATS &stat) { return stat.id.compare(feature.name.c_str()) == 0; });
if (psStats == asFeatureStats + numFeatureStats)
auto psStats = std::find_if(asFeatureStats.begin(), asFeatureStats.end(), [&feature](FEATURE_STATS& stat) { return stat.id.compare(feature.name.c_str()) == 0; });
if (psStats == asFeatureStats.end())
{
debug(LOG_ERROR, "Feature type \"%s\" unknown", feature.name.c_str());
continue; // ignore this
Expand All @@ -6927,7 +6924,7 @@ static bool loadWzMapFeature(WzMap::Map &wzMap, std::unordered_map<UDWORD, UDWOR
debug(LOG_ERROR, "Found duplicate hard-coded object ID in map data: %" PRIu32 "", feature.id.value());
}
}
pFeature = buildFeature(psStats, feature.position.x, feature.position.y, true, newID);
pFeature = buildFeature(&*psStats, feature.position.x, feature.position.y, true, newID);
if (!pFeature)
{
debug(LOG_ERROR, "Unable to create feature %s", feature.name.c_str());
Expand Down Expand Up @@ -6962,17 +6959,16 @@ bool loadSaveFeature2(const char *pFileName)
ini.beginGroup(list[i]);
WzString name = ini.string("name");
Position pos = ini.vector3i("position");
int statInc;
bool found = false;
FEATURE_STATS *psStats = nullptr;

//get the stats for this feature
for (statInc = 0; statInc < numFeatureStats; statInc++)
for (FEATURE_STATS& stat : asFeatureStats)
{
psStats = asFeatureStats + statInc;
//loop until find the same name
if (psStats->id.compare(name) == 0)
if (stat.id.compare(name) == 0)
{
psStats = &stat;
found = true;
break;
}
Expand Down Expand Up @@ -7702,7 +7698,7 @@ static bool writeMessageFile(const char *pFileName)
for (int player = 0; player < game.maxPlayers; player++)
{
ASSERT(player < MAX_PLAYERS, "player out of bounds: %d", player);
for (MESSAGE *psMessage = apsMessages[player]; psMessage != nullptr; psMessage = psMessage->psNext)
for (const MESSAGE *psMessage : apsMessages[player])
{
ini.beginGroup("message_" + WzString::number(numMessages++));
ini.setValue("id", numMessages - 1); // for future use
Expand All @@ -7712,16 +7708,12 @@ static bool writeMessageFile(const char *pFileName)
if (psMessage->type == MSG_PROXIMITY)
{
//get the matching proximity message
PROXIMITY_DISPLAY *psProx = nullptr;
for (psProx = apsProxDisp[player]; psProx != nullptr; psProx = psProx->psNext)
auto psProxIt = std::find_if(apsProxDisp[player].begin(), apsProxDisp[player].end(), [psMessage](PROXIMITY_DISPLAY* psProx)
{
//compare the pointers
if (psProx->psMessage == psMessage)
{
break;
}
}
ASSERT(psProx != nullptr, "Save message; proximity display not found for message");
return psProx->psMessage == psMessage; //compare the pointers
});
ASSERT(psProxIt != apsProxDisp[player].end(), "Save message; proximity display not found for message");
PROXIMITY_DISPLAY* psProx = *psProxIt;
if (psProx && psProx->type == POS_PROXDATA)
{
//message has viewdata so store the name
Expand Down
35 changes: 16 additions & 19 deletions src/hci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1270,12 +1270,12 @@ void intOpenDebugMenu(OBJECT_TYPE id)
editPosMode = IED_NOPOS;
break;
case OBJ_FEATURE:
for (unsigned i = 0; i < std::min<unsigned>(numFeatureStats, MAXFEATURES); ++i)
for (unsigned i = 0, end = std::min<unsigned>(asFeatureStats.size(), MAXFEATURES); i < end; ++i)
{
apsFeatureList[i] = asFeatureStats + i;
apsFeatureList[i] = &asFeatureStats.at(i);
ManManson marked this conversation as resolved.
Show resolved Hide resolved
}
ppsStatsList = (BASE_STATS **)apsFeatureList;
intAddDebugStatsForm(ppsStatsList, std::min<unsigned>(numFeatureStats, MAXFEATURES));
intAddDebugStatsForm(ppsStatsList, std::min<unsigned>(asFeatureStats.size(), MAXFEATURES));
intMode = INT_EDITSTAT;
editPosMode = IED_NOPOS;
break;
Expand All @@ -1300,7 +1300,6 @@ static void intProcessEditStats(UDWORD id)
debugMenuDroidDeliveryPoint.factoryInc = 0;
debugMenuDroidDeliveryPoint.player = selectedPlayer;
debugMenuDroidDeliveryPoint.selected = false;
debugMenuDroidDeliveryPoint.psNext = nullptr;
startDeliveryPosition(&debugMenuDroidDeliveryPoint);
}
else
Expand Down Expand Up @@ -2648,7 +2647,6 @@ void forceHidePowerBar(bool forceSetPowerBarUpState)
/* Add the Proximity message buttons */
bool intAddProximityButton(PROXIMITY_DISPLAY *psProxDisp, UDWORD inc)
{
PROXIMITY_DISPLAY *psProxDisp2;
UDWORD cnt;

if (selectedPlayer >= MAX_PLAYERS)
Expand All @@ -2666,9 +2664,13 @@ bool intAddProximityButton(PROXIMITY_DISPLAY *psProxDisp, UDWORD inc)
for (cnt = IDPROX_START; cnt < IDPROX_END; cnt++)
{
// go down the prox msgs and see if it's free.
for (psProxDisp2 = apsProxDisp[selectedPlayer]; psProxDisp2 && psProxDisp2->buttonID != cnt; psProxDisp2 = psProxDisp2->psNext) {}
const auto& proxDispList = apsProxDisp[selectedPlayer];
auto proxDispIt = std::find_if(proxDispList.begin(), proxDispList.end(), [cnt](PROXIMITY_DISPLAY* pd)
{
return pd->buttonID == cnt;
});

if (psProxDisp2 == nullptr) // value was unused.
if (proxDispIt == proxDispList.end()) // value was unused.
{
sBFormInit.id = cnt;
break;
Expand Down Expand Up @@ -2710,8 +2712,6 @@ void intRemoveProximityButton(PROXIMITY_DISPLAY *psProxDisp)
/*deals with the proximity message when clicked on*/
void processProximityButtons(UDWORD id)
{
PROXIMITY_DISPLAY *psProxDisp;

if (!doWeDrawProximitys())
{
return;
Expand All @@ -2720,20 +2720,17 @@ void processProximityButtons(UDWORD id)
if (selectedPlayer >= MAX_PLAYERS) { return; /* no-op */ }

//find which proximity display this relates to
psProxDisp = nullptr;
for (psProxDisp = apsProxDisp[selectedPlayer]; psProxDisp; psProxDisp = psProxDisp->psNext)
const auto& proxDispList = apsProxDisp[selectedPlayer];
auto proxDispIt = std::find_if(proxDispList.begin(), proxDispList.end(), [id](PROXIMITY_DISPLAY* psProxDisp)
{
if (psProxDisp->buttonID == id)
{
break;
}
}
if (psProxDisp)
return psProxDisp->buttonID == id;
});
if (proxDispIt != proxDispList.end())
{
//if not been read - display info
if (!psProxDisp->psMessage->read)
if (!(*proxDispIt)->psMessage->read)
{
displayProximityMessage(psProxDisp);
displayProximityMessage(*proxDispIt);
}
}
}
Expand Down
Loading
Loading