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 */
30 .string sourceLocFile;
39 entity spawn_pure() = #600;
41 #define spawn_pure() _spawn()
44 entity __spawn(string _classname, string _sourceFile, int _sourceLine, bool pure)
46 entity this = pure ? spawn_pure() : _spawn();
47 this.classname = _classname;
48 this.sourceLocFile = _sourceFile;
49 this.sourceLocLine = _sourceLine;
53 setorigin(this, '0 0 10000');
60 #define entityclass(...) EVAL_entityclass(OVERLOAD(entityclass, __VA_ARGS__))
61 #define EVAL_entityclass(...) __VA_ARGS__
62 #define entityclass_1(name) entityclass_2(name, Object)
63 #ifndef QCC_SUPPORT_ENTITYCLASS
64 #define entityclass_2(name, base) typedef entity name
66 #define _new(class, pure) __spawn( #class, __FILE__, __LINE__, pure)
68 #define entityclass_2(name, base) entityclass name : base {}
69 #define class(name) [[class(name)]]
70 #define _new(class, pure) ((class) __spawn( #class, __FILE__, __LINE__, pure))
72 /** entities you care about seeing (.origin works) */
73 #define new(class) _new(class, false)
74 /** purely logical entities (.origin doesn't work) */
75 #define new_pure(class) _new(class, true)
76 #define spawn() __spawn("entity", __FILE__, __LINE__, false)
78 entity _clearentity_ent;
79 STATIC_INIT(clearentity)
81 _clearentity_ent = new_pure(clearentity);
82 make_pure(_clearentity_ent);
84 void clearentity(entity e)
89 bool was_pure = is_pure(e);
90 copyentity(_clearentity_ent, e);
91 if (!was_pure) make_impure(e);
97 // Classes have a `spawn##cname(entity)` constructor
98 // The parameter is used across [[accumulate]] functions
100 // Macros to hide this implementation detail:
102 #define NEW_(cname, ...) \
103 OVERLOAD_(spawn##cname, __VA_ARGS__)
104 #define NEW(cname, ...) \
105 NEW_(cname, new_pure(cname),##__VA_ARGS__)(new_pure(cname),##__VA_ARGS__)
107 #define CONSTRUCT_(cname, ...) \
108 OVERLOAD_(spawn##cname, __VA_ARGS__)
109 #define CONSTRUCT(cname, ...) \
110 CONSTRUCT_(cname, this,##__VA_ARGS__)(this,##__VA_ARGS__)
112 #define NEW(cname, ...) \
113 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
115 #define CONSTRUCT(cname, ...) \
116 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
119 #define CONSTRUCTOR(cname, ...) \
120 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
124 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
129 void RegisterClasses() {}
130 STATIC_INIT(RegisterClasses)
135 #define VTBL(cname, base) \
136 INIT_STATIC(cname); \
137 entity cname##_vtbl; \
138 void cname##_vtbl_init() \
140 cname e = new_pure(vtbl); \
141 spawn##cname##_static(e); \
142 e.vtblname = #cname; \
143 /* Top level objects refer to themselves */ \
144 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
147 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
149 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
150 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
152 #define CLASS(cname, base) \
153 entityclass(cname, base); \
154 class(cname).bool instanceOf##cname; \
160 copyentity(cname##_vtbl, this); \
163 spawn##base##_static(this); \
164 this.instanceOf##cname = true; \
168 /* Only statically initialize the current class, it contains everything it inherits */ \
169 if (cname##_vtbl.vtblname == this.classname) \
171 spawn##cname##_static(this); \
172 this.classname = #cname; \
173 this.vtblname = string_null; \
174 this.vtblbase = cname##_vtbl; \
176 spawn##base##_1(this); \
179 #define METHOD_REFERENCE(cname, name) \
182 #define STATIC_METHOD(cname, name, prototype) \
183 prototype METHOD_REFERENCE(cname, name)
185 #define METHOD(cname, name, prototype) \
186 STATIC_METHOD(cname, name, prototype); \
187 class(cname) .prototype name; \
190 this.name = METHOD_REFERENCE(cname, name); \
192 STATIC_METHOD(cname, name, prototype)
194 #define ATTRIB(cname, name, type, val) \
195 class(cname).type name; \
198 noref bool strzone; /* Error on strzone() calls. */ \
202 #define ATTRIB_STRZONE(cname, name, type, val) \
203 class(cname).type name; \
207 strunzone(this.name); \
208 this.name = strzone(val); \
211 #define ATTRIBARRAY(cname, name, type, cnt) \
212 class(cname).type name[cnt];
214 #define ENDCLASS(cname) \
220 #define SUPER(cname) (cname##_vtbl.vtblbase)
222 #define spawn_static(this)
223 #define spawn_1(this)
226 METHOD(Object, describe, string(entity this))
228 string s = _("No description");
229 if (cvar("developer"))
231 for (int i = 0, n = numentityfields(); i < n; ++i)
233 string value = getentityfieldstring(i, this);
234 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
239 METHOD(Object, display, void(entity this, void(string name, string icon) returns))
241 returns(sprintf("entity %i", this), "nopreview_map");