]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
s/make_pure/new_pure/
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / oo.qh
1 #pragma once
2
3 #include "misc.qh"
4 #include "nil.qh"
5 #include "static.qh"
6
7 #ifdef MENUQC
8         #define NULL (null_entity)
9 #else
10         #define NULL (world)
11 #endif
12
13 .vector origin;
14 .bool pure_data;
15 /** @deprecated, use new_pure or NEW(class) */
16 #define make_pure(e) \
17         MACRO_BEGIN \
18         { \
19                 (e).pure_data = true; \
20         } MACRO_END
21 #define make_impure(e) \
22         MACRO_BEGIN \
23         { \
24                 (e).pure_data = false; \
25         } MACRO_END
26 #define is_pure(e) ((e).pure_data)
27
28 .string classname;
29 /** Location entity was spawned from in source */
30 .string sourceLoc;
31 entity _spawn();
32
33 #ifndef SPAWN_PURE
34 #define SPAWN_PURE 0
35 #endif
36
37 // pure entities: need no .origin
38 #if SPAWN_PURE
39 entity spawn_pure() = #600;
40 #else
41 #define spawn_pure() _spawn()
42 #endif
43
44 entity __spawn(string _classname, string _sourceLoc, bool pure)
45 {
46         entity this = pure ? spawn_pure() : _spawn();
47         this.classname = _classname;
48         this.sourceLoc = _sourceLoc;
49         if (pure) {
50                 make_pure(this);
51                 #ifdef CSQC
52                 setorigin(this, '0 0 10000');
53                 #endif
54                 #ifdef SVQC
55         setorigin(this, '0 0 -10000');
56         #endif
57         }
58         return this;
59 }
60
61
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
67         #define class(name)
68         #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
69 #else
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))
73 #endif
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)
79
80 entity _clearentity_ent;
81 STATIC_INIT(clearentity)
82 {
83         _clearentity_ent = new_pure(clearentity);
84 }
85 void clearentity(entity e)
86 {
87 #ifdef CSQC
88                 int n = e.entnum;
89 #endif
90         bool was_pure = is_pure(e);
91         copyentity(_clearentity_ent, e);
92         if (!was_pure) make_impure(e);
93 #ifdef CSQC
94                 e.entnum = n;
95 #endif
96 }
97
98 // Classes have a `spawn##cname(entity)` constructor
99 // The parameter is used across [[accumulate]] functions
100
101 // Macros to hide this implementation detail:
102 #ifdef __STDC__
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__))
107 #else
108         #define NEW(cname, ...) \
109                 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
110
111         #define CONSTRUCT(cname, ...) \
112                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
113 #endif
114
115 #define CONSTRUCTOR(cname, ...) \
116         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
117         { \
118                 return = this; \
119         } \
120         [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
121
122 .string vtblname;
123 .entity vtblbase;
124
125 void RegisterClasses() {}
126 STATIC_INIT(RegisterClasses)
127 {
128         RegisterClasses();
129 }
130
131 #define VTBL(cname, base) \
132         INIT_STATIC(cname); \
133         entity cname##_vtbl; \
134         void cname##_vtbl_init() \
135         { \
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; \
141                 cname##_vtbl = e; \
142         } \
143         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
144
145 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
146 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
147
148 #define CLASS(cname, base)                  \
149         entityclass(cname, base);               \
150         class(cname).bool instanceOf##cname;   \
151         VTBL(cname, base)                       \
152         INIT_STATIC(cname) \
153         {                    \
154                 if (cname##_vtbl) \
155                 {                 \
156                         copyentity(cname##_vtbl, this); \
157                         return;                         \
158                 }                                   \
159                 spawn##base##_static(this);         \
160                 this.instanceOf##cname = true;      \
161         }                                       \
162         INIT(cname) \
163         {                           \
164                 /* Only statically initialize the current class, it contains everything it inherits */ \
165                 if (cname##_vtbl.vtblname == this.classname) \
166                 { \
167                         spawn##cname##_static(this);    \
168                         this.classname = #cname;        \
169                         this.vtblname = string_null;    \
170                         this.vtblbase = cname##_vtbl;   \
171                 }                                   \
172                 spawn##base##_1(this);              \
173         }
174
175 #define METHOD_REFERENCE(cname, name) \
176         cname##_##name
177
178 #define STATIC_METHOD(cname, name, prototype) \
179         prototype METHOD_REFERENCE(cname, name)
180
181 #define METHOD(cname, name, prototype) \
182         STATIC_METHOD(cname, name, prototype); \
183         class(cname) .prototype name; \
184         INIT_STATIC(cname) \
185         { \
186                 this.name = METHOD_REFERENCE(cname, name); \
187         } \
188         STATIC_METHOD(cname, name, prototype)
189
190 #define ATTRIB(cname, name, type, val)      \
191         class(cname).type name;                \
192         INIT(cname) \
193         { \
194                 noref bool strzone; /* Error on strzone() calls. */ \
195                 this.name = val; \
196         }
197
198 #define ATTRIB_STRZONE(cname, name, type, val)      \
199         class(cname).type name;                \
200         INIT(cname) \
201         { \
202                 if (this.name) \
203                         strunzone(this.name); \
204                 this.name = strzone(val); \
205         }
206
207 #define ATTRIBARRAY(cname, name, type, cnt) \
208         class(cname).type name[cnt];
209
210 #define ENDCLASS(cname) \
211         INIT(cname) \
212         { \
213                 return this; \
214         }
215
216 #define SUPER(cname) (cname##_vtbl.vtblbase)
217
218 #define spawn_static(this)
219 #define spawn_1(this)
220 #define _vtbl NULL
221 CLASS(Object, );
222         METHOD(Object, describe, string(entity this))
223         {
224                 string s = _("No description");
225                 if (cvar("developer"))
226                 {
227                         for (int i = 0, n = numentityfields(); i < n; ++i)
228                         {
229                                 string value = getentityfieldstring(i, this);
230                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
231                         }
232                 }
233                 return s;
234         }
235         METHOD(Object, display, void(entity this, void(string name, string icon) returns))
236         {
237                 returns(sprintf("entity %i", this), "nopreview_map");
238         }
239 ENDCLASS(Object)
240 #undef spawn_static
241 #undef spawn_1
242 #undef _vtbl