DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

MapFile.h (8513B)


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