7 #define NULL (null_entity)
13 /** Location entity was spawned from in source */
14 .string sourceLocFile;
17 entity __spawn(string _classname, string _sourceFile, int _sourceLine) {
18 entity this = _spawn();
19 this.classname = _classname;
20 this.sourceLocFile = _sourceFile;
21 this.sourceLocLine = _sourceLine;
27 #define entityclass(...) EVAL(OVERLOAD(entityclass, __VA_ARGS__))
28 #define entityclass_1(name) entityclass_2(name, Object)
29 #ifndef QCC_SUPPORT_ENTITYCLASS
30 #define entityclass_2(name, base) typedef entity name
32 #define new(class) __spawn(#class, __FILE__, __LINE__)
34 #define entityclass_2(name, base) entityclass name : base {}
35 #define class(name) [[class(name)]]
36 #define new(class) ((class) __spawn(#class, __FILE__, __LINE__))
39 // Classes have a `spawn##cname(entity)` constructor
40 // The parameter is used across [[accumulate]] functions
42 // Macros to hide this implementation detail:
44 #define NEW(cname, ...) \
45 OVERLOAD(spawn##cname, new(cname), ##__VA_ARGS__)
47 #define CONSTRUCT(cname, ...) \
48 OVERLOAD(spawn##cname, this, ##__VA_ARGS__)
50 #define NEW_(cname, ...) \
51 OVERLOAD_(spawn##cname, __VA_ARGS__)
52 #define NEW(cname, ...) \
53 NEW_(cname, new(cname), ##__VA_ARGS__)(new(cname), ##__VA_ARGS__)
55 #define CONSTRUCT_(cname, ...) \
56 OVERLOAD_(spawn##cname, __VA_ARGS__)
57 #define CONSTRUCT(cname, ...) \
58 CONSTRUCT_(cname, this, ##__VA_ARGS__)(this, ##__VA_ARGS__)
61 #define CONSTRUCTOR(cname, ...) \
62 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) { return = this; } \
63 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
68 void RegisterClasses() { }
69 STATIC_INIT(RegisterClasses) { RegisterClasses(); }
71 #define VTBL(cname, base) \
73 entity cname##_vtbl; \
74 void cname##_vtbl_init() { \
75 cname e = new(vtbl); \
76 spawn##cname##_static(e); \
77 e.vtblname = #cname; \
78 /* Top level objects refer to themselves */ \
79 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
82 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
84 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
85 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
87 #define CLASS(cname, base) \
88 entityclass(cname, base); \
89 class(cname) .bool instanceOf##cname; \
91 INIT_STATIC(cname) { \
93 copyentity(cname##_vtbl, this); \
96 spawn##base##_static(this); \
97 this.instanceOf##cname = true; \
100 /* Only statically initialize the current class, it contains everything it inherits */ \
101 if (cname##_vtbl.vtblname == this.classname) { \
102 spawn##cname##_static(this); \
103 this.classname = #cname; \
104 this.vtblname = string_null; \
105 this.vtblbase = cname##_vtbl; \
107 spawn##base##_1(this); \
110 #define METHOD(cname, name, prototype) \
111 class(cname) .prototype name; \
112 prototype cname##_##name; \
113 INIT_STATIC(cname) { this.name = cname##_##name; } \
114 prototype cname##_##name
116 #define ATTRIB(cname, name, type, val) \
117 class(cname) .type name; \
118 INIT(cname) { this.name = val; }
120 #define ATTRIBARRAY(cname, name, type, cnt) \
121 class(cname) .type name[cnt];
123 #define ENDCLASS(cname) \
124 [[last]] INIT(cname) { return this; }
126 #define SUPER(cname) (cname##_vtbl.vtblbase)
127 #define super (this.vtblbase.vtblbase)
129 #define spawn_static(this)
130 #define spawn_1(this)
133 METHOD(Object, describe, string(entity this)) {
134 string s = _("No description");
135 if (cvar("developer")) {
136 for (int i = 0, n = numentityfields(); i < n; ++i) {
137 string value = getentityfieldstring(i, this);
138 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
143 METHOD(Object, display, void(entity this, void(string name, string icon) returns)) {
144 returns(sprintf("entity %i", this), "nopreview_map");