-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_chat.cpp
560 lines (467 loc) · 18.3 KB
/
bot_chat.cpp
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* The GINA Bot - a computer opponent for Valve's FPS game Half-Life
* Copyright (c) 2011, Wei Mingzhi <whistler_wmz@users.sf.net>
*
* This file is part of The GINA Bot.
*
* The GINA Bot is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* The GINA Bot is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with The GINA Bot; if not, visit <http://www.gnu.org/licenses>.
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
/*******************************************************
* bot_chat.cpp *
*******************************************************
* Purpose: Bots chatting code *
* Engine-specific: no *
*******************************************************/
#include "main.h"
CBotChat::CBotChat(CBaseBot *pBot):
m_pBot(pBot),
m_flChatTime(0),
m_strMsgBuffer("")
{
}
CBotChat::~CBotChat()
{
}
// Returns if the bot is at a good position to chat.
bool CBotChat::IsGoodChatPosition()
{
if (!m_pBot->IsAlive())
return true; // if the bot is dead all positions are valid
if (m_pBot->IsInWater())
return false; // do not chat if under water
return m_pBot->IsOnFloor(); // must be standing on the ground
}
void CBotChat::ReplyToMessage(CClient *pFrom, const char *msg)
{
if (m_flChatTime > g_pServer->GetTime())
return; // we already have something to say
// generate a reply for this message
char *p = g_pServer->BotManager()->chat.GenerateReply(m_pBot, pFrom, msg);
// if we have found a reply
if (p) {
PrepareMessage(p); // reply to the message
}
}
void CBotChat::PrepareMessage(const char *msg, bool is_team_msg)
{
if (m_flChatTime > g_pServer->GetTime())
return; // we already have something to say
m_strMsgBuffer = msg; // store it into our buffer
// and delay sometime to say it based on length of the message and bot skill
m_flChatTime = g_pServer->GetTime() + (0.005 * m_pBot->m_pProfile->skill * strlen(msg));
m_fIsTeamMsg = is_team_msg;
}
void CBotChat::Think()
{
if (m_flChatTime > 0 && m_flChatTime < g_pServer->GetTime()) {
// It's the time to say the message now
// if we are at the good position, send the message in the buffer
if (IsGoodChatPosition()) {
if (m_fIsTeamMsg)
m_pBot->SayTeam(m_strMsgBuffer.c_str());
else
m_pBot->Say(m_strMsgBuffer.c_str());
m_flChatTime = 0; // don't say it again
}
}
}
void CChatItem::AddMessage(char *sz)
{
msg.push_back(std::string(sz)); // add it to the list
}
const char *CChatItem::PickMessage()
{
assert(msg.size());
// just pick a random message from the list
return msg[RandomLong(0, msg.size() - 1)].c_str();
}
const char *CChatReply::IsValidReply(const char *sentence)
{
char upper_sentence[256], *p = upper_sentence;
strncpy(upper_sentence, sentence, 256);
// convert the sentence to upper case
while (*p) {
*p = toupper(*p);
p++;
}
// search in the keyword list
for (int i = 0; i < key.size(); i++) {
const char *p = strstr(upper_sentence, key[i].c_str());
if (p) {
return p;
}
}
return NULL;
}
void CChatReply::AddKeyword(char *sz)
{
char upper_sz[256], *p = upper_sz;
strncpy(upper_sz, sz, 256);
// convert the string to upper case
while (*p) {
*p = toupper(*p);
p++;
}
trim(upper_sz); // remove the leading and trailing blanks
key.push_back(std::string(upper_sz)); // add it to the list
}
void CChatReply::AddMessage(char *sz)
{
msg.push_back(std::string(sz));
}
const char *CChatReply::PickMessage(void)
{
if (msg.size() <= 0)
return NULL; // reliability check
// just pick a random message from the list
return msg[RandomLong(0, msg.size() - 1)].c_str();
}
//==============================================================
// Chat Manager
//==============================================================
CChatManager::CChatManager()
{
LoadBotChat();
}
CChatManager::~CChatManager()
{
}
// load the bot chat data file
void CChatManager::LoadBotChat()
{
char str[256];
FILE *fp = fopen(g_General.BuildFileName("botchat.txt"), "r");
if (!fp) {
printf("BOT: cannot load file botchat.txt !\n");
return;
}
bool fIsReply = false, fLoadingKeywords = false;
while (fgets(str, 256, fp)) {
trim(str); // trim all the blanks or linefeeds
// skip all comment lines or empty lines
if (!str[0] || str[0] == ';' || str[0] == '/' || str[0] == '#')
continue;
// check if this is a section line (e.g., [SECTION])
int length = strlen(str);
if (str[0] == '[' && str[length - 1] == ']') {
int i;
char section[256];
section[0] = '\0';
strcpy(section, &str[1]);
section[length - 2] = 0; // remove the ]
trim(section);
// convert the key name to upper case
for (i = 0; i < length - 2; i++) {
section[i] = toupper(section[i]);
}
if (strcmpi(section, "REPLIES") == 0) {
fIsReply = true;
} else {
fIsReply = false;
unsigned short usCrc = CRC_Block((unsigned char *)section, length - 2); // CRC the section name
// add a session
m_ChatItem.push_back(CChatItem(usCrc));
}
} else if (fIsReply) {
if (strncmp(str, "@KEY ", 5) == 0) {
// This is a keyword line. Add the keyword to our list
if (!fLoadingKeywords) {
m_ChatReply.push_back(CChatReply());
fLoadingKeywords = true;
}
m_ChatReply[m_ChatReply.size() - 1].AddKeyword(&str[5]);
} else if (m_ChatReply.size() > 0) {
// This is a reply message line. Add it to our list
m_ChatReply[m_ChatReply.size() - 1].AddMessage(str);
fLoadingKeywords = false;
}
} else if (m_ChatItem.size() > 0) {
// This is a chat message line. Add it to our list
m_ChatItem[m_ChatItem.size() - 1].AddMessage(str);
}
}
fclose(fp); // close the file
}
// this function gets called when the client "from" sends a chat message "message"
// to the client "to"
void CChatManager::ChatMessage(CClient *from, CClient *to, const char *message)
{
if (!to->IsBot())
return; // ignore this message if the destination is NOT a bot
CBaseBot *pBot = (CBaseBot *)to;
if (!pBot->Chat()->IsGoodChatPosition())
return; // not a good chat position; don't proceed
// only process the message randomly, don't reply to every message
if (RandomLong(1, 100) < 50)
pBot->Chat()->ReplyToMessage(from, message);
}
// Generate a chat message from a specified message type, and return
// the pointer of the generated message
char *CChatManager::GenerateChat(CBaseBot *pClient, const char *type)
{
static char msg[256]; // for the chat message
msg[0] = '\0'; // initialize the string
unsigned short usCrc = CRC_Block((unsigned char *)type, strlen(type));
// find if we already have this section
int found;
for (found = 0; found < m_ChatItem.size(); found++) {
if (m_ChatItem[found].type == usCrc)
break;
}
if (found < m_ChatItem.size()) {
char sz[256];
strncpy(sz, m_ChatItem[found].PickMessage(), 256);
int length = strlen(sz);
char *p = sz;
while (p != NULL && *p) {
if (*p == '%') {
char type = *(++p); // skip to next character to get the type
int hiscore = -9999, j, count;
CClient *pPickedClient = NULL;
char buffer[32];
switch (type) {
case '%':
strncat(msg, "%", 256);
break;
case 'n': // bot name
strncpy(buffer, pClient->GetNetName(), 32);
HumanizeName(buffer);
strncat(msg, buffer, 256); // append it to the message
break;
case 'r': // random opponent
j = RandomLong(0, g_pServer->GetMaxClients());
for (count = 0; count < g_pServer->GetMaxClients(); count++, j++) {
CClient *p = g_pServer->m_rgpClients[j];
if (p != pClient && p->IsValid()) {
strncpy(buffer, p->GetNetName(), 32);
HumanizeName(buffer);
strncat(msg, buffer, 256); // append it to the message
break;
}
}
if (count >= g_pServer->GetMaxClients())
return NULL; // can't find any player, don't say anything
break;
case 'f': // opponent in first place
for (j = 0; j < g_pServer->GetMaxClients(); j++) {
CClient *p = g_pServer->m_rgpClients[j];
if (p == pClient || !p->IsValid())
continue;
if (p->GetFrags() >= hiscore) {
hiscore = p->GetFrags();
pPickedClient = p;
}
}
if (pPickedClient) {
strncpy(buffer, pPickedClient->GetNetName(), 32);
HumanizeName(buffer); // humanize the name
strncat(msg, buffer, 256); // append it to the message
} else {
return NULL; // can't find such a player, don't say anything
}
break;
case 'l': // opponent in last place
hiscore = 9999;
for (j = 0; j < g_pServer->GetMaxClients(); j++) {
CClient *p = g_pServer->m_rgpClients[j];
if (p == pClient || !p->IsValid())
continue;
if (p->GetFrags() <= hiscore) {
hiscore = p->GetFrags();
pPickedClient = p;
}
}
if (pPickedClient) {
strncpy(buffer, pPickedClient->GetNetName(), 32);
HumanizeName(buffer); // humanize the name
strncat(msg, buffer, 256); // append it to the message
} else {
return NULL; // can't find such a player, don't say anything
}
break;
case 'm': // map title
strncat(msg, g_pServer->GetMapName(), 256); // get the map name
break;
case 'v': // last victim of this bot
if (pClient->Chat()->m_strLastVictim.length() <= 0)
return NULL; // we don't have a victim yet, don't say anything
strncat(msg, pClient->Chat()->m_strLastVictim.c_str(), 256);
break;
case 'k': // last player killed this bot
if (pClient->Chat()->m_strLastKiller.length() <= 0)
return NULL; // this bot hasn't been killed yet, don't say anything
strncat(msg, pClient->Chat()->m_strLastKiller.c_str(), 256);
break;
}
p++; // skip to the next character
continue;
}
char *prev = p;
p = strchr(p, '%'); // search for the next "%" sign
if (p) {
*p = '\0'; // if found, terminate the string
}
strncat(msg, prev, 256); // append the remaining text
if (p) {
*p = '%'; // restore the '%'
}
}
HumanizeChat(msg); // 'Humanize' the chat message
return msg;
}
return NULL; // not found
}
// Generate a reply message from the input message, and return the
// pointer of the reply message
char *CChatManager::GenerateReply(CBaseBot *pClient, CClient *pFrom,
const char *msg)
{
static char message[256]; // for the reply message
message[0] = '\0'; // initialize the string
char *p;
int i;
// find a reply block with the correct keyword
for (i = 0; i < m_ChatReply.size(); i++) {
if (m_ChatReply[i].IsValidReply(msg))
break;
}
if (i >= m_ChatReply.size()) {
// no replies for this sentence are found
if (RandomLong(1, 100) > 50) {
// about half the time use a general reply
if ((p = GenerateChat(pClient, "GENERAL_REPLIES")) != NULL) {
strncpy(message, p, 256);
} else {
return NULL; // No general replies
}
} else {
return NULL; // don't reply to this message
}
} else {
// we have replies for this sentence, generate the message now
p = (char *)m_ChatReply[i].PickMessage();
if (!p) {
return NULL; // no message is available, don't proceed
}
while (p && *p) {
if (*p == '%') {
char type = *(++p); // skip to next character to get the type
int hiscore = -9999, j, count;
CClient *pPickedClient = NULL;
char buffer[32];
switch (type) {
case '%':
strncat(message, "%", 256);
break;
case 'n': // bot name
strncpy(buffer, pClient->GetNetName(), 32);
HumanizeName(buffer);
strncat(message, buffer, 256); // append it to the message
break;
case 'r': // random opponent
j = RandomLong(0, g_pServer->GetMaxClients());
for (count = 0; count < g_pServer->GetMaxClients(); count++, j++) {
CClient *p = g_pServer->m_rgpClients[j];
if (p != pClient && p->IsValid()) {
strncpy(buffer, p->GetNetName(), 32);
HumanizeName(buffer);
strncat(message, buffer, 256); // append it to the message
break;
}
}
if (count >= g_pServer->GetMaxClients())
return NULL; // can't find any player, don't say anything
break;
case 'f': // opponent in first place
for (j = 0; j < g_pServer->GetMaxClients(); j++) {
CClient *p = g_pServer->m_rgpClients[j];
if (p == pClient || !p->IsValid())
continue;
if (p->GetFrags() >= hiscore) {
hiscore = p->GetFrags();
pPickedClient = p;
}
}
if (pPickedClient) {
strncpy(buffer, pPickedClient->GetNetName(), 32);
HumanizeName(buffer); // humanize the name
strncat(message, buffer, 256); // append it to the message
} else {
return NULL; // can't find such a player, don't say anything
}
break;
case 'l': // opponent in last place
hiscore = 9999;
for (j = 0; j < g_pServer->GetMaxClients(); j++) {
CClient *p = g_pServer->m_rgpClients[j];
if (p == pClient || !p->IsValid())
continue;
if (p->GetFrags() <= hiscore) {
hiscore = p->GetFrags();
pPickedClient = p;
}
}
if (pPickedClient) {
strncpy(buffer, pPickedClient->GetNetName(), 32);
HumanizeName(buffer); // humanize the name
strncat(message, buffer, 256); // append it to the message
} else {
return NULL; // can't find such a player, don't say anything
}
break;
case 'v': // last victim of this bot
if (pClient->Chat()->m_strLastVictim.length() <= 0)
return NULL; // we don't have a victim yet
strncat(message, pClient->Chat()->m_strLastVictim.c_str(), 256);
break;
case 'k': // last player killed this bot
if (pClient->Chat()->m_strLastKiller.length() <= 0)
return NULL; // this bot hasn't been killed yet
strncat(message, pClient->Chat()->m_strLastKiller.c_str(), 256);
break;
case 'm': // map title
strncat(message, g_pServer->GetMapName(), 256); // get the map name
break;
// reply specific items
case 's': // player to reply
strncat(message, pFrom->GetNetName(), 256);
break;
}
p++; // skip to the next character
continue;
}
char *prev = p;
p = strchr(p, '%'); // search for the next "%" sign
if (p) {
*p = '\0'; // if found, terminate the string
}
strncat(message, prev, 256); // append the remaining text
if (p) {
*p = '%'; // restore the '%'
}
}
HumanizeChat(message); // 'humanize' the chat message
}
return message; // return the message
}