9 /** @deprecated use new_pure or NEW(class) */
10 #define make_pure(e) \
13 (e).pure_data = true; \
15 #define make_impure(e) \
18 (e).pure_data = false; \
20 #define is_pure(e) ((e).pure_data)
23 /** Location entity was spawned from in source */
31 // pure entities: need no .origin
33 entity spawn_pure() = #600;
35 #define spawn_pure() _spawn()
38 entity __spawn(string _classname, string _sourceLoc, bool pure)
40 entity this = pure ? spawn_pure() : _spawn();
41 this.classname = _classname;
42 this.sourceLoc = _sourceLoc;
46 setorigin(this, (world.mins + world.maxs) * 0.5);
49 setorigin(this, (world.mins + world.maxs) * 0.5);
56 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
57 #define EVAL_entityclass(...) __VA_ARGS__
58 #define entityclass_1(name) entityclass_2(name, Object)
59 #ifndef QCC_SUPPORT_ENTITYCLASS
60 #define entityclass_2(name, base) USING(name, entity)
62 #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
64 #define entityclass_2(name, base) entityclass name : base {}
65 #define class(name) [[class(name)]]
66 #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
68 /** entities you care about seeing (.origin works) */
69 #define new(class) _new(class, false)
70 /** purely logical entities (.origin doesn't work) */
71 #define new_pure(class) _new(class, true)
72 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
74 [[accumulate]] void ONREMOVE(entity this) {}
77 #define delete_fn builtin_remove
80 #define delete(this) MACRO_BEGIN { \
81 entity _this = (this); \
82 void(entity) _dtor = _this.dtor; \
84 if (_dtor) _dtor(_this); else delete_fn(_this); \
88 entity _clearentity_ent;
89 STATIC_INIT(clearentity)
91 _clearentity_ent = new_pure(clearentity);
93 void clearentity(entity e)
98 bool was_pure = is_pure(e);
99 copyentity(_clearentity_ent, e);
100 if (!was_pure) make_impure(e);
106 // Classes have a `spawn##cname(entity)` constructor
107 // The parameter is used across [[accumulate]] functions
111 // Macros to hide this implementation detail:
113 #define NEW(cname, ...) \
114 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
116 #define _TRANSMUTE(cname, this, ...) \
117 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
119 #define CONSTRUCT(cname, ...) \
120 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
122 #define NEW(cname, ...) \
123 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
125 #define _TRANSMUTE(cname, this, ...) \
126 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
128 #define CONSTRUCT(cname, ...) \
129 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
132 #define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
133 entity _e = (this); \
134 if (_e.vtblbase != cname##_vtbl) { \
135 _e.transmute = true; \
136 _e.classname = #cname; \
137 _TRANSMUTE(cname, _e, __VA_ARGS__); \
141 #define CONSTRUCTOR(cname, ...) \
142 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
146 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
151 void RegisterClasses() {}
152 STATIC_INIT(RegisterClasses)
157 #define VTBL(cname, base) \
158 _INIT_STATIC(cname); \
159 entity cname##_vtbl; \
160 void cname##_vtbl_init() \
162 cname e = new_pure(vtbl); \
163 spawn##cname##_static(e); \
164 e.vtblname = #cname; \
165 /* Top level objects refer to themselves */ \
166 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
169 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
171 #define _INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
172 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
175 #define DEBUG_STUFF(cname)
177 #define DEBUG_STUFF(cname) \
178 bool is_##cname(entity e) { return e.instanceOf##cname; } \
179 void isnt_##cname(entity e) { eprint(e); }
183 #define CLASS(cname, base) \
184 entityclass(cname, base); \
185 class(cname).bool instanceOf##cname; \
188 _INIT_STATIC(cname) \
190 if (cname##_vtbl && !this.transmute)\
192 copyentity(cname##_vtbl, this); \
195 spawn##base##_static(this); \
196 this.instanceOf##cname = true; \
200 /* Only statically initialize the current class, it contains everything it inherits */ \
201 if (cname##_vtbl.vtblname == this.classname) \
203 spawn##cname##_static(this); \
204 this.transmute = false; \
205 this.classname = #cname; \
206 this.vtblname = string_null; \
207 this.vtblbase = cname##_vtbl; \
209 spawn##base##_1(this); \
212 #define METHOD_REFERENCE(cname, name) \
215 #define STATIC_METHOD(cname, name, prototype) \
216 prototype METHOD_REFERENCE(cname, name)
218 #define METHOD(cname, name, prototype) \
219 STATIC_METHOD(cname, name, prototype); \
220 class(cname) .prototype name; \
221 _INIT_STATIC(cname) \
223 this.name = METHOD_REFERENCE(cname, name); \
225 STATIC_METHOD(cname, name, prototype)
227 #define DESTRUCTOR(cname) \
228 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
229 METHOD(cname, dtor, void(cname this)) \
231 METHOD_REFERENCE(cname, dtorimpl)(this); \
232 this.instanceOf##cname = false; \
233 entity super = SUPER(cname); \
234 if (super != cname##_vtbl) super.dtor(this); \
236 STATIC_METHOD(cname, dtorimpl, void(cname this))
238 #define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
239 #define EVAL_ATTRIB(...) __VA_ARGS__
240 #define ATTRIB_3(cname, name, type) INIT(cname) {} class(cname) .type name
241 #define ATTRIB_4(cname, name, type, val) \
242 ATTRIB_3(cname, name, type); \
245 noref bool strzone; /* Error on strzone() calls. */ \
248 ATTRIB_3(cname, name, type)
250 #define STATIC_ATTRIB(cname, name, type, val) \
251 type cname##_##name; \
252 _INIT_STATIC(cname) \
254 noref bool strzone; /* Error on strzone() calls. */ \
255 cname##_##name = val; \
258 // cleanup potentially zoned strings from base classes
260 #define ATTRIB_STRZONE(cname, name, type, val) \
261 class(cname).type name; \
265 strunzone(this.name); \
266 this.name = strzone(val); \
269 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
270 type cname##_##name; \
271 _INIT_STATIC(cname) \
273 if (cname##_##name) \
274 strunzone(cname##_##name); \
275 cname##_##name = val; \
278 #define ATTRIBARRAY(cname, name, type, cnt) \
279 class(cname) .type name[cnt]
281 #define ENDCLASS(cname) \
287 #define SUPER(cname) (cname##_vtbl.vtblbase)
289 #define spawn_static(this)
290 #define spawn_1(this)
293 DESTRUCTOR(Object) { builtin_remove(this); }
294 #define remove(this) delete(this)
295 METHOD(Object, describe, string(Object this))
298 string s = _("No description");
299 if (cvar("developer"))
301 for (int i = 0, n = numentityfields(); i < n; ++i)
303 string value = getentityfieldstring(i, this);
304 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
309 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
312 returns(sprintf("entity %i", this), "nopreview_map");