summaryrefslogtreecommitdiff
path: root/engine/pr_edict.cpp
blob: 755adc1cdf320ffffddb4692d703686686a85f5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $Workfile:     $
// $Date:         $
//
//-----------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================//
#include "quakedef.h"
#include <stddef.h>
#include "vengineserver_impl.h"
#include "server.h"
#include "pr_edict.h"
#include "world.h"
#include "ispatialpartition.h"
#include "utllinkedlist.h"
#include "framesnapshot.h"
#include "sv_log.h"
#include "tier1/utlmap.h"
#include "tier1/utlvector.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

// Edicts won't get reallocated for this many seconds after being freed.
#define EDICT_FREETIME	1.0



#ifdef _DEBUG
#include <malloc.h>
#endif // _DEBUG

static ConVar		sv_useexplicitdelete( "sv_useexplicitdelete", "1", FCVAR_DEVELOPMENTONLY, "Explicitly delete dormant client entities caused by AllowImmediateReuse()." );
static ConVar		sv_lowedict_threshold( "sv_lowedict_threshold", "8", FCVAR_NONE, "When only this many edicts are free, take the action specified by sv_lowedict_action.", true, 0, true, MAX_EDICTS );
static ConVar		sv_lowedict_action( "sv_lowedict_action", "0", FCVAR_NONE, "0 - no action, 1 - warn to log file, 2 - attempt to restart the game, if applicable, 3 - restart the map, 4 - go to the next map in the map cycle, 5 - spew all edicts.", true, 0, true, 5 );

// Bitmask of free edicts.
static CBitVec< MAX_EDICTS > g_FreeEdicts;

/*
=================
ED_ClearEdict

Sets everything to NULL, done when new entity is allocated for game.dll
=================
*/
static void ED_ClearEdict( edict_t *e )
{
	e->ClearFree();
	e->ClearStateChanged();
	e->SetChangeInfoSerialNumber( 0 );
	
	serverGameEnts->FreeContainingEntity(e);
	InitializeEntityDLLFields(e);

	e->m_NetworkSerialNumber = -1;  // must be filled by game.dll
	Assert( (int) e->m_EdictIndex == (e - sv.edicts) );
}

void ED_ClearFreeFlag( edict_t *e )
{
	e->ClearFree();
	g_FreeEdicts.Clear( e->m_EdictIndex );
}

void ED_ClearFreeEdictList()
{
	// Clear the free edicts bitfield.
	g_FreeEdicts.ClearAll();
}

static void SpewEdicts()
{
	CUtlMap< const char*, int > mapEnts;
	mapEnts.SetLessFunc( StringLessThan );

	// Tally up each class
	int nEdictNum = 1;
	for( int i=0; i<sv.num_edicts; ++i )
	{
		edict_t *e = &sv.edicts[i];
		++nEdictNum;
		unsigned short nIndex = mapEnts.Find( e->GetClassName() );
		if ( nIndex == mapEnts.InvalidIndex() )
		{
			nIndex = mapEnts.Insert( e->GetClassName() );
			mapEnts[ nIndex ] = 0;
		}
		mapEnts[ nIndex ] = mapEnts[ nIndex ] + 1;
	}

	struct EdictCount_t
	{
		EdictCount_t( const char *pszClassName, int nCount )
			:
			m_pszClassName( pszClassName ),
			m_nCount( nCount )
		{}

		const char *m_pszClassName;
		int m_nCount;
	};

	CUtlVector<EdictCount_t*> vecEnts;

	FOR_EACH_MAP_FAST( mapEnts, i )
	{
		vecEnts.AddToTail( new EdictCount_t( mapEnts.Key( i ), mapEnts[ i ] ) );
	}

	struct EdictSorter_t
	{
		static int SortEdicts( EdictCount_t* const *p1, EdictCount_t* const *p2 )
		{
			return (*p1)->m_nCount - (*p2)->m_nCount;
		}
	};

	vecEnts.Sort( &EdictSorter_t::SortEdicts );

	g_Log.Printf( "Spewing edict counts:\n" );
	FOR_EACH_VEC( vecEnts, i )
	{
		g_Log.Printf( "(%3.2f%%) %d\t%s\n", vecEnts[i]->m_nCount/(float)nEdictNum * 100.f, vecEnts[i]->m_nCount, vecEnts[i]->m_pszClassName );
	}
	g_Log.Printf( "Total edicts: %d\n", nEdictNum );

	vecEnts.PurgeAndDeleteElements();
}

