]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/base.qh
Merge branch 'master' into Mario/turrets
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / base.qh
1 #ifndef MUTATORS_BASE_H
2 #define MUTATORS_BASE_H
3
4 const int CBC_ORDER_FIRST = 1;
5 const int CBC_ORDER_LAST = 2;
6 const int CBC_ORDER_EXCLUSIVE = 3;
7 const int CBC_ORDER_ANY = 4;
8
9 bool CallbackChain_ReturnValue; // read-only field of the current return value
10
11 /**
12  * Callbacks may be added to zero or more callback chains.
13  */
14 CLASS(Callback, Object)
15     /**
16      * a callback function is like this:
17      * bool mycallback()
18      * {
19      *     do something
20      *     return false;
21      * }
22      */
23     ATTRIB(Callback, cbc_func, bool(), func_null)
24     CONSTRUCTOR(Callback, bool() func) {
25         CONSTRUCT(Callback);
26         this.cbc_func = func;
27         return this;
28     }
29 ENDCLASS(Callback)
30
31 /**
32  * Callback chains contain zero or more callbacks.
33  */
34 CLASS(CallbackChain, Object)
35     CLASS(CallbackNode, Object)
36         ATTRIB(CallbackNode, cbc, Callback, NULL)
37         ATTRIB(CallbackNode, cbc_next, CallbackNode, NULL)
38         ATTRIB(CallbackNode, cbc_order, int, 0)
39         CONSTRUCTOR(CallbackNode, Callback it, int order) {
40             CONSTRUCT(CallbackNode);
41             this.cbc = it;
42             this.cbc_order = order;
43             return this;
44         }
45     ENDCLASS(CallbackNode)
46
47     ATTRIB(CallbackChain, cbc_next, CallbackNode, NULL)
48     ATTRIB(CallbackChain, cbc_order, int, 0)
49     CONSTRUCTOR(CallbackChain, string _name) {
50         CONSTRUCT(CallbackChain);
51         this.netname = _name;
52         return this;
53     }
54
55     bool CallbackChain_Add(CallbackChain this, Callback cb, int order)
56     {
57         if (order & CBC_ORDER_FIRST) {
58             if (order & CBC_ORDER_LAST)
59                 if (this.cbc_order & CBC_ORDER_ANY)
60                     return false;
61             if (this.cbc_order & CBC_ORDER_FIRST)
62                 return false;
63         } else if (order & CBC_ORDER_LAST) {
64             if (this.cbc_order & CBC_ORDER_LAST)
65                 return false;
66         }
67         entity node = NEW(CallbackNode, cb, order);
68         if (order & CBC_ORDER_FIRST) {
69             node.cbc_next = this.cbc_next;
70             this.cbc_next = node;
71         } else if (order & CBC_ORDER_LAST) {
72             CallbackNode prev = NULL, it = this.cbc_next;
73             while (it) { prev = it, it = it.cbc_next; }
74             if (prev) prev.cbc_next = node;
75             else this.cbc_next = node;
76         } else {
77             // by default we execute last, but before a possible CBC_ORDER_LAST callback
78             CallbackNode prev = NULL, it = this.cbc_next;
79             while (it && !(it.cbc_order & CBC_ORDER_LAST)) { prev = it, it = it.cbc_next; }
80             node.cbc_next = it;
81             if (prev) prev.cbc_next = node;
82             else this.cbc_next = node;
83         }
84         this.cbc_order |= (order | CBC_ORDER_ANY);
85         return true;
86     }
87     int CallbackChain_Remove(CallbackChain this, Callback cb)
88     {
89         int n = 0, order = 0;
90         for (Callback prev = NULL, it = this.cbc_next; it; prev = it, it = it.cbc_next) {
91             if (it.cbc == cb) {
92                 // remove it from the chain
93                 Callback next = it.cbc_next;
94                 if (prev) prev.cbc_next = next;
95                 else this.cbc_next = next;
96                 ++n;
97             }
98             // it is now something we want to keep
99             order |= (it.cbc_order & CBC_ORDER_ANY);
100         }
101         this.cbc_order = order;
102         return n;
103     }
104     bool CallbackChain_Call(CallbackChain this)
105     {
106         bool r = false;
107         for (Callback it = this.cbc_next; it; it = it.cbc_next) {
108             CallbackChain_ReturnValue = r;
109             r |= it.cbc.cbc_func();
110         }
111         return r; // callbacks return an error status, so 0 is default return value
112     }
113 ENDCLASS(CallbackChain)
114
115 #define _MUTATOR_HANDLE_NOP(type, id)
116 #define _MUTATOR_HANDLE_PARAMS(type, id) , type in_##id
117 #define _MUTATOR_HANDLE_PREPARE(type, id) id = in_##id;
118 #define _MUTATOR_HANDLE_PUSHTMP(type, id) type tmp_##id = id;
119 #define _MUTATOR_HANDLE_PUSHOUT(type, id) type out_##id = id;
120 #define _MUTATOR_HANDLE_POPTMP(type, id) id = tmp_##id;
121 #define _MUTATOR_HANDLE_POPOUT(type, id) id = out_##id;
122
123 void RegisterHooks() {};
124 void RegisterCallbacks() {};
125 void RegisterMutators() {};
126
127 #define _MUTATOR_HOOKABLE(id, ...) CallbackChain HOOK_##id; bool __Mutator_Send_##id(__VA_ARGS__)
128 #define MUTATOR_HOOKABLE(id, params) \
129     _MUTATOR_HOOKABLE(id, int params(_MUTATOR_HANDLE_PARAMS, _MUTATOR_HANDLE_NOP)) { \
130         params(_MUTATOR_HANDLE_PUSHTMP, _MUTATOR_HANDLE_NOP) \
131         params(_MUTATOR_HANDLE_PREPARE, _MUTATOR_HANDLE_NOP) \
132         bool ret = CallbackChain_Call(HOOK_##id); \
133         params(_MUTATOR_HANDLE_NOP,     _MUTATOR_HANDLE_PUSHOUT) \
134         params(_MUTATOR_HANDLE_POPTMP,  _MUTATOR_HANDLE_NOP) \
135         params(_MUTATOR_HANDLE_NOP,     _MUTATOR_HANDLE_POPOUT) \
136         return ret; \
137     } \
138     [[accumulate]] void RegisterHooks() { HOOK_##id = NEW(CallbackChain, #id); }
139 #define MUTATOR_CALLHOOK(id, ...) APPLY(__Mutator_Send_##id, 0, ##__VA_ARGS__)
140
141 enum {
142     MUTATOR_REMOVING,
143     MUTATOR_ADDING,
144     MUTATOR_ROLLING_BACK
145 };
146
147 typedef bool(int) mutatorfunc_t;
148
149 CLASS(Mutator, Object)
150     ATTRIB(Mutator, m_id, int, 0)
151     ATTRIB(Mutator, mutatorname, string, string_null)
152     ATTRIB(Mutator, mutatorfunc, mutatorfunc_t, func_null)
153     ATTRIB(Mutator, mutatorcheck, bool(), func_null)
154     CONSTRUCTOR(Mutator, string _name, mutatorfunc_t func) {
155         CONSTRUCT(Mutator);
156         this.mutatorname = _name;
157         this.mutatorfunc = func;
158         return this;
159     }
160 ENDCLASS(Mutator)
161
162 const int MAX_MUTATORS = 15;
163 Mutator loaded_mutators[MAX_MUTATORS];
164
165 bool Mutator_Add(Mutator mut)
166 {
167     int j = -1;
168     for (int i = 0; i < MAX_MUTATORS; ++i) {
169         if (loaded_mutators[i] == mut)
170             return true; // already added
171         if (!(loaded_mutators[i]))
172             j = i;
173     }
174     if (j < 0) {
175         backtrace("WARNING: too many mutators, cannot add any more\n");
176         return false;
177     }
178     loaded_mutators[j] = mut;
179     mutatorfunc_t func = mut.mutatorfunc;
180     if (!func(MUTATOR_ADDING)) {
181         // good
182         return true;
183     }
184     backtrace("WARNING: when adding mutator: adding failed, rolling back\n");
185     if (func(MUTATOR_ROLLING_BACK)) {
186         // baaaaad
187         error("WARNING: when adding mutator: rolling back failed");
188     }
189     return false;
190 }
191
192 void Mutator_Remove(Mutator mut)
193 {
194     int i;
195     for (i = 0; i < MAX_MUTATORS; ++i)
196         if (loaded_mutators[i] == mut)
197             break;
198     if (i >= MAX_MUTATORS) {
199         backtrace("WARNING: removing not-added mutator\n");
200         return;
201     }
202     loaded_mutators[i] = NULL;
203     mutatorfunc_t func = mut.mutatorfunc;
204     if (func(MUTATOR_REMOVING)) {
205         // baaaaad
206         error("Mutator_Remove: removing mutator failed");
207     }
208 }
209
210 #define MUTATOR_DECLARATION(name) \
211     Mutator MUTATOR_##name
212 #define MUTATOR_DEFINITION(name) \
213     bool MUTATORFUNCTION_##name(int mode); \
214     [[accumulate]] void RegisterMutators() { MUTATOR_##name = NEW(Mutator, #name, MUTATORFUNCTION_##name); } \
215     [[last]] bool MUTATORFUNCTION_##name(int mode)
216
217 const int MUTATORS_MAX = MAX_MUTATORS;
218 noref entity MUTATORS[MUTATORS_MAX], MUTATORS_first, MUTATORS_last;
219 noref int MUTATORS_COUNT;
220 #define REGISTER_MUTATOR(id, dependence) \
221     bool MUTATORFUNCTION_##id##_hooks(int mode) { return = false; } \
222     bool MUTATORFUNCTION_##id(int mode) { \
223         return = false; \
224         bool ret = MUTATORFUNCTION_##id##_hooks(mode); if (ret) return ret; \
225     } \
226     bool MUTATOR_##id##_check() { return dependence; } \
227     REGISTER(RegisterMutators, MUTATOR, MUTATORS, MUTATORS_COUNT, id, m_id, NEW(Mutator, #id, MUTATORFUNCTION_##id)) \
228     { this.mutatorcheck = MUTATOR_##id##_check; } \
229     [[accumulate]] bool MUTATORFUNCTION_##id(int mode)
230
231 STATIC_INIT(Mutators) {
232     RegisterHooks();
233     RegisterCallbacks();
234     RegisterMutators();
235     FOREACH(MUTATORS, it.mutatorcheck(), LAMBDA(Mutator_Add(it)));
236 }
237
238 #define MUTATOR_ONADD                   if (mode == MUTATOR_ADDING)
239 #define MUTATOR_ONREMOVE                if (mode == MUTATOR_REMOVING)
240 #define MUTATOR_ONROLLBACK_OR_REMOVE    if (mode == MUTATOR_REMOVING || mode == MUTATOR_ROLLING_BACK)
241 #define MUTATOR_ADD(name)               Mutator_Add(MUTATOR_##name)
242 #define MUTATOR_REMOVE(name)            Mutator_Remove(MUTATOR_##name)
243 #define MUTATOR_RETURNVALUE             CallbackChain_ReturnValue
244
245 #define _MUTATOR_CALLBACK(name, func) \
246     Callback CALLBACK_##name; \
247     bool func(); \
248     [[accumulate]] void RegisterCallbacks() { CALLBACK_##name = NEW(Callback, func); }
249
250 #define MUTATOR_HOOKFUNCTION(...) \
251     OVERLOAD(MUTATOR_HOOKFUNCTION, __VA_ARGS__)
252
253 #define MUTATOR_HOOKFUNCTION_1(name) \
254     _MUTATOR_CALLBACK(name, HOOKFUNCTION_##name) \
255     bool HOOKFUNCTION_##name()
256
257 #define MUTATOR_HOOKFUNCTION_2(mut, cb) \
258     MUTATOR_HOOKFUNCTION(mut, cb, CBC_ORDER_ANY)
259
260 #define MUTATOR_HOOKFUNCTION_3(mut, cb, order) \
261     _MUTATOR_CALLBACK(mut##_##cb, mut##_##cb) \
262     [[accumulate]] bool MUTATORFUNCTION_##mut##_hooks(int mode) { MUTATOR_HOOK(cb, mut##_##cb, order); } \
263     bool mut##_##cb() { return = false; } \
264     [[accumulate]] bool mut##_##cb()
265
266 #define MUTATOR_HOOK(cb, func, order) do {                              \
267     MUTATOR_ONADD {                                                     \
268         if (!CallbackChain_Add(HOOK_##cb, CALLBACK_##func, order)) {    \
269             print("HOOK FAILED: ", #cb, ":", #func, "\n");              \
270             return true;                                                \
271         }                                                               \
272     }                                                                   \
273     MUTATOR_ONROLLBACK_OR_REMOVE {                                      \
274         CallbackChain_Remove(HOOK_##cb, CALLBACK_##func);               \
275     }                                                                   \
276 } while (0)
277
278 #include "events.qh"
279
280 #endif