CT_CHAT.C 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. //
  2. // Chat mode
  3. //
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "DoomDef.h"
  7. #include "P_local.h"
  8. #include "soundst.h"
  9. #define QUEUESIZE 128
  10. #define MESSAGESIZE 128
  11. #define MESSAGELEN 265
  12. #define CT_PLR_GREEN 1
  13. #define CT_PLR_YELLOW 2
  14. #define CT_PLR_RED 3
  15. #define CT_PLR_BLUE 4
  16. #define CT_PLR_ALL 5
  17. #define CT_KEY_GREEN 'g'
  18. #define CT_KEY_YELLOW 'y'
  19. #define CT_KEY_RED 'r'
  20. #define CT_KEY_BLUE 'b'
  21. #define CT_KEY_ALL 't'
  22. #define CT_ESCAPE 6
  23. // Public data
  24. void CT_Init(void);
  25. void CT_Drawer(void);
  26. boolean CT_Responder(event_t *ev);
  27. void CT_Ticker(void);
  28. char CT_dequeueChatChar(void);
  29. boolean chatmodeon;
  30. // Private data
  31. void CT_queueChatChar(char ch);
  32. void CT_ClearChatMessage(int player);
  33. void CT_AddChar(int player, char c);
  34. void CT_BackSpace(int player);
  35. int head;
  36. int tail;
  37. byte ChatQueue[QUEUESIZE];
  38. int chat_dest[MAXPLAYERS];
  39. char chat_msg[MAXPLAYERS][MESSAGESIZE];
  40. char plr_lastmsg[MAXPLAYERS][MESSAGESIZE+9]; // add in the length of the pre-string
  41. int msgptr[MAXPLAYERS];
  42. int msglen[MAXPLAYERS];
  43. boolean cheated;
  44. static int FontABaseLump;
  45. char *CT_FromPlrText[MAXPLAYERS] =
  46. {
  47. "GREEN: ",
  48. "YELLOW: ",
  49. "RED: ",
  50. "BLUE: "
  51. };
  52. char *chat_macros[10];
  53. boolean altdown;
  54. boolean shiftdown;
  55. //===========================================================================
  56. //
  57. // CT_Init
  58. //
  59. // Initialize chat mode data
  60. //===========================================================================
  61. void CT_Init(void)
  62. {
  63. int i;
  64. head = 0; //initialize the queue index
  65. tail = 0;
  66. chatmodeon = false;
  67. memset(ChatQueue, 0, QUEUESIZE);
  68. for(i = 0; i < MAXPLAYERS; i++)
  69. {
  70. chat_dest[i] = 0;
  71. msgptr[i] = 0;
  72. memset(plr_lastmsg[i], 0, MESSAGESIZE);
  73. memset(chat_msg[i], 0, MESSAGESIZE);
  74. }
  75. FontABaseLump = W_GetNumForName("FONTA_S")+1;
  76. return;
  77. }
  78. //===========================================================================
  79. //
  80. // CT_Stop
  81. //
  82. //===========================================================================
  83. void CT_Stop(void)
  84. {
  85. chatmodeon = false;
  86. return;
  87. }
  88. //===========================================================================
  89. //
  90. // CT_Responder
  91. //
  92. //===========================================================================
  93. boolean CT_Responder(event_t *ev)
  94. {
  95. char *macro;
  96. int sendto;
  97. if(!netgame)
  98. {
  99. return false;
  100. }
  101. if(ev->data1 == KEY_LALT || ev->data2 == KEY_RALT)
  102. {
  103. altdown = (ev->type == ev_keydown);
  104. return false;
  105. }
  106. if(ev->data1 == KEY_RSHIFT)
  107. {
  108. shiftdown = (ev->type == ev_keydown);
  109. return false;
  110. }
  111. if(ev->type != ev_keydown)
  112. {
  113. return false;
  114. }
  115. if(!chatmodeon)
  116. {
  117. sendto = 0;
  118. if(ev->data1 == CT_KEY_ALL)
  119. {
  120. sendto = CT_PLR_ALL;
  121. }
  122. else if(ev->data1 == CT_KEY_GREEN)
  123. {
  124. sendto = CT_PLR_GREEN;
  125. }
  126. else if(ev->data1 == CT_KEY_YELLOW)
  127. {
  128. sendto = CT_PLR_YELLOW;
  129. }
  130. else if(ev->data1 == CT_KEY_RED)
  131. {
  132. sendto = CT_PLR_RED;
  133. }
  134. else if(ev->data1 == CT_KEY_BLUE)
  135. {
  136. sendto = CT_PLR_BLUE;
  137. }
  138. if(sendto == 0 || (sendto != CT_PLR_ALL && !playeringame[sendto-1])
  139. || sendto == consoleplayer+1)
  140. {
  141. return false;
  142. }
  143. CT_queueChatChar(sendto);
  144. chatmodeon = true;
  145. return true;
  146. }
  147. else
  148. {
  149. if(altdown)
  150. {
  151. if(ev->data1 >= '0' && ev->data1 <= '9')
  152. {
  153. if(ev->data1 == '0')
  154. { // macro 0 comes after macro 9
  155. ev->data1 = '9'+1;
  156. }
  157. macro = chat_macros[ev->data1-'1'];
  158. CT_queueChatChar(KEY_ENTER); //send old message
  159. CT_queueChatChar(chat_dest[consoleplayer]); // chose the dest.
  160. while(*macro)
  161. {
  162. CT_queueChatChar(toupper(*macro++));
  163. }
  164. CT_queueChatChar(KEY_ENTER); //send it off...
  165. CT_Stop();
  166. return true;
  167. }
  168. }
  169. if(ev->data1 == KEY_ENTER)
  170. {
  171. CT_queueChatChar(KEY_ENTER);
  172. CT_Stop();
  173. return true;
  174. }
  175. else if(ev->data1 == KEY_ESCAPE)
  176. {
  177. CT_queueChatChar(CT_ESCAPE);
  178. CT_Stop();
  179. return true;
  180. }
  181. else if(ev->data1 >= 'a' && ev->data1 <= 'z')
  182. {
  183. CT_queueChatChar(ev->data1-32);
  184. return true;
  185. }
  186. else if(shiftdown)
  187. {
  188. if(ev->data1 == '1')
  189. {
  190. CT_queueChatChar('!');
  191. return true;
  192. }
  193. else if(ev->data1 == '/')
  194. {
  195. CT_queueChatChar('?');
  196. return true;
  197. }
  198. }
  199. else
  200. {
  201. if(ev->data1 == ' ' || ev->data1 == ',' || ev->data1 == '.'
  202. || (ev->data1 >= '0' && ev->data1 <= '9') || ev->data1 == '\''
  203. || ev->data1 == KEY_BACKSPACE || ev->data1 == '-' || ev->data1 == '=')
  204. {
  205. CT_queueChatChar(ev->data1);
  206. return true;
  207. }
  208. }
  209. }
  210. return false;
  211. }
  212. //===========================================================================
  213. //
  214. // CT_Ticker
  215. //
  216. //===========================================================================
  217. void CT_Ticker(void)
  218. {
  219. int i;
  220. int j;
  221. char c;
  222. int numplayers;
  223. for(i=0; i < MAXPLAYERS; i++)
  224. {
  225. if(!playeringame[i])
  226. {
  227. continue;
  228. }
  229. if((c = players[i].cmd.chatchar) != 0)
  230. {
  231. if(c <= 5)
  232. {
  233. chat_dest[i] = c;
  234. continue;
  235. }
  236. else if(c == CT_ESCAPE)
  237. {
  238. CT_ClearChatMessage(i);
  239. }
  240. else if(c == KEY_ENTER)
  241. {
  242. numplayers = 0;
  243. for(j = 0; j < MAXPLAYERS; j++)
  244. {
  245. numplayers += playeringame[j];
  246. }
  247. CT_AddChar(i, 0); // set the end of message character
  248. if(numplayers > 2)
  249. {
  250. strcpy(plr_lastmsg[i], CT_FromPlrText[i]);
  251. strcat(plr_lastmsg[i], chat_msg[i]);
  252. }
  253. else
  254. {
  255. strcpy(plr_lastmsg[i], chat_msg[i]);
  256. }
  257. if(i != consoleplayer && (chat_dest[i] == consoleplayer+1
  258. || chat_dest[i] == CT_PLR_ALL) && *chat_msg[i])
  259. {
  260. P_SetMessage(&players[consoleplayer], plr_lastmsg[i],
  261. true);
  262. S_StartSound(NULL, sfx_chat);
  263. }
  264. else if(i == consoleplayer && (*chat_msg[i]))
  265. {
  266. if(numplayers > 1)
  267. {
  268. P_SetMessage(&players[consoleplayer], "-MESSAGE SENT-",
  269. true);
  270. S_StartSound(NULL, sfx_chat);
  271. }
  272. else
  273. {
  274. P_SetMessage(&players[consoleplayer],
  275. "THERE ARE NO OTHER PLAYERS IN THE GAME!", true);
  276. S_StartSound(NULL, sfx_chat);
  277. }
  278. }
  279. CT_ClearChatMessage(i);
  280. }
  281. else if(c == KEY_BACKSPACE)
  282. {
  283. CT_BackSpace(i);
  284. }
  285. else
  286. {
  287. CT_AddChar(i, c);
  288. }
  289. }
  290. }
  291. return;
  292. }
  293. //===========================================================================
  294. //
  295. // CT_Drawer
  296. //
  297. //===========================================================================
  298. void CT_Drawer(void)
  299. {
  300. int i;
  301. int x;
  302. patch_t *patch;
  303. if(chatmodeon)
  304. {
  305. x = 25;
  306. for(i = 0; i < msgptr[consoleplayer]; i++)
  307. {
  308. if(chat_msg[consoleplayer][i] < 33)
  309. {
  310. x += 6;
  311. }
  312. else
  313. {
  314. patch=W_CacheLumpNum(FontABaseLump+
  315. chat_msg[consoleplayer][i]-33, PU_CACHE);
  316. V_DrawPatch(x, 10, patch);
  317. x += patch->width;
  318. }
  319. }
  320. V_DrawPatch(x, 10, W_CacheLumpName("FONTA59", PU_CACHE));
  321. BorderTopRefresh = true;
  322. UpdateState |= I_MESSAGES;
  323. }
  324. }
  325. //===========================================================================
  326. //
  327. // CT_queueChatChar
  328. //
  329. //===========================================================================
  330. void CT_queueChatChar(char ch)
  331. {
  332. if((tail+1)&(QUEUESIZE-1) == head)
  333. { // the queue is full
  334. return;
  335. }
  336. ChatQueue[tail] = ch;
  337. tail = (tail+1)&(QUEUESIZE-1);
  338. }
  339. //===========================================================================
  340. //
  341. // CT_dequeueChatChar
  342. //
  343. //===========================================================================
  344. char CT_dequeueChatChar(void)
  345. {
  346. byte temp;
  347. if(head == tail)
  348. { // queue is empty
  349. return 0;
  350. }
  351. temp = ChatQueue[head];
  352. head = (head+1)&(QUEUESIZE-1);
  353. return temp;
  354. }
  355. //===========================================================================
  356. //
  357. // CT_AddChar
  358. //
  359. //===========================================================================
  360. void CT_AddChar(int player, char c)
  361. {
  362. patch_t *patch;
  363. if(msgptr[player]+1 >= MESSAGESIZE || msglen[player] >= MESSAGELEN)
  364. { // full.
  365. return;
  366. }
  367. chat_msg[player][msgptr[player]] = c;
  368. msgptr[player]++;
  369. if(c < 33)
  370. {
  371. msglen[player] += 6;
  372. }
  373. else
  374. {
  375. patch = W_CacheLumpNum(FontABaseLump+c-33, PU_CACHE);
  376. msglen[player] += patch->width;
  377. }
  378. }
  379. //===========================================================================
  380. //
  381. // CT_BackSpace
  382. //
  383. // Backs up a space, when the user hits (obviously) backspace
  384. //===========================================================================
  385. void CT_BackSpace(int player)
  386. {
  387. patch_t *patch;
  388. char c;
  389. if(msgptr[player] == 0)
  390. { // message is already blank
  391. return;
  392. }
  393. msgptr[player]--;
  394. c = chat_msg[player][msgptr[player]];
  395. if(c < 33)
  396. {
  397. msglen[player] -= 6;
  398. }
  399. else
  400. {
  401. patch = W_CacheLumpNum(FontABaseLump+c-33, PU_CACHE);
  402. msglen[player] -= patch->width;
  403. }
  404. chat_msg[player][msgptr[player]] = 0;
  405. }
  406. //===========================================================================
  407. //
  408. // CT_ClearChatMessage
  409. //
  410. // Clears out the data for the chat message, but the player's message
  411. // is still saved in plrmsg.
  412. //===========================================================================
  413. void CT_ClearChatMessage(int player)
  414. {
  415. memset(chat_msg[player], 0, MESSAGESIZE);
  416. msgptr[player] = 0;
  417. msglen[player] = 0;
  418. }