/*
=================
ED_Alloc

Either finds a free edict, or allocates a new one.
Try to avoid reusing an entity that was recently freed, because it
can cause the client to think the entity morphed into something else
instead of being removed and recreated, which can cause interpolated
angles and bad trails.
=================
*/


edict_t *ED_Alloc( int iForceEdictIndex )
{
	if ( iForceEdictIndex >= 0 )
	{
		if ( iForceEdictIndex >= sv.num_edicts )
		{
			Warning( "ED_Alloc( %d ) - invalid edict index specified.", iForceEdictIndex );
			return NULL;
		}
		
		edict_t *e = &sv.edicts[ iForceEdictIndex ];
		if ( e->IsFree() )
		{
			Assert( iForceEdictIndex == e->m_EdictIndex );
			--sv.free_edicts;
			Assert( g_FreeEdicts.IsBitSet( iForceEdictIndex ) );
			g_FreeEdicts.Clear( iForceEdictIndex );
			ED_ClearEdict( e );
			return e;
		}
		else
		{
			return NULL;
		}
	}

	// Check the free list first.
	int iBit = -1;
	for ( ;; )
	{
		iBit = g_FreeEdicts.FindNextSetBit( iBit + 1 );
		if ( iBit < 0 )
			break;

		edict_t *pEdict = &sv.edicts[ iBit ];

		// If this assert goes off, someone most likely called pedict->ClearFree() and not ED_ClearFreeFlag()?
		Assert( pEdict->IsFree() );
		Assert( iBit == pEdict->m_EdictIndex );
		if ( ( pEdict->freetime < 2 ) || ( sv.GetTime() - pEdict->freetime >= EDICT_FREETIME ) )
		{
			// If we have no freetime, we've had AllowImmediateReuse() called. We need
			// to explicitly delete this old entity.
			if ( pEdict->freetime == 0 && sv_useexplicitdelete.GetBool() )
			{
				//Warning("ADDING SLOT to snapshot: %d\n", i );
				framesnapshotmanager->AddExplicitDelete( iBit );
			}

			--sv.free_edicts;
			g_FreeEdicts.Clear( pEdict->m_EdictIndex );
			ED_ClearEdict( pEdict );
			return pEdict;
		}
	}

	// Allocate a new edict.
	if ( sv.num_edicts >= sv.max_edicts )
	{
		AssertMsg( 0, "Can't allocate edict" );

		SpewEdicts(); // Log the entities we have before we die

		if ( sv.max_edicts == 0 )
			Sys_Error( "ED_Alloc: No edicts yet" );
		Sys_Error ("ED_Alloc: no free edicts");
	}

	// Do this before clearing since clear now needs to call back into the edict to deduce the index so can get the changeinfo data in the parallel structure
	edict_t *pEdict = sv.edicts + sv.num_edicts++; 

	// We should not be in the free list...
	Assert( !g_FreeEdicts.IsBitSet( pEdict->m_EdictIndex ) );
	ED_ClearEdict( pEdict );

	if ( sv_lowedict_action.GetInt() > 0 && sv.num_edicts >= sv.max_edicts - sv_lowedict_threshold.GetInt() )
	{
		int nEdictsRemaining = sv.max_edicts - sv.num_edicts;
		g_Log.Printf( "Warning: free edicts below threshold. %i free edict%s remaining.\n", nEdictsRemaining, nEdictsRemaining == 1 ? "" : "s" );

		switch ( sv_lowedict_action.GetInt() )
		{
		case 2:
			// restart the game
			{
				ConVarRef mp_restartgame_immediate( "mp_restartgame_immediate" );
				if ( mp_restartgame_immediate.IsValid() )
				{
					mp_restartgame_immediate.SetValue( 1 );
				}
				else
				{
					ConVarRef mp_restartgame( "mp_restartgame" );
					if ( mp_restartgame.IsValid() )
					{
						mp_restartgame.SetValue( 1 );
					}
				}
			}
			break;
		case 3:
			// restart the map
			g_pVEngineServer->ChangeLevel( sv.GetMapName(), NULL );
			break;
		case 4:
			// go to the next map
			g_pVEngineServer->ServerCommand( "changelevel_next\n" );
			break;
		case 5:
			// spew all edicts
			SpewEdicts();
			break;
		}
	}
	
	return pEdict;
}


