aboutsummaryrefslogtreecommitdiff
path: root/mp/src/utils/vbsp/prtfile.cpp
blob: 71d7e5cc3ac037bee6776540150c48500b4b6e5f (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
369
370
371
372
373
374
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//

#include "vbsp.h"
#include "collisionutils.h"
/*
==============================================================================

PORTAL FILE GENERATION

Save out name.prt for qvis to read
==============================================================================
*/


#define	PORTALFILE	"PRT1"

struct cluster_portals_t
{
	CUtlVector<portal_t *>	portals;
};

int		num_visclusters;				// clusters the player can be in
int		num_visportals;

int g_SkyCluster = -1;

void WriteFloat (FILE *f, vec_t v)
{
	if ( fabs(v - RoundInt(v)) < 0.001 )
		fprintf (f,"%i ",(int)RoundInt(v));
	else
		fprintf (f,"%f ",v);
}


/*
=================
WritePortalFile_r
=================
*/
void WritePortalFile(FILE *pFile, const CUtlVector<cluster_portals_t> &list)
{
	portal_t	*p;
	winding_t	*w;
	Vector		normal;
	vec_t		dist;

	for ( int clusterIndex = 0; clusterIndex < list.Count(); clusterIndex++ )
	{
		for ( int j = 0; j < list[clusterIndex].portals.Count(); j++ )
		{
			p = list[clusterIndex].portals[j];
			w = p->winding;
			// write out to the file
			
			// sometimes planes get turned around when they are very near
			// the changeover point between different axis.  interpret the
			// plane the same way vis will, and flip the side orders if needed
			// FIXME: is this still relevent?
			WindingPlane (w, normal, &dist);
			if ( DotProduct (p->plane.normal, normal) < 0.99 )
			{	// backwards...
				fprintf (pFile,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster);
			}
			else
			{
				fprintf (pFile,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster);
			}
			
			for (int i=0 ; i<w->numpoints ; i++)
			{
				fprintf (pFile,"(");
				WriteFloat (pFile, w->p[i][0]);
				WriteFloat (pFile, w->p[i][1]);
				WriteFloat (pFile, w->p[i][2]);
				fprintf (pFile,") ");
			}
			fprintf (pFile,"\n");
		}
	}
}

struct viscluster_t
{
	bspbrush_t *pBrushes;
	int clusterIndex;
	int leafCount;
	int leafStart;
};

static CUtlVector<viscluster_t> g_VisClusters;

// add to the list of brushes the merge leaves into single vis clusters
void AddVisCluster( entity_t *pFuncVisCluster )
{
	viscluster_t tmp;
	Vector clipMins, clipMaxs;
	clipMins[0] = clipMins[1] = clipMins[2] = MIN_COORD_INTEGER;
	clipMaxs[0] = clipMaxs[1] = clipMaxs[2] = MAX_COORD_INTEGER;

	// build the map brushes out into the minimum non-overlapping set of brushes
	bspbrush_t *pBSPBrush = MakeBspBrushList( pFuncVisCluster->firstbrush, pFuncVisCluster->firstbrush + pFuncVisCluster->numbrushes, 
		clipMins, clipMaxs, NO_DETAIL);
	tmp.pBrushes = ChopBrushes( pBSPBrush );

	// store the entry in the list
	tmp.clusterIndex = -1;
	tmp.leafCount = 0;
	tmp.leafStart = 0;

#if DEBUG_VISUALIZE_CLUSTERS
	int debug = atoi(ValueForKey(pFuncVisCluster,"debug"));
	if ( debug )
	{
		Msg("Debug vis cluster %d\n", g_VisClusters.Count() );
	}
#endif

	g_VisClusters.AddToTail( tmp );
	
	// clear out this entity so it won't get written to the bsp
	pFuncVisCluster->epairs = NULL;
	pFuncVisCluster->numbrushes = 0;
}

// returns the total overlapping volume of intersection between the node and the brush list
float VolumeOfIntersection( bspbrush_t *pBrushList, node_t *pNode )
{
	float volume = 0.0f;
	for ( bspbrush_t *pBrush = pBrushList; pBrush; pBrush = pBrush->next )
	{
		if ( IsBoxIntersectingBox( pNode->mins, pNode->maxs, pBrush->mins, pBrush->maxs ) )
		{
			bspbrush_t *pIntersect = IntersectBrush( pNode->volume, pBrush );
			if ( pIntersect )
			{
				volume += BrushVolume( pIntersect );
				FreeBrush( pIntersect );
			}
		}
	}

	return volume;
}

// Search for a forced cluster that this node is within
// NOTE: Returns the first one found, these won't merge themselves together
int GetVisCluster( node_t *pNode )
{
	float maxVolume = BrushVolume(pNode->volume) * 0.10f;		// needs to cover at least 10% of the volume to overlap
	int maxIndex = -1;
	// UNDONE: This could get slow
	for ( int i = g_VisClusters.Count(); --i >= 0; )
	{
		float volume = VolumeOfIntersection( g_VisClusters[i].pBrushes, pNode );
		if ( volume > maxVolume )
		{
			volume = maxVolume;
			maxIndex = i;
		}
	}
	return maxIndex;
}
/*
================
NumberLeafs_r
================
*/
void BuildVisLeafList_r (node_t *node, CUtlVector<node_t *> &leaves)
{
	if (node->planenum != PLANENUM_LEAF)
	{	// decision node
		node->cluster = -99;
		BuildVisLeafList_r (node->children[0], leaves);
		BuildVisLeafList_r (node->children[1], leaves);
		return;
	}
	
	if ( node->contents & CONTENTS_SOLID )
	{	// solid block, viewpoint never inside
		node->cluster = -1;
		return;
	}
	leaves.AddToTail(node);
}

// Give each leaf in the list of empty leaves a vis cluster number
// some are within func_viscluster volumes and will be merged together
// every other leaf gets its own unique number
void NumberLeafs( const CUtlVector<node_t *> &leaves )
{
	for ( int i = 0; i < leaves.Count(); i++ )
	{
		node_t *node = leaves[i];
		int visCluster = GetVisCluster( node );
		if ( visCluster >= 0 )
		{
			if ( g_VisClusters[visCluster].clusterIndex < 0 )
			{
				g_VisClusters[visCluster].clusterIndex = num_visclusters;
				num_visclusters++;
			}
			g_VisClusters[visCluster].leafCount++;
			node->cluster = g_VisClusters[visCluster].clusterIndex;
		}
		else
		{
			if ( !g_bSkyVis && Is3DSkyboxArea( node->area ) )
			{
				if ( g_SkyCluster < 0 )
				{
					// allocate a cluster for the sky
					g_SkyCluster = num_visclusters;
					num_visclusters++;
				}
				node->cluster = g_SkyCluster;
			}
			else
			{
				node->cluster = num_visclusters;
				num_visclusters++;
			}
		}
	}

#if DEBUG_VISUALIZE_CLUSTERS
	for ( int i = 0; i < g_VisClusters.Count(); i++ )
	{
		char name[256];
		sprintf(name, "u:\\main\\game\\ep2\\maps\\vis_%02d.gl", i );
		FileHandle_t fp = g_pFileSystem->Open( name, "w" );
		Msg("Writing %s\n", name );
		for ( bspbrush_t *pBrush = g_VisClusters[i].pBrushes; pBrush; pBrush = pBrush->next )
		{
			for (int i =  0; i < pBrush->numsides; i++ )
				OutputWindingColor( pBrush->sides[i].winding, fp, 0, 255, 0 );
		}
		for ( int j = 0; j < leaves.Count(); j++ )
		{
			if ( leaves[j]->cluster == g_VisClusters[i].clusterIndex )
			{
				bspbrush_t *pBrush = leaves[j]->volume;
				for (int k = 0; k < pBrush->numsides; k++ )
					OutputWindingColor( pBrush->sides[k].winding, fp, 64 + (j&31), 64, 64 - (j&31) );
			}
		}
		g_pFileSystem->Close(fp);
	}
#endif
}

// build a list of all vis portals that connect clusters
int BuildPortalList( CUtlVector<cluster_portals_t> &portalList, const CUtlVector<node_t *> &leaves )
{
	int portalCount = 0;
	for ( int i = 0; i < leaves.Count(); i++ )
	{
		node_t *node = leaves[i];
		// count the portals
		for (portal_t *p = node->portals ; p ; )
		{
			if (p->nodes[0] == node)		// only write out from first leaf
			{
				if ( p->nodes[0]->cluster != p->nodes[1]->cluster )
				{
					if (Portal_VisFlood (p))
					{
						portalCount++;
						portalList[node->cluster].portals.AddToTail(p);
					}
				}
				p = p->next[0];
			}
			else
				p = p->next[1];		
		}
	}
	return portalCount;
}


/*
================
CreateVisPortals_r
================
*/
void CreateVisPortals_r (node_t *node)
{
	// stop as soon as we get to a leaf
	if (node->planenum == PLANENUM_LEAF )
		return;

	MakeNodePortal (node);
	SplitNodePortals (node);

	CreateVisPortals_r (node->children[0]);
	CreateVisPortals_r (node->children[1]);
}

int		clusterleaf;
void SaveClusters_r (node_t *node)
{
	if (node->planenum == PLANENUM_LEAF)
	{
		dleafs[clusterleaf++].cluster = node->cluster;
		return;
	}
	SaveClusters_r (node->children[0]);
	SaveClusters_r (node->children[1]);
}

/*
================
WritePortalFile
================
*/
void WritePortalFile (tree_t *tree)
{
	char	filename[1024];
	node_t *headnode;
	int start = Plat_FloatTime();

	qprintf ("--- WritePortalFile ---\n");

	sprintf (filename, "%s.prt", source);
	Msg ("writing %s...", filename);

	headnode = tree->headnode;

	FreeTreePortals_r (headnode);
	MakeHeadnodePortals (tree);

	CreateVisPortals_r (headnode);

// set the cluster field in every leaf and count the total number of portals
	num_visclusters = 0;
	Msg("Building visibility clusters...\n");
	CUtlVector<node_t *> leaves;
	BuildVisLeafList_r( headnode, leaves );

	NumberLeafs (leaves);
	CUtlVector<cluster_portals_t> portalList;
	portalList.SetCount( num_visclusters );
	num_visportals = BuildPortalList( portalList, leaves );
// write the file
	FILE *pf = fopen (filename, "w");
	if (!pf)
		Error ("Error opening %s", filename);
		
	fprintf (pf, "%s\n", PORTALFILE);
	fprintf (pf, "%i\n", num_visclusters);
	fprintf (pf, "%i\n", num_visportals);

	qprintf ("%5i visclusters\n", num_visclusters);
	qprintf ("%5i visportals\n", num_visportals);

	WritePortalFile(pf, portalList);

	fclose (pf);

	// we need to store the clusters out now because ordering
	// issues made us do this after writebsp...
	clusterleaf = 1;
	SaveClusters_r (headnode);

	Msg("done (%d)\n", (int)(Plat_FloatTime() - start) );
}