Put pure entities in the corners of the map instead of out of bounds, fixes heavy...
[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 (0, null_entity)
9         #define world NULL
10 #else
11         #define NULL (0, world)
12 #endif
13
14 .vector origin;
15 .bool pure_data;
16 /** @deprecated use new_pure or NEW(class) */
17 #define make_pure(e) \
18         MACRO_BEGIN \
19         { \
20                 (e).pure_data = true; \
21         } MACRO_END
22 #define make_impure(e) \
23         MACRO_BEGIN \
24         { \
25                 (e).pure_data = false; \
26         } MACRO_END
27 #define is_pure(e) ((e).pure_data)
28
29 .string classname;
30 /** Location entity was spawned from in source */
31 .string sourceLoc;
32 entity _spawn();
33
34 #ifndef SPAWN_PURE
35 #define SPAWN_PURE 0
36 #endif
37
38 // pure entities: need no .origin
39 #if SPAWN_PURE
40 entity spawn_pure() = #600;
41 #else
42 #define spawn_pure() _spawn()
43 #endif
44
45 entity __spawn(string _classname, string _sourceLoc, bool pure)
46 {
47         entity this = pure ? spawn_pure() : _spawn();
48         this.classname = _classname;
49         this.sourceLoc = _sourceLoc;
50         if (pure) {
51                 make_pure(this);
52                 #ifdef CSQC
53                 setorigin(this, world.mins + '0 0 20');
54                 #endif
55                 #ifdef SVQC
56         setorigin(this, world.maxs - '0 0 20');
57         #endif
58         }
59         return this;
60 }
61
62
63 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
64 #define EVAL_entityclass(...) __VA_ARGS__
65 #define entityclass_1(name) entityclass_2(name, Object)
66 #ifndef QCC_SUPPORT_ENTITYCLASS
67         #define entityclass_2(name, base) USING(name, entity)
68         #define class(name)
69         #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
70 #else
71         #define entityclass_2(name, base) entityclass name : base {}
72         #define class(name) [[class(name)]]
73         #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
74 #endif
75 /** entities you care about seeing (.origin works) */
76 #define new(class) _new(class, false)
77 /** purely logical entities (.origin doesn't work) */
78 #define new_pure(class) _new(class, true)
79 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
80
81 #define delete(this) MACRO_BEGIN { \
82     entity _this = (this); \
83     void(entity) _dtor = _this.dtor; \
84     if (_dtor) _dtor(_this); else remove(_this); \
85     /* this = NULL; */  \
86 } MACRO_END
87
88 entity _clearentity_ent;
89 STATIC_INIT(clearentity)
90 {
91         _clearentity_ent = new_pure(clearentity);
92 }
93 void clearentity(entity e)
94 {
95 #ifdef CSQC
96                 int n = e.entnum;
97 #endif
98         bool was_pure = is_pure(e);
99         copyentity(_clearentity_ent, e);
100         if (!was_pure) make_impure(e);
101 #ifdef CSQC
102                 e.entnum = n;
103 #endif
104 }
105
106 // Classes have a `spawn##cname(entity)` constructor
107 // The parameter is used across [[accumulate]] functions
108
109 .bool transmute;
110
111 // Macros to hide this implementation detail:
112 #ifdef __STDC__
113         #define NEW(cname, ...) \
114                 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
115
116     #define _TRANSMUTE(cname, this, ...) \
117         OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
118
119         #define CONSTRUCT(cname, ...) \
120                 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
121 #else
122         #define NEW(cname, ...) \
123                 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
124
125     #define _TRANSMUTE(cname, this, ...) \
126         OVERLOAD(spawn##cname, this,##__VA_ARGS__)
127
128         #define CONSTRUCT(cname, ...) \
129                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
130 #endif
131
132 #define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
133     entity _e = (this); \
134     if (_e.vtblbase != cname##_vtbl) { \
135         _e.transmute = true; \
136         _e.classname = #cname; \
137         _TRANSMUTE(cname, _e, __VA_ARGS__); \
138     } \
139     MACRO_END
140
141 #define CONSTRUCTOR(cname, ...) \
142         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
143         { \
144                 return = this; \
145         } \
146         [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
147
148 .string vtblname;
149 .entity vtblbase;
150
151 void RegisterClasses() {}
152 STATIC_INIT(RegisterClasses)
153 {
154         RegisterClasses();
155 }
156
157 #define VTBL(cname, base) \
158         _INIT_STATIC(cname); \
159         entity cname##_vtbl; \
160         void cname##_vtbl_init() \
161         { \
162                 cname e = new_pure(vtbl); \
163                 spawn##cname##_static(e); \
164                 e.vtblname = #cname; \
165                 /* Top level objects refer to themselves */ \
166                 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
167                 cname##_vtbl = e; \
168         } \
169         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
170
171 #define _INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
172 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
173
174 #define CLASS(cname, base)                  \
175         entityclass(cname, base);               \
176         class(cname).bool instanceOf##cname;    \
177     bool is_##cname(entity e) { return e.instanceOf##cname; } \
178     void isnt_##cname(entity e) { eprint(e); } \
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 METHOD_REFERENCE(cname, name) \
205         cname##_##name
206
207 #define STATIC_METHOD(cname, name, prototype) \
208         prototype METHOD_REFERENCE(cname, name)
209
210 #define METHOD(cname, name, prototype) \
211         STATIC_METHOD(cname, name, prototype); \
212         class(cname) .prototype name; \
213         _INIT_STATIC(cname) \
214         { \
215                 this.name = METHOD_REFERENCE(cname, name); \
216         } \
217         STATIC_METHOD(cname, name, prototype)
218
219 #define DESTRUCTOR(cname) \
220         STATIC_METHOD(cname, dtorimpl, void(cname this)); \
221     METHOD(cname, dtor, void(cname this)) \
222     { \
223         METHOD_REFERENCE(cname, dtorimpl)(this); \
224         this.instanceOf##cname = false; \
225         entity super = SUPER(cname); \
226         if (super != cname##_vtbl) super.dtor(this); \
227     } \
228         STATIC_METHOD(cname, dtorimpl, void(cname this))
229
230 #define ATTRIB(cname, name, type, val)      \
231         class(cname).type name;                \
232         INIT(cname) \
233         { \
234                 noref bool strzone; /* Error on strzone() calls. */ \
235                 this.name = val; \
236         }
237
238 #define STATIC_ATTRIB(cname, name, type, val) \
239         type cname##_##name; \
240         _INIT_STATIC(cname) \
241         { \
242                 noref bool strzone; /* Error on strzone() calls. */ \
243                 cname##_##name = val; \
244         }
245
246 // cleanup potentially zoned strings from base classes
247
248 #define ATTRIB_STRZONE(cname, name, type, val)      \
249         class(cname).type name;                \
250         INIT(cname) \
251         { \
252                 if (this.name) \
253                         strunzone(this.name); \
254                 this.name = strzone(val); \
255         }
256
257 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
258         type cname##_##name; \
259         _INIT_STATIC(cname) \
260         { \
261         if (cname##_##name) \
262             strunzone(cname##_##name); \
263                 cname##_##name = val; \
264         }
265
266 #define ATTRIBARRAY(cname, name, type, cnt) \
267         class(cname).type name[cnt];
268
269 #define ENDCLASS(cname) \
270         INIT(cname) \
271         { \
272                 return this; \
273         }
274
275 #define SUPER(cname) (cname##_vtbl.vtblbase)
276
277 #define spawn_static(this)
278 #define spawn_1(this)
279 #define _vtbl NULL
280 CLASS(Object, );
281     DESTRUCTOR(Object) { remove(this); }
282     #define remove(this) delete(this)
283         METHOD(Object, describe, string(Object this))
284         {
285             TC(Object, this);
286                 string s = _("No description");
287                 if (cvar("developer"))
288                 {
289                         for (int i = 0, n = numentityfields(); i < n; ++i)
290                         {
291                                 string value = getentityfieldstring(i, this);
292                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
293                         }
294                 }
295                 return s;
296         }
297         METHOD(Object, display, void(Object this, void(string name, string icon) returns))
298         {
299             TC(Object, this);
300                 returns(sprintf("entity %i", this), "nopreview_map");
301         }
302 ENDCLASS(Object)
303 #undef spawn_static
304 #undef spawn_1
305 #undef _vtbl