Debian Codegraph @

code/botlib/be_ai_chat.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 /*****************************************************************************
24 * name: be_ai_chat.c
25 *
26 * desc: bot chat AI
27 *
28 * $Archive: /MissionPack/code/botlib/be_ai_chat.c $
29 *
30 *****************************************************************************/
31
32 #include "../qcommon/q_shared.h"
33 #include "l_memory.h"
34 #include "l_libvar.h"
35 #include "l_script.h"
36 #include "l_precomp.h"
37 #include "l_struct.h"
38 #include "l_utils.h"
39 #include "l_log.h"
40 #include "aasfile.h"
41 #include "botlib.h"
42 #include "be_aas.h"
43 #include "be_aas_funcs.h"
44 #include "be_interface.h"
45 #include "be_ea.h"
46 #include "be_ai_chat.h"
47
48
49 //escape character
50 #define ESCAPE_CHAR 0x01 //'_'
51 //
52 // "hi ", people, " ", 0, " entered the game"
53 //becomes:
54 // "hi _rpeople_ _v0_ entered the game"
55 //
56
57 //match piece types
58 #define MT_VARIABLE 1 //variable match piece
59 #define MT_STRING 2 //string match piece
60 //reply chat key flags
61 #define RCKFL_AND 1 //key must be present
62 #define RCKFL_NOT 2 //key must be absent
63 #define RCKFL_NAME 4 //name of bot must be present
64 #define RCKFL_STRING 8 //key is a string
65 #define RCKFL_VARIABLES 16 //key is a match template
66 #define RCKFL_BOTNAMES 32 //key is a series of botnames
67 #define RCKFL_GENDERFEMALE 64 //bot must be female
68 #define RCKFL_GENDERMALE 128 //bot must be male
69 #define RCKFL_GENDERLESS 256 //bot must be genderless
70 //time to ignore a chat message after using it
71 #define CHATMESSAGE_RECENTTIME 20
72
73 //the actuall chat messages
74 typedef struct bot_chatmessage_s
75 {
76 char *chatmessage; //chat message string
77 float time; //last time used
78 struct bot_chatmessage_s *next; //next chat message in a list
79 } bot_chatmessage_t;
80 //bot chat type with chat lines
81 typedef struct bot_chattype_s
82 {
83 char name[MAX_CHATTYPE_NAME];
84 int numchatmessages;
85 bot_chatmessage_t *firstchatmessage;
86 struct bot_chattype_s *next;
87 } bot_chattype_t;
88 //bot chat lines
89 typedef struct bot_chat_s
90 {
91 bot_chattype_t *types;
92 } bot_chat_t;
93
94 //random string
95 typedef struct bot_randomstring_s
96 {
97 char *string;
98 struct bot_randomstring_s *next;
99 } bot_randomstring_t;
100 //list with random strings
101 typedef struct bot_randomlist_s
102 {
103 char *string;
104 int numstrings;
105 bot_randomstring_t *firstrandomstring;
106 struct bot_randomlist_s *next;
107 } bot_randomlist_t;
108
109 //synonym
110 typedef struct bot_synonym_s
111 {
112 char *string;
113 float weight;
114 struct bot_synonym_s *next;
115 } bot_synonym_t;
116 //list with synonyms
117 typedef struct bot_synonymlist_s
118 {
119 unsigned long int context;
120 float totalweight;
121 bot_synonym_t *firstsynonym;
122 struct bot_synonymlist_s *next;
123 } bot_synonymlist_t;
124
125 //fixed match string
126 typedef struct bot_matchstring_s
127 {
128 char *string;
129 struct bot_matchstring_s *next;
130 } bot_matchstring_t;
131
132 //piece of a match template
133 typedef struct bot_matchpiece_s
134 {
135 int type;
136 bot_matchstring_t *firststring;
137 int variable;
138 struct bot_matchpiece_s *next;
139 } bot_matchpiece_t;
140 //match template
141 typedef struct bot_matchtemplate_s
142 {
143 unsigned long int context;
144 int type;
145 int subtype;
146 bot_matchpiece_t *first;
147 struct bot_matchtemplate_s *next;
148 } bot_matchtemplate_t;
149
150 //reply chat key
151 typedef struct bot_replychatkey_s
152 {
153 int flags;
154 char *string;
155 bot_matchpiece_t *match;
156 struct bot_replychatkey_s *next;
157 } bot_replychatkey_t;
158 //reply chat
159 typedef struct bot_replychat_s
160 {
161 bot_replychatkey_t *keys;
162 float priority;
163 int numchatmessages;
164 bot_chatmessage_t *firstchatmessage;
165 struct bot_replychat_s *next;
166 } bot_replychat_t;
167
168 //string list
169 typedef struct bot_stringlist_s
170 {
171 char *string;
172 struct bot_stringlist_s *next;
173 } bot_stringlist_t;
174
175 //chat state of a bot
176 typedef struct bot_chatstate_s
177 {
178 int gender; //0=it, 1=female, 2=male
179 int client; //client number
180 char name[32]; //name of the bot
181 char chatmessage[MAX_MESSAGE_SIZE];
182 int handle;
183 //the console messages visible to the bot
184 bot_consolemessage_t *firstmessage; //first message is the first typed message
185 bot_consolemessage_t *lastmessage; //last message is the last typed message, bottom of console
186 //number of console messages stored in the state
187 int numconsolemessages;
188 //the bot chat lines
189 bot_chat_t *chat;
190 } bot_chatstate_t;
191
192 typedef struct {
193 bot_chat_t *chat;
194 char filename[MAX_QPATH];
195 char chatname[MAX_QPATH];
196 } bot_ichatdata_t;
197
198 bot_ichatdata_t *ichatdata[MAX_CLIENTS];
199
200 bot_chatstate_t *botchatstates[MAX_CLIENTS+1];
201 //console message heap
202 bot_consolemessage_t *consolemessageheap = NULL;
203 bot_consolemessage_t *freeconsolemessages = NULL;
204 //list with match strings
205 bot_matchtemplate_t *matchtemplates = NULL;
206 //list with synonyms
207 bot_synonymlist_t *synonyms = NULL;
208 //list with random strings
209 bot_randomlist_t *randomstrings = NULL;
210 //reply chats
211 bot_replychat_t *replychats = NULL;
212
213 //========================================================================
214 //
215 // Parameter: -
216 // Returns: -
217 // Changes Globals: -
218 //========================================================================
219 bot_chatstate_t *BotChatStateFromHandle(int handle)
220 {
221 if (handle <= 0 || handle > MAX_CLIENTS)
222 {
223 botimport.Print(PRT_FATAL, "chat state handle %d out of range\n", handle);
224 return NULL;
225 } //end if
226 if (!botchatstates[handle])
227 {
228 botimport.Print(PRT_FATAL, "invalid chat state %d\n", handle);
229 return NULL;
230 } //end if
231 return botchatstates[handle];
232 } //end of the function BotChatStateFromHandle
233 //===========================================================================
234 // initialize the heap with unused console messages
235 //
236 // Parameter: -
237 // Returns: -
238 // Changes Globals: -
239 //===========================================================================
240 void InitConsoleMessageHeap(void)
241 {
242 int i, max_messages;
243
244 if (consolemessageheap) FreeMemory(consolemessageheap);
245 //
246 max_messages = (int) LibVarValue("max_messages", "1024");
247 consolemessageheap = (bot_consolemessage_t *) GetClearedHunkMemory(max_messages *
248 sizeof(bot_consolemessage_t));
249 consolemessageheap[0].prev = NULL;
250 consolemessageheap[0].next = &consolemessageheap[1];
251 for (i = 1; i < max_messages-1; i++)
252 {
253 consolemessageheap[i].prev = &consolemessageheap[i - 1];
254 consolemessageheap[i].next = &consolemessageheap[i + 1];
255 } //end for
256 consolemessageheap[max_messages-1].prev = &consolemessageheap[max_messages-2];
257 consolemessageheap[max_messages-1].next = NULL;
258 //pointer to the free console messages
259 freeconsolemessages = consolemessageheap;
260 } //end of the function InitConsoleMessageHeap
261 //===========================================================================
262 // allocate one console message from the heap
263 //
264 // Parameter: -
265 // Returns: -
266 // Changes Globals: -
267 //===========================================================================
268 bot_consolemessage_t *AllocConsoleMessage(void)
269 {
270 bot_consolemessage_t *message;
271 message = freeconsolemessages;
272 if (freeconsolemessages) freeconsolemessages = freeconsolemessages->next;
273 if (freeconsolemessages) freeconsolemessages->prev = NULL;
274 return message;
275 } //end of the function AllocConsoleMessage
276 //===========================================================================
277 // deallocate one console message from the heap
278 //
279 // Parameter: -
280 // Returns: -
281 // Changes Globals: -
282 //===========================================================================
283 void FreeConsoleMessage(bot_consolemessage_t *message)
284 {
285 if (freeconsolemessages) freeconsolemessages->prev = message;
286 message->prev = NULL;
287 message->next = freeconsolemessages;
288 freeconsolemessages = message;
289 } //end of the function FreeConsoleMessage
290 //===========================================================================
291 //
292 // Parameter: -
293 // Returns: -
294 // Changes Globals: -
295 //===========================================================================
296 void BotRemoveConsoleMessage(int chatstate, int handle)
297 {
298 bot_consolemessage_t *m, *nextm;
299 bot_chatstate_t *cs;
300
301 cs = BotChatStateFromHandle(chatstate);
302 if (!cs) return;
303
304 for (m = cs->firstmessage; m; m = nextm)
305 {
306 nextm = m->next;
307 if (m->handle == handle)
308 {
309 if (m->next) m->next->prev = m->prev;
310 else cs->lastmessage = m->prev;
311 if (m->prev) m->prev->next = m->next;
312 else cs->firstmessage = m->next;
313
314 FreeConsoleMessage(m);
315 cs->numconsolemessages--;
316 break;
317 } //end if
318 } //end for
319 } //end of the function BotRemoveConsoleMessage
320 //===========================================================================
321 //
322 // Parameter: -
323 // Returns: -
324 // Changes Globals: -
325 //===========================================================================
326 void BotQueueConsoleMessage(int chatstate, int type, char *message)
327 {
328 bot_consolemessage_t *m;
329 bot_chatstate_t *cs;
330
331 cs = BotChatStateFromHandle(chatstate);
332 if (!cs) return;
333
334 m = AllocConsoleMessage();
335 if (!m)
336 {
337 botimport.Print(PRT_ERROR, "empty console message heap\n");
338 return;
339 } //end if
340 cs->handle++;
341 if (cs->handle <= 0 || cs->handle > 8192) cs->handle = 1;
342 m->handle = cs->handle;
343 m->time = AAS_Time();
344 m->type = type;
345 Q_strncpyz(m->message, message, MAX_MESSAGE_SIZE);
346 m->next = NULL;
347 if (cs->lastmessage)
348 {
349 cs->lastmessage->next = m;
350 m->prev = cs->lastmessage;
351 cs->lastmessage = m;
352 } //end if
353 else
354 {
355 cs->lastmessage = m;
356 cs->firstmessage = m;
357 m->prev = NULL;
358 } //end if
359 cs->numconsolemessages++;
360 } //end of the function BotQueueConsoleMessage
361 //===========================================================================
362 //
363 // Parameter: -
364 // Returns: -
365 // Changes Globals: -
366 //===========================================================================
367 int BotNextConsoleMessage(int chatstate, bot_consolemessage_t *cm)
368 {
369 bot_chatstate_t *cs;
370 bot_consolemessage_t *firstmsg;
371
372 cs = BotChatStateFromHandle(chatstate);
373 if (!cs) return 0;
374 if ((firstmsg = cs->firstmessage))
375 {
376 cm->handle = firstmsg->handle;
377 cm->time = firstmsg->time;
378 cm->type = firstmsg->type;
379 Q_strncpyz(cm->message, firstmsg->message,
380 sizeof(cm->message));
381
382 /* We omit setting the two pointers in cm because pointer
383 * size in the VM differs between the size in the engine on
384 * 64 bit machines, which would lead to a buffer overflow if
385 * this functions is called from the VM. The pointers are
386 * of no interest to functions calling
387 * BotNextConsoleMessage anyways.
388 */
389
390 return cm->handle;
391 } //end if
392 return 0;
393 } //end of the function BotConsoleMessage
394 //===========================================================================
395 //
396 // Parameter: -
397 // Returns: -
398 // Changes Globals: -
399 //===========================================================================
400 int BotNumConsoleMessages(int chatstate)
401 {
402 bot_chatstate_t *cs;
403
404 cs = BotChatStateFromHandle(chatstate);
405 if (!cs) return 0;
406 return cs->numconsolemessages;
407 } //end of the function BotNumConsoleMessages
408 //===========================================================================
409 //
410 // Parameter: -
411 // Returns: -
412 // Changes Globals: -
413 //===========================================================================
414 int IsWhiteSpace(char c)
415 {
416 if ((c >= 'a' && c <= 'z')
417 || (c >= 'A' && c <= 'Z')
418 || (c >= '0' && c <= '9')
419 || c == '(' || c == ')'
420 || c == '?' || c == ':'
421 || c == '\''|| c == '/'
422 || c == ',' || c == '.'
423 || c == '[' || c == ']'
424 || c == '-' || c == '_'
425 || c == '+' || c == '=') return qfalse;
426 return qtrue;
427 } //end of the function IsWhiteSpace
428 //===========================================================================
429 //
430 // Parameter: -
431 // Returns: -
432 // Changes Globals: -
433 //===========================================================================
434 void BotRemoveTildes(char *message)
435 {
436 int i;
437
438 //remove all tildes from the chat message
439 for (i = 0; message[i]; i++)
440 {
441 if (message[i] == '~')
442 {
443 memmove(&message[i], &message[i+1], strlen(&message[i+1])+1);
444 } //end if
445 } //end for
446 } //end of the function BotRemoveTildes
447 //===========================================================================
448 //
449 // Parameter: -
450 // Returns: -
451 // Changes Globals: -
452 //===========================================================================
453 void UnifyWhiteSpaces(char *string)
454 {
455 char *ptr, *oldptr;
456
457 for (ptr = oldptr = string; *ptr; oldptr = ptr)
458 {
459 while(*ptr && IsWhiteSpace(*ptr)) ptr++;
460 if (ptr > oldptr)
461 {
462 //if not at the start and not at the end of the string
463 //write only one space
464 if (oldptr > string && *ptr) *oldptr++ = ' ';
465 //remove all other white spaces
466 if (ptr > oldptr) memmove(oldptr, ptr, strlen(ptr)+1);
467 } //end if
468 while(*ptr && !IsWhiteSpace(*ptr)) ptr++;
469 } //end while
470 } //end of the function UnifyWhiteSpaces
471 //===========================================================================
472 //
473 // Parameter: -
474 // Returns: -
475 // Changes Globals: -
476 //===========================================================================
477 int StringContains(char *str1, char *str2, int casesensitive)
478 {
479 int len, i, j, index;
480
481 if (str1 == NULL || str2 == NULL) return -1;
482
483 len = strlen(str1) - strlen(str2);
484 index = 0;
485 for (i = 0; i <= len; i++, str1++, index++)
486 {
487 for (j = 0; str2[j]; j++)
488 {
489 if (casesensitive)
490 {
491 if (str1[j] != str2[j]) break;
492 } //end if
493 else
494 {
495 if (toupper(str1[j]) != toupper(str2[j])) break;
496 } //end else
497 } //end for
498 if (!str2[j]) return index;
499 } //end for
500 return -1;
501 } //end of the function StringContains
502 //===========================================================================
503 //
504 // Parameter: -
505 // Returns: -
506 // Changes Globals: -
507 //===========================================================================
508 char *StringContainsWord(char *str1, char *str2, int casesensitive)
509 {
510 int len, i, j;
511
512 len = strlen(str1) - strlen(str2);
513 for (i = 0; i <= len; i++, str1++)
514 {
515 //if not at the start of the string
516 if (i)
517 {
518 //skip to the start of the next word
519 while(*str1 && *str1 != ' ' && *str1 != '.' && *str1 != ',' && *str1 != '!') str1++;
520 if (!*str1) break;
521 str1++;
522 } //end for
523 //compare the word
524 for (j = 0; str2[j]; j++)
525 {
526 if (casesensitive)
527 {
528 if (str1[j] != str2[j]) break;
529 } //end if
530 else
531 {
532 if (toupper(str1[j]) != toupper(str2[j])) break;
533 } //end else
534 } //end for
535 //if there was a word match
536 if (!str2[j])
537 {
538 //if the first string has an end of word
539 if (!str1[j] || str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '!') return str1;
540 } //end if
541 } //end for
542 return NULL;
543 } //end of the function StringContainsWord
544 //===========================================================================
545 //
546 // Parameter: -
547 // Returns: -
548 // Changes Globals: -
549 //===========================================================================
550 void StringReplaceWords(char *string, char *synonym, char *replacement)
551 {
552 char *str, *str2;
553
554 //find the synonym in the string
555 str = StringContainsWord(string, synonym, qfalse);
556 //if the synonym occurred in the string
557 while(str)
558 {
559 //if the synonym isn't part of the replacement which is already in the string
560 //useful for abbreviations
561 str2 = StringContainsWord(string, replacement, qfalse);
562 while(str2)
563 {
564 if (str2 <= str && str < str2 + strlen(replacement)) break;
565 str2 = StringContainsWord(str2+1, replacement, qfalse);
566 } //end while
567 if (!str2)
568 {
569 memmove(str + strlen(replacement), str+strlen(synonym), strlen(str+strlen(synonym))+1);
570 //append the synonum replacement
571 Com_Memcpy(str, replacement, strlen(replacement));
572 } //end if
573 //find the next synonym in the string
574 str = StringContainsWord(str+strlen(replacement), synonym, qfalse);
575 } //end if
576 } //end of the function StringReplaceWords
577 //===========================================================================
578 //
579 // Parameter: -
580 // Returns: -
581 // Changes Globals: -
582 //===========================================================================
583 void BotDumpSynonymList(bot_synonymlist_t *synlist)
584 {
585 FILE *fp;
586 bot_synonymlist_t *syn;
587 bot_synonym_t *synonym;
588
589 fp = Log_FilePointer();
590 if (!fp) return;
591 for (syn = synlist; syn; syn = syn->next)
592 {
593 fprintf(fp, "%ld : [", syn->context);
594 for (synonym = syn->firstsynonym; synonym; synonym = synonym->next)
595 {
596 fprintf(fp, "(\"%s\", %1.2f)", synonym->string, synonym->weight);
597 if (synonym->next) fprintf(fp, ", ");
598 } //end for
599 fprintf(fp, "]\n");
600 } //end for
601 } //end of the function BotDumpSynonymList
602 //===========================================================================
603 //
604 // Parameter: -
605 // Returns: -
606 // Changes Globals: -
607 //===========================================================================
608 bot_synonymlist_t *BotLoadSynonyms(char *filename)
609 {
610 int pass, size, contextlevel, numsynonyms;
611 unsigned long int context, contextstack[32];
612 char *ptr = NULL;
613 source_t *source;
614 token_t token;
615 bot_synonymlist_t *synlist, *lastsyn, *syn;
616 bot_synonym_t *synonym, *lastsynonym;
617
618 size = 0;
619 synlist = NULL; //make compiler happy
620 syn = NULL; //make compiler happy
621 synonym = NULL; //make compiler happy
622 //the synonyms are parsed in two phases
623 for (pass = 0; pass < 2; pass++)
624 {
625 //
626 if (pass && size) ptr = (char *) GetClearedHunkMemory(size);
627 //
628 PC_SetBaseFolder(BOTFILESBASEFOLDER);
629 source = LoadSourceFile(filename);
630 if (!source)
631 {
632 botimport.Print(PRT_ERROR, "couldn't load %s\n", filename);
633 return NULL;
634 } //end if
635 //
636 context = 0;
637 contextlevel = 0;
638 synlist = NULL; //list synonyms
639 lastsyn = NULL; //last synonym in the list
640 //
641 while(PC_ReadToken(source, &token))
642 {
643 if (token.type == TT_NUMBER)
644 {
645 context |= token.intvalue;
646 contextstack[contextlevel] = token.intvalue;
647 contextlevel++;
648 if (contextlevel >= 32)
649 {
650 SourceError(source, "more than 32 context levels");
651 FreeSource(source);
652 return NULL;
653 } //end if
654 if (!PC_ExpectTokenString(source, "{"))
655 {
656 FreeSource(source);
657 return NULL;
658 } //end if
659 } //end if
660 else if (token.type == TT_PUNCTUATION)
661 {
662 if (!strcmp(token.string, "}"))
663 {
664 contextlevel--;
665 if (contextlevel < 0)
666 {
667 SourceError(source, "too many }");
668 FreeSource(source);
669 return NULL;
670 } //end if
671 context &= ~contextstack[contextlevel];
672 } //end if
673 else if (!strcmp(token.string, "["))
674 {
675 size += sizeof(bot_synonymlist_t);
676 if (pass && ptr)
677 {
678 syn = (bot_synonymlist_t *) ptr;
679 ptr += sizeof(bot_synonymlist_t);
680 syn->context = context;
681 syn->firstsynonym = NULL;
682 syn->next = NULL;
683 if (lastsyn) lastsyn->next = syn;
684 else synlist = syn;
685 lastsyn = syn;
686 } //end if
687 numsynonyms = 0;
688 lastsynonym = NULL;
689 while(1)
690 {
691 size_t len;
692 if (!PC_ExpectTokenString(source, "(") ||
693 !PC_ExpectTokenType(source, TT_STRING, 0, &token))
694 {
695 FreeSource(source);
696 return NULL;
697 } //end if
698 StripDoubleQuotes(token.string);
699 if (strlen(token.string) <= 0)
700 {
701 SourceError(source, "empty string");
702 FreeSource(source);
703 return NULL;
704 } //end if
705 len = strlen(token.string) + 1;
706 len = PAD(len, sizeof(long));
707 size += sizeof(bot_synonym_t) + len;
708 if (pass && ptr)
709 {
710 synonym = (bot_synonym_t *) ptr;
711 ptr += sizeof(bot_synonym_t);
712 synonym->string = ptr;
713 ptr += len;
714 strcpy(synonym->string, token.string);
715 //
716 if (lastsynonym) lastsynonym->next = synonym;
717 else syn->firstsynonym = synonym;
718 lastsynonym = synonym;
719 } //end if
720 numsynonyms++;
721 if (!PC_ExpectTokenString(source, ",") ||
722 !PC_ExpectTokenType(source, TT_NUMBER, 0, &token) ||
723 !PC_ExpectTokenString(source, ")"))
724 {
725 FreeSource(source);
726 return NULL;
727 } //end if
728 if (pass && ptr)
729 {
730 synonym->weight = token.floatvalue;
731 syn->totalweight += synonym->weight;
732 } //end if
733 if (PC_CheckTokenString(source, "]")) break;
734 if (!PC_ExpectTokenString(source, ","))
735 {
736 FreeSource(source);
737 return NULL;
738 } //end if
739 } //end while
740 if (numsynonyms < 2)
741 {
742 SourceError(source, "synonym must have at least two entries");
743 FreeSource(source);
744 return NULL;
745 } //end if
746 } //end else
747 else
748 {
749 SourceError(source, "unexpected %s", token.string);
750 FreeSource(source);
751 return NULL;
752 } //end if
753 } //end else if
754 } //end while
755 //
756 FreeSource(source);
757 //
758 if (contextlevel > 0)
759 {
760 SourceError(source, "missing }");
761 return NULL;
762 } //end if
763 } //end for
764 botimport.Print(PRT_MESSAGE, "loaded %s\n", filename);
765 //
766 //BotDumpSynonymList(synlist);
767 //
768 return synlist;
769 } //end of the function BotLoadSynonyms
770 //===========================================================================
771 // replace all the synonyms in the string
772 //
773 // Parameter: -
774 // Returns: -
775 // Changes Globals: -
776 //===========================================================================
777 void BotReplaceSynonyms(char *string, unsigned long int context)
778 {
779 bot_synonymlist_t *syn;
780 bot_synonym_t *synonym;
781
782 for (syn = synonyms; syn; syn = syn->next)
783 {
784 if (!(syn->context & context)) continue;
785 for (synonym = syn->firstsynonym->next; synonym; synonym = synonym->next)
786 {
787 StringReplaceWords(string, synonym->string, syn->firstsynonym->string);
788 } //end for
789 } //end for
790 } //end of the function BotReplaceSynonyms
791 //===========================================================================
792 //
793 // Parameter: -
794 // Returns: -
795 // Changes Globals: -
796 //===========================================================================
797 void BotReplaceWeightedSynonyms(char *string, unsigned long int context)
798 {
799 bot_synonymlist_t *syn;
800 bot_synonym_t *synonym, *replacement;
801 float weight, curweight;
802
803 for (syn = synonyms; syn; syn = syn->next)
804 {
805 if (!(syn->context & context)) continue;
806 //choose a weighted random replacement synonym
807 weight = random() * syn->totalweight;
808 if (!weight) continue;
809 curweight = 0;
810 for (replacement = syn->firstsynonym; replacement; replacement = replacement->next)
811 {
812 curweight += replacement->weight;
813 if (weight < curweight) break;
814 } //end for
815 if (!replacement) continue;
816 //replace all synonyms with the replacement
817 for (synonym = syn->firstsynonym; synonym; synonym = synonym->next)
818 {
819 if (synonym == replacement) continue;
820 StringReplaceWords(string, synonym->string, replacement->string);
821 } //end for
822 } //end for
823 } //end of the function BotReplaceWeightedSynonyms
824 //===========================================================================
825 //
826 // Parameter: -
827 // Returns: -
828 // Changes Globals: -
829 //===========================================================================
830 void BotReplaceReplySynonyms(char *string, unsigned long int context)
831 {
832 char *str1, *str2, *replacement;
833 bot_synonymlist_t *syn;
834 bot_synonym_t *synonym;
835
836 for (str1 = string; *str1; )
837 {
838 //go to the start of the next word
839 while(*str1 && *str1 <= ' ') str1++;
840 if (!*str1) break;
841 //
842 for (syn = synonyms; syn; syn = syn->next)
843 {
844 if (!(syn->context & context)) continue;
845 for (synonym = syn->firstsynonym->next; synonym; synonym = synonym->next)
846 {
847 //if the synonym is not at the front of the string continue
848 str2 = StringContainsWord(str1, synonym->string, qfalse);
849 if (!str2 || str2 != str1) continue;
850 //
851 replacement = syn->firstsynonym->string;
852 //if the replacement IS in front of the string continue
853 str2 = StringContainsWord(str1, replacement, qfalse);
854 if (str2 && str2 == str1) continue;
855 //
856 memmove(str1 + strlen(replacement), str1+strlen(synonym->string),
857 strlen(str1+strlen(synonym->string)) + 1);
858 //append the synonum replacement
859 Com_Memcpy(str1, replacement, strlen(replacement));
860 //
861 break;
862 } //end for
863 //if a synonym has been replaced
864 if (synonym) break;
865 } //end for
866 //skip over this word
867 while(*str1 && *str1 > ' ') str1++;
868 if (!*str1) break;
869 } //end while
870 } //end of the function BotReplaceReplySynonyms
871 //===========================================================================
872 //
873 // Parameter: -
874 // Returns: -
875 // Changes Globals: -
876 //===========================================================================
877 int BotLoadChatMessage(source_t *source, char *chatmessagestring)
878 {
879 char *ptr;
880 token_t token;
881
882 ptr = chatmessagestring;
883 *ptr = 0;
884 //
885 while(1)
886 {
887 if (!PC_ExpectAnyToken(source, &token)) return qfalse;
888 //fixed string
889 if (token.type == TT_STRING)
890 {
891 StripDoubleQuotes(token.string);
892 if (strlen(ptr) + strlen(token.string) + 1 > MAX_MESSAGE_SIZE)
893 {
894 SourceError(source, "chat message too long");
895 return qfalse;
896 } //end if
897 strcat(ptr, token.string);
898 } //end else if
899 //variable string
900 else if (token.type == TT_NUMBER && (token.subtype & TT_INTEGER))
901 {
902 if (strlen(ptr) + 7 > MAX_MESSAGE_SIZE)
903 {
904 SourceError(source, "chat message too long");
905 return qfalse;
906 } //end if
907 sprintf(&ptr[strlen(ptr)], "%cv%ld%c", ESCAPE_CHAR, token.intvalue, ESCAPE_CHAR);
908 } //end if
909 //random string
910 else if (token.type == TT_NAME)
911 {
912 if (strlen(ptr) + 7 > MAX_MESSAGE_SIZE)
913 {
914 SourceError(source, "chat message too long");
915 return qfalse;
916 } //end if
917 sprintf(&ptr[strlen(ptr)], "%cr%s%c", ESCAPE_CHAR, token.string, ESCAPE_CHAR);
918 } //end else if
919 else
920 {
921 SourceError(source, "unknown message component %s", token.string);
922 return qfalse;
923 } //end else
924 if (PC_CheckTokenString(source, ";")) break;
925 if (!PC_ExpectTokenString(source, ",")) return qfalse;
926 } //end while
927 //
928 return qtrue;
929 } //end of the function BotLoadChatMessage
930 //===========================================================================
931 //
932 // Parameter: -
933 // Returns: -
934 // Changes Globals: -
935 //===========================================================================
936 void BotDumpRandomStringList(bot_randomlist_t *randomlist)
937 {
938 FILE *fp;
939 bot_randomlist_t *random;
940 bot_randomstring_t *rs;
941
942 fp = Log_FilePointer();
943 if (!fp) return;
944 for (random = randomlist; random; random = random->next)
945 {
946 fprintf(fp, "%s = {", random->string);
947 for (rs = random->firstrandomstring; rs; rs = rs->next)
948 {
949 fprintf(fp, "\"%s\"", rs->string);
950 if (rs->next) fprintf(fp, ", ");
951 else fprintf(fp, "}\n");
952 } //end for
953 } //end for
954 } //end of the function BotDumpRandomStringList
955 //===========================================================================
956 //
957 // Parameter: -
958 // Returns: -
959 // Changes Globals: -
960 //===========================================================================
961 bot_randomlist_t *BotLoadRandomStrings(char *filename)
962 {
963 int pass, size;
964 char *ptr = NULL, chatmessagestring[MAX_MESSAGE_SIZE];
965 source_t *source;
966 token_t token;
967 bot_randomlist_t *randomlist, *lastrandom, *random;
968 bot_randomstring_t *randomstring;
969
970 #ifdef DEBUG
971 int starttime = Sys_MilliSeconds();
972 #endif //DEBUG
973
974 size = 0;
975 randomlist = NULL;
976 random = NULL;
977 //the synonyms are parsed in two phases
978 for (pass = 0; pass < 2; pass++)
979 {
980 //
981 if (pass && size) ptr = (char *) GetClearedHunkMemory(size);
982 //
983 PC_SetBaseFolder(BOTFILESBASEFOLDER);
984 source = LoadSourceFile(filename);
985 if (!source)
986 {
987 botimport.Print(PRT_ERROR, "couldn't load %s\n", filename);
988 return NULL;
989 } //end if
990 //
991 randomlist = NULL; //list
992 lastrandom = NULL; //last
993 //
994 while(PC_ReadToken(source, &token))
995 {
996 size_t len;
997 if (token.type != TT_NAME)
998 {
999 SourceError(source, "unknown random %s", token.string);
1000 FreeSource(source);
1001 return NULL;
1002 } //end if
1003 len = strlen(token.string) + 1;
1004 len = PAD(len, sizeof(long));
1005 size += sizeof(bot_randomlist_t) + len;
1006 if (pass && ptr)
1007 {
1008 random = (bot_randomlist_t *) ptr;
1009 ptr += sizeof(bot_randomlist_t);
1010 random->string = ptr;
1011 ptr += len;
1012 strcpy(random->string, token.string);
1013 random->firstrandomstring = NULL;
1014 random->numstrings = 0;
1015 //
1016 if (lastrandom) lastrandom->next = random;
1017 else randomlist = random;
1018 lastrandom = random;
1019 } //end if
1020 if (!PC_ExpectTokenString(source, "=") ||
1021 !PC_ExpectTokenString(source, "{"))
1022 {
1023 FreeSource(source);
1024 return NULL;
1025 } //end if
1026 while(!PC_CheckTokenString(source, "}"))
1027 {
1028 if (!BotLoadChatMessage(source, chatmessagestring))
1029 {
1030 FreeSource(source);
1031 return NULL;
1032 } //end if
1033 len = strlen(chatmessagestring) + 1;
1034 len = PAD(len, sizeof(long));
1035 size += sizeof(bot_randomstring_t) + len;
1036 if (pass && ptr)
1037 {
1038 randomstring = (bot_randomstring_t *) ptr;
1039 ptr += sizeof(bot_randomstring_t);
1040 randomstring->string = ptr;
1041 ptr += len;
1042 strcpy(randomstring->string, chatmessagestring);
1043 //
1044 random->numstrings++;
1045 randomstring->next = random->firstrandomstring;
1046 random->firstrandomstring = randomstring;
1047 } //end if
1048 } //end while
1049 } //end while
1050 //free the source after one pass
1051 FreeSource(source);
1052 } //end for
1053 botimport.Print(PRT_MESSAGE, "loaded %s\n", filename);
1054 //
1055 #ifdef DEBUG
1056 botimport.Print(PRT_MESSAGE, "random strings %d msec\n", Sys_MilliSeconds() - starttime);
1057 //BotDumpRandomStringList(randomlist);
1058 #endif //DEBUG
1059 //
1060 return randomlist;
1061 } //end of the function BotLoadRandomStrings
1062 //===========================================================================
1063 //
1064 // Parameter: -
1065 // Returns: -
1066 // Changes Globals: -
1067 //===========================================================================
1068 char *RandomString(char *name)
1069 {
1070 bot_randomlist_t *random;
1071 bot_randomstring_t *rs;
1072 int i;
1073
1074 for (random = randomstrings; random; random = random->next)
1075 {
1076 if (!strcmp(random->string, name))
1077 {
1078 i = random() * random->numstrings;
1079 for (rs = random->firstrandomstring; rs; rs = rs->next)
1080 {
1081 if (--i < 0) break;
1082 } //end for
1083 if (rs)
1084 {
1085 return rs->string;
1086 } //end if
1087 } //end for
1088 } //end for
1089 return NULL;
1090 } //end of the function RandomString
1091 //===========================================================================
1092 //
1093 // Parameter: -
1094 // Returns: -
1095 // Changes Globals: -
1096 //===========================================================================
1097 void BotDumpMatchTemplates(bot_matchtemplate_t *matches)
1098 {
1099 FILE *fp;
1100 bot_matchtemplate_t *mt;
1101 bot_matchpiece_t *mp;
1102 bot_matchstring_t *ms;
1103
1104 fp = Log_FilePointer();
1105 if (!fp) return;
1106 for (mt = matches; mt; mt = mt->next)
1107 {
1108 fprintf(fp, "{ " );
1109 for (mp = mt->first; mp; mp = mp->next)
1110 {
1111 if (mp->type == MT_STRING)
1112 {
1113 for (ms = mp->firststring; ms; ms = ms->next)
1114 {
1115 fprintf(fp, "\"%s\"", ms->string);
1116 if (ms->next) fprintf(fp, "|");
1117 } //end for
1118 } //end if
1119 else if (mp->type == MT_VARIABLE)
1120 {
1121 fprintf(fp, "%d", mp->variable);
1122 } //end else if
1123 if (mp->next) fprintf(fp, ", ");
1124 } //end for
1125 fprintf(fp, " = (%d, %d);}\n", mt->type, mt->subtype);
1126 } //end for
1127 } //end of the function BotDumpMatchTemplates
1128 //===========================================================================
1129 //
1130 // Parameter: -
1131 // Returns: -
1132 // Changes Globals: -
1133 //===========================================================================
1134 void BotFreeMatchPieces(bot_matchpiece_t *matchpieces)
1135 {
1136 bot_matchpiece_t *mp, *nextmp;
1137 bot_matchstring_t *ms, *nextms;
1138
1139 for (mp = matchpieces; mp; mp = nextmp)
1140 {
1141 nextmp = mp->next;
1142 if (mp->type == MT_STRING)
1143 {
1144 for (ms = mp->firststring; ms; ms = nextms)
1145 {
1146 nextms = ms->next;
1147 FreeMemory(ms);
1148 } //end for
1149 } //end if
1150 FreeMemory(mp);
1151 } //end for
1152 } //end of the function BotFreeMatchPieces
1153 //===========================================================================
1154 //
1155 // Parameter: -
1156 // Returns: -
1157 // Changes Globals: -
1158 //===========================================================================
1159 bot_matchpiece_t *BotLoadMatchPieces(source_t *source, char *endtoken)
1160 {
1161 int lastwasvariable, emptystring;
1162 token_t token;
1163 bot_matchpiece_t *matchpiece, *firstpiece, *lastpiece;
1164 bot_matchstring_t *matchstring, *lastmatchstring;
1165
1166 firstpiece = NULL;
1167 lastpiece = NULL;
1168 //
1169 lastwasvariable = qfalse;
1170 //
1171 while(PC_ReadToken(source, &token))
1172 {
1173 if (token.type == TT_NUMBER && (token.subtype & TT_INTEGER))
1174 {
1175 if (token.intvalue >= MAX_MATCHVARIABLES)
1176 {
1177 SourceError(source, "can't have more than %d match variables", MAX_MATCHVARIABLES);
1178 FreeSource(source);
1179 BotFreeMatchPieces(firstpiece);
1180 return NULL;
1181 } //end if
1182 if (lastwasvariable)
1183 {
1184 SourceError(source, "not allowed to have adjacent variables");
1185 FreeSource(source);
1186 BotFreeMatchPieces(firstpiece);
1187 return NULL;
1188 } //end if
1189 lastwasvariable = qtrue;
1190 //
1191 matchpiece = (bot_matchpiece_t *) GetClearedHunkMemory(sizeof(bot_matchpiece_t));
1192 matchpiece->type = MT_VARIABLE;
1193 matchpiece->variable = token.intvalue;
1194 matchpiece->next = NULL;
1195 if (lastpiece) lastpiece->next = matchpiece;
1196 else firstpiece = matchpiece;
1197 lastpiece = matchpiece;
1198 } //end if
1199 else if (token.type == TT_STRING)
1200 {
1201 //
1202 matchpiece = (bot_matchpiece_t *) GetClearedHunkMemory(sizeof(bot_matchpiece_t));
1203 matchpiece->firststring = NULL;
1204 matchpiece->type = MT_STRING;
1205 matchpiece->variable = 0;
1206 matchpiece->next = NULL;
1207 if (lastpiece) lastpiece->next = matchpiece;
1208 else firstpiece = matchpiece;
1209 lastpiece = matchpiece;
1210 //
1211 lastmatchstring = NULL;
1212 emptystring = qfalse;
1213 //
1214 do
1215 {
1216 if (matchpiece->firststring)
1217 {
1218 if (!PC_ExpectTokenType(source, TT_STRING, 0, &token))
1219 {
1220 FreeSource(source);
1221 BotFreeMatchPieces(firstpiece);
1222 return NULL;
1223 } //end if
1224 } //end if
1225 StripDoubleQuotes(token.string);
1226 matchstring = (bot_matchstring_t *) GetClearedHunkMemory(sizeof(bot_matchstring_t) + strlen(token.string) + 1);
1227 matchstring->string = (char *) matchstring + sizeof(bot_matchstring_t);
1228 strcpy(matchstring->string, token.string);
1229 if (!strlen(token.string)) emptystring = qtrue;
1230 matchstring->next = NULL;
1231 if (lastmatchstring) lastmatchstring->next = matchstring;
1232 else matchpiece->firststring = matchstring;
1233 lastmatchstring = matchstring;
1234 } while(PC_CheckTokenString(source, "|"));
1235 //if there was no empty string found
1236 if (!emptystring) lastwasvariable = qfalse;
1237 } //end if
1238 else
1239 {
1240 SourceError(source, "invalid token %s", token.string);
1241 FreeSource(source);
1242 BotFreeMatchPieces(firstpiece);
1243 return NULL;
1244 } //end else
1245 if (PC_CheckTokenString(source, endtoken)) break;
1246 if (!PC_ExpectTokenString(source, ","))
1247 {
1248 FreeSource(source);
1249 BotFreeMatchPieces(firstpiece);
1250 return NULL;
1251 } //end if
1252 } //end while
1253 return firstpiece;
1254 } //end of the function BotLoadMatchPieces
1255 //===========================================================================
1256 //
1257 // Parameter: -
1258 // Returns: -
1259 // Changes Globals: -
1260 //===========================================================================
1261 void BotFreeMatchTemplates(bot_matchtemplate_t *mt)
1262 {
1263 bot_matchtemplate_t *nextmt;
1264
1265 for (; mt; mt = nextmt)
1266 {
1267 nextmt = mt->next;
1268 BotFreeMatchPieces(mt->first);
1269 FreeMemory(mt);
1270 } //end for
1271 } //end of the function BotFreeMatchTemplates
1272 //===========================================================================
1273 //
1274 // Parameter: -
1275 // Returns: -
1276 // Changes Globals: -
1277 //===========================================================================
1278 bot_matchtemplate_t *BotLoadMatchTemplates(char *matchfile)
1279 {
1280 source_t *source;
1281 token_t token;
1282 bot_matchtemplate_t *matchtemplate, *matches, *lastmatch;
1283 unsigned long int context;
1284
1285 PC_SetBaseFolder(BOTFILESBASEFOLDER);
1286 source = LoadSourceFile(matchfile);
1287 if (!source)
1288 {
1289 botimport.Print(PRT_ERROR, "couldn't load %s\n", matchfile);
1290 return NULL;
1291 } //end if
1292 //
1293 matches = NULL; //list with matches
1294 lastmatch = NULL; //last match in the list
1295
1296 while(PC_ReadToken(source, &token))
1297 {
1298 if (token.type != TT_NUMBER || !(token.subtype & TT_INTEGER))
1299 {
1300 SourceError(source, "expected integer, found %s", token.string);
1301 BotFreeMatchTemplates(matches);
1302 FreeSource(source);
1303 return NULL;
1304 } //end if
1305 //the context
1306 context = token.intvalue;
1307 //
1308 if (!PC_ExpectTokenString(source, "{"))
1309 {
1310 BotFreeMatchTemplates(matches);
1311 FreeSource(source);
1312 return NULL;
1313 } //end if
1314 //
1315 while(PC_ReadToken(source, &token))
1316 {
1317 if (!strcmp(token.string, "}")) break;
1318 //
1319 PC_UnreadLastToken(source);
1320 //
1321 matchtemplate = (bot_matchtemplate_t *) GetClearedHunkMemory(sizeof(bot_matchtemplate_t));
1322 matchtemplate->context = context;
1323 matchtemplate->next = NULL;
1324 //add the match template to the list
1325 if (lastmatch) lastmatch->next = matchtemplate;
1326 else matches = matchtemplate;
1327 lastmatch = matchtemplate;
1328 //load the match template
1329 matchtemplate->first = BotLoadMatchPieces(source, "=");
1330 if (!matchtemplate->first)
1331 {
1332 BotFreeMatchTemplates(matches);
1333 return NULL;
1334 } //end if
1335 //read the match type
1336 if (!PC_ExpectTokenString(source, "(") ||
1337 !PC_ExpectTokenType(source, TT_NUMBER, TT_INTEGER, &token))
1338 {
1339 BotFreeMatchTemplates(matches);
1340 FreeSource(source);
1341 return NULL;
1342 } //end if
1343 matchtemplate->type = token.intvalue;
1344 //read the match subtype
1345 if (!PC_ExpectTokenString(source, ",") ||
1346 !PC_ExpectTokenType(source, TT_NUMBER, TT_INTEGER, &token))
1347 {
1348 BotFreeMatchTemplates(matches);
1349 FreeSource(source);
1350 return NULL;
1351 } //end if
1352 matchtemplate->subtype = token.intvalue;
1353 //read trailing punctuations
1354 if (!PC_ExpectTokenString(source, ")") ||
1355 !PC_ExpectTokenString(source, ";"))
1356 {
1357 BotFreeMatchTemplates(matches);
1358 FreeSource(source);
1359 return NULL;
1360 } //end if
1361 } //end while
1362 } //end while
1363 //free the source
1364 FreeSource(source);
1365 botimport.Print(PRT_MESSAGE, "loaded %s\n", matchfile);
1366 //
1367 //BotDumpMatchTemplates(matches);
1368 //
1369 return matches;
1370 } //end of the function BotLoadMatchTemplates
1371 //===========================================================================
1372 //
1373 // Parameter: -
1374 // Returns: -
1375 // Changes Globals: -
1376 //===========================================================================
1377 int StringsMatch(bot_matchpiece_t *pieces, bot_match_t *match)
1378 {
1379 int lastvariable, index;
1380 char *strptr, *newstrptr;
1381 bot_matchpiece_t *mp;
1382 bot_matchstring_t *ms;
1383
1384 //no last variable
1385 lastvariable = -1;
1386 //pointer to the string to compare the match string with
1387 strptr = match->string;
1388 //Log_Write("match: %s", strptr);
1389 //compare the string with the current match string
1390 for (mp = pieces; mp; mp = mp->next)
1391 {
1392 //if it is a piece of string
1393 if (mp->type == MT_STRING)
1394 {
1395 newstrptr = NULL;
1396 for (ms = mp->firststring; ms; ms = ms->next)
1397 {
1398 if (!strlen(ms->string))
1399 {
1400 newstrptr = strptr;
1401 break;
1402 } //end if
1403 //Log_Write("MT_STRING: %s", mp->string);
1404 index = StringContains(strptr, ms->string, qfalse);
1405 if (index >= 0)
1406 {
1407 newstrptr = strptr + index;
1408 if (lastvariable >= 0)
1409 {
1410 match->variables[lastvariable].length =
1411 (newstrptr - match->string) - match->variables[lastvariable].offset;
1412 //newstrptr - match->variables[lastvariable].ptr;
1413 lastvariable = -1;
1414 break;
1415 } //end if
1416 else if (index == 0)
1417 {
1418 break;
1419 } //end else
1420 newstrptr = NULL;
1421 } //end if
1422 } //end for
1423 if (!newstrptr) return qfalse;
1424 strptr = newstrptr + strlen(ms->string);
1425 } //end if
1426 //if it is a variable piece of string
1427 else if (mp->type == MT_VARIABLE)
1428 {
1429 //Log_Write("MT_VARIABLE");
1430 match->variables[mp->variable].offset = strptr - match->string;
1431 lastvariable = mp->variable;
1432 } //end else if
1433 } //end for
1434 //if a match was found
1435 if (!mp && (lastvariable >= 0 || !strlen(strptr)))
1436 {
1437 //if the last piece was a variable string
1438 if (lastvariable >= 0)
1439 {
1440 assert( match->variables[lastvariable].offset >= 0 );
1441 match->variables[lastvariable].length =
1442 strlen(&match->string[ (int) match->variables[lastvariable].offset]);
1443 } //end if
1444 return qtrue;
1445 } //end if
1446 return qfalse;
1447 } //end of the function StringsMatch
1448 //===========================================================================
1449 //
1450 // Parameter: -
1451 // Returns: -
1452 // Changes Globals: -
1453 //===========================================================================
1454 int BotFindMatch(char *str, bot_match_t *match, unsigned long int context)
1455 {
1456 int i;
1457 bot_matchtemplate_t *ms;
1458
1459 Q_strncpyz(match->string, str, MAX_MESSAGE_SIZE);
1460 //remove any trailing enters
1461 while(strlen(match->string) &&
1462 match->string[strlen(match->string)-1] == '\n')
1463 {
1464 match->string[strlen(match->string)-1] = '\0';
1465 } //end while
1466 //compare the string with all the match strings
1467 for (ms = matchtemplates; ms; ms = ms->next)
1468 {
1469 if (!(ms->context & context)) continue;
1470 //reset the match variable offsets
1471 for (i = 0; i < MAX_MATCHVARIABLES; i++) match->variables[i].offset = -1;
1472 //
1473 if (StringsMatch(ms->first, match))
1474 {
1475 match->type = ms->type;
1476 match->subtype = ms->subtype;
1477 return qtrue;
1478 } //end if
1479 } //end for
1480 return qfalse;
1481 } //end of the function BotFindMatch
1482 //===========================================================================
1483 //
1484 // Parameter: -
1485 // Returns: -
1486 // Changes Globals: -
1487 //===========================================================================
1488 void BotMatchVariable(bot_match_t *match, int variable, char *buf, int size)
1489 {
1490 if (variable < 0 || variable >= MAX_MATCHVARIABLES)
1491 {
1492 botimport.Print(PRT_FATAL, "BotMatchVariable: variable out of range\n");
1493 strcpy(buf, "");
1494 return;
1495 } //end if
1496
1497 if (match->variables[variable].offset >= 0)
1498 {
1499 if (match->variables[variable].length < size)
1500 size = match->variables[variable].length+1;
1501 assert( match->variables[variable].offset >= 0 );
1502 Q_strncpyz(buf, &match->string[ (int) match->variables[variable].offset], size);
1503 } //end if
1504 else
1505 {
1506 strcpy(buf, "");
1507 } //end else
1508 } //end of the function BotMatchVariable
1509 //===========================================================================
1510 //
1511 // Parameter: -
1512 // Returns: -
1513 // Changes Globals: -
1514 //===========================================================================
1515 bot_stringlist_t *BotFindStringInList(bot_stringlist_t *list, char *string)
1516 {
1517 bot_stringlist_t *s;
1518
1519 for (s = list; s; s = s->next)
1520 {
1521 if (!strcmp(s->string, string)) return s;
1522 } //end for
1523 return NULL;
1524 } //end of the function BotFindStringInList
1525 //===========================================================================
1526 //
1527 // Parameter: -
1528 // Returns: -
1529 // Changes Globals: -
1530 //===========================================================================
1531 bot_stringlist_t *BotCheckChatMessageIntegrety(char *message, bot_stringlist_t *stringlist)
1532 {
1533 int i;
1534 char *msgptr;
1535 char temp[MAX_MESSAGE_SIZE];
1536 bot_stringlist_t *s;
1537
1538 msgptr = message;
1539 //
1540 while(*msgptr)
1541 {
1542 if (*msgptr == ESCAPE_CHAR)
1543 {
1544 msgptr++;
1545 switch(*msgptr)
1546 {
1547 case 'v': //variable
1548 {
1549 //step over the 'v'
1550 msgptr++;
1551 while(*msgptr && *msgptr != ESCAPE_CHAR) msgptr++;
1552 //step over the trailing escape char
1553 if (*msgptr) msgptr++;
1554 break;
1555 } //end case
1556 case 'r': //random
1557 {
1558 //step over the 'r'
1559 msgptr++;
1560 for (i = 0; (*msgptr && *msgptr != ESCAPE_CHAR); i++)
1561 {
1562 temp[i] = *msgptr++;
1563 } //end while
1564 temp[i] = '\0';
1565 //step over the trailing escape char
1566 if (*msgptr) msgptr++;
1567 //find the random keyword
1568 if (!RandomString(temp))
1569 {
1570 if (!BotFindStringInList(stringlist, temp))
1571 {
1572 Log_Write("%s = {\"%s\"} //MISSING RANDOM\r\n", temp, temp);
1573 s = GetClearedMemory(sizeof(bot_stringlist_t) + strlen(temp) + 1);
1574 s->string = (char *) s + sizeof(bot_stringlist_t);
1575 strcpy(s->string, temp);
1576 s->next = stringlist;
1577 stringlist = s;
1578 } //end if
1579 } //end if
1580 break;
1581 } //end case
1582 default:
1583 {
1584 botimport.Print(PRT_FATAL, "BotCheckChatMessageIntegrety: message \"%s\" invalid escape char\n", message);
1585 break;
1586 } //end default
1587 } //end switch
1588 } //end if
1589 else
1590 {
1591 msgptr++;
1592 } //end else
1593 } //end while
1594 return stringlist;
1595 } //end of the function BotCheckChatMessageIntegrety
1596 //===========================================================================
1597 //
1598 // Parameter: -
1599 // Returns: -
1600 // Changes Globals: -
1601 //===========================================================================
1602 void BotCheckInitialChatIntegrety(bot_chat_t *chat)
1603 {
1604 bot_chattype_t *t;
1605 bot_chatmessage_t *cm;
1606 bot_stringlist_t *stringlist, *s, *nexts;
1607
1608 stringlist = NULL;
1609 for (t = chat->types; t; t = t->next)
1610 {
1611 for (cm = t->firstchatmessage; cm; cm = cm->next)
1612 {
1613 stringlist = BotCheckChatMessageIntegrety(cm->chatmessage, stringlist);
1614 } //end for
1615 } //end for
1616 for (s = stringlist; s; s = nexts)
1617 {
1618 nexts = s->next;
1619 FreeMemory(s);
1620 } //end for
1621 } //end of the function BotCheckInitialChatIntegrety
1622 //===========================================================================
1623 //
1624 // Parameter: -
1625 // Returns: -
1626 // Changes Globals: -
1627 //===========================================================================
1628 void BotCheckReplyChatIntegrety(bot_replychat_t *replychat)
1629 {
1630 bot_replychat_t *rp;
1631 bot_chatmessage_t *cm;
1632 bot_stringlist_t *stringlist, *s, *nexts;
1633
1634 stringlist = NULL;
1635 for (rp = replychat; rp; rp = rp->next)
1636 {
1637 for (cm = rp->firstchatmessage; cm; cm = cm->next)
1638 {
1639 stringlist = BotCheckChatMessageIntegrety(cm->chatmessage, stringlist);
1640 } //end for
1641 } //end for
1642 for (s = stringlist; s; s = nexts)
1643 {
1644 nexts = s->next;
1645 FreeMemory(s);
1646 } //end for
1647 } //end of the function BotCheckReplyChatIntegrety
1648 //===========================================================================
1649 //
1650 // Parameter: -
1651 // Returns: -
1652 // Changes Globals: -
1653 //===========================================================================
1654 void BotDumpReplyChat(bot_replychat_t *replychat)
1655 {
1656 FILE *fp;
1657 bot_replychat_t *rp;
1658 bot_replychatkey_t *key;
1659 bot_chatmessage_t *cm;
1660 bot_matchpiece_t *mp;
1661
1662 fp = Log_FilePointer();
1663 if (!fp) return;
1664 fprintf(fp, "BotDumpReplyChat:\n");
1665 for (rp = replychat; rp; rp = rp->next)
1666 {
1667 fprintf(fp, "[");
1668 for (key = rp->keys; key; key = key->next)
1669 {
1670 if (key->flags & RCKFL_AND) fprintf(fp, "&");
1671 else if (key->flags & RCKFL_NOT) fprintf(fp, "!");
1672 //
1673 if (key->flags & RCKFL_NAME) fprintf(fp, "name");
1674 else if (key->flags & RCKFL_GENDERFEMALE) fprintf(fp, "female");
1675 else if (key->flags & RCKFL_GENDERMALE) fprintf(fp, "male");
1676 else if (key->flags & RCKFL_GENDERLESS) fprintf(fp, "it");
1677 else if (key->flags & RCKFL_VARIABLES)
1678 {
1679 fprintf(fp, "(");
1680 for (mp = key->match; mp; mp = mp->next)
1681 {
1682 if (mp->type == MT_STRING) fprintf(fp, "\"%s\"", mp->firststring->string);
1683 else fprintf(fp, "%d", mp->variable);
1684 if (mp->next) fprintf(fp, ", ");
1685 } //end for
1686 fprintf(fp, ")");
1687 } //end if
1688 else if (key->flags & RCKFL_STRING)
1689 {
1690 fprintf(fp, "\"%s\"", key->string);
1691 } //end if
1692 if (key->next) fprintf(fp, ", ");
1693 else fprintf(fp, "] = %1.0f\n", rp->priority);
1694 } //end for
1695 fprintf(fp, "{\n");
1696 for (cm = rp->firstchatmessage; cm; cm = cm->next)
1697 {
1698 fprintf(fp, "\t\"%s\";\n", cm->chatmessage);
1699 } //end for
1700 fprintf(fp, "}\n");
1701 } //end for
1702 } //end of the function BotDumpReplyChat
1703 //===========================================================================
1704 //
1705 // Parameter: -
1706 // Returns: -
1707 // Changes Globals: -
1708 //===========================================================================
1709 void BotFreeReplyChat(bot_replychat_t *replychat)
1710 {
1711 bot_replychat_t *rp, *nextrp;
1712 bot_replychatkey_t *key, *nextkey;
1713 bot_chatmessage_t *cm, *nextcm;
1714
1715 for (rp = replychat; rp; rp = nextrp)
1716 {
1717 nextrp = rp->next;
1718 for (key = rp->keys; key; key = nextkey)
1719 {
1720 nextkey = key->next;
1721 if (key->match) BotFreeMatchPieces(key->match);
1722 if (key->string) FreeMemory(key->string);
1723 FreeMemory(key);
1724 } //end for
1725 for (cm = rp->firstchatmessage; cm; cm = nextcm)
1726 {
1727 nextcm = cm->next;
1728 FreeMemory(cm);
1729 } //end for
1730 FreeMemory(rp);
1731 } //end for
1732 } //end of the function BotFreeReplyChat
1733 //===========================================================================
1734 //
1735 // Parameter: -
1736 // Returns: -
1737 // Changes Globals: -
1738 //===========================================================================
1739 void BotCheckValidReplyChatKeySet(source_t *source, bot_replychatkey_t *keys)
1740 {
1741 int allprefixed, hasvariableskey, hasstringkey;
1742 bot_matchpiece_t *m;
1743 bot_matchstring_t *ms;
1744 bot_replychatkey_t *key, *key2;
1745
1746 //
1747 allprefixed = qtrue;
1748 hasvariableskey = hasstringkey = qfalse;
1749 for (key = keys; key; key = key->next)
1750 {
1751 if (!(key->flags & (RCKFL_AND|RCKFL_NOT)))
1752 {
1753 allprefixed = qfalse;
1754 if (key->flags & RCKFL_VARIABLES)
1755 {
1756 for (m = key->match; m; m = m->next)
1757 {
1758 if (m->type == MT_VARIABLE) hasvariableskey = qtrue;
1759 } //end for
1760 } //end if
1761 else if (key->flags & RCKFL_STRING)
1762 {
1763 hasstringkey = qtrue;
1764 } //end else if
1765 } //end if
1766 else if ((key->flags & RCKFL_AND) && (key->flags & RCKFL_STRING))
1767 {
1768 for (key2 = keys; key2; key2 = key2->next)
1769 {
1770 if (key2 == key) continue;
1771 if (key2->flags & RCKFL_NOT) continue;
1772 if (key2->flags & RCKFL_VARIABLES)
1773 {
1774 for (m = key2->match; m; m = m->next)
1775 {
1776 if (m->type == MT_STRING)
1777 {
1778 for (ms = m->firststring; ms; ms = ms->next)
1779 {
1780 if (StringContains(ms->string, key->string, qfalse) != -1)
1781 {
1782 break;
1783 } //end if
1784 } //end for
1785 if (ms) break;
1786 } //end if
1787 else if (m->type == MT_VARIABLE)
1788 {
1789 break;
1790 } //end if
1791 } //end for
1792 if (!m)
1793 {
1794 SourceWarning(source, "one of the match templates does not "
1795 "leave space for the key %s with the & prefix", key->string);
1796 } //end if
1797 } //end if
1798 } //end for
1799 } //end else
1800 if ((key->flags & RCKFL_NOT) && (key->flags & RCKFL_STRING))
1801 {
1802 for (key2 = keys; key2; key2 = key2->next)
1803 {
1804 if (key2 == key) continue;
1805 if (key2->flags & RCKFL_NOT) continue;
1806 if (key2->flags & RCKFL_STRING)
1807 {
1808 if (StringContains(key2->string, key->string, qfalse) != -1)
1809 {
1810 SourceWarning(source, "the key %s with prefix ! is inside the key %s", key->string, key2->string);
1811 } //end if
1812 } //end if
1813 else if (key2->flags & RCKFL_VARIABLES)
1814 {
1815 for (m = key2->match; m; m = m->next)
1816 {
1817 if (m->type == MT_STRING)
1818 {
1819 for (ms = m->firststring; ms; ms = ms->next)
1820 {
1821 if (StringContains(ms->string, key->string, qfalse) != -1)
1822 {
1823 SourceWarning(source, "the key %s with prefix ! is inside "
1824 "the match template string %s", key->string, ms->string);
1825 } //end if
1826 } //end for
1827 } //end if
1828 } //end for
1829 } //end else if
1830 } //end for
1831 } //end if
1832 } //end for
1833 if (allprefixed) SourceWarning(source, "all keys have a & or ! prefix");
1834 if (hasvariableskey && hasstringkey)
1835 {
1836 SourceWarning(source, "variables from the match template(s) could be "
1837 "invalid when outputting one of the chat messages");
1838 } //end if
1839 } //end of the function BotCheckValidReplyChatKeySet
1840 //===========================================================================
1841 //
1842 // Parameter: -
1843 // Returns: -
1844 // Changes Globals: -
1845 //===========================================================================
1846 bot_replychat_t *BotLoadReplyChat(char *filename)
1847 {
1848 char chatmessagestring[MAX_MESSAGE_SIZE];
1849 char namebuffer[MAX_MESSAGE_SIZE];
1850 source_t *source;
1851 token_t token;
1852 bot_chatmessage_t *chatmessage = NULL;
1853 bot_replychat_t *replychat, *replychatlist;
1854 bot_replychatkey_t *key;
1855
1856 PC_SetBaseFolder(BOTFILESBASEFOLDER);
1857 source = LoadSourceFile(filename);
1858 if (!source)
1859 {
1860 botimport.Print(PRT_ERROR, "couldn't load %s\n", filename);
1861 return NULL;
1862 } //end if
1863 //
1864 replychatlist = NULL;
1865 //
1866 while(PC_ReadToken(source, &token))
1867 {
1868 if (strcmp(token.string, "["))
1869 {
1870 SourceError(source, "expected [, found %s", token.string);
1871 BotFreeReplyChat(replychatlist);
1872 FreeSource(source);
1873 return NULL;
1874 } //end if
1875 //
1876 replychat = GetClearedHunkMemory(sizeof(bot_replychat_t));
1877 replychat->keys = NULL;
1878 replychat->next = replychatlist;
1879 replychatlist = replychat;
1880 //read the keys, there must be at least one key
1881 do
1882 {
1883 //allocate a key
1884 key = (bot_replychatkey_t *) GetClearedHunkMemory(sizeof(bot_replychatkey_t));
1885 key->flags = 0;
1886 key->string = NULL;
1887 key->match = NULL;
1888 key->next = replychat->keys;
1889 replychat->keys = key;
1890 //check for MUST BE PRESENT and MUST BE ABSENT keys
1891 if (PC_CheckTokenString(source, "&")) key->flags |= RCKFL_AND;
1892 else if (PC_CheckTokenString(source, "!")) key->flags |= RCKFL_NOT;
1893 //special keys
1894 if (PC_CheckTokenString(source, "name")) key->flags |= RCKFL_NAME;
1895 else if (PC_CheckTokenString(source, "female")) key->flags |= RCKFL_GENDERFEMALE;
1896 else if (PC_CheckTokenString(source, "male")) key->flags |= RCKFL_GENDERMALE;
1897 else if (PC_CheckTokenString(source, "it")) key->flags |= RCKFL_GENDERLESS;
1898 else if (PC_CheckTokenString(source, "(")) //match key
1899 {
1900 key->flags |= RCKFL_VARIABLES;
1901 key->match = BotLoadMatchPieces(source, ")");
1902 if (!key->match)
1903 {
1904 BotFreeReplyChat(replychatlist);
1905 return NULL;
1906 } //end if
1907 } //end else if
1908 else if (PC_CheckTokenString(source, "<")) //bot names
1909 {
1910 key->flags |= RCKFL_BOTNAMES;
1911 strcpy(namebuffer, "");
1912 do
1913 {
1914 if (!PC_ExpectTokenType(source, TT_STRING, 0, &token))
1915 {
1916 BotFreeReplyChat(replychatlist);
1917 FreeSource(source);
1918 return NULL;
1919 } //end if
1920 StripDoubleQuotes(token.string);
1921 if (strlen(namebuffer)) strcat(namebuffer, "\\");
1922 strcat(namebuffer, token.string);
1923 } while(PC_CheckTokenString(source, ","));
1924 if (!PC_ExpectTokenString(source, ">"))
1925 {
1926 BotFreeReplyChat(replychatlist);
1927 FreeSource(source);
1928 return NULL;
1929 } //end if
1930 key->string = (char *) GetClearedHunkMemory(strlen(namebuffer) + 1);
1931 strcpy(key->string, namebuffer);
1932 } //end else if
1933 else //normal string key
1934 {
1935 key->flags |= RCKFL_STRING;
1936 if (!PC_ExpectTokenType(source, TT_STRING, 0, &token))
1937 {
1938 BotFreeReplyChat(replychatlist);
1939 FreeSource(source);
1940 return NULL;
1941 } //end if
1942 StripDoubleQuotes(token.string);
1943 key->string = (char *) GetClearedHunkMemory(strlen(token.string) + 1);
1944 strcpy(key->string, token.string);
1945 } //end else
1946 //
1947 PC_CheckTokenString(source, ",");
1948 } while(!PC_CheckTokenString(source, "]"));
1949 //
1950 BotCheckValidReplyChatKeySet(source, replychat->keys);
1951 //read the = sign and the priority
1952 if (!PC_ExpectTokenString(source, "=") ||
1953 !PC_ExpectTokenType(source, TT_NUMBER, 0, &token))
1954 {
1955 BotFreeReplyChat(replychatlist);
1956 FreeSource(source);
1957 return NULL;
1958 } //end if
1959 replychat->priority = token.floatvalue;
1960 //read the leading {
1961 if (!PC_ExpectTokenString(source, "{"))
1962 {
1963 BotFreeReplyChat(replychatlist);
1964 FreeSource(source);
1965 return NULL;
1966 } //end if
1967 replychat->numchatmessages = 0;
1968 //while the trailing } is not found
1969 while(!PC_CheckTokenString(source, "}"))
1970 {
1971 if (!BotLoadChatMessage(source, chatmessagestring))
1972 {
1973 BotFreeReplyChat(replychatlist);
1974 FreeSource(source);
1975 return NULL;
1976 } //end if
1977 chatmessage = (bot_chatmessage_t *) GetClearedHunkMemory(sizeof(bot_chatmessage_t) + strlen(chatmessagestring) + 1);
1978 chatmessage->chatmessage = (char *) chatmessage + sizeof(bot_chatmessage_t);
1979 strcpy(chatmessage->chatmessage, chatmessagestring);
1980 chatmessage->time = -2*CHATMESSAGE_RECENTTIME;
1981 chatmessage->next = replychat->firstchatmessage;
1982 //add the chat message to the reply chat
1983 replychat->firstchatmessage = chatmessage;
1984 replychat->numchatmessages++;
1985 } //end while
1986 } //end while
1987 FreeSource(source);
1988 botimport.Print(PRT_MESSAGE, "loaded %s\n", filename);
1989 //
1990 //BotDumpReplyChat(replychatlist);
1991 if (botDeveloper)
1992 {
1993 BotCheckReplyChatIntegrety(replychatlist);
1994 } //end if
1995 //
1996 if (!replychatlist) botimport.Print(PRT_MESSAGE, "no rchats\n");
1997 //
1998 return replychatlist;
1999 } //end of the function BotLoadReplyChat
2000 //===========================================================================
2001 //
2002 // Parameter: -
2003 // Returns: -
2004 // Changes Globals: -
2005 //===========================================================================
2006 void BotDumpInitialChat(bot_chat_t *chat)
2007 {
2008 bot_chattype_t *t;
2009 bot_chatmessage_t *m;
2010
2011 Log_Write("{");
2012 for (t = chat->types; t; t = t->next)
2013 {
2014 Log_Write(" type \"%s\"", t->name);
2015 Log_Write(" {");
2016 Log_Write(" numchatmessages = %d", t->numchatmessages);
2017 for (m = t->firstchatmessage; m; m = m->next)
2018 {
2019 Log_Write(" \"%s\"", m->chatmessage);
2020 } //end for
2021 Log_Write(" }");
2022 } //end for
2023 Log_Write("}");
2024 } //end of the function BotDumpInitialChat
2025 //===========================================================================
2026 //
2027 // Parameter: -
2028 // Returns: -
2029 // Changes Globals: -
2030 //===========================================================================
2031 bot_chat_t *BotLoadInitialChat(char *chatfile, char *chatname)
2032 {
2033 int pass, foundchat, indent, size;
2034 char *ptr = NULL;
2035 char chatmessagestring[MAX_MESSAGE_SIZE];
2036 source_t *source;
2037 token_t token;
2038 bot_chat_t *chat = NULL;
2039 bot_chattype_t *chattype = NULL;
2040 bot_chatmessage_t *chatmessage = NULL;
2041 #ifdef DEBUG
2042 int starttime;
2043
2044 starttime = Sys_MilliSeconds();
2045 #endif //DEBUG
2046 //
2047 size = 0;
2048 foundchat = qfalse;
2049 //a bot chat is parsed in two phases
2050 for (pass = 0; pass < 2; pass++)
2051 {
2052 //allocate memory
2053 if (pass && size) ptr = (char *) GetClearedMemory(size);
2054 //load the source file
2055 PC_SetBaseFolder(BOTFILESBASEFOLDER);
2056 source = LoadSourceFile(chatfile);
2057 if (!source)
2058 {
2059 botimport.Print(PRT_ERROR, "couldn't load %s\n", chatfile);
2060 return NULL;
2061 } //end if
2062 //chat structure
2063 if (pass)
2064 {
2065 chat = (bot_chat_t *) ptr;
2066 ptr += sizeof(bot_chat_t);
2067 } //end if
2068 size = sizeof(bot_chat_t);
2069 //
2070 while(PC_ReadToken(source, &token))
2071 {
2072 if (!strcmp(token.string, "chat"))
2073 {
2074 if (!PC_ExpectTokenType(source, TT_STRING, 0, &token))
2075 {
2076 FreeSource(source);
2077 return NULL;
2078 } //end if
2079 StripDoubleQuotes(token.string);
2080 //after the chat name we expect an opening brace
2081 if (!PC_ExpectTokenString(source, "{"))
2082 {
2083 FreeSource(source);
2084 return NULL;
2085 } //end if
2086 //if the chat name is found
2087 if (!Q_stricmp(token.string, chatname))
2088 {
2089 foundchat = qtrue;
2090 //read the chat types
2091 while(1)
2092 {
2093 if (!PC_ExpectAnyToken(source, &token))
2094 {
2095 FreeSource(source);
2096 return NULL;
2097 } //end if
2098 if (!strcmp(token.string, "}")) break;
2099 if (strcmp(token.string, "type"))
2100 {
2101 SourceError(source, "expected type found %s", token.string);
2102 FreeSource(source);
2103 return NULL;
2104 } //end if
2105 //expect the chat type name
2106 if (!PC_ExpectTokenType(source, TT_STRING, 0, &token) ||
2107 !PC_ExpectTokenString(source, "{"))
2108 {
2109 FreeSource(source);
2110 return NULL;
2111 } //end if
2112 StripDoubleQuotes(token.string);
2113 if (pass && ptr)
2114 {
2115 chattype = (bot_chattype_t *) ptr;
2116 Q_strncpyz(chattype->name, token.string, MAX_CHATTYPE_NAME);
2117 chattype->firstchatmessage = NULL;
2118 //add the chat type to the chat
2119 chattype->next = chat->types;
2120 chat->types = chattype;
2121 //
2122 ptr += sizeof(bot_chattype_t);
2123 } //end if
2124 size += sizeof(bot_chattype_t);
2125 //read the chat messages
2126 while(!PC_CheckTokenString(source, "}"))
2127 {
2128 size_t len;
2129 if (!BotLoadChatMessage(source, chatmessagestring))
2130 {
2131 FreeSource(source);
2132 return NULL;
2133 } //end if
2134 len = strlen(chatmessagestring) + 1;
2135 len = PAD(len, sizeof(long));
2136 if (pass && ptr)
2137 {
2138 chatmessage = (bot_chatmessage_t *) ptr;
2139 chatmessage->time = -2*CHATMESSAGE_RECENTTIME;
2140 //put the chat message in the list
2141 chatmessage->next = chattype->firstchatmessage;
2142 chattype->firstchatmessage = chatmessage;
2143 //store the chat message
2144 ptr += sizeof(bot_chatmessage_t);
2145 chatmessage->chatmessage = ptr;
2146 strcpy(chatmessage->chatmessage, chatmessagestring);
2147 ptr += len;
2148 //the number of chat messages increased
2149 chattype->numchatmessages++;
2150 } //end if
2151 size += sizeof(bot_chatmessage_t) + len;
2152 } //end if
2153 } //end while
2154 } //end if
2155 else //skip the bot chat
2156 {
2157 indent = 1;
2158 while(indent)
2159 {
2160 if (!PC_ExpectAnyToken(source, &token))
2161 {
2162 FreeSource(source);
2163 return NULL;
2164 } //end if
2165 if (!strcmp(token.string, "{")) indent++;
2166 else if (!strcmp(token.string, "}")) indent--;
2167 } //end while
2168 } //end else
2169 } //end if
2170 else
2171 {
2172 SourceError(source, "unknown definition %s", token.string);
2173 FreeSource(source);
2174 return NULL;
2175 } //end else
2176 } //end while
2177 //free the source
2178 FreeSource(source);
2179 //if the requested character is not found
2180 if (!foundchat)
2181 {
2182 botimport.Print(PRT_ERROR, "couldn't find chat %s in %s\n", chatname, chatfile);
2183 return NULL;
2184 } //end if
2185 } //end for
2186 //
2187 botimport.Print(PRT_MESSAGE, "loaded %s from %s\n", chatname, chatfile);
2188 //
2189 //BotDumpInitialChat(chat);
2190 if (botDeveloper)
2191 {
2192 BotCheckInitialChatIntegrety(chat);
2193 } //end if
2194 #ifdef DEBUG
2195 botimport.Print(PRT_MESSAGE, "initial chats loaded in %d msec\n", Sys_MilliSeconds() - starttime);
2196 #endif //DEBUG
2197 //character was read successfully
2198 return chat;
2199 } //end of the function BotLoadInitialChat
2200 //===========================================================================
2201 //
2202 // Parameter: -
2203 // Returns: -
2204 // Changes Globals: -
2205 //===========================================================================
2206 void BotFreeChatFile(int chatstate)
2207 {
2208 bot_chatstate_t *cs;
2209
2210 cs = BotChatStateFromHandle(chatstate);
2211 if (!cs) return;
2212 if (cs->chat) FreeMemory(cs->chat);
2213 cs->chat = NULL;
2214 } //end of the function BotFreeChatFile
2215 //===========================================================================
2216 //
2217 // Parameter: -
2218 // Returns: -
2219 // Changes Globals: -
2220 //===========================================================================
2221 int BotLoadChatFile(int chatstate, char *chatfile, char *chatname)
2222 {
2223 bot_chatstate_t *cs;
2224 int n, avail = 0;
2225
2226 cs = BotChatStateFromHandle(chatstate);
2227 if (!cs) return BLERR_CANNOTLOADICHAT;
2228 BotFreeChatFile(chatstate);
2229
2230 if (!LibVarGetValue("bot_reloadcharacters"))
2231 {
2232 avail = -1;
2233 for( n = 0; n < MAX_CLIENTS; n++ ) {
2234 if( !ichatdata[n] ) {
2235 if( avail == -1 ) {
2236 avail = n;
2237 }
2238 continue;
2239 }
2240 if( strcmp( chatfile, ichatdata[n]->filename ) != 0 ) {
2241 continue;
2242 }
2243 if( strcmp( chatname, ichatdata[n]->chatname ) != 0 ) {
2244 continue;
2245 }
2246 cs->chat = ichatdata[n]->chat;
2247 // botimport.Print( PRT_MESSAGE, "retained %s from %s\n", chatname, chatfile );
2248 return BLERR_NOERROR;
2249 }
2250
2251 if( avail == -1 ) {
2252 botimport.Print(PRT_FATAL, "ichatdata table full; couldn't load chat %s from %s\n", chatname, chatfile);
2253 return BLERR_CANNOTLOADICHAT;
2254 }
2255 }
2256
2257 cs->chat = BotLoadInitialChat(chatfile, chatname);
2258 if (!cs->chat)
2259 {
2260 botimport.Print(PRT_FATAL, "couldn't load chat %s from %s\n", chatname, chatfile);
2261 return BLERR_CANNOTLOADICHAT;
2262 } //end if
2263 if (!LibVarGetValue("bot_reloadcharacters"))
2264 {
2265 ichatdata[avail] = GetClearedMemory( sizeof(bot_ichatdata_t) );
2266 ichatdata[avail]->chat = cs->chat;
2267 Q_strncpyz( ichatdata[avail]->chatname, chatname, sizeof(ichatdata[avail]->chatname) );
2268 Q_strncpyz( ichatdata[avail]->filename, chatfile, sizeof(ichatdata[avail]->filename) );
2269 } //end if
2270
2271 return BLERR_NOERROR;
2272 } //end of the function BotLoadChatFile
2273 //===========================================================================
2274 //
2275 // Parameter: -
2276 // Returns: -
2277 // Changes Globals: -
2278 //===========================================================================
2279 int BotExpandChatMessage(char *outmessage, char *message, unsigned long mcontext,
2280 bot_match_t *match, unsigned long vcontext, int reply)
2281 {
2282 int num, len, i, expansion;
2283 char *outputbuf, *ptr, *msgptr;
2284 char temp[MAX_MESSAGE_SIZE];
2285
2286 expansion = qfalse;
2287 msgptr = message;
2288 outputbuf = outmessage;
2289 len = 0;
2290 //
2291 while(*msgptr)
2292 {
2293 if (*msgptr == ESCAPE_CHAR)
2294 {
2295 msgptr++;
2296 switch(*msgptr)
2297 {
2298 case 'v': //variable
2299 {
2300 msgptr++;
2301 num = 0;
2302 while(*msgptr && *msgptr != ESCAPE_CHAR)
2303 {
2304 num = num * 10 + (*msgptr++) - '0';
2305 } //end while
2306 //step over the trailing escape char
2307 if (*msgptr) msgptr++;
2308 if (num > MAX_MATCHVARIABLES)
2309 {
2310 botimport.Print(PRT_ERROR, "BotConstructChat: message %s variable %d out of range\n", message, num);
2311 return qfalse;
2312 } //end if
2313 if (match->variables[num].offset >= 0)
2314 {
2315 assert( match->variables[num].offset >= 0 );
2316 ptr = &match->string[ (int) match->variables[num].offset];
2317 for (i = 0; i < match->variables[num].length; i++)
2318 {
2319 temp[i] = ptr[i];
2320 } //end for
2321 temp[i] = 0;
2322 //if it's a reply message
2323 if (reply)
2324 {
2325 //replace the reply synonyms in the variables
2326 BotReplaceReplySynonyms(temp, vcontext);
2327 } //end if
2328 else
2329 {
2330 //replace synonyms in the variable context
2331 BotReplaceSynonyms(temp, vcontext);
2332 } //end else
2333 //
2334 if (len + strlen(temp) >= MAX_MESSAGE_SIZE)
2335 {
2336 botimport.Print(PRT_ERROR, "BotConstructChat: message %s too long\n", message);
2337 return qfalse;
2338 } //end if
2339 strcpy(&outputbuf[len], temp);
2340 len += strlen(temp);
2341 } //end if
2342 break;
2343 } //end case
2344 case 'r': //random
2345 {
2346 msgptr++;
2347 for (i = 0; (*msgptr && *msgptr != ESCAPE_CHAR); i++)
2348 {
2349 temp[i] = *msgptr++;
2350 } //end while
2351 temp[i] = '\0';
2352 //step over the trailing escape char
2353 if (*msgptr) msgptr++;
2354 //find the random keyword
2355 ptr = RandomString(temp);
2356 if (!ptr)
2357 {
2358 botimport.Print(PRT_ERROR, "BotConstructChat: unknown random string %s\n", temp);
2359 return qfalse;
2360 } //end if
2361 if (len + strlen(ptr) >= MAX_MESSAGE_SIZE)
2362 {
2363 botimport.Print(PRT_ERROR, "BotConstructChat: message \"%s\" too long\n", message);
2364 return qfalse;
2365 } //end if
2366 strcpy(&outputbuf[len], ptr);
2367 len += strlen(ptr);
2368 expansion = qtrue;
2369 break;
2370 } //end case
2371 default:
2372 {
2373 botimport.Print(PRT_FATAL, "BotConstructChat: message \"%s\" invalid escape char\n", message);
2374 break;
2375 } //end default
2376 } //end switch
2377 } //end if
2378 else
2379 {
2380 outputbuf[len++] = *msgptr++;
2381 if (len >= MAX_MESSAGE_SIZE)
2382 {
2383 botimport.Print(PRT_ERROR, "BotConstructChat: message \"%s\" too long\n", message);
2384 break;
2385 } //end if
2386 } //end else
2387 } //end while
2388 outputbuf[len] = '\0';
2389 //replace synonyms weighted in the message context
2390 BotReplaceWeightedSynonyms(outputbuf, mcontext);
2391 //return true if a random was expanded
2392 return expansion;
2393 } //end of the function BotExpandChatMessage
2394 //===========================================================================
2395 //
2396 // Parameter: -
2397 // Returns: -
2398 // Changes Globals: -
2399 //===========================================================================
2400 void BotConstructChatMessage(bot_chatstate_t *chatstate, char *message, unsigned long mcontext,
2401 bot_match_t *match, unsigned long vcontext, int reply)
2402 {
2403 int i;
2404 char srcmessage[MAX_MESSAGE_SIZE];
2405
2406 strcpy(srcmessage, message);
2407 for (i = 0; i < 10; i++)
2408 {
2409 if (!BotExpandChatMessage(chatstate->chatmessage, srcmessage, mcontext, match, vcontext, reply))
2410 {
2411 break;
2412 } //end if
2413 strcpy(srcmessage, chatstate->chatmessage);
2414 } //end for
2415 if (i >= 10)
2416 {
2417 botimport.Print(PRT_WARNING, "too many expansions in chat message\n");
2418 botimport.Print(PRT_WARNING, "%s\n", chatstate->chatmessage);
2419 } //end if
2420 } //end of the function BotConstructChatMessage
2421 //===========================================================================
2422 // randomly chooses one of the chat message of the given type
2423 //
2424 // Parameter: -
2425 // Returns: -
2426 // Changes Globals: -
2427 //===========================================================================
2428 char *BotChooseInitialChatMessage(bot_chatstate_t *cs, char *type)
2429 {
2430 int n, numchatmessages;
2431 float besttime;
2432 bot_chattype_t *t;
2433 bot_chatmessage_t *m, *bestchatmessage;
2434 bot_chat_t *chat;
2435
2436 chat = cs->chat;
2437 for (t = chat->types; t; t = t->next)
2438 {
2439 if (!Q_stricmp(t->name, type))
2440 {
2441 numchatmessages = 0;
2442 for (m = t->firstchatmessage; m; m = m->next)
2443 {
2444 if (m->time > AAS_Time()) continue;
2445 numchatmessages++;
2446 } //end if
2447 //if all chat messages have been used recently
2448 if (numchatmessages <= 0)
2449 {
2450 besttime = 0;
2451 bestchatmessage = NULL;
2452 for (m = t->firstchatmessage; m; m = m->next)
2453 {
2454 if (!besttime || m->time < besttime)
2455 {
2456 bestchatmessage = m;
2457 besttime = m->time;
2458 } //end if
2459 } //end for
2460 if (bestchatmessage) return bestchatmessage->chatmessage;
2461 } //end if
2462 else //choose a chat message randomly
2463 {
2464 n = random() * numchatmessages;
2465 for (m = t->firstchatmessage; m; m = m->next)
2466 {
2467 if (m->time > AAS_Time()) continue;
2468 if (--n < 0)
2469 {
2470 m->time = AAS_Time() + CHATMESSAGE_RECENTTIME;
2471 return m->chatmessage;
2472 } //end if
2473 } //end for
2474 } //end else
2475 return NULL;
2476 } //end if
2477 } //end for
2478 return NULL;
2479 } //end of the function BotChooseInitialChatMessage
2480 //===========================================================================
2481 //
2482 // Parameter: -
2483 // Returns: -
2484 // Changes Globals: -
2485 //===========================================================================
2486 int BotNumInitialChats(int chatstate, char *type)
2487 {
2488 bot_chatstate_t *cs;
2489 bot_chattype_t *t;
2490
2491 cs = BotChatStateFromHandle(chatstate);
2492 if (!cs) return 0;
2493
2494 for (t = cs->chat->types; t; t = t->next)
2495 {
2496 if (!Q_stricmp(t->name, type))
2497 {
2498 if (LibVarGetValue("bot_testichat")) {
2499 botimport.Print(PRT_MESSAGE, "%s has %d chat lines\n", type, t->numchatmessages);
2500 botimport.Print(PRT_MESSAGE, "-------------------\n");
2501 }
2502 return t->numchatmessages;
2503 } //end if
2504 } //end for
2505 return 0;
2506 } //end of the function BotNumInitialChats
2507 //===========================================================================
2508 //
2509 // Parameter: -
2510 // Returns: -
2511 // Changes Globals: -
2512 //===========================================================================
2513 void BotInitialChat(int chatstate, char *type, int mcontext, char *var0, char *var1, char *var2, char *var3, char *var4, char *var5, char *var6, char *var7)
2514 {
2515 char *message;
2516 int index;
2517 bot_match_t match;
2518 bot_chatstate_t *cs;
2519
2520 cs = BotChatStateFromHandle(chatstate);
2521 if (!cs) return;
2522 //if no chat file is loaded
2523 if (!cs->chat) return;
2524 //choose a chat message randomly of the given type
2525 message = BotChooseInitialChatMessage(cs, type);
2526 //if there's no message of the given type
2527 if (!message)
2528 {
2529 #ifdef DEBUG
2530 botimport.Print(PRT_MESSAGE, "no chat messages of type %s\n", type);
2531 #endif //DEBUG
2532 return;
2533 } //end if
2534 //
2535 Com_Memset(&match, 0, sizeof(match));
2536 index = 0;
2537 if( var0 ) {
2538 strcat(match.string, var0);
2539 match.variables[0].offset = index;
2540 match.variables[0].length = strlen(var0);
2541 index += strlen(var0);
2542 }
2543 if( var1 ) {
2544 strcat(match.string, var1);
2545 match.variables[1].offset = index;
2546 match.variables[1].length = strlen(var1);
2547 index += strlen(var1);
2548 }
2549 if( var2 ) {
2550 strcat(match.string, var2);
2551 match.variables[2].offset = index;
2552 match.variables[2].length = strlen(var2);
2553 index += strlen(var2);
2554 }
2555 if( var3 ) {
2556 strcat(match.string, var3);
2557 match.variables[3].offset = index;
2558 match.variables[3].length = strlen(var3);
2559 index += strlen(var3);
2560 }
2561 if( var4 ) {
2562 strcat(match.string, var4);
2563 match.variables[4].offset = index;
2564 match.variables[4].length = strlen(var4);
2565 index += strlen(var4);
2566 }
2567 if( var5 ) {
2568 strcat(match.string, var5);
2569 match.variables[5].offset = index;
2570 match.variables[5].length = strlen(var5);
2571 index += strlen(var5);
2572 }
2573 if( var6 ) {
2574 strcat(match.string, var6);
2575 match.variables[6].offset = index;
2576 match.variables[6].length = strlen(var6);
2577 index += strlen(var6);
2578 }
2579 if( var7 ) {
2580 strcat(match.string, var7);
2581 match.variables[7].offset = index;
2582 match.variables[7].length = strlen(var7);
2583 }
2584 //
2585 BotConstructChatMessage(cs, message, mcontext, &match, 0, qfalse);
2586 } //end of the function BotInitialChat
2587 //===========================================================================
2588 //
2589 // Parameter: -
2590 // Returns: -
2591 // Changes Globals: -
2592 //===========================================================================
2593 void BotPrintReplyChatKeys(bot_replychat_t *replychat)
2594 {
2595 bot_replychatkey_t *key;
2596 bot_matchpiece_t *mp;
2597
2598 botimport.Print(PRT_MESSAGE, "[");
2599 for (key = replychat->keys; key; key = key->next)
2600 {
2601 if (key->flags & RCKFL_AND) botimport.Print(PRT_MESSAGE, "&");
2602 else if (key->flags & RCKFL_NOT) botimport.Print(PRT_MESSAGE, "!");
2603 //
2604 if (key->flags & RCKFL_NAME) botimport.Print(PRT_MESSAGE, "name");
2605 else if (key->flags & RCKFL_GENDERFEMALE) botimport.Print(PRT_MESSAGE, "female");
2606 else if (key->flags & RCKFL_GENDERMALE) botimport.Print(PRT_MESSAGE, "male");
2607 else if (key->flags & RCKFL_GENDERLESS) botimport.Print(PRT_MESSAGE, "it");
2608 else if (key->flags & RCKFL_VARIABLES)
2609 {
2610 botimport.Print(PRT_MESSAGE, "(");
2611 for (mp = key->match; mp; mp = mp->next)
2612 {
2613 if (mp->type == MT_STRING) botimport.Print(PRT_MESSAGE, "\"%s\"", mp->firststring->string);
2614 else botimport.Print(PRT_MESSAGE, "%d", mp->variable);
2615 if (mp->next) botimport.Print(PRT_MESSAGE, ", ");
2616 } //end for
2617 botimport.Print(PRT_MESSAGE, ")");
2618 } //end if
2619 else if (key->flags & RCKFL_STRING)
2620 {
2621 botimport.Print(PRT_MESSAGE, "\"%s\"", key->string);
2622 } //end if
2623 if (key->next) botimport.Print(PRT_MESSAGE, ", ");
2624 else botimport.Print(PRT_MESSAGE, "] = %1.0f\n", replychat->priority);
2625 } //end for
2626 botimport.Print(PRT_MESSAGE, "{\n");
2627 } //end of the function BotPrintReplyChatKeys
2628 //===========================================================================
2629 //
2630 // Parameter: -
2631 // Returns: -
2632 // Changes Globals: -
2633 //===========================================================================
2634 int BotReplyChat(int chatstate, char *message, int mcontext, int vcontext, char *var0, char *var1, char *var2, char *var3, char *var4, char *var5, char *var6, char *var7)
2635 {
2636 bot_replychat_t *rchat, *bestrchat;
2637 bot_replychatkey_t *key;
2638 bot_chatmessage_t *m, *bestchatmessage;
2639 bot_match_t match, bestmatch;
2640 int bestpriority, num, found, res, numchatmessages, index;
2641 bot_chatstate_t *cs;
2642
2643 cs = BotChatStateFromHandle(chatstate);
2644 if (!cs) return qfalse;
2645 Com_Memset(&match, 0, sizeof(bot_match_t));
2646 strcpy(match.string, message);
2647 bestpriority = -1;
2648 bestchatmessage = NULL;
2649 bestrchat = NULL;
2650 //go through all the reply chats
2651 for (rchat = replychats; rchat; rchat = rchat->next)
2652 {
2653 found = qfalse;
2654 for (key = rchat->keys; key; key = key->next)
2655 {
2656 res = qfalse;
2657 //get the match result
2658 if (key->flags & RCKFL_NAME) res = (StringContains(message, cs->name, qfalse) != -1);
2659 else if (key->flags & RCKFL_BOTNAMES) res = (StringContains(key->string, cs->name, qfalse) != -1);
2660 else if (key->flags & RCKFL_GENDERFEMALE) res = (cs->gender == CHAT_GENDERFEMALE);
2661 else if (key->flags & RCKFL_GENDERMALE) res = (cs->gender == CHAT_GENDERMALE);
2662 else if (key->flags & RCKFL_GENDERLESS) res = (cs->gender == CHAT_GENDERLESS);
2663 else if (key->flags & RCKFL_VARIABLES) res = StringsMatch(key->match, &match);
2664 else if (key->flags & RCKFL_STRING) res = (StringContainsWord(message, key->string, qfalse) != NULL);
2665 //if the key must be present
2666 if (key->flags & RCKFL_AND)
2667 {
2668 if (!res)
2669 {
2670 found = qfalse;
2671 break;
2672 } //end if
2673 } //end else if
2674 //if the key must be absent
2675 else if (key->flags & RCKFL_NOT)
2676 {
2677 if (res)
2678 {
2679 found = qfalse;
2680 break;
2681 } //end if
2682 } //end if
2683 else if (res)
2684 {
2685 found = qtrue;
2686 } //end else
2687 } //end for
2688 //
2689 if (found)
2690 {
2691 if (rchat->priority > bestpriority)
2692 {
2693 numchatmessages = 0;
2694 for (m = rchat->firstchatmessage; m; m = m->next)
2695 {
2696 if (m->time > AAS_Time()) continue;
2697 numchatmessages++;
2698 } //end if
2699 num = random() * numchatmessages;
2700 for (m = rchat->firstchatmessage; m; m = m->next)
2701 {
2702 if (--num < 0) break;
2703 if (m->time > AAS_Time()) continue;
2704 } //end for
2705 //if the reply chat has a message
2706 if (m)
2707 {
2708 Com_Memcpy(&bestmatch, &match, sizeof(bot_match_t));
2709 bestchatmessage = m;
2710 bestrchat = rchat;
2711 bestpriority = rchat->priority;
2712 } //end if
2713 } //end if
2714 } //end if
2715 } //end for
2716 if (bestchatmessage)
2717 {
2718 index = strlen(bestmatch.string);
2719 if( var0 ) {
2720 strcat(bestmatch.string, var0);
2721 bestmatch.variables[0].offset = index;
2722 bestmatch.variables[0].length = strlen(var0);
2723 index += strlen(var0);
2724 }
2725 if( var1 ) {
2726 strcat(bestmatch.string, var1);
2727 bestmatch.variables[1].offset = index;
2728 bestmatch.variables[1].length = strlen(var1);
2729 index += strlen(var1);
2730 }
2731 if( var2 ) {
2732 strcat(bestmatch.string, var2);
2733 bestmatch.variables[2].offset = index;
2734 bestmatch.variables[2].length = strlen(var2);
2735 index += strlen(var2);
2736 }
2737 if( var3 ) {
2738 strcat(bestmatch.string, var3);
2739 bestmatch.variables[3].offset = index;
2740 bestmatch.variables[3].length = strlen(var3);
2741 index += strlen(var3);
2742 }
2743 if( var4 ) {
2744 strcat(bestmatch.string, var4);
2745 bestmatch.variables[4].offset = index;
2746 bestmatch.variables[4].length = strlen(var4);
2747 index += strlen(var4);
2748 }
2749 if( var5 ) {
2750 strcat(bestmatch.string, var5);
2751 bestmatch.variables[5].offset = index;
2752 bestmatch.variables[5].length = strlen(var5);
2753 index += strlen(var5);
2754 }
2755 if( var6 ) {
2756 strcat(bestmatch.string, var6);
2757 bestmatch.variables[6].offset = index;
2758 bestmatch.variables[6].length = strlen(var6);
2759 index += strlen(var6);
2760 }
2761 if( var7 ) {
2762 strcat(bestmatch.string, var7);
2763 bestmatch.variables[7].offset = index;
2764 bestmatch.variables[7].length = strlen(var7);
2765 }
2766 if (LibVarGetValue("bot_testrchat"))
2767 {
2768 for (m = bestrchat->firstchatmessage; m; m = m->next)
2769 {
2770 BotConstructChatMessage(cs, m->chatmessage, mcontext, &bestmatch, vcontext, qtrue);
2771 BotRemoveTildes(cs->chatmessage);
2772 botimport.Print(PRT_MESSAGE, "%s\n", cs->chatmessage);
2773 } //end if
2774 } //end if
2775 else
2776 {
2777 bestchatmessage->time = AAS_Time() + CHATMESSAGE_RECENTTIME;
2778 BotConstructChatMessage(cs, bestchatmessage->chatmessage, mcontext, &bestmatch, vcontext, qtrue);
2779 } //end else
2780 return qtrue;
2781 } //end if
2782 return qfalse;
2783 } //end of the function BotReplyChat
2784 //===========================================================================
2785 //
2786 // Parameter: -
2787 // Returns: -
2788 // Changes Globals: -
2789 //===========================================================================
2790 int BotChatLength(int chatstate)
2791 {
2792 bot_chatstate_t *cs;
2793
2794 cs = BotChatStateFromHandle(chatstate);
2795 if (!cs) return 0;
2796 return strlen(cs->chatmessage);
2797 } //end of the function BotChatLength
2798 //===========================================================================
2799 //
2800 // Parameter: -
2801 // Returns: -
2802 // Changes Globals: -
2803 //===========================================================================
2804 void BotEnterChat(int chatstate, int clientto, int sendto)
2805 {
2806 bot_chatstate_t *cs;
2807
2808 cs = BotChatStateFromHandle(chatstate);
2809 if (!cs) return;
2810
2811 if (strlen(cs->chatmessage))
2812 {
2813 BotRemoveTildes(cs->chatmessage);
2814 if (LibVarGetValue("bot_testichat")) {
2815 botimport.Print(PRT_MESSAGE, "%s\n", cs->chatmessage);
2816 }
2817 else {
2818 switch(sendto) {
2819 case CHAT_TEAM:
2820 EA_Command(cs->client, va("say_team %s", cs->chatmessage));
2821 break;
2822 case CHAT_TELL:
2823 EA_Command(cs->client, va("tell %d %s", clientto, cs->chatmessage));
2824 break;
2825 default: //CHAT_ALL
2826 EA_Command(cs->client, va("say %s", cs->chatmessage));
2827 break;
2828 }
2829 }
2830 //clear the chat message from the state
2831 strcpy(cs->chatmessage, "");
2832 } //end if
2833 } //end of the function BotEnterChat
2834 //===========================================================================
2835 //
2836 // Parameter: -
2837 // Returns: -
2838 // Changes Globals: -
2839 //===========================================================================
2840 void BotGetChatMessage(int chatstate, char *buf, int size)
2841 {
2842 bot_chatstate_t *cs;
2843
2844 cs = BotChatStateFromHandle(chatstate);
2845 if (!cs) return;
2846
2847 BotRemoveTildes(cs->chatmessage);
2848 Q_strncpyz(buf, cs->chatmessage, size);
2849 //clear the chat message from the state
2850 strcpy(cs->chatmessage, "");
2851 } //end of the function BotGetChatMessage
2852 //===========================================================================
2853 //
2854 // Parameter: -
2855 // Returns: -
2856 // Changes Globals: -
2857 //===========================================================================
2858 void BotSetChatGender(int chatstate, int gender)
2859 {
2860 bot_chatstate_t *cs;
2861
2862 cs = BotChatStateFromHandle(chatstate);
2863 if (!cs) return;
2864 switch(gender)
2865 {
2866 case CHAT_GENDERFEMALE: cs->gender = CHAT_GENDERFEMALE; break;
2867 case CHAT_GENDERMALE: cs->gender = CHAT_GENDERMALE; break;
2868 default: cs->gender = CHAT_GENDERLESS; break;
2869 } //end switch
2870 } //end of the function BotSetChatGender
2871 //===========================================================================
2872 //
2873 // Parameter: -
2874 // Returns: -
2875 // Changes Globals: -
2876 //===========================================================================
2877 void BotSetChatName(int chatstate, char *name, int client)
2878 {
2879 bot_chatstate_t *cs;
2880
2881 cs = BotChatStateFromHandle(chatstate);
2882 if (!cs) return;
2883 cs->client = client;
2884 Q_strncpyz(cs->name, name, sizeof(cs->name));
2885 } //end of the function BotSetChatName
2886 //===========================================================================
2887 //
2888 // Parameter: -
2889 // Returns: -
2890 // Changes Globals: -
2891 //===========================================================================
2892 void BotResetChatAI(void)
2893 {
2894 bot_replychat_t *rchat;
2895 bot_chatmessage_t *m;
2896
2897 for (rchat = replychats; rchat; rchat = rchat->next)
2898 {
2899 for (m = rchat->firstchatmessage; m; m = m->next)
2900 {
2901 m->time = 0;
2902 } //end for
2903 } //end for
2904 } //end of the function BotResetChatAI
2905 //========================================================================
2906 //
2907 // Parameter: -
2908 // Returns: -
2909 // Changes Globals: -
2910 //========================================================================
2911 int BotAllocChatState(void)
2912 {
2913 int i;
2914
2915 for (i = 1; i <= MAX_CLIENTS; i++)
2916 {
2917 if (!botchatstates[i])
2918 {
2919 botchatstates[i] = GetClearedMemory(sizeof(bot_chatstate_t));
2920 return i;
2921 } //end if
2922 } //end for
2923 return 0;
2924 } //end of the function BotAllocChatState
2925 //========================================================================
2926 //
2927 // Parameter: -
2928 // Returns: -
2929 // Changes Globals: -
2930 //========================================================================
2931 void BotFreeChatState(int handle)
2932 {
2933 bot_consolemessage_t m;
2934 int h;
2935
2936 if (handle <= 0 || handle > MAX_CLIENTS)
2937 {
2938 botimport.Print(PRT_FATAL, "chat state handle %d out of range\n", handle);
2939 return;
2940 } //end if
2941 if (!botchatstates[handle])
2942 {
2943 botimport.Print(PRT_FATAL, "invalid chat state %d\n", handle);
2944 return;
2945 } //end if
2946 if (LibVarGetValue("bot_reloadcharacters"))
2947 {
2948 BotFreeChatFile(handle);
2949 } //end if
2950 //free all the console messages left in the chat state
2951 for (h = BotNextConsoleMessage(handle, &m); h; h = BotNextConsoleMessage(handle, &m))
2952 {
2953 //remove the console message
2954 BotRemoveConsoleMessage(handle, h);
2955 } //end for
2956 FreeMemory(botchatstates[handle]);
2957 botchatstates[handle] = NULL;
2958 } //end of the function BotFreeChatState
2959 //===========================================================================
2960 //
2961 // Parameter: -
2962 // Returns: -
2963 // Changes Globals: -
2964 //===========================================================================
2965 int BotSetupChatAI(void)
2966 {
2967 char *file;
2968
2969 #ifdef DEBUG
2970 int starttime = Sys_MilliSeconds();
2971 #endif //DEBUG
2972
2973 file = LibVarString("synfile", "syn.c");
2974 synonyms = BotLoadSynonyms(file);
2975 file = LibVarString("rndfile", "rnd.c");
2976 randomstrings = BotLoadRandomStrings(file);
2977 file = LibVarString("matchfile", "match.c");
2978 matchtemplates = BotLoadMatchTemplates(file);
2979 //
2980 if (!LibVarValue("nochat", "0"))
2981 {
2982 file = LibVarString("rchatfile", "rchat.c");
2983 replychats = BotLoadReplyChat(file);
2984 } //end if
2985
2986 InitConsoleMessageHeap();
2987
2988 #ifdef DEBUG
2989 botimport.Print(PRT_MESSAGE, "setup chat AI %d msec\n", Sys_MilliSeconds() - starttime);
2990 #endif //DEBUG
2991 return BLERR_NOERROR;
2992 } //end of the function BotSetupChatAI
2993 //===========================================================================
2994 //
2995 // Parameter: -
2996 // Returns: -
2997 // Changes Globals: -
2998 //===========================================================================
2999 void BotShutdownChatAI(void)
3000 {
3001 int i;
3002
3003 //free all remaining chat states
3004 for(i = 0; i < MAX_CLIENTS; i++)
3005 {
3006 if (botchatstates[i])
3007 {
3008 BotFreeChatState(i);
3009 } //end if
3010 } //end for
3011 //free all cached chats
3012 for(i = 0; i < MAX_CLIENTS; i++)
3013 {
3014 if (ichatdata[i])
3015 {
3016 FreeMemory(ichatdata[i]->chat);
3017 FreeMemory(ichatdata[i]);
3018 ichatdata[i] = NULL;
3019 } //end if
3020 } //end for
3021 if (consolemessageheap) FreeMemory(consolemessageheap);
3022 consolemessageheap = NULL;
3023 if (matchtemplates) BotFreeMatchTemplates(matchtemplates);
3024 matchtemplates = NULL;
3025 if (randomstrings) FreeMemory(randomstrings);
3026 randomstrings = NULL;
3027 if (synonyms) FreeMemory(synonyms);
3028 synonyms = NULL;
3029 if (replychats) BotFreeReplyChat(replychats);
3030 replychats = NULL;
3031 } //end of the function BotShutdownChatAI