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