7 // pure entities: not linked to the area grid
8 // see: sv_areagrid_link_SOLID_NOT and cl_areagrid_link_SOLID_NOT
12 /** direct use is @deprecated use new_pure or NEW(class) */
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)
18 #define make_pure(e) \
20 #define is_pure(e) ((e).pure_data)
22 #define make_impure(e) \
23 (e).pure_data = false;
26 /** Location entity was spawned from in source */
35 entity spawn_pure() = #600;
37 #define spawn_pure() _spawn()
40 entity __spawn(string _classname, string _sourceLoc, bool pure)
42 entity this = pure ? spawn_pure() : _spawn();
43 this.classname = _classname;
44 this.sourceLoc = _sourceLoc;
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)
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))
64 /** entities you care about collisions with */
65 #define new(class) _new(class, false)
66 /** purely logical entities (not linked to the area grid) */
67 #define new_pure(class) _new(class, true)
68 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
70 ACCUMULATE void ONREMOVE(entity this) {}
73 #define delete_fn builtin_remove
76 .void(entity this) dtor;
77 #define delete(this) MACRO_BEGIN \
78 entity _this = (this); \
79 void(entity) _dtor = _this.dtor; \
81 if (_dtor) _dtor(_this); else delete_fn(_this); \
85 void IL_REMOVE_RAW(entity it);
86 void copyentity_qc(entity src, entity dst)
88 copyentity(src, dst); // builtin function
92 entity _clearentity_ent;
93 STATIC_INIT(clearentity)
95 _clearentity_ent = new_pure(clearentity);
97 void clearentity(entity e)
102 bool was_pure = is_pure(e);
103 copyentity_qc(_clearentity_ent, e);
104 if (!was_pure) make_impure(e);
110 // Classes have a `spawn##cname(entity)` constructor
111 // The parameter is used across ACCUMULATE functions
115 // Macros to hide this implementation detail:
117 #define NEW(cname, ...) \
118 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
120 #define _TRANSMUTE(cname, this, ...) \
121 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
123 #define CONSTRUCT(cname, ...) \
124 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
126 #define NEW(cname, ...) \
127 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
129 #define _TRANSMUTE(cname, this, ...) \
130 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
132 #define CONSTRUCT(cname, ...) \
133 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
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__); \
145 #define CLASS(...) EVAL_CLASS(OVERLOAD__(CLASS, __VA_ARGS__))
146 #define EVAL_CLASS(...) __VA_ARGS__
148 #define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
149 #define EVAL_ATTRIB(...) __VA_ARGS__
151 #ifdef QCC_SUPPORT_CLASS
153 #warning "QCC_SUPPORT_CLASS not implemented"
155 #define CLASS_1(name) CLASS_2(name, entity)
156 #define CLASS_2(name, base) class name : base {
158 #define INIT(class) void class::class()
159 #define CONSTRUCTOR(class, ...) void class::class(__VA_ARGS__)
160 #define DESTRUCTOR(class) class::~class()
162 #define SUPER(class) super
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
168 #define ATTRIB_STRZONE(class, name, T, val) T name = val
169 #define STATIC_ATTRIB_STRZONE(class, name, T, val) static T name = val
171 #define ATTRIBARRAY(class, name, T, val) T name[val]
173 #define METHOD(class, name, prototype) virtual void class::name()
174 #define STATIC_METHOD(class, name, prototype) static void class::name()
176 #define ENDCLASS(class) };
180 #define CLASS_1(cname) CLASS_2(cname, )
181 #define CLASS_2(cname, base) \
182 entityclass(cname, base); \
183 classfield(cname).bool instanceOf##cname; \
186 _INIT_STATIC(cname) \
188 if (cname##_vtbl && !this.transmute) \
190 copyentity_qc(cname##_vtbl, this); \
193 spawn##base##_static(this); \
194 this.instanceOf##cname = true; \
198 /* Only statically initialize the current class, it contains everything it inherits */ \
199 if (cname##_vtbl.vtblname == this.classname) \
201 spawn##cname##_static(this); \
202 this.transmute = false; \
203 this.classname = #cname; \
204 this.vtblname = string_null; \
205 this.vtblbase = cname##_vtbl; \
207 spawn##base##_1(this); \
210 #define INIT(cname) \
211 ACCUMULATE cname spawn##cname##_1(cname this)
213 #define CONSTRUCTOR(cname, ...) \
214 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
218 ACCUMULATE cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
220 #define DESTRUCTOR(cname) \
221 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
222 METHOD(cname, dtor, void(cname this)) \
224 METHOD_REFERENCE(cname, dtorimpl)(this); \
225 this.instanceOf##cname = false; \
226 entity super = SUPER(cname); \
227 if (super != cname##_vtbl) super.dtor(this); \
229 STATIC_METHOD(cname, dtorimpl, void(cname this))
231 #define SUPER(cname) (cname##_vtbl.vtblbase)
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); \
238 noref bool strzone; /* Error on strzone() calls. */ \
241 ATTRIB_3(cname, name, type)
243 #define STATIC_ATTRIB(cname, name, type, val) \
244 type cname##_##name; \
245 _INIT_STATIC(cname) \
247 noref bool strzone; /* Error on strzone() calls. */ \
248 cname##_##name = val; \
251 // cleanup potentially zoned strings from base classes
252 #define ATTRIB_STRZONE(cname, name, type, val) \
253 classfield(cname).type name; \
256 strcpy(this.name, val); \
259 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
260 type cname##_##name; \
261 _INIT_STATIC(cname) \
263 strcpy(cname##_##name, val); \
266 #define ATTRIBARRAY(cname, name, type, cnt) \
267 classfield(cname) .type name[cnt]
269 #define METHOD(cname, name, prototype) \
270 STATIC_METHOD(cname, name, prototype); \
271 classfield(cname) .prototype name; \
272 _INIT_STATIC(cname) \
274 this.name = METHOD_REFERENCE(cname, name); \
276 STATIC_METHOD(cname, name, prototype)
278 #define STATIC_METHOD(cname, name, prototype) \
279 prototype METHOD_REFERENCE(cname, name)
281 #define ENDCLASS(cname) \
292 void RegisterClasses() {}
293 STATIC_INIT(RegisterClasses)
298 #define VTBL(cname, base) \
299 _INIT_STATIC(cname); \
300 entity cname##_vtbl; \
301 void cname##_vtbl_init() \
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; \
310 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
312 #define _INIT_STATIC(cname) ACCUMULATE void spawn##cname##_static(cname this)
315 #define DEBUG_STUFF(cname)
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); }
322 #define METHOD_REFERENCE(cname, name) \
327 #define spawn_static(this)
328 #define spawn_1(this)
331 DESTRUCTOR(Object) { builtin_remove(this); }
332 #define remove(this) delete(this)
333 METHOD(Object, describe, string(Object this))
336 string s = _("No description");
337 if (cvar("developer") > 0)
339 for (int i = 0, n = numentityfields(); i < n; ++i)
341 string value = getentityfieldstring(i, this);
342 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
347 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
350 returns(sprintf("entity %i", this), "nopreview_map");