5 entity CallbackChain_New(string name)
9 e.classname = "callbackchain";
14 float CallbackChain_Add(entity cb, float() func, float order)
17 if(order & CBC_ORDER_FIRST)
19 if(order & CBC_ORDER_LAST)
20 if(cb.cbc_order & CBC_ORDER_ANY)
22 if(cb.cbc_order & CBC_ORDER_FIRST)
25 else if(order & CBC_ORDER_LAST)
27 if(cb.cbc_order & CBC_ORDER_LAST)
32 thiscb.classname = "callback";
33 thiscb.cbc_func = func;
34 thiscb.cbc_order = order;
35 if(order & CBC_ORDER_FIRST)
37 thiscb.cbc_next = cb.cbc_next;
40 else if(order & CBC_ORDER_LAST)
42 for(e = cb; e.cbc_next; e = e.cbc_next);
47 // by default we execute last, but before a possible CBC_ORDER_LAST callback
48 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!
49 thiscb.cbc_next = e.cbc_next;
52 cb.cbc_order |= (order | CBC_ORDER_ANY);
56 float CallbackChain_Remove(entity cb, float() func)
63 for(e = cb; e.cbc_next; e = e.cbc_next)
65 while(e.cbc_next.cbc_func == func)
67 // remove e.cbc_next from the chain
69 e2 = e.cbc_next.cbc_next;
74 // e.cbc_next is now something we want to keep
75 order |= (e.cbc_next.cbc_order & CBC_ORDER_ANY);
81 float CallbackChain_Call(entity cb)
86 for(e = cb; e.cbc_next; e = e.cbc_next)
88 CallbackChain_ReturnValue = r;
89 r |= e.cbc_next.cbc_func();
91 return r; // callbacks return an error status, so 0 is default return value
94 #define MAX_MUTATORS 8
95 string loaded_mutators[MAX_MUTATORS];
96 float Mutator_Add(mutatorfunc_t func, string name)
100 for(i = 0; i < MAX_MUTATORS; ++i)
102 if(name == loaded_mutators[i])
103 return 1; // already added
104 if (!(loaded_mutators[i]))
109 backtrace("WARNING: too many mutators, cannot add any more\n");
112 loaded_mutators[j] = name;
114 if(func(MUTATOR_ADDING) == 0)
120 backtrace("WARNING: when adding mutator: adding failed, rolling back\n");
122 if(func(MUTATOR_ROLLING_BACK) != 0)
125 error("WARNING: when adding mutator: rolling back failed");
129 void Mutator_Remove(float(float) func, string name)
132 for(i = 0; i < MAX_MUTATORS; ++i)
133 if(name == loaded_mutators[i])
135 if(i >= MAX_MUTATORS)
137 backtrace("WARNING: removing not-added mutator\n");
140 loaded_mutators[i] = string_null;
142 if(func(MUTATOR_REMOVING) != 0)
145 error("Mutator_Remove: removing mutator failed");