9 #define NULL (null_entity)
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 */
31 .string sourceLocFile;
34 entity __spawn(string _classname, string _sourceFile, int _sourceLine, bool pure)
36 entity this = _spawn();
37 this.classname = _classname;
38 this.sourceLocFile = _sourceFile;
39 this.sourceLocLine = _sourceLine;
43 setorigin(this, '0 0 10000');
50 #define entityclass(...) EVAL(OVERLOAD(entityclass, __VA_ARGS__))
51 #define entityclass_1(name) entityclass_2(name, Object)
52 #ifndef QCC_SUPPORT_ENTITYCLASS
53 #define entityclass_2(name, base) typedef entity name
55 #define _new(class, pure) __spawn( #class, __FILE__, __LINE__, pure)
57 #define entityclass_2(name, base) entityclass name : base {}
58 #define class(name) [[class(name)]]
59 #define _new(class, pure) ((class) __spawn( #class, __FILE__, __LINE__, pure))
61 /** entities you care about seeing (.origin works) */
62 #define new(class) _new(class, false)
63 /** purely logical entities (.origin doesn't work) */
64 #define new_pure(class) _new(class, true)
65 #define spawn() __spawn("entity", __FILE__, __LINE__, false)
67 entity _clearentity_ent;
68 STATIC_INIT(clearentity)
70 _clearentity_ent = new_pure(clearentity);
71 make_pure(_clearentity_ent);
73 void clearentity(entity e)
78 bool was_pure = is_pure(e);
79 copyentity(_clearentity_ent, e);
80 if (!was_pure) make_impure(e);
86 // Classes have a `spawn##cname(entity)` constructor
87 // The parameter is used across [[accumulate]] functions
89 // Macros to hide this implementation detail:
91 #define NEW(cname, ...) \
92 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
94 #define CONSTRUCT(cname, ...) \
95 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
97 #define NEW_(cname, ...) \
98 OVERLOAD_(spawn##cname, __VA_ARGS__)
99 #define NEW(cname, ...) \
100 NEW_(cname, new_pure(cname),##__VA_ARGS__)(new_pure(cname),##__VA_ARGS__)
102 #define CONSTRUCT_(cname, ...) \
103 OVERLOAD_(spawn##cname, __VA_ARGS__)
104 #define CONSTRUCT(cname, ...) \
105 CONSTRUCT_(cname, this,##__VA_ARGS__)(this,##__VA_ARGS__)
108 #define CONSTRUCTOR(cname, ...) \
109 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
113 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
118 void RegisterClasses() {}
119 STATIC_INIT(RegisterClasses)
124 #define VTBL(cname, base) \
125 INIT_STATIC(cname); \
126 entity cname##_vtbl; \
127 void cname##_vtbl_init() \
129 cname e = new_pure(vtbl); \
130 spawn##cname##_static(e); \
131 e.vtblname = #cname; \
132 /* Top level objects refer to themselves */ \
133 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
136 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
138 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
139 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
141 #define CLASS(cname, base) \
142 entityclass(cname, base); \
143 class(cname).bool instanceOf##cname; \
149 copyentity(cname##_vtbl, this); \
152 spawn##base##_static(this); \
153 this.instanceOf##cname = true; \
157 /* Only statically initialize the current class, it contains everything it inherits */ \
158 if (cname##_vtbl.vtblname == this.classname) \
160 spawn##cname##_static(this); \
161 this.classname = #cname; \
162 this.vtblname = string_null; \
163 this.vtblbase = cname##_vtbl; \
165 spawn##base##_1(this); \
168 #define METHOD_REFERENCE(cname, name) \
171 #define STATIC_METHOD(cname, name, prototype) \
172 prototype METHOD_REFERENCE(cname, name)
174 #define METHOD(cname, name, prototype) \
175 STATIC_METHOD(cname, name, prototype); \
176 class(cname) .prototype name; \
179 this.name = METHOD_REFERENCE(cname, name); \
181 STATIC_METHOD(cname, name, prototype)
183 #define ATTRIB(cname, name, type, val) \
184 class(cname).type name; \
187 noref bool strzone; /* Error on strzone() calls. */ \
191 #define ATTRIB_STRZONE(cname, name, type, val) \
192 class(cname).type name; \
196 strunzone(this.name); \
197 this.name = strzone(val); \
200 #define ATTRIBARRAY(cname, name, type, cnt) \
201 class(cname).type name[cnt];
203 #define ENDCLASS(cname) \
209 #define SUPER(cname) (cname##_vtbl.vtblbase)
211 #define spawn_static(this)
212 #define spawn_1(this)
215 METHOD(Object, describe, string(entity this))
217 string s = _("No description");
218 if (cvar("developer"))
220 for (int i = 0, n = numentityfields(); i < n; ++i)
222 string value = getentityfieldstring(i, this);
223 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
228 METHOD(Object, display, void(entity this, void(string name, string icon) returns))
230 returns(sprintf("entity %i", this), "nopreview_map");