MapFile.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __MAPFILE_H__
  21. #define __MAPFILE_H__
  22. /*
  23. ===============================================================================
  24. Reads or writes the contents of .map files into a standard internal
  25. format, which can then be moved into private formats for collision
  26. detection, map processing, or editor use.
  27. No validation (duplicate planes, null area brushes, etc) is performed.
  28. There are no limits to the number of any of the elements in maps.
  29. The order of entities, brushes, and sides is maintained.
  30. ===============================================================================
  31. */
  32. const int OLD_MAP_VERSION = 1;
  33. const int CURRENT_MAP_VERSION = 2;
  34. const int DEFAULT_CURVE_SUBDIVISION = 4;
  35. const float DEFAULT_CURVE_MAX_ERROR = 4.0f;
  36. const float DEFAULT_CURVE_MAX_ERROR_CD = 24.0f;
  37. const float DEFAULT_CURVE_MAX_LENGTH = -1.0f;
  38. const float DEFAULT_CURVE_MAX_LENGTH_CD = -1.0f;
  39. class idMapPrimitive {
  40. public:
  41. enum { TYPE_INVALID = -1, TYPE_BRUSH, TYPE_PATCH };
  42. idDict epairs;
  43. idMapPrimitive() { type = TYPE_INVALID; }
  44. virtual ~idMapPrimitive() { }
  45. int GetType() const { return type; }
  46. protected:
  47. int type;
  48. };
  49. class idMapBrushSide {
  50. friend class idMapBrush;
  51. public:
  52. idMapBrushSide();
  53. ~idMapBrushSide() { }
  54. const char * GetMaterial() const { return material; }
  55. void SetMaterial( const char *p ) { material = p; }
  56. const idPlane & GetPlane() const { return plane; }
  57. void SetPlane( const idPlane &p ) { plane = p; }
  58. void SetTextureMatrix( const idVec3 mat[2] ) { texMat[0] = mat[0]; texMat[1] = mat[1]; }
  59. void GetTextureMatrix( idVec3 &mat1, idVec3 &mat2 ) { mat1 = texMat[0]; mat2 = texMat[1]; }
  60. void GetTextureVectors( idVec4 v[2] ) const;
  61. protected:
  62. idStr material;
  63. idPlane plane;
  64. idVec3 texMat[2];
  65. idVec3 origin;
  66. };
  67. ID_INLINE idMapBrushSide::idMapBrushSide() {
  68. plane.Zero();
  69. texMat[0].Zero();
  70. texMat[1].Zero();
  71. origin.Zero();
  72. }
  73. class idMapBrush : public idMapPrimitive {
  74. public:
  75. idMapBrush() { type = TYPE_BRUSH; sides.Resize( 8, 4 ); }
  76. ~idMapBrush() { sides.DeleteContents( true ); }
  77. static idMapBrush * Parse( idLexer &src, const idVec3 &origin, bool newFormat = true, float version = CURRENT_MAP_VERSION );
  78. static idMapBrush * ParseQ3( idLexer &src, const idVec3 &origin );
  79. bool Write( idFile *fp, int primitiveNum, const idVec3 &origin ) const;
  80. int GetNumSides() const { return sides.Num(); }
  81. int AddSide( idMapBrushSide *side ) { return sides.Append( side ); }
  82. idMapBrushSide * GetSide( int i ) const { return sides[i]; }
  83. unsigned int GetGeometryCRC() const;
  84. protected:
  85. int numSides;
  86. idList<idMapBrushSide*, TAG_IDLIB_LIST_MAP> sides;
  87. };
  88. class idMapPatch : public idMapPrimitive, public idSurface_Patch {
  89. public:
  90. idMapPatch();
  91. idMapPatch( int maxPatchWidth, int maxPatchHeight );
  92. ~idMapPatch() { }
  93. static idMapPatch * Parse( idLexer &src, const idVec3 &origin, bool patchDef3 = true, float version = CURRENT_MAP_VERSION );
  94. bool Write( idFile *fp, int primitiveNum, const idVec3 &origin ) const;
  95. const char * GetMaterial() const { return material; }
  96. void SetMaterial( const char *p ) { material = p; }
  97. int GetHorzSubdivisions() const { return horzSubdivisions; }
  98. int GetVertSubdivisions() const { return vertSubdivisions; }
  99. bool GetExplicitlySubdivided() const { return explicitSubdivisions; }
  100. void SetHorzSubdivisions( int n ) { horzSubdivisions = n; }
  101. void SetVertSubdivisions( int n ) { vertSubdivisions = n; }
  102. void SetExplicitlySubdivided( bool b ) { explicitSubdivisions = b; }
  103. unsigned int GetGeometryCRC() const;
  104. protected:
  105. idStr material;
  106. int horzSubdivisions;
  107. int vertSubdivisions;
  108. bool explicitSubdivisions;
  109. };
  110. ID_INLINE idMapPatch::idMapPatch() {
  111. type = TYPE_PATCH;
  112. horzSubdivisions = vertSubdivisions = 0;
  113. explicitSubdivisions = false;
  114. width = height = 0;
  115. maxWidth = maxHeight = 0;
  116. expanded = false;
  117. }
  118. ID_INLINE idMapPatch::idMapPatch( int maxPatchWidth, int maxPatchHeight ) {
  119. type = TYPE_PATCH;
  120. horzSubdivisions = vertSubdivisions = 0;
  121. explicitSubdivisions = false;
  122. width = height = 0;
  123. maxWidth = maxPatchWidth;
  124. maxHeight = maxPatchHeight;
  125. verts.SetNum( maxWidth * maxHeight );
  126. expanded = false;
  127. }
  128. class idMapEntity {
  129. friend class idMapFile;
  130. public:
  131. idDict epairs;
  132. public:
  133. idMapEntity() { epairs.SetHashSize( 64 ); }
  134. ~idMapEntity() { primitives.DeleteContents( true ); }
  135. static idMapEntity * Parse( idLexer &src, bool worldSpawn = false, float version = CURRENT_MAP_VERSION );
  136. bool Write( idFile *fp, int entityNum ) const;
  137. int GetNumPrimitives() const { return primitives.Num(); }
  138. idMapPrimitive * GetPrimitive( int i ) const { return primitives[i]; }
  139. void AddPrimitive( idMapPrimitive *p ) { primitives.Append( p ); }
  140. unsigned int GetGeometryCRC() const;
  141. void RemovePrimitiveData();
  142. protected:
  143. idList<idMapPrimitive*, TAG_IDLIB_LIST_MAP> primitives;
  144. };
  145. class idMapFile {
  146. public:
  147. idMapFile();
  148. ~idMapFile() { entities.DeleteContents( true ); }
  149. // filename does not require an extension
  150. // normally this will use a .reg file instead of a .map file if it exists,
  151. // which is what the game and dmap want, but the editor will want to always
  152. // load a .map file
  153. bool Parse( const char *filename, bool ignoreRegion = false, bool osPath = false );
  154. bool Write( const char *fileName, const char *ext, bool fromBasePath = true );
  155. // get the number of entities in the map
  156. int GetNumEntities() const { return entities.Num(); }
  157. // get the specified entity
  158. idMapEntity * GetEntity( int i ) const { return entities[i]; }
  159. // get the name without file extension
  160. const char * GetName() const { return name; }
  161. // get the file time
  162. ID_TIME_T GetFileTime() const { return fileTime; }
  163. // get CRC for the map geometry
  164. // texture coordinates and entity key/value pairs are not taken into account
  165. unsigned int GetGeometryCRC() const { return geometryCRC; }
  166. // returns true if the file on disk changed
  167. bool NeedsReload();
  168. int AddEntity( idMapEntity *mapentity );
  169. idMapEntity * FindEntity( const char *name );
  170. void RemoveEntity( idMapEntity *mapEnt );
  171. void RemoveEntities( const char *classname );
  172. void RemoveAllEntities();
  173. void RemovePrimitiveData();
  174. bool HasPrimitiveData() { return hasPrimitiveData; }
  175. protected:
  176. float version;
  177. ID_TIME_T fileTime;
  178. unsigned int geometryCRC;
  179. idList<idMapEntity *, TAG_IDLIB_LIST_MAP> entities;
  180. idStr name;
  181. bool hasPrimitiveData;
  182. private:
  183. void SetGeometryCRC();
  184. };
  185. ID_INLINE idMapFile::idMapFile() {
  186. version = CURRENT_MAP_VERSION;
  187. fileTime = 0;
  188. geometryCRC = 0;
  189. entities.Resize( 1024, 256 );
  190. hasPrimitiveData = false;
  191. }
  192. #endif /* !__MAPFILE_H__ */