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.

410 lines
14 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #ifndef BSPLIB_H
  10. #define BSPLIB_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "bspfile.h"
  15. #include "utlvector.h"
  16. #include "utlstring.h"
  17. #include "utllinkedlist.h"
  18. #include "byteswap.h"
  19. #include "mathlib/vector4d.h"
  20. #ifdef ENGINE_DLL
  21. #include "zone.h"
  22. #endif
  23. #ifdef ENGINE_DLL
  24. typedef CUtlVector<unsigned char, CHunkMemory<unsigned char> > CDispLightmapSamplePositions;
  25. #else
  26. typedef CUtlVector<unsigned char> CDispLightmapSamplePositions;
  27. #endif
  28. class ISpatialQuery;
  29. struct Ray_t;
  30. class Vector2D;
  31. struct portal_t;
  32. class CUtlBuffer;
  33. class IZip;
  34. // this is only true in vrad
  35. extern bool g_bHDR;
  36. // default width/height of luxels in world units.
  37. #define DEFAULT_LUXEL_SIZE ( 16.0f )
  38. #define SINGLE_BRUSH_MAP (MAX_BRUSH_LIGHTMAP_DIM_INCLUDING_BORDER*MAX_BRUSH_LIGHTMAP_DIM_INCLUDING_BORDER)
  39. #define SINGLEMAP (MAX_LIGHTMAP_DIM_INCLUDING_BORDER*MAX_LIGHTMAP_DIM_INCLUDING_BORDER)
  40. struct entity_t
  41. {
  42. Vector origin;
  43. int firstbrush;
  44. int numbrushes;
  45. epair_t *epairs;
  46. // only valid for func_areaportals
  47. int areaportalnum;
  48. int portalareas[2];
  49. portal_t *m_pPortalsLeadingIntoAreas[2]; // portals leading into portalareas
  50. };
  51. extern int num_entities;
  52. extern entity_t entities[MAX_MAP_ENTITIES];
  53. extern int nummodels;
  54. extern dmodel_t dmodels[MAX_MAP_MODELS];
  55. extern int visdatasize;
  56. extern byte dvisdata[MAX_MAP_VISIBILITY];
  57. extern dvis_t *dvis;
  58. extern CUtlVector<byte> dlightdataHDR;
  59. extern CUtlVector<byte> dlightdataLDR;
  60. extern CUtlVector<byte> *pdlightdata;
  61. extern CUtlVector<char> dentdata;
  62. extern int numleafs;
  63. #if !defined( _X360 )
  64. extern dleaf_t dleafs[MAX_MAP_LEAFS];
  65. #else
  66. extern dleaf_t *dleafs;
  67. #endif
  68. extern CUtlVector<dleafambientlighting_t> *g_pLeafAmbientLighting;
  69. extern CUtlVector<dleafambientindex_t> *g_pLeafAmbientIndex;
  70. extern unsigned short g_LeafMinDistToWater[MAX_MAP_LEAFS];
  71. extern int numplanes;
  72. extern dplane_t dplanes[MAX_MAP_PLANES];
  73. extern int numvertexes;
  74. extern dvertex_t dvertexes[MAX_MAP_VERTS];
  75. extern int g_numvertnormalindices; // dfaces reference these. These index g_vertnormals.
  76. extern unsigned short g_vertnormalindices[MAX_MAP_VERTNORMALS];
  77. extern int g_numvertnormals;
  78. extern Vector g_vertnormals[MAX_MAP_VERTNORMALS];
  79. extern int numnodes;
  80. extern dnode_t dnodes[MAX_MAP_NODES];
  81. extern CUtlVector<texinfo_t> texinfo;
  82. extern int numtexdata;
  83. extern dtexdata_t dtexdata[MAX_MAP_TEXDATA];
  84. // displacement map .bsp file info
  85. extern CUtlVector<ddispinfo_t> g_dispinfo;
  86. extern CUtlVector<CDispVert> g_DispVerts;
  87. extern CUtlVector<CDispTri> g_DispTris;
  88. extern CUtlVector<CDispMultiBlend> g_DispMultiBlend;
  89. extern CDispLightmapSamplePositions g_DispLightmapSamplePositions; // LUMP_DISP_LIGHTMAP_SAMPLE_POSITIONS
  90. extern int numorigfaces;
  91. extern dface_t dorigfaces[MAX_MAP_FACES];
  92. extern int g_numprimitives;
  93. extern dprimitive_t g_primitives[MAX_MAP_PRIMITIVES];
  94. extern int g_numprimverts;
  95. extern dprimvert_t g_primverts[MAX_MAP_PRIMVERTS];
  96. extern int g_numprimindices;
  97. extern unsigned short g_primindices[MAX_MAP_PRIMINDICES];
  98. extern int numfaces;
  99. extern dface_t dfaces[MAX_MAP_FACES];
  100. extern int numfaceids;
  101. extern CUtlVector<dfaceid_t> dfaceids;
  102. extern CUtlVector<uint16> dfacebrushes;
  103. extern CUtlVector<dfacebrushlist_t> dfacebrushlists;
  104. extern int numfaces_hdr;
  105. extern dface_t dfaces_hdr[MAX_MAP_FACES];
  106. extern int numedges;
  107. extern dedge_t dedges[MAX_MAP_EDGES];
  108. extern int numleaffaces;
  109. extern unsigned short dleaffaces[MAX_MAP_LEAFFACES];
  110. extern int numleafbrushes;
  111. extern unsigned short dleafbrushes[MAX_MAP_LEAFBRUSHES];
  112. extern int numsurfedges;
  113. extern int dsurfedges[MAX_MAP_SURFEDGES];
  114. extern int numareas;
  115. extern darea_t dareas[MAX_MAP_AREAS];
  116. extern int numareaportals;
  117. extern dareaportal_t dareaportals[MAX_MAP_AREAPORTALS];
  118. extern int numbrushes;
  119. extern dbrush_t dbrushes[MAX_MAP_BRUSHES];
  120. extern int numbrushsides;
  121. extern dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES];
  122. extern int *pNumworldlights;
  123. extern dworldlight_t *dworldlights;
  124. extern Vector g_ClipPortalVerts[MAX_MAP_PORTALVERTS];
  125. extern int g_nClipPortalVerts;
  126. extern dcubemapsample_t g_CubemapSamples[MAX_MAP_CUBEMAPSAMPLES];
  127. extern int g_nCubemapSamples;
  128. extern int g_nOverlayCount;
  129. extern doverlay_t g_Overlays[MAX_MAP_OVERLAYS];
  130. extern doverlayfade_t g_OverlayFades[MAX_MAP_OVERLAYS]; // Parallel array of fade info in a separate lump to avoid breaking backwards compat
  131. extern doverlaysystemlevel_t g_OverlaySystemLevels[MAX_MAP_OVERLAYS]; // Parallel array of system level info in a separate lump to avoid breaking backwards compat
  132. extern int g_nWaterOverlayCount;
  133. extern dwateroverlay_t g_WaterOverlays[MAX_MAP_WATEROVERLAYS];
  134. extern CUtlVector<char> g_TexDataStringData;
  135. extern CUtlVector<int> g_TexDataStringTable;
  136. extern int numleafwaterdata;
  137. extern dleafwaterdata_t dleafwaterdata[MAX_MAP_LEAFWATERDATA];
  138. extern CUtlVector<CFaceMacroTextureInfo> g_FaceMacroTextureInfos;
  139. extern CUtlVector<doccluderdata_t> g_OccluderData;
  140. extern CUtlVector<doccluderpolydata_t> g_OccluderPolyData;
  141. extern CUtlVector<int> g_OccluderVertexIndices;
  142. // level flags - see LVLFLAGS_xxx in bspfile.h
  143. extern uint32 g_LevelFlags;
  144. // physics collision data
  145. extern byte *g_pPhysCollide;
  146. extern int g_PhysCollideSize;
  147. extern byte *g_pPhysDisp;
  148. extern int g_PhysDispSize;
  149. // Embedded pack/pak file
  150. IZip *GetPakFile( void );
  151. IZip *GetSwapPakFile( void );
  152. void ClearPakFile( IZip *pak );
  153. void AddFileToPak( IZip *pak, const char *pRelativeName, const char *fullpath );
  154. void AddBufferToPak( IZip *pak, const char *pRelativeName, void *data, int length, bool bTextMode );
  155. bool FileExistsInPak( IZip *pak, const char *pRelativeName );
  156. bool ReadFileFromPak( IZip *pak, const char *pRelativeName, bool bTextMode, CUtlBuffer &buf );
  157. void RemoveFileFromPak( IZip *pak, const char *pRelativeName );
  158. int GetNextFilename( IZip *pak, int id, char *pBuffer, int bufferSize, int &fileSize );
  159. void ForceAlignment( IZip *pak, bool bAlign, bool bCompatibleFormat, unsigned int alignmentSize );
  160. typedef bool (*CompressFunc_t)( CUtlBuffer &inputBuffer, CUtlBuffer &outputBuffer );
  161. typedef bool (*VTFConvertFunc_t)( const char *pDebugName, CUtlBuffer &sourceBuf, CUtlBuffer &targetBuf, CompressFunc_t pCompressFunc, int nMaxMip );
  162. typedef bool (*VHVFixupFunc_t)( const char *pVhvFilename, const char *pModelName, CUtlBuffer &sourceBuf, CUtlBuffer &targetBuf );
  163. typedef bool (*StudioConvertFunc_t)( const char *pDebugName, CUtlBuffer &sourceBuf, CUtlBuffer &targetBuf /*, CompressFunc_t pCompressFunc */ );
  164. //-----------------------------------------------------------------------------
  165. // Game lump memory storage
  166. //-----------------------------------------------------------------------------
  167. // NOTE: This is not optimal at all; since I expect client lumps to
  168. // not be accessed all that often.
  169. struct GameLump_t
  170. {
  171. GameLumpId_t m_Id;
  172. unsigned short m_Flags;
  173. unsigned short m_Version;
  174. CUtlMemory< unsigned char > m_Memory;
  175. };
  176. //-----------------------------------------------------------------------------
  177. // Handle to a game lump
  178. //-----------------------------------------------------------------------------
  179. typedef unsigned short GameLumpHandle_t;
  180. class CGameLump
  181. {
  182. public:
  183. //-----------------------------------------------------------------------------
  184. // Convert four-CC code to a handle + back
  185. //-----------------------------------------------------------------------------
  186. GameLumpHandle_t GetGameLumpHandle( GameLumpId_t id );
  187. GameLumpId_t GetGameLumpId( GameLumpHandle_t handle );
  188. int GetGameLumpFlags( GameLumpHandle_t handle );
  189. int GetGameLumpVersion( GameLumpHandle_t handle );
  190. void ComputeGameLumpSizeAndCount( int& size, int& clumpCount );
  191. void ParseGameLump( BSPHeader_t *pHeader );
  192. void SwapGameLump( GameLumpId_t id, int version, byte *dest, byte *src, int size );
  193. //-----------------------------------------------------------------------------
  194. // Game lump accessor methods
  195. //-----------------------------------------------------------------------------
  196. void* GetGameLump( GameLumpHandle_t handle );
  197. int GameLumpSize( GameLumpHandle_t handle );
  198. //-----------------------------------------------------------------------------
  199. // Game lump iteration methods
  200. //-----------------------------------------------------------------------------
  201. GameLumpHandle_t FirstGameLump();
  202. GameLumpHandle_t NextGameLump( GameLumpHandle_t handle );
  203. GameLumpHandle_t InvalidGameLump();
  204. //-----------------------------------------------------------------------------
  205. // Game lump creation/destruction method
  206. //-----------------------------------------------------------------------------
  207. GameLumpHandle_t CreateGameLump( GameLumpId_t id, int size, int flags, int version );
  208. void DestroyGameLump( GameLumpHandle_t handle );
  209. void DestroyAllGameLumps();
  210. private:
  211. CUtlLinkedList< GameLump_t, GameLumpHandle_t > m_GameLumps;
  212. };
  213. extern CGameLump g_GameLumps;
  214. extern CByteswap g_Swap;
  215. //-----------------------------------------------------------------------------
  216. // Helper for the bspzip tool
  217. //-----------------------------------------------------------------------------
  218. void ExtractZipFileFromBSP( char *pBSPFileName, char *pZipFileName );
  219. //-----------------------------------------------------------------------------
  220. // String table methods
  221. //-----------------------------------------------------------------------------
  222. const char * TexDataStringTable_GetString( int stringID );
  223. int TexDataStringTable_AddOrFindString( const char *pString );
  224. void DecompressVis (byte *in, byte *decompressed);
  225. int CompressVis (byte *vis, byte *dest);
  226. void OpenBSPFile( const char *filename );
  227. void CloseBSPFile(void);
  228. void LoadBSPFile( const char *filename );
  229. void LoadBSPFile_FileSystemOnly( const char *filename );
  230. void LoadBSPFileTexinfo( const char *filename );
  231. void WriteBSPFile( const char *filename, char *pUnused = NULL );
  232. void PrintBSPFileSizes(void);
  233. void PrintBSPPackDirectory(void);
  234. void ReleasePakFileLumps(void);
  235. bool SwapBSPFile( const char *filename, const char *swapFilename, bool bSwapOnLoad, VTFConvertFunc_t pVTFConvertFunc, VHVFixupFunc_t pVHVFixupFunc, StudioConvertFunc_t pStudioConvertFunc, CompressFunc_t pCompressFunc, char const *szPlatform );
  236. bool GetPakFileLump( const char *pBSPFilename, void **pPakData, int *pPakSize );
  237. bool SetPakFileLump( const char *pBSPFilename, const char *pNewFilename, void *pPakData, int pakSize );
  238. void WriteLumpToFile( char *filename, int lump );
  239. bool GetBSPDependants( const char *pBSPFilename, CUtlVector< CUtlString > *pList );
  240. void UnloadBSPFile();
  241. void ParseEntities (void);
  242. void UnparseEntities (void);
  243. void PrintEntity (entity_t *ent);
  244. epair_t *SetKeyValue ( entity_t *ent, const char *key, const char *value, bool bAllowDuplicates = false );
  245. void RemoveKey( entity_t *pMapEnt, const char *pKey );
  246. char *ValueForKey (entity_t *ent, const char *key);
  247. // will return "" if not present
  248. int IntForKey (entity_t *ent, const char *key);
  249. vec_t FloatForKey (entity_t *ent, const char *key);
  250. vec_t FloatForKeyWithDefault (entity_t *ent, const char *key, float default_value);
  251. void GetVectorForKey (entity_t *ent, const char *key, Vector& vec);
  252. void GetVector2DForKey (entity_t *ent, const char *key, Vector2D& vec);
  253. void GetAnglesForKey (entity_t *ent, const char *key, QAngle& vec);
  254. epair_t *ParseEpair (void);
  255. // Build a list of the face's vertices (index into dvertexes).
  256. // points must be able to hold pFace->numedges indices.
  257. void BuildFaceCalcWindingData( dface_t *pFace, int *points );
  258. // Convert a tristrip to a trilist.
  259. // Removes degenerates.
  260. // Fills in pTriListIndices and pnTriListIndices.
  261. // You must free pTriListIndices with delete[].
  262. void TriStripToTriList(
  263. unsigned short const *pTriStripIndices,
  264. int nTriStripIndices,
  265. unsigned short **pTriListIndices,
  266. int *pnTriListIndices );
  267. // Calculates the lightmap coordinates at a given set of positions given the
  268. // lightmap basis information.
  269. void CalcTextureCoordsAtPoints(
  270. float const texelsPerWorldUnits[2][4],
  271. int const subtractOffset[2],
  272. Vector const *pPoints,
  273. int const nPoints,
  274. Vector2D *pCoords );
  275. // Figure out lightmap extents on all (lit) faces.
  276. void UpdateAllFaceLightmapExtents();
  277. //-----------------------------------------------------------------------------
  278. // Gets at an interface for the tree for enumeration of leaves in volumes.
  279. //-----------------------------------------------------------------------------
  280. ISpatialQuery* ToolBSPTree();
  281. class IBSPNodeEnumerator
  282. {
  283. public:
  284. // call back with a node and a context
  285. virtual bool EnumerateNode( int node, Ray_t const& ray, float f, intp context ) = 0;
  286. // call back with a leaf and a context
  287. virtual bool EnumerateLeaf( int leaf, Ray_t const& ray, float start, float end, intp context ) = 0;
  288. };
  289. //-----------------------------------------------------------------------------
  290. // Enumerates nodes + leafs in front to back order...
  291. //-----------------------------------------------------------------------------
  292. bool EnumerateNodesAlongRay( Ray_t const& ray, IBSPNodeEnumerator* pEnum, intp context );
  293. //-----------------------------------------------------------------------------
  294. // Helps us find all leaves associated with a particular cluster
  295. //-----------------------------------------------------------------------------
  296. struct clusterlist_t
  297. {
  298. int leafCount;
  299. CUtlVector<int> leafs;
  300. };
  301. extern CUtlVector<clusterlist_t> g_ClusterLeaves;
  302. // Call this to build the mapping from cluster to leaves
  303. void BuildClusterTable( );
  304. void GetPlatformMapPath( const char *pMapPath, char *pPlatformMapPath, int dxlevel, int maxLength );
  305. void SetHDRMode( bool bHDR );
  306. // ----------------------------------------------------------------------------- //
  307. // Helper accessors for the various structures.
  308. // ----------------------------------------------------------------------------- //
  309. inline ColorRGBExp32* dface_AvgLightColor( dface_t *pFace, int nLightStyleIndex )
  310. {
  311. return (ColorRGBExp32*)&(*pdlightdata)[pFace->lightofs - (nLightStyleIndex+1) * 4];
  312. }
  313. inline const char* TexInfo_TexName( int iTexInfo )
  314. {
  315. return TexDataStringTable_GetString( dtexdata[texinfo[iTexInfo].texdata].nameStringTableID );
  316. }
  317. #endif // BSPLIB_H