]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
Include a couple more used fields in the spawnfunc field list
[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 .vector origin;
8
9 .bool pure_data;
10 #define is_pure(e) ((e).pure_data)
11 /** @deprecated use new_pure or NEW(class) */
12 #define make_pure(e) MACRO_BEGIN \
13         (e).pure_data = true; \
14 MACRO_END
15 #define make_impure(e) MACRO_BEGIN \
16         (e).pure_data = false; \
17 MACRO_END
18
19 .string classname;
20 /** Location entity was spawned from in source */
21 .string sourceLoc;
22 entity _spawn();
23
24 #ifndef SPAWN_PURE
25 #define SPAWN_PURE 0
26 #endif
27
28 // pure entities: need no .origin
29 #if SPAWN_PURE
30 entity spawn_pure() = #600;
31 #else
32 #define spawn_pure() _spawn()
33 #endif
34
35 entity __spawn(string _classname, string _sourceLoc, bool pure)
36 {
37         entity this = pure ? spawn_pure() : _spawn();
38         this.classname = _classname;
39         this.sourceLoc = _sourceLoc;
40         if (pure) {
41                 make_pure(this);
42                 #ifdef CSQC
43                 setorigin(this, (world.mins + world.maxs) * 0.5);
44                 #endif
45                 #ifdef SVQC
46         setorigin(this, (world.mins + world.maxs) * 0.5);
47         #endif
48         }
49         return this;
50 }
51
52
53 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
54 #define EVAL_entityclass(...) __VA_ARGS__
55 #define entityclass_1(name) entityclass_2(name, Object)
56 #ifndef QCC_SUPPORT_ENTITYCLASS
57         #define entityclass_2(name, base) USING(name, entity)
58         #define classfield(name)
59         #define _new(class, pure) __spawn(#class, __FILE__ ":" STR(__LINE__), pure)
60 #else
61         #define entityclass_2(name, base) entityclass name : base {}
62         #define classfield(name) [[class(name)]]
63         #define _new(class, pure) ((class) __spawn(#class, __FILE__ ":" STR(__LINE__), pure))
64 #endif
65 /** entities you care about seeing (.origin works) */
66 #define new(class) _new(class, false)
67 /** purely logical entities (.origin doesn't work) */
68 #define new_pure(class) _new(class, true)
69 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
70
71 ACCUMULATE void ONREMOVE(entity this) {}
72
73 #ifndef SVQC
74         #define delete_fn builtin_remove
75 #endif
76
77 .void(entity this) dtor;
78 #define delete(this) MACRO_BEGIN \
79     entity _this = (this); \
80     void(entity) _dtor = _this.dtor; \
81     ONREMOVE(this); \
82     if (_dtor) _dtor(_this); else delete_fn(_this); \
83     /* this = NULL; */  \
84 MACRO_END
85
86 entity _clearentity_ent;
87 STATIC_INIT(clearentity)
88 {
89         _clearentity_ent = new_pure(clearentity);
90 }
91 void clearentity(entity e)
92 {
93 #ifdef CSQC
94                 int n = e.entnum;
95 #endif
96         bool was_pure = is_pure(e);
97         copyentity(_clearentity_ent, e);
98         if (!was_pure) make_impure(e);
99 #ifdef CSQC
100                 e.entnum = n;
101 #endif
102 }
103
104 // Classes have a `spawn##cname(entity)` constructor
105 // The parameter is used across ACCUMULATE functions
106
107 .bool transmute;
108
109 // Macros to hide this implementation detail:
110 #ifdef __STDC__
111         #define NEW(cname, ...) \
112                 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
113
114     #define _TRANSMUTE(cname, this, ...) \
115         OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
116
117         #define CONSTRUCT(cname, ...) \
118                 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
119 #else
120         #define NEW(cname, ...) \
121                 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
122
123     #define _TRANSMUTE(cname, this, ...) \
124         OVERLOAD(spawn##cname, this,##__VA_ARGS__)
125
126         #define CONSTRUCT(cname, ...) \
127                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
128 #endif
129
130 #define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
131     entity _e = (this); \
132     if (_e.vtblbase != cname##_vtbl) { \
133         _e.transmute = true; \
134         _e.classname = #cname; \
135         _TRANSMUTE(cname, _e, __VA_ARGS__); \
136     } \
137     MACRO_END
138
139 #define CLASS(...) EVAL_CLASS(OVERLOAD__(CLASS, __VA_ARGS__))
140 #define EVAL_CLASS(...) __VA_ARGS__
141
142 #define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
143 #define EVAL_ATTRIB(...) __VA_ARGS__
144
145 #ifdef QCC_SUPPORT_CLASS
146
147 #warning "QCC_SUPPORT_CLASS not implemented"
148
149 #define CLASS_1(name)                               CLASS_2(name, entity)
150 #define CLASS_2(name, base)                         class name : base {
151
152 #define INIT(class)                                     void class::class()
153 #define CONSTRUCTOR(class, ...)                         void class::class(__VA_ARGS__)
154 #define DESTRUCTOR(class)                               class::~class()
155
156 #define SUPER(class)                                    super
157
158 #define ATTRIB_3(class, name, T)                        T name
159 #define ATTRIB_4(class, name, T, val)                   ATTRIB_3(class, name, T) = val
160 #define STATIC_ATTRIB(class, name, T, val)              static T name = val
161
162 #define ATTRIB_STRZONE(class, name, T, val)             T name = val
163 #define STATIC_ATTRIB_STRZONE(class, name, T, val)      static T name = val
164
165 #define ATTRIBARRAY(class, name, T, val)                T name[val]
166
167 #define METHOD(class, name, prototype)                  virtual void class::name()
168 #define STATIC_METHOD(class, name, prototype)           static void class::name()
169
170 #define ENDCLASS(class)                             };
171
172 #else
173
174 #define CLASS_1(cname) CLASS_2(cname, )
175 #define CLASS_2(cname, base)                                                                       \
176         entityclass(cname, base);                                                                      \
177         classfield(cname).bool instanceOf##cname;                                                      \
178         DEBUG_STUFF(cname)                                                                             \
179         VTBL(cname, base)                                                                              \
180         _INIT_STATIC(cname)                                                                            \
181         {                                                                                              \
182                 if (cname##_vtbl && !this.transmute)                                                       \
183                 {                                                                                          \
184                         copyentity(cname##_vtbl, this);                                                        \
185                         return;                                                                                \
186                 }                                                                                          \
187                 spawn##base##_static(this);                                                                \
188                 this.instanceOf##cname = true;                                                             \
189         }                                                                                              \
190         INIT(cname)                                                                                    \
191         {                                                                                              \
192                 /* Only statically initialize the current class, it contains everything it inherits */     \
193                 if (cname##_vtbl.vtblname == this.classname)                                               \
194                 {                                                                                          \
195                         spawn##cname##_static(this);                                                           \
196                         this.transmute = false;                                                                \
197                         this.classname = #cname;                                                               \
198                         this.vtblname = string_null;                                                           \
199                         this.vtblbase = cname##_vtbl;                                                          \
200                 }                                                                                          \
201                 spawn##base##_1(this);                                                                     \
202         }
203
204 #define INIT(cname)                                                                                \
205         ACCUMULATE cname spawn##cname##_1(cname this)
206
207 #define CONSTRUCTOR(cname, ...)                                                                    \
208         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)                                          \
209         {                                                                                              \
210                 return = this;                                                                             \
211         }                                                                                              \
212         ACCUMULATE cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
213
214 #define DESTRUCTOR(cname)                                                                          \
215         STATIC_METHOD(cname, dtorimpl, void(cname this));                                              \
216     METHOD(cname, dtor, void(cname this))                                                          \
217     {                                                                                              \
218         METHOD_REFERENCE(cname, dtorimpl)(this);                                                   \
219         this.instanceOf##cname = false;                                                            \
220         entity super = SUPER(cname);                                                               \
221         if (super != cname##_vtbl) super.dtor(this);                                               \
222     }                                                                                              \
223         STATIC_METHOD(cname, dtorimpl, void(cname this))
224
225 #define SUPER(cname) (cname##_vtbl.vtblbase)
226
227 #define ATTRIB_3(cname, name, type) classfield(cname) .type name
228 #define ATTRIB_4(cname, name, type, val)                                                           \
229         ATTRIB_3(cname, name, type);                                                                   \
230         INIT(cname)                                                                                    \
231         {                                                                                              \
232                 noref bool strzone; /* Error on strzone() calls. */                                        \
233                 this.name = val;                                                                           \
234         }                                                                                              \
235         ATTRIB_3(cname, name, type)
236
237 #define STATIC_ATTRIB(cname, name, type, val)                                                      \
238         type cname##_##name;                                                                           \
239         _INIT_STATIC(cname)                                                                            \
240         {                                                                                              \
241                 noref bool strzone; /* Error on strzone() calls. */                                        \
242                 cname##_##name = val;                                                                      \
243         }
244
245 // cleanup potentially zoned strings from base classes
246 #define ATTRIB_STRZONE(cname, name, type, val)                                                     \
247         classfield(cname).type name;                                                                   \
248         INIT(cname)                                                                                    \
249         {                                                                                              \
250                 strcpy(this.name, val);                                                                    \
251         }
252
253 #define STATIC_ATTRIB_STRZONE(cname, name, type, val)                                              \
254         type cname##_##name;                                                                           \
255         _INIT_STATIC(cname)                                                                            \
256         {                                                                                              \
257                 strcpy(cname##_##name, val);                                                               \
258         }
259
260 #define ATTRIBARRAY(cname, name, type, cnt)                                                        \
261         classfield(cname) .type name[cnt]
262
263 #define METHOD(cname, name, prototype)                                                             \
264         STATIC_METHOD(cname, name, prototype);                                                         \
265         classfield(cname) .prototype name;                                                             \
266         _INIT_STATIC(cname)                                                                            \
267         {                                                                                              \
268                 this.name = METHOD_REFERENCE(cname, name);                                                 \
269         }                                                                                              \
270         STATIC_METHOD(cname, name, prototype)
271
272 #define STATIC_METHOD(cname, name, prototype)                                                      \
273         prototype METHOD_REFERENCE(cname, name)
274
275 #define ENDCLASS(cname)                                                                            \
276         INIT(cname)                                                                                    \
277         {                                                                                              \
278                 return this;                                                                               \
279         }
280
281 // impl
282
283 .string vtblname;
284 .entity vtblbase;
285
286 void RegisterClasses() {}
287 STATIC_INIT(RegisterClasses)
288 {
289         RegisterClasses();
290 }
291
292 #define VTBL(cname, base) \
293         _INIT_STATIC(cname); \
294         entity cname##_vtbl; \
295         void cname##_vtbl_init() \
296         { \
297                 cname e = new_pure(vtbl); \
298                 spawn##cname##_static(e); \
299                 e.vtblname = #cname; \
300                 /* Top level objects refer to themselves */ \
301                 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
302                 cname##_vtbl = e; \
303         } \
304         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
305
306 #define _INIT_STATIC(cname) ACCUMULATE void spawn##cname##_static(cname this)
307
308 #if NDEBUG
309         #define DEBUG_STUFF(cname)
310 #else
311         #define DEBUG_STUFF(cname) \
312                 ERASEABLE bool is_##cname(entity e) { return e.instanceOf##cname; } \
313                 ERASEABLE void isnt_##cname(entity e) { eprint(e); }
314 #endif
315
316 #define METHOD_REFERENCE(cname, name) \
317         cname##_##name
318
319 #endif
320
321 #define spawn_static(this)
322 #define spawn_1(this)
323 #define _vtbl NULL
324 CLASS(Object)
325     DESTRUCTOR(Object) { builtin_remove(this); }
326     #define remove(this) delete(this)
327         METHOD(Object, describe, string(Object this))
328         {
329                 TC(Object, this);
330                 string s = _("No description");
331                 if (cvar("developer"))
332                 {
333                         for (int i = 0, n = numentityfields(); i < n; ++i)
334                         {
335                                 string value = getentityfieldstring(i, this);
336                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
337                         }
338                 }
339                 return s;
340         }
341         METHOD(Object, display, void(Object this, void(string name, string icon) returns))
342         {
343                 TC(Object, this);
344                 returns(sprintf("entity %i", this), "nopreview_map");
345         }
346 ENDCLASS(Object)
347 #undef spawn_static
348 #undef spawn_1
349 #undef _vtbl