]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Clean up chat replacement handling, add a mutator hook for extra fun
authorMario <zacjardine@y7mail.com>
Sun, 23 Aug 2015 10:54:14 +0000 (20:54 +1000)
committerMario <zacjardine@y7mail.com>
Sun, 23 Aug 2015 10:54:14 +0000 (20:54 +1000)
qcsrc/server/miscfunctions.qc
qcsrc/server/mutators/events.qh

index 81e0a41df0d9bb3aac224215187103219621013d..31ebc2f856d7845ca97bd9813c2b2f89b4103ca0 100644 (file)
@@ -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);
index ec9e9c99a90dc5dd52c9f1fae1b2e976259f583f..01cdd168f797e454d6af7398b470627cc46adda1 100644 (file)
@@ -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);