summaryrefslogtreecommitdiff
path: root/hammer/mapworld.h
blob: 0b4f2857b64f66f8191b17a88cd8ad9a784a65b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#ifndef MAPWORLD_H
#define MAPWORLD_H
#ifdef _WIN32
#pragma once
#endif

#include "MapEntity.h"
#include "EditGameClass.h"
#include "MapClass.h"
#include "MapPath.h"

// Flags for SaveVMF.
#define SAVEFLAGS_LIGHTSONLY	(1<<0)
#define SAVEFLAGS_AUTOSAVE		(1<<1)


#define MAX_VISIBLE_OBJECTS		10000

#define NUM_HASHED_ENTITY_BUCKETS	200


class BoundBox;
class CChunkFile;
class CVisGroup;
class CCullTreeNode;
class IEditorTexture;
class CMapGroup;
class CMapDoc;
class CMapInstance;

struct SaveLists_t;


struct UsedTexture_t
{
	IEditorTexture *pTex;
	int nUsageCount;
};


class CUsedTextureList : public CUtlVector<UsedTexture_t>
{
public:

	inline int Find(IEditorTexture *pTex)
	{
		for (int i = 0; i < Count(); i++)
		{
			if (Element(i).pTex == pTex)
			{
				return i;
			}
		}
		
		return -1;	
	}
};


class CMapWorld : public CMapClass, public CEditGameClass
{
	public:

		DECLARE_MAPCLASS(CMapWorld,CMapClass)

		CMapWorld( void );
		CMapWorld( CMapDoc *pOwningDocument );
		~CMapWorld(void);

		CMapDoc *GetOwningDocument( void ) { return m_pOwningDocument; }

		//
		// Public interface to the culling tree.
		//
		void CullTree_Build(void);
		inline CCullTreeNode *CullTree_GetCullTree(void) { return(m_pCullTree); }

		//
		// CMapClass virtual overrides.
		//
		virtual void AddChild(CMapClass *pChild);
		virtual CMapClass *Copy(bool bUpdateDependencies);
		virtual CMapClass *CopyFrom(CMapClass *pFrom, bool bUpdateDependencies);
		virtual void PresaveWorld(void);
		virtual void RemoveChild(CMapClass *pChild, bool bUpdateBounds = true);
		virtual bool IsWorld() { return true; }

		virtual CMapEntity *FindChildByKeyValue( const char* key, const char* value, bool *bIsInInstance = NULL, VMatrix *InstanceMatrix = NULL );

		void AddObjectToWorld(CMapClass *pObject, CMapClass *pParent = NULL);
		void RemoveObjectFromWorld(CMapClass *pObject, bool bRemoveChildren);

		// Groups have to be treated as logical because they potentially have logical children
		virtual bool IsLogical(void) { return true; }
		virtual bool IsVisibleLogical(void) { return IsVisible(); }
		virtual bool IsEditable( void );

		//
		// Serialization.
		//
		ChunkFileResult_t LoadVMF(CChunkFile *pFile);
		void PostloadWorld(void);
		void PostloadVisGroups(void);
		
		// saveFlags is a combination of SAVEFLAGS_ defines.
		ChunkFileResult_t SaveSolids(CChunkFile *pFile, CSaveInfo *pSaveInfo, int saveFlags);
		ChunkFileResult_t SaveVMF(CChunkFile *pFile, CSaveInfo *pSaveInfo, int saveFlags);
		
		virtual int SerializeRMF(std::fstream &file, BOOL fIsStoring);
		virtual int SerializeMAP(std::fstream &file, BOOL fIsStoring, BoundBox *pIntersecting = NULL);

		virtual void UpdateChild(CMapClass *pChild);

		void UpdateAllDependencies( CMapClass *pObject );

		//
		// Face ID management.
		//
		inline int FaceID_GetNext(void);
		inline void FaceID_SetNext(int nNextFaceID);
		CMapFace *FaceID_FaceForID(int nFaceID);
		void FaceID_StringToFaceIDLists(CMapFaceIDList *pFullFaceList, CMapFaceIDList *pPartialFaceList, const char *pszValue);
		void FaceID_StringToFaceLists(CMapFaceList *pFullFaceList, CMapFaceList *pPartialFaceList, const char *pszValue);
		static bool FaceID_FaceIDListsToString(char *pszList, int nSize, CMapFaceIDList *pFullFaceIDList, CMapFaceIDList *pPartialFaceIDList);
		static bool FaceID_FaceListsToString(char *pszValue, int nSize, CMapFaceList *pFullFaceList, CMapFaceList *pPartialFaceList);

