8 #define NULL (null_entity)
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 */
38 entity spawn_pure() = #600;
40 #define spawn_pure() _spawn()
43 entity __spawn(string _classname, string _sourceLoc, bool pure)
45 entity this = pure ? spawn_pure() : _spawn();
46 this.classname = _classname;
47 this.sourceLoc = _sourceLoc;
51 setorigin(this, '0 0 10000');
58 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
59 #define EVAL_entityclass(...) __VA_ARGS__
60 #define entityclass_1(name) entityclass_2(name, Object)
61 #ifndef QCC_SUPPORT_ENTITYCLASS
62 #define entityclass_2(name, base) typedef entity name
64 #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
66 #define entityclass_2(name, base) entityclass name : base {}
67 #define class(name) [[class(name)]]
68 #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
70 /** entities you care about seeing (.origin works) */
71 #define new(class) _new(class, false)
72 /** purely logical entities (.origin doesn't work) */
73 #define new_pure(class) _new(class, true)
74 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
76 entity _clearentity_ent;
77 STATIC_INIT(clearentity)
79 _clearentity_ent = new_pure(clearentity);
80 make_pure(_clearentity_ent);
82 void clearentity(entity e)
87 bool was_pure = is_pure(e);
88 copyentity(_clearentity_ent, e);
89 if (!was_pure) make_impure(e);
95 // Classes have a `spawn##cname(entity)` constructor
96 // The parameter is used across [[accumulate]] functions
98 // Macros to hide this implementation detail:
100 #define NEW(cname, ...) \
101 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
102 #define CONSTRUCT(cname, ...) \
103 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
105 #define NEW(cname, ...) \
106 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
108 #define CONSTRUCT(cname, ...) \
109 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
112 #define CONSTRUCTOR(cname, ...) \
113 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
117 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
122 void RegisterClasses() {}
123 STATIC_INIT(RegisterClasses)
128 #define VTBL(cname, base) \
129 INIT_STATIC(cname); \
130 entity cname##_vtbl; \
131 void cname##_vtbl_init() \
133 cname e = new_pure(vtbl); \
134 spawn##cname##_static(e); \
135 e.vtblname = #cname; \
136 /* Top level objects refer to themselves */ \
137 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
140 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
142 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
143 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
145 #define CLASS(cname, base) \
146 entityclass(cname, base); \
147 class(cname).bool instanceOf##cname; \
153 copyentity(cname##_vtbl, this); \
156 spawn##base##_static(this); \
157 this.instanceOf##cname = true; \
161 /* Only statically initialize the current class, it contains everything it inherits */ \
162 if (cname##_vtbl.vtblname == this.classname) \
164 spawn##cname##_static(this); \
165 this.classname = #cname; \
166 this.vtblname = string_null; \
167 this.vtblbase = cname##_vtbl; \
169 spawn##base##_1(this); \
172 #define METHOD_REFERENCE(cname, name) \
175 #define STATIC_METHOD(cname, name, prototype) \
176 prototype METHOD_REFERENCE(cname, name)
178 #define METHOD(cname, name, prototype) \
179 STATIC_METHOD(cname, name, prototype); \
180 class(cname) .prototype name; \
183 this.name = METHOD_REFERENCE(cname, name); \
185 STATIC_METHOD(cname, name, prototype)
187 #define ATTRIB(cname, name, type, val) \
188 class(cname).type name; \
191 noref bool strzone; /* Error on strzone() calls. */ \
195 #define ATTRIB_STRZONE(cname, name, type, val) \
196 class(cname).type name; \
200 strunzone(this.name); \
201 this.name = strzone(val); \
204 #define ATTRIBARRAY(cname, name, type, cnt) \
205 class(cname).type name[cnt];
207 #define ENDCLASS(cname) \
213 #define SUPER(cname) (cname##_vtbl.vtblbase)
215 #define spawn_static(this)
216 #define spawn_1(this)
219 METHOD(Object, describe, string(entity this))
221 string s = _("No description");
222 if (cvar("developer"))
224 for (int i = 0, n = numentityfields(); i < n; ++i)
226 string value = getentityfieldstring(i, this);
227 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
232 METHOD(Object, display, void(entity this, void(string name, string icon) returns))
234 returns(sprintf("entity %i", this), "nopreview_map");