Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
oo.qh
Go to the documentation of this file.
1#pragma once
2
3#include "misc.qh"
4#include "nil.qh"
5#include "static.qh"
6
7// pure entities: not linked to the area grid
8// see: sv_areagrid_link_SOLID_NOT and cl_areagrid_link_SOLID_NOT
10
11#ifdef GAMEQC
13 #define make_pure(e) \
14 (e).pure_data = true; \
15 (e).solid = SOLID_NOT;
16 #define is_pure(e) ((e).pure_data && (e).solid == SOLID_NOT)
17#else
18 #define make_pure(e) \
19 (e).pure_data = true;
20 #define is_pure(e) ((e).pure_data)
21#endif // GAMEQC
22#define make_impure(e) \
23 (e).pure_data = false;
24
25.string classname;
27.string sourceLoc;
29
30#ifndef SPAWN_PURE
31#define SPAWN_PURE 0
32#endif
33
34#if SPAWN_PURE
35entity spawn_pure() = #600;
36#else
37#define spawn_pure() _spawn()
38#endif
39
40entity __spawn(string _classname, string _sourceLoc, bool pure)
41{
42 entity this = pure ? spawn_pure() : _spawn();
43 this.classname = _classname;
44 this.sourceLoc = _sourceLoc;
45 if (pure)
46 make_pure(this);
47 return this;
48}
49
50
51#define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
52#define EVAL_entityclass(...) __VA_ARGS__
53#define entityclass_1(name) entityclass_2(name, Object)
54#ifndef QCC_SUPPORT_ENTITYCLASS
55 #define entityclass_2(name, base) USING(name, entity)
56 #define classfield(name)
57 #define _new(class, pure) __spawn(#class, __FILE__ ":" STR(__LINE__), pure)
58#else
59 #define entityclass_2(name, base) entityclass name : base {}
60 #define classfield(name) [[class(name)]]
61 #define _new(class, pure) ((class) __spawn(#class, __FILE__ ":" STR(__LINE__), pure))
62#endif
64#define new(class) _new(class, false)
66#define new_pure(class) _new(class, true)
67#define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
68
70
71#ifndef SVQC
72 #define delete_fn builtin_remove
73#endif
74
75.void(entity this) dtor;
76#define delete(this) MACRO_BEGIN \
77 entity _this = (this); \
78 void(entity) _dtor = _this.dtor; \
79 ONREMOVE(this); \
80 if (_dtor) \
81 _dtor(_this); \
82 else \
83 delete_fn(_this); \
84 /* this = NULL; */ \
85MACRO_END
86
87void IL_REMOVE_RAW(entity it);
89{
90 copyentity(src, dst); // builtin function
91 IL_REMOVE_RAW(dst);
92}
93
100{
101#ifdef CSQC
102 int n = e.entnum;
103#endif
104 bool was_pure = is_pure(e);
106 if (!was_pure)
107 make_impure(e);
108#ifdef CSQC
109 e.entnum = n;
110#endif
111}
112
113// Classes have a `spawn##cname(entity)` constructor
114// The parameter is used across ACCUMULATE functions
115
117
118// Macros to hide this implementation detail:
119#ifdef __STDC__
120 #define NEW(cname, ...) \
121 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
122
123 #define _TRANSMUTE(cname, this, ...) \
124 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
125
126 #define CONSTRUCT(cname, ...) \
127 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
128#else
129 #define NEW(cname, ...) \
130 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
131
132 #define _TRANSMUTE(cname, this, ...) \
133 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
134
135 #define CONSTRUCT(cname, ...) \
136 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
137#endif
138
139#define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
140 entity _e = (this); \
141 if (_e.vtblbase != cname##_vtbl) \
142 { \
143 _e.transmute = true; \
144 _e.classname = #cname; \
145 _TRANSMUTE(cname, _e, __VA_ARGS__); \
146 } \
147MACRO_END
148
149#define CLASS(...) EVAL_CLASS(OVERLOAD__(CLASS, __VA_ARGS__))
150#define EVAL_CLASS(...) __VA_ARGS__
151
152#define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
153#define EVAL_ATTRIB(...) __VA_ARGS__
154
155#ifdef QCC_SUPPORT_CLASS
156
157#warning "QCC_SUPPORT_CLASS not implemented"
158
159#define CLASS_1(name) CLASS_2(name, entity)
160#define CLASS_2(name, base) class name : base {
161
162#define INIT(class) void class::class()
163#define CONSTRUCTOR(class, ...) void class::class(__VA_ARGS__)
164#define DESTRUCTOR(class) class::~class()
165
166#define SUPER(class) super
167
168#define ATTRIB_3(class, name, T) T name
169#define ATTRIB_4(class, name, T, val) ATTRIB_3(class, name, T) = val
170#define STATIC_ATTRIB(class, name, T, val) static T name = val
171
172#define ATTRIB_STRZONE(class, name, T, val) T name = val
173#define STATIC_ATTRIB_STRZONE(class, name, T, val) static T name = val
174
175#define ATTRIBARRAY(class, name, T, val) T name[val]
176
177#define METHOD(class, name, prototype) virtual void class::name()
178#define STATIC_METHOD(class, name, prototype) static void class::name()
179
180#define ENDCLASS(class) };
181
182#else
183
184#define CLASS_1(cname) CLASS_2(cname, )
185#define CLASS_2(cname, base) \
186 entityclass(cname, base); \
187 classfield(cname).bool instanceOf##cname; \
188 DEBUG_STUFF(cname) \
189 VTBL(cname, base) \
190 _INIT_STATIC(cname) \
191 { \
192 if (cname##_vtbl && !this.transmute) \
193 { \
194 copyentity_qc(cname##_vtbl, this); \
195 return; \
196 } \
197 spawn##base##_static(this); \
198 this.instanceOf##cname = true; \
199 } \
200 INIT(cname) \
201 { \
202 /* Only statically initialize the current class, it contains everything it inherits */ \
203 if (cname##_vtbl.vtblname == this.classname) \
204 { \
205 spawn##cname##_static(this); \
206 this.transmute = false; \
207 this.classname = #cname; \
208 this.vtblname = string_null; \
209 this.vtblbase = cname##_vtbl; \
210 } \
211 spawn##base##_1(this); \
212 }
213
214#define INIT(cname) \
215 ACCUMULATE cname spawn##cname##_1(cname this)
216
217#define CONSTRUCTOR(cname, ...) \
218 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
219 { \
220 return = this; \
221 } \
222 ACCUMULATE cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
223
224#define DESTRUCTOR(cname) \
225 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
226 METHOD(cname, dtor, void(cname this)) \
227 { \
228 METHOD_REFERENCE(cname, dtorimpl)(this); \
229 this.instanceOf##cname = false; \
230 entity super = SUPER(cname); \
231 if (super != cname##_vtbl) \
232 super.dtor(this); \
233 } \
234 STATIC_METHOD(cname, dtorimpl, void(cname this))
235
236#define SUPER(cname) (cname##_vtbl.vtblbase)
237
238#define ATTRIB_3(cname, name, type) classfield(cname) .type name
239#define ATTRIB_4(cname, name, type, val) \
240 ATTRIB_3(cname, name, type); \
241 INIT(cname) \
242 { \
243 noref bool strzone; /* Error on strzone() calls. */ \
244 this.name = val; \
245 } \
246 ATTRIB_3(cname, name, type)
247
248#define STATIC_ATTRIB(cname, name, type, val) \
249 type cname##_##name; \
250 _INIT_STATIC(cname) \
251 { \
252 noref bool strzone; /* Error on strzone() calls. */ \
253 cname##_##name = val; \
254 }
255
256// cleanup potentially zoned strings from base classes
257#define ATTRIB_STRZONE(cname, name, type, val) \
258 classfield(cname).type name; \
259 INIT(cname) \
260 { \
261 strcpy(this.name, val); \
262 }
263
264#define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
265 type cname##_##name; \
266 _INIT_STATIC(cname) \
267 { \
268 strcpy(cname##_##name, val); \
269 }
270
271#define ATTRIBARRAY(cname, name, type, cnt) \
272 classfield(cname) .type name[cnt]
273
274#define METHOD(cname, name, prototype) \
275 STATIC_METHOD(cname, name, prototype); \
276 classfield(cname) .prototype name; \
277 _INIT_STATIC(cname) \
278 { \
279 this.name = METHOD_REFERENCE(cname, name); \
280 } \
281 STATIC_METHOD(cname, name, prototype)
282
283#define STATIC_METHOD(cname, name, prototype) \
284 prototype METHOD_REFERENCE(cname, name)
285
286#define ENDCLASS(cname) \
287 INIT(cname) \
288 { \
289 return this; \
290 }
291
292// impl
293
294.string vtblname;
295.entity vtblbase;
296
302
303#define VTBL(cname, base) \
304 _INIT_STATIC(cname); \
305 entity cname##_vtbl; \
306 void cname##_vtbl_init() \
307 { \
308 cname e = new_pure(vtbl); \
309 spawn##cname##_static(e); \
310 e.vtblname = #cname; \
311 /* Top level objects refer to themselves */ \
312 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
313 cname##_vtbl = e; \
314 } \
315 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
316
317#define _INIT_STATIC(cname) ACCUMULATE void spawn##cname##_static(cname this)
318
319#if NDEBUG
320 #define DEBUG_STUFF(cname)
321#else
322 #define DEBUG_STUFF(cname) \
323 ERASEABLE bool is_##cname(entity e) \
324 { \
325 return e.instanceOf##cname; \
326 } \
327 ERASEABLE void isnt_##cname(entity e) \
328 { \
329 eprint(e); \
330 }
331#endif
332
333#define METHOD_REFERENCE(cname, name) \
334 cname##_##name
335
336#endif
337
338#define spawn_static(this)
339#define spawn_1(this)
340#define _vtbl NULL
343 {
344 builtin_remove(this);
345 }
346 #define remove(this) delete(this)
347 METHOD(Object, describe, string(Object this))
348 {
349 TC(Object, this);
350 string s = _("No description");
351 if (autocvar_developer > 0)
352 for (int i = 0, n = numentityfields(); i < n; ++i)
353 {
354 string value = getentityfieldstring(i, this);
355 if (value != "")
356 s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
357 }
358 return s;
359 }
360 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
361 {
362 TC(Object, this);
363 returns(sprintf("entity %i", this), "nopreview_map");
364 }
366#undef spawn_static
367#undef spawn_1
368#undef _vtbl
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
string classname
#define ACCUMULATE
Definition _all.inc:31
#define TC(T, sym)
Definition _all.inc:82
noref int autocvar_developer
Definition log.qh:93
string name
Definition menu.qh:30
void copyentity(entity src, entity dst)
#define is_pure(e)
Definition oo.qh:16
entity __spawn(string _classname, string _sourceLoc, bool pure)
Definition oo.qh:40
bool transmute
Definition oo.qh:116
#define make_impure(e)
Definition oo.qh:22
#define spawn_pure()
Definition oo.qh:37
#define make_pure(e)
direct use is
Definition oo.qh:13
void IL_REMOVE_RAW(entity it)
Clears any IL data from an entity (not an intrusive list).
void clearentity(entity e)
Definition oo.qh:99
ACCUMULATE void ONREMOVE(entity this)
Definition oo.qh:69
#define CLASS(...)
Definition oo.qh:149
#define ENDCLASS(cname)
Definition oo.qh:286
void RegisterClasses()
Definition oo.qh:297
#define DESTRUCTOR(cname)
Definition oo.qh:224
#define new_pure(class)
purely logical entities (not linked to the area grid)
Definition oo.qh:66
bool pure_data
Definition oo.qh:9
#define METHOD(cname, name, prototype)
Definition oo.qh:274
entity _clearentity_ent
Definition oo.qh:94
entity _spawn()
void copyentity_qc(entity src, entity dst)
Definition oo.qh:88
entity vtblbase
Definition oo.qh:295
string vtblname
Definition oo.qh:294
string sourceLoc
Location entity was spawned from in source.
Definition oo.qh:27
#define STATIC_INIT(func)
during worldspawn
Definition static.qh:33