📄 be_ai_chat.c
字号:
//read trailing punctuations
if (!PC_ExpectTokenString(source, ")") ||
!PC_ExpectTokenString(source, ";"))
{
BotFreeMatchTemplates(matches);
FreeSource(source);
return NULL;
} //end if
} //end while
} //end while
//free the source
FreeSource(source);
botimport.Print(PRT_MESSAGE, "loaded %s\n", matchfile);
//
//BotDumpMatchTemplates(matches);
//
return matches;
} //end of the function BotLoadMatchTemplates
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int StringsMatch(bot_matchpiece_t *pieces, bot_match_t *match)
{
int lastvariable, index;
char *strptr, *newstrptr;
bot_matchpiece_t *mp;
bot_matchstring_t *ms;
//no last variable
lastvariable = -1;
//pointer to the string to compare the match string with
strptr = match->string;
//Log_Write("match: %s", strptr);
//compare the string with the current match string
for (mp = pieces; mp; mp = mp->next)
{
//if it is a piece of string
if (mp->type == MT_STRING)
{
newstrptr = NULL;
for (ms = mp->firststring; ms; ms = ms->next)
{
if (!strlen(ms->string))
{
newstrptr = strptr;
break;
} //end if
//Log_Write("MT_STRING: %s", mp->string);
index = StringContains(strptr, ms->string, qfalse);
if (index >= 0)
{
newstrptr = strptr + index;
if (lastvariable >= 0)
{
match->variables[lastvariable].length =
(newstrptr - match->string) - match->variables[lastvariable].offset;
//newstrptr - match->variables[lastvariable].ptr;
lastvariable = -1;
break;
} //end if
else if (index == 0)
{
break;
} //end else
newstrptr = NULL;
} //end if
} //end for
if (!newstrptr) return qfalse;
strptr = newstrptr + strlen(ms->string);
} //end if
//if it is a variable piece of string
else if (mp->type == MT_VARIABLE)
{
//Log_Write("MT_VARIABLE");
match->variables[mp->variable].offset = strptr - match->string;
lastvariable = mp->variable;
} //end else if
} //end for
//if a match was found
if (!mp && (lastvariable >= 0 || !strlen(strptr)))
{
//if the last piece was a variable string
if (lastvariable >= 0)
{
assert( match->variables[lastvariable].offset >= 0 ); // bk001204
match->variables[lastvariable].length =
strlen(&match->string[ (int) match->variables[lastvariable].offset]);
} //end if
return qtrue;
} //end if
return qfalse;
} //end of the function StringsMatch
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BotFindMatch(char *str, bot_match_t *match, unsigned long int context)
{
int i;
bot_matchtemplate_t *ms;
strncpy(match->string, str, MAX_MESSAGE_SIZE);
//remove any trailing enters
while(strlen(match->string) &&
match->string[strlen(match->string)-1] == '\n')
{
match->string[strlen(match->string)-1] = '\0';
} //end while
//compare the string with all the match strings
for (ms = matchtemplates; ms; ms = ms->next)
{
if (!(ms->context & context)) continue;
//reset the match variable offsets
for (i = 0; i < MAX_MATCHVARIABLES; i++) match->variables[i].offset = -1;
//
if (StringsMatch(ms->first, match))
{
match->type = ms->type;
match->subtype = ms->subtype;
return qtrue;
} //end if
} //end for
return qfalse;
} //end of the function BotFindMatch
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotMatchVariable(bot_match_t *match, int variable, char *buf, int size)
{
if (variable < 0 || variable >= MAX_MATCHVARIABLES)
{
botimport.Print(PRT_FATAL, "BotMatchVariable: variable out of range\n");
strcpy(buf, "");
return;
} //end if
if (match->variables[variable].offset >= 0)
{
if (match->variables[variable].length < size)
size = match->variables[variable].length+1;
assert( match->variables[variable].offset >= 0 ); // bk001204
strncpy(buf, &match->string[ (int) match->variables[variable].offset], size-1);
buf[size-1] = '\0';
} //end if
else
{
strcpy(buf, "");
} //end else
return;
} //end of the function BotMatchVariable
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_stringlist_t *BotFindStringInList(bot_stringlist_t *list, char *string)
{
bot_stringlist_t *s;
for (s = list; s; s = s->next)
{
if (!strcmp(s->string, string)) return s;
} //end for
return NULL;
} //end of the function BotFindStringInList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_stringlist_t *BotCheckChatMessageIntegrety(char *message, bot_stringlist_t *stringlist)
{
int i;
char *msgptr;
char temp[MAX_MESSAGE_SIZE];
bot_stringlist_t *s;
msgptr = message;
//
while(*msgptr)
{
if (*msgptr == ESCAPE_CHAR)
{
msgptr++;
switch(*msgptr)
{
case 'v': //variable
{
//step over the 'v'
msgptr++;
while(*msgptr && *msgptr != ESCAPE_CHAR) msgptr++;
//step over the trailing escape char
if (*msgptr) msgptr++;
break;
} //end case
case 'r': //random
{
//step over the 'r'
msgptr++;
for (i = 0; (*msgptr && *msgptr != ESCAPE_CHAR); i++)
{
temp[i] = *msgptr++;
} //end while
temp[i] = '\0';
//step over the trailing escape char
if (*msgptr) msgptr++;
//find the random keyword
if (!RandomString(temp))
{
if (!BotFindStringInList(stringlist, temp))
{
Log_Write("%s = {\"%s\"} //MISSING RANDOM\r\n", temp, temp);
s = GetClearedMemory(sizeof(bot_stringlist_t) + strlen(temp) + 1);
s->string = (char *) s + sizeof(bot_stringlist_t);
strcpy(s->string, temp);
s->next = stringlist;
stringlist = s;
} //end if
} //end if
break;
} //end case
default:
{
botimport.Print(PRT_FATAL, "BotCheckChatMessageIntegrety: message \"%s\" invalid escape char\n", message);
break;
} //end default
} //end switch
} //end if
else
{
msgptr++;
} //end else
} //end while
return stringlist;
} //end of the function BotCheckChatMessageIntegrety
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotCheckInitialChatIntegrety(bot_chat_t *chat)
{
bot_chattype_t *t;
bot_chatmessage_t *cm;
bot_stringlist_t *stringlist, *s, *nexts;
stringlist = NULL;
for (t = chat->types; t; t = t->next)
{
for (cm = t->firstchatmessage; cm; cm = cm->next)
{
stringlist = BotCheckChatMessageIntegrety(cm->chatmessage, stringlist);
} //end for
} //end for
for (s = stringlist; s; s = nexts)
{
nexts = s->next;
FreeMemory(s);
} //end for
} //end of the function BotCheckInitialChatIntegrety
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotCheckReplyChatIntegrety(bot_replychat_t *replychat)
{
bot_replychat_t *rp;
bot_chatmessage_t *cm;
bot_stringlist_t *stringlist, *s, *nexts;
stringlist = NULL;
for (rp = replychat; rp; rp = rp->next)
{
for (cm = rp->firstchatmessage; cm; cm = cm->next)
{
stringlist = BotCheckChatMessageIntegrety(cm->chatmessage, stringlist);
} //end for
} //end for
for (s = stringlist; s; s = nexts)
{
nexts = s->next;
FreeMemory(s);
} //end for
} //end of the function BotCheckReplyChatIntegrety
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotDumpReplyChat(bot_replychat_t *replychat)
{
FILE *fp;
bot_replychat_t *rp;
bot_replychatkey_t *key;
bot_chatmessage_t *cm;
bot_matchpiece_t *mp;
fp = Log_FilePointer();
if (!fp) return;
fprintf(fp, "BotDumpReplyChat:\n");
for (rp = replychat; rp; rp = rp->next)
{
fprintf(fp, "[");
for (key = rp->keys; key; key = key->next)
{
if (key->flags & RCKFL_AND) fprintf(fp, "&");
else if (key->flags & RCKFL_NOT) fprintf(fp, "!");
//
if (key->flags & RCKFL_NAME) fprintf(fp, "name");
else if (key->flags & RCKFL_GENDERFEMALE) fprintf(fp, "female");
else if (key->flags & RCKFL_GENDERMALE) fprintf(fp, "male");
else if (key->flags & RCKFL_GENDERLESS) fprintf(fp, "it");
else if (key->flags & RCKFL_VARIABLES)
{
fprintf(fp, "(");
for (mp = key->match; mp; mp = mp->next)
{
if (mp->type == MT_STRING) fprintf(fp, "\"%s\"", mp->firststring->string);
else fprintf(fp, "%d", mp->variable);
if (mp->next) fprintf(fp, ", ");
} //end for
fprintf(fp, ")");
} //end if
else if (key->flags & RCKFL_STRING)
{
fprintf(fp, "\"%s\"", key->string);
} //end if
if (key->next) fprintf(fp, ", ");
else fprintf(fp, "] = %1.0f\n", rp->priority);
} //end for
fprintf(fp, "{\n");
for (cm = rp->firstchatmessage; cm; cm = cm->next)
{
fprintf(fp, "\t\"%s\";\n", cm->chatmessage);
} //end for
fprintf(fp, "}\n");
} //end for
} //end of the function BotDumpReplyChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotFreeReplyChat(bot_replychat_t *replychat)
{
bot_replychat_t *rp, *nextrp;
bot_replychatkey_t *key, *nextkey;
bot_chatmessage_t *cm, *nextcm;
for (rp = replychat; rp; rp = nextrp)
{
nextrp = rp->next;
for (key = rp->keys; key; key = nextkey)
{
nextkey = key->next;
if (key->match) BotFreeMatchPieces(key->match);
if (key->string) FreeMemory(key->string);
FreeMemory(key);
} //end for
for (cm = rp->firstchatmessage; cm; cm = nextcm)
{
nextcm = cm->next;
FreeMemory(cm);
} //end for
FreeMemory(rp);
} //end for
} //end of the function BotFreeReplyChat
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotCheckValidReplyChatKeySet(source_t *source, bot_replychatkey_t *keys)
{
int allprefixed, hasvariableskey, hasstringkey;
bot_matchpiece_t *m;
bot_matchstring_t *ms;
bot_replychatkey_t *key, *key2;
//
allprefixed = qtrue;
hasvariableskey = hasstringkey = qfalse;
for (key = keys; key; key = key->next)
{
if (!(key->flags & (RCKFL_AND|RCKFL_NOT)))
{
allprefixed = qfalse;
if (key->flags & RCKFL_VARIABLES)
{
for (m = key->match; m; m = m->next)
{
if (m->type == MT_VARIABLE) hasvariableskey = qtrue;
} //end for
} //end if
else if (key->flags & RCKFL_STRING)
{
hasstringkey = qtrue;
} //end else if
} //end if
else if ((key->flags & RCKFL_AND) && (key->flags & RCKFL_STRING))
{
for (key2 = keys; key2; key2 = key2->next)
{
if (key2 == key) continue;
if (key2->flags & RCKFL_NOT) continue;
if (key2->flags & RCKFL_VARIABLES)
{
for (m = key2->match; m; m = m->next)
{
if (m->type == MT_STRING)
{
for (ms = m->firststring; ms; ms = ms->next)
{
if (StringContains(ms->string, key->string, qfalse) != -1)
{
break;
} //end if
} //end for
if (ms) break;
} //end if
else if (m->type == MT_VARIABLE)
{
break;
} //end if
} //end for
if (!m)
{
SourceWarning(source, "one of the match templates does not "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -