8 #define NULL (0, null_entity)
11 #define NULL (0, world)
16 /** @deprecated use new_pure or NEW(class) */
17 #define make_pure(e) \
20 (e).pure_data = true; \
22 #define make_impure(e) \
25 (e).pure_data = false; \
27 #define is_pure(e) ((e).pure_data)
30 /** Location entity was spawned from in source */
38 // pure entities: need no .origin
40 entity spawn_pure() = #600;
42 #define spawn_pure() _spawn()
45 entity __spawn(string _classname, string _sourceLoc, bool pure)
47 entity this = pure ? spawn_pure() : _spawn();
48 this.classname = _classname;
49 this.sourceLoc = _sourceLoc;
53 setorigin(this, '0 0 10000');
56 setorigin(this, '0 0 -10000');
63 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
64 #define EVAL_entityclass(...) __VA_ARGS__
65 #define entityclass_1(name) entityclass_2(name, Object)
66 #ifndef QCC_SUPPORT_ENTITYCLASS
67 #define entityclass_2(name, base) USING(name, entity)
69 #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
71 #define entityclass_2(name, base) entityclass name : base {}
72 #define class(name) [[class(name)]]
73 #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
75 /** entities you care about seeing (.origin works) */
76 #define new(class) _new(class, false)
77 /** purely logical entities (.origin doesn't work) */
78 #define new_pure(class) _new(class, true)
79 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
81 #define delete(this) MACRO_BEGIN { \
82 entity _this = (this); \
83 void(entity) _dtor = _this.dtor; \
84 if (_dtor) _dtor(_this); else remove(_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)
174 #define CLASS(cname, base) \
175 entityclass(cname, base); \
176 class(cname).bool instanceOf##cname; \
177 bool is_##cname(entity e) { return e.instanceOf##cname; } \
178 void isnt_##cname(entity e) { eprint(e); } \
180 _INIT_STATIC(cname) \
182 if (cname##_vtbl && !this.transmute)\
184 copyentity(cname##_vtbl, this); \
187 spawn##base##_static(this); \
188 this.instanceOf##cname = true; \
192 /* Only statically initialize the current class, it contains everything it inherits */ \
193 if (cname##_vtbl.vtblname == this.classname) \
195 spawn##cname##_static(this); \
196 this.transmute = false; \
197 this.classname = #cname; \
198 this.vtblname = string_null; \
199 this.vtblbase = cname##_vtbl; \
201 spawn##base##_1(this); \
204 #define METHOD_REFERENCE(cname, name) \
207 #define STATIC_METHOD(cname, name, prototype) \
208 prototype METHOD_REFERENCE(cname, name)
210 #define METHOD(cname, name, prototype) \
211 STATIC_METHOD(cname, name, prototype); \
212 class(cname) .prototype name; \
213 _INIT_STATIC(cname) \
215 this.name = METHOD_REFERENCE(cname, name); \
217 STATIC_METHOD(cname, name, prototype)
219 #define DESTRUCTOR(cname) \
220 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
221 METHOD(cname, dtor, void(cname this)) \
223 METHOD_REFERENCE(cname, dtorimpl)(this); \
224 this.instanceOf##cname = false; \
225 entity super = SUPER(cname); \
226 if (super != cname##_vtbl) super.dtor(this); \
228 STATIC_METHOD(cname, dtorimpl, void(cname this))
230 #define ATTRIB(cname, name, type, val) \
231 class(cname).type name; \
234 noref bool strzone; /* Error on strzone() calls. */ \
238 #define STATIC_ATTRIB(cname, name, type, val) \
239 type cname##_##name; \
240 _INIT_STATIC(cname) \
242 noref bool strzone; /* Error on strzone() calls. */ \
243 cname##_##name = val; \
246 // cleanup potentially zoned strings from base classes
248 #define ATTRIB_STRZONE(cname, name, type, val) \
249 class(cname).type name; \
253 strunzone(this.name); \
254 this.name = strzone(val); \
257 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
258 type cname##_##name; \
259 _INIT_STATIC(cname) \
261 if (cname##_##name) \
262 strunzone(cname##_##name); \
263 cname##_##name = val; \
266 #define ATTRIBARRAY(cname, name, type, cnt) \
267 class(cname).type name[cnt];
269 #define ENDCLASS(cname) \
275 #define SUPER(cname) (cname##_vtbl.vtblbase)
277 #define spawn_static(this)
278 #define spawn_1(this)
281 DESTRUCTOR(Object) { remove(this); }
282 #define remove(this) delete(this)
283 METHOD(Object, describe, string(Object this))
286 string s = _("No description");
287 if (cvar("developer"))
289 for (int i = 0, n = numentityfields(); i < n; ++i)
291 string value = getentityfieldstring(i, this);
292 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
297 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
300 returns(sprintf("entity %i", this), "nopreview_map");