1 //**************************************************************************
2 //**
3 //**	##   ##    ##    ##   ##   ####     ####   ###     ###
4 //**	##   ##  ##  ##  ##   ##  ##  ##   ##  ##  ####   ####
5 //**	 ## ##  ##    ##  ## ##  ##    ## ##    ## ## ## ## ##
6 //**	 ## ##  ########  ## ##  ##    ## ##    ## ##  ###  ##
7 //**	  ###   ##    ##   ###    ##  ##   ##  ##  ##       ##
8 //**	   #    ##    ##    #      ####     ####   ##       ##
9 //**
10 //**	$Id: vc_class.h 4154 2010-03-11 20:42:47Z firebrand_kh $
11 //**
12 //**	Copyright (C) 1999-2006 Jānis Legzdiņš
13 //**
14 //**	This program is free software; you can redistribute it and/or
15 //**  modify it under the terms of the GNU General Public License
16 //**  as published by the Free Software Foundation; either version 2
17 //**  of the License, or (at your option) any later version.
18 //**
19 //**	This program is distributed in the hope that it will be useful,
20 //**  but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //**  GNU General Public License for more details.
23 //**
24 //**************************************************************************
25 
26 enum ENativeConstructor		{EC_NativeConstructor};
27 
28 //
29 // Flags describing a class.
30 //
31 enum EClassFlags
32 {
33 	// Base flags.
34 	CLASS_Native				= 0x0001,
35 	CLASS_Abstract				= 0x0002,	//	Class is abstract and can't be instantiated directly.
36 	CLASS_SkipSuperStateLabels	= 0x0004,	//	Don't copy state labels
37 };
38 
39 //
40 // Flags describing a class instance.
41 //
42 enum EClassObjectFlags
43 {
44 	CLASSOF_Native			= 0x00000001,   // Native
45 	CLASSOF_PostLoaded		= 0x00000002,	// PostLoad has been called
46 };
47 
48 //
49 // Dynamic Light types
50 //
51 enum DLType
52 {
53 	DLTYPE_Point,
54 	DLTYPE_MuzzleFlash,
55 	DLTYPE_Pulse,
56 	DLTYPE_Flicker,
57 	DLTYPE_FlickerRandom,
58 	DLTYPE_Sector,
59 };
60 
61 //==========================================================================
62 //
63 //	VStateLabel
64 //
65 //==========================================================================
66 
67 struct VStateLabel
68 {
69 	//	Persistent fields
70 	VName					Name;
71 	VState*					State;
72 	TArray<VStateLabel>		SubLabels;
73 
VStateLabelVStateLabel74 	VStateLabel()
75 	: Name(NAME_None)
76 	, State(NULL)
77 	{}
78 
79 	friend VStream& operator<<(VStream&, VStateLabel&);
80 };
81 
82 //==========================================================================
83 //
84 //	VStateLabelDef
85 //
86 //==========================================================================
87 
88 struct VStateLabelDef
89 {
90 	VStr		Name;
91 	VState*		State;
92 	TLocation	Loc;
93 	VName		GotoLabel;
94 	vint32		GotoOffset;
95 
VStateLabelDefVStateLabelDef96 	VStateLabelDef()
97 	: State(NULL)
98 	, GotoLabel(NAME_None)
99 	, GotoOffset(0)
100 	{}
101 };
102 
103 //==========================================================================
104 //
105 //	VRepField
106 //
107 //==========================================================================
108 
109 struct VRepField
110 {
111 	VName				Name;
112 	TLocation			Loc;
113 	VMemberBase*		Member;
114 
115 	friend VStream& operator<<(VStream& Strm, VRepField& Fld)
116 	{
117 		return Strm << Fld.Member;
118 	}
119 };
120 
121 //==========================================================================
122 //
123 //	VRepInfo
124 //
125 //==========================================================================
126 
127 struct VRepInfo
128 {
129 	bool				Reliable;
130 	VMethod*			Cond;
131 	TArray<VRepField>	RepFields;
132 
133 	friend VStream& operator<<(VStream& Strm, VRepInfo& RI)
134 	{
135 		return Strm << RI.Cond << RI.RepFields;
136 	}
137 };
138 
139 //==========================================================================
140 //
141 //	VDecorateStateAction
142 //
143 //==========================================================================
144 
145 struct VDecorateStateAction
146 {
147 	VName			Name;
148 	VMethod*		Method;
149 };
150 
151 //==========================================================================
152 //
153 //	VLightEffectDef
154 //
155 //==========================================================================
156 
157 struct VLightEffectDef
158 {
159 	VName				Name;
160 	vuint8				Type;
161 	vint32				Colour;
162 	float				Radius;
163 	float				Radius2;
164 	float				MinLight;
165 	TVec				Offset;
166 	float				Chance;
167 	float				Interval;
168 	float				Scale;
169 };
170 
171 //==========================================================================
172 //
173 //	VParticleEffectDef
174 //
175 //==========================================================================
176 
177 struct VParticleEffectDef
178 {
179 	VName				Name;
180 	vuint8				Type;
181 	vuint8				Type2;
182 	vint32				Colour;
183 	TVec				Offset;
184 	vint32				Count;
185 	float				OrgRnd;
186 	TVec				Velocity;
187 	float				VelRnd;
188 	float				Accel;
189 	float				Grav;
190 	float				Duration;
191 	float				Ramp;
192 };
193 
194 //==========================================================================
195 //
196 //	VSpriteEffect
197 //
198 //==========================================================================
199 
200 struct VSpriteEffect
201 {
202 	vint32				SpriteIndex;
203 	vint8				Frame;
204 	VLightEffectDef*	LightDef;
205 	VParticleEffectDef*	PartDef;
206 };
207 
208 //==========================================================================
209 //
210 //	VClass
211 //
212 //==========================================================================
213 
214 class VClass : public VMemberBase
215 {
216 public:
217 	enum { LOWER_CASE_HASH_SIZE = 8 * 1024 };
218 
219 	//	Persistent fields
220 	VClass*					ParentClass;
221 	VField*					Fields;
222 	VState*					States;
223 	TArray<VMethod*>		Methods;
224 	VMethod*				DefaultProperties;
225 	TArray<VRepInfo>		RepInfos;
226 	TArray<VStateLabel>		StateLabels;
227 
228 	//	Compiler fields
229 	VName					ParentClassName;
230 	TLocation				ParentClassLoc;
231 	VExpression*			GameExpr;
232 	VExpression*			MobjInfoExpr;
233 	VExpression*			ScriptIdExpr;
234 	TArray<VStruct*>		Structs;
235 	TArray<VConstant*>		Constants;
236 	TArray<VProperty*>		Properties;
237 	TArray<VStateLabelDef>	StateLabelDefs;
238 	bool					Defined;
239 
240 	// Internal per-object variables.
241 	vuint32					ObjectFlags;		// Private EObjectFlags used by object manager.
242 	VClass*					LinkNext;			// Next class in linked list
243 
244 	vint32					ClassSize;
245 	vint32					ClassUnalignedSize;
246 	vuint32					ClassFlags;
247 	VMethod**				ClassVTable;
248 	void					(*ClassConstructor)();
249 
250 	vint32					ClassNumMethods;
251 
252 	VField*					ReferenceFields;
253 	VField*					DestructorFields;
254 	VField*					NetFields;
255 	VMethod*				NetMethods;
256 	VState*					NetStates;
257 	TArray<VState*>			StatesLookup;
258 	vint32					NumNetFields;
259 
260 	vuint8*					Defaults;
261 
262 	VClass*					Replacement;
263 	VClass*					Replacee;
264 
265 	TArray<VDecorateStateAction>	DecorateStateActions;
266 
267 	TArray<VSpriteEffect>	SpriteEffects;
268 
269 	VName					LowerCaseName;
270 	VClass*					LowerCaseHashNext;
271 
272 	static TArray<mobjinfo_t>	GMobjInfos;
273 	static TArray<mobjinfo_t>	GScriptIds;
274 	static TArray<VName>		GSpriteNames;
275 	static VClass*				GLowerCaseHashTable[LOWER_CASE_HASH_SIZE];
276 
277 	//	Structors.
278 	VClass(VName, VMemberBase*, TLocation);
279 	VClass(ENativeConstructor, size_t ASize, vuint32 AClassFlags,
280 		VClass *AParent, EName AName, void(*ACtor)());
281 	~VClass();
282 
283 	// Systemwide functions.
284 	static VClass* FindClass(const char *);
285 	static VClass* FindClassNoCase(const char *);
286 	static VClass* FindClassLowerCase(VName);
287 	static int FindSprite(VName, bool = true);
288 #ifndef IN_VCC
289 	static void GetSpriteNames(TArray<FReplacedString>&);
290 	static void ReplaceSpriteNames(TArray<FReplacedString>&);
291 #endif
292 	static void StaticReinitStatesLookup();
293 
294 	void Serialise(VStream&);
295 	void PostLoad();
296 	void Shutdown();
297 
298 	void AddConstant(VConstant*);
299 	void AddField(VField*);
300 	void AddProperty(VProperty*);
301 	void AddState(VState*);
302 	void AddMethod(VMethod*);
303 
304 	VConstant* FindConstant(VName);
305 	VField* FindField(VName);
306 	VField* FindField(VName, TLocation, VClass*);
307 	VField* FindFieldChecked(VName);
308 	VProperty* FindProperty(VName);
309 	VMethod* FindMethod(VName, bool = true);
310 	VMethod* FindMethodChecked(VName);
311 	int GetMethodIndex(VName);
312 	VState* FindState(VName);
313 	VState* FindStateChecked(VName);
314 	VStateLabel* FindStateLabel(VName, VName = NAME_None, bool = false);
315 	VStateLabel* FindStateLabel(TArray<VName>&, bool);
316 	VStateLabel* FindStateLabelChecked(VName, VName = NAME_None, bool = false);
317 	VStateLabel* FindStateLabelChecked(TArray<VName>&, bool);
318 	VDecorateStateAction* FindDecorateStateAction(VName);
319 
320 	bool Define();
321 	bool DefineMembers();
322 	void Emit();
323 	void DecorateEmit();
324 	void DecoratePostLoad();
325 	void EmitStateLabels();
326 	VState* ResolveStateLabel(TLocation, VName, int);
327 	void SetStateLabel(VName, VState*);
328 	void SetStateLabel(const TArray<VName>&, VState*);
329 
IsChildOf(const VClass * SomeBaseClass)330 	bool IsChildOf(const VClass *SomeBaseClass) const
331 	{
332 		for (const VClass *c = this; c; c = c->GetSuperClass())
333 		{
334 			if (SomeBaseClass == c)
335 			{
336 				return true;
337 			}
338 		}
339 		return false;
340 	}
341 
342 	// Accessors.
GetSuperClass()343 	VClass *GetSuperClass() const
344 	{
345 		return ParentClass;
346 	}
347 
348 #ifndef IN_VCC
349 	void CopyObject(const vuint8*, vuint8*);
350 	void SerialiseObject(VStream&, VObject*);
351 	void CleanObject(VObject*);
352 	void DestructObject(VObject*);
353 	VClass* CreateDerivedClass(VName, VMemberBase*, TLocation);
354 #endif
355 	VClass* GetReplacement();
356 	VClass* GetReplacee();
357 	void HashLowerCased();
358 
359 private:
360 	void CalcFieldOffsets();
361 	void InitNetFields();
362 	void InitReferences();
363 	void InitDestructorFields();
364 	void CreateVTable();
365 	void InitStatesLookup();
366 #ifndef IN_VCC
367 	void CreateDefaults();
368 #endif
369 
370 public:
371 	friend inline VStream& operator<<(VStream& Strm, VClass*& Obj)
372 	{ return Strm << *(VMemberBase**)&Obj; }
373 
374 	friend class VPackage;
375 };
376