Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file ..src/fpsgame/client.cpp
- #include "game.h"
- namespace game
- {
- /*
- * some code here, just skiped to our added code. ^.^
- *
- */
- VARP(highlightingtype, 0, 0, 1); // to control the type of highlighting: 0 = by name, 1 = by words
- VARP(hlwordminsize, 1, 3, 20); // min size of the words to compare
- VARP(hlwordmaxsize, 1, 8, 20); // max size of the words to compare
- SVARP(highlightwords, ""); // words to find in the highlighting by words.
- SVARP(highlightseparator, ""); // separators added by the user (default: spaces)
- // aux funtion to search one substring in a string (no case compare)
- const char *str_find_nocase(const char *haystack, const char *needle)
- {
- while(*haystack) /* native implementation */
- {
- const char *a = haystack;
- const char *b = needle;
- while(*a && *b && tolower(*a) == tolower(*b))
- {
- a++;
- b++;
- }
- if(!(*b))
- return haystack;
- haystack++;
- }
- return NULL;
- }
- static int highlighting_ByName(const char *ptext)
- {
- int found = 0, len;
- char textchat[MAXTRANS], *tword = NULL/*, defdelim[] = " "*/;
- string delim;
- copystring(delim, " "); // set [spaces] as default separator.
- // add the separators chosen by the user (if exist).
- if(highlightseparator[0]) concatstring(delim, highlightseparator);
- // make a copy of the text of chat.
- copystring(textchat, ptext, strlen(ptext)+1);
- tword = strtok(textchat, delim); // get the first word
- while(tword && !found)
- {
- len = (int)strlen(tword);
- // compare the word with our name.
- if(strcasecmp(player1->name, tword)==0) found = 1;
- // no match, then if the word is less or more than the size selected: find substring (word) in our name
- else if(len>=hlwordminsize && len<=hlwordmaxsize)
- {
- if(str_find_nocase(player1->name, tword)) found = 1;
- }
- tword = strtok(NULL, delim); // get the next word.
- }
- if(found) playsound(S_CHATALERT);
- return found;
- }
- static int highlighting_ByWords(const char *texto)
- {
- if(!highlightwords[0]) return 0; // if there is no words added by the user, do nothing
- int found = 0;
- char hlwords[512];
- char *hltoken = NULL, delim[] = " ";
- // copy the words to find in aux var: hlwords
- copystring(hlwords, highlightwords); // maybe this is not necesary, >.<'
- hltoken = strtok(hlwords, delim); // get hte first word and..
- // serch the word in all the text chat.
- while (hltoken != NULL && !found)
- {
- if (str_find_nocase(texto, hltoken))
- {
- found = 1;
- playsound(S_CHATALERT);
- }
- hltoken = strtok(NULL, delim); // get the next word.
- }
- return found;
- }
- /*
- * find the player name with the substring cadena (string) in the name of payer.
- * don't forget add the prototype of this function in ..src/shared/igame.h
- */
- const char* findPlayer(int &index, const char *cadena)
- {
- const char *pname = NULL;
- if(!cadena[0] && index<0) // There is no string (cadena) to find
- {
- fpsent *d = players[0]; // start with the first player in the list.
- index = 0;
- pname = colorname(d);
- }
- // There is string to find, search in the player list since the last point checked.
- else
- {
- const char *found = NULL;
- loopv(players)
- {
- fpsent *d = players[i];
- // set the point to search = last point checked
- if(i<=index) continue;
- // then, compare the string with the player name.
- else found = str_find_nocase(d->name, cadena);
- if(found) {
- pname = colorname(d);
- index = i; // set the last point checked.
- break;
- }
- }
- if(!found) index = -1;
- }
- return pname;
- }
- void parsemessages(int cn, fpsent *d, ucharbuf &p)
- {
- static char text[MAXTRANS];
- int type;
- bool mapchanged = false, demopacket = false;
- while(p.remaining()) switch(type = getint(p))
- {
- /*
- * some cases before our code, ^.^
- *
- */
- case N_TEXT:
- {
- if(!d) return;
- int hlfound; // added: to get the result of the highlighting search.
- getstring(text, p);
- filtertext(text, text);
- if(isignored(d->clientnum)) break;
- if(d->state!=CS_DEAD && d->state!=CS_SPECTATOR)
- particle_textcopy(d->abovehead(), text, PART_TEXT, 2000, 0x32FF64, 4.0f, -8);
- // added: get the result of the hifglighting: based on our name or based on words.
- hlfound = highlightingtype ? highlighting_ByWords(text) : highlighting_ByName(text);
- conoutf(CON_CHAT, "%s:%s %s", colorname(d), hlfound ? "\f3" : "\f0", text); // text red = match, then is green.
- break;
- }
- case N_SAYTEAM:
- {
- int hlfound; // added: to get the result of the highlighting search.
- int tcn = getint(p);
- fpsent *t = getclient(tcn);
- getstring(text, p);
- filtertext(text, text);
- if(!t || isignored(t->clientnum)) break;
- if(t->state!=CS_DEAD && t->state!=CS_SPECTATOR)
- particle_textcopy(t->abovehead(), text, PART_TEXT, 2000, 0x6496FF, 4.0f, -8);
- // same as above (N_TEXT) but in team chat.
- hlfound = highlightingtype ? highlighting_ByWords(text) : highlighting_ByName(text);
- conoutf(CON_TEAMCHAT, "%s:%s %s", colorname(t), hlfound ? "\f3" : "\f1", text); // text red = match, then is blue.
- break;
- }
- /*
- *
- * A lot of cases after this, ^.^
- *
- */
- default:
- neterr("type", cn < 0);
- return;
- }
- }
- /*
- * more functions here, ^.^
- *
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement