Team Fortress 2 Source Code as on 22/4/2020
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.

407 lines
14 KiB

  1. //========= Copyright 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 "zip_utils.h"
  15. #include "bspfile.h"
  16. #include "utlvector.h"
  17. #include "utlstring.h"
  18. #include "utllinkedlist.h"
  19. #include "byteswap.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 CDispLightmapSamplePositions g_DispLightmapSamplePositions; // LUMP_DISP_LIGHTMAP_SAMPLE_POSITIONS
  89. extern int numorigfaces;
  90. extern dface_t dorigfaces[MAX_MAP_FACES];
  91. extern int g_numprimitives;
  92. extern dprimitive_t g_primitives[MAX_MAP_PRIMITIVES];
  93. extern int g_numprimverts;
  94. extern dprimvert_t g_primverts[MAX_MAP_PRIMVERTS];
  95. extern int g_numprimindices;
  96. extern unsigned short g_primindices[MAX_MAP_PRIMINDICES];
  97. extern int numfaces;
  98. extern dface_t dfaces[MAX_MAP_FACES];
  99. extern int numfaceids;
  100. extern CUtlVector<dfaceid_t> dfaceids;
  101. extern int numfaces_hdr;
  102. extern dface_t dfaces_hdr[MAX_MAP_FACES];
  103. extern int numedges;
  104. extern dedge_t dedges[MAX_MAP_EDGES];
  105. extern int numleaffaces;
  106. extern unsigned short dleaffaces[MAX_MAP_LEAFFACES];
  107. extern int numleafbrushes;
  108. extern unsigned short dleafbrushes[MAX_MAP_LEAFBRUSHES];
  109. extern int numsurfedges;
  110. extern int dsurfedges[MAX_MAP_SURFEDGES];
  111. extern int numareas;
  112. extern darea_t dareas[MAX_MAP_AREAS];
  113. extern int numareaportals;
  114. extern dareaportal_t dareaportals[MAX_MAP_AREAPORTALS];
  115. extern int numbrushes;
  116. extern dbrush_t dbrushes[MAX_MAP_BRUSHES];
  117. extern int numbrushsides;
  118. extern dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES];
  119. extern int *pNumworldlights;
  120. extern dworldlight_t *dworldlights;
  121. extern Vector g_ClipPortalVerts[MAX_MAP_PORTALVERTS];
  122. extern int g_nClipPortalVerts;
  123. extern dcubemapsample_t g_CubemapSamples[MAX_MAP_CUBEMAPSAMPLES];
  124. extern int g_nCubemapSamples;
  125. extern int g_nOverlayCount;
  126. extern doverlay_t g_Overlays[MAX_MAP_OVERLAYS];
  127. extern doverlayfade_t g_OverlayFades[MAX_MAP_OVERLAYS]; // Parallel array of fade info in a separate lump to avoid breaking backwards compat
  128. extern int g_nWaterOverlayCount;
  129. extern dwateroverlay_t g_WaterOverlays[MAX_MAP_WATEROVERLAYS];
  130. extern CUtlVector<char> g_TexDataStringData;
  131. extern CUtlVector<int> g_TexDataStringTable;
  132. extern int numleafwaterdata;
  133. extern dleafwaterdata_t dleafwaterdata[MAX_MAP_LEAFWATERDATA];
  134. extern CUtlVector<CFaceMacroTextureInfo> g_FaceMacroTextureInfos;
  135. extern CUtlVector<doccluderdata_t> g_OccluderData;
  136. extern CUtlVector<doccluderpolydata_t> g_OccluderPolyData;
  137. extern CUtlVector<int> g_OccluderVertexIndices;
  138. // level flags - see LVLFLAGS_xxx in bspfile.h
  139. extern uint32 g_LevelFlags;
  140. // physics collision data
  141. extern byte *g_pPhysCollide;
  142. extern int g_PhysCollideSize;
  143. extern byte *g_pPhysDisp;
  144. extern int g_PhysDispSize;
  145. // Embedded pack/pak file
  146. IZip *GetPakFile( void );
  147. IZip *GetSwapPakFile( void );
  148. void ClearPakFile( IZip *pak );
  149. void AddFileToPak( IZip *pak, const char *pRelativeName, const char *fullpath, IZip::eCompressionType compressionType = IZip::eCompressionType_None );
  150. void AddBufferToPak( IZip *pak, const char *pRelativeName, void *data, int length, bool bTextMode, IZip::eCompressionType compressionType = IZip::eCompressionType_None );
  151. void AddDirToPak( IZip *pak, const char *pDirPath, const char *pPakPrefix = NULL );
  152. bool FileExistsInPak( IZip *pak, const char *pRelativeName );
  153. bool ReadFileFromPak( IZip *pak, const char *pRelativeName, bool bTextMode, CUtlBuffer &buf );
  154. void RemoveFileFromPak( IZip *pak, const char *pRelativeName );
  155. int GetNextFilename( IZip *pak, int id, char *pBuffer, int bufferSize, int &fileSize );
  156. void ForceAlignment( IZip *pak, bool bAlign, bool bCompatibleFormat, unsigned int alignmentSize );
  157. typedef bool (*CompressFunc_t)( CUtlBuffer &inputBuffer, CUtlBuffer &outputBuffer );
  158. typedef bool (*VTFConvertFunc_t)( const char *pDebugName, CUtlBuffer &sourceBuf, CUtlBuffer &targetBuf, CompressFunc_t pCompressFunc );
  159. typedef bool (*VHVFixupFunc_t)( const char *pVhvFilename, const char *pModelName, CUtlBuffer &sourceBuf, CUtlBuffer &targetBuf );
  160. //-----------------------------------------------------------------------------
  161. // Game lump memory storage
  162. //-----------------------------------------------------------------------------
  163. // NOTE: This is not optimal at all; since I expect client lumps to
  164. // not be accessed all that often.
  165. struct GameLump_t
  166. {
  167. GameLumpId_t m_Id;
  168. unsigned short m_Flags;
  169. unsigned short m_Version;
  170. CUtlMemory< unsigned char > m_Memory;
  171. };
  172. //-----------------------------------------------------------------------------
  173. // Handle to a game lump
  174. //-----------------------------------------------------------------------------
  175. typedef unsigned short GameLumpHandle_t;
  176. class CGameLump
  177. {
  178. public:
  179. //-----------------------------------------------------------------------------
  180. // Convert four-CC code to a handle + back
  181. //-----------------------------------------------------------------------------
  182. GameLumpHandle_t GetGameLumpHandle( GameLumpId_t id );
  183. GameLumpId_t GetGameLumpId( GameLumpHandle_t handle );
  184. int GetGameLumpFlags( GameLumpHandle_t handle );
  185. int GetGameLumpVersion( GameLumpHandle_t handle );
  186. void ComputeGameLumpSizeAndCount( int& size, int& clumpCount );
  187. void ParseGameLump( dheader_t* pHeader );
  188. void SwapGameLump( GameLumpId_t id, int version, byte *dest, byte *src, int size );
  189. //-----------------------------------------------------------------------------
  190. // Game lump accessor methods
  191. //-----------------------------------------------------------------------------
  192. void* GetGameLump( GameLumpHandle_t handle );
  193. int GameLumpSize( GameLumpHandle_t handle );
  194. //-----------------------------------------------------------------------------
  195. // Game lump iteration methods
  196. //-----------------------------------------------------------------------------
  197. GameLumpHandle_t FirstGameLump();
  198. GameLumpHandle_t NextGameLump( GameLumpHandle_t handle );
  199. GameLumpHandle_t InvalidGameLump();
  200. //-----------------------------------------------------------------------------
  201. // Game lump creation/destruction method
  202. //-----------------------------------------------------------------------------
  203. GameLumpHandle_t CreateGameLump( GameLumpId_t id, int size, int flags, int version );
  204. void DestroyGameLump( GameLumpHandle_t handle );
  205. void DestroyAllGameLumps();
  206. private:
  207. CUtlLinkedList< GameLump_t, GameLumpHandle_t > m_GameLumps;
  208. };
  209. extern CGameLump g_GameLumps;
  210. extern CByteswap g_Swap;
  211. //-----------------------------------------------------------------------------
  212. // Helper for the bspzip tool
  213. //-----------------------------------------------------------------------------
  214. void ExtractZipFileFromBSP( char *pBSPFileName, char *pZipFileName );
  215. //-----------------------------------------------------------------------------
  216. // String table methods
  217. //-----------------------------------------------------------------------------
  218. const char * TexDataStringTable_GetString( int stringID );
  219. int TexDataStringTable_AddOrFindString( const char *pString );
  220. void DecompressVis (byte *in, byte *decompressed);
  221. int CompressVis (byte *vis, byte *dest);
  222. void OpenBSPFile( const char *filename );
  223. void CloseBSPFile(void);
  224. void LoadBSPFile( const char *filename );
  225. void LoadBSPFile_FileSystemOnly( const char *filename );
  226. void LoadBSPFileTexinfo( const char *filename );
  227. void WriteBSPFile( const char *filename, char *pUnused = NULL );
  228. void PrintBSPFileSizes(void);
  229. void PrintBSPPackDirectory(void);
  230. void ReleasePakFileLumps(void);
  231. bool RepackBSPCallback_LZMA( CUtlBuffer &inputBuffer, CUtlBuffer &outputBuffer );
  232. bool RepackBSP( CUtlBuffer &inputBuffer, CUtlBuffer &outputBuffer, CompressFunc_t pCompressFunc, IZip::eCompressionType packfileCompression );
  233. bool SwapBSPFile( const char *filename, const char *swapFilename, bool bSwapOnLoad, VTFConvertFunc_t pVTFConvertFunc, VHVFixupFunc_t pVHVFixupFunc, CompressFunc_t pCompressFunc );
  234. bool GetPakFileLump( const char *pBSPFilename, void **pPakData, int *pPakSize );
  235. bool SetPakFileLump( const char *pBSPFilename, const char *pNewFilename, void *pPakData, int pakSize );
  236. void WriteLumpToFile( char *filename, int lump );
  237. void WriteLumpToFile( char *filename, int lump, int nLumpVersion, void *pBuffer, size_t nBufLen );
  238. bool GetBSPDependants( const char *pBSPFilename, CUtlVector< CUtlString > *pList );
  239. void UnloadBSPFile();
  240. void ParseEntities (void);
  241. void UnparseEntities (void);
  242. void PrintEntity (entity_t *ent);
  243. void SetKeyValue (entity_t *ent, const char *key, const char *value);
  244. char *ValueForKey (entity_t *ent, char *key);
  245. // will return "" if not present
  246. int IntForKey (entity_t *ent, char *key);
  247. int IntForKeyWithDefault(entity_t *ent, char *key, int nDefault );
  248. vec_t FloatForKey (entity_t *ent, char *key);
  249. vec_t FloatForKeyWithDefault (entity_t *ent, char *key, float default_value);
  250. void GetVectorForKey (entity_t *ent, char *key, Vector& vec);
  251. void GetVector2DForKey (entity_t *ent, char *key, Vector2D& vec);
  252. void GetAnglesForKey (entity_t *ent, char *key, QAngle& vec);
  253. epair_t *ParseEpair (void);
  254. void StripTrailing (char *e);
  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, int 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, int context ) = 0;
  288. };
  289. //-----------------------------------------------------------------------------
  290. // Enumerates nodes + leafs in front to back order...
  291. //-----------------------------------------------------------------------------
  292. bool EnumerateNodesAlongRay( Ray_t const& ray, IBSPNodeEnumerator* pEnum, int 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 SetHDRMode( bool bHDR );
  305. // ----------------------------------------------------------------------------- //
  306. // Helper accessors for the various structures.
  307. // ----------------------------------------------------------------------------- //
  308. inline ColorRGBExp32* dface_AvgLightColor( dface_t *pFace, int nLightStyleIndex )
  309. {
  310. return (ColorRGBExp32*)&(*pdlightdata)[pFace->lightofs - (nLightStyleIndex+1) * 4];
  311. }
  312. inline const char* TexInfo_TexName( int iTexInfo )
  313. {
  314. return TexDataStringTable_GetString( dtexdata[texinfo[iTexInfo].texdata].nameStringTableID );
  315. }
  316. #endif // BSPLIB_H