summaryrefslogtreecommitdiff
path: root/hammer/mapsidelist.cpp
blob: 2073e05a9182401d1ee9c9bbf8476af77e635a4f (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Implements a helper that manages a single keyvalue of type "side"
//			or "sidelist" for the entity that is its parent.
//
//=============================================================================//

#include "stdafx.h"
#include "fgdlib/HelperInfo.h"
#include "materialsystem/imesh.h"
#include "MapClass.h"
#include "MapSolid.h"
#include "MapWorld.h"			// For the world's face ID functions.
#include "MapSideList.h"
#include "Material.h"
#include "Render3D.h"
#include "mapdoc.h"

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

IMPLEMENT_MAPCLASS(CMapSideList);


//-----------------------------------------------------------------------------
// Purpose: Factory function. Used for creating a CMapSideList from a set
//			of string parameters from the FGD file.
// Input  : pInfo - Pointer to helper info class which gives us information
//				about how to create the class.
// Output : Returns a pointer to the class, NULL if an error occurs.
//-----------------------------------------------------------------------------
CMapClass *CMapSideList::CreateMapSideList(CHelperInfo *pHelperInfo, CMapEntity *pParent)
{
	CMapSideList *pSideList = NULL;

	const char *pszParam = pHelperInfo->GetParameter(0);
	if (pszParam != NULL)
	{
		pSideList = new CMapSideList(pszParam);
	}
	else
	{
		pSideList = new CMapSideList("sides");
	}

	return(pSideList);
}


//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CMapSideList::CMapSideList(void)
{
	m_szKeyName[0] = '\0';
}


//-----------------------------------------------------------------------------
// Purpose: Constructor with key name.
//-----------------------------------------------------------------------------
CMapSideList::CMapSideList(char const *pszKeyName)
{
	strcpy( m_szKeyName, pszKeyName );
}


//-----------------------------------------------------------------------------
// Purpose: Destructor.
//-----------------------------------------------------------------------------
CMapSideList::~CMapSideList(void)
{
}


//-----------------------------------------------------------------------------
// Gets the keyvalue from our parent entity and rebuilds the face list from that.
//-----------------------------------------------------------------------------
void CMapSideList::RebuildFaceList()
{
	CMapWorld *pWorld = GetWorldObject(this);
	if (pWorld == NULL)
	{
		return;
	}

	CMapEntity *pParent = dynamic_cast <CMapEntity *>(GetParent());
	const char *pszValue = pParent->GetKeyValue(m_szKeyName);
	if (pszValue != NULL)
	{
		BuildFaceListForValue(pszValue, pWorld);
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pszValue - 
//			pWorld - The world object that we are contained in.
//-----------------------------------------------------------------------------
void CMapSideList::BuildFaceListForValue(char const *pszValue, CMapWorld *pWorld)
{
	CMapFaceList NewFaces;
	pWorld->FaceID_StringToFaceLists(&NewFaces, NULL, pszValue);

	//
	// Detach from the faces that are not in the new list. Go
	// in reverse order since we are removing items as we go.
	//
	if (m_Faces.Count() > 0)
	{
		for (int i = m_Faces.Count() - 1; i >= 0; i--)
		{
			CMapFace *pFace = m_Faces.Element(i);
			Assert(pFace != NULL);
			if ((pFace != NULL) && (NewFaces.Find(pFace) == -1))
			{
				CMapSolid *pSolid = (CMapSolid *)pFace->GetParent();
				UpdateDependency(pSolid, NULL);
				m_Faces.FastRemove(i);
			}
		}
	}

	//
	// Attach to the faces that are not in the old list.
	//
	for (int i = 0; i < NewFaces.Count(); i++)
	{
		CMapFace *pFace = NewFaces.Element(i);
		Assert(pFace != NULL);

		if ((pFace != NULL) && (m_Faces.Find(pFace) == -1))
		{
			CMapSolid *pSolid = (CMapSolid *)pFace->GetParent();
			UpdateDependency(NULL, pSolid);
			m_Faces.AddToTail(pFace);
		}
	}

	CalcBounds();
}


//-----------------------------------------------------------------------------
// Purpose: Calculates our bounds.
// Input  : bFullUpdate - 
//-----------------------------------------------------------------------------
void CMapSideList::CalcBounds(BOOL bFullUpdate)
{
	//
	// We're just a point in the 2D view because we don't render there.
	//
	m_Render2DBox.ResetBounds();
	m_Render2DBox.UpdateBounds(m_Origin);

	//
	// Our culling bounds includes the endpoints of all the lines we draw when
	// our parent entity is selected.
	//
	m_CullBox.ResetBounds();
	m_CullBox.UpdateBounds(m_Origin);

	for (int i = 0; i < m_Faces.Count(); i++)
	{
		CMapFace *pFace = m_Faces.Element(i);

		Vector Center;
		pFace->GetCenter(Center);
		m_CullBox.UpdateBounds(Center);
	}
	m_BoundingBox = m_CullBox;
}


//-----------------------------------------------------------------------------
// Purpose: Returns a copy of this object.
// Input  : bUpdateDependencies - Whether the new object should link to any
//				other objects in the world when it copies pointers.
//-----------------------------------------------------------------------------
CMapClass *CMapSideList::Copy(bool bUpdateDependencies)
{
	CMapSideList *pCopy = new CMapSideList;
	if (pCopy != NULL)
	{
		pCopy->CopyFrom(this, bUpdateDependencies);
	}
	return(pCopy);
}


//-----------------------------------------------------------------------------
// Purpose: Turns us into an exact copy of the given object.
// Input  : pFrom - Object to copy.
// Input  : bUpdateDependencies - Whether we should link to any other objects
//				in the world when we copy pointers.
//-----------------------------------------------------------------------------
CMapClass *CMapSideList::CopyFrom(CMapClass *pOther, bool bUpdateDependencies)
{
	CMapSideList *pFrom = dynamic_cast <CMapSideList *>(pOther);
	Assert(pFrom != NULL);

	CMapClass::CopyFrom(pOther, bUpdateDependencies);

	strcpy(m_szKeyName, pFrom->m_szKeyName);
	m_Faces = pFrom->m_Faces;

	if (bUpdateDependencies)
	{
		for (int i = 0; i < m_Faces.Count(); i++)
		{
			CMapFace *pFace = m_Faces.Element(i);
			CMapSolid *pSolid = (CMapSolid *)pFace->GetParent();
			UpdateDependency(pSolid, NULL);
		}
	}

	return(this);
}


//-----------------------------------------------------------------------------
// Purpose: Searches for a face with the given unique ID in the list of objects.
//			FIXME: should this be in CMapObjectList?
// Input  : nFaceID - Face ID to search for.
//			List - List of objects to search.
// Output : Returns the face with the given ID if it was found, NULL if not.
//-----------------------------------------------------------------------------
CMapFace *CMapSideList::FindFaceIDInList(int nFaceID, const CMapObjectList &List)
{
	FOR_EACH_OBJ( List, pos )
	{
		//
		// If this object is a solid, look for the face there.
		//
		CMapClass *pObject = List.Element(pos);
		CMapSolid *pSolid = dynamic_cast <CMapSolid *>(pObject);
		if (pSolid != NULL)
		{
			CMapFace *pFace = pSolid->FindFaceID(nFaceID);
			if (pFace != NULL)
			{
				return(pFace);
			}
		}

		//
		// Check all of this object's solid children.
		//
		EnumChildrenPos_t pos2;
		CMapClass *pChild = pObject->GetFirstDescendent(pos2);
		while (pChild != NULL)
		{
			pSolid = dynamic_cast <CMapSolid *>(pChild);
			if (pSolid != NULL)
			{
				CMapFace *pFace = pSolid->FindFaceID(nFaceID);
				if (pFace != NULL)
				{
					return(pFace);
				}
			}	

			pChild = pObject->GetNextDescendent(pos2);
		}
	}

	return(NULL);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the total approximate memory consumed by this object.
//-----------------------------------------------------------------------------
size_t CMapSideList::GetSize(void)
{
	return(sizeof(this) + m_Faces.Count() * sizeof(CMapFace *));
}


//-----------------------------------------------------------------------------
// Purpose: Notification that we have just been cloned. Fix up our clone's
//			face list based on the the objects that were cloned with it.
// Input  : pClone - 
//			OriginalList - 
//			NewList - 
//-----------------------------------------------------------------------------
void CMapSideList::OnClone(CMapClass *pCloneObject, CMapWorld *pWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList)
{
	ReplaceFacesInCopy((CMapSideList *)pCloneObject, OriginalList, NewList);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pCopy - 
//			pSourceWorld - 
//			pDestWorld - 
//			OriginalList - 
//			NewList - 
//-----------------------------------------------------------------------------
void CMapSideList::OnPaste(CMapClass *pCopyObject, CMapWorld *pSourceWorld, CMapWorld *pDestWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList)
{
	CMapSideList *pCopy = (CMapSideList *)pCopyObject;

	//
	// HACK: This is kinda nasty. Build a list of face IDs from our parent keyvalue so
	// we can remap them to faces in the clipboard.
	//
	CMapEntity *pParent = dynamic_cast <CMapEntity *>(pCopy->GetParent());
	const char *pszValue = pParent->GetKeyValue(m_szKeyName);
	if (pszValue != NULL)
	{
		char szVal[KEYVALUE_MAX_VALUE_LENGTH];
		strcpy(szVal, pszValue);

		char *psz = strtok(szVal, " ");
		while (psz != NULL)
		{
			//
			// The substring should now be a single face ID. Get the corresponding
			// face and add it to the list.
			//
			int nFaceID = atoi(psz);
			CMapFace *pFace = FindFaceIDInList(nFaceID, OriginalList);
			if (pFace != NULL)
			{
				pCopy->m_Faces.AddToTail(pFace);
			}

			//
			// Get the next substring.
			//
			psz = strtok(NULL, " ");
		}
	}

	//
	// Now replace all faces with their new counterparts, as in Clone.
	//
	ReplaceFacesInCopy(pCopy, OriginalList, NewList);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pObject - 
//			eNotifyType - 
//-----------------------------------------------------------------------------
void CMapSideList::OnNotifyDependent(CMapClass *pObject, Notify_Dependent_t eNotifyType)
{
	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	if ( pDoc->IsLoading() )
		return;

	if ( eNotifyType == Notify_Changed )
	{
		// Remove? Purge?
		m_Faces.RemoveAll();
		
		// Rebuild the face list 
		RebuildFaceList();
	}

	if (eNotifyType == Notify_Removed || eNotifyType == Notify_Clipped)
	{
		//
		// Check for a solid that we refer to via face ID going away.
		//
		CMapSolid *pSolid = dynamic_cast<CMapSolid *>(pObject);
		if ((pSolid != NULL) && (m_Faces.Count() > 0))
		{
			//
			// Remove faces from our list that are in this solid.
			// Do it backwards so we can remove them as we go. Also, add
			// the face IDs to our list of lost IDs so that we can reacquire
			// the face in our list if the solid comes back later.
			//
			for (int i = m_Faces.Count() - 1; i >= 0; i--)
			{
				CMapFace *pFace = m_Faces.Element(i);
				if (pFace != NULL)
				{
					CMapSolid *pParent = (CMapSolid *)pFace->GetParent();
					if (pParent == pSolid)
					{
						m_LostFaceIDs.AddToTail(pFace->GetFaceID());
						m_Faces.FastRemove(i);
					}
				}
			}
		
			//
			// Submit the updated face list to our parent entity.
			//
			UpdateParentKey();
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : key - 
//			value - 
//-----------------------------------------------------------------------------
void CMapSideList::OnParentKeyChanged(char const *pszKey, char const *pszValue)
{
	CMapWorld *pWorld = GetWorldObject(this);
	if (pWorld == NULL)
	{
		// We're probably being copied into the clipboard.
		return;
	}

	//
	// Update our face list if the key we care about is changing.
	//
	if (!stricmp(pszKey, m_szKeyName))
	{
		BuildFaceListForValue(pszValue, pWorld);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Called just after this object has been removed from the world so
//			that it can unlink itself from other objects in the world.
// Input  : pWorld - The world that we were just removed from.
//			bNotifyChildren - Whether we should forward notification to our children.
//-----------------------------------------------------------------------------
void CMapSideList::OnRemoveFromWorld(CMapWorld *pWorld, bool bNotifyChildren)
{
	CMapClass::OnRemoveFromWorld(pWorld, bNotifyChildren);

	for (int i = 0; i < m_Faces.Count(); i++)
	{
		CMapFace *pFace = m_Faces.Element(i);
		CMapSolid *pSolid = (CMapSolid *)pFace->GetParent();
		UpdateDependency(pSolid, NULL);
	}
	
	m_Faces.RemoveAll();
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : List - 
//-----------------------------------------------------------------------------
void CMapSideList::RemoveFacesNotInList(const CMapObjectList &List)
{
	if (m_Faces.Count() > 0)
	{
		for (int i = m_Faces.Count() - 1; i >= 0; i--)
		{
			CMapFace *pFace = m_Faces.Element(i);

			if (FindFaceIDInList(pFace->GetFaceID(), List) == NULL)
			{
				CMapSolid *pSolid = (CMapSolid *)pFace->GetParent();
				UpdateDependency(pSolid, NULL);
				m_Faces.FastRemove(i);
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Called from OnClone and OnPaste. Replaces references (in the
//			cloned object) to faces in the original list of objects with references
//			to corresponding faces in the new list of objects.
// Input  : pClone - 
//			OriginalList - 
//			NewList - 
//-----------------------------------------------------------------------------
void CMapSideList::ReplaceFacesInCopy(CMapSideList *pCopy, const CMapObjectList &OriginalList, CMapObjectList &NewList)
{
	Assert( OriginalList.Count() == NewList.Count() );
	
	FOR_EACH_OBJ( OriginalList, pos )
	{
		CMapClass *pOriginal = OriginalList.Element(pos);
		CMapClass *pNew = NewList.Element(pos);

		if (pOriginal != this)
		{
			//
			// Check to see if these two objects are solids.
			//
			CMapSolid *pOrigSolid = dynamic_cast <CMapSolid *>(pOriginal);
			if (pOrigSolid != NULL)
			{
				CMapSolid *pNewSolid = dynamic_cast <CMapSolid *>(pNew);
				Assert(pNewSolid != NULL);

				if (pNewSolid != NULL)
				{
					pCopy->ReplaceSolidFaces(pOrigSolid, pNewSolid);
				}
			}

			//
			// Check all of these objects' children.
			//
			EnumChildrenPos_t e1;
			EnumChildrenPos_t e2;

			CMapClass *pOrigChild = pOriginal->GetFirstDescendent(e1);
			CMapClass *pNewChild = pNew->GetFirstDescendent(e2);

			while (pOrigChild != NULL)
			{
				Assert(pNewChild != NULL);

				pOrigSolid = dynamic_cast <CMapSolid *>(pOrigChild);
				if (pOrigSolid != NULL)
				{
					CMapSolid *pNewSolid = dynamic_cast <CMapSolid *>(pNewChild);
					Assert(pNewSolid != NULL);

					if (pNewSolid != NULL)
					{
						pCopy->ReplaceSolidFaces(pOrigSolid, pNewSolid);
					}
				}

				pOrigChild = pOriginal->GetNextDescendent(e1);
				pNewChild = pNew->GetNextDescendent(e2);
			}
		}
	}
	
	//
	// Update the keyvalue in the copy's parent entity.
	//
	pCopy->UpdateParentKey();
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pRender - Interface to use for rendering.
//-----------------------------------------------------------------------------
void CMapSideList::Render2D(CRender2D *pRender)
{
}


//-----------------------------------------------------------------------------
// Purpose: Renders us in the 3D view.
// Input  : pRender - Interface to use for rendering.
//-----------------------------------------------------------------------------
void CMapSideList::Render3D(CRender3D *pRender)
{
	if ( !m_pParent->IsSelected() )
		return;
	
	//
	// Draw lines from us to the center of all faces in the list.
	//
	pRender->PushRenderMode(RENDER_MODE_WIREFRAME);

	CMeshBuilder meshBuilder;
	CMatRenderContextPtr pRenderContext( MaterialSystemInterface() );
	IMesh* pMesh = pRenderContext->GetDynamicMesh();

	meshBuilder.Begin(pMesh, MATERIAL_LINES, m_Faces.Count());

	for (int i = 0; i < m_Faces.Count(); i++)
	{
		CMapFace *pFace = m_Faces.Element(i);

		Vector Center;
		pFace->GetCenter(Center);

		unsigned char color[3];
		color[0] = SELECT_EDGE_RED; 
		color[1] = SELECT_EDGE_GREEN;
		color[2] = SELECT_EDGE_BLUE;

		meshBuilder.Color3ubv( color );
		meshBuilder.Position3f(m_Origin.x, m_Origin.y, m_Origin.z);
		meshBuilder.AdvanceVertex();

		meshBuilder.Color3ubv( color );
		meshBuilder.Position3f(Center.x, Center.y, Center.z);
		meshBuilder.AdvanceVertex();
	}

	meshBuilder.End();
	pMesh->Draw();

	pRender->PopRenderMode();
}


//-----------------------------------------------------------------------------
// Purpose: Called from OnClone and OnPaste, updates references to face IDs 
//			in the one solid with references to corresponding face IDs in
//			another solid.
// Input  : pOrigSolid - Solid with faces to find.
//			pNewSolid - Solid with faces to replace with.
// Output : Returns true if it replaced at least one face.
//-----------------------------------------------------------------------------
bool CMapSideList::ReplaceSolidFaces(CMapSolid *pOrigSolid, CMapSolid *pNewSolid)
{
	bool bDidSomething = false;
	for (int i = 0; i < pOrigSolid->GetFaceCount(); i++)
	{
		CMapFace *pFace = pOrigSolid->GetFace(i);

		int nIndex = m_Faces.FindFaceID(pFace->GetFaceID());
		if (nIndex != -1)
		{
			//
			// Replace the element in our face list and unlink
			// us from the original solid, relinking us to the new solid.
			//
			m_Faces.Element(nIndex) = pNewSolid->GetFace(i);
			UpdateDependency(pOrigSolid, pNewSolid);
			bDidSomething = true;
		}
	}

	return(bDidSomething);
}


//-----------------------------------------------------------------------------
// Purpose: Something happened in the world that requires us to refresh our
//			dependencies. Try to reacquire face IDs in our deleted faces list.
// Input  : pWorld - 
//-----------------------------------------------------------------------------
void CMapSideList::UpdateDependencies(CMapWorld *pWorld, CMapClass *pObject)
{
	CMapClass::UpdateDependencies(pWorld, pObject);

	//
	// See if it is a solid that holds faces in our lost faces list.
	//
	CMapSolid *pSolid = dynamic_cast <CMapSolid *>(pObject);
	if ((pSolid != NULL) && (m_LostFaceIDs.Count() > 0))
	{
		//
		// Walk the list backwards so we can remove as we go.
		//
		for (int i = m_LostFaceIDs.Count() - 1; i >= 0; i--)
		{
			int nFaceID = m_LostFaceIDs.Element(i);

			CMapFace *pFace = pSolid->FindFaceID(nFaceID);
			if (pFace != NULL)
			{
				if (m_Faces.Find(pFace) == -1)
				{
					m_Faces.AddToTail(pFace);
				}

				//
				// We've reacquired the face, so it's no longer lost.
				//
				m_LostFaceIDs.FastRemove(i);
			}
		}

		UpdateParentKey();
	}
}


//-----------------------------------------------------------------------------
// Purpose: Builds a new value string for our parent's facelist key from our
//			current list of faces and submits the keyvalue to our parent for
//			storage.
//-----------------------------------------------------------------------------
void CMapSideList::UpdateParentKey(void)
{
	char szValue[KEYVALUE_MAX_VALUE_LENGTH];
	CMapWorld::FaceID_FaceListsToString(szValue, sizeof(szValue), &m_Faces, NULL);

	CMapEntity *pEntity = dynamic_cast<CMapEntity *>(m_pParent);
	if (pEntity != NULL)
	{
		pEntity->NotifyChildKeyChanged(this, m_szKeyName, szValue);
	}
}