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;
40 entity spawn_pure() = #600;
42 #define spawn_pure() _spawn()
45 entity __spawn(string _classname, string _sourceFile, int _sourceLine, bool pure)
47 entity this = pure ? spawn_pure() : _spawn();
48 this.classname = _classname;
49 this.sourceLocFile = _sourceFile;
50 this.sourceLocLine = _sourceLine;
54 setorigin(this, '0 0 10000');
61 #define entityclass(...) EVAL_entityclass(OVERLOAD(entityclass, __VA_ARGS__))
62 #define EVAL_entityclass(...) __VA_ARGS__
63 #define entityclass_1(name) entityclass_2(name, Object)
64 #ifndef QCC_SUPPORT_ENTITYCLASS
65 #define entityclass_2(name, base) typedef entity name
67 #define _new(class, pure) __spawn( #class, __FILE__, __LINE__, pure)
69 #define entityclass_2(name, base) entityclass name : base {}
70 #define class(name) [[class(name)]]
71 #define _new(class, pure) ((class) __spawn( #class, __FILE__, __LINE__, pure))
73 /** entities you care about seeing (.origin works) */
74 #define new(class) _new(class, false)
75 /** purely logical entities (.origin doesn't work) */
76 #define new_pure(class) _new(class, true)
77 #define spawn() __spawn("entity", __FILE__, __LINE__, false)
79 entity _clearentity_ent;
80 STATIC_INIT(clearentity)
82 _clearentity_ent = new_pure(clearentity);
83 make_pure(_clearentity_ent);
85 void clearentity(entity e)
90 bool was_pure = is_pure(e);
91 copyentity(_clearentity_ent, e);
92 if (!was_pure) make_impure(e);
98 // Classes have a `spawn##cname(entity)` constructor
99 // The parameter is used across [[accumulate]] functions
101 // Macros to hide this implementation detail:
103 #define NEW(cname, ...) \
104 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
106 #define CONSTRUCT(cname, ...) \
107 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
109 #define NEW_(cname, ...) \
110 OVERLOAD_(spawn##cname, __VA_ARGS__)
111 #define NEW(cname, ...) \
112 NEW_(cname, new_pure(cname),##__VA_ARGS__)(new_pure(cname),##__VA_ARGS__)
114 #define CONSTRUCT_(cname, ...) \
115 OVERLOAD_(spawn##cname, __VA_ARGS__)
116 #define CONSTRUCT(cname, ...) \
117 CONSTRUCT_(cname, this,##__VA_ARGS__)(this,##__VA_ARGS__)
120 #define CONSTRUCTOR(cname, ...) \
121 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
125 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
130 void RegisterClasses() {}
131 STATIC_INIT(RegisterClasses)
136 #define VTBL(cname, base) \
137 INIT_STATIC(cname); \
138 entity cname##_vtbl; \
139 void cname##_vtbl_init() \
141 cname e = new_pure(vtbl); \
142 spawn##cname##_static(e); \
143 e.vtblname = #cname; \
144 /* Top level objects refer to themselves */ \
145 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
148 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
150 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
151 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
153 #define CLASS(cname, base) \
154 entityclass(cname, base); \
155 class(cname).bool instanceOf##cname; \
161 copyentity(cname##_vtbl, this); \
164 spawn##base##_static(this); \
165 this.instanceOf##cname = true; \
169 /* Only statically initialize the current class, it contains everything it inherits */ \
170 if (cname##_vtbl.vtblname == this.classname) \
172 spawn##cname##_static(this); \
173 this.classname = #cname; \
174 this.vtblname = string_null; \
175 this.vtblbase = cname##_vtbl; \
177 spawn##base##_1(this); \
180 #define METHOD_REFERENCE(cname, name) \
183 #define STATIC_METHOD(cname, name, prototype) \
184 prototype METHOD_REFERENCE(cname, name)
186 #define METHOD(cname, name, prototype) \
187 STATIC_METHOD(cname, name, prototype); \
188 class(cname) .prototype name; \
191 this.name = METHOD_REFERENCE(cname, name); \
193 STATIC_METHOD(cname, name, prototype)
195 #define ATTRIB(cname, name, type, val) \
196 class(cname).type name; \
199 noref bool strzone; /* Error on strzone() calls. */ \
203 #define ATTRIB_STRZONE(cname, name, type, val) \
204 class(cname).type name; \
208 strunzone(this.name); \
209 this.name = strzone(val); \
212 #define ATTRIBARRAY(cname, name, type, cnt) \
213 class(cname).type name[cnt];
215 #define ENDCLASS(cname) \
221 #define SUPER(cname) (cname##_vtbl.vtblbase)
223 #define spawn_static(this)
224 #define spawn_1(this)
227 METHOD(Object, describe, string(entity this))
229 string s = _("No description");
230 if (cvar("developer"))
232 for (int i = 0, n = numentityfields(); i < n; ++i)
234 string value = getentityfieldstring(i, this);
235 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
240 METHOD(Object, display, void(entity this, void(string name, string icon) returns))
242 returns(sprintf("entity %i", this), "nopreview_map");