Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.05 KB | None | 0 0
  1. /**************************************************************************
  2. * *
  3. * Colored Chat Functions *
  4. * Author: exvel, Editor: Popoklopsi, Powerlord, Bara, Klexen *
  5. * Version: CSGO 1.0 *
  6. * Modified Specifically for CSGO Color Support by Klexen *
  7. **************************************************************************/
  8.  
  9.  
  10. #if defined _colors_included
  11. #endinput
  12. #endif
  13. #define _colors_included
  14.  
  15. #define MAX_MESSAGE_LENGTH 250
  16. #define MAX_COLORS 11
  17.  
  18. #define SERVER_INDEX 0
  19. #define NO_INDEX -1
  20. #define NO_PLAYER -2
  21.  
  22. enum Colors
  23. {
  24. Color_Default = 0,
  25. Color_Darkred,
  26. Color_Pink,
  27. Color_Green,
  28. Color_Yellow,
  29. Color_Red,
  30. Color_Gray,
  31. Color_Blue,
  32. Color_Darkblue,
  33. Color_Purple,
  34. Color_Lightgreen
  35. }
  36.  
  37. /* Colors' properties */
  38. new String:CTag[][] = {"{NORMAL}", "{DARKRED}", "{PINK}", "{GREEN}", "{YELLOW}", "{LIGHTGREEN}", "{RED}", "{GRAY}", "{BLUE}", "{DARKBLUE}", "{PURPLE}"};
  39. new String:CTagCode[][] = {"\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x0B", "\x0C", "\x0E"};
  40. new bool:CTagReqSayText2[] = {false, false, false, false, false, false, false, false, false, false, false};
  41. new bool:CEventIsHooked = false;
  42. new bool:CSkipList[MAXPLAYERS+1] = {false,...};
  43.  
  44. /* Game default profile */
  45. new bool:CProfile_Colors[] = {true, false, false, false, false, false, false, false, false, false, false};
  46. new CProfile_TeamIndex[] = {NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX};
  47. new bool:CProfile_SayText2 = false;
  48.  
  49. /**
  50. * Prints a message to a specific client in the chat area.
  51. * Supports color tags.
  52. *
  53. * @param client Client index.
  54. * @param szMessage Message (formatting rules).
  55. * @return No return
  56. *
  57. * On error/Errors: If the client is not connected an error will be thrown.
  58. */
  59. stock CPrintToChat(client, const String:szMessage[], any:...)
  60. {
  61. if (client <= 0 || client > MaxClients)
  62. ThrowError("Invalid client index %d", client);
  63.  
  64. if (!IsClientInGame(client))
  65. ThrowError("Client %d is not in game", client);
  66.  
  67. decl String:szBuffer[MAX_MESSAGE_LENGTH];
  68. decl String:szCMessage[MAX_MESSAGE_LENGTH];
  69.  
  70. SetGlobalTransTarget(client);
  71.  
  72. Format(szBuffer, sizeof(szBuffer), "\x01%s", szMessage);
  73. VFormat(szCMessage, sizeof(szCMessage), szBuffer, 3);
  74.  
  75. new index = CFormat(szCMessage, sizeof(szCMessage));
  76.  
  77. if (index == NO_INDEX)
  78. PrintToChat(client, "%s", szCMessage);
  79. else
  80. CSayText2(client, index, szCMessage);
  81. }
  82.  
  83. stock CReplyToCommand(client, const String:szMessage[], any:...)
  84. {
  85.  
  86. decl String:szCMessage[MAX_MESSAGE_LENGTH];
  87. VFormat(szCMessage, sizeof(szCMessage), szMessage, 3);
  88.  
  89. if (client == 0)
  90. {
  91. CRemoveTags(szCMessage, sizeof(szCMessage));
  92. PrintToServer("%s", szCMessage);
  93. }
  94. else if (GetCmdReplySource() == SM_REPLY_TO_CONSOLE)
  95. {
  96. CRemoveTags(szCMessage, sizeof(szCMessage));
  97. PrintToConsole(client, "%s", szCMessage);
  98. }
  99. else
  100. {
  101. CPrintToChat(client, "%s", szCMessage);
  102. }
  103. }
  104.  
  105.  
  106. /**
  107. * Prints a message to all clients in the chat area.
  108. * Supports color tags.
  109. *
  110. * @param client Client index.
  111. * @param szMessage Message (formatting rules)
  112. * @return No return
  113. */
  114. stock CPrintToChatAll(const String:szMessage[], any:...)
  115. {
  116. decl String:szBuffer[MAX_MESSAGE_LENGTH];
  117.  
  118. for (new i = 1; i <= MaxClients; i++)
  119. {
  120. if (IsClientInGame(i) && !IsFakeClient(i) && !CSkipList[i])
  121. {
  122. SetGlobalTransTarget(i);
  123. VFormat(szBuffer, sizeof(szBuffer), szMessage, 2);
  124.  
  125. CPrintToChat(i, "%s", szBuffer);
  126. }
  127.  
  128. CSkipList[i] = false;
  129. }
  130. }
  131.  
  132. /**
  133. * Prints a message to a specific client in the chat area.
  134. * Supports color tags and teamcolor tag.
  135. *
  136. * @param client Client index.
  137. * @param author Author index whose color will be used for teamcolor tag.
  138. * @param szMessage Message (formatting rules).
  139. * @return No return
  140. *
  141. * On error/Errors: If the client or author are not connected an error will be thrown.
  142. */
  143. stock CPrintToChatEx(client, author, const String:szMessage[], any:...)
  144. {
  145. if (client <= 0 || client > MaxClients)
  146. ThrowError("Invalid client index %d", client);
  147.  
  148. if (!IsClientInGame(client))
  149. ThrowError("Client %d is not in game", client);
  150.  
  151. if (author < 0 || author > MaxClients)
  152. ThrowError("Invalid client index %d", author);
  153.  
  154. decl String:szBuffer[MAX_MESSAGE_LENGTH];
  155. decl String:szCMessage[MAX_MESSAGE_LENGTH];
  156.  
  157. SetGlobalTransTarget(client);
  158.  
  159. Format(szBuffer, sizeof(szBuffer), "\x01%s", szMessage);
  160. VFormat(szCMessage, sizeof(szCMessage), szBuffer, 4);
  161.  
  162. new index = CFormat(szCMessage, sizeof(szCMessage), author);
  163.  
  164. if (index == NO_INDEX)
  165. PrintToChat(client, "%s", szCMessage);
  166. else
  167. CSayText2(client, author, szCMessage);
  168. }
  169.  
  170. /**
  171. * Prints a message to all clients in the chat area.
  172. * Supports color tags and teamcolor tag.
  173. *
  174. * @param author Author index whos color will be used for teamcolor tag.
  175. * @param szMessage Message (formatting rules).
  176. * @return No return
  177. *
  178. * On error/Errors: If the author is not connected an error will be thrown.
  179. */
  180. stock CPrintToChatAllEx(author, const String:szMessage[], any:...)
  181. {
  182. if (author < 0 || author > MaxClients)
  183. ThrowError("Invalid client index %d", author);
  184.  
  185. if (!IsClientInGame(author))
  186. ThrowError("Client %d is not in game", author);
  187.  
  188. decl String:szBuffer[MAX_MESSAGE_LENGTH];
  189.  
  190. for (new i = 1; i <= MaxClients; i++)
  191. {
  192. if (IsClientInGame(i) && !IsFakeClient(i) && !CSkipList[i])
  193. {
  194. SetGlobalTransTarget(i);
  195. VFormat(szBuffer, sizeof(szBuffer), szMessage, 3);
  196.  
  197. CPrintToChatEx(i, author, "%s", szBuffer);
  198. }
  199.  
  200. CSkipList[i] = false;
  201. }
  202. }
  203.  
  204. /**
  205. * Removes color tags from the string.
  206. *
  207. * @param szMessage String.
  208. * @return No return
  209. */
  210. stock CRemoveTags(String:szMessage[], maxlength)
  211. {
  212. for (new i = 0; i < MAX_COLORS; i++)
  213. ReplaceString(szMessage, maxlength, CTag[i], "", false);
  214.  
  215. ReplaceString(szMessage, maxlength, "{teamcolor}", "", false);
  216. }
  217.  
  218. /**
  219. * Checks whether a color is allowed or not
  220. *
  221. * @param tag Color Tag.
  222. * @return True when color is supported, otherwise false
  223. */
  224. stock CColorAllowed(Colors:color)
  225. {
  226. if (!CEventIsHooked)
  227. {
  228. CSetupProfile();
  229.  
  230. CEventIsHooked = true;
  231. }
  232.  
  233. return CProfile_Colors[color];
  234. }
  235.  
  236. /**
  237. * Replace the color with another color
  238. * Handle with care!
  239. *
  240. * @param color color to replace.
  241. * @param newColor color to replace with.
  242. * @noreturn
  243. */
  244. stock CReplaceColor(Colors:color, Colors:newColor)
  245. {
  246. if (!CEventIsHooked)
  247. {
  248. CSetupProfile();
  249.  
  250. CEventIsHooked = true;
  251. }
  252.  
  253. CProfile_Colors[color] = CProfile_Colors[newColor];
  254. CProfile_TeamIndex[color] = CProfile_TeamIndex[newColor];
  255.  
  256. CTagReqSayText2[color] = CTagReqSayText2[newColor];
  257. Format(CTagCode[color], sizeof(CTagCode[]), CTagCode[newColor])
  258. }
  259.  
  260. /**
  261. * This function should only be used right in front of
  262. * CPrintToChatAll or CPrintToChatAllEx and it tells
  263. * to those funcions to skip specified client when printing
  264. * message to all clients. After message is printed client will
  265. * no more be skipped.
  266. *
  267. * @param client Client index
  268. * @return No return
  269. */
  270. stock CSkipNextClient(client)
  271. {
  272. if (client <= 0 || client > MaxClients)
  273. ThrowError("Invalid client index %d", client);
  274.  
  275. CSkipList[client] = true;
  276. }
  277.  
  278. /**
  279. * Replaces color tags in a string with color codes
  280. *
  281. * @param szMessage String.
  282. * @param maxlength Maximum length of the string buffer.
  283. * @return Client index that can be used for SayText2 author index
  284. *
  285. * On error/Errors: If there is more then one team color is used an error will be thrown.
  286. */
  287. stock CFormat(String:szMessage[], maxlength, author=NO_INDEX)
  288. {
  289. decl String:szGameName[30];
  290.  
  291. GetGameFolderName(szGameName, sizeof(szGameName));
  292.  
  293. /* Hook event for auto profile setup on map start */
  294. if (!CEventIsHooked)
  295. {
  296. CSetupProfile();
  297. HookEvent("server_spawn", CEvent_MapStart, EventHookMode_PostNoCopy);
  298.  
  299. CEventIsHooked = true;
  300. }
  301.  
  302. new iRandomPlayer = NO_INDEX;
  303.  
  304. // On CS:GO set invisible precolor
  305. if (StrEqual(szGameName, "csgo", false))
  306. Format(szMessage, maxlength, " \x01\x0B\x01%s", szMessage);
  307.  
  308. /* If author was specified replace {teamcolor} tag */
  309. if (author != NO_INDEX)
  310. {
  311. if (CProfile_SayText2)
  312. {
  313. ReplaceString(szMessage, maxlength, "{teamcolor}", "\x03", false);
  314.  
  315. iRandomPlayer = author;
  316. }
  317. /* If saytext2 is not supported by game replace {teamcolor} with green tag */
  318. else
  319. ReplaceString(szMessage, maxlength, "{teamcolor}", CTagCode[Color_Green], false);
  320. }
  321. else
  322. ReplaceString(szMessage, maxlength, "{teamcolor}", "", false);
  323.  
  324. /* For other color tags we need a loop */
  325. for (new i = 0; i < MAX_COLORS; i++)
  326. {
  327. /* If tag not found - skip */
  328. if (StrContains(szMessage, CTag[i], false) == -1)
  329. continue;
  330.  
  331. /* If tag is not supported by game replace it with green tag */
  332. else if (!CProfile_Colors[i])
  333. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[Color_Green], false);
  334.  
  335. /* If tag doesn't need saytext2 simply replace */
  336. else if (!CTagReqSayText2[i])
  337. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[i], false);
  338.  
  339. /* Tag needs saytext2 */
  340. else
  341. {
  342. /* If saytext2 is not supported by game replace tag with green tag */
  343. if (!CProfile_SayText2)
  344. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[Color_Green], false);
  345.  
  346. /* Game supports saytext2 */
  347. else
  348. {
  349. /* If random player for tag wasn't specified replace tag and find player */
  350. if (iRandomPlayer == NO_INDEX)
  351. {
  352. /* Searching for valid client for tag */
  353. iRandomPlayer = CFindRandomPlayerByTeam(CProfile_TeamIndex[i]);
  354.  
  355. /* If player not found replace tag with green color tag */
  356. if (iRandomPlayer == NO_PLAYER)
  357. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[Color_Green], false);
  358.  
  359. /* If player was found simply replace */
  360. else
  361. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[i], false);
  362.  
  363. }
  364. /* If found another team color tag throw error */
  365. else
  366. {
  367. //ReplaceString(szMessage, maxlength, CTag[i], "");
  368. ThrowError("Using two team colors in one message is not allowed");
  369. }
  370. }
  371.  
  372. }
  373. }
  374.  
  375. return iRandomPlayer;
  376. }
  377.  
  378. /**
  379. * Founds a random player with specified team
  380. *
  381. * @param color_team Client team.
  382. * @return Client index or NO_PLAYER if no player found
  383. */
  384. stock CFindRandomPlayerByTeam(color_team)
  385. {
  386. if (color_team == SERVER_INDEX)
  387. return 0;
  388. else
  389. {
  390. for (new i = 1; i <= MaxClients; i++)
  391. {
  392. if (IsClientInGame(i) && GetClientTeam(i) == color_team)
  393. return i;
  394. }
  395. }
  396.  
  397. return NO_PLAYER;
  398. }
  399.  
  400. /**
  401. * Sends a SayText2 usermessage to a client
  402. *
  403. * @param szMessage Client index
  404. * @param maxlength Author index
  405. * @param szMessage Message
  406. * @return No return.
  407. */
  408. stock CSayText2(client, author, const String:szMessage[])
  409. {
  410. new Handle:hBuffer = StartMessageOne("SayText2", client, USERMSG_RELIABLE|USERMSG_BLOCKHOOKS);
  411.  
  412. if(GetFeatureStatus(FeatureType_Native, "GetUserMessageType") == FeatureStatus_Available && GetUserMessageType() == UM_Protobuf)
  413. {
  414. PbSetInt(hBuffer, "ent_idx", author);
  415. PbSetBool(hBuffer, "chat", true);
  416. PbSetString(hBuffer, "msg_name", szMessage);
  417. PbAddString(hBuffer, "params", "");
  418. PbAddString(hBuffer, "params", "");
  419. PbAddString(hBuffer, "params", "");
  420. PbAddString(hBuffer, "params", "");
  421. }
  422. else
  423. {
  424. BfWriteByte(hBuffer, author);
  425. BfWriteByte(hBuffer, true);
  426. BfWriteString(hBuffer, szMessage);
  427. }
  428.  
  429. EndMessage();
  430. }
  431.  
  432. /**
  433. * Creates game color profile
  434. * This function must be edited if you want to add more games support
  435. *
  436. * @return No return.
  437. */
  438. stock CSetupProfile()
  439. {
  440. decl String:szGameName[30];
  441. GetGameFolderName(szGameName, sizeof(szGameName));
  442.  
  443. if (StrEqual(szGameName, "csgo", false))
  444. {
  445. CProfile_Colors[Color_Darkred] = true;
  446. CProfile_Colors[Color_Pink] = true;
  447. CProfile_Colors[Color_Green] = true;
  448. CProfile_Colors[Color_Yellow] = true;
  449. CProfile_Colors[Color_Red] = true;
  450. CProfile_Colors[Color_Gray] = true;
  451. CProfile_Colors[Color_Blue] = true;
  452. CProfile_Colors[Color_Darkblue] = true;
  453. CProfile_Colors[Color_Purple] = true;
  454. CProfile_Colors[Color_Lightgreen] = true;
  455. CProfile_TeamIndex[Color_Red] = 2;
  456. CProfile_TeamIndex[Color_Blue] = 3;
  457. CProfile_SayText2 = true;
  458. }
  459. /* Profile for other games */
  460. else
  461. {
  462. if (GetUserMessageId("SayText2") == INVALID_MESSAGE_ID)
  463. {
  464. CProfile_SayText2 = false;
  465. }
  466. else
  467. {
  468. CProfile_Colors[Color_Red] = true;
  469. CProfile_Colors[Color_Blue] = true;
  470. CProfile_TeamIndex[Color_Red] = 2;
  471. CProfile_TeamIndex[Color_Blue] = 3;
  472. CProfile_SayText2 = true;
  473. }
  474. }
  475. }
  476.  
  477. public Action:CEvent_MapStart(Handle:event, const String:name[], bool:dontBroadcast)
  478. {
  479. CSetupProfile();
  480.  
  481. for (new i = 1; i <= MaxClients; i++)
  482. CSkipList[i] = false;
  483. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement