| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#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 |
|
| 50 |
#define ESCAPE_CHAR 0x01 //'_' |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#define MT_VARIABLE 1 //variable match piece |
| 59 |
#define MT_STRING 2 //string match piece |
| 60 |
|
| 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 |
|
| 71 |
#define CHATMESSAGE_RECENTTIME 20 |
| 72 |
|
| 73 |
|
| 74 |
typedef struct bot_chatmessage_s |
| 75 |
{ |
| 76 |
char *chatmessage; |
| 77 |
float time; |
| 78 |
struct bot_chatmessage_s *next; |
| 79 |
} bot_chatmessage_t; |
| 80 |
|
| 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 |
|
| 89 |
typedef struct bot_chat_s |
| 90 |
{ |
| 91 |
bot_chattype_t *types; |
| 92 |
} bot_chat_t; |
| 93 |
|
| 94 |
|
| 95 |
typedef struct bot_randomstring_s |
| 96 |
{ |
| 97 |
char *string; |
| 98 |
struct bot_randomstring_s *next; |
| 99 |
} bot_randomstring_t; |
| 100 |
|
| 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 |
|
| 110 |
typedef struct bot_synonym_s |
| 111 |
{ |
| 112 |
char *string; |
| 113 |
float weight; |
| 114 |
struct bot_synonym_s *next; |
| 115 |
} bot_synonym_t; |
| 116 |
|
| 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 |
|
| 126 |
typedef struct bot_matchstring_s |
| 127 |
{ |
| 128 |
char *string; |
| 129 |
struct bot_matchstring_s *next; |
| 130 |
} bot_matchstring_t; |
| 131 |
|
| 132 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 169 |
typedef struct bot_stringlist_s |
| 170 |
{ |
| 171 |
char *string; |
| 172 |
struct bot_stringlist_s *next; |
| 173 |
} bot_stringlist_t; |
| 174 |
|
| 175 |
|
| 176 |
typedef struct bot_chatstate_s |
| 177 |
{ |
| 178 |
int gender; |
| 179 |
int client; |
| 180 |
char name[32]; |
| 181 |
char chatmessage[MAX_MESSAGE_SIZE]; |
| 182 |
int handle; |
| 183 |
|
| 184 |
bot_consolemessage_t *firstmessage; |
| 185 |
bot_consolemessage_t *lastmessage; |
| 186 |
|
| 187 |
int numconsolemessages; |
| 188 |
|
| 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 |
|
| 202 |
bot_consolemessage_t *consolemessageheap = NULL; |
| 203 |
bot_consolemessage_t *freeconsolemessages = NULL; |
| 204 |
|
| 205 |
bot_matchtemplate_t *matchtemplates = NULL; |
| 206 |
|
| 207 |
bot_synonymlist_t *synonyms = NULL; |
| 208 |
|
| 209 |
bot_randomlist_t *randomstrings = NULL; |
| 210 |
|
| 211 |
bot_replychat_t *replychats = NULL; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 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 |
} |
| 226 |
if (!botchatstates[handle]) |
| 227 |
{ |
| 228 |
botimport.Print(PRT_FATAL, "invalid chat state %d\n", handle); |
| 229 |
return NULL; |
| 230 |
} |
| 231 |
return botchatstates[handle]; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 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 |
} |
| 256 |
consolemessageheap[max_messages-1].prev = &consolemessageheap[max_messages-2]; |
| 257 |
consolemessageheap[max_messages-1].next = NULL; |
| 258 |
|
| 259 |
freeconsolemessages = consolemessageheap; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 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 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 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 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 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 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 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 |
} |
| 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 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
cs->lastmessage = m; |
| 356 |
cs->firstmessage = m; |
| 357 |
m->prev = NULL; |
| 358 |
} |
| 359 |
cs->numconsolemessages++; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 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 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
return cm->handle; |
| 391 |
} |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 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 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 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 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
void BotRemoveTildes(char *message) |
| 435 |
{ |
| 436 |
int i; |
| 437 |
|
| 438 |
|
| 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 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 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 |
|
| 463 |
|
| 464 |
if (oldptr > string && *ptr) *oldptr++ = ' '; |
| 465 |
|
| 466 |
if (ptr > oldptr) memmove(oldptr, ptr, strlen(ptr)+1); |
| 467 |
} |
| 468 |
while(*ptr && !IsWhiteSpace(*ptr)) ptr++; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 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 |
} |
| 493 |
else |
| 494 |
{ |
| 495 |
if (toupper(str1[j]) != toupper(str2[j])) break; |
| 496 |
} |
| 497 |
} |
| 498 |
if (!str2[j]) return index; |
| 499 |
} |
| 500 |
return -1; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 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 |
|
| 516 |
if (i) |
| 517 |
{ |
| 518 |
|
| 519 |
while(*str1 && *str1 != ' ' && *str1 != '.' && *str1 != ',' && *str1 != '!') str1++; |
| 520 |
if (!*str1) break; |
| 521 |
str1++; |
| 522 |
} |
| 523 |
|
| 524 |
for (j = 0; str2[j]; j++) |
| 525 |
{ |
| 526 |
if (casesensitive) |
| 527 |
{ |
| 528 |
if (str1[j] != str2[j]) break; |
| 529 |
} |
| 530 |
else |
| 531 |
{ |
| 532 |
if (toupper(str1[j]) != toupper(str2[j])) break; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
if (!str2[j]) |
| 537 |
{ |
| 538 |
|
| 539 |
if (!str1[j] || str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '!') return str1; |
| 540 |
} |
| 541 |
} |
| 542 |
return NULL; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
void StringReplaceWords(char *string, char *synonym, char *replacement) |
| 551 |
{ |
| 552 |
char *str, *str2; |
| 553 |
|
| 554 |
|
| 555 |
str = StringContainsWord(string, synonym, qfalse); |
| 556 |
|
| 557 |
while(str) |
| 558 |
{ |
| 559 |
|
| 560 |
|
| 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 |
} |
| 567 |
if (!str2) |
| 568 |
{ |
| 569 |
memmove(str + strlen(replacement), str+strlen(synonym), strlen(str+strlen(synonym))+1); |
| 570 |
|
| 571 |
Com_Memcpy(str, replacement, strlen(replacement)); |
| 572 |
} |
| 573 |
|
| 574 |
str = StringContainsWord(str+strlen(replacement), synonym, qfalse); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 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 |
} |
| 599 |
fprintf(fp, "]\n"); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 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; |
| 620 |
syn = NULL; |
| 621 |
synonym = NULL; |
| 622 |
|
| 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 |
} |
| 635 |
|
| 636 |
context = 0; |
| 637 |
contextlevel = 0; |
| 638 |
synlist = NULL; |
| 639 |
lastsyn = NULL; |
| 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 |
} |
| 654 |
if (!PC_ExpectTokenString(source, "{")) |
| 655 |
{ |
| 656 |
FreeSource(source); |
| 657 |
return NULL; |
| 658 |
} |
| 659 |
} |
| 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 |
} |
| 671 |
context &= ~contextstack[contextlevel]; |
| 672 |
} |
| 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 |
} |
| 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 |
} |
| 698 |
StripDoubleQuotes(token.string); |
| 699 |
if (strlen(token.string) <= 0) |
| 700 |
{ |
| 701 |
SourceError(source, "empty string"); |
| 702 |
FreeSource(source); |
| 703 |
return NULL; |
| 704 |
} |
| 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 |
} |
| 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 |
} |
| 728 |
if (pass && ptr) |
| 729 |
{ |
| 730 |
synonym->weight = token.floatvalue; |
| 731 |
syn->totalweight += synonym->weight; |
| 732 |
} |
| 733 |
if (PC_CheckTokenString(source, "]")) break; |
| 734 |
if (!PC_ExpectTokenString(source, ",")) |
| 735 |
{ |
| 736 |
FreeSource(source); |
| 737 |
return NULL; |
| 738 |
} |
| 739 |
} |
| 740 |
if (numsynonyms < 2) |
| 741 |
{ |
| 742 |
SourceError(source, "synonym must have at least two entries"); |
| 743 |
FreeSource(source); |
| 744 |
return NULL; |
| 745 |
} |
| 746 |
} |
| 747 |
else |
| 748 |
{ |
| 749 |
SourceError(source, "unexpected %s", token.string); |
| 750 |
FreeSource(source); |
| 751 |
return NULL; |
| 752 |
} |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
FreeSource(source); |
| 757 |
|
| 758 |
if (contextlevel > 0) |
| 759 |
{ |
| 760 |
SourceError(source, "missing }"); |
| 761 |
return NULL; |
| 762 |
} |
| 763 |
} |
| 764 |
botimport.Print(PRT_MESSAGE, "loaded %s\n", filename); |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
return synlist; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 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 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 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 |
|
| 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 |
} |
| 815 |
if (!replacement) continue; |
| 816 |
|
| 817 |
for (synonym = syn->firstsynonym; synonym; synonym = synonym->next) |
| 818 |
{ |
| 819 |
if (synonym == replacement) continue; |
| 820 |
StringReplaceWords(string, synonym->string, replacement->string); |
| 821 |
} |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 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 |
|
| 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 |
|
| 848 |
str2 = StringContainsWord(str1, synonym->string, qfalse); |
| 849 |
if (!str2 || str2 != str1) continue; |
| 850 |
|
| 851 |
replacement = syn->firstsynonym->string; |
| 852 |
|
| 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 |
|
| 859 |
Com_Memcpy(str1, replacement, strlen(replacement)); |
| 860 |
|
| 861 |
break; |
| 862 |
} |
| 863 |
|
| 864 |
if (synonym) break; |
| 865 |
} |
| 866 |
|
| 867 |
while(*str1 && *str1 > ' ') str1++; |
| 868 |
if (!*str1) break; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 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 |
|
| 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 |
} |
| 897 |
strcat(ptr, token.string); |
| 898 |
} |
| 899 |
|
| 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 |
} |
| 907 |
sprintf(&ptr[strlen(ptr)], "%cv%ld%c", ESCAPE_CHAR, token.intvalue, ESCAPE_CHAR); |
| 908 |
} |
| 909 |
|
| 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 |
} |
| 917 |
sprintf(&ptr[strlen(ptr)], "%cr%s%c", ESCAPE_CHAR, token.string, ESCAPE_CHAR); |
| 918 |
} |
| 919 |
else |
| 920 |
{ |
| 921 |
SourceError(source, "unknown message component %s", token.string); |
| 922 |
return qfalse; |
| 923 |
} |
| 924 |
if (PC_CheckTokenString(source, ";")) break; |
| 925 |
if (!PC_ExpectTokenString(source, ",")) return qfalse; |
| 926 |
} |
| 927 |
|
| 928 |
return qtrue; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 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 |
} |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 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 |
| 973 |
|
| 974 |
size = 0; |
| 975 |
randomlist = NULL; |
| 976 |
random = NULL; |
| 977 |
|
| 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 |
} |
| 990 |
|
| 991 |
randomlist = NULL; |
| 992 |
lastrandom = NULL; |
| 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 |
} |
| 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 |
} |
| 1020 |
if (!PC_ExpectTokenString(source, "=") || |
| 1021 |
!PC_ExpectTokenString(source, "{")) |
| 1022 |
{ |
| 1023 |
FreeSource(source); |
| 1024 |
return NULL; |
| 1025 |
} |
| 1026 |
while(!PC_CheckTokenString(source, "}")) |
| 1027 |
{ |
| 1028 |
if (!BotLoadChatMessage(source, chatmessagestring)) |
| 1029 |
{ |
| 1030 |
FreeSource(source); |
| 1031 |
return NULL; |
| 1032 |
} |
| 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 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
FreeSource(source); |
| 1052 |
} |
| 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 |
|
| 1058 |
#endif |
| 1059 |
|
| 1060 |
return randomlist; |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 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 |
} |
| 1083 |
if (rs) |
| 1084 |
{ |
| 1085 |
return rs->string; |
| 1086 |
} |
| 1087 |
} |
| 1088 |
} |
| 1089 |
return NULL; |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 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 |
} |
| 1118 |
} |
| 1119 |
else if (mp->type == MT_VARIABLE) |
| 1120 |
{ |
| 1121 |
fprintf(fp, "%d", mp->variable); |
| 1122 |
} |
| 1123 |
if (mp->next) fprintf(fp, ", "); |
| 1124 |
} |
| 1125 |
fprintf(fp, " = (%d, %d);}\n", mt->type, mt->subtype); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 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 |
} |
| 1149 |
} |
| 1150 |
FreeMemory(mp); |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 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 |
} |
| 1182 |
if (lastwasvariable) |
| 1183 |
{ |
| 1184 |
SourceError(source, "not allowed to have adjacent variables"); |
| 1185 |
FreeSource(source); |
| 1186 |
BotFreeMatchPieces(firstpiece); |
| 1187 |
return NULL; |
| 1188 |
} |
| 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 |
} |
| 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 |
} |
| 1224 |
} |
| 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 |
|
| 1236 |
if (!emptystring) lastwasvariable = qfalse; |
| 1237 |
} |
| 1238 |
else |
| 1239 |
{ |
| 1240 |
SourceError(source, "invalid token %s", token.string); |
| 1241 |
FreeSource(source); |
| 1242 |
BotFreeMatchPieces(firstpiece); |
| 1243 |
return NULL; |
| 1244 |
} |
| 1245 |
if (PC_CheckTokenString(source, endtoken)) break; |
| 1246 |
if (!PC_ExpectTokenString(source, ",")) |
| 1247 |
{ |
| 1248 |
FreeSource(source); |
| 1249 |
BotFreeMatchPieces(firstpiece); |
| 1250 |
return NULL; |
| 1251 |
} |
| 1252 |
} |
| 1253 |
return firstpiece; |
| 1254 |
} |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 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 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 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 |
} |
| 1292 |
|
| 1293 |
matches = NULL; |
| 1294 |
lastmatch = NULL; |
| 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 |
} |
| 1305 |
|
| 1306 |
context = token.intvalue; |
| 1307 |
|
| 1308 |
if (!PC_ExpectTokenString(source, "{")) |
| 1309 |
{ |
| 1310 |
BotFreeMatchTemplates(matches); |
| 1311 |
FreeSource(source); |
| 1312 |
return NULL; |
| 1313 |
} |
| 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 |
|
| 1325 |
if (lastmatch) lastmatch->next = matchtemplate; |
| 1326 |
else matches = matchtemplate; |
| 1327 |
lastmatch = matchtemplate; |
| 1328 |
|
| 1329 |
matchtemplate->first = BotLoadMatchPieces(source, "="); |
| 1330 |
if (!matchtemplate->first) |
| 1331 |
{ |
| 1332 |
BotFreeMatchTemplates(matches); |
| 1333 |
return NULL; |
| 1334 |
} |
| 1335 |
|
| 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 |
} |
| 1343 |
matchtemplate->type = token.intvalue; |
| 1344 |
|
| 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 |
} |
| 1352 |
matchtemplate->subtype = token.intvalue; |
| 1353 |
|
| 1354 |
if (!PC_ExpectTokenString(source, ")") || |
| 1355 |
!PC_ExpectTokenString(source, ";")) |
| 1356 |
{ |
| 1357 |
BotFreeMatchTemplates(matches); |
| 1358 |
FreeSource(source); |
| 1359 |
return NULL; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
FreeSource(source); |
| 1365 |
botimport.Print(PRT_MESSAGE, "loaded %s\n", matchfile); |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
return matches; |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 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 |
|
| 1385 |
lastvariable = -1; |
| 1386 |
|
| 1387 |
strptr = match->string; |
| 1388 |
|
| 1389 |
|
| 1390 |
for (mp = pieces; mp; mp = mp->next) |
| 1391 |
{ |
| 1392 |
|
| 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 |
} |
| 1403 |
|
| 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 |
|
| 1413 |
lastvariable = -1; |
| 1414 |
break; |
| 1415 |
} |
| 1416 |
else if (index == 0) |
| 1417 |
{ |
| 1418 |
break; |
| 1419 |
} |
| 1420 |
newstrptr = NULL; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
if (!newstrptr) return qfalse; |
| 1424 |
strptr = newstrptr + strlen(ms->string); |
| 1425 |
} |
| 1426 |
|
| 1427 |
else if (mp->type == MT_VARIABLE) |
| 1428 |
{ |
| 1429 |
|
| 1430 |
match->variables[mp->variable].offset = strptr - match->string; |
| 1431 |
lastvariable = mp->variable; |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
if (!mp && (lastvariable >= 0 || !strlen(strptr))) |
| 1436 |
{ |
| 1437 |
|
| 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 |
} |
| 1444 |
return qtrue; |
| 1445 |
} |
| 1446 |
return qfalse; |
| 1447 |
} |
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 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 |
|
| 1461 |
while(strlen(match->string) && |
| 1462 |
match->string[strlen(match->string)-1] == '\n') |
| 1463 |
{ |
| 1464 |
match->string[strlen(match->string)-1] = '\0'; |
| 1465 |
} |
| 1466 |
|
| 1467 |
for (ms = matchtemplates; ms; ms = ms->next) |
| 1468 |
{ |
| 1469 |
if (!(ms->context & context)) continue; |
| 1470 |
|
| 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 |
} |
| 1479 |
} |
| 1480 |
return qfalse; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 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 |
} |
| 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 |
} |
| 1504 |
else |
| 1505 |
{ |
| 1506 |
strcpy(buf, ""); |
| 1507 |
} |
| 1508 |
} |
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 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 |
} |
| 1523 |
return NULL; |
| 1524 |
} |
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 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': |
| 1548 |
{ |
| 1549 |
|
| 1550 |
msgptr++; |
| 1551 |
while(*msgptr && *msgptr != ESCAPE_CHAR) msgptr++; |
| 1552 |
|
| 1553 |
if (*msgptr) msgptr++; |
| 1554 |
break; |
| 1555 |
} |
| 1556 |
case 'r': |
| 1557 |
{ |
| 1558 |
|
| 1559 |
msgptr++; |
| 1560 |
for (i = 0; (*msgptr && *msgptr != ESCAPE_CHAR); i++) |
| 1561 |
{ |
| 1562 |
temp[i] = *msgptr++; |
| 1563 |
} |
| 1564 |
temp[i] = '\0'; |
| 1565 |
|
| 1566 |
if (*msgptr) msgptr++; |
| 1567 |
|
| 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 |
} |
| 1579 |
} |
| 1580 |
break; |
| 1581 |
} |
| 1582 |
default: |
| 1583 |
{ |
| 1584 |
botimport.Print(PRT_FATAL, "BotCheckChatMessageIntegrety: message \"%s\" invalid escape char\n", message); |
| 1585 |
break; |
| 1586 |
} |
| 1587 |
} |
| 1588 |
} |
| 1589 |
else |
| 1590 |
{ |
| 1591 |
msgptr++; |
| 1592 |
} |
| 1593 |
} |
| 1594 |
return stringlist; |
| 1595 |
} |
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 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 |
} |
| 1615 |
} |
| 1616 |
for (s = stringlist; s; s = nexts) |
| 1617 |
{ |
| 1618 |
nexts = s->next; |
| 1619 |
FreeMemory(s); |
| 1620 |
} |
| 1621 |
} |
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 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 |
} |
| 1641 |
} |
| 1642 |
for (s = stringlist; s; s = nexts) |
| 1643 |
{ |
| 1644 |
nexts = s->next; |
| 1645 |
FreeMemory(s); |
| 1646 |
} |
| 1647 |
} |
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 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 |
} |
| 1686 |
fprintf(fp, ")"); |
| 1687 |
} |
| 1688 |
else if (key->flags & RCKFL_STRING) |
| 1689 |
{ |
| 1690 |
fprintf(fp, "\"%s\"", key->string); |
| 1691 |
} |
| 1692 |
if (key->next) fprintf(fp, ", "); |
| 1693 |
else fprintf(fp, "] = %1.0f\n", rp->priority); |
| 1694 |
} |
| 1695 |
fprintf(fp, "{\n"); |
| 1696 |
for (cm = rp->firstchatmessage; cm; cm = cm->next) |
| 1697 |
{ |
| 1698 |
fprintf(fp, "\t\"%s\";\n", cm->chatmessage); |
| 1699 |
} |
| 1700 |
fprintf(fp, "}\n"); |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 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 |
} |
| 1725 |
for (cm = rp->firstchatmessage; cm; cm = nextcm) |
| 1726 |
{ |
| 1727 |
nextcm = cm->next; |
| 1728 |
FreeMemory(cm); |
| 1729 |
} |
| 1730 |
FreeMemory(rp); |
| 1731 |
} |
| 1732 |
} |
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 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 |
} |
| 1760 |
} |
| 1761 |
else if (key->flags & RCKFL_STRING) |
| 1762 |
{ |
| 1763 |
hasstringkey = qtrue; |
| 1764 |
} |
| 1765 |
} |
| 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 |
} |
| 1784 |
} |
| 1785 |
if (ms) break; |
| 1786 |
} |
| 1787 |
else if (m->type == MT_VARIABLE) |
| 1788 |
{ |
| 1789 |
break; |
| 1790 |
} |
| 1791 |
} |
| 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 |
} |
| 1797 |
} |
| 1798 |
} |
| 1799 |
} |
| 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 |
} |
| 1812 |
} |
| 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 |
} |
| 1826 |
} |
| 1827 |
} |
| 1828 |
} |
| 1829 |
} |
| 1830 |
} |
| 1831 |
} |
| 1832 |
} |
| 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 |
} |
| 1839 |
} |
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 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 |
} |
| 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 |
} |
| 1875 |
|
| 1876 |
replychat = GetClearedHunkMemory(sizeof(bot_replychat_t)); |
| 1877 |
replychat->keys = NULL; |
| 1878 |
replychat->next = replychatlist; |
| 1879 |
replychatlist = replychat; |
| 1880 |
|
| 1881 |
do |
| 1882 |
{ |
| 1883 |
|
| 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 |
|
| 1891 |
if (PC_CheckTokenString(source, "&")) key->flags |= RCKFL_AND; |
| 1892 |
else if (PC_CheckTokenString(source, "!")) key->flags |= RCKFL_NOT; |
| 1893 |
|
| 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, "(")) |
| 1899 |
{ |
| 1900 |
key->flags |= RCKFL_VARIABLES; |
| 1901 |
key->match = BotLoadMatchPieces(source, ")"); |
| 1902 |
if (!key->match) |
| 1903 |
{ |
| 1904 |
BotFreeReplyChat(replychatlist); |
| 1905 |
return NULL; |
| 1906 |
} |
| 1907 |
} |
| 1908 |
else if (PC_CheckTokenString(source, "<")) |
| 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 |
} |
| 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 |
} |
| 1930 |
key->string = (char *) GetClearedHunkMemory(strlen(namebuffer) + 1); |
| 1931 |
strcpy(key->string, namebuffer); |
| 1932 |
} |
| 1933 |
else |
| 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 |
} |
| 1942 |
StripDoubleQuotes(token.string); |
| 1943 |
key->string = (char *) GetClearedHunkMemory(strlen(token.string) + 1); |
| 1944 |
strcpy(key->string, token.string); |
| 1945 |
} |
| 1946 |
|
| 1947 |
PC_CheckTokenString(source, ","); |
| 1948 |
} while(!PC_CheckTokenString(source, "]")); |
| 1949 |
|
| 1950 |
BotCheckValidReplyChatKeySet(source, replychat->keys); |
| 1951 |
|
| 1952 |
if (!PC_ExpectTokenString(source, "=") || |
| 1953 |
!PC_ExpectTokenType(source, TT_NUMBER, 0, &token)) |
| 1954 |
{ |
| 1955 |
BotFreeReplyChat(replychatlist); |
| 1956 |
FreeSource(source); |
| 1957 |
return NULL; |
| 1958 |
} |
| 1959 |
replychat->priority = token.floatvalue; |
| 1960 |
|
| 1961 |
if (!PC_ExpectTokenString(source, "{")) |
| 1962 |
{ |
| 1963 |
BotFreeReplyChat(replychatlist); |
| 1964 |
FreeSource(source); |
| 1965 |
return NULL; |
| 1966 |
} |
| 1967 |
replychat->numchatmessages = 0; |
| 1968 |
|
| 1969 |
while(!PC_CheckTokenString(source, "}")) |
| 1970 |
{ |
| 1971 |
if (!BotLoadChatMessage(source, chatmessagestring)) |
| 1972 |
{ |
| 1973 |
BotFreeReplyChat(replychatlist); |
| 1974 |
FreeSource(source); |
| 1975 |
return NULL; |
| 1976 |
} |
| 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 |
|
| 1983 |
replychat->firstchatmessage = chatmessage; |
| 1984 |
replychat->numchatmessages++; |
| 1985 |
} |
| 1986 |
} |
| 1987 |
FreeSource(source); |
| 1988 |
botimport.Print(PRT_MESSAGE, "loaded %s\n", filename); |
| 1989 |
|
| 1990 |
|
| 1991 |
if (botDeveloper) |
| 1992 |
{ |
| 1993 |
BotCheckReplyChatIntegrety(replychatlist); |
| 1994 |
} |
| 1995 |
|
| 1996 |
if (!replychatlist) botimport.Print(PRT_MESSAGE, "no rchats\n"); |
| 1997 |
|
| 1998 |
return replychatlist; |
| 1999 |
} |
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 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 |
} |
| 2021 |
Log_Write(" }"); |
| 2022 |
} |
| 2023 |
Log_Write("}"); |
| 2024 |
} |
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 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 |
| 2046 |
|
| 2047 |
size = 0; |
| 2048 |
foundchat = qfalse; |
| 2049 |
|
| 2050 |
for (pass = 0; pass < 2; pass++) |
| 2051 |
{ |
| 2052 |
|
| 2053 |
if (pass && size) ptr = (char *) GetClearedMemory(size); |
| 2054 |
|
| 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 |
} |
| 2062 |
|
| 2063 |
if (pass) |
| 2064 |
{ |
| 2065 |
chat = (bot_chat_t *) ptr; |
| 2066 |
ptr += sizeof(bot_chat_t); |
| 2067 |
} |
| 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 |
} |
| 2079 |
StripDoubleQuotes(token.string); |
| 2080 |
|
| 2081 |
if (!PC_ExpectTokenString(source, "{")) |
| 2082 |
{ |
| 2083 |
FreeSource(source); |
| 2084 |
return NULL; |
| 2085 |
} |
| 2086 |
|
| 2087 |
if (!Q_stricmp(token.string, chatname)) |
| 2088 |
{ |
| 2089 |
foundchat = qtrue; |
| 2090 |
|
| 2091 |
while(1) |
| 2092 |
{ |
| 2093 |
if (!PC_ExpectAnyToken(source, &token)) |
| 2094 |
{ |
| 2095 |
FreeSource(source); |
| 2096 |
return NULL; |
| 2097 |
} |
| 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 |
} |
| 2105 |
|
| 2106 |
if (!PC_ExpectTokenType(source, TT_STRING, 0, &token) || |
| 2107 |
!PC_ExpectTokenString(source, "{")) |
| 2108 |
{ |
| 2109 |
FreeSource(source); |
| 2110 |
return NULL; |
| 2111 |
} |
| 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 |
|
| 2119 |
chattype->next = chat->types; |
| 2120 |
chat->types = chattype; |
| 2121 |
|
| 2122 |
ptr += sizeof(bot_chattype_t); |
| 2123 |
} |
| 2124 |
size += sizeof(bot_chattype_t); |
| 2125 |
|
| 2126 |
while(!PC_CheckTokenString(source, "}")) |
| 2127 |
{ |
| 2128 |
size_t len; |
| 2129 |
if (!BotLoadChatMessage(source, chatmessagestring)) |
| 2130 |
{ |
| 2131 |
FreeSource(source); |
| 2132 |
return NULL; |
| 2133 |
} |
| 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 |
|
| 2141 |
chatmessage->next = chattype->firstchatmessage; |
| 2142 |
chattype->firstchatmessage = chatmessage; |
| 2143 |
|
| 2144 |
ptr += sizeof(bot_chatmessage_t); |
| 2145 |
chatmessage->chatmessage = ptr; |
| 2146 |
strcpy(chatmessage->chatmessage, chatmessagestring); |
| 2147 |
ptr += len; |
| 2148 |
|
| 2149 |
chattype->numchatmessages++; |
| 2150 |
} |
| 2151 |
size += sizeof(bot_chatmessage_t) + len; |
| 2152 |
} |
| 2153 |
} |
| 2154 |
} |
| 2155 |
else |
| 2156 |
{ |
| 2157 |
indent = 1; |
| 2158 |
while(indent) |
| 2159 |
{ |
| 2160 |
if (!PC_ExpectAnyToken(source, &token)) |
| 2161 |
{ |
| 2162 |
FreeSource(source); |
| 2163 |
return NULL; |
| 2164 |
} |
| 2165 |
if (!strcmp(token.string, "{")) indent++; |
| 2166 |
else if (!strcmp(token.string, "}")) indent--; |
| 2167 |
} |
| 2168 |
} |
| 2169 |
} |
| 2170 |
else |
| 2171 |
{ |
| 2172 |
SourceError(source, "unknown definition %s", token.string); |
| 2173 |
FreeSource(source); |
| 2174 |
return NULL; |
| 2175 |
} |
| 2176 |
} |
| 2177 |
|
| 2178 |
FreeSource(source); |
| 2179 |
|
| 2180 |
if (!foundchat) |
| 2181 |
{ |
| 2182 |
botimport.Print(PRT_ERROR, "couldn't find chat %s in %s\n", chatname, chatfile); |
| 2183 |
return NULL; |
| 2184 |
} |
| 2185 |
} |
| 2186 |
|
| 2187 |
botimport.Print(PRT_MESSAGE, "loaded %s from %s\n", chatname, chatfile); |
| 2188 |
|
| 2189 |
|
| 2190 |
if (botDeveloper) |
| 2191 |
{ |
| 2192 |
BotCheckInitialChatIntegrety(chat); |
| 2193 |
} |
| 2194 |
#ifdef DEBUG |
| 2195 |
botimport.Print(PRT_MESSAGE, "initial chats loaded in %d msec\n", Sys_MilliSeconds() - starttime); |
| 2196 |
#endif |
| 2197 |
|
| 2198 |
return chat; |
| 2199 |
} |
| 2200 |
|
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 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 |
} |
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
|
| 2219 |
|
| 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 |
|
| 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 |
} |
| 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 |
} |
| 2270 |
|
| 2271 |
return BLERR_NOERROR; |
| 2272 |
} |
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 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': |
| 2299 |
{ |
| 2300 |
msgptr++; |
| 2301 |
num = 0; |
| 2302 |
while(*msgptr && *msgptr != ESCAPE_CHAR) |
| 2303 |
{ |
| 2304 |
num = num * 10 + (*msgptr++) - '0'; |
| 2305 |
} |
| 2306 |
|
| 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 |
} |
| 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 |
} |
| 2321 |
temp[i] = 0; |
| 2322 |
|
| 2323 |
if (reply) |
| 2324 |
{ |
| 2325 |
|
| 2326 |
BotReplaceReplySynonyms(temp, vcontext); |
| 2327 |
} |
| 2328 |
else |
| 2329 |
{ |
| 2330 |
|
| 2331 |
BotReplaceSynonyms(temp, vcontext); |
| 2332 |
} |
| 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 |
} |
| 2339 |
strcpy(&outputbuf[len], temp); |
| 2340 |
len += strlen(temp); |
| 2341 |
} |
| 2342 |
break; |
| 2343 |
} |
| 2344 |
case 'r': |
| 2345 |
{ |
| 2346 |
msgptr++; |
| 2347 |
for (i = 0; (*msgptr && *msgptr != ESCAPE_CHAR); i++) |
| 2348 |
{ |
| 2349 |
temp[i] = *msgptr++; |
| 2350 |
} |
| 2351 |
temp[i] = '\0'; |
| 2352 |
|
| 2353 |
if (*msgptr) msgptr++; |
| 2354 |
|
| 2355 |
ptr = RandomString(temp); |
| 2356 |
if (!ptr) |
| 2357 |
{ |
| 2358 |
botimport.Print(PRT_ERROR, "BotConstructChat: unknown random string %s\n", temp); |
| 2359 |
return qfalse; |
| 2360 |
} |
| 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 |
} |
| 2366 |
strcpy(&outputbuf[len], ptr); |
| 2367 |
len += strlen(ptr); |
| 2368 |
expansion = qtrue; |
| 2369 |
break; |
| 2370 |
} |
| 2371 |
default: |
| 2372 |
{ |
| 2373 |
botimport.Print(PRT_FATAL, "BotConstructChat: message \"%s\" invalid escape char\n", message); |
| 2374 |
break; |
| 2375 |
} |
| 2376 |
} |
| 2377 |
} |
| 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 |
} |
| 2386 |
} |
| 2387 |
} |
| 2388 |
outputbuf[len] = '\0'; |
| 2389 |
|
| 2390 |
BotReplaceWeightedSynonyms(outputbuf, mcontext); |
| 2391 |
|
| 2392 |
return expansion; |
| 2393 |
} |
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 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 |
} |
| 2413 |
strcpy(srcmessage, chatstate->chatmessage); |
| 2414 |
} |
| 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 |
} |
| 2420 |
} |
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 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 |
} |
| 2447 |
|
| 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 |
} |
| 2459 |
} |
| 2460 |
if (bestchatmessage) return bestchatmessage->chatmessage; |
| 2461 |
} |
| 2462 |
else |
| 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 |
} |
| 2473 |
} |
| 2474 |
} |
| 2475 |
return NULL; |
| 2476 |
} |
| 2477 |
} |
| 2478 |
return NULL; |
| 2479 |
} |
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
|
| 2484 |
|
| 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 |
} |
| 2504 |
} |
| 2505 |
return 0; |
| 2506 |
} |
| 2507 |
|
| 2508 |
|
| 2509 |
|
| 2510 |
|
| 2511 |
|
| 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 |
|
| 2523 |
if (!cs->chat) return; |
| 2524 |
|
| 2525 |
message = BotChooseInitialChatMessage(cs, type); |
| 2526 |
|
| 2527 |
if (!message) |
| 2528 |
{ |
| 2529 |
#ifdef DEBUG |
| 2530 |
botimport.Print(PRT_MESSAGE, "no chat messages of type %s\n", type); |
| 2531 |
#endif |
| 2532 |
return; |
| 2533 |
} |
| 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 |
} |
| 2587 |
|
| 2588 |
|
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 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 |
} |
| 2617 |
botimport.Print(PRT_MESSAGE, ")"); |
| 2618 |
} |
| 2619 |
else if (key->flags & RCKFL_STRING) |
| 2620 |
{ |
| 2621 |
botimport.Print(PRT_MESSAGE, "\"%s\"", key->string); |
| 2622 |
} |
| 2623 |
if (key->next) botimport.Print(PRT_MESSAGE, ", "); |
| 2624 |
else botimport.Print(PRT_MESSAGE, "] = %1.0f\n", replychat->priority); |
| 2625 |
} |
| 2626 |
botimport.Print(PRT_MESSAGE, "{\n"); |
| 2627 |
} |
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2666 |
if (key->flags & RCKFL_AND) |
| 2667 |
{ |
| 2668 |
if (!res) |
| 2669 |
{ |
| 2670 |
found = qfalse; |
| 2671 |
break; |
| 2672 |
} |
| 2673 |
} |
| 2674 |
|
| 2675 |
else if (key->flags & RCKFL_NOT) |
| 2676 |
{ |
| 2677 |
if (res) |
| 2678 |
{ |
| 2679 |
found = qfalse; |
| 2680 |
break; |
| 2681 |
} |
| 2682 |
} |
| 2683 |
else if (res) |
| 2684 |
{ |
| 2685 |
found = qtrue; |
| 2686 |
} |
| 2687 |
} |
| 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 |
} |
| 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 |
} |
| 2705 |
|
| 2706 |
if (m) |
| 2707 |
{ |
| 2708 |
Com_Memcpy(&bestmatch, &match, sizeof(bot_match_t)); |
| 2709 |
bestchatmessage = m; |
| 2710 |
bestrchat = rchat; |
| 2711 |
bestpriority = rchat->priority; |
| 2712 |
} |
| 2713 |
} |
| 2714 |
} |
| 2715 |
} |
| 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 |
} |
| 2774 |
} |
| 2775 |
else |
| 2776 |
{ |
| 2777 |
bestchatmessage->time = AAS_Time() + CHATMESSAGE_RECENTTIME; |
| 2778 |
BotConstructChatMessage(cs, bestchatmessage->chatmessage, mcontext, &bestmatch, vcontext, qtrue); |
| 2779 |
} |
| 2780 |
return qtrue; |
| 2781 |
} |
| 2782 |
return qfalse; |
| 2783 |
} |
| 2784 |
|
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
|
| 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 |
} |
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 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: |
| 2826 |
EA_Command(cs->client, va("say %s", cs->chatmessage)); |
| 2827 |
break; |
| 2828 |
} |
| 2829 |
} |
| 2830 |
|
| 2831 |
strcpy(cs->chatmessage, ""); |
| 2832 |
} |
| 2833 |
} |
| 2834 |
|
| 2835 |
|
| 2836 |
|
| 2837 |
|
| 2838 |
|
| 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 |
|
| 2850 |
strcpy(cs->chatmessage, ""); |
| 2851 |
} |
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
|
| 2856 |
|
| 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 |
} |
| 2870 |
} |
| 2871 |
|
| 2872 |
|
| 2873 |
|
| 2874 |
|
| 2875 |
|
| 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 |
} |
| 2886 |
|
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
|
| 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 |
} |
| 2903 |
} |
| 2904 |
} |
| 2905 |
|
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 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 |
} |
| 2922 |
} |
| 2923 |
return 0; |
| 2924 |
} |
| 2925 |
|
| 2926 |
|
| 2927 |
|
| 2928 |
|
| 2929 |
|
| 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 |
} |
| 2941 |
if (!botchatstates[handle]) |
| 2942 |
{ |
| 2943 |
botimport.Print(PRT_FATAL, "invalid chat state %d\n", handle); |
| 2944 |
return; |
| 2945 |
} |
| 2946 |
if (LibVarGetValue("bot_reloadcharacters")) |
| 2947 |
{ |
| 2948 |
BotFreeChatFile(handle); |
| 2949 |
} |
| 2950 |
|
| 2951 |
for (h = BotNextConsoleMessage(handle, &m); h; h = BotNextConsoleMessage(handle, &m)) |
| 2952 |
{ |
| 2953 |
|
| 2954 |
BotRemoveConsoleMessage(handle, h); |
| 2955 |
} |
| 2956 |
FreeMemory(botchatstates[handle]); |
| 2957 |
botchatstates[handle] = NULL; |
| 2958 |
} |
| 2959 |
|
| 2960 |
|
| 2961 |
|
| 2962 |
|
| 2963 |
|
| 2964 |
|
| 2965 |
int BotSetupChatAI(void) |
| 2966 |
{ |
| 2967 |
char *file; |
| 2968 |
|
| 2969 |
#ifdef DEBUG |
| 2970 |
int starttime = Sys_MilliSeconds(); |
| 2971 |
#endif |
| 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 |
} |
| 2985 |
|
| 2986 |
InitConsoleMessageHeap(); |
| 2987 |
|
| 2988 |
#ifdef DEBUG |
| 2989 |
botimport.Print(PRT_MESSAGE, "setup chat AI %d msec\n", Sys_MilliSeconds() - starttime); |
| 2990 |
#endif |
| 2991 |
return BLERR_NOERROR; |
| 2992 |
} |
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 2997 |
|
| 2998 |
|
| 2999 |
void BotShutdownChatAI(void) |
| 3000 |
{ |
| 3001 |
int i; |
| 3002 |
|
| 3003 |
|
| 3004 |
for(i = 0; i < MAX_CLIENTS; i++) |
| 3005 |
{ |
| 3006 |
if (botchatstates[i]) |
| 3007 |
{ |
| 3008 |
BotFreeChatState(i); |
| 3009 |
} |
| 3010 |
} |
| 3011 |
|
| 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 |
} |
| 3020 |
} |
| 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 |
} |