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 */
37 // pure entities: need no .origin
39 entity spawn_pure() = #600;
41 #define spawn_pure() _spawn()
44 entity __spawn(string _classname, string _sourceLoc, bool pure)
46 entity this = pure ? spawn_pure() : _spawn();
47 this.classname = _classname;
48 this.sourceLoc = _sourceLoc;
52 setorigin(this, '0 0 10000');
55 setorigin(this, '0 0 -10000');
62 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
63 #define EVAL_entityclass(...) __VA_ARGS__
64 #define entityclass_1(name) entityclass_2(name, Object)
65 #ifndef QCC_SUPPORT_ENTITYCLASS
66 #define entityclass_2(name, base) typedef entity name
68 #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
70 #define entityclass_2(name, base) entityclass name : base {}
71 #define class(name) [[class(name)]]
72 #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
74 /** entities you care about seeing (.origin works) */
75 #define new(class) _new(class, false)
76 /** purely logical entities (.origin doesn't work) */
77 #define new_pure(class) _new(class, true)
78 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
80 entity _clearentity_ent;
81 STATIC_INIT(clearentity)
83 _clearentity_ent = new_pure(clearentity);
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) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
105 #define CONSTRUCT(cname, ...) \
106 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
108 #define NEW(cname, ...) \
109 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
111 #define CONSTRUCT(cname, ...) \
112 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
115 #define CONSTRUCTOR(cname, ...) \
116 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
120 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
125 void RegisterClasses() {}
126 STATIC_INIT(RegisterClasses)
131 #define VTBL(cname, base) \
132 INIT_STATIC(cname); \
133 entity cname##_vtbl; \
134 void cname##_vtbl_init() \
136 cname e = new_pure(vtbl); \
137 spawn##cname##_static(e); \
138 e.vtblname = #cname; \
139 /* Top level objects refer to themselves */ \
140 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
143 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
145 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
146 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
148 #define CLASS(cname, base) \
149 entityclass(cname, base); \
150 class(cname).bool instanceOf##cname; \
156 copyentity(cname##_vtbl, this); \
159 spawn##base##_static(this); \
160 this.instanceOf##cname = true; \
164 /* Only statically initialize the current class, it contains everything it inherits */ \
165 if (cname##_vtbl.vtblname == this.classname) \
167 spawn##cname##_static(this); \
168 this.classname = #cname; \
169 this.vtblname = string_null; \
170 this.vtblbase = cname##_vtbl; \
172 spawn##base##_1(this); \
175 #define METHOD_REFERENCE(cname, name) \
178 #define STATIC_METHOD(cname, name, prototype) \
179 prototype METHOD_REFERENCE(cname, name)
181 #define METHOD(cname, name, prototype) \
182 STATIC_METHOD(cname, name, prototype); \
183 class(cname) .prototype name; \
186 this.name = METHOD_REFERENCE(cname, name); \
188 STATIC_METHOD(cname, name, prototype)
190 #define ATTRIB(cname, name, type, val) \
191 class(cname).type name; \
194 noref bool strzone; /* Error on strzone() calls. */ \
198 #define ATTRIB_STRZONE(cname, name, type, val) \
199 class(cname).type name; \
203 strunzone(this.name); \
204 this.name = strzone(val); \
207 #define ATTRIBARRAY(cname, name, type, cnt) \
208 class(cname).type name[cnt];
210 #define ENDCLASS(cname) \
216 #define SUPER(cname) (cname##_vtbl.vtblbase)
218 #define spawn_static(this)
219 #define spawn_1(this)
222 METHOD(Object, describe, string(entity this))
224 string s = _("No description");
225 if (cvar("developer"))
227 for (int i = 0, n = numentityfields(); i < n; ++i)
229 string value = getentityfieldstring(i, this);
230 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
235 METHOD(Object, display, void(entity this, void(string name, string icon) returns))
237 returns(sprintf("entity %i", this), "nopreview_map");