]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/self.qh
58375001453060dd454dd2d8bd04e8bb8802b2ef
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / self.qh
1 #pragma once
2
3 // Transition from global 'self' to local 'this'
4
5 // Step 1: auto oldself
6 #if 1
7     #define SELFPARAM() noref const entity this = self
8 #endif
9
10 // Step 2: const self
11 #if 1
12     #define self (0, self)
13     [[alias("self")]] entity __self;
14     #define setself(s) (__self = s)
15     #define WITHSELF(value, block) WITH(entity, __self, value, (0, block))
16 #endif
17
18 // Step 3: propagate SELFPARAM()
19 #if 1
20     #undef self
21     #define self (this, self)
22     #undef SELFPARAM
23     #define SELFPARAM() const entity this = __self
24 #endif
25
26 // Step 4: kill unstructured setself
27 #if 1
28     #undef setself
29 #endif
30
31 // Step 5: this should work
32 #if 0
33     #undef self
34     #define self (0, this)
35 #endif
36
37 // Step 6: remove SELFPARAM, add parameters
38 #if 0
39     #undef SELFPARAM
40 #endif
41
42 // Step 7: remove WITHSELF, no replacement
43 #if 0
44     #undef WITHSELF
45     #define WITHSELF(value, block) block
46 #endif
47
48 #define SELFWRAP(T, R, args, forward) \
49     .R() T; \
50     .R() __##T = T; \
51     .R args self##T; \
52     R T##_self() { SELFPARAM(); return this.self##T forward; }
53
54 noref entity _selftemp;
55 #define SELFWRAP_SET(T, e, f) \
56     (_selftemp = (e), _selftemp.__##T = ((f) ? T##_self : func_null), _selftemp.self##T = (f))
57 #define SELFWRAP_GET(T, e) \
58     (0, (e).self##T)
59 #define _SELFWRAP_SET(T, e, f) \
60     ((e).__##T = (f))
61 #define _SELFWRAP_GET(T, e) \
62     (0, (e).__##T)
63
64 SELFWRAP(think, void, (entity this), (this))
65 #define setthink(e, f) SELFWRAP_SET(think, e, f)
66 #define getthink(e) SELFWRAP_GET(think, e)
67
68 SELFWRAP(touch, void, (entity this), (this))
69 #define settouch(e, f) SELFWRAP_SET(touch, e, f)
70 #define gettouch(e) SELFWRAP_GET(touch, e)
71
72 SELFWRAP(predraw, void, (entity this), (this))
73 #define setpredraw(e, f) SELFWRAP_SET(predraw, e, f)
74
75 SELFWRAP(customizeentityforclient, bool, (entity this), (this))
76 #define setcefc(e, f) SELFWRAP_SET(customizeentityforclient, e, f)
77 #define getcefc(e) SELFWRAP_GET(customizeentityforclient, e)
78
79 #ifndef MENUQC
80 void adaptor_think2touch(entity this) { WITH(entity, other, NULL, gettouch(this)(this)); }
81 void adaptor_think2use(entity this) { if (this.use) this.use(this, NULL, NULL); }
82 #endif