]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/mutators/base.qc
Merge branch 'maint' (early part before Transifex)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / base.qc
index f4761b884dca28175d58da08589a777c589722c2..2f78ec1827448ad82616451f3bd3e8c21211d03b 100644 (file)
@@ -1,72 +1,60 @@
-.float() cbc_func;
+#include "base.qh"
+#include "../_all.qh"
+
+.bool() cbc_func;
 .entity cbc_next;
-.float cbc_order;
+.int cbc_order;
 
 entity CallbackChain_New(string name)
 {
-       entity e;
-       e = spawn();
+       entity e = spawn();
        e.classname = "callbackchain";
        e.netname = name;
        return e;
 }
 
-float CallbackChain_Add(entity cb, float() func, float order)
+bool CallbackChain_Add(entity cb, bool() func, int order)
 {
-       entity e;
-       if(order & CBC_ORDER_FIRST)
-       {
-               if(order & CBC_ORDER_LAST)
-                       if(cb.cbc_order & CBC_ORDER_ANY)
-                               return 0;
-               if(cb.cbc_order & CBC_ORDER_FIRST)
-                       return 0;
-       }
-       else if(order & CBC_ORDER_LAST)
-       {
-               if(cb.cbc_order & CBC_ORDER_LAST)
-                       return 0;
+       if (order & CBC_ORDER_FIRST) {
+               if (order & CBC_ORDER_LAST)
+                       if (cb.cbc_order & CBC_ORDER_ANY)
+                               return false;
+               if (cb.cbc_order & CBC_ORDER_FIRST)
+                       return false;
+       } else if (order & CBC_ORDER_LAST) {
+               if (cb.cbc_order & CBC_ORDER_LAST)
+                       return false;
        }
-       entity thiscb;
-       thiscb = spawn();
+       entity thiscb = spawn();
        thiscb.classname = "callback";
        thiscb.cbc_func = func;
        thiscb.cbc_order = order;
-       if(order & CBC_ORDER_FIRST)
-       {
+       if (order & CBC_ORDER_FIRST) {
                thiscb.cbc_next = cb.cbc_next;
                cb.cbc_next = thiscb;
-       }
-       else if(order & CBC_ORDER_LAST)
-       {
-               for(e = cb; e.cbc_next; e = e.cbc_next);
+       } else if (order & CBC_ORDER_LAST) {
+               entity e = cb;
+               while (e.cbc_next) e = e.cbc_next;
                e.cbc_next = thiscb;
-       }
-       else
-       {
+       } else {
                // by default we execute last, but before a possible CBC_ORDER_LAST callback
-               for(e = cb; e.cbc_next && !(e.cbc_next.cbc_order & CBC_ORDER_LAST); e = e.cbc_next); // we must make sure that we insert BEFORE an CBC_ORDER_LAST mutator!
+               entity e = cb;
+               // we must make sure that we insert BEFORE an CBC_ORDER_LAST mutator!
+               while (e.cbc_next && !(e.cbc_next.cbc_order & CBC_ORDER_LAST)) e = e.cbc_next;
                thiscb.cbc_next = e.cbc_next;
                e.cbc_next = thiscb;
        }
        cb.cbc_order |= (order | CBC_ORDER_ANY);
-       return 1;
+       return true;
 }
 
-float CallbackChain_Remove(entity cb, float() func)
+int CallbackChain_Remove(entity cb, bool() func)
 {
-       float order;
-       entity e;
-       float n;
-       n = 0;
-       order = 0;
-       for(e = cb; e.cbc_next; e = e.cbc_next)
-       {
-               while(e.cbc_next.cbc_func == func)
-               {
+       int n = 0, order = 0;
+       for (entity e = cb; e.cbc_next; e = e.cbc_next) {
+               while (e.cbc_next.cbc_func == func) {
                        // remove e.cbc_next from the chain
-                       entity e2;
-                       e2 = e.cbc_next.cbc_next;
+                       entity e2 = e.cbc_next.cbc_next;
                        remove(e.cbc_next);
                        e.cbc_next = e2;
                        ++n;
@@ -78,69 +66,59 @@ float CallbackChain_Remove(entity cb, float() func)
        return n;
 }
 
-float CallbackChain_Call(entity cb)
+bool CallbackChain_Call(entity cb)
 {
-       float r;
-       entity e;
-       r = 0;
-       for(e = cb; e.cbc_next; e = e.cbc_next)
-       {
+       bool r = false;
+       for (entity e = cb; e.cbc_next; e = e.cbc_next) {
                CallbackChain_ReturnValue = r;
                r |= e.cbc_next.cbc_func();
        }
        return r; // callbacks return an error status, so 0 is default return value
 }
 
-#define MAX_MUTATORS 8
+const int MAX_MUTATORS = 15;
 string loaded_mutators[MAX_MUTATORS];
-float Mutator_Add(mutatorfunc_t func, string name)
+bool Mutator_Add(mutatorfunc_t func, string name)
 {
-       float i, j;
-       j = -1;
-       for(i = 0; i < MAX_MUTATORS; ++i)
-       {
-               if(name == loaded_mutators[i])
-                       return 1; // already added
-               if not(loaded_mutators[i])
+       int j = -1;
+       for (int i = 0; i < MAX_MUTATORS; ++i) {
+               if (name == loaded_mutators[i])
+                       return true; // already added
+               if (!(loaded_mutators[i]))
                        j = i;
        }
-       if(j < 0)
-       {
+       if (j < 0) {
                backtrace("WARNING: too many mutators, cannot add any more\n");
-               return 0;
+               return false;
        }
        loaded_mutators[j] = name;
 
-       if(func(MUTATOR_ADDING) == 0)
-       {
+       if (!func(MUTATOR_ADDING)) {
                // good
-               return 1;
+               return true;
        }
 
        backtrace("WARNING: when adding mutator: adding failed, rolling back\n");
 
-       if(func(MUTATOR_ROLLING_BACK) != 0)
-       {
+       if (func(MUTATOR_ROLLING_BACK)) {
                // baaaaad
                error("WARNING: when adding mutator: rolling back failed");
        }
-       return 0;
+       return false;
 }
-void Mutator_Remove(float(float) func, string name)
+void Mutator_Remove(mutatorfunc_t func, string name)
 {
-       float i;
-       for(i = 0; i < MAX_MUTATORS; ++i)
-               if(name == loaded_mutators[i])
+       int i;
+       for (i = 0; i < MAX_MUTATORS; ++i)
+               if (name == loaded_mutators[i])
                        break;
-       if(i >= MAX_MUTATORS)
-       {
+       if (i >= MAX_MUTATORS) {
                backtrace("WARNING: removing not-added mutator\n");
                return;
        }
        loaded_mutators[i] = string_null;
 
-       if(func(MUTATOR_REMOVING) != 0)
-       {
+       if (func(MUTATOR_REMOVING) != 0) {
                // baaaaad
                error("Mutator_Remove: removing mutator failed");
        }