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