void ED_AllowImmediateReuse()
{
	edict_t *pEdict = sv.edicts + sv.GetMaxClients() + 1;
	for ( int i=sv.GetMaxClients()+1; i < sv.num_edicts; i++ )
	{
		if ( pEdict->IsFree() )
		{
			pEdict->freetime = 0;
		}

		pEdict++;
	}
}


/*
=================
ED_Free

Marks the edict as free
FIXME: walk all entities and NULL out references to this entity
=================
*/
void ED_Free (edict_t *ed)
{
	if (ed->IsFree())
	{
#ifdef _DEBUG
//		ConDMsg("duplicate free on '%s'\n", pr_strings + ed->classname );
#endif
		return;
	}

	// don't free player edicts
	if ( (ed - sv.edicts) >= 1 && (ed - sv.edicts) <= sv.GetMaxClients() )
		return;

	// release the DLL entity that's attached to this edict, if any
	serverGameEnts->FreeContainingEntity( ed );

	ed->SetFree();
	ed->freetime = sv.GetTime();

	++sv.free_edicts;
	Assert( !g_FreeEdicts.IsBitSet( ed->m_EdictIndex ) );
	g_FreeEdicts.Set( ed->m_EdictIndex );

	// Increment the serial number so it knows to send explicit deletes the clients.
	ed->m_NetworkSerialNumber++; 
}

//
// 	serverGameEnts->FreeContainingEntity( pEdict )  frees up memory associated with a DLL entity.
// InitializeEntityDLLFields clears out fields to NULL or UNKNOWN.
// Release is for terminating a DLL entity.  Initialize is for initializing one.
//
void InitializeEntityDLLFields( edict_t *pEdict )
{
	// clear all the game variables
	size_t sz = offsetof( edict_t, m_pUnk ) + sizeof( void* );
	memset( ((byte*)pEdict) + sz, 0, sizeof(edict_t) - sz );
}

int NUM_FOR_EDICT(const edict_t *e)
{
	if ( &sv.edicts[e->m_EdictIndex] == e ) // NOTE: old server.dll may stomp m_EdictIndex
		return e->m_EdictIndex;
	int index = e - sv.edicts;
	if ( (unsigned int) index >= (unsigned int) sv.num_edicts )
		Sys_Error ("NUM_FOR_EDICT: bad pointer");
	return index;
}

edict_t *EDICT_NUM(int n)
{
	if ((unsigned int) n >= (unsigned int) sv.max_edicts)
		Sys_Error ("EDICT_NUM: bad number %i", n);
	return &sv.edicts[n];
}


static inline int NUM_FOR_EDICTINFO(const edict_t *e)
{
	if ( &sv.edicts[e->m_EdictIndex] == e ) // NOTE: old server.dll may stomp m_EdictIndex
		return e->m_EdictIndex;
	int index = e - sv.edicts;
	if ( (unsigned int) index >= (unsigned int) sv.max_edicts )
		Sys_Error ("NUM_FOR_EDICTINFO: bad pointer");
	return index;
}


IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
{
	return &sv.edictchangeinfo[ NUM_FOR_EDICTINFO( (const edict_t*)this ) ];
}

const IChangeInfoAccessor *CBaseEdict::GetChangeAccessor() const
{
	return &sv.edictchangeinfo[ NUM_FOR_EDICTINFO( (const edict_t*)this ) ];
}