summaryrefslogtreecommitdiff
path: root/hammer/mapoverlaytrans.cpp
blob: 27050cb4dc08a760b95d1af466488f09cdad32e2 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//
//=============================================================================

#include <stdafx.h>
#include "MapEntity.h"
#include "MapOverlayTrans.h"
#include "DispShore.h"
#include "TextureSystem.h"
#include "ChunkFile.h"

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

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CMapOverlayTransition::CMapOverlayTransition()
{
	m_ShoreData.m_pTexture = NULL;
	m_ShoreData.m_vecLengthTexcoord.Init();
	m_ShoreData.m_vecWidthTexcoord.Init();
	m_ShoreData.m_flWidths[0] = 0.0f;
	m_ShoreData.m_flWidths[1] = 0.0f;
	m_bIsWater = true;
	m_aFaceCache1.Purge();
	m_aFaceCache2.Purge();
	m_bDebugDraw = false;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CMapOverlayTransition::~CMapOverlayTransition()
{
}

//-----------------------------------------------------------------------------
// Purpose:
// NOTE: static
//-----------------------------------------------------------------------------
CMapClass *CMapOverlayTransition::Create( CHelperInfo *pInfo, CMapEntity *pParent )
{
	CMapOverlayTransition *pOverlayTrans = new CMapOverlayTransition;
	return pOverlayTrans;
}

//-----------------------------------------------------------------------------
// Purpose: Called after the entire map has been loaded. This allows the object
//			to perform any linking with other map objects or to do other operations
//			that require all world objects to be present.
// Input  : pWorld - The world that we are in.
//-----------------------------------------------------------------------------
void CMapOverlayTransition::PostloadWorld( CMapWorld *pWorld )
{
	OnApply();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::CalcBounds( BOOL bFullUpdate )
{
	CMapClass::CalcBounds( bFullUpdate );

	Shoreline_t *pShoreline = GetShoreManager()->GetShoreline( ( int )GetParent() );
	if ( pShoreline )
	{
		Vector vecMins( 99999.0f, 99999.0f, 99999.0f );
		Vector vecMaxs( -99999.0f, -99999.0f, -99999.0f );

		m_Render2DBox.ResetBounds();
		m_CullBox.ResetBounds();

		int nSegmentCount = pShoreline->m_aSegments.Count();
		for ( int iSegment = 0; iSegment < nSegmentCount; ++iSegment )
		{
			for ( int iAxis = 0; iAxis < 3; ++iAxis )
			{
				if ( pShoreline->m_aSegments[iSegment].m_vecPoints[0][iAxis] < vecMins[iAxis] )
				{
					vecMins[iAxis] = pShoreline->m_aSegments[iSegment].m_vecPoints[0][iAxis];
				}

				if ( pShoreline->m_aSegments[iSegment].m_vecPoints[1][iAxis] < vecMins[iAxis] )
				{
					vecMins[iAxis] = pShoreline->m_aSegments[iSegment].m_vecPoints[1][iAxis];
				}

				if ( pShoreline->m_aSegments[iSegment].m_vecPoints[0][iAxis] > vecMaxs[iAxis] )
				{
					vecMaxs[iAxis] = pShoreline->m_aSegments[iSegment].m_vecPoints[0][iAxis];
				}

				if ( pShoreline->m_aSegments[iSegment].m_vecPoints[1][iAxis] > vecMaxs[iAxis] )
				{
					vecMaxs[iAxis] = pShoreline->m_aSegments[iSegment].m_vecPoints[1][iAxis];
				}
			}
		}

		m_Render2DBox.UpdateBounds( vecMins, vecMaxs );
		m_CullBox = m_Render2DBox;
		m_BoundingBox = m_CullBox;
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CMapClass *CMapOverlayTransition::Copy( bool bUpdateDependencies )
{
	CMapOverlayTransition *pCopy = new CMapOverlayTransition;
	if ( pCopy )
	{
		pCopy->CopyFrom( this, bUpdateDependencies );
	}

	return pCopy;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CMapClass *CMapOverlayTransition::CopyFrom( CMapClass *pObject, bool bUpdateDependencies )
{
	// Verify the object is of the correct type and cast.
	Assert( pObject->IsMapClass( MAPCLASS_TYPE( CMapOverlayTransition ) ) );
	CMapOverlayTransition *pFrom = ( CMapOverlayTransition* )pObject;
	if ( pFrom )
	{
		m_ShoreData.m_pTexture = pFrom->m_ShoreData.m_pTexture;
		m_ShoreData.m_vecLengthTexcoord = pFrom->m_ShoreData.m_vecLengthTexcoord;
		m_ShoreData.m_vecWidthTexcoord = pFrom->m_ShoreData.m_vecWidthTexcoord;
		m_ShoreData.m_flWidths[0] = pFrom->m_ShoreData.m_flWidths[0];
		m_ShoreData.m_flWidths[1] = pFrom->m_ShoreData.m_flWidths[1];
	}

	return this;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::OnParentKeyChanged( const char* szKey, const char* szValue )
{
	// Material data.
	if ( !stricmp( szKey, "material" ) )
	{
		IEditorTexture *pTex = g_Textures.FindActiveTexture( szValue );
		if ( pTex )
		{
			m_ShoreData.m_pTexture = pTex;
		}
	}

	// Texture data.
	if ( !stricmp( szKey, "LengthTexcoordStart" ) )	
	{ 
		m_ShoreData.m_vecLengthTexcoord[0] = atof( szValue );
	}
	if ( !stricmp( szKey, "LengthTexcoordEnd" ) )	
	{ 
		m_ShoreData.m_vecLengthTexcoord[1] = atof( szValue );
	}
	if ( !stricmp( szKey, "WidthTexcoordStart" ) )	
	{ 
		m_ShoreData.m_vecWidthTexcoord[0] = atof( szValue );
	}
	if ( !stricmp( szKey, "WidthTexcoordEnd" ) )	
	{ 
		m_ShoreData.m_vecWidthTexcoord[1] = atof( szValue );
	}

	// Width data.
	if ( !stricmp( szKey, "Width1" ) )	
	{ 
		m_ShoreData.m_flWidths[0] = atof( szValue );
	}
	if ( !stricmp( szKey, "Width2" ) )	
	{ 
		m_ShoreData.m_flWidths[1] = atof( szValue );
	}

	// Debug data.
	if ( !stricmp( szKey, "DebugDraw" ) )
	{
		m_bDebugDraw = true;
		if ( atoi( szValue ) == 0 )
		{
			m_bDebugDraw = false;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::OnNotifyDependent( CMapClass *pObject, Notify_Dependent_t eNotifyType )
{
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::OnAddToWorld( CMapWorld *pWorld )
{
	OnApply();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::OnRemoveFromWorld( CMapWorld *pWorld, bool bNotifyChildren )
{
	GetShoreManager()->RemoveShoreline( ( int )GetParent() );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::OnUndoRedo( void )
{
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::DoTransform( const VMatrix& matrix )
{
	return;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::OnPaste( CMapClass *pCopy, CMapWorld *pSourceWorld, CMapWorld *pDestWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList)
{
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::OnClone( CMapClass *pClone, CMapWorld *pWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList )
{
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMapOverlayTransition::Render3D( CRender3D *pRender )
{
	GetShoreManager()->Draw( pRender );
	if ( m_bDebugDraw )
	{
		GetShoreManager()->DebugDraw( pRender );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Generate a face list from the parent entities' side list child.
//-----------------------------------------------------------------------------
bool CMapOverlayTransition::BuildFaceCaches( void )
{
	CMapEntity *pEntity = dynamic_cast<CMapEntity*>( GetParent() );
	if ( pEntity )
	{
		const CMapObjectList *pChildren = pEntity->GetChildren();
		FOR_EACH_OBJ( *pChildren, pos )
		{
			CMapSideList *pSideList = dynamic_cast<CMapSideList*>( pChildren->Element(pos) );
			if ( pSideList )
			{
				// Check name.
				if ( !stricmp( "sides" ,pSideList->GetKeyName() ) )
				{
					int nFaceCount = pSideList->GetFaceCount();
					for ( int iFace = 0; iFace < nFaceCount; ++iFace )
					{
						m_aFaceCache1.AddToTail( pSideList->GetFace( iFace ) );
					}
				}
				else if ( !stricmp( "sides2", pSideList->GetKeyName() ) )
				{
					int nFaceCount = pSideList->GetFaceCount();
					for ( int iFace = 0; iFace < nFaceCount; ++iFace )
					{
						if ( m_bIsWater )
						{
							// Verify that the face is a water face.
							if ( pSideList->GetFace( iFace )->GetTexture()->IsWater() )
							{
								m_aFaceCache2.AddToTail( pSideList->GetFace( iFace ) );
							}
						}
						else
						{
							m_aFaceCache2.AddToTail( pSideList->GetFace( iFace ) );
						}
					}
				}
			}
		}
	}

	return ( ( m_aFaceCache1.Count() > 0 ) && ( m_aFaceCache2.Count() > 0 ) );
}

//-----------------------------------------------------------------------------
// Purpose: Create the transition overlays.
//-----------------------------------------------------------------------------
bool CMapOverlayTransition::OnApply( void )
{
	if ( m_bIsWater )
	{
		// Create the shoreline.
		if ( GetShoreManager()->Init() )
		{
			if ( BuildFaceCaches() )
			{
				m_nShorelineId = ( int )GetParent();

				GetShoreManager()->AddShoreline( m_nShorelineId );
				Shoreline_t *pShoreline = GetShoreManager()->GetShoreline( m_nShorelineId );
				pShoreline->m_ShoreData = m_ShoreData;

				GetShoreManager()->BuildShoreline( m_nShorelineId, m_aFaceCache1, m_aFaceCache2 );

				// Clean up the face list.
				m_aFaceCache1.Purge();
				m_aFaceCache2.Purge();
			}

			GetShoreManager()->Shutdown();
		}

		// Post updated.
		PostUpdate( Notify_Changed );

		return true;
	}
	else
	{
		// This part is not implemented yet!
		return true;
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapOverlayTransition::LoadVMF( CChunkFile *pFile )
{
	// This doesn't need to be implemented until we can "edit" the overlay data.  For
	// now just regenerate the data.

	return ChunkFile_Ok;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapOverlayTransition::SaveVMF( CChunkFile *pFile, CSaveInfo *pSaveInfo )
{
	ChunkFileResult_t eResult = pFile->BeginChunk("overlaytransition");

	m_nShorelineId = ( int )GetParent();
	Shoreline_t *pShoreline = GetShoreManager()->GetShoreline( m_nShorelineId );
	if ( pShoreline )
	{
		int nOverlayCount = pShoreline->m_aOverlays.Count();
		for ( int iOverlay = 0; iOverlay < nOverlayCount; ++iOverlay )
		{
			CMapOverlay *pOverlay = &pShoreline->m_aOverlays[iOverlay];
			if ( pOverlay )
			{
				pOverlay->SaveDataToVMF( pFile, pSaveInfo );
			}
		}
	} 

	if ( eResult == ChunkFile_Ok )
	{
		eResult = pFile->EndChunk();
	}

	return eResult;
}