Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implements an entity helper that extracts the bounds of a non-nodraw
  4. // face from a solid sibling, saving them as keyvalues in the entity.
  5. //
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include "Box3D.h"
  9. #include "fgdlib/HelperInfo.h"
  10. #include "MapQuadBounds.h"
  11. #include "mathlib/MathLib.h"
  12. #include "Render3D.h"
  13. #include "material.h"
  14. #include "materialsystem/imaterialsystem.h"
  15. #include "materialsystem/IMesh.h"
  16. #include "mapsolid.h"
  17. #include "mapentity.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include <tier0/memdbgon.h>
  20. IMPLEMENT_MAPCLASS(CMapQuadBounds)
  21. #define QUAD_ERR_NONE 0
  22. #define QUAD_ERR_MULT_FACES 1
  23. #define QUAD_ERR_NOT_QUAD 2
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Factory function. Used for creating a CMapQuadBounds helper from a
  26. // set of string parameters from the FGD file.
  27. // Input : *pInfo - Pointer to helper info class which gives us information
  28. // about how to create the helper.
  29. // Output : Returns a pointer to the helper, NULL if an error occurs.
  30. //-----------------------------------------------------------------------------
  31. CMapClass *CMapQuadBounds::CreateQuadBounds(CHelperInfo *pHelperInfo, CMapEntity *pParent)
  32. {
  33. CMapQuadBounds* pQuadBounds = new CMapQuadBounds;
  34. return(pQuadBounds);
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose: Constructor.
  38. //-----------------------------------------------------------------------------
  39. CMapQuadBounds::CMapQuadBounds(void)
  40. {
  41. m_vLowerLeft.Init();
  42. m_vUpperLeft.Init();
  43. m_vLowerRight.Init();
  44. m_vUpperRight.Init();
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose: Destructor.
  48. //-----------------------------------------------------------------------------
  49. CMapQuadBounds::~CMapQuadBounds(void)
  50. {
  51. }
  52. //------------------------------------------------------------------------------
  53. // Purpose: Before saving, fill in my parent entity's keys with the bounds of
  54. // a non-nodraw face from a sibling solid.
  55. //------------------------------------------------------------------------------
  56. void CMapQuadBounds::PresaveWorld(void)
  57. {
  58. CMapEntity *pMapEntity = dynamic_cast<CMapEntity*>(GetParent());
  59. if (!pMapEntity)
  60. {
  61. return;
  62. }
  63. CMapSolid *pSolid = pMapEntity->GetChildOfType((CMapSolid*)NULL);
  64. if (pSolid)
  65. {
  66. int nFaces = pSolid->GetFaceCount();
  67. bool bFound = false;
  68. for (int i = 0; i < nFaces; i++)
  69. {
  70. //
  71. // Look for face with 4 points that isn't no draw
  72. //
  73. CMapFace *pFace = pSolid->GetFace(i);
  74. char szCurrentTexture[MAX_PATH];
  75. pFace->GetTextureName(szCurrentTexture);
  76. int nPoints = pFace->GetPointCount();
  77. // Ignore no draw surfaces
  78. if (stricmp(szCurrentTexture, "tools/toolsnodraw"))
  79. {
  80. if (bFound)
  81. {
  82. m_nError = QUAD_ERR_MULT_FACES;
  83. }
  84. else if (nPoints != 4)
  85. {
  86. m_nError = QUAD_ERR_NOT_QUAD;
  87. }
  88. else
  89. {
  90. Vector vLowerLeft,vUpperLeft,vLowerRight,vUpperRight;
  91. pFace->GetPoint(m_vLowerLeft, 0);
  92. pFace->GetPoint(m_vLowerRight,1);
  93. pFace->GetPoint(m_vUpperRight,2);
  94. pFace->GetPoint(m_vUpperLeft, 3);
  95. bFound = true;
  96. m_nError = QUAD_ERR_NONE;
  97. }
  98. }
  99. }
  100. static char buf[64];
  101. sprintf( buf, "%g %g %g", (double)m_vLowerLeft[0], (double)m_vLowerLeft[1], (double)m_vLowerLeft[2] );
  102. pMapEntity->SetKeyValue( "lowerleft", buf );
  103. sprintf( buf, "%g %g %g", (double)m_vUpperLeft[0], (double)m_vUpperLeft[1], (double)m_vUpperLeft[2] );
  104. pMapEntity->SetKeyValue( "upperleft", buf );
  105. sprintf( buf, "%g %g %g", (double)m_vLowerRight[0], (double)m_vLowerRight[1], (double)m_vLowerRight[2] );
  106. pMapEntity->SetKeyValue( "lowerright", buf );
  107. sprintf( buf, "%g %g %g", (double)m_vUpperRight[0], (double)m_vUpperRight[1], (double)m_vUpperRight[2] );
  108. pMapEntity->SetKeyValue( "upperright", buf );
  109. sprintf( buf, "%i", m_nError);
  110. pMapEntity->SetKeyValue( "error", buf );
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose: Returns an exact copy of this object.
  115. //-----------------------------------------------------------------------------
  116. CMapClass *CMapQuadBounds::Copy(bool bUpdateDependencies)
  117. {
  118. CMapQuadBounds *pCopy = new CMapQuadBounds;
  119. if (pCopy != NULL)
  120. {
  121. pCopy->CopyFrom(this, bUpdateDependencies);
  122. }
  123. return(pCopy);
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Purpose: Makes this an exact duplicate of pObject.
  127. // Input : pObject - Object to copy.
  128. // Output : Returns this.
  129. //-----------------------------------------------------------------------------
  130. CMapClass *CMapQuadBounds::CopyFrom(CMapClass *pObject, bool bUpdateDependencies)
  131. {
  132. Assert(pObject->IsMapClass(MAPCLASS_TYPE(CMapQuadBounds)));
  133. CMapQuadBounds *pFrom = (CMapQuadBounds *)pObject;
  134. CMapClass::CopyFrom(pObject, bUpdateDependencies);
  135. m_vLowerLeft = pFrom->m_vLowerLeft;
  136. m_vUpperLeft = pFrom->m_vUpperLeft;
  137. m_vLowerRight = pFrom->m_vLowerRight;
  138. m_vUpperRight = pFrom->m_vUpperRight;
  139. m_nError = pFrom->m_nError;
  140. return(this);
  141. }