Skip to content

Commit

Permalink
Switch apsMessages and asFeatureStats to std::list and `std::ve…
Browse files Browse the repository at this point in the history
…ctor`, respectively

Signed-off-by: Pavel Solodovnikov <[email protected]>
  • Loading branch information
ManManson committed Jan 7, 2024
1 parent f0432a7 commit 5d7cc89
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 186 deletions.
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
23 changes: 11 additions & 12 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6789,7 +6789,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 +6852,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 +6905,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 +6927,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 +6962,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 +7701,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 Down
6 changes: 3 additions & 3 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);
}
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 Down
22 changes: 8 additions & 14 deletions src/intelmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ void W_INTELLIGENCEOVERLAY_FORM::initialize(bool _playCurrent, bool animate)
/* Add the message buttons */

//add each button
for (MESSAGE *psMessage = apsMessages[selectedPlayer]; psMessage != nullptr; psMessage = psMessage->psNext)
for (MESSAGE *psMessage : apsMessages[selectedPlayer])
{
/*if (psMessage->type == MSG_TUTORIAL)
{
Expand Down Expand Up @@ -901,22 +901,20 @@ static void StartMessageSequences(MESSAGE *psMessage, bool Start)

static void intCleanUpIntelMap()
{
MESSAGE *psMessage, *psNext;
bool removedAMessage = false;

if (selectedPlayer < MAX_PLAYERS)
{
//remove any research messages that have been read
for (psMessage = apsMessages[selectedPlayer]; psMessage != nullptr; psMessage =
psNext)
mutating_list_iterate(apsMessages[selectedPlayer], [&removedAMessage](MESSAGE* psMessage)
{
psNext = psMessage->psNext;
if (psMessage->type == MSG_RESEARCH && psMessage->read)
{
removeMessage(psMessage, selectedPlayer);
removedAMessage = true;
}
}
return IterationResult::CONTINUE_ITERATION;
});
}
if (removedAMessage)
{
Expand Down Expand Up @@ -1205,20 +1203,16 @@ void addVideoText(SEQ_DISPLAY *psSeqDisplay, UDWORD sequence)
/*sets psCurrentMsg for the Intelligence screen*/
void setCurrentMsg()
{
MESSAGE *psMsg, *psLastMsg;

ASSERT_OR_RETURN(, selectedPlayer < MAX_PLAYERS, "Unsupported selectedPlayer: %" PRIu32 "", selectedPlayer);

psLastMsg = nullptr;
for (psMsg = apsMessages[selectedPlayer]; psMsg != nullptr; psMsg =
psMsg->psNext)
for (auto it = apsMessages[selectedPlayer].rbegin(), end = apsMessages[selectedPlayer].rend(); it != end; ++it)
{
if (psMsg->type != MSG_PROXIMITY)
if ((*it)->type != MSG_PROXIMITY)
{
psLastMsg = psMsg;
psCurrentMsg = *it;
return;
}
}
psCurrentMsg = psLastMsg;
}

/*sets which states need to be paused when the intelligence screen is up*/
Expand Down
Loading

0 comments on commit 5d7cc89

Please sign in to comment.