		void GetUsedTextures(CUsedTextureList &List);
		void Subtract(CMapObjectList &Results, CMapClass *pSubtractWith, CMapClass *pSubtractFrom);

		CUtlVector<CMapPath*> m_Paths;

		// Interface to list of all the entities in the world:
		const CMapEntityList *EntityList_GetList(void) { return(&m_EntityList); }
		inline int EntityList_GetCount();
		inline CMapEntity *EntityList_GetEntity( int nIndex );

		CMapEntity *FindEntityByName( const char *pszName, bool bVisiblesOnly = false, bool bSearchInstanceParms = false );
		bool FindEntitiesByKeyValue(CMapEntityList &Found, const char *szKey, const char *szValue, bool bVisiblesOnly);
		bool FindEntitiesByName(CMapEntityList &Found, const char *szName, bool bVisiblesOnly);
		bool FindEntitiesByClassName(CMapEntityList &Found, const char *szClassName, bool bVisiblesOnly);
		bool FindEntitiesByNameOrClassName(CMapEntityList &Found, const char *pszName, bool bVisiblesOnly);
		
		bool GenerateNewTargetname( const char *startName, char *newName, int newNameBufferSize, bool bMakeUnique, const char *szPrefix, CMapClass *pRoot = NULL );

		// displacement management
		inline IWorldEditDispMgr *GetWorldEditDispManager( void ) { return m_pWorldDispMgr; }

		int GetGroupList(CUtlVector<CMapGroup *> &GroupList);

	protected:

		//
		// Protected entity list functions.
		//
		void AddEntity( CMapEntity *pEntity );
		void EntityList_Add(CMapClass *pObject);
		void EntityList_Remove(CMapClass *pObject, bool bRemoveChildren);

		int FindEntityBucket( CMapEntity *pEntity, int *pnIndex );

		//
		// Serialization.
		//
		static ChunkFileResult_t LoadKeyCallback(const char *szKey, const char *szValue, CMapWorld *pWorld);
		static ChunkFileResult_t LoadGroupCallback(CChunkFile *pFile, CMapWorld *pWorld);
		static ChunkFileResult_t LoadHiddenCallback(CChunkFile *pFile, CMapWorld *pWorld);
		static ChunkFileResult_t LoadHiddenSolidCallback(CChunkFile *pFile, CMapWorld *pWorld);
		static ChunkFileResult_t LoadSolidCallback(CChunkFile *pFile, CMapWorld *pWorld);

		ChunkFileResult_t LoadSolid(CChunkFile *pFile, bool bVisible);

		static BOOL BuildSaveListsCallback(CMapClass *pObject, SaveLists_t *pSaveLists);
		ChunkFileResult_t SaveObjectListVMF(CChunkFile *pFile, CSaveInfo *pSaveInfo, const CMapObjectList *pList, int saveFlags);

		//
		// Culling tree operations.
		//
		void CullTree_SplitNode(CCullTreeNode *pNode);
		void CullTree_DumpNode(CCullTreeNode *pNode, int nDepth);
		void CullTree_FreeNode(CCullTreeNode *pNode);
		void CullTree_Free(void);

		CCullTreeNode *m_pCullTree;		// This world's objects stored in a spatial hierarchy for culling.
		
		CMapEntityList m_EntityList;									// A flat list of all the entities in this world.
		CMapEntityList m_EntityListByName[NUM_HASHED_ENTITY_BUCKETS];	// A list of all the entities in the world, hashed by name checksum.

		int m_nNextFaceID;						// Used for assigning unique IDs to every solid face in this world.

		IWorldEditDispMgr	*m_pWorldDispMgr;	// world editable displacement manager

		CMapDoc				*m_pOwningDocument;
};


//-----------------------------------------------------------------------------
// Purpose: Returns the next unique face ID for this world.
//-----------------------------------------------------------------------------
inline int CMapWorld::FaceID_GetNext(void)
{
	return(m_nNextFaceID++);
}


//-----------------------------------------------------------------------------
// Purpose: Sets the unique face ID to assign to the next face that is added
//			to this world.
//-----------------------------------------------------------------------------
inline void CMapWorld::FaceID_SetNext(int nNextFaceID)
{
	m_nNextFaceID = nNextFaceID;
}


inline int CMapWorld::EntityList_GetCount()
{
	return m_EntityList.Count();
}


inline CMapEntity *CMapWorld::EntityList_GetEntity( int nIndex )
{
	return m_EntityList.Element( nIndex );
}


#endif // MAPWORLD_H