aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/server/nav_merge.cpp
blob: d62764ab59cc8dea84aa5a01fcdc0764389e366f (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
// nav_merge.cpp
// Save/merge for partial nav meshes
//========= Copyright Valve Corporation, All rights reserved. ============//

#include "cbase.h"
#include "fmtstr.h"
#include "tier0/vprof.h"
#include "utldict.h"

#include "nav_mesh.h"

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


//--------------------------------------------------------------------------------------------------------
void CNavArea::SaveToSelectedSet( KeyValues *areaKey ) const
{
	const char *placeName = TheNavMesh->PlaceToName( GetPlace() );
	areaKey->SetString( "Place", (placeName)?placeName:"" );

	areaKey->SetInt( "Attributes", GetAttributes() );
}


//--------------------------------------------------------------------------------------------------------
void CNavArea::RestoreFromSelectedSet( KeyValues *areaKey )
{
	SetPlace( TheNavMesh->NameToPlace( areaKey->GetString( "Place" ) ) );

	SetAttributes( areaKey->GetInt( "Attributes" ) );
}


//--------------------------------------------------------------------------------------------------------
class BuildSelectedSet
{
public:
	BuildSelectedSet( KeyValues *kv )
	{
		m_kv = kv;
		m_areaCount = 0;
	}

	bool operator() ( CNavArea *area )
	{
		CFmtStrN<32> name( "%d", area->GetID() );
		KeyValues *areaKey = m_kv->FindKey( name.Access(), true );
		if ( areaKey )
		{
			++m_areaCount;

			WriteCorner( area, areaKey, NORTH_WEST, "NorthWest" );
			WriteCorner( area, areaKey, NORTH_EAST, "NorthEast" );
			WriteCorner( area, areaKey, SOUTH_WEST, "SouthWest" );
			WriteCorner( area, areaKey, SOUTH_EAST, "SouthEast" );

			WriteConnections( area, areaKey, NORTH, "North" );
			WriteConnections( area, areaKey, SOUTH, "South" );
			WriteConnections( area, areaKey, EAST, "East" );
			WriteConnections( area, areaKey, WEST, "West" );

			area->SaveToSelectedSet( areaKey );
		}
		return true;
	}

	int Count( void ) const
	{
		return m_areaCount;
	}

private:
	void WriteCorner( CNavArea *area, KeyValues *areaKey, NavCornerType corner, const char *cornerName )
	{
		KeyValues *cornerKey = areaKey->FindKey( cornerName, true );
		if ( cornerKey )
		{
			Vector pos = area->GetCorner( corner );
			cornerKey->SetFloat( "x", pos.x );
			cornerKey->SetFloat( "y", pos.y );
			cornerKey->SetFloat( "z", pos.z );
		}
	}

	void WriteConnections( CNavArea *area, KeyValues *areaKey, NavDirType dir, const char *dirName )
	{
		KeyValues *dirKey = areaKey->FindKey( dirName, true );
		if ( dirKey )
		{
			for ( int i=0; i<area->GetAdjacentCount( dir ); ++i )
			{
				CNavArea *other = area->GetAdjacentArea( dir, i );
				if ( other && TheNavMesh->IsInSelectedSet( other ) )
				{
					CFmtStrN<32> name( "%d", i );
					dirKey->SetInt( name.Access(), other->GetID() );
				}
			}
		}
	}

	int m_areaCount;
	KeyValues *m_kv;
};


//--------------------------------------------------------------------------------------------------------
void CNavMesh::CommandNavSaveSelected( const CCommand &args )
{
	KeyValues *data = new KeyValues( "Selected Nav Areas" );
	data->SetInt( "version", 1 );

	BuildSelectedSet setBuilder( data );
	TheNavMesh->ForAllSelectedAreas( setBuilder );

	if ( !setBuilder.Count() )
	{
		Msg( "Not saving empty selected set to disk.\n" );
		data->deleteThis();
		return;
	}

	char fname[32];
	char path[MAX_PATH];
	if ( args.ArgC() == 2 )
	{
		V_FileBase( args[0], fname, sizeof( fname ) );
	}
	else
	{
		V_strncpy( fname, STRING( gpGlobals->mapname ), sizeof( fname ) );
	}

	int i;
	for ( i=0; i<1000; ++i )
	{
		V_snprintf( path, sizeof( path ), "maps/%s_selected_%4.4d.txt", fname, i );
		if ( !filesystem->FileExists( path ) )
		{
			break;
		}
	}

	if ( i == 1000 )
	{
		Msg( "Unable to find a filename to save the selected set to disk.\n" );
		data->deleteThis();
		return;
	}

	if ( !data->SaveToFile( filesystem, path ) )
	{
		Msg( "Unable to save the selected set to disk.\n" );
	}

	Msg( "Selected set saved to %s.  Use 'nav_merge_mesh %s_selected_%4.4d' to merge it into another mesh.\n", path, fname, i );
	data->deleteThis();
}


//--------------------------------------------------------------------------------------------------------
CON_COMMAND_F( nav_save_selected, "Writes the selected set to disk for merging into another mesh via nav_merge_mesh.", FCVAR_GAMEDLL | FCVAR_CHEAT )
{
	if ( !UTIL_IsCommandIssuedByServerAdmin() )
		return;

	TheNavMesh->CommandNavSaveSelected( args );
}


//--------------------------------------------------------------------------------------------------------
Vector ReadCorner( KeyValues *areaKey, const char *cornerName )
{
	Vector pos( vec3_origin );
	KeyValues *cornerKey = areaKey->FindKey( cornerName, false );
	if ( cornerKey )
	{
		pos.x = cornerKey->GetFloat( "x" );
		pos.y = cornerKey->GetFloat( "y" );
		pos.z = cornerKey->GetFloat( "z" );
	}

	return pos;
}


//--------------------------------------------------------------------------------------------------------
void ReconnectMergedArea( CUtlDict< CNavArea *, int > &newAreas, KeyValues *areaKey, NavDirType dir, const char *dirName )
{
	int index = newAreas.Find( areaKey->GetName() );
	if ( index == newAreas.InvalidIndex() )
	{
		Assert( false );
		return;
	}

	CNavArea *area = newAreas[index];

	KeyValues *dirKey = areaKey->FindKey( dirName, true );
	if ( dirKey )
	{
		KeyValues *connection = dirKey->GetFirstValue();
		while ( connection )
		{
			const char *otherID = connection->GetString();
			int otherIndex = newAreas.Find( otherID );
			Assert( otherIndex != newAreas.InvalidIndex() );
			if ( otherIndex != newAreas.InvalidIndex() )
			{
				CNavArea *other = newAreas[otherIndex];

				area->ConnectTo( other, dir );	// only a 1-way connection.  the other area will connect back to us.
			}

			connection = connection->GetNextValue();
		}
	}
}


//--------------------------------------------------------------------------------------------------------
void CNavMesh::CommandNavMergeMesh( const CCommand &args )
{
	if ( args.ArgC() != 2 )
	{
		Msg( "Usage: nav_merge_mesh filename\n" );
		return;
	}

	char fname[64];
	char path[MAX_PATH];
	V_FileBase( args[1], fname, sizeof( fname ) );
	V_snprintf( path, sizeof( path ), "maps/%s.txt", fname );

	KeyValues *data = new KeyValues( "Nav Selected Set" );
	if ( !data->LoadFromFile( filesystem, path ) )
	{
		Msg( "Unable to load %s.\n", path );
	}
	else
	{
		// Loaded the data - plug it into the existing mesh!

		// First add the areas, and put them in the correct places.  We can save off the new area ID
		// at the same time.
		CUtlDict< CNavArea *, int > newAreas;
		CUtlVector< CNavArea * > areaVector;
		KeyValues *areaKey = data->GetFirstSubKey();
		while ( areaKey )
		{
			Vector northWest = ReadCorner( areaKey, "NorthWest" );
			Vector northEast = ReadCorner( areaKey, "NorthEast" );
			Vector southWest = ReadCorner( areaKey, "SouthWest" );
			Vector southEast = ReadCorner( areaKey, "SouthEast" );

			CNavArea *newArea = TheNavMesh->CreateArea();
			if (newArea == NULL)
			{
				Warning( "nav_merge_mesh: Out of memory\n" );
				return;
			}

			newArea->Build( northWest, northEast, southEast, southWest );
			TheNavAreas.AddToTail( newArea );
			TheNavMesh->AddNavArea( newArea );
			areaVector.AddToTail( newArea );

			// save the new ID for connections
			int index = newAreas.Find( areaKey->GetName() );
			Assert( index == newAreas.InvalidIndex() );
			if ( index == newAreas.InvalidIndex() )
			{
				newAreas.Insert( areaKey->GetName(), newArea );
			}

			// Restore additional data
			newArea->RestoreFromSelectedSet( areaKey );

			areaKey = areaKey->GetNextKey();
		}

		// Go back and reconnect the new areas to each other
		areaKey = data->GetFirstSubKey();
		while ( areaKey )
		{
			ReconnectMergedArea( newAreas, areaKey, NORTH, "North" );
			ReconnectMergedArea( newAreas, areaKey, SOUTH, "South" );
			ReconnectMergedArea( newAreas, areaKey, EAST, "East" );
			ReconnectMergedArea( newAreas, areaKey, WEST, "West" );

			areaKey = areaKey->GetNextKey();
		}

		// Connect selected areas with pre-existing areas
		StitchAreaSet( &areaVector );
	}

	data->deleteThis();
}


//--------------------------------------------------------------------------------------------------------
int NavMeshMergeAutocomplete( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
{
	char *commandName = "nav_merge_mesh";
	int numMatches = 0;
	partial += Q_strlen( commandName ) + 1;
	int partialLength = Q_strlen( partial );

	FileFindHandle_t findHandle;
	char txtFilenameNoExtension[ MAX_PATH ];
	const char *txtFilename = filesystem->FindFirstEx( "maps/*_selected_*.txt", "MOD", &findHandle );
	while ( txtFilename )
	{
		Q_FileBase( txtFilename, txtFilenameNoExtension, sizeof( txtFilenameNoExtension ) );
		if ( !Q_strnicmp( txtFilenameNoExtension, partial, partialLength ) && V_stristr( txtFilenameNoExtension, "_selected_" ) )
		{
			// Add the place name to the autocomplete array
			Q_snprintf( commands[ numMatches++ ], COMMAND_COMPLETION_ITEM_LENGTH, "%s %s", commandName, txtFilenameNoExtension );

			// Make sure we don't try to return too many place names
			if ( numMatches == COMMAND_COMPLETION_MAXITEMS )
				return numMatches;
		}

		txtFilename = filesystem->FindNext( findHandle );
	}
	filesystem->FindClose( findHandle );

	return numMatches;
}


//--------------------------------------------------------------------------------------------------------
CON_COMMAND_F_COMPLETION( nav_merge_mesh, "Merges a saved selected set into the current mesh.", FCVAR_GAMEDLL | FCVAR_CHEAT, NavMeshMergeAutocomplete )
{
	if ( !UTIL_IsCommandIssuedByServerAdmin() )
		return;

	TheNavMesh->CommandNavMergeMesh( args );
}


//--------------------------------------------------------------------------------------------------------




//--------------------------------------------------------------------------------------------------------