9 #define NULL (null_entity)
16 #define make_pure(e) \
19 (e).pure_data = true; \
22 #define is_pure(e) ((e).pure_data)
25 /** Location entity was spawned from in source */
26 .string sourceLocFile;
29 entity __spawn(string _classname, string _sourceFile, int _sourceLine, bool pure)
31 entity this = _spawn();
32 this.classname = _classname;
33 this.sourceLocFile = _sourceFile;
34 this.sourceLocLine = _sourceLine;
35 if (pure) make_pure(this);
40 #define entityclass(...) EVAL(OVERLOAD(entityclass, __VA_ARGS__))
41 #define entityclass_1(name) entityclass_2(name, Object)
42 #ifndef QCC_SUPPORT_ENTITYCLASS
43 #define entityclass_2(name, base) typedef entity name
45 #define new(class) __spawn( #class, __FILE__, __LINE__, false)
47 #define entityclass_2(name, base) entityclass name : base {}
48 #define class(name) [[class(name)]]
49 #define new(class) ((class) __spawn( #class, __FILE__, __LINE__, false))
51 #define spawn() __spawn("entity", __FILE__, __LINE__, false)
53 entity _clearentity_ent;
54 STATIC_INIT(clearentity)
56 _clearentity_ent = new(clearentity);
58 void clearentity(entity e)
63 copyentity(_clearentity_ent, e);
69 // Classes have a `spawn##cname(entity)` constructor
70 // The parameter is used across [[accumulate]] functions
72 // Macros to hide this implementation detail:
74 #define NEW(cname, ...) \
75 OVERLOAD(spawn##cname, new(cname),##__VA_ARGS__)
77 #define CONSTRUCT(cname, ...) \
78 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
80 #define NEW_(cname, ...) \
81 OVERLOAD_(spawn##cname, __VA_ARGS__)
82 #define NEW(cname, ...) \
83 NEW_(cname, new(cname),##__VA_ARGS__)(new(cname),##__VA_ARGS__)
85 #define CONSTRUCT_(cname, ...) \
86 OVERLOAD_(spawn##cname, __VA_ARGS__)
87 #define CONSTRUCT(cname, ...) \
88 CONSTRUCT_(cname, this,##__VA_ARGS__)(this,##__VA_ARGS__)
91 #define CONSTRUCTOR(cname, ...) \
92 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
96 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
101 void RegisterClasses() {}
102 STATIC_INIT(RegisterClasses)
107 #define VTBL(cname, base) \
108 INIT_STATIC(cname); \
109 entity cname##_vtbl; \
110 void cname##_vtbl_init() \
112 cname e = new(vtbl); \
114 spawn##cname##_static(e); \
115 e.vtblname = #cname; \
116 /* Top level objects refer to themselves */ \
117 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
120 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
122 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
123 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
125 #define CLASS(cname, base) \
126 entityclass(cname, base); \
127 class(cname).bool instanceOf##cname; \
133 copyentity(cname##_vtbl, this); \
136 spawn##base##_static(this); \
137 this.instanceOf##cname = true; \
141 /* Only statically initialize the current class, it contains everything it inherits */ \
142 if (cname##_vtbl.vtblname == this.classname) \
144 spawn##cname##_static(this); \
145 this.classname = #cname; \
146 this.vtblname = string_null; \
147 this.vtblbase = cname##_vtbl; \
149 spawn##base##_1(this); \
152 #define METHOD(cname, name, prototype) \
153 class(cname).prototype name; \
154 prototype cname##_##name; \
157 this.name = cname##_##name; \
159 prototype cname##_##name
161 #define ATTRIB(cname, name, type, val) \
162 class(cname).type name; \
165 noref bool strzone; /* Error on strzone() calls. */ \
169 #define ATTRIB_STRZONE(cname, name, type, val) \
170 class(cname).type name; \
174 strunzone(this.name); \
175 this.name = strzone(val); \
178 #define ATTRIBARRAY(cname, name, type, cnt) \
179 class(cname).type name[cnt];
181 #define ENDCLASS(cname) \
187 #define SUPER(cname) (cname##_vtbl.vtblbase)
189 #define spawn_static(this)
190 #define spawn_1(this)
193 METHOD(Object, describe, string(entity this))
195 string s = _("No description");
196 if (cvar("developer"))
198 for (int i = 0, n = numentityfields(); i < n; ++i)
200 string value = getentityfieldstring(i, this);
201 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
206 METHOD(Object, display, void(entity this, void(string name, string icon) returns))
208 returns(sprintf("entity %i", this), "nopreview_map");