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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
perf(gameengine): index waypoints and trigger areas by name
Olcay Seyganc29bb0871d70
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3f201f23b..6028a2c14 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -887,6 +887,12 @@ scripts, and every folder inside those lists, until the name turned up. The compbehaviour is written in exactly those scripts and they call each other constantly. The names arefixed the moment a map is loaded, so they are now looked up directly instead of hunted for.+**And the places on the map, looked up by name.** A map is dotted with named spots to walk to and+named regions to watch — "move here", "is anyone standing in this area yet". Every time a script+mentioned one, the game read down the entire list of them until the name turned up, and the area+question is asked by every script that watches a region, on every pass. Both lists are fixed the+moment a map loads, so both are now looked up directly.+**And every burning building asking where its smoke is.** A damaged structure, a laser beam, abeacon, a shockwave — each of them holds on to an effect by number and asks the game to hand it back,every frame, for as long as it lives. Asking meant reading down the list of every effect alive in thediff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/PolygonTrigger.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/PolygonTrigger.hindex a7169362e..7522daa40 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/PolygonTrigger.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/PolygonTrigger.h@@ -90,6 +90,10 @@ protected:static PolygonTrigger* ThePolygonTriggerListPtr;static Int s_currentID; ///< Current id for new triggers.+ // Bumped by everything that can change the list or a trigger's name, so that anyone keeping an+ // index of the list by name can tell that theirs went stale. Starts at 1 so that a cache which+ // has never been built (revision 0) never looks current.+ static UnsignedInt s_listRevision;protected:void reallocate(void);@@ -115,12 +119,13 @@ public:public:static void addPolygonTrigger(PolygonTrigger *pTrigger);static void removePolygonTrigger(PolygonTrigger *pTrigger);- void setNextPoly(PolygonTrigger *nextPoly) {m_nextPolygonTrigger = nextPoly;} ///< Link the next map object.+ static UnsignedInt getListRevision(void) {return s_listRevision;}+ void setNextPoly(PolygonTrigger *nextPoly) {m_nextPolygonTrigger = nextPoly; ++s_listRevision;} ///< Link the next map object.void addPoint(const ICoord3D &point);void setPoint(const ICoord3D &point, Int ndx);void insertPoint(const ICoord3D &point, Int ndx);void deletePoint(Int ndx);- void setTriggerName(AsciiString name) {m_triggerName = name;};+ void setTriggerName(AsciiString name) {m_triggerName = name; ++s_listRevision;};void setLayerName(AsciiString name) {m_layerName = name;};AsciiString getLayerName(void) const {return m_layerName;}diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/TerrainLogic.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/TerrainLogic.hindex 98150831a..84259eba9 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/TerrainLogic.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/TerrainLogic.h@@ -330,10 +330,16 @@ protected:Bool parseWaypointData(DataChunkInput &file, DataChunkInfo *info, void *userData);/// Add a waypoint to the list.void addWaypoint(MapObject *pMapObj);+ /// Put a built waypoint at the head of the list.+ void linkWaypoint(Waypoint *pWay);/// Add a directed link between waypoints.void addWaypointLink(Int id1, Int id2);/// Deletes all waypoints.void deleteWaypoints(void);+ /// Index every waypoint by name and by id.+ void buildWaypointIndex(void);+ /// Index every polygon trigger by name.+ void buildTriggerAreaIndex(void);/// Deletes all bridges.void deleteBridges(void);@@ -350,6 +356,21 @@ protected:Waypoint *m_waypointListHead;Bridge *m_bridgeListHead;+ // Finding a waypoint or a trigger area by name used to walk the whole list on every call, and a+ // map's scripts do that many times per evaluation. Both lists are fixed for the life of a map,+ // so each is indexed once and the index is dropped whenever the list underneath it changes.+ typedef std::hash_map< AsciiString, Waypoint *, rts::hash<AsciiString>, rts::equal_to<AsciiString> > WaypointNameMap;+ typedef std::hash_map< UnsignedInt, Waypoint *, rts::hash<UnsignedInt>, rts::equal_to<UnsignedInt> > WaypointIDMap;+ typedef std::hash_map< AsciiString, PolygonTrigger *, rts::hash<AsciiString>, rts::equal_to<AsciiString> > TriggerAreaNameMap;++ WaypointNameMap m_waypointsByName;+ WaypointIDMap m_waypointsByID;+ Bool m_waypointIndexValid;++ TriggerAreaNameMap m_triggerAreasByName;+ /// the PolygonTrigger list revision m_triggerAreasByName was built from; 0 means never built+ UnsignedInt m_triggerAreaIndexRevision;+Bool m_bridgeDamageStatesChanged;AsciiString m_filenameString; ///< filename for terrain datadiff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Map/PolygonTrigger.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Map/PolygonTrigger.cppindex c7238f091..2ecaacbfe 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Map/PolygonTrigger.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Map/PolygonTrigger.cpp@@ -38,6 +38,7 @@/* ********* PolygonTrigger class ****************************/PolygonTrigger *PolygonTrigger::ThePolygonTriggerListPtr = NULL;Int PolygonTrigger::s_currentID = 1;+UnsignedInt PolygonTrigger::s_listRevision = 1;/**PolygonTrigger - Constructor.*/@@ -301,6 +302,7 @@ void PolygonTrigger::addPolygonTrigger(PolygonTrigger *pTrigger)}pTrigger->m_nextPolygonTrigger = ThePolygonTriggerListPtr;ThePolygonTriggerListPtr = pTrigger;+ ++s_listRevision;}/**@@ -327,6 +329,7 @@ void PolygonTrigger::removePolygonTrigger(PolygonTrigger *pTrigger)}}pTrigger->m_nextPolygonTrigger = NULL;+ ++s_listRevision;}/**@@ -337,6 +340,7 @@ void PolygonTrigger::deleteTriggers(void)PolygonTrigger *pList = ThePolygonTriggerListPtr;ThePolygonTriggerListPtr = NULL;s_currentID = 1;+ ++s_listRevision;pList->deleteInstance();}diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Map/TerrainLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Map/TerrainLogic.cppindex c18e65726..de7f0572d 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Map/TerrainLogic.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Map/TerrainLogic.cpp@@ -983,6 +983,8 @@ TerrainLogic::TerrainLogic()m_waypointListHead = NULL;m_bridgeListHead = NULL;+ m_waypointIndexValid = FALSE;+ m_triggerAreaIndexRevision = 0;m_mapData = NULL;m_bridgeDamageStatesChanged = FALSE;m_mapDX = 0;@@ -1366,8 +1368,18 @@ void TerrainLogic::addWaypoint(MapObject *pMapObj)DEBUG_ASSERTCRASH(pMapObj->isWaypoint(), ("not a waypoint"));Waypoint *pWay = newInstance(Waypoint)(pMapObj->getWaypointID(), pMapObj->getWaypointName(),&loc, label1, label2, label3, biDirectional);+ linkWaypoint(pWay);+}++//-------------------------------------------------------------------------------------------------+/** Puts a built waypoint at the head of the list, which is where the name and id lookups expect+ * the newest one to be, and drops the index that no longer describes the list. */+//-------------------------------------------------------------------------------------------------+void TerrainLogic::linkWaypoint(Waypoint *pWay)+{pWay->setNext(m_waypointListHead);m_waypointListHead = pWay;+ m_waypointIndexValid = FALSE;}//-------------------------------------------------------------------------------------------------@@ -1423,6 +1435,40 @@ void TerrainLogic::deleteWaypoints(void)pWay->deleteInstance();}m_waypointListHead = NULL;+ m_waypointsByName.clear();+ m_waypointsByID.clear();+ m_waypointIndexValid = FALSE;+}++//-------------------------------------------------------------------------------------------------+/** Indexes every waypoint by name and by id. The walk runs from the head of the list, and the+ * entries are insert-if-absent, so a name or an id that appears twice still resolves to the same+ * waypoint the old linear searches returned. */+//-------------------------------------------------------------------------------------------------+void TerrainLogic::buildWaypointIndex(void)+{+ m_waypointsByName.clear();+ m_waypointsByID.clear();+ m_waypointIndexValid = TRUE;++ for( Waypoint *way = m_waypointListHead; way; way = way->getNext() ) {+ m_waypointsByName.insert( std::make_pair( way->getName(), way ) );+ m_waypointsByID.insert( std::make_pair( (UnsignedInt)way->getID(), way ) );+ }+}++//-------------------------------------------------------------------------------------------------+/** Indexes every polygon trigger by name. Same rule as the waypoints: walked from the head of the+ * list, insert-if-absent, so a duplicated area name resolves exactly as it did before. */+//-------------------------------------------------------------------------------------------------+void TerrainLogic::buildTriggerAreaIndex(void)+{+ m_triggerAreasByName.clear();+ m_triggerAreaIndexRevision = PolygonTrigger::getListRevision();++ for (PolygonTrigger* pTrig = PolygonTrigger::getFirstPolygonTrigger(); pTrig; pTrig = pTrig->getNext()) {+ m_triggerAreasByName.insert( std::make_pair( pTrig->getTriggerName(), pTrig ) );+ }}//-------------------------------------------------------------------------------------------------@@ -1564,11 +1610,11 @@ void TerrainLogic::addLandmarkBridgeToLogic(Object *bridgeObj)//-------------------------------------------------------------------------------------------------Waypoint *TerrainLogic::getWaypointByName( AsciiString name ){- for( Waypoint *way = m_waypointListHead; way; way = way->getNext() )- if (way->getName() == name)- return way;+ if (!m_waypointIndexValid)+ buildWaypointIndex();- return NULL;+ WaypointNameMap::const_iterator it = m_waypointsByName.find( name );+ return (it != m_waypointsByName.end()) ? it->second : NULL;}//-------------------------------------------------------------------------------------------------@@ -1576,11 +1622,11 @@ Waypoint *TerrainLogic::getWaypointByName( AsciiString name )//-------------------------------------------------------------------------------------------------Waypoint *TerrainLogic::getWaypointByID( UnsignedInt id ){- for( Waypoint *way = m_waypointListHead; way; way = way->getNext() )- if (way->getID() == id)- return way;+ if (!m_waypointIndexValid)+ buildWaypointIndex();- return NULL;+ WaypointIDMap::const_iterator it = m_waypointsByID.find( id );+ return (it != m_waypointsByID.end()) ? it->second : NULL;}//-------------------------------------------------------------------------------------------------@@ -1640,12 +1686,11 @@ Bool TerrainLogic::isPurposeOfPath( Waypoint *pWay, AsciiString label )//-------------------------------------------------------------------------------------------------PolygonTrigger *TerrainLogic::getTriggerAreaByName( AsciiString name ){- for (PolygonTrigger* pTrig = PolygonTrigger::getFirstPolygonTrigger(); pTrig; pTrig = pTrig->getNext()) {- AsciiString trigName = pTrig->getTriggerName();- if (name == trigName)- return pTrig;- }- return NULL;+ if (m_triggerAreaIndexRevision != PolygonTrigger::getListRevision())+ buildTriggerAreaIndex();++ TriggerAreaNameMap::const_iterator it = m_triggerAreasByName.find( name );+ return (it != m_triggerAreasByName.end()) ? it->second : NULL;}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex e3dda3b4c..8f16dd2c5 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -52,6 +52,8 @@#include "GameLogic/ScriptEngine.h"#include "GameLogic/Scripts.h"#include "GameLogic/SidesList.h"+#include "GameLogic/TerrainLogic.h"+#include "GameLogic/PolygonTrigger.h"#include "Common/TunnelTracker.h"#include "Common/StateMachine.h"@@ -3291,3 +3293,102 @@ TEST(selfslug_is_monotonic_in_the_cushion)}CHECK( seenNo );}++// ---------------------------------------------------------------------------------------------+// TerrainLogic used to answer "which waypoint is called X" and "which trigger area is called X"+// by walking the whole list, on every call, and the scripts ask constantly. Both are indexed now,+// and the index has to notice the list changing under it.+// ---------------------------------------------------------------------------------------------+class TestTerrainLogic : public TerrainLogic+{+public:+ void addTestWaypoint( UnsignedInt id, AsciiString name )+ {+ Coord3D loc;+ loc.x = (Real)id;+ loc.y = 0.0f;+ loc.z = 0.0f;+ Waypoint *way = newInstance(Waypoint)( (WaypointID)id, name, &loc,+ AsciiString::TheEmptyString,+ AsciiString::TheEmptyString,+ AsciiString::TheEmptyString, FALSE );+ linkWaypoint( way );+ }++ void dropAllWaypoints( void ) { deleteWaypoints(); }+};++// A missing waypoint reads as id 0 / name "<none>" rather than a null dereference, so a broken+// index reports itself instead of taking the whole run down.+static UnsignedInt waypointID( Waypoint *way ) { return way ? (UnsignedInt)way->getID() : 0; }+static AsciiString waypointName( Waypoint *way ) { return way ? way->getName() : AsciiString("<none>"); }++TEST(terrainlogic_finds_a_waypoint_by_name_and_by_id)+{+ TestTerrainLogic tl;++ tl.addTestWaypoint( 1, "Alpha" );+ tl.addTestWaypoint( 2, "Bravo" );+ tl.addTestWaypoint( 3, "Charlie" );++ CHECK( tl.getWaypointByName( "Bravo" ) != NULL );+ CHECK_EQ( waypointID( tl.getWaypointByName( "Bravo" ) ), (UnsignedInt)2 );+ CHECK( tl.getWaypointByID( 3 ) != NULL );+ CHECK_STR( waypointName( tl.getWaypointByID( 3 ) ).str(), "Charlie" );+ CHECK( tl.getWaypointByName( "Nobody" ) == NULL );+ CHECK( tl.getWaypointByID( 99 ) == NULL );++ // the index was built by those lookups; a waypoint added afterwards still has to be found+ tl.addTestWaypoint( 4, "Delta" );+ CHECK( tl.getWaypointByName( "Delta" ) != NULL );+ CHECK_EQ( waypointID( tl.getWaypointByName( "Delta" ) ), (UnsignedInt)4 );+ CHECK( tl.getWaypointByName( "Alpha" ) != NULL );++ // the list is built by pushing onto the head and the old search returned the first match from+ // the head, so the most recently added of two waypoints sharing a name is the one that answers+ tl.addTestWaypoint( 5, "Alpha" );+ CHECK_EQ( waypointID( tl.getWaypointByName( "Alpha" ) ), (UnsignedInt)5 );++ tl.dropAllWaypoints();+ CHECK( tl.getWaypointByName( "Alpha" ) == NULL );+ CHECK( tl.getWaypointByID( 1 ) == NULL );+}++TEST(terrainlogic_finds_a_trigger_area_by_name_and_notices_edits)+{+ PolygonTrigger::deleteTriggers();++ TestTerrainLogic tl;++ PolygonTrigger *zone1 = newInstance(PolygonTrigger)( 4 );+ zone1->setTriggerName( "Zone1" );+ PolygonTrigger::addPolygonTrigger( zone1 );++ PolygonTrigger *zone2 = newInstance(PolygonTrigger)( 4 );+ zone2->setTriggerName( "Zone2" );+ PolygonTrigger::addPolygonTrigger( zone2 );++ CHECK( tl.getTriggerAreaByName( "Zone1" ) == zone1 );+ CHECK( tl.getTriggerAreaByName( "Zone2" ) == zone2 );+ CHECK( tl.getTriggerAreaByName( "Zone3" ) == NULL );++ // added after the index was built+ PolygonTrigger *zone3 = newInstance(PolygonTrigger)( 4 );+ zone3->setTriggerName( "Zone3" );+ PolygonTrigger::addPolygonTrigger( zone3 );+ CHECK( tl.getTriggerAreaByName( "Zone3" ) == zone3 );++ // renamed after the index was built+ zone3->setTriggerName( "Zone9" );+ CHECK( tl.getTriggerAreaByName( "Zone9" ) == zone3 );+ CHECK( tl.getTriggerAreaByName( "Zone3" ) == NULL );++ // unlinked after the index was built+ PolygonTrigger::removePolygonTrigger( zone2 );+ CHECK( tl.getTriggerAreaByName( "Zone2" ) == NULL );+ CHECK( tl.getTriggerAreaByName( "Zone1" ) == zone1 );+ zone2->deleteInstance();++ PolygonTrigger::deleteTriggers();+ CHECK( tl.getTriggerAreaByName( "Zone1" ) == NULL );+}