📄 be_ai_chat.c
字号:
SourceError(source, "chat message too long\n");
return qfalse;
} //end if
sprintf(&ptr[strlen(ptr)], "%cv%ld%c", ESCAPE_CHAR, token.intvalue, ESCAPE_CHAR);
} //end if
//random string
else if (token.type == TT_NAME)
{
if (strlen(ptr) + 7 > MAX_MESSAGE_SIZE)
{
SourceError(source, "chat message too long\n");
return qfalse;
} //end if
sprintf(&ptr[strlen(ptr)], "%cr%s%c", ESCAPE_CHAR, token.string, ESCAPE_CHAR);
} //end else if
else
{
SourceError(source, "unknown message component %s\n", token.string);
return qfalse;
} //end else
if (PC_CheckTokenString(source, ";")) break;
if (!PC_ExpectTokenString(source, ",")) return qfalse;
} //end while
//
return qtrue;
} //end of the function BotLoadChatMessage
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotDumpRandomStringList(bot_randomlist_t *randomlist)
{
FILE *fp;
bot_randomlist_t *random;
bot_randomstring_t *rs;
fp = Log_FilePointer();
if (!fp) return;
for (random = randomlist; random; random = random->next)
{
fprintf(fp, "%s = {", random->string);
for (rs = random->firstrandomstring; rs; rs = rs->next)
{
fprintf(fp, "\"%s\"", rs->string);
if (rs->next) fprintf(fp, ", ");
else fprintf(fp, "}\n");
} //end for
} //end for
} //end of the function BotDumpRandomStringList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_randomlist_t *BotLoadRandomStrings(char *filename)
{
int pass, size;
char *ptr = NULL, chatmessagestring[MAX_MESSAGE_SIZE];
source_t *source;
token_t token;
bot_randomlist_t *randomlist, *lastrandom, *random;
bot_randomstring_t *randomstring;
#ifdef DEBUG
int starttime = Sys_MilliSeconds();
#endif //DEBUG
size = 0;
randomlist = NULL;
random = NULL;
//the synonyms are parsed in two phases
for (pass = 0; pass < 2; pass++)
{
//
if (pass && size) ptr = (char *) GetClearedHunkMemory(size);
//
PC_SetBaseFolder(BOTFILESBASEFOLDER);
source = LoadSourceFile(filename);
if (!source)
{
botimport.Print(PRT_ERROR, "counldn't load %s\n", filename);
return NULL;
} //end if
//
randomlist = NULL; //list
lastrandom = NULL; //last
//
while(PC_ReadToken(source, &token))
{
if (token.type != TT_NAME)
{
SourceError(source, "unknown random %s", token.string);
FreeSource(source);
return NULL;
} //end if
size += sizeof(bot_randomlist_t) + strlen(token.string) + 1;
if (pass)
{
random = (bot_randomlist_t *) ptr;
ptr += sizeof(bot_randomlist_t);
random->string = ptr;
ptr += strlen(token.string) + 1;
strcpy(random->string, token.string);
random->firstrandomstring = NULL;
random->numstrings = 0;
//
if (lastrandom) lastrandom->next = random;
else randomlist = random;
lastrandom = random;
} //end if
if (!PC_ExpectTokenString(source, "=") ||
!PC_ExpectTokenString(source, "{"))
{
FreeSource(source);
return NULL;
} //end if
while(!PC_CheckTokenString(source, "}"))
{
if (!BotLoadChatMessage(source, chatmessagestring))
{
FreeSource(source);
return NULL;
} //end if
size += sizeof(bot_randomstring_t) + strlen(chatmessagestring) + 1;
if (pass)
{
randomstring = (bot_randomstring_t *) ptr;
ptr += sizeof(bot_randomstring_t);
randomstring->string = ptr;
ptr += strlen(chatmessagestring) + 1;
strcpy(randomstring->string, chatmessagestring);
//
random->numstrings++;
randomstring->next = random->firstrandomstring;
random->firstrandomstring = randomstring;
} //end if
} //end while
} //end while
//free the source after one pass
FreeSource(source);
} //end for
botimport.Print(PRT_MESSAGE, "loaded %s\n", filename);
//
#ifdef DEBUG
botimport.Print(PRT_MESSAGE, "random strings %d msec\n", Sys_MilliSeconds() - starttime);
//BotDumpRandomStringList(randomlist);
#endif //DEBUG
//
return randomlist;
} //end of the function BotLoadRandomStrings
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
char *RandomString(char *name)
{
bot_randomlist_t *random;
bot_randomstring_t *rs;
int i;
for (random = randomstrings; random; random = random->next)
{
if (!strcmp(random->string, name))
{
i = random() * random->numstrings;
for (rs = random->firstrandomstring; rs; rs = rs->next)
{
if (--i < 0) break;
} //end for
if (rs)
{
return rs->string;
} //end if
} //end for
} //end for
return NULL;
} //end of the function RandomString
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotDumpMatchTemplates(bot_matchtemplate_t *matches)
{
FILE *fp;
bot_matchtemplate_t *mt;
bot_matchpiece_t *mp;
bot_matchstring_t *ms;
fp = Log_FilePointer();
if (!fp) return;
for (mt = matches; mt; mt = mt->next)
{
fprintf(fp, "{ " );
for (mp = mt->first; mp; mp = mp->next)
{
if (mp->type == MT_STRING)
{
for (ms = mp->firststring; ms; ms = ms->next)
{
fprintf(fp, "\"%s\"", ms->string);
if (ms->next) fprintf(fp, "|");
} //end for
} //end if
else if (mp->type == MT_VARIABLE)
{
fprintf(fp, "%d", mp->variable);
} //end else if
if (mp->next) fprintf(fp, ", ");
} //end for
fprintf(fp, " = (%d, %d);}\n", mt->type, mt->subtype);
} //end for
} //end of the function BotDumpMatchTemplates
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotFreeMatchPieces(bot_matchpiece_t *matchpieces)
{
bot_matchpiece_t *mp, *nextmp;
bot_matchstring_t *ms, *nextms;
for (mp = matchpieces; mp; mp = nextmp)
{
nextmp = mp->next;
if (mp->type == MT_STRING)
{
for (ms = mp->firststring; ms; ms = nextms)
{
nextms = ms->next;
FreeMemory(ms);
} //end for
} //end if
FreeMemory(mp);
} //end for
} //end of the function BotFreeMatchPieces
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_matchpiece_t *BotLoadMatchPieces(source_t *source, char *endtoken)
{
int lastwasvariable, emptystring;
token_t token;
bot_matchpiece_t *matchpiece, *firstpiece, *lastpiece;
bot_matchstring_t *matchstring, *lastmatchstring;
firstpiece = NULL;
lastpiece = NULL;
//
lastwasvariable = qfalse;
//
while(PC_ReadToken(source, &token))
{
if (token.type == TT_NUMBER && (token.subtype & TT_INTEGER))
{
if (token.intvalue < 0 || token.intvalue >= MAX_MATCHVARIABLES)
{
SourceError(source, "can't have more than %d match variables\n", MAX_MATCHVARIABLES);
FreeSource(source);
BotFreeMatchPieces(firstpiece);
return NULL;
} //end if
if (lastwasvariable)
{
SourceError(source, "not allowed to have adjacent variables\n");
FreeSource(source);
BotFreeMatchPieces(firstpiece);
return NULL;
} //end if
lastwasvariable = qtrue;
//
matchpiece = (bot_matchpiece_t *) GetClearedHunkMemory(sizeof(bot_matchpiece_t));
matchpiece->type = MT_VARIABLE;
matchpiece->variable = token.intvalue;
matchpiece->next = NULL;
if (lastpiece) lastpiece->next = matchpiece;
else firstpiece = matchpiece;
lastpiece = matchpiece;
} //end if
else if (token.type == TT_STRING)
{
//
matchpiece = (bot_matchpiece_t *) GetClearedHunkMemory(sizeof(bot_matchpiece_t));
matchpiece->firststring = NULL;
matchpiece->type = MT_STRING;
matchpiece->variable = 0;
matchpiece->next = NULL;
if (lastpiece) lastpiece->next = matchpiece;
else firstpiece = matchpiece;
lastpiece = matchpiece;
//
lastmatchstring = NULL;
emptystring = qfalse;
//
do
{
if (matchpiece->firststring)
{
if (!PC_ExpectTokenType(source, TT_STRING, 0, &token))
{
FreeSource(source);
BotFreeMatchPieces(firstpiece);
return NULL;
} //end if
} //end if
StripDoubleQuotes(token.string);
matchstring = (bot_matchstring_t *) GetClearedHunkMemory(sizeof(bot_matchstring_t) + strlen(token.string) + 1);
matchstring->string = (char *) matchstring + sizeof(bot_matchstring_t);
strcpy(matchstring->string, token.string);
if (!strlen(token.string)) emptystring = qtrue;
matchstring->next = NULL;
if (lastmatchstring) lastmatchstring->next = matchstring;
else matchpiece->firststring = matchstring;
lastmatchstring = matchstring;
} while(PC_CheckTokenString(source, "|"));
//if there was no empty string found
if (!emptystring) lastwasvariable = qfalse;
} //end if
else
{
SourceError(source, "invalid token %s\n", token.string);
FreeSource(source);
BotFreeMatchPieces(firstpiece);
return NULL;
} //end else
if (PC_CheckTokenString(source, endtoken)) break;
if (!PC_ExpectTokenString(source, ","))
{
FreeSource(source);
BotFreeMatchPieces(firstpiece);
return NULL;
} //end if
} //end while
return firstpiece;
} //end of the function BotLoadMatchPieces
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BotFreeMatchTemplates(bot_matchtemplate_t *mt)
{
bot_matchtemplate_t *nextmt;
for (; mt; mt = nextmt)
{
nextmt = mt->next;
BotFreeMatchPieces(mt->first);
FreeMemory(mt);
} //end for
} //end of the function BotFreeMatchTemplates
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bot_matchtemplate_t *BotLoadMatchTemplates(char *matchfile)
{
source_t *source;
token_t token;
bot_matchtemplate_t *matchtemplate, *matches, *lastmatch;
unsigned long int context;
PC_SetBaseFolder(BOTFILESBASEFOLDER);
source = LoadSourceFile(matchfile);
if (!source)
{
botimport.Print(PRT_ERROR, "counldn't load %s\n", matchfile);
return NULL;
} //end if
//
matches = NULL; //list with matches
lastmatch = NULL; //last match in the list
while(PC_ReadToken(source, &token))
{
if (token.type != TT_NUMBER || !(token.subtype & TT_INTEGER))
{
SourceError(source, "expected integer, found %s\n", token.string);
BotFreeMatchTemplates(matches);
FreeSource(source);
return NULL;
} //end if
//the context
context = token.intvalue;
//
if (!PC_ExpectTokenString(source, "{"))
{
BotFreeMatchTemplates(matches);
FreeSource(source);
return NULL;
} //end if
//
while(PC_ReadToken(source, &token))
{
if (!strcmp(token.string, "}")) break;
//
PC_UnreadLastToken(source);
//
matchtemplate = (bot_matchtemplate_t *) GetClearedHunkMemory(sizeof(bot_matchtemplate_t));
matchtemplate->context = context;
matchtemplate->next = NULL;
//add the match template to the list
if (lastmatch) lastmatch->next = matchtemplate;
else matches = matchtemplate;
lastmatch = matchtemplate;
//load the match template
matchtemplate->first = BotLoadMatchPieces(source, "=");
if (!matchtemplate->first)
{
BotFreeMatchTemplates(matches);
return NULL;
} //end if
//read the match type
if (!PC_ExpectTokenString(source, "(") ||
!PC_ExpectTokenType(source, TT_NUMBER, TT_INTEGER, &token))
{
BotFreeMatchTemplates(matches);
FreeSource(source);
return NULL;
} //end if
matchtemplate->type = token.intvalue;
//read the match subtype
if (!PC_ExpectTokenString(source, ",") ||
!PC_ExpectTokenType(source, TT_NUMBER, TT_INTEGER, &token))
{
BotFreeMatchTemplates(matches);
FreeSource(source);
return NULL;
} //end if
matchtemplate->subtype = token.intvalue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -