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 #define delete(this) MACRO_BEGIN { \
75 entity _this = (this); \
76 void(entity) _dtor = _this.dtor; \
77 if (_dtor) _dtor(_this); else remove(_this); \
81 entity _clearentity_ent;
82 STATIC_INIT(clearentity)
84 _clearentity_ent = new_pure(clearentity);
86 void clearentity(entity e)
91 bool was_pure = is_pure(e);
92 copyentity(_clearentity_ent, e);
93 if (!was_pure) make_impure(e);
99 // Classes have a `spawn##cname(entity)` constructor
100 // The parameter is used across [[accumulate]] functions
104 // Macros to hide this implementation detail:
106 #define NEW(cname, ...) \
107 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
109 #define _TRANSMUTE(cname, this, ...) \
110 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
112 #define CONSTRUCT(cname, ...) \
113 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
115 #define NEW(cname, ...) \
116 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
118 #define _TRANSMUTE(cname, this, ...) \
119 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
121 #define CONSTRUCT(cname, ...) \
122 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
125 #define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
126 entity _e = (this); \
127 if (_e.vtblbase != cname##_vtbl) { \
128 _e.transmute = true; \
129 _e.classname = #cname; \
130 _TRANSMUTE(cname, _e, __VA_ARGS__); \
134 #define CONSTRUCTOR(cname, ...) \
135 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
139 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
144 void RegisterClasses() {}
145 STATIC_INIT(RegisterClasses)
150 #define VTBL(cname, base) \
151 _INIT_STATIC(cname); \
152 entity cname##_vtbl; \
153 void cname##_vtbl_init() \
155 cname e = new_pure(vtbl); \
156 spawn##cname##_static(e); \
157 e.vtblname = #cname; \
158 /* Top level objects refer to themselves */ \
159 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
162 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
164 #define _INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
165 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
167 #define CLASS(cname, base) \
168 entityclass(cname, base); \
169 class(cname).bool instanceOf##cname; \
170 bool is_##cname(entity e) { return e.instanceOf##cname; } \
171 void isnt_##cname(entity e) { eprint(e); } \
173 _INIT_STATIC(cname) \
175 if (cname##_vtbl && !this.transmute)\
177 copyentity(cname##_vtbl, this); \
180 spawn##base##_static(this); \
181 this.instanceOf##cname = true; \
185 /* Only statically initialize the current class, it contains everything it inherits */ \
186 if (cname##_vtbl.vtblname == this.classname) \
188 spawn##cname##_static(this); \
189 this.transmute = false; \
190 this.classname = #cname; \
191 this.vtblname = string_null; \
192 this.vtblbase = cname##_vtbl; \
194 spawn##base##_1(this); \
197 #define METHOD_REFERENCE(cname, name) \
200 #define STATIC_METHOD(cname, name, prototype) \
201 prototype METHOD_REFERENCE(cname, name)
203 #define METHOD(cname, name, prototype) \
204 STATIC_METHOD(cname, name, prototype); \
205 class(cname) .prototype name; \
206 _INIT_STATIC(cname) \
208 this.name = METHOD_REFERENCE(cname, name); \
210 STATIC_METHOD(cname, name, prototype)
212 #define DESTRUCTOR(cname) \
213 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
214 METHOD(cname, dtor, void(cname this)) \
216 METHOD_REFERENCE(cname, dtorimpl)(this); \
217 this.instanceOf##cname = false; \
218 entity super = SUPER(cname); \
219 if (super != cname##_vtbl) super.dtor(this); \
221 STATIC_METHOD(cname, dtorimpl, void(cname this))
223 #define ATTRIB(cname, name, type, val) \
224 class(cname).type name; \
227 noref bool strzone; /* Error on strzone() calls. */ \
231 #define STATIC_ATTRIB(cname, name, type, val) \
232 type cname##_##name; \
233 _INIT_STATIC(cname) \
235 noref bool strzone; /* Error on strzone() calls. */ \
236 cname##_##name = val; \
239 // cleanup potentially zoned strings from base classes
241 #define ATTRIB_STRZONE(cname, name, type, val) \
242 class(cname).type name; \
246 strunzone(this.name); \
247 this.name = strzone(val); \
250 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
251 type cname##_##name; \
252 _INIT_STATIC(cname) \
254 if (cname##_##name) \
255 strunzone(cname##_##name); \
256 cname##_##name = val; \
259 #define ATTRIBARRAY(cname, name, type, cnt) \
260 class(cname).type name[cnt];
262 #define ENDCLASS(cname) \
268 #define SUPER(cname) (cname##_vtbl.vtblbase)
270 #define spawn_static(this)
271 #define spawn_1(this)
274 DESTRUCTOR(Object) { remove(this); }
275 #define remove(this) delete(this)
276 METHOD(Object, describe, string(Object this))
279 string s = _("No description");
280 if (cvar("developer"))
282 for (int i = 0, n = numentityfields(); i < n; ++i)
284 string value = getentityfieldstring(i, this);
285 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
290 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
293 returns(sprintf("entity %i", this), "nopreview_map");