From 4cc16d988faa7f5f998c24db0afcf1d6a917ba1c Mon Sep 17 00:00:00 2001 From: Mario Date: Sun, 23 Aug 2015 20:54:14 +1000 Subject: [PATCH] Clean up chat replacement handling, add a mutator hook for extra fun --- qcsrc/server/miscfunctions.qc | 67 ++++++++++++++------------------- qcsrc/server/mutators/events.qh | 10 +++++ 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/qcsrc/server/miscfunctions.qc b/qcsrc/server/miscfunctions.qc index 81e0a41df..31ebc2f85 100644 --- a/qcsrc/server/miscfunctions.qc +++ b/qcsrc/server/miscfunctions.qc @@ -249,9 +249,16 @@ string formatmessage(string msg) entity cursor_ent; string escape; string replacement; + string ammoitems; p = 0; n = 7; + ammoitems = "batteries"; + if(self.items & IT_PLASMA) ammoitems = "plasma"; + if(self.items & IT_CELLS) ammoitems = "cells"; + if(self.items & IT_ROCKETS) ammoitems = "rockets"; + if(self.items & IT_SHELLS) ammoitems = "shells"; + WarpZone_crosshair_trace(self); cursor = trace_endpos; cursor_ent = trace_ent; @@ -278,45 +285,27 @@ string formatmessage(string msg) replacement = substring(msg, p, 2); escape = substring(msg, p + 1, 1); - if (escape == "%") - replacement = "%"; - else if (escape == "\\") - replacement = "\\"; - else if (escape == "n") - replacement = "\n"; - else if (escape == "a") - replacement = ftos(floor(self.armorvalue)); - else if (escape == "h") - replacement = ftos(floor(self.health)); - else if (escape == "l") - replacement = NearestLocation(self.origin); - else if (escape == "y") - replacement = NearestLocation(cursor); - else if (escape == "d") - replacement = NearestLocation(self.death_origin); - else if (escape == "w") { - float wep; - wep = self.weapon; - if (!wep) - wep = self.switchweapon; - if (!wep) - wep = self.cnt; - replacement = WEP_NAME(wep); - } else if (escape == "W") { - if (self.items & ITEM_Shells.m_itemid) replacement = "shells"; - else if (self.items & ITEM_Bullets.m_itemid) replacement = "bullets"; - else if (self.items & ITEM_Rockets.m_itemid) replacement = "rockets"; - else if (self.items & ITEM_Cells.m_itemid) replacement = "cells"; - else if (self.items & ITEM_Plasma.m_itemid) replacement = "plasma"; - else replacement = "batteries"; // ;) - } else if (escape == "x") { - replacement = cursor_ent.netname; - if (replacement == "" || !cursor_ent) - replacement = "nothing"; - } else if (escape == "s") - replacement = ftos(vlen(self.velocity - self.velocity.z * '0 0 1')); - else if (escape == "S") - replacement = ftos(vlen(self.velocity)); + switch(escape) + { + case "%": replacement = "%"; break; + case "\\":replacement = "\\"; break; + case "n": replacement = "\n"; break; + case "a": replacement = ftos(floor(self.armorvalue)); break; + case "h": replacement = ftos(floor(self.health)); break; + case "l": replacement = NearestLocation(self.origin); break; + case "y": replacement = NearestLocation(cursor); break; + case "d": replacement = NearestLocation(self.death_origin); break; + case "w": replacement = WEP_NAME((!self.weapon) ? (!self.switchweapon ? self.cnt : self.switchweapon) : self.weapon); break; + case "W": replacement = ammoitems; break; + case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break; + case "s": replacement = ftos(vlen(self.velocity - self.velocity_z * '0 0 1')); break; + case "S": replacement = ftos(vlen(self.velocity)); break; + default: + { + MUTATOR_CALLHOOK(FormatMessage, escape, replacement); + break; + } + } msg = strcat(substring(msg, 0, p), replacement, substring(msg, p+2, strlen(msg) - (p+2))); p = p + strlen(replacement); diff --git a/qcsrc/server/mutators/events.qh b/qcsrc/server/mutators/events.qh index ec9e9c99a..01cdd168f 100644 --- a/qcsrc/server/mutators/events.qh +++ b/qcsrc/server/mutators/events.qh @@ -102,6 +102,16 @@ MUTATOR_HOOKABLE(GetTeamCount, EV_GetTeamCount); /**/ MUTATOR_HOOKABLE(SpectateCopy, EV_SpectateCopy); +/** called when formatting a chat message to replace fancy functions */ +#define EV_FormatMessage(i, o) \ + /**/ i(string, format_escape) \ + /**/ i(string, format_replacement) \ + /**/ o(string, format_replacement) \ + /**/ +string format_escape; +string format_replacement; +MUTATOR_HOOKABLE(FormatMessage, EV_FormatMessage); + /** returns 1 if throwing the current weapon shall not be allowed */ MUTATOR_HOOKABLE(ForbidThrowCurrentWeapon, EV_NO_ARGS); -- 2.39.2