zp50_colorchat.inc
Original include source with line numbers.
| 1 | #if defined _zp50_colorchat_included |
| 2 | #endinput |
| 3 | #endif |
| 4 | #define _zp50_colorchat_included |
| 5 | |
| 6 | #include <zp50_colorchat_const> |
| 7 | |
| 8 | // -------------- |
| 9 | // Example usage: |
| 10 | // -------------- |
| 11 | // zp_colored_print(id, "Class: %L", id, "CLASS_HUMAN") |
| 12 | // zp_colored_print(0, "Class: %L", LANG_PLAYER, "CLASS_HUMAN") |
| 13 | // ---------------------------------------------------------------------------- |
| 14 | |
| 15 | // Prints a colored message to target (use 0 for everyone), supports ML formatting. |
| 16 | // - Supports passing -1 as an argument (ZP 4.3 stock had a bug with that) |
| 17 | // - Does not support blue/red/gray colors |
| 18 | stock zp_colored_print(target, const message[], any:...) |
| 19 | { |
| 20 | static buffer[512], msg_SayText = 0 |
| 21 | if( !msg_SayText ) msg_SayText = get_user_msgid("SayText") |
| 22 | |
| 23 | // Send to everyone |
| 24 | if (!target) |
| 25 | { |
| 26 | static player, maxplayers, argscount |
| 27 | maxplayers = get_maxplayers() |
| 28 | argscount = numargs() |
| 29 | |
| 30 | for (player = 1; player <= maxplayers; player++) |
| 31 | { |
| 32 | // Not connected |
| 33 | if (!is_user_connected(player)) |
| 34 | continue; |
| 35 | |
| 36 | // Remember changed arguments |
| 37 | static arg_index, changed_args[20], changedcount // [20] = max LANG_PLAYER occurencies |
| 38 | changedcount = 0 |
| 39 | |
| 40 | // Replace LANG_PLAYER with player id |
| 41 | for (arg_index = 2; arg_index < argscount; arg_index++) |
| 42 | { |
| 43 | if (getarg(arg_index) == LANG_PLAYER && arg_index + 1 < argscount) |
| 44 | { |
| 45 | // Check if next param string is a registered language translation |
| 46 | static lang_key[64], arg_subindex |
| 47 | arg_subindex = 0 |
| 48 | while ((lang_key[arg_subindex] = getarg(arg_index + 1, arg_subindex++))) { /* keep looping */ } |
| 49 | if (GetLangTransKey(lang_key) != TransKey_Bad) |
| 50 | { |
| 51 | setarg(arg_index, 0, player) |
| 52 | changed_args[changedcount++] = arg_index |
| 53 | arg_index++ // skip next argument since we know it's a translation key |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Format message for player (+add ZP prefix) |
| 59 | vformat(buffer, charsmax(buffer), message, 3) |
| 60 | format(buffer, charsmax(buffer), "%s%s", ZP_PREFIX, buffer) |
| 61 | |
| 62 | // Send it |
| 63 | message_begin(MSG_ONE_UNRELIABLE, msg_SayText, _, player) |
| 64 | write_byte(player) |
| 65 | write_string(buffer) |
| 66 | message_end() |
| 67 | |
| 68 | // Replace back player id's with LANG_PLAYER |
| 69 | for (arg_index = 0; arg_index < changedcount; arg_index++) |
| 70 | setarg(changed_args[arg_index], 0, LANG_PLAYER) |
| 71 | } |
| 72 | } |
| 73 | // Send to specific target |
| 74 | else |
| 75 | { |
| 76 | // Format message for player (+add ZP prefix) |
| 77 | vformat(buffer, charsmax(buffer), message, 3) |
| 78 | format(buffer, charsmax(buffer), "%s%s", ZP_PREFIX, buffer) |
| 79 | |
| 80 | // Send it |
| 81 | message_begin(MSG_ONE, msg_SayText, _, target) |
| 82 | write_byte(target) |
| 83 | write_string(buffer) |
| 84 | message_end() |
| 85 | } |
| 86 | } |