Skip to content

Commit 86a12e9

Browse files
committed
Revert "Fixes to UniqueID loading"
This reverts commit ad309ad.
1 parent 0e75bb8 commit 86a12e9

File tree

6 files changed

+41
-34
lines changed

6 files changed

+41
-34
lines changed

Source/Entities/MovableObject.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,10 @@ int MovableObject::Create(const MovableObject& reference) {
266266
m_NumberValueMap = reference.m_NumberValueMap;
267267
m_ObjectValueMap = reference.m_ObjectValueMap;
268268

269-
// If we're currently performing a game save or load we want to persist our existing IDs
269+
// If we have a -1 unique ID, then we're currently performing a game save or load
270+
// In this case, we actually want to persist our existing IDs
270271
if (g_MovableMan.ShouldPersistUniqueIDs()) {
271-
m_UniqueID = reference.m_UniqueID == 0 ? g_MovableMan.GetNextUniqueID() : reference.m_UniqueID;
272+
m_UniqueID = reference.m_UniqueID;
272273
m_AgeTimer = reference.m_AgeTimer;
273274
} else {
274275
// Otherwise we're copying from a preset normally and ought to create a new object
@@ -383,7 +384,11 @@ int MovableObject::ReadProperty(const std::string_view& propName, Reader& reader
383384
MatchProperty("SimUpdatesBetweenScriptedUpdates", { reader >> m_SimUpdatesBetweenScriptedUpdates; });
384385
MatchProperty("AddCustomValue", { ReadCustomValueProperty(reader); });
385386
MatchProperty("ForceIntoMasterLuaState", { reader >> m_ForceIntoMasterLuaState; });
386-
MatchProperty("SpecialBehaviour_SetUniqueID", { reader >> m_UniqueID; });
387+
MatchProperty("SpecialBehaviour_SetUniqueID", {
388+
long oldID = m_UniqueID;
389+
reader >> m_UniqueID;
390+
g_MovableMan.ReregisterObjectIfApplicable(this, oldID);
391+
});
387392

388393
EndPropertyList;
389394
}

Source/Entities/MovableObject.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,6 @@ namespace RTE {
979979
/// @param newRestThreshold New rest threshold value
980980
void SetRestThreshold(int newRestThreshold) { m_RestThreshold = newRestThreshold; }
981981

982-
/// Resets this MO's unique persistent ID. Unsafe, only for internal use!
983-
void ResetUniqueID() { m_UniqueID = 0; }
984-
985982
/// Returns this MO's unique persistent ID
986983
/// @return Returns this MO's unique persistent ID
987984
long GetUniqueID() const { return m_UniqueID; }

Source/Entities/Scene.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,12 +1167,12 @@ void Scene::SaveSceneObject(Writer& writer, const SceneObject* sceneObjectToSave
11671167
}
11681168

11691169
if (const MovableObject* movableObjectToSave = dynamic_cast<const MovableObject*>(sceneObjectToSave); movableObjectToSave && saveFullData) {
1170-
writer.NewPropertyWithValue("SpecialBehaviour_SetUniqueID", movableObjectToSave->GetUniqueID());
11711170
writer.NewPropertyWithValue("HUDVisible", movableObjectToSave->GetHUDVisible());
11721171
writer.NewPropertyWithValue("Velocity", movableObjectToSave->GetVel());
11731172
writer.NewPropertyWithValue("LifeTime", movableObjectToSave->GetLifetime());
11741173
writer.NewPropertyWithValue("Age", movableObjectToSave->GetAge());
11751174
writer.NewPropertyWithValue("PinStrength", movableObjectToSave->GetPinStrength());
1175+
writer.NewPropertyWithValue("SpecialBehaviour_SetUniqueID", movableObjectToSave->GetUniqueID());
11761176
}
11771177

11781178
if (const MOSprite* moSpriteToSave = dynamic_cast<const MOSprite*>(sceneObjectToSave)) {

Source/Managers/ActivityMan.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
128128
}
129129
}
130130

131-
writer->NewPropertyWithValue("MaxUniqueID", currentMaxID);
132-
writer->NewPropertyWithValue("CurrentSimTicks", g_TimerMan.GetSimTickCount());
133131
writer->NewPropertyWithValue("OriginalScenePresetName", scene->GetPresetName());
134132
writer->NewPropertyWithValue("PlaceObjectsIfSceneIsRestarted", g_SceneMan.GetPlaceObjectsOnLoad());
135133
writer->NewPropertyWithValue("PlaceUnitsIfSceneIsRestarted", g_SceneMan.GetPlaceUnitsOnLoad());
136134
writer->NewPropertyWithValue("Scene", modifiableScene.get());
135+
writer->NewPropertyWithValue("MaxUniqueID", currentMaxID);
136+
writer->NewPropertyWithValue("CurrentSimTicks", g_TimerMan.GetSimTickCount());
137137

138138
auto saveWriterData = [](Writer* writerToSave) {
139139
writerToSave->EndWrite();
@@ -170,22 +170,14 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
170170
std::unique_ptr<Scene> scene(std::make_unique<Scene>());
171171
std::unique_ptr<GAScripted> activity(std::make_unique<GAScripted>());
172172

173-
g_MovableMan.ClearKnownObjects();
174-
175-
g_MovableMan.SetMaxUniqueID(std::numeric_limits<long>::min());
176-
177-
long maxUniqueID = 1;
173+
long maxUniqueID = 0;
178174
long long simTimeTicks = 0;
179175
std::string originalScenePresetName = fileName;
180176
bool placeObjectsIfSceneIsRestarted = true;
181177
bool placeUnitsIfSceneIsRestarted = true;
182178
while (reader.NextProperty()) {
183179
std::string propName = reader.ReadPropName();
184-
if (propName == "MaxUniqueID") {
185-
reader >> maxUniqueID;
186-
} else if (propName == "CurrentSimTicks") {
187-
reader >> simTimeTicks;
188-
} else if (propName == "Activity") {
180+
if (propName == "Activity") {
189181
reader >> activity.get();
190182
} else if (propName == "OriginalScenePresetName") {
191183
reader >> originalScenePresetName;
@@ -195,11 +187,14 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
195187
reader >> placeUnitsIfSceneIsRestarted;
196188
} else if (propName == "Scene") {
197189
reader >> scene.get();
190+
} else if (propName == "MaxUniqueID") {
191+
reader >> maxUniqueID;
192+
g_MovableMan.SetShouldPersistUniqueIDs(true);
193+
} else if (propName == "CurrentSimTicks") {
194+
reader >> simTimeTicks;
198195
}
199196
}
200197

201-
g_MovableMan.SetShouldPersistUniqueIDs(true);
202-
203198
// SetSceneToLoad() doesn't Clone(), but when the Activity starts, it will eventually call LoadScene(), which does a Clone() of scene internally.
204199
g_SceneMan.SetSceneToLoad(scene.get(), true, true);
205200
// Saved Scenes get their presetname set to their filename to ensure they're separate from the preset Scene they're based off of.
@@ -208,18 +203,18 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
208203
// For starting Activity, we need to directly clone the Activity we want to start.
209204
StartActivity(dynamic_cast<GAScripted*>(activity->Clone()));
210205

211-
g_MovableMan.SetShouldPersistUniqueIDs(false);
212-
213206
// Set the max unique ID to our loaded maximum so we don't stomp over any existing ones
214-
g_MovableMan.SetMaxUniqueID(maxUniqueID);
207+
if (maxUniqueID != 0) {
208+
g_MovableMan.SetMaxUniqueID(maxUniqueID);
209+
}
215210

211+
g_MovableMan.SetShouldPersistUniqueIDs(false);
216212
//g_TimerMan.SetSimTickCount(simTimeTicks); // Don't do for now... causes issues with negative times in places
217213

218214
// When this method exits, our Scene object will be destroyed, which will cause problems if you try to restart it. To avoid this, set the Scene to load to the preset object with the same name.
219215
g_SceneMan.SetSceneToLoad(originalScenePresetName, placeObjectsIfSceneIsRestarted, placeUnitsIfSceneIsRestarted);
220216

221217
g_ConsoleMan.PrintString("SYSTEM: Game \"" + fileName + "\" loaded!");
222-
223218
return true;
224219
}
225220

Source/Managers/MovableMan.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ MOID MovableMan::GetMOIDPixel(int pixelX, int pixelY, const std::vector<int>& mo
166166
}
167167

168168
void MovableMan::RegisterObject(MovableObject* mo) {
169-
if (!mo || mo->GetUniqueID() <= 0) {
169+
if (!mo) {
170170
return;
171171
}
172172

@@ -183,12 +183,15 @@ void MovableMan::UnregisterObject(MovableObject* mo) {
183183
m_KnownObjects.erase(mo->GetUniqueID());
184184
}
185185

186-
void RTE::MovableMan::ClearKnownObjects() {
187-
for (auto& pair : m_KnownObjects) {
188-
pair.second->ResetUniqueID();
186+
void MovableMan::ReregisterObjectIfApplicable(MovableObject* mo, long oldUniqueId) {
187+
if (mo == nullptr || m_KnownObjects.find(oldUniqueId) == m_KnownObjects.end()) {
188+
// Old ID was never registered, don't need to do anything
189+
return;
189190
}
190191

191-
m_KnownObjects.clear();
192+
std::lock_guard<std::mutex> guard(m_ObjectRegisteredMutex);
193+
m_KnownObjects[oldUniqueId] = nullptr;
194+
m_KnownObjects[mo->GetUniqueID()] = mo;
192195
}
193196

194197
const std::vector<MovableObject*>* MovableMan::GetMOsInBox(const Box& box, int ignoreTeam, bool getsHitByMOsOnly) const {

Source/Managers/MovableMan.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,19 @@ namespace RTE {
482482
/// @param mo MO to remove.
483483
void UnregisterObject(MovableObject* mo);
484484

485+
/// Reregisters an object in a global Map collection to handle if it's unique ID changes
486+
/// @param mo MO to reregister if necessary.
487+
/// @param oldUniqueID The MO's old UniqueID.
488+
void ReregisterObjectIfApplicable(MovableObject* mo, long oldUniqueId);
489+
485490
/// Returns the next unique id for MO's and increments unique ID counter
486491
/// @return Returns the next unique id.
487-
long GetNextUniqueID() { return ++m_UniqueIDCounter; }
492+
long GetNextUniqueID() {
493+
if (m_ShouldPersistUniqueIDs) {
494+
return 0;
495+
}
496+
return ++m_UniqueIDCounter;
497+
}
488498

489499
/// Returns the max unique id for MO's
490500
/// @return Returns the current max unique id.
@@ -502,9 +512,6 @@ namespace RTE {
502512
/// @param newValue Whether we should be persisting uniqueIDs.
503513
void SetShouldPersistUniqueIDs(bool newValue) { m_ShouldPersistUniqueIDs = newValue; }
504514

505-
/// Clears all known objects. Should only be used for internal usage, it's unsafe!
506-
void ClearKnownObjects();
507-
508515
/// Uses a global lookup map to find an object by it's unique id.
509516
/// @param id Unique Id to look for.
510517
/// @return Object found or 0 if not found any.

0 commit comments

Comments
 (0)