Skip to content

Commit 01f1df5

Browse files
committed
Revert "UniqueID and Age are now persisted through game save/load"
This reverts commit b0b026f.
1 parent 896e8db commit 01f1df5

File tree

12 files changed

+19
-114
lines changed

12 files changed

+19
-114
lines changed

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
4444

4545
- The Signal Hunt activity no longer has a preview image, as it was not formatted correctly and spoiled the interior structure of the cave.
4646

47-
- `MovableObject`'s `UniqueID` and `Age` fields are now persisted through game save/load.
48-
4947
</details>
5048

5149
<details><summary><b>Fixed</b></summary>

Source/Entities/MovableObject.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using namespace RTE;
2323

2424
AbstractClassInfo(MovableObject, SceneObject);
2525

26+
std::atomic<long> MovableObject::m_UniqueIDCounter = 1;
2627
std::string MovableObject::ms_EmptyString = "";
2728

2829
MovableObject::MovableObject() {
@@ -140,17 +141,14 @@ int MovableObject::Create() {
140141
if (SceneObject::Create() < 0)
141142
return -1;
142143

144+
m_AgeTimer.Reset();
143145
m_RestTimer.Reset();
144146

145147
// If the stop time hasn't been assigned, just make the same as the life time.
146148
if (m_EffectStopTime <= 0)
147149
m_EffectStopTime = m_Lifetime;
148150

149-
if (m_UniqueID == 0)
150-
{
151-
m_UniqueID = g_MovableMan.GetNextUniqueID();
152-
m_AgeTimer.Reset();
153-
}
151+
m_UniqueID = MovableObject::GetNextUniqueID();
154152

155153
m_MOIDHit = g_NoMOID;
156154
m_TerrainMatHit = g_MaterialAir;
@@ -179,7 +177,7 @@ int MovableObject::Create(const float mass,
179177
m_HitsMOs = hitMOs;
180178
m_GetsHitByMOs = getHitByMOs;
181179

182-
m_UniqueID = g_MovableMan.GetNextUniqueID();
180+
m_UniqueID = MovableObject::GetNextUniqueID();
183181

184182
m_MOIDHit = g_NoMOID;
185183
m_TerrainMatHit = g_MaterialAir;
@@ -205,6 +203,9 @@ int MovableObject::Create(const MovableObject& reference) {
205203
m_RestThreshold = reference.m_RestThreshold;
206204
// m_Force = reference.m_Force;
207205
// m_ImpulseForce = reference.m_ImpulseForce;
206+
// Should reset age instead??
207+
// m_AgeTimer = reference.m_AgeTimer;
208+
m_AgeTimer.Reset();
208209
m_RestTimer.Reset();
209210
m_Lifetime = reference.m_Lifetime;
210211
m_Sharpness = reference.m_Sharpness;
@@ -265,17 +266,7 @@ int MovableObject::Create(const MovableObject& reference) {
265266
m_NumberValueMap = reference.m_NumberValueMap;
266267
m_ObjectValueMap = reference.m_ObjectValueMap;
267268

268-
// If we have a -1 unique ID, then we're currently performing a game save or load
269-
// In this case, we actually want to persist our existing IDs
270-
if (g_MovableMan.ShouldPersistUniqueIDs()) {
271-
m_UniqueID = reference.m_UniqueID;
272-
m_AgeTimer = reference.m_AgeTimer;
273-
} else {
274-
// Otherwise we're copying from a preset normally and ought to create a new object
275-
m_UniqueID = g_MovableMan.GetNextUniqueID();
276-
m_AgeTimer.Reset();
277-
}
278-
269+
m_UniqueID = MovableObject::GetNextUniqueID();
279270
g_MovableMan.RegisterObject(this);
280271

281272
return 0;
@@ -383,11 +374,6 @@ int MovableObject::ReadProperty(const std::string_view& propName, Reader& reader
383374
MatchProperty("SimUpdatesBetweenScriptedUpdates", { reader >> m_SimUpdatesBetweenScriptedUpdates; });
384375
MatchProperty("AddCustomValue", { ReadCustomValueProperty(reader); });
385376
MatchProperty("ForceIntoMasterLuaState", { reader >> m_ForceIntoMasterLuaState; });
386-
MatchProperty("SpecialBehaviour_SetUniqueID", {
387-
long oldID = m_UniqueID;
388-
reader >> m_UniqueID;
389-
g_MovableMan.ReregisterObjectIfApplicable(this, oldID);
390-
});
391377

392378
EndPropertyList;
393379
}

Source/Entities/MovableObject.h

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

982+
/// Returns the next unique id for MO's and increments unique ID counter
983+
/// @return Returns the next unique id.
984+
static long GetNextUniqueID() { return ++m_UniqueIDCounter; }
985+
982986
/// Returns this MO's unique persistent ID
983987
/// @return Returns this MO's unique persistent ID
984988
long GetUniqueID() const { return m_UniqueID; }
@@ -1135,7 +1139,8 @@ namespace RTE {
11351139
/// @param A MovableObject object which is passed in by reference.
11361140
// Member variables
11371141
static Entity::ClassInfo m_sClass;
1138-
1142+
// Global counter with unique ID's
1143+
static std::atomic<long> m_UniqueIDCounter;
11391144
// The type of MO this is, either Actor, Item, or Particle
11401145
int m_MOType;
11411146
float m_Mass; // In metric kilograms (kg).

Source/Entities/Scene.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,6 @@ void Scene::SaveSceneObject(Writer& writer, const SceneObject* sceneObjectToSave
11721172
writer.NewPropertyWithValue("LifeTime", movableObjectToSave->GetLifetime());
11731173
writer.NewPropertyWithValue("Age", movableObjectToSave->GetAge());
11741174
writer.NewPropertyWithValue("PinStrength", movableObjectToSave->GetPinStrength());
1175-
writer.NewPropertyWithValue("SpecialBehaviour_SetUniqueID", movableObjectToSave->GetUniqueID());
11761175
}
11771176

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

Source/GUI/GUIReader.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,6 @@ GUIReader& GUIReader::operator>>(unsigned long& var) {
359359
return *this;
360360
}
361361

362-
GUIReader& GUIReader::operator>>(long long& var) {
363-
DiscardEmptySpace();
364-
*m_Stream >> var;
365-
return *this;
366-
}
367-
368362
// Yeah, this is dumb - read as double and cast.
369363
// This is because, for whatever fucking reason, iostream can save out floats at a precision that it's then unable to read...
370364
GUIReader& GUIReader::operator>>(float& var) {

Source/GUI/GUIReader.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ namespace RTE {
9292
GUIReader& operator>>(unsigned int& var);
9393
GUIReader& operator>>(long& var);
9494
GUIReader& operator>>(unsigned long& var);
95-
GUIReader& operator>>(long long& var);
9695
GUIReader& operator>>(float& var);
9796
GUIReader& operator>>(double& var);
9897
GUIReader& operator>>(std::string& var);

Source/Main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ void PollSDLEvents() {
245245
void RunMenuLoop() {
246246
g_UInputMan.DisableKeys(false);
247247
g_UInputMan.TrapMousePos(false);
248-
g_TimerMan.PauseSim(true);
249248

250249
while (!System::IsSetToQuit()) {
251250
g_WindowMan.ClearRenderer();

Source/Managers/ActivityMan.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
9393
return false;
9494
}
9595

96-
long currentMaxID = g_MovableMan.GetMaxUniqueID();
97-
g_MovableMan.SetShouldPersistUniqueIDs(true);
98-
9996
// We need a copy of our scene, because we have to do some fixup to remove PLACEONLOAD items and only keep the current MovableMan state.
10097
std::unique_ptr<Scene> modifiableScene(dynamic_cast<Scene*>(scene->Clone()));
10198

@@ -124,8 +121,6 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
124121
}
125122
}
126123

127-
writer->NewPropertyWithValue("MaxUniqueID", currentMaxID);
128-
writer->NewPropertyWithValue("CurrentSimTicks", g_TimerMan.GetSimTickCount());
129124
writer->NewPropertyWithValue("OriginalScenePresetName", scene->GetPresetName());
130125
writer->NewPropertyWithValue("PlaceObjectsIfSceneIsRestarted", g_SceneMan.GetPlaceObjectsOnLoad());
131126
writer->NewPropertyWithValue("PlaceUnitsIfSceneIsRestarted", g_SceneMan.GetPlaceUnitsOnLoad());
@@ -142,8 +137,6 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
142137
// We didn't transfer ownership, so we must be very careful that sceneAltered's deletion doesn't touch the stuff we got from MovableMan.
143138
modifiableScene->ClearPlacedObjectSet(Scene::PlacedObjectSets::PLACEONLOAD, false);
144139

145-
g_MovableMan.SetShouldPersistUniqueIDs(false);
146-
147140
g_ConsoleMan.PrintString("SYSTEM: Game saved to \"" + fileName + "\"!");
148141
return true;
149142
}
@@ -163,20 +156,13 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
163156
std::unique_ptr<Scene> scene(std::make_unique<Scene>());
164157
std::unique_ptr<GAScripted> activity(std::make_unique<GAScripted>());
165158

166-
long maxUniqueID = 0;
167-
long long simTimeTicks = 0;
168159
std::string originalScenePresetName = fileName;
169160
bool placeObjectsIfSceneIsRestarted = true;
170161
bool placeUnitsIfSceneIsRestarted = true;
171162
while (reader.NextProperty()) {
172163
std::string propName = reader.ReadPropName();
173164
if (propName == "Activity") {
174165
reader >> activity.get();
175-
} else if (propName == "MaxUniqueID") {
176-
reader >> maxUniqueID;
177-
g_MovableMan.SetShouldPersistUniqueIDs(true);
178-
} else if (propName == "CurrentSimTicks") {
179-
reader >> simTimeTicks;
180166
} else if (propName == "OriginalScenePresetName") {
181167
reader >> originalScenePresetName;
182168
} else if (propName == "PlaceObjectsIfSceneIsRestarted") {
@@ -195,15 +181,6 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
195181
scene->SetPresetName(originalScenePresetName);
196182
// For starting Activity, we need to directly clone the Activity we want to start.
197183
StartActivity(dynamic_cast<GAScripted*>(activity->Clone()));
198-
199-
// Set the max unique ID to our loaded maximum so we don't stomp over any existing ones
200-
if (maxUniqueID != 0) {
201-
g_MovableMan.SetMaxUniqueID(maxUniqueID);
202-
}
203-
204-
g_MovableMan.SetShouldPersistUniqueIDs(false);
205-
g_TimerMan.SetSimTickCount(simTimeTicks);
206-
207184
// 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.
208185
g_SceneMan.SetSceneToLoad(originalScenePresetName, placeObjectsIfSceneIsRestarted, placeUnitsIfSceneIsRestarted);
209186

Source/Managers/MovableMan.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ struct MOXPosComparison {
4343

4444
MovableMan::MovableMan() {
4545
Clear();
46-
m_UniqueIDCounter = 1;
4746
}
4847

4948
MovableMan::~MovableMan() {
@@ -76,7 +75,6 @@ void MovableMan::Clear() {
7675
m_MaxDroppedItems = 100;
7776
m_SettlingEnabled = true;
7877
m_MOSubtractionEnabled = true;
79-
m_ShouldPersistUniqueIDs = false;
8078
}
8179

8280
int MovableMan::Initialize() {
@@ -183,17 +181,6 @@ void MovableMan::UnregisterObject(MovableObject* mo) {
183181
m_KnownObjects.erase(mo->GetUniqueID());
184182
}
185183

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;
190-
}
191-
192-
std::lock_guard<std::mutex> guard(m_ObjectRegisteredMutex);
193-
m_KnownObjects[oldUniqueId] = nullptr;
194-
m_KnownObjects[mo->GetUniqueID()] = mo;
195-
}
196-
197184
const std::vector<MovableObject*>* MovableMan::GetMOsInBox(const Box& box, int ignoreTeam, bool getsHitByMOsOnly) const {
198185
std::vector<MovableObject*>* vectorForLua = new std::vector<MovableObject*>();
199186
*vectorForLua = std::move(g_SceneMan.GetMOIDGrid().GetMOsInBox(box, ignoreTeam, getsHitByMOsOnly));

Source/Managers/MovableMan.h

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -482,40 +482,14 @@ 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-
490-
/// Returns the next unique id for MO's and increments unique ID counter
491-
/// @return Returns the next unique id.
492-
long GetNextUniqueID() { if (m_ShouldPersistUniqueIDs) { return 0; } return ++m_UniqueIDCounter; }
493-
494-
/// Returns the max unique id for MO's
495-
/// @return Returns the current max unique id.
496-
long GetMaxUniqueID() { return m_UniqueIDCounter; }
497-
498-
/// Set the max unique id for MO's. This is used so we can consistently save/load unique IDs so they're consistent within a game session
499-
/// @param id Unique Id to set.
500-
void SetMaxUniqueID(long newMaxID) { m_UniqueIDCounter = newMaxID; }
501-
502-
/// Returns whether we should be persisting uniqueIDs for save/load
503-
/// @return Returns whether we are persisting uniqueIDs.
504-
bool ShouldPersistUniqueIDs() { return m_ShouldPersistUniqueIDs; }
505-
506-
/// Sets whether we should be persisting uniqueIDs for save/load
507-
/// @param newValue Whether we should be persisting uniqueIDs.
508-
void SetShouldPersistUniqueIDs(bool newValue) { m_ShouldPersistUniqueIDs = newValue; }
509-
510485
/// Uses a global lookup map to find an object by it's unique id.
511486
/// @param id Unique Id to look for.
512487
/// @return Object found or 0 if not found any.
513-
MovableObject* FindObjectByUniqueID(long id) {
514-
if (m_KnownObjects.count(id) > 0) {
488+
MovableObject* FindObjectByUniqueID(long int id) {
489+
if (m_KnownObjects.count(id) > 0)
515490
return m_KnownObjects[id];
516-
} else {
491+
else
517492
return 0;
518-
}
519493
}
520494

521495
/// Returns the size of the object registry collection
@@ -577,10 +551,6 @@ namespace RTE {
577551
std::deque<Actor*> m_Actors;
578552
// A map to give a unique contiguous identifier per-actor. This is re-created per frame.
579553
std::unordered_map<const Actor*, int> m_ContiguousActorIDs;
580-
// Global counter with unique ID's
581-
std::atomic<long> m_UniqueIDCounter;
582-
// Whether or not UniqueIDs should be persisted (for save/load)
583-
bool m_ShouldPersistUniqueIDs;
584554
// List of items that are pickup-able by actors
585555
std::deque<MovableObject*> m_Items;
586556
// List of free, dead particles flying around
@@ -651,8 +621,8 @@ namespace RTE {
651621

652622
unsigned int m_SimUpdateFrameNumber;
653623

654-
// Global map which stores all objects so they could be found by their unique ID
655-
std::map<long, MovableObject*> m_KnownObjects;
624+
// Global map which stores all objects so they could be foud by their unique ID
625+
std::map<long int, MovableObject*> m_KnownObjects;
656626

657627
/// Private member variable and method declarations
658628
private:

0 commit comments

Comments
 (0)