Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gamelogic): restore the overridden template's weapons from a save

Olcay Seygan195652aa5231
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7bbcd0f48..d7c3fc297 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -163,6 +163,11 @@ map. Ctrl+A is still everything at once.
- **Loading a save remembers what your guards were guarding.** The guard order was written to the
file one slot too far and overwrote the position it was guarding, so units restored from a save
could come back watching the wrong place.
+- **Units come back from a save with the weapons the map gave them.** A map can rewrite what a
+ unit is — different damage, different range, different reload — and many of them do. Saving wrote
+ down the unit's name, and loading looked that name up and found the original, un-rewritten version
+ underneath the map's changes. Reload a save on such a map and your army quietly went back to
+ stock weapons. It now finds the version the map actually gave you.
- **Restarting a skirmish restarts the same skirmish.** Hitting restart threw away the number the
match was built from and started the next attempt on a different one, so the replay it was writing
no longer matched the game being played and would not play back. Restart now hands the game back
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/ThingTemplate.h b/GeneralsMD/Code/GameEngine/Include/Common/ThingTemplate.h
index 5cd8a1aa4..b7f613980 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/ThingTemplate.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/ThingTemplate.h
@@ -772,6 +772,20 @@ private:
// Inlining
//-----------------------------------------------------------------------------
+//
+// A map's INI can override an object's template, and ThingFactory::newOverride hangs the override
+// off the end of the base template's chain rather than replacing it - both keep the same name, and
+// TheThingFactory->findTemplate always hands back the head of the chain, the un-overridden one.
+// Object's constructor has always ended its template with this ("Force the thing template to use
+// the most overridden version of itself - jkmcd"), so anything that looks a template up by name and
+// then compares or indexes it against a live object has to do the same or it is talking about a
+// different template that happens to share a name.
+//
+inline const ThingTemplate *finalOverrideOf( const ThingTemplate *tmpl )
+{
+ return tmpl ? (const ThingTemplate *)tmpl->getFinalOverride() : NULL;
+}
+
//-----------------------------------------------------------------------------
// Externals
//-----------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
index 2dadb8256..9a18acf80 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
@@ -233,7 +233,7 @@ Object::Object( const ThingTemplate *tt, const ObjectStatusMaskType &objectStatu
// Force the thing template to use the most overridden version of itself - jkmcd
// Note that after this, the object will be using m_template, which forces the usage of the
// most overridden version of tt, so this is okay.
- tt = (const ThingTemplate *) tt->getFinalOverride();
+ tt = finalOverrideOf( tt );
Int i, modIdx;
AsciiString modName;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/WeaponSet.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/WeaponSet.cpp
index f3bc2f117..1b9b44869 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/WeaponSet.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/WeaponSet.cpp
@@ -236,6 +236,11 @@ void WeaponSet::xfer( Xfer *xfer )
if (tt == NULL)
throw INI_INVALID_DATA;
+ // The name was written from a live object, whose template Object's constructor took to
+ // the end of its override chain; findTemplate hands back the head of that chain. Without
+ // this the weapon set restored from a save is the map override's predecessor.
+ tt = finalOverrideOf( tt );
+
m_curWeaponTemplateSet = tt->findWeaponTemplateSet(wsFlags);
if (m_curWeaponTemplateSet == NULL)
throw INI_INVALID_DATA;
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index e2e35699c..df883bbb4 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -39,6 +39,7 @@
#include "Common/RandomValue.h"
#include "GameLogic/LogicRandomValue.h"
#include "GameClient/ClientRandomValue.h"
+#include "Common/ThingTemplate.h"
#include "Common/AudioRandomValue.h"
#include "GameNetwork/CrcAgreement.h"
#include "GameNetwork/NetworkUtil.h"
@@ -3795,3 +3796,42 @@ TEST(a_games_seed_sets_the_client_and_audio_streams_as_well_as_the_logic_one)
CHECK( audioMoved );
CHECK( logicMoved );
}
+
+/* A map's INI overrides an object template by hanging a copy off the end of the original's chain,
+ under the same name. findTemplate hands back the head of that chain; Object's constructor walks
+ to the end. WeaponSet::xfer wrote a live object's template name into the save and looked it up
+ again on load without walking, so a unit on an overridden map came back holding the weapon set of
+ the template the override replaced. Both sites go through finalOverrideOf now.
+
+ The chain lives entirely in Overridable, and finalOverrideOf touches nothing else, so the test
+ builds it out of plain Overridables: a ThingTemplate cannot be constructed here, its constructor
+ reads TheGlobalData. ThingTemplate's only base is Overridable, non-virtually, so the cast is an
+ identity on the pointer. */
+TEST(a_template_looked_up_by_name_is_taken_to_the_end_of_its_override_chain)
+{
+ Overridable *base = newInstance( Overridable );
+ Overridable *override = newInstance( Overridable );
+ Overridable *later = newInstance( Overridable );
+
+ #define AS_TEMPLATE(p) ((const ThingTemplate *)(const Overridable *)(p))
+
+ // nothing overrides it yet, so the answer is the template itself
+ CHECK( finalOverrideOf( AS_TEMPLATE(base) ) == AS_TEMPLATE(base) );
+
+ // one override, then a second hung off the first - which is how newOverride builds them
+ base->setNextOverride( override );
+ CHECK( finalOverrideOf( AS_TEMPLATE(base) ) == AS_TEMPLATE(override) );
+
+ override->setNextOverride( later );
+ CHECK( finalOverrideOf( AS_TEMPLATE(base) ) == AS_TEMPLATE(later) );
+ CHECK( finalOverrideOf( AS_TEMPLATE(override) ) == AS_TEMPLATE(later) );
+ CHECK( finalOverrideOf( AS_TEMPLATE(later) ) == AS_TEMPLATE(later) );
+
+ // and a name that matched nothing stays nothing rather than being dereferenced
+ CHECK( finalOverrideOf( NULL ) == NULL );
+
+ #undef AS_TEMPLATE
+
+ // one delete, not three: ~Overridable deletes whatever the chain still points at
+ base->deleteInstance();
+}