From a8d2a64ef912a3296d4899ab3177db19a7cfbcb5 Mon Sep 17 00:00:00 2001 From: Marco Hladik Date: Mon, 9 Sep 2019 18:58:40 +0200 Subject: [PATCH] plugins/chatsounds: make it more readable for 80 columns --- src/plugins/chatsounds.c | 137 ++++++++++++++++++++----------------- src/plugins/chatsounds.src | 2 +- 2 files changed, 77 insertions(+), 62 deletions(-) diff --git a/src/plugins/chatsounds.c b/src/plugins/chatsounds.c index 01ec0a29..5c9aef79 100755 --- a/src/plugins/chatsounds.c +++ b/src/plugins/chatsounds.c @@ -15,34 +15,99 @@ PERFORMANCE OF THIS SOFTWARE. var int autocvar_chatplug_suppresschat = 0; -entity csndemitter; +/* our sound boogeyman */ +static entity emitter; +static int g_sounds; +/* where we save our lists */ typedef struct { string sentence; string sample; } chatsentence_t; -chatsentence_t *g_chatsounds; -var int g_chatsounds_count; +static chatsentence_t *g_table; -int ChatLookUp(string cmd) +void +ChatPlay(int id) { - for (int i = 0; i < g_chatsounds_count; i++) { - int sc = tokenizebyseparator(g_chatsounds[i].sentence, ";"); + int r, count, flags; + count = tokenizebyseparator(g_table[id].sample, ";"); + r = random(0, count); + flags = SOUNDFLAG_NOREVERB; /* this should be enough for now */ + sound(emitter, CHAN_BODY, argv(r), 1.0f, ATTN_NONE, 100, flags); +} + +int +ChatLookUp(string cmd) +{ + int i; + for (i = 0; i < g_sounds; i++) { + int sc = tokenizebyseparator(g_table[i].sentence, ";"); for (int c = 0; c < sc; c++) { if (cmd == argv(c)) { - int count = tokenizebyseparator(g_chatsounds[i].sample, ";"); - int r = random(0, count); - sound(csndemitter, CHAN_BODY, argv(r), 1.0f, ATTN_NONE, 100, SOUNDFLAG_NOREVERB); + ChatPlay(i); break; } } } - /* Some jokester might set it to non 0/1 */ + /* some joker might set it to non 0/1 */ return autocvar_chatplug_suppresschat == 1 ? TRUE : FALSE; } -int FMX_ParseClientCommand(string cmd) +void +initents(void) +{ + /* why waste entity slots? */ + if (g_sounds > 0) { + emitter = spawn(); + } +} + +void +init(float prevprogs) +{ + string temp; + int i = 0; + int c = 0; + filestream chatfile; + chatfile = fopen("chatsounds.txt", FILE_READ); + + if (chatfile < 0) { + print("Chat Sound Plugin: chatsounds.txt not found.\n"); + return; + } + + /* count lines */ + while ((temp = fgets(chatfile))) { + c = tokenize_console(temp); + if (c != 2) { + continue; + } + g_sounds++; + } + fseek(chatfile, 0); + + g_table = memalloc(sizeof(chatsentence_t) * g_sounds); + while ((temp = fgets(chatfile))) { + c = tokenize_console(temp); + if (c != 2 ) { + continue; + } + g_table[i].sentence = strtolower(argv(0)); + g_table[i].sample = strtolower(argv(1)); + c = tokenizebyseparator(g_table[i].sample, ";"); + for (int x = 0; x < c; x++) { + precache_sound(argv(x)); + print(sprintf("Caching: %s\n", argv(x))); + } + i++; + } + fclose(chatfile); +} + +/* plugin hook */ +int +FMX_ParseClientCommand(string cmd) { tokenize(cmd); switch (argv(0)) { @@ -54,53 +119,3 @@ int FMX_ParseClientCommand(string cmd) } return TRUE; } - -void initents(void) -{ - /* Why waste entity slots? */ - if (g_chatsounds_count > 0) { - csndemitter = spawn(); - } -} - -void init(float prevprogs) -{ - filestream chatfile; - chatfile = fopen("chatsounds.txt", FILE_READ); - - if (chatfile >= 0) { - string temp; - int i = 0; - int c = 0; - - /* count lines */ - while ((temp = fgets(chatfile))) { - c = tokenize_console(temp); - if (c != 2) { - continue; - } - g_chatsounds_count++; - } - fseek(chatfile, 0); - print(sprintf("Chat Sound Plugin: %i record(s) initialized\n", g_chatsounds_count)); - - g_chatsounds = memalloc(sizeof(chatsentence_t) * g_chatsounds_count); - while ((temp = fgets(chatfile))) { - c = tokenize_console(temp); - if (c != 2 ) { - continue; - } - g_chatsounds[i].sentence = strtolower(argv(0)); - g_chatsounds[i].sample = strtolower(argv(1)); - c = tokenizebyseparator(g_chatsounds[i].sample, ";"); - for (int x = 0; x < c; x++) { - precache_sound(argv(x)); - print(sprintf("Caching: %s\n", argv(x))); - } - i++; - } - fclose(chatfile); - } else { - print("Chat Sound Plugin: chatsounds.txt not found.\n"); - } -} diff --git a/src/plugins/chatsounds.src b/src/plugins/chatsounds.src index fe5efd41..5492ac2f 100755 --- a/src/plugins/chatsounds.src +++ b/src/plugins/chatsounds.src @@ -3,6 +3,6 @@ #define QWSSQC #includelist -../builtins.h +../shared/fteextensions.qc chatsounds.c #endlist