summaryrefslogtreecommitdiff
path: root/hammer/mapquadbounds.cpp
blob: e98d5a79cb6ce47bdde21522a9cbd30bc97a9277 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Implements an entity helper that extracts the bounds of a non-nodraw
//			face from a solid sibling, saving them as keyvalues in the entity.
//
//=============================================================================//

#include "stdafx.h"
#include "Box3D.h"
#include "fgdlib/HelperInfo.h"
#include "MapQuadBounds.h"
#include "mathlib/MathLib.h"
#include "Render3D.h"
#include "material.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/imesh.h"
#include "mapsolid.h"
#include "mapentity.h"

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


IMPLEMENT_MAPCLASS(CMapQuadBounds)


#define QUAD_ERR_NONE		0
#define QUAD_ERR_MULT_FACES 1
#define QUAD_ERR_NOT_QUAD	2


//-----------------------------------------------------------------------------
// Purpose: Factory function. Used for creating a CMapQuadBounds helper 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 helper.
// Output : Returns a pointer to the helper, NULL if an error occurs.
//-----------------------------------------------------------------------------
CMapClass *CMapQuadBounds::CreateQuadBounds(CHelperInfo *pHelperInfo, CMapEntity *pParent)
{
	CMapQuadBounds* pQuadBounds = new CMapQuadBounds;
	return(pQuadBounds);
}


//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CMapQuadBounds::CMapQuadBounds(void)
{
	m_vLowerLeft.Init();
	m_vUpperLeft.Init();
	m_vLowerRight.Init();
	m_vUpperRight.Init();
}


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


//------------------------------------------------------------------------------
// Purpose: Before saving, fill in my parent entity's keys with the bounds of
//			a non-nodraw face from a sibling solid.
//------------------------------------------------------------------------------
void CMapQuadBounds::PresaveWorld(void) 
{

	CMapEntity *pMapEntity = dynamic_cast<CMapEntity*>(GetParent());

	if (!pMapEntity)
	{
		return;
	}
	CMapSolid *pSolid = pMapEntity->GetChildOfType((CMapSolid*)NULL);

	if (pSolid)
	{
		int		nFaces = pSolid->GetFaceCount();
		bool	bFound = false;
		for (int i = 0; i < nFaces; i++)
		{
			//
			// Look for face with 4 points that isn't no draw
			//
			CMapFace *pFace = pSolid->GetFace(i);

			char szCurrentTexture[MAX_PATH];
			pFace->GetTextureName(szCurrentTexture);
			int nPoints = pFace->GetPointCount();

			// Ignore no draw surfaces
			if (stricmp(szCurrentTexture, "tools/toolsnodraw"))
			{
				if (bFound)
				{
					m_nError = QUAD_ERR_MULT_FACES;
				}
				else if (nPoints != 4)
				{
					m_nError = QUAD_ERR_NOT_QUAD;
				}
				else
				{
					Vector vLowerLeft,vUpperLeft,vLowerRight,vUpperRight;
					pFace->GetPoint(m_vLowerLeft, 0);
					pFace->GetPoint(m_vLowerRight,1);
					pFace->GetPoint(m_vUpperRight,2);
					pFace->GetPoint(m_vUpperLeft, 3);
					bFound	 = true;
					m_nError = QUAD_ERR_NONE;
				}
			}
		} 

		static char buf[64];
		sprintf( buf, "%g %g %g", (double)m_vLowerLeft[0], (double)m_vLowerLeft[1], (double)m_vLowerLeft[2] );
		pMapEntity->SetKeyValue( "lowerleft", buf );

		sprintf( buf, "%g %g %g", (double)m_vUpperLeft[0], (double)m_vUpperLeft[1], (double)m_vUpperLeft[2] );
		pMapEntity->SetKeyValue( "upperleft", buf );

		sprintf( buf, "%g %g %g", (double)m_vLowerRight[0], (double)m_vLowerRight[1], (double)m_vLowerRight[2] );
		pMapEntity->SetKeyValue( "lowerright", buf );

		sprintf( buf, "%g %g %g", (double)m_vUpperRight[0], (double)m_vUpperRight[1], (double)m_vUpperRight[2] );
		pMapEntity->SetKeyValue( "upperright", buf );

		sprintf( buf, "%i", m_nError);
		pMapEntity->SetKeyValue( "error", buf );

	}
}


//-----------------------------------------------------------------------------
// Purpose: Returns an exact copy of this object.
//-----------------------------------------------------------------------------
CMapClass *CMapQuadBounds::Copy(bool bUpdateDependencies)
{
	CMapQuadBounds *pCopy = new CMapQuadBounds;

	if (pCopy != NULL)
	{
		pCopy->CopyFrom(this, bUpdateDependencies);
	}

	return(pCopy);
}



//-----------------------------------------------------------------------------
// Purpose: Makes this an exact duplicate of pObject.
// Input  : pObject - Object to copy.
// Output : Returns this.
//-----------------------------------------------------------------------------
CMapClass *CMapQuadBounds::CopyFrom(CMapClass *pObject, bool bUpdateDependencies)
{
	Assert(pObject->IsMapClass(MAPCLASS_TYPE(CMapQuadBounds)));
	CMapQuadBounds *pFrom = (CMapQuadBounds *)pObject;

	CMapClass::CopyFrom(pObject, bUpdateDependencies);

	m_vLowerLeft	= pFrom->m_vLowerLeft;
	m_vUpperLeft	= pFrom->m_vUpperLeft;
	m_vLowerRight	= pFrom->m_vLowerRight;
	m_vUpperRight	= pFrom->m_vUpperRight;
	m_nError		= pFrom->m_nError;

	return(this);
}