]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
Merge branch 'master' into terencehill/lms_itemtimes_fix
[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 #if SPAWN_PURE
38 entity spawn_pure() = #600;
39 #else
40 #define spawn_pure() _spawn()
41 #endif
42
43 entity __spawn(string _classname, string _sourceLoc, bool pure)
44 {
45         entity this = pure ? spawn_pure() : _spawn();
46         this.classname = _classname;
47         this.sourceLoc = _sourceLoc;
48         if (pure) {
49                 make_pure(this);
50                 #ifdef CSQC
51                 setorigin(this, '0 0 10000');
52                 #endif
53         }
54         return this;
55 }
56
57
58 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
59 #define EVAL_entityclass(...) __VA_ARGS__
60 #define entityclass_1(name) entityclass_2(name, Object)
61 #ifndef QCC_SUPPORT_ENTITYCLASS
62         #define entityclass_2(name, base) typedef entity name
63         #define class(name)
64         #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
65 #else
66         #define entityclass_2(name, base) entityclass name : base {}
67         #define class(name) [[class(name)]]
68         #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
69 #endif
70 /** entities you care about seeing (.origin works) */
71 #define new(class) _new(class, false)
72 /** purely logical entities (.origin doesn't work) */
73 #define new_pure(class) _new(class, true)
74 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
75
76 entity _clearentity_ent;
77 STATIC_INIT(clearentity)
78 {
79         _clearentity_ent = new_pure(clearentity);
80         make_pure(_clearentity_ent);
81 }
82 void clearentity(entity e)
83 {
84 #ifdef CSQC
85                 int n = e.entnum;
86 #endif
87         bool was_pure = is_pure(e);
88         copyentity(_clearentity_ent, e);
89         if (!was_pure) make_impure(e);
90 #ifdef CSQC
91                 e.entnum = n;
92 #endif
93 }
94
95 // Classes have a `spawn##cname(entity)` constructor
96 // The parameter is used across [[accumulate]] functions
97
98 // Macros to hide this implementation detail:
99 #ifdef __STDC__
100         #define NEW(cname, ...) \
101                 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
102         #define CONSTRUCT(cname, ...) \
103                 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
104 #else
105         #define NEW(cname, ...) \
106                 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
107
108         #define CONSTRUCT(cname, ...) \
109                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
110 #endif
111
112 #define CONSTRUCTOR(cname, ...) \
113         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
114         { \
115                 return = this; \
116         } \
117         [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
118
119 .string vtblname;
120 .entity vtblbase;
121
122 void RegisterClasses() {}
123 STATIC_INIT(RegisterClasses)
124 {
125         RegisterClasses();
126 }
127
128 #define VTBL(cname, base) \
129         INIT_STATIC(cname); \
130         entity cname##_vtbl; \
131         void cname##_vtbl_init() \
132         { \
133                 cname e = new_pure(vtbl); \
134                 spawn##cname##_static(e); \
135                 e.vtblname = #cname; \
136                 /* Top level objects refer to themselves */ \
137                 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
138                 cname##_vtbl = e; \
139         } \
140         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
141
142 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
143 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
144
145 #define CLASS(cname, base)                  \
146         entityclass(cname, base);               \
147         class(cname).bool instanceOf##cname;   \
148         VTBL(cname, base)                       \
149         INIT_STATIC(cname) \
150         {                    \
151                 if (cname##_vtbl) \
152                 {                 \
153                         copyentity(cname##_vtbl, this); \
154                         return;                         \
155                 }                                   \
156                 spawn##base##_static(this);         \
157                 this.instanceOf##cname = true;      \
158         }                                       \
159         INIT(cname) \
160         {                           \
161                 /* Only statically initialize the current class, it contains everything it inherits */ \
162                 if (cname##_vtbl.vtblname == this.classname) \
163                 { \
164                         spawn##cname##_static(this);    \
165                         this.classname = #cname;        \
166                         this.vtblname = string_null;    \
167                         this.vtblbase = cname##_vtbl;   \
168                 }                                   \
169                 spawn##base##_1(this);              \
170         }
171
172 #define METHOD_REFERENCE(cname, name) \
173         cname##_##name
174
175 #define STATIC_METHOD(cname, name, prototype) \
176         prototype METHOD_REFERENCE(cname, name)
177
178 #define METHOD(cname, name, prototype) \
179         STATIC_METHOD(cname, name, prototype); \
180         class(cname) .prototype name; \
181         INIT_STATIC(cname) \
182         { \
183                 this.name = METHOD_REFERENCE(cname, name); \
184         } \
185         STATIC_METHOD(cname, name, prototype)
186
187 #define ATTRIB(cname, name, type, val)      \
188         class(cname).type name;                \
189         INIT(cname) \
190         { \
191                 noref bool strzone; /* Error on strzone() calls. */ \
192                 this.name = val; \
193         }
194
195 #define ATTRIB_STRZONE(cname, name, type, val)      \
196         class(cname).type name;                \
197         INIT(cname) \
198         { \
199                 if (this.name) \
200                         strunzone(this.name); \
201                 this.name = strzone(val); \
202         }
203
204 #define ATTRIBARRAY(cname, name, type, cnt) \
205         class(cname).type name[cnt];
206
207 #define ENDCLASS(cname) \
208         INIT(cname) \
209         { \
210                 return this; \
211         }
212
213 #define SUPER(cname) (cname##_vtbl.vtblbase)
214
215 #define spawn_static(this)
216 #define spawn_1(this)
217 #define _vtbl NULL
218 CLASS(Object, );
219         METHOD(Object, describe, string(entity this))
220         {
221                 string s = _("No description");
222                 if (cvar("developer"))
223                 {
224                         for (int i = 0, n = numentityfields(); i < n; ++i)
225                         {
226                                 string value = getentityfieldstring(i, this);
227                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
228                         }
229                 }
230                 return s;
231         }
232         METHOD(Object, display, void(entity this, void(string name, string icon) returns))
233         {
234                 returns(sprintf("entity %i", this), "nopreview_map");
235         }
236 ENDCLASS(Object)
237 #undef spawn_static
238 #undef spawn_1
239 #undef _vtbl