8 #define NULL (0, null_entity)
10 #define NULL (0, world)
15 /** @deprecated use new_pure or NEW(class) */
16 #define make_pure(e) \
19 (e).pure_data = true; \
21 #define make_impure(e) \
24 (e).pure_data = false; \
26 #define is_pure(e) ((e).pure_data)
29 /** Location entity was spawned from in source */
37 // pure entities: need no .origin
39 entity spawn_pure() = #600;
41 #define spawn_pure() _spawn()
44 entity __spawn(string _classname, string _sourceLoc, bool pure)
46 entity this = pure ? spawn_pure() : _spawn();
47 this.classname = _classname;
48 this.sourceLoc = _sourceLoc;
52 setorigin(this, '0 0 10000');
55 setorigin(this, '0 0 -10000');
62 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
63 #define EVAL_entityclass(...) __VA_ARGS__
64 #define entityclass_1(name) entityclass_2(name, Object)
65 #ifndef QCC_SUPPORT_ENTITYCLASS
66 #define entityclass_2(name, base) USING(name, entity)
68 #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
70 #define entityclass_2(name, base) entityclass name : base {}
71 #define class(name) [[class(name)]]
72 #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
74 /** entities you care about seeing (.origin works) */
75 #define new(class) _new(class, false)
76 /** purely logical entities (.origin doesn't work) */
77 #define new_pure(class) _new(class, true)
78 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
80 #define delete(this) MACRO_BEGIN { \
81 entity _this = (this); \
82 void(entity) _dtor = _this.dtor; \
83 if (_dtor) _dtor(_this); else remove(_this); \
87 entity _clearentity_ent;
88 STATIC_INIT(clearentity)
90 _clearentity_ent = new_pure(clearentity);
92 void clearentity(entity e)
97 bool was_pure = is_pure(e);
98 copyentity(_clearentity_ent, e);
99 if (!was_pure) make_impure(e);
105 // Classes have a `spawn##cname(entity)` constructor
106 // The parameter is used across [[accumulate]] functions
110 // Macros to hide this implementation detail:
112 #define NEW(cname, ...) \
113 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
115 #define _TRANSMUTE(cname, this, ...) \
116 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
118 #define CONSTRUCT(cname, ...) \
119 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
121 #define NEW(cname, ...) \
122 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
124 #define _TRANSMUTE(cname, this, ...) \
125 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
127 #define CONSTRUCT(cname, ...) \
128 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
131 #define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
132 entity _e = (this); \
133 if (_e.vtblbase != cname##_vtbl) { \
134 _e.transmute = true; \
135 _e.classname = #cname; \
136 _TRANSMUTE(cname, _e, __VA_ARGS__); \
140 #define CONSTRUCTOR(cname, ...) \
141 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
145 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
150 void RegisterClasses() {}
151 STATIC_INIT(RegisterClasses)
156 #define VTBL(cname, base) \
157 _INIT_STATIC(cname); \
158 entity cname##_vtbl; \
159 void cname##_vtbl_init() \
161 cname e = new_pure(vtbl); \
162 spawn##cname##_static(e); \
163 e.vtblname = #cname; \
164 /* Top level objects refer to themselves */ \
165 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
168 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
170 #define _INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
171 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
173 #define CLASS(cname, base) \
174 entityclass(cname, base); \
175 class(cname).bool instanceOf##cname; \
176 bool is_##cname(entity e) { return e.instanceOf##cname; } \
178 _INIT_STATIC(cname) \
180 if (cname##_vtbl && !this.transmute)\
182 copyentity(cname##_vtbl, this); \
185 spawn##base##_static(this); \
186 this.instanceOf##cname = true; \
190 /* Only statically initialize the current class, it contains everything it inherits */ \
191 if (cname##_vtbl.vtblname == this.classname) \
193 spawn##cname##_static(this); \
194 this.transmute = false; \
195 this.classname = #cname; \
196 this.vtblname = string_null; \
197 this.vtblbase = cname##_vtbl; \
199 spawn##base##_1(this); \
202 #define METHOD_REFERENCE(cname, name) \
205 #define STATIC_METHOD(cname, name, prototype) \
206 prototype METHOD_REFERENCE(cname, name)
208 #define METHOD(cname, name, prototype) \
209 STATIC_METHOD(cname, name, prototype); \
210 class(cname) .prototype name; \
211 _INIT_STATIC(cname) \
213 this.name = METHOD_REFERENCE(cname, name); \
215 STATIC_METHOD(cname, name, prototype)
217 #define DESTRUCTOR(cname) \
218 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
219 METHOD(cname, dtor, void(cname this)) \
221 METHOD_REFERENCE(cname, dtorimpl)(this); \
222 this.instanceOf##cname = false; \
223 entity super = SUPER(cname); \
224 if (super != cname##_vtbl) super.dtor(this); \
226 STATIC_METHOD(cname, dtorimpl, void(cname this))
228 #define ATTRIB(cname, name, type, val) \
229 class(cname).type name; \
232 noref bool strzone; /* Error on strzone() calls. */ \
236 #define STATIC_ATTRIB(cname, name, type, val) \
237 type cname##_##name; \
238 _INIT_STATIC(cname) \
240 noref bool strzone; /* Error on strzone() calls. */ \
241 cname##_##name = val; \
244 // cleanup potentially zoned strings from base classes
246 #define ATTRIB_STRZONE(cname, name, type, val) \
247 class(cname).type name; \
251 strunzone(this.name); \
252 this.name = strzone(val); \
255 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
256 type cname##_##name; \
257 _INIT_STATIC(cname) \
259 if (cname##_##name) \
260 strunzone(cname##_##name); \
261 cname##_##name = val; \
264 #define ATTRIBARRAY(cname, name, type, cnt) \
265 class(cname).type name[cnt];
267 #define ENDCLASS(cname) \
273 #define SUPER(cname) (cname##_vtbl.vtblbase)
275 #define spawn_static(this)
276 #define spawn_1(this)
279 DESTRUCTOR(Object) { remove(this); }
280 #define remove(this) delete(this)
281 METHOD(Object, describe, string(Object this))
284 string s = _("No description");
285 if (cvar("developer"))
287 for (int i = 0, n = numentityfields(); i < n; ++i)
289 string value = getentityfieldstring(i, this);
290 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
295 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
298 returns(sprintf("entity %i", this), "nopreview_map");