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 }
48 return this;
49}
50
51
52#define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
53#define EVAL_entityclass(...) __VA_ARGS__
54#define entityclass_1(name) entityclass_2(name, Object)
55#ifndef QCC_SUPPORT_ENTITYCLASS
56 #define entityclass_2(name, base) USING(name, entity)
57 #define classfield(name)
58 #define _new(class, pure) __spawn(#class, __FILE__ ":" STR(__LINE__), pure)
59#else
60 #define entityclass_2(name, base) entityclass name : base {}
61 #define classfield(name) [[class(name)]]
62 #define _new(class, pure) ((class) __spawn(#class, __FILE__ ":" STR(__LINE__), pure))
63#endif
65#define new(class) _new(class, false)
67#define new_pure(class) _new(class, true)
68#define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
69
71
72#ifndef SVQC
73 #define delete_fn builtin_remove
74#endif
75
76.void(entity this) dtor;
77#define delete(this) MACRO_BEGIN \
78 entity _this = (this); \
79 void(entity) _dtor = _this.dtor; \
80 ONREMOVE(this); \
81 if (_dtor) _dtor(_this); else delete_fn(_this); \
82 /* this = NULL; */ \
83MACRO_END
84
85void IL_REMOVE_RAW(entity it);
87{
88 copyentity(src, dst); // builtin function
89 IL_REMOVE_RAW(dst);
90}
91
98{
99#ifdef CSQC
100 int n = e.entnum;
101#endif
102 bool was_pure = is_pure(e);
104 if (!was_pure) make_impure(e);
105#ifdef CSQC
106 e.entnum = n;
107#endif
108}
109
110// Classes have a `spawn##cname(entity)` constructor
111// The parameter is used across ACCUMULATE functions
112
114
115// Macros to hide this implementation detail:
116#ifdef __STDC__
117 #define NEW(cname, ...) \
118 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
119
120 #define _TRANSMUTE(cname, this, ...) \
121 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
122
123 #define CONSTRUCT(cname, ...) \
124 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
125#else
126 #define NEW(cname, ...) \
127 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
128
129 #define _TRANSMUTE(cname, this, ...) \
130 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
131
132 #define CONSTRUCT(cname, ...) \
133 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
134#endif
135
136#define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
137 entity _e = (this); \
138 if (_e.vtblbase != cname##_vtbl) { \
139 _e.transmute = true; \
140 _e.classname = #cname; \
141 _TRANSMUTE(cname, _e, __VA_ARGS__); \
142 } \
143 MACRO_END
144
145#define CLASS(...) EVAL_CLASS(OVERLOAD__(CLASS, __VA_ARGS__))
146#define EVAL_CLASS(...) __VA_ARGS__
147
148#define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
149#define EVAL_ATTRIB(...) __VA_ARGS__
150
151#ifdef QCC_SUPPORT_CLASS
152
153#warning "QCC_SUPPORT_CLASS not implemented"
154
155#define CLASS_1(name) CLASS_2(name, entity)
156#define CLASS_2(name, base) class name : base {
157
158#define INIT(class) void class::class()
159#define CONSTRUCTOR(class, ...) void class::class(__VA_ARGS__)
160#define DESTRUCTOR(class) class::~class()
161
162#define SUPER(class) super
163
164#define ATTRIB_3(class, name, T) T name
165#define ATTRIB_4(class, name, T, val) ATTRIB_3(class, name, T) = val
166#define STATIC_ATTRIB(class, name, T, val) static T name = val
167
168#define ATTRIB_STRZONE(class, name, T, val) T name = val
169#define STATIC_ATTRIB_STRZONE(class, name, T, val) static T name = val
170
171#define ATTRIBARRAY(class, name, T, val) T name[val]
172
173#define METHOD(class, name, prototype) virtual void class::name()
174#define STATIC_METHOD(class, name, prototype) static void class::name()
175
176#define ENDCLASS(class) };
177
178#else
179
180#define CLASS_1(cname) CLASS_2(cname, )
181#define CLASS_2(cname, base) \
182 entityclass(cname, base); \
183 classfield(cname).bool instanceOf##cname; \
184 DEBUG_STUFF(cname) \
185 VTBL(cname, base) \
186 _INIT_STATIC(cname) \
187 { \
188 if (cname##_vtbl && !this.transmute) \
189 { \
190 copyentity_qc(cname##_vtbl, this); \
191 return; \
192 } \
193 spawn##base##_static(this); \
194 this.instanceOf##cname = true; \
195 } \
196 INIT(cname) \
197 { \
198 /* Only statically initialize the current class, it contains everything it inherits */ \
199 if (cname##_vtbl.vtblname == this.classname) \
200 { \
201 spawn##cname##_static(this); \
202 this.transmute = false; \
203 this.classname = #cname; \
204 this.vtblname = string_null; \
205 this.vtblbase = cname##_vtbl; \
206 } \
207 spawn##base##_1(this); \
208 }
209
210#define INIT(cname) \
211 ACCUMULATE cname spawn##cname##_1(cname this)
212
213#define CONSTRUCTOR(cname, ...) \
214 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
215 { \
216 return = this; \
217 } \
218 ACCUMULATE cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
219
220#define DESTRUCTOR(cname) \
221 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
222 METHOD(cname, dtor, void(cname this)) \
223 { \
224 METHOD_REFERENCE(cname, dtorimpl)(this); \
225 this.instanceOf##cname = false; \
226 entity super = SUPER(cname); \
227 if (super != cname##_vtbl) super.dtor(this); \
228 } \
229 STATIC_METHOD(cname, dtorimpl, void(cname this))
230
231#define SUPER(cname) (cname##_vtbl.vtblbase)
232
233#define ATTRIB_3(cname, name, type) classfield(cname) .type name
234#define ATTRIB_4(cname, name, type, val) \
235 ATTRIB_3(cname, name, type); \
236 INIT(cname) \
237 { \
238 noref bool strzone; /* Error on strzone() calls. */ \
239 this.name = val; \
240 } \
241 ATTRIB_3(cname, name, type)
242
243#define STATIC_ATTRIB(cname, name, type, val) \
244 type cname##_##name; \
245 _INIT_STATIC(cname) \
246 { \
247 noref bool strzone; /* Error on strzone() calls. */ \
248 cname##_##name = val; \
249 }
250
251// cleanup potentially zoned strings from base classes
252#define ATTRIB_STRZONE(cname, name, type, val) \
253 classfield(cname).type name; \
254 INIT(cname) \
255 { \
256 strcpy(this.name, val); \
257 }
258
259#define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
260 type cname##_##name; \
261 _INIT_STATIC(cname) \
262 { \
263 strcpy(cname##_##name, val); \
264 }
265
266#define ATTRIBARRAY(cname, name, type, cnt) \
267 classfield(cname) .type name[cnt]
268
269#define METHOD(cname, name, prototype) \
270 STATIC_METHOD(cname, name, prototype); \
271 classfield(cname) .prototype name; \
272 _INIT_STATIC(cname) \
273 { \
274 this.name = METHOD_REFERENCE(cname, name); \
275 } \
276 STATIC_METHOD(cname, name, prototype)
277
278#define STATIC_METHOD(cname, name, prototype) \
279 prototype METHOD_REFERENCE(cname, name)
280
281#define ENDCLASS(cname) \
282 INIT(cname) \
283 { \
284 return this; \
285 }
286
287// impl
288
289.string vtblname;
290.entity vtblbase;
291
297
298#define VTBL(cname, base) \
299 _INIT_STATIC(cname); \
300 entity cname##_vtbl; \
301 void cname##_vtbl_init() \
302 { \
303 cname e = new_pure(vtbl); \
304 spawn##cname##_static(e); \
305 e.vtblname = #cname; \
306 /* Top level objects refer to themselves */ \
307 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
308 cname##_vtbl = e; \
309 } \
310 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
311
312#define _INIT_STATIC(cname) ACCUMULATE void spawn##cname##_static(cname this)
313
314#if NDEBUG
315 #define DEBUG_STUFF(cname)
316#else
317 #define DEBUG_STUFF(cname) \
318 ERASEABLE bool is_##cname(entity e) { return e.instanceOf##cname; } \
319 ERASEABLE void isnt_##cname(entity e) { eprint(e); }
320#endif
321
322#define METHOD_REFERENCE(cname, name) \
323 cname##_##name
324
325#endif
326
327#define spawn_static(this)
328#define spawn_1(this)
329#define _vtbl NULL
331 DESTRUCTOR(Object) { builtin_remove(this); }
332 #define remove(this) delete(this)
333 METHOD(Object, describe, string(Object this))
334 {
335 TC(Object, this);
336 string s = _("No description");
337 if (autocvar_developer > 0)
338 {
339 for (int i = 0, n = numentityfields(); i < n; ++i)
340 {
341 string value = getentityfieldstring(i, this);
342 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
343 }
344 }
345 return s;
346 }
347 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
348 {
349 TC(Object, this);
350 returns(sprintf("entity %i", this), "nopreview_map");
351 }
353#undef spawn_static
354#undef spawn_1
355#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:96
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:113
#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)
void clearentity(entity e)
Definition oo.qh:97
ACCUMULATE void ONREMOVE(entity this)
Definition oo.qh:70
#define CLASS(...)
Definition oo.qh:145
#define ENDCLASS(cname)
Definition oo.qh:281
void RegisterClasses()
Definition oo.qh:292
#define DESTRUCTOR(cname)
Definition oo.qh:220
#define new_pure(class)
purely logical entities (not linked to the area grid)
Definition oo.qh:67
bool pure_data
Definition oo.qh:9
#define METHOD(cname, name, prototype)
Definition oo.qh:269
entity _clearentity_ent
Definition oo.qh:92
entity _spawn()
void copyentity_qc(entity src, entity dst)
Definition oo.qh:86
entity vtblbase
Definition oo.qh:290
string vtblname
Definition oo.qh:289
string sourceLoc
Location entity was spawned from in source.
Definition oo.qh:27
#define STATIC_INIT(func)
during worldspawn
Definition static.qh:32