1 #ifndef MUTATORS_BASE_H
2 #define MUTATORS_BASE_H
3 const int CBC_ORDER_FIRST = 1;
4 const int CBC_ORDER_LAST = 2;
5 const int CBC_ORDER_EXCLUSIVE = 3;
6 const int CBC_ORDER_ANY = 4;
8 bool CallbackChain_ReturnValue; // read-only field of the current return value
10 entity CallbackChain_New(string name);
11 bool CallbackChain_Add(entity cb, bool() func, int order);
12 int CallbackChain_Remove(entity cb, bool() func);
13 // a callback function is like this:
14 // bool mycallback(entity me)
19 bool CallbackChain_Call(entity cb);
26 typedef bool(int) mutatorfunc_t;
27 bool Mutator_Add(mutatorfunc_t func, string name);
28 void Mutator_Remove(mutatorfunc_t func, string name); // calls error() on fail
30 #define MUTATOR_ADD(name) Mutator_Add(MUTATOR_##name, #name)
31 #define MUTATOR_REMOVE(name) Mutator_Remove(MUTATOR_##name, #name)
32 #define MUTATOR_DEFINITION(name) bool MUTATOR_##name(int mode)
33 #define MUTATOR_DECLARATION(name) bool MUTATOR_##name(int mode)
34 #define MUTATOR_HOOKFUNCTION(name) bool HOOKFUNCTION_##name()
35 #define MUTATOR_HOOK(cb, func, order) do { \
37 if (!HOOK_##cb) HOOK_##cb = CallbackChain_New(#cb); \
38 if (!CallbackChain_Add(HOOK_##cb, HOOKFUNCTION_##func, order)) { \
39 print("HOOK FAILED: ", #func, "\n"); \
43 MUTATOR_ONROLLBACK_OR_REMOVE { \
44 if (HOOK_##cb) CallbackChain_Remove(HOOK_##cb, HOOKFUNCTION_##func); \
47 #define MUTATOR_ONADD if (mode == MUTATOR_ADDING)
48 #define MUTATOR_ONREMOVE if (mode == MUTATOR_REMOVING)
49 #define MUTATOR_ONROLLBACK_OR_REMOVE if (mode == MUTATOR_REMOVING || mode == MUTATOR_ROLLING_BACK)
51 #define _MUTATOR_HOOKABLE(id, ...) entity HOOK_##id; bool __Mutator_Send_##id(__VA_ARGS__)
52 #define MUTATOR_CALLHOOK(id, ...) APPLY(__Mutator_Send_##id, 0, ##__VA_ARGS__)
54 #define MUTATOR_RETURNVALUE CallbackChain_ReturnValue
56 #define HANDLE_NOP(type, id)
57 #define HANDLE_PARAMS(type, id) , type in_##id
58 #define HANDLE_PREPARE(type, id) id = in_##id;
59 #define HANDLE_PUSHTMP(type, id) type tmp_##id = id;
60 #define HANDLE_PUSHOUT(type, id) type out_##id = id;
61 #define HANDLE_POPTMP(type, id) id = tmp_##id;
62 #define HANDLE_POPOUT(type, id) id = out_##id;
64 #define MUTATOR_HOOKABLE(id, params) \
65 _MUTATOR_HOOKABLE(id, int params(HANDLE_PARAMS, HANDLE_NOP)) { \
66 params(HANDLE_PUSHTMP, HANDLE_NOP) \
67 params(HANDLE_PREPARE, HANDLE_NOP) \
68 bool ret = CallbackChain_Call(HOOK_##id); \
69 params(HANDLE_NOP, HANDLE_PUSHOUT) \
70 params(HANDLE_POPTMP, HANDLE_NOP) \
71 params(HANDLE_NOP, HANDLE_POPOUT) \
77 // register all possible hooks here
78 // some parameters are commented to avoid duplicate declarations
80 #define EV_NO_ARGS(i, o)
82 /** called when a player becomes observer, after shared setup */
83 #define EV_MakePlayerObserver(i, o) \
85 MUTATOR_HOOKABLE(MakePlayerObserver, EV_MakePlayerObserver)
88 #define EV_PutClientInServer(i, o) \
89 /** client wanting to spawn */ i(entity, self) \
91 MUTATOR_HOOKABLE(PutClientInServer, EV_PutClientInServer);
93 /** called when a player spawns as player, after shared setup, before his weapon is chosen (so items may be changed in here) */
94 #define EV_PlayerSpawn(i, o) \
95 /** spot that was used, or world */ i(entity, spawn_spot) \
98 MUTATOR_HOOKABLE(PlayerSpawn, EV_PlayerSpawn);
100 /** called in reset_map */
101 #define EV_reset_map_global(i, o) \
103 MUTATOR_HOOKABLE(reset_map_global, EV_reset_map_global);
105 /** called in reset_map */
106 #define EV_reset_map_players(i, o) \
108 MUTATOR_HOOKABLE(reset_map_players, EV_reset_map_players);
110 /** returns 1 if clearing player score shall not be allowed */
111 #define EV_ForbidPlayerScore_Clear(i, o) \
113 MUTATOR_HOOKABLE(ForbidPlayerScore_Clear, EV_ForbidPlayerScore_Clear);
115 /** called when a player disconnects */
116 #define EV_ClientDisconnect(i, o) \
118 MUTATOR_HOOKABLE(ClientDisconnect, EV_ClientDisconnect);
120 /** called when a player dies to e.g. remove stuff he was carrying. */
121 #define EV_PlayerDies(i, o) \
122 /**/ i(entity, frag_inflictor) \
123 /**/ i(entity, frag_attacker) \
124 /** same as self */ i(entity, frag_target) \
125 /**/ i(int, frag_deathtype) \
127 entity frag_inflictor;
128 entity frag_attacker;
131 MUTATOR_HOOKABLE(PlayerDies, EV_PlayerDies);
133 /** called when a player presses the jump key */
134 #define EV_PlayerJump(i, o) \
135 /**/ i(float, player_multijump) \
136 /**/ i(float, player_jumpheight) \
137 /**/ o(float, player_multijump) \
138 /**/ o(float, player_jumpheight) \
140 float player_multijump;
141 float player_jumpheight;
142 MUTATOR_HOOKABLE(PlayerJump, EV_PlayerJump);
144 /** called when someone was fragged by "self", and is expected to change frag_score to adjust scoring for the kill */
145 #define EV_GiveFragsForKill(i, o) \
146 /** same as self */ i(entity, frag_attacker) \
147 /**/ i(entity, frag_target) \
148 /**/ i(float, frag_score) \
149 /**/ o(float, frag_score) \
152 MUTATOR_HOOKABLE(GiveFragsForKill, EV_GiveFragsForKill);
154 /** called when the match ends */
155 MUTATOR_HOOKABLE(MatchEnd, EV_NO_ARGS);
157 /** should adjust ret_float to contain the team count */
158 #define EV_GetTeamCount(i, o) \
159 /**/ i(float, ret_float) \
160 /**/ o(float, ret_float) \
163 MUTATOR_HOOKABLE(GetTeamCount, EV_GetTeamCount);
165 /** copies variables for spectating "other" to "self" */
166 #define EV_SpectateCopy(i, o) \
167 /**/ i(entity, other) \
168 /**/ i(entity, self) \
170 MUTATOR_HOOKABLE(SpectateCopy, EV_SpectateCopy);
172 /** returns 1 if throwing the current weapon shall not be allowed */
173 MUTATOR_HOOKABLE(ForbidThrowCurrentWeapon, EV_NO_ARGS);
175 /** allows changing attack rate */
176 #define EV_WeaponRateFactor(i, o) \
177 /**/ i(float, weapon_rate) \
178 /**/ o(float, weapon_rate) \
181 MUTATOR_HOOKABLE(WeaponRateFactor, EV_WeaponRateFactor);
183 /** allows changing weapon speed (projectiles mostly) */
184 #define EV_WeaponSpeedFactor(i, o) \
185 /**/ i(float, ret_float) \
186 /**/ o(float, ret_float) \
188 MUTATOR_HOOKABLE(WeaponSpeedFactor, EV_WeaponSpeedFactor);
190 /** adjusts {warmup_}start_{items,weapons,ammo_{cells,plasma,rockets,nails,shells,fuel}} */
191 MUTATOR_HOOKABLE(SetStartItems, EV_NO_ARGS);
193 /** appends ":mutatorname" to ret_string for logging */
194 #define EV_BuildMutatorsString(i, o) \
195 /**/ i(string, ret_string) \
196 /**/ o(string, ret_string) \
199 MUTATOR_HOOKABLE(BuildMutatorsString, EV_BuildMutatorsString);
201 /** appends ", Mutator name" to ret_string for display */
202 #define EV_BuildMutatorsPrettyString(i, o) \
203 /**/ i(string, ret_string) \
204 /**/ o(string, ret_string) \
206 MUTATOR_HOOKABLE(BuildMutatorsPrettyString, EV_BuildMutatorsPrettyString);
208 /** called every frame. customizes the waypoint for spectators */
209 #define EV_CustomizeWaypoint(i, o) \
210 /** waypoint */ i(entity, self) \
211 /** player; other.enemy = spectator */ i(entity, other) \
213 MUTATOR_HOOKABLE(CustomizeWaypoint, EV_CustomizeWaypoint);
216 * checks if the current item may be spawned (self.items and self.weapons may be read and written to, as well as the ammo_ fields)
217 * return error to request removal
219 MUTATOR_HOOKABLE(FilterItem, EV_NO_ARGS);
221 /** return error to request removal */
222 #define EV_TurretSpawn(i, o) \
223 /** turret */ i(entity, self) \
225 MUTATOR_HOOKABLE(TurretSpawn, EV_TurretSpawn);
227 /** return error to prevent entity spawn, or modify the entity */
228 MUTATOR_HOOKABLE(OnEntityPreSpawn, EV_NO_ARGS);
230 /** runs in the event loop for players; is called for ALL player entities, also bots, also the dead, or spectators */
231 MUTATOR_HOOKABLE(PlayerPreThink, EV_NO_ARGS);
233 /** TODO change this into a general PlayerPostThink hook? */
234 MUTATOR_HOOKABLE(GetPressedKeys, EV_NO_ARGS);
237 * called before any player physics, may adjust variables for movement,
238 * is run AFTER bot code and idle checking
240 MUTATOR_HOOKABLE(PlayerPhysics, EV_NO_ARGS);
242 /** is meant to call GetCvars_handle*(get_cvars_s, get_cvars_f, cvarfield, "cvarname") for cvars this mutator needs from the client */
243 #define EV_GetCvars(i, o) \
244 /**/ i(float, get_cvars_f) \
245 /**/ i(string, get_cvars_s) \
249 MUTATOR_HOOKABLE(GetCvars, EV_NO_ARGS); // NOTE: Can't use EV_GetCvars because of `SZ_GetSpace: overflow`
251 /** can edit any "just fired" projectile */
252 #define EV_EditProjectile(i, o) \
253 /**/ i(entity, self) \
254 /**/ i(entity, other) \
256 MUTATOR_HOOKABLE(EditProjectile, EV_EditProjectile);
258 /** called when a monster spawns */
259 MUTATOR_HOOKABLE(MonsterSpawn, EV_NO_ARGS);
261 /** called when a monster dies */
262 #define EV_MonsterDies(i, o) \
263 /**/ i(entity, frag_attacker) \
265 MUTATOR_HOOKABLE(MonsterDies, EV_MonsterDies);
267 /** called when a monster wants to respawn */
268 #define EV_MonsterRespawn(i, o) \
269 /**/ i(entity, other) \
271 MUTATOR_HOOKABLE(MonsterRespawn, EV_MonsterRespawn);
273 /** called when a monster is dropping loot */
274 #define EV_MonsterDropItem(i, o) \
275 /**/ i(entity, other) \
276 /**/ o(entity, other) \
278 .void() monster_loot;
279 MUTATOR_HOOKABLE(MonsterDropItem, EV_MonsterDropItem);
282 * called when a monster moves
283 * returning true makes the monster stop
285 #define EV_MonsterMove(i, o) \
286 /**/ i(float, monster_speed_run) \
287 /**/ o(float, monster_speed_run) \
288 /**/ i(float, monster_speed_walk) \
289 /**/ o(float, monster_speed_walk) \
290 /**/ i(entity, monster_target) \
292 float monster_speed_run;
293 float monster_speed_walk;
294 entity monster_target;
295 MUTATOR_HOOKABLE(MonsterMove, EV_MonsterMove);
297 /** called when a monster looks for another target */
298 MUTATOR_HOOKABLE(MonsterFindTarget, EV_NO_ARGS);
300 /** called to change a random monster to a miniboss */
301 MUTATOR_HOOKABLE(MonsterCheckBossFlag, EV_NO_ARGS);
304 * called when a player tries to spawn a monster
305 * return 1 to prevent spawning
307 MUTATOR_HOOKABLE(AllowMobSpawning, EV_NO_ARGS);
309 /** called when a player gets damaged to e.g. remove stuff he was carrying. */
310 #define EV_PlayerDamage_SplitHealthArmor(i, o) \
311 /**/ i(entity, frag_inflictor) \
312 /**/ i(entity, frag_attacker) \
313 /** same as self */ i(entity, frag_target) \
314 /** NOTE: this force already HAS been applied */ i(vector, damage_force) \
315 /**/ i(float, damage_take) \
316 /**/ o(float, damage_take) \
317 /**/ i(float, damage_save) \
318 /**/ o(float, damage_save) \
323 MUTATOR_HOOKABLE(PlayerDamage_SplitHealthArmor, EV_PlayerDamage_SplitHealthArmor);
326 * called to adjust damage and force values which are applied to the player, used for e.g. strength damage/force multiplier
327 * i'm not sure if I should change this around slightly (Naming of the entities, and also how they're done in g_damage).
329 #define EV_PlayerDamage_Calculate(i, o) \
330 /**/ i(entity, frag_attacker) \
331 /**/ i(entity, frag_target) \
332 /**/ i(float, frag_deathtype) \
333 /**/ i(float, frag_damage) \
334 /**/ o(float, frag_damage) \
335 /**/ i(float, frag_mirrordamage) \
336 /**/ o(float, frag_mirrordamage) \
337 /**/ i(vector, frag_force) \
338 /**/ o(vector, frag_force) \
341 float frag_mirrordamage;
343 MUTATOR_HOOKABLE(PlayerDamage_Calculate, EV_PlayerDamage_Calculate);
345 /** called at the end of player_powerups() in cl_client.qc, used for manipulating the values which are set by powerup items. */
346 #define EV_PlayerPowerups(i, o) \
347 /**/ i(entity, self) \
348 /**/ i(int, olditems) \
351 MUTATOR_HOOKABLE(PlayerPowerups, EV_PlayerPowerups);
354 * called every player think frame
355 * return 1 to disable regen
357 #define EV_PlayerRegen(i, o) \
358 /**/ i(float, regen_mod_max) \
359 /**/ o(float, regen_mod_max) \
360 /**/ i(float, regen_mod_regen) \
361 /**/ o(float, regen_mod_regen) \
362 /**/ i(float, regen_mod_rot) \
363 /**/ o(float, regen_mod_rot) \
364 /**/ i(float, regen_mod_limit) \
365 /**/ o(float, regen_mod_limit) \
368 float regen_mod_regen;
370 float regen_mod_limit;
371 MUTATOR_HOOKABLE(PlayerRegen, EV_PlayerRegen);
374 * called when the use key is pressed
375 * if MUTATOR_RETURNVALUE is 1, don't do anything
376 * return 1 if the use key actually did something
378 MUTATOR_HOOKABLE(PlayerUseKey, EV_NO_ARGS);
381 * called when a client command is parsed
382 * NOTE: hooks MUST start with if(MUTATOR_RETURNVALUE) return 0;
383 * NOTE: return 1 if you handled the command, return 0 to continue handling
384 * NOTE: THESE HOOKS MUST NEVER EVER CALL tokenize()
386 * MUTATOR_HOOKFUNCTION(foo_SV_ParseClientCommand)
388 * if (MUTATOR_RETURNVALUE) // command was already handled?
390 * if (cmd_name == "echocvar" && cmd_argc >= 2)
392 * print(cvar_string(argv(1)), "\n");
395 * if (cmd_name == "echostring" && cmd_argc >= 2)
397 * print(substring(cmd_string, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), "\n");
403 #define EV_SV_ParseClientCommand(i, o) \
404 /** command name */ i(string, cmd_name) \
405 /** also, argv() can be used */ i(int, cmd_argc) \
406 /** whole command, use only if you really have to */ i(string, cmd_string) \
411 MUTATOR_HOOKABLE(SV_ParseClientCommand, EV_SV_ParseClientCommand);
414 * called when a spawnpoint is being evaluated
415 * return 1 to make the spawnpoint unusable
417 #define EV_Spawn_Score(i, o) \
418 /** player wanting to spawn */ i(entity, self) \
419 /** spot to be evaluated */ i(entity, spawn_spot) \
420 /** _x is priority, _y is "distance" */ i(vector, spawn_score) \
421 /**/ o(vector, spawn_score) \
424 MUTATOR_HOOKABLE(Spawn_Score, EV_Spawn_Score);
426 /** runs globally each server frame */
427 MUTATOR_HOOKABLE(SV_StartFrame, EV_NO_ARGS);
429 #define EV_SetModname(i, o) \
430 /** name of the mutator/mod if it warrants showing as such in the server browser */ \
433 MUTATOR_HOOKABLE(SetModname, EV_SetModname);
436 * called for each item being spawned on a map, including dropped weapons
437 * return 1 to remove an item
439 #define EV_Item_Spawn(i, o) \
440 /** the item */ i(entity, self) \
442 MUTATOR_HOOKABLE(Item_Spawn, EV_Item_Spawn);
444 #define EV_SetWeaponreplace(i, o) \
445 /** map entity */ i(entity, self) \
446 /** weapon info */ i(entity, other) \
447 /**/ i(string, ret_string) \
448 /**/ o(string, ret_string) \
450 MUTATOR_HOOKABLE(SetWeaponreplace, EV_SetWeaponreplace);
452 /** called when an item is about to respawn */
453 #define EV_Item_RespawnCountdown(i, o) \
454 /**/ i(string, item_name) \
455 /**/ o(string, item_name) \
456 /**/ i(vector, item_color) \
457 /**/ o(vector, item_color) \
461 MUTATOR_HOOKABLE(Item_RespawnCountdown, EV_Item_RespawnCountdown);
463 /** called when a bot checks a target to attack */
464 #define EV_BotShouldAttack(i, o) \
465 /**/ i(entity, checkentity) \
468 MUTATOR_HOOKABLE(BotShouldAttack, EV_BotShouldAttack);
471 * called whenever a player goes through a portal gun teleport
472 * allows you to strip a player of an item if they go through the teleporter to help prevent cheating
474 #define EV_PortalTeleport(i, o) \
475 /**/ i(entity, self) \
477 MUTATOR_HOOKABLE(PortalTeleport, EV_PortalTeleport);
480 * called whenever a player uses impulse 33 (help me) in cl_impulse.qc
481 * normally help me ping uses self.waypointsprite_attachedforcarrier,
482 * but if your mutator uses something different then you can handle it
483 * in a special manner using this hook
485 #define EV_HelpMePing(i, o) \
486 /** the player who pressed impulse 33 */ i(entity, self) \
488 MUTATOR_HOOKABLE(HelpMePing, EV_HelpMePing);
491 * called when a vehicle initializes
492 * return true to remove the vehicle
494 MUTATOR_HOOKABLE(VehicleSpawn, EV_NO_ARGS);
497 * called when a player enters a vehicle
498 * allows mutators to set special settings in this event
500 #define EV_VehicleEnter(i, o) \
501 /** player */ i(entity, vh_player) \
502 /** vehicle */ i(entity, vh_vehicle) \
506 MUTATOR_HOOKABLE(VehicleEnter, EV_VehicleEnter);
509 * called when a player touches a vehicle
510 * return true to stop player from entering the vehicle
512 #define EV_VehicleTouch(i, o) \
513 /** vehicle */ i(entity, self) \
514 /** player */ i(entity, other) \
516 MUTATOR_HOOKABLE(VehicleTouch, EV_VehicleTouch);
519 * called when a player exits a vehicle
520 * allows mutators to set special settings in this event
522 #define EV_VehicleExit(i, o) \
523 /** player */ i(entity, vh_player) \
524 /** vehicle */ i(entity, vh_vehicle) \
526 MUTATOR_HOOKABLE(VehicleExit, EV_VehicleExit);
528 /** called when a speedrun is aborted and the player is teleported back to start position */
529 #define EV_AbortSpeedrun(i, o) \
530 /** player */ i(entity, self) \
532 MUTATOR_HOOKABLE(AbortSpeedrun, EV_AbortSpeedrun);
534 /** called at when a item is touched. Called early, can edit item properties. */
535 #define EV_ItemTouch(i, o) \
536 /** item */ i(entity, self) \
537 /** player */ i(entity, other) \
539 MUTATOR_HOOKABLE(ItemTouch, EV_ItemTouch);
542 MUT_ITEMTOUCH_CONTINUE, // return this flag to make the function continue as normal
543 MUT_ITEMTOUCH_RETURN, // return this flag to make the function return (handled entirely by mutator)
544 MUT_ITEMTOUCH_PICKUP // return this flag to have the item "picked up" and taken even after mutator handled it
547 /** called at when a player connect */
548 #define EV_ClientConnect(i, o) \
549 /** player */ i(entity, self) \
551 MUTATOR_HOOKABLE(ClientConnect, EV_ClientConnect);
553 #define EV_HavocBot_ChooseRole(i, o) \
554 /**/ i(entity, self) \
556 MUTATOR_HOOKABLE(HavocBot_ChooseRole, EV_HavocBot_ChooseRole);
558 /** called when a target is checked for accuracy */
559 #define EV_AccuracyTargetValid(i, o) \
560 /** attacker */ i(entity, frag_attacker) \
561 /** target */ i(entity, frag_target) \
563 MUTATOR_HOOKABLE(AccuracyTargetValid, EV_AccuracyTargetValid);
565 MUT_ACCADD_VALID, // return this flag to make the function continue if target is a client
566 MUT_ACCADD_INVALID, // return this flag to make the function always continue
567 MUT_ACCADD_INDIFFERENT // return this flag to make the function always return