Advertisement
Guest User

highlighting

a guest
Aug 19th, 2014
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.75 KB | None | 0 0
  1. // file ..src/fpsgame/client.cpp
  2.  
  3. #include "game.h"
  4.  
  5. namespace game
  6. {
  7.     /*
  8.      * some code here, just skiped to our added code. ^.^
  9.      *
  10.      */
  11.     VARP(highlightingtype, 0, 0, 1);    // to control the type of highlighting: 0 = by name, 1 = by words
  12.     VARP(hlwordminsize, 1, 3, 20);  // min size of the words to compare
  13.     VARP(hlwordmaxsize, 1, 8, 20);  // max size of the words to compare
  14.     SVARP(highlightwords, "");      // words to find in the highlighting by words.
  15.     SVARP(highlightseparator, "");  // separators added by the user (default: spaces)
  16.  
  17.     // aux funtion to search one substring in a string (no case compare)
  18.     const char *str_find_nocase(const char *haystack, const char *needle)
  19.     {
  20.         while(*haystack) /* native implementation */
  21.         {
  22.             const char *a = haystack;
  23.             const char *b = needle;
  24.             while(*a && *b && tolower(*a) == tolower(*b))
  25.             {
  26.                 a++;
  27.                 b++;
  28.             }
  29.             if(!(*b))
  30.                 return haystack;
  31.             haystack++;
  32.         }
  33.  
  34.         return NULL;
  35.     }
  36.    
  37.     static int highlighting_ByName(const char *ptext)
  38.     {
  39.         int found = 0, len;
  40.         char textchat[MAXTRANS], *tword = NULL/*, defdelim[] = " "*/;
  41.         string delim;
  42.  
  43.         copystring(delim, " "); // set [spaces] as default separator.
  44.  
  45.         // add the separators chosen by the user (if exist).
  46.         if(highlightseparator[0]) concatstring(delim, highlightseparator);
  47.  
  48.         // make a copy of the text of chat.
  49.         copystring(textchat, ptext, strlen(ptext)+1);
  50.  
  51.         tword = strtok(textchat, delim); // get the first word
  52.  
  53.         while(tword && !found)
  54.         {
  55.             len = (int)strlen(tword);
  56.            
  57.             // compare the word with our name.
  58.             if(strcasecmp(player1->name, tword)==0) found = 1;
  59.            
  60.             // no match, then if the word is less or more than the size selected: find substring (word) in our name
  61.             else if(len>=hlwordminsize && len<=hlwordmaxsize)
  62.             {
  63.                 if(str_find_nocase(player1->name, tword)) found = 1;
  64.             }
  65.  
  66.             tword = strtok(NULL, delim); // get the next word.
  67.         }
  68.  
  69.         if(found) playsound(S_CHATALERT);
  70.  
  71.         return found;
  72.     }
  73.  
  74.     static int highlighting_ByWords(const char *texto)
  75.     {
  76.         if(!highlightwords[0]) return 0; // if there is no words added by the user, do nothing
  77.  
  78.         int found = 0;
  79.         char hlwords[512];
  80.         char *hltoken = NULL, delim[] = " ";
  81.  
  82.         // copy the words to find in aux var: hlwords
  83.         copystring(hlwords, highlightwords);    // maybe this is not necesary, >.<'
  84.  
  85.         hltoken = strtok(hlwords, delim);   // get hte first word and..
  86.        
  87.         // serch the word in all the text chat.
  88.         while (hltoken != NULL && !found)
  89.         {
  90.             if (str_find_nocase(texto, hltoken))
  91.             {
  92.                 found = 1;
  93.                 playsound(S_CHATALERT);
  94.             }
  95.  
  96.             hltoken = strtok(NULL, delim); // get the next word.
  97.         }
  98.  
  99.         return found;
  100.     }
  101.    
  102.     /*
  103.      * find the player name with the substring cadena (string) in the name of payer.
  104.      * don't forget add the prototype of this function in ..src/shared/igame.h
  105.      */
  106.     const char* findPlayer(int &index, const char *cadena)
  107.     {
  108.         const char *pname = NULL;
  109.  
  110.         if(!cadena[0] && index<0)   // There is no string (cadena) to find
  111.         {
  112.             fpsent *d = players[0]; // start with the first player in the list.
  113.             index = 0;
  114.             pname = colorname(d);
  115.         }
  116.         // There is string to find, search in the player list since the last point checked.
  117.         else
  118.         {
  119.             const char *found = NULL;
  120.             loopv(players)
  121.             {
  122.                 fpsent *d = players[i];
  123.                
  124.                 // set the point to search = last point checked
  125.                 if(i<=index) continue;
  126.                
  127.                 // then, compare the string with the player name.
  128.                 else found = str_find_nocase(d->name, cadena);
  129.                
  130.                 if(found) {
  131.                     pname = colorname(d);
  132.                     index = i;  // set the last point checked.
  133.                     break;
  134.                 }
  135.             }
  136.             if(!found) index = -1;
  137.         }
  138.  
  139.         return pname;
  140.     }
  141.  
  142.  
  143.     void parsemessages(int cn, fpsent *d, ucharbuf &p)
  144.     {
  145.         static char text[MAXTRANS];
  146.         int type;
  147.         bool mapchanged = false, demopacket = false;
  148.  
  149.         while(p.remaining()) switch(type = getint(p))
  150.         {
  151.             /*
  152.              *  some cases before our code, ^.^
  153.              *
  154.              */
  155.            
  156.             case N_TEXT:
  157.             {
  158.                 if(!d) return;
  159.                 int hlfound; // added: to get the result of the highlighting search.
  160.                 getstring(text, p);
  161.                 filtertext(text, text);
  162.                 if(isignored(d->clientnum)) break;
  163.                 if(d->state!=CS_DEAD && d->state!=CS_SPECTATOR)
  164.                     particle_textcopy(d->abovehead(), text, PART_TEXT, 2000, 0x32FF64, 4.0f, -8);
  165.                
  166.                 // added: get the result of the hifglighting: based on our name or based on words.
  167.                 hlfound = highlightingtype ? highlighting_ByWords(text) : highlighting_ByName(text);
  168.                 conoutf(CON_CHAT, "%s:%s %s", colorname(d), hlfound ? "\f3" : "\f0", text); // text red = match, then is green.
  169.                
  170.                 break;
  171.             }
  172.  
  173.             case N_SAYTEAM:
  174.             {
  175.                 int hlfound; // added: to get the result of the highlighting search.
  176.                 int tcn = getint(p);
  177.                 fpsent *t = getclient(tcn);
  178.                 getstring(text, p);
  179.                 filtertext(text, text);
  180.                 if(!t || isignored(t->clientnum)) break;
  181.                 if(t->state!=CS_DEAD && t->state!=CS_SPECTATOR)
  182.                     particle_textcopy(t->abovehead(), text, PART_TEXT, 2000, 0x6496FF, 4.0f, -8);
  183.                
  184.                 // same as above (N_TEXT) but in team chat.
  185.                 hlfound = highlightingtype ? highlighting_ByWords(text) : highlighting_ByName(text);
  186.                 conoutf(CON_TEAMCHAT, "%s:%s %s", colorname(t), hlfound ? "\f3" : "\f1", text);  // text red = match, then is blue.
  187.                
  188.                 break;
  189.             }
  190.            
  191.             /*
  192.              *
  193.              * A lot of cases after this, ^.^
  194.              *
  195.              */
  196.  
  197.             default:
  198.                 neterr("type", cn < 0);
  199.                 return;
  200.         }
  201.     }
  202.    
  203.     /*
  204.      * more functions here, ^.^
  205.      *
  206.      */
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement