X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fxonotic-data.pk3dir.git;a=blobdiff_plain;f=qcsrc%2Fcommon%2Fmutators%2Fbase.qh;h=c1d658576e2cb4b22e43a7e6446356312467265f;hp=7ef70f2c46b9db347c92f45b8d04444942531fca;hb=991de5e6922cd3c283de56c3249624f0f1bfe767;hpb=53911d2ce2724f2da23fb6806e491ce795ae4326 diff --git a/qcsrc/common/mutators/base.qh b/qcsrc/common/mutators/base.qh index 7ef70f2c46..c1d658576e 100644 --- a/qcsrc/common/mutators/base.qh +++ b/qcsrc/common/mutators/base.qh @@ -1,5 +1,9 @@ #pragma once +#ifdef CSQC +#include +#endif + const int CBC_ORDER_FIRST = 1; const int CBC_ORDER_LAST = 2; const int CBC_ORDER_EXCLUSIVE = 3; @@ -131,7 +135,7 @@ void RegisterCallbacks() {}; params(_MUTATOR_HANDLE_NOP, _MUTATOR_HANDLE_POPOUT) \ return ret; \ } \ - [[accumulate]] void RegisterHooks() { HOOK_##id = NEW(CallbackChain, #id); } + ACCUMULATE void RegisterHooks() { HOOK_##id = NEW(CallbackChain, #id); } #define MUTATOR_CALLHOOK(id, ...) _MUTATOR_CALLHOOK(id, __VA_ARGS__) #ifdef __STDC__ @@ -167,6 +171,8 @@ void Mutator_Remove(Mutator mut); bool mutator_log = false; .bool m_added; +#define MUTATOR_IS_ENABLED(this) MUTATOR_##this.mutatorcheck() + #ifdef GAMEQC /** server mutators activate corresponding client mutators for all clients */ REGISTER_NET_LINKED(Mutator) @@ -185,9 +191,9 @@ bool Mutator_SendEntity(entity this, entity to, int sf) void NET_Mutator_Remove(entity this) { string s = this.netname; - WITH(bool, mutator_log, true, LAMBDA( + WITH(bool, mutator_log, true, { FOREACH(Mutators, it.registered_id == s, Mutator_Remove(it)); - )); + }); } NET_HANDLE(Mutator, bool isNew) { @@ -199,9 +205,9 @@ NET_HANDLE(Mutator, bool isNew) make_pure(this); this.entremove = NET_Mutator_Remove; int added = 0; - WITH(bool, mutator_log, true, LAMBDA( + WITH(bool, mutator_log, true, { FOREACH(Mutators, it.registered_id == s, { Mutator_Add(it); ++added; }); - )); + }); if (added > 1) LOG_WARNF("Added more than one mutator for %s", s); } } @@ -253,15 +259,15 @@ void Mutator_Remove(Mutator mut) } #define REGISTER_MUTATOR(id, dependence) \ - bool MUTATORFUNCTION_##id##_hooks(int mode) { return = false; } \ - bool MUTATORFUNCTION_##id(int mode) { \ + bool MUTATORFUNC_##id##_hooks(int mode) { return = false; } \ + bool MUTATORFUNC_##id(int mode) { \ return = false; \ - bool ret = MUTATORFUNCTION_##id##_hooks(mode); if (ret) return ret; \ + bool ret = MUTATORFUNC_##id##_hooks(mode); if (ret) return ret; \ } \ bool MUTATOR_##id##_check() { return dependence; } \ - REGISTER(Mutators, MUTATOR, id, m_id, NEW(Mutator, #id, MUTATORFUNCTION_##id)) \ + REGISTER(Mutators, MUTATOR, id, m_id, NEW(Mutator, #id, MUTATORFUNC_##id)) \ { this.mutatorcheck = MUTATOR_##id##_check; } \ - [[accumulate]] bool MUTATORFUNCTION_##id(int mode) + ACCUMULATE bool MUTATORFUNC_##id(int mode) STATIC_INIT(Mutators) { RegisterHooks(); @@ -276,14 +282,28 @@ STATIC_INIT_LATE(Mutators) { #define MUTATOR_ONADD if (mode == MUTATOR_ADDING) #define MUTATOR_ONREMOVE if (mode == MUTATOR_REMOVING) #define MUTATOR_ONROLLBACK_OR_REMOVE if (mode == MUTATOR_REMOVING || mode == MUTATOR_ROLLING_BACK) + +#define MUTATOR_STATIC() MACRO_BEGIN \ + MUTATOR_ONADD { \ + /* game loads at time 1 */ \ + if (time > 1) { \ + error("This is a game type and it cannot be added at runtime."); \ + } \ + } \ + MUTATOR_ONREMOVE { \ + LOG_INFO("This is a game type and it cannot be removed at runtime."); \ + return -1; \ + } \ +MACRO_END + #define MUTATOR_ADD(name) Mutator_Add(MUTATOR_##name) #define MUTATOR_REMOVE(name) Mutator_Remove(MUTATOR_##name) #define MUTATOR_RETURNVALUE CallbackChain_ReturnValue #define _MUTATOR_CALLBACK(name, func) \ - Callback CALLBACK_##name; \ + Callback CB_##name; \ bool func(); \ - [[accumulate]] void RegisterCallbacks() { CALLBACK_##name = NEW(Callback, func); } + ACCUMULATE void RegisterCallbacks() { CB_##name = NEW(Callback, func); } #define MUTATOR_HOOKFUNCTION(...) \ EVAL_MUTATOR_HOOKFUNCTION(OVERLOAD(MUTATOR_HOOKFUNCTION, __VA_ARGS__)) @@ -294,20 +314,26 @@ STATIC_INIT_LATE(Mutators) { #define MUTATOR_HOOKFUNCTION_3(mut, cb, order) \ _MUTATOR_CALLBACK(mut##_##cb, mut##_##cb) \ - [[accumulate]] bool MUTATORFUNCTION_##mut##_hooks(int mode) { MUTATOR_HOOK(cb, mut##_##cb, order); } \ + ACCUMULATE bool MUTATORFUNC_##mut##_hooks(int mode) { MUTATOR_HOOK(cb, mut##_##cb, order); } \ bool mut##_##cb() { return = false; } \ - [[accumulate]] bool mut##_##cb() - -#define MUTATOR_HOOK(cb, func, order) MACRO_BEGIN { \ - MUTATOR_ONADD { \ - if (!CallbackChain_Add(HOOK_##cb, CALLBACK_##func, order)) { \ - LOG_INFO("HOOK FAILED: ", #cb, ":", #func, "\n"); \ - return true; \ - } \ - } \ - MUTATOR_ONROLLBACK_OR_REMOVE { \ - CallbackChain_Remove(HOOK_##cb, CALLBACK_##func); \ - } \ -} MACRO_END + ACCUMULATE bool mut##_##cb() + +void _mutPrintFail(string cb, string func) +{ + // this is inside a function to avoid expanding it on compilation everytime + LOG_INFO("HOOK FAILED: ", cb, ":", func); +} + +#define MUTATOR_HOOK(cb, func, order) MACRO_BEGIN \ + MUTATOR_ONADD { \ + if (!CallbackChain_Add(HOOK_##cb, CB_##func, order)) { \ + _mutPrintFail(#cb, #func); \ + return true; \ + } \ + } \ + MUTATOR_ONROLLBACK_OR_REMOVE { \ + CallbackChain_Remove(HOOK_##cb, CB_##func); \ + } \ +MACRO_END #include "events.qh"