-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbot_chat.cpp
More file actions
522 lines (450 loc) · 13.9 KB
/
bot_chat.cpp
File metadata and controls
522 lines (450 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// ####################################
// # #
// # Ping of Death - Bot #
// # by #
// # Markus Klinge aka Count Floyd #
// # #
// ####################################
//
// Started from the HPB-Bot Alpha Source
// by Botman so Credits for a lot of the basic
// HL Server/Client Stuff goes to him
//
// bot_chat.cpp
//
// Contains parsing stuff & chat selection for chatting Bots
#include "bot_globals.h"
#ifdef __linux__
#define strncpy_s strncpy
#define strcat_s strcat
#define strcpy_s strcpy
#endif
inline void StripClanTags(char* pszTemp1, char* pszReturn, char* cTag1, char* cTag2)
{
// Strips out Words between Chars like []
unsigned char ucLen = (unsigned char)strlen(pszTemp1);
int cnt;
char* pszEndPattern = strstr(pszTemp1, cTag2);
char* pszStartPattern = strstr(pszTemp1, cTag1);
if (pszStartPattern && pszEndPattern)
{
if (pszEndPattern - pszStartPattern - 1 < ucLen - 2)
{
if (pszStartPattern != pszTemp1)
{
#ifdef _WIN32
cnt = (int)(pszStartPattern - pszTemp1);
strncpy_s(pszReturn, cnt + 1, pszTemp1, cnt);
#else
if (pszStartPattern - 1 != pszTemp1)
strncpy(pszReturn, pszTemp1, (pszStartPattern - pszTemp1) - 1);
#endif
}
if (pszEndPattern < pszTemp1 + ucLen)
{
cnt = (int)ucLen - (pszEndPattern - pszStartPattern - 1);
#ifdef _WIN32
strcat_s(pszReturn, cnt, pszEndPattern + 1);
#else
strcat(pszReturn, pszEndPattern + 1);
#endif
}
}
else
#ifdef _WIN32
strncpy_s(pszReturn, (int)strlen(pszTemp1) + 1, pszTemp1, (int)strlen(pszTemp1));
#else
strcpy(pszReturn, pszTemp1);
#endif
}
else
#ifdef _WIN32
strncpy_s(pszReturn, (int)strlen(pszTemp1) + 1, pszTemp1, (int)strlen(pszTemp1));
#else
strcpy(pszReturn, pszTemp1);
#endif
}
void ConvertNameToHuman(char* pszName, char* pszReturn)
{
// Converts given Names to a more human like style for output
char szTemp1[80];
char szTemp2[80];
// char* pszLast;
// unsigned char ucLen = 1;
unsigned char ucActLen = 1;
memset(szTemp1, 0, sizeof szTemp1);
memset(szTemp2, 0, sizeof szTemp2);
StripClanTags(pszName, szTemp1, "[", "]");
StripClanTags(szTemp1, szTemp2, "(", ")");
memset(szTemp1, 0, sizeof szTemp1);
StripClanTags(szTemp2, szTemp1, "{", "}");
// strip out all spaces at the end of the name...
ucActLen = (unsigned char)strlen(szTemp1);
// pszLast = szTemp1 + ucActLen - 1;
while (ucActLen > 0)
{
if (szTemp1[ucActLen - 1] == ' ')
{
szTemp1[ucActLen - 1] = '\0';
ucActLen = (unsigned char)strlen(szTemp1);
if (g_b_DebugChat)
ALERT(at_logged, "[DEBUG] ConvertNameToHuman - There was a space in the name %s.\n", szTemp2);
}
else
ucActLen = 0;
}
#ifdef _WIN32
strcpy_s(pszReturn, (unsigned int)strlen(szTemp1) + 1, szTemp1);
#else
strcpy(pszReturn, szTemp1);
#endif
if (g_b_DebugChat)
ALERT(at_logged, "[DEBUG] ConvertNameToHuman(3) - Player's name %s.\n", pszReturn);
}
void BotPrepareChatMessage(bot_t* pBot, char* pszText)
{
// Parses Messages from the Botchat, replaces Keywords
// and converts Names into a more human style
int iLen;
char szNamePlaceholder[80];
int iIndex = 0;
int i;
memset(&pBot->szMiscStrings, 0, sizeof pBot->szMiscStrings);
memset(szNamePlaceholder, 0, sizeof szNamePlaceholder);
char* pszTextStart = pszText;
char* pszPattern = pszText;
edict_t* pTalkEdict = NULL;
while (pszPattern)
{
// all replacement placeholders start with a %
pszPattern = strchr(pszTextStart, '%');
if (pszPattern)
{
iLen = pszPattern - pszTextStart;
if (iLen > 0)
#ifdef _WIN32
strncat_s(pBot->szMiscStrings, sizeof pBot->szMiscStrings, pszTextStart, iLen);
#else
strncpy(pBot->szMiscStrings, pszTextStart, iLen);
#endif
// strncpy_s (pBot->szMiscStrings, iLen + 1, pszTextStart, iLen);
pszPattern++;
// Player with most frags ?
if (*pszPattern == 'f')
{
int iHighestFrags = -9000; // just pick some start value
int iCurrFrags;
for (i = 0; i < gpGlobals->maxClients; i++)
{
if (!(clients[i].iFlags & CLIENT_USED)
|| clients[i].pEdict == pBot->pEdict)
continue;
iCurrFrags = (int)clients[i].pEdict->v.frags; // KWo - to remove warning
if (iCurrFrags > iHighestFrags)
{
iHighestFrags = iCurrFrags;
iIndex = i;
}
}
// fix fix fix, all day long...
if (iIndex < gpGlobals->maxClients)
{
pTalkEdict = clients[iIndex].pEdict;
if (!FNullEnt(pTalkEdict))
{
ConvertNameToHuman((char*)STRING(pTalkEdict->v.netname), szNamePlaceholder);
#ifdef _WIN32
strncat_s(pBot->szMiscStrings, sizeof pBot->szMiscStrings, szNamePlaceholder, strlen(szNamePlaceholder));
#else
strcat(pBot->szMiscStrings, szNamePlaceholder);
#endif
}
}
}
// Mapname ?
else if (*pszPattern == 'm')
#ifdef _WIN32
strncat_s(pBot->szMiscStrings, sizeof pBot->szMiscStrings, STRING(gpGlobals->mapname), strlen(STRING(gpGlobals->mapname)));
#else
strcat(pBot->szMiscStrings, STRING(gpGlobals->mapname));
#endif
// Roundtime ?
else if (*pszPattern == 'r')
{
char szTime[] = "000:00";
int iTime = (int)(g_fTimeRoundEnd - gpGlobals->time);
#ifdef _WIN32
sprintf_s(szTime, sizeof szTime, "%02d:%02d", iTime / 60, iTime % 60);
strncat_s(pBot->szMiscStrings, sizeof pBot->szMiscStrings, szTime, strlen(szTime));
#else
strcat(pBot->szMiscStrings, szTime);
sprintf(szTime, "%02d:%02d", iTime / 60, iTime % 60);
#endif
}
// Chat Reply ?
else if (*pszPattern == 's') // KWo - 09.03.2010 - rewritten...
{
// crash fixes, crash fixes...
iIndex = pBot->SaytextBuffer.iEntityIndex;
i = 0;
if (iIndex < 1 || iIndex > gpGlobals->maxClients)
{
iIndex = (int)RANDOM_LONG(1, gpGlobals->maxClients);
while ((iIndex == g_i_botthink_index || !(clients[iIndex - 1].iFlags & CLIENT_USED))
&& i < 64)
{
iIndex = (int)RANDOM_LONG(1, gpGlobals->maxClients);
i++;
}
}
if ((iIndex == g_i_botthink_index || !(clients[iIndex - 1].iFlags & CLIENT_USED)) && i == 0)
{
i = 0;
while ((iIndex == g_i_botthink_index || !(clients[iIndex - 1].iFlags & CLIENT_USED))
&& i < 64)
{
iIndex = (int)RANDOM_LONG(1, gpGlobals->maxClients);
i++;
}
}
pBot->SaytextBuffer.iEntityIndex = iIndex;
if (pBot->SaytextBuffer.iEntityIndex > 0
&& pBot->SaytextBuffer.iEntityIndex <= gpGlobals->maxClients)
{
pTalkEdict = INDEXENT(pBot->SaytextBuffer.iEntityIndex);
if (!FNullEnt(pTalkEdict))
{
ConvertNameToHuman((char*)STRING(pTalkEdict->v.netname), szNamePlaceholder);
if (g_b_DebugChat)
ALERT(at_logged, "[DEBUG] BotPrepareChatMessage(2) - Player's name %s.\n", szNamePlaceholder);
#ifdef _WIN32
strncat_s(pBot->szMiscStrings, sizeof pBot->szMiscStrings, szNamePlaceholder, strlen(szNamePlaceholder));
#else
strcat(pBot->szMiscStrings, szNamePlaceholder);
#endif
}
}
}
// Teammate alive ?
else if (*pszPattern == 't')
{
for (i = 0; i < gpGlobals->maxClients; i++)
{
if (!(clients[i].iFlags & CLIENT_USED)
|| !(clients[i].iFlags & CLIENT_ALIVE)
|| clients[i].iTeam != pBot->bot_team
|| clients[i].pEdict == pBot->pEdict)
continue;
break;
}
if (i < gpGlobals->maxClients)
{
pTalkEdict = clients[i].pEdict;
if (!FNullEnt(pTalkEdict))
{
ConvertNameToHuman((char*)STRING(pTalkEdict->v.netname), szNamePlaceholder);
#ifdef _WIN32
strncat_s(pBot->szMiscStrings, sizeof pBot->szMiscStrings, szNamePlaceholder, strlen(szNamePlaceholder));
#else
strcat(pBot->szMiscStrings, szNamePlaceholder);
#endif
}
}
}
// Killed victim ?
else if (*pszPattern == 'v')
{
pTalkEdict = pBot->pLastVictim;
if (!FNullEnt(pTalkEdict))
{
ConvertNameToHuman((char*)STRING(pTalkEdict->v.netname), szNamePlaceholder);
#ifdef _WIN32
strncat_s(pBot->szMiscStrings, sizeof pBot->szMiscStrings, szNamePlaceholder, strlen(szNamePlaceholder));
#else
strcat(pBot->szMiscStrings, szNamePlaceholder);
#endif
}
}
pszPattern++;
pszTextStart = pszPattern;
}
}
#ifdef _WIN32
strncat_s(pBot->szMiscStrings, sizeof pBot->szMiscStrings, pszTextStart, strlen(pszTextStart));
#else
strcat(pBot->szMiscStrings, pszTextStart);
#endif
// removes trailing '\n'
iLen = (int)strlen(pBot->szMiscStrings);
while ((pBot->szMiscStrings[iLen - 1] == '\n' || pBot->szMiscStrings[iLen - 1] == ' '
|| pBot->szMiscStrings[iLen - 1] == '\r' || pBot->szMiscStrings[iLen - 1] == '\t') && iLen > 0)
pBot->szMiscStrings[--iLen] = 0;
if (g_b_DebugChat)
ALERT(at_logged, "[DEBUG] BotPrepareChatMessage - Bot %s prepared the message: %s.\n", pBot->name, pBot->szMiscStrings);
return;
}
bool BotCheckKeywords(bot_t* pBot, char* pszMessage, char* pszReply)
{
replynode_t* pReply = pChatReplies;
char szKeyword[128];
char* pszCurrKeyword;
char* pszKeywordEnd;
int iLen, iRandom;
char cNumRetries;
while (pReply != NULL)
{
pszCurrKeyword = (char*)&pReply->szKeywords;
while (pszCurrKeyword)
{
pszKeywordEnd = strchr(pszCurrKeyword, '@');
if (pszKeywordEnd)
{
iLen = pszKeywordEnd - pszCurrKeyword;
#ifdef _WIN32
strncpy_s(szKeyword, iLen, pszCurrKeyword, iLen - 1);
#else
strncpy(szKeyword, pszCurrKeyword, iLen);
#endif
szKeyword[iLen] = 0x0;
// Parse Text for occurences of keywords
char* pPattern = strstr(pszMessage, szKeyword);
if (pPattern)
{
STRINGNODE* pNode = pReply->pReplies;
if (pReply->cNumReplies == 1)
#ifdef _WIN32
strcpy_s(pszReply, sizeof pNode->szString, pNode->szString);
#else
strcpy(pszReply, pNode->szString);
#endif
else
{
cNumRetries = 0;
do
{
iRandom = RANDOM_LONG(1, pReply->cNumReplies);
cNumRetries++;
} while (cNumRetries <= 10 /* pReply->cNumReplies */
&& (iRandom == pReply->cLastReply[0] || iRandom == pReply->cLastReply[1]
|| iRandom == pReply->cLastReply[2] || iRandom == pReply->cLastReply[3])); // KWo - 27.03.2010
pReply->cLastReply[3] = pReply->cLastReply[2]; // KWo - 27.03.2010
pReply->cLastReply[2] = pReply->cLastReply[1]; // KWo - 27.03.2010
pReply->cLastReply[1] = pReply->cLastReply[0]; // KWo - 27.03.2010
pReply->cLastReply[0] = iRandom;
cNumRetries = 1;
while (cNumRetries < iRandom)
{
pNode = pNode->Next;
cNumRetries++;
}
#ifdef _WIN32
strcpy_s(pszReply, sizeof pNode->szString, pNode->szString);
#else
strcpy(pszReply, pNode->szString);
#endif
}
if (g_b_DebugChat)
ALERT(at_logged, "[DEBUG] BotCheckKeywords - Bot %s found a keyword: %s.\n", pBot->name, szKeyword);
return true;
}
pszKeywordEnd++;
if (*pszKeywordEnd == 0)
pszKeywordEnd = NULL;
}
pszCurrKeyword = pszKeywordEnd;
}
pReply = pReply->pNextReplyNode;
}
// Didn't find a keyword ? // 50% of the time use some universal reply
if (RANDOM_LONG(1, 100) < 50)
{
iRandom = RANDOM_LONG(0, iNumNoKwChats - 1);
cNumRetries = 0;
while (cNumRetries < 5
&& (iUsedUnknownChatIndex[0] == iRandom
|| iUsedUnknownChatIndex[1] == iRandom
|| iUsedUnknownChatIndex[2] == iRandom
|| iUsedUnknownChatIndex[3] == iRandom
|| iUsedUnknownChatIndex[4] == iRandom)) // KWo - 27.03.2010
{
iRandom = RANDOM_LONG(0, iNumNoKwChats - 1);
cNumRetries++;
}
iUsedUnknownChatIndex[4] = iUsedUnknownChatIndex[3];
iUsedUnknownChatIndex[3] = iUsedUnknownChatIndex[2];
iUsedUnknownChatIndex[2] = iUsedUnknownChatIndex[1];
iUsedUnknownChatIndex[1] = iUsedUnknownChatIndex[0];
iUsedUnknownChatIndex[0] = iRandom;
if (g_b_DebugChat)
ALERT(at_logged, "[DEBUG] BotCheckKeywords - Bot %s didn't find a keyword; uses some universal reply %s.\n", pBot->name, pszReply);
#ifdef _WIN32
strcpy_s(pszReply, sizeof szNoKwChat[iRandom], szNoKwChat[iRandom]);
#else
strcpy(pszReply, szNoKwChat[iRandom]);
#endif
return true;
}
return false;
}
bool BotParseChat(bot_t* pBot, char* pszReply)
{
char szMessage[512];
int iMessageLen;
int i = 0;
// Copy to safe place
#ifdef _WIN32
strcpy_s(szMessage, sizeof pBot->SaytextBuffer.szSayText, pBot->SaytextBuffer.szSayText);
#else
strcpy(szMessage, pBot->SaytextBuffer.szSayText);
#endif
// Text to uppercase for Keyword parsing
iMessageLen = (int)strlen(szMessage);
for (i = 0; i < iMessageLen; i++)
szMessage[i] = (char)toupper((char)szMessage[i]); // KWo it was (int)
szMessage[i] = 0;
// Find the : char behind the name to get the start of the real text
if (g_bIsOldCS15)
{
i = 0; // KWo - 31.03.2006
while (i <= iMessageLen)
{
if (szMessage[i] == ':')
break;
i++;
}
if (g_b_DebugChat)
ALERT(at_logged, "[DEBUG] BotParseChat - Bot %s got the message: %s.\n", pBot->name, szMessage);
return BotCheckKeywords(pBot, &szMessage[i], pszReply); // KWo - 31.03.2006
}
return BotCheckKeywords(pBot, &szMessage[0], pszReply); // KWo - 31.03.2006
}
bool BotRepliesToPlayer(bot_t* pBot)
{
char szText[256];
if (pBot->SaytextBuffer.iEntityIndex > 0
&& pBot->SaytextBuffer.iEntityIndex <= gpGlobals->maxClients
&& pBot->SaytextBuffer.szSayText[0] != 0)
{
if (pBot->SaytextBuffer.fTimeNextChat < gpGlobals->time
&& g_fLastChatTime + 2.0f < gpGlobals->time) // KWo - 10.03.2013
{
if (RANDOM_LONG(0, 100) < pBot->SaytextBuffer.cChatProbability // KWo - 06.03.2010
&& BotParseChat(pBot, (char*)&szText))
{
if (g_b_DebugChat)
ALERT(at_logged, "[DEBUG] BotRepliesToPlayer - Bot %s wants to reply: %s.\n", pBot->name, szText);
BotPrepareChatMessage(pBot, (char*)&szText);
BotPushMessageQueue(pBot, MSG_CS_SAY);
pBot->SaytextBuffer.iEntityIndex = -1;
pBot->SaytextBuffer.szSayText[0] = 0x0;
pBot->SaytextBuffer.fTimeNextChat = gpGlobals->time + pBot->SaytextBuffer.fChatDelay;
pBot->f_lastchattime = gpGlobals->time; // KWo - 27.03.2010
g_fLastChatTime = gpGlobals->time; // KWo - 27.03.2010
return true;
}
pBot->SaytextBuffer.iEntityIndex = -1;
pBot->SaytextBuffer.szSayText[0] = 0x0;
}
}
return false;
}