@@ -149,7 +149,11 @@ static int SetGlobalCounter(ILuaInterface lua) {
149149
150150 [ LuaFunction ]
151151 static int CleanUpMap ( ILuaInterface lua ) {
152- // todo: refuse with "Cannot run game.CleanUpMap while deleting entities!\n" while entities are being deleted
152+ if ( Util . g_bRemovingEntity > 0 || Util . g_bDisableEhandleAccess ) {
153+ lua . ErrorFromLua ( "Cannot run game.CleanUpMap while deleting entities!\n " ) ;
154+ return 0 ;
155+ }
156+
153157 if ( MapCleanupRunning || MapCleanupPending ) {
154158 lua . ErrorFromLua ( "Cannot run game.CleanUpMap while it is already running!\n " ) ;
155159 return 0 ;
@@ -198,13 +202,18 @@ static void DoCleanUpMap(bool dontSendToClients, ILuaObject filters) {
198202 GarrysMod . BroadcastLua ( filter , "game.CleanUpMap()" ) ;
199203 }
200204
201- // todo: unidentified step between the client broadcast and AllowImmediateEdictReuse
205+ // todo: s_RagdollLRU.RemoveAll()
202206 engine . AllowImmediateEdictReuse ( ) ;
203207
204208 gGM ? . Call ( ( int ) LUA_POOLEDSTRING . PostCleanupMap ) ;
205209 MapCleanupRunning = false ;
206210 }
207211
212+ public static void ResetMapCleanupSchedule ( ) {
213+ MapCleanupPending = false ;
214+ MapCleanupDontSendToClients = false ;
215+ }
216+
208217 public static void RunMapCleanupIfNeeded ( ) {
209218 if ( ! MapCleanupPending )
210219 return ;
@@ -259,12 +268,17 @@ static void DoCleanUpMap() {
259268 MapCleanupRunning = true ;
260269 gGM ? . Call ( ( int ) LUA_POOLEDSTRING . PreCleanupMap ) ;
261270
262- // todo: remove the Lua-created clientside entities
271+ // todo: C_PhysPropClientside.RecreateAll()
263272 engine . ClientCmd ( "r_cleardecals\n " ) ;
264273 enginesound . StopAllSounds ( true ) ;
265- // todo: unidentified call that follows StopAllSounds
266- // todo: release every C_Hairball in the client entity list
267- // todo: unidentified step before the PostCleanupMap hook
274+ Soundscape_OnStopAllSounds ( ) ;
275+ List < C_BaseEntity > hairballs = [ ] ;
276+ for ( C_BaseEntity ? ent = cl_entitylist . FirstBaseEntity ( ) ; ent != null ; ent = cl_entitylist . NextBaseEntity ( ent ) )
277+ if ( ! ent . IsEFlagSet ( EFL . KillMe ) && strcmp ( ent . GetClassname ( ) , "class C_Hairball" ) == 0 )
278+ hairballs . Add ( ent ) ;
279+ foreach ( C_BaseEntity ent in hairballs )
280+ ent . Release ( ) ;
281+ // todo: s_RagdollLRU.RemoveAll()
268282
269283 gGM ? . Call ( ( int ) LUA_POOLEDSTRING . PostCleanupMap ) ;
270284 MapCleanupRunning = false ;
0 commit comments