Difference between revisions of "Resetting Maps and Entities"

From Valve Developer Community
Jump to: navigation, search
m (spelling)
m (Client Side)
Line 236: Line 236:
==Client Side==
==Client Side==
As part of the reset process client side entities and decals also have be cleared up, this is done by listening for the <code>game_round_restart</code> event that was fired by the server during the reset process. The neatest place to do this is in '''clientmode_shared.cpp'''.
As part of the reset process client side entities and decals also have to be cleared up, this is done by listening for the <code>game_round_restart</code> event that was fired by the server during the reset process. The best place to do this is in '''clientmode_shared.cpp'''.
Add a listener to <code>ClientModeShared::Init()</code>
Add a listener to <code>ClientModeShared::Init()</code>

Revision as of 18:25, 31 October 2006


The example code provided can be used by copying and pasting, but it is best to ensure you read through the rest of the text to gain a proper understanding of the process behind it. Each section of code is explained thoroughly in the body of text and via comments. It is recommended to have the source code open as you read through the article so you can easily find the relevant sections.

The goal of this article is to enable a mod to restart a round by resetting all map entities to their default locations, the code provided was originally for the Source Only SDK, but has been tested in the HL2MP SDK to ensure it works.

The first thing covered are the files that should be created, these contain the list of entities to keep and a custom entity filter.

Server Side

Add two new empty files to the server project: mapfilter.cpp and mapfilter.h. It is recommended that any new files are created in a separate folder from the core SDK codebase.


// general definitions and includes
#include "cbase.h"
#include "mapentities.h"
#include "utllinkedlist.h"

The following is a generic list of entities that should be preserved between resets, be sure that the last entry is set to NULL. Add any mod specific entities (such as custom game rules) that do not want to be recreated on reset.

// These entities are preserved each round restart. The rest are removed and recreated.
static const char *s_PreserveEnts[] =
  NULL,// END Marker

CMapEntityRef is a purely helper class, it is accessed through the linked list that is declared extern beneath the class definition.

class CMapEntityRef
  int m_iEdict;
  int m_iSerialNumber;
extern CUtlLinkedList<CMapEntityRef, unsigned short> g_MapEntityRefs;

The following is our custom map entity filter, this gets used on initial level load and is used to fill the linked list with entity data.

class CMapLoadEntityFilter : public IMapEntityFilter
virtual bool ShouldCreateEntity( const char *pClassname ) { // During map load, create all the entities. return true; }
virtual CBaseEntity* CreateNextEntity( const char *pClassname ) { // create each entity in turn and an instance of CMapEntityRef CBaseEntity *pRet = CreateEntityByName( pClassname );
CMapEntityRef ref; ref.m_iEdict = -1; ref.m_iSerialNumber = -1;
// if the new entity is valid store the entity information in ref if ( pRet ) { ref.m_iEdict = pRet->entindex();
if ( pRet->edict() ) ref.m_iSerialNumber = pRet->edict()->m_NetworkSerialNumber; }
// add the new ref to the linked list and return the entity g_MapEntityRefs.AddToTail( ref ); return pRet; } };


All that's done here is the declaration of our linked list

#include "cbase.h"
#include "mapfilter.h"
// memdbgon must be the last include file in a .cpp file!!! #include "tier0/memdbgon.h"
CUtlLinkedList<CMapEntityRef, unsigned short> g_MapEntityRefs;


Find CServerGameDLL::LevelInit_ParseAllEntities in sdk_gameinterface.cpp, which is called when a level is first loaded and where the first addition is made. Add the following code to the function:

// Load the entities and build up a new list of the map entities and their starting state in here.
CMapLoadEntityFilter filter;
MapEntity_ParseAllEntities( pMapEntities, &filter );


Now add a function called CleanMap to the gamerules, this is the function that will be called when the map has to be reset (due to a round end etc). The function FindInList is used in at a couple of points, the following code show's the function definition.

// FindInList searches the provided array for the compare string
// we use it to check against our list of preserved entities (s_PreserveEnts)
bool FindInList(const char *s_List[], const char *compare)
  int index = 0;
while(s_List[index]) { if ( Q_strcmp(s_List[index], compare) == 0 ) return true;
index++; }
return false; }

The following code makes up the entire CleanUpMap function, it is divided into smaller sections to allow for clarification of some of the finer points.

void CSDKGameRules::CleanMap()
  // Recreate all the map entities from the map data (preserving their indices),
  // then remove everything else except the players.
// Get rid of all entities except players. CBaseEntity *pCur = gEntList.FirstEnt(); while ( pCur ) { if ( !FindInList( s_PreserveEnts, pCur->GetClassname() ) ) { UTIL_Remove( pCur ); }
pCur = gEntList.NextEnt( pCur ); }
// Really remove the entities so we can have access to their slots below. gEntList.CleanupDeleteList();

The following is an inline class declaration, it is meant to be here - it acts in a similar fashion to the previous CMapLoadEntityFilter, but rather than storing entity values it checks against the stored values to decide how to recreate entities.

  // Now reload the map entities.
  class CMapEntityFilter : public IMapEntityFilter
        virtual bool ShouldCreateEntity( const char *pClassname )
           // Don't recreate the preserved entities.
           if ( !FindInList( s_PreserveEnts, pClassname ) )
              return true;
              // Increment our iterator since it's not going to call CreateNextEntity for this ent.
              if ( m_iIterator != g_MapEntityRefs.InvalidIndex() )
                 m_iIterator = g_MapEntityRefs.Next( m_iIterator );
return false; } }
virtual CBaseEntity* CreateNextEntity( const char *pClassname ) { if ( m_iIterator == g_MapEntityRefs.InvalidIndex() ) { // We should never reach this point - g_MapEntityRefs should have been filled // when we loaded the map due to the use of CHDNMapLoadFilter, but we cover ourselves // by checking here. Assert( m_iIterator != g_MapEntityRefs.InvalidIndex() ); return NULL; } else { CMapEntityRef &ref = g_MapEntityRefs[m_iIterator]; m_iIterator = g_MapEntityRefs.Next( m_iIterator ); // Seek to the next entity.
if ( ref.m_iEdict == -1 || engine->PEntityOfEntIndex( ref.m_iEdict ) ) { // the entities previous edict has been used for whatever reason, // so just create it and use any spare edict slot return CreateEntityByName( pClassname ); } else { // The entity's edict slot was free, so we put it back where it came from. return CreateEntityByName( pClassname, ref.m_iEdict ); } } }
public: int m_iIterator; // Iterator into g_MapEntityRefs. };

The final task of this function is to call MapEntity_ParseAllEntities using the new filter class, this guarantees that only the entities that need to be recreated will be.

  CMapEntityFilter filter;
  filter.m_iIterator = g_MapEntityRefs.Head();
// final task, trigger the recreation of any entities that need it. MapEntity_ParseAllEntities( engine->GetMapEntitiesString(), &filter, true ); }

An important stage is to fire an event that signifies when the map has been restarted, this will allow the clients to pick up and perform their own clean up - the code below is a demonstration of this, be sure to add the event to ModEvents.res

IGameEvent * event = gameeventmanager->CreateEvent( "game_round_restart" );
if ( event )
  DevMsg(1, "Fired game_round_restart event\n");
  // fire the event
  gameeventmanager->FireEvent( event );

Client Side

As part of the reset process client side entities and decals also have to be cleared up, this is done by listening for the game_round_restart event that was fired by the server during the reset process. The best place to do this is in clientmode_shared.cpp.

Add a listener to ClientModeShared::Init()

gameeventmanager->AddListener( this, "game_round_restart", false );

Finally put the following at the end of ClientModeShared::FireGameEvent

else if ( Q_strcmp( "game_round_restart", eventname ) == 0 )
  // recreate all client side physics props
// Just tell engine to clear decals engine->ClientCmd( "r_cleardecals\n" );
//stop any looping sounds enginesound->StopAllSounds( true );
Soundscape_OnStopAllSounds(); // Tell the soundscape system. }