📄 ai_main.c
字号:
//set the view angles
//NOTE: the ucmd->angles are the angles WITHOUT the delta angles
ucmd->angles[PITCH] = ANGLE2SHORT(bi->viewangles[PITCH]);
ucmd->angles[YAW] = ANGLE2SHORT(bi->viewangles[YAW]);
ucmd->angles[ROLL] = ANGLE2SHORT(bi->viewangles[ROLL]);
//subtract the delta angles
for (j = 0; j < 3; j++) {
temp = ucmd->angles[j] - delta_angles[j];
/*NOTE: disabled because temp should be mod first
if ( j == PITCH ) {
// don't let the player look up or down more than 90 degrees
if ( temp > 16000 ) temp = 16000;
else if ( temp < -16000 ) temp = -16000;
}
*/
ucmd->angles[j] = temp;
}
//NOTE: movement is relative to the REAL view angles
//get the horizontal forward and right vector
//get the pitch in the range [-180, 180]
if (bi->dir[2]) angles[PITCH] = bi->viewangles[PITCH];
else angles[PITCH] = 0;
angles[YAW] = bi->viewangles[YAW];
angles[ROLL] = 0;
AngleVectors(angles, forward, right, NULL);
//bot input speed is in the range [0, 400]
bi->speed = bi->speed * 127 / 400;
//set the view independent movement
ucmd->forwardmove = DotProduct(forward, bi->dir) * bi->speed;
ucmd->rightmove = DotProduct(right, bi->dir) * bi->speed;
ucmd->upmove = abs(forward[2]) * bi->dir[2] * bi->speed;
//normal keyboard movement
if (bi->actionflags & ACTION_MOVEFORWARD) ucmd->forwardmove += 127;
if (bi->actionflags & ACTION_MOVEBACK) ucmd->forwardmove -= 127;
if (bi->actionflags & ACTION_MOVELEFT) ucmd->rightmove -= 127;
if (bi->actionflags & ACTION_MOVERIGHT) ucmd->rightmove += 127;
//jump/moveup
if (bi->actionflags & ACTION_JUMP) ucmd->upmove += 127;
//crouch/movedown
if (bi->actionflags & ACTION_CROUCH) ucmd->upmove -= 127;
//
//Com_Printf("forward = %d right = %d up = %d\n", ucmd.forwardmove, ucmd.rightmove, ucmd.upmove);
//Com_Printf("ucmd->serverTime = %d\n", ucmd->serverTime);
}
/*
==============
BotUpdateInput
==============
*/
void BotUpdateInput(bot_state_t *bs, int time, int elapsed_time) {
bot_input_t bi;
int j;
//add the delta angles to the bot's current view angles
for (j = 0; j < 3; j++) {
bs->viewangles[j] = AngleMod(bs->viewangles[j] + SHORT2ANGLE(bs->cur_ps.delta_angles[j]));
}
//change the bot view angles
BotChangeViewAngles(bs, (float) elapsed_time / 1000);
//retrieve the bot input
trap_EA_GetInput(bs->client, (float) time / 1000, &bi);
//respawn hack
if (bi.actionflags & ACTION_RESPAWN) {
if (bs->lastucmd.buttons & BUTTON_ATTACK) bi.actionflags &= ~(ACTION_RESPAWN|ACTION_ATTACK);
}
//convert the bot input to a usercmd
BotInputToUserCommand(&bi, &bs->lastucmd, bs->cur_ps.delta_angles, time);
//subtract the delta angles
for (j = 0; j < 3; j++) {
bs->viewangles[j] = AngleMod(bs->viewangles[j] - SHORT2ANGLE(bs->cur_ps.delta_angles[j]));
}
}
/*
==============
BotAIRegularUpdate
==============
*/
void BotAIRegularUpdate(void) {
if (regularupdate_time < FloatTime()) {
trap_BotUpdateEntityItems();
regularupdate_time = FloatTime() + 0.3;
}
}
/*
==============
RemoveColorEscapeSequences
==============
*/
void RemoveColorEscapeSequences( char *text ) {
int i, l;
l = 0;
for ( i = 0; text[i]; i++ ) {
if (Q_IsColorString(&text[i])) {
i++;
continue;
}
if (text[i] > 0x7E)
continue;
text[l++] = text[i];
}
text[l] = '\0';
}
/*
==============
BotAI
==============
*/
int BotAI(int client, float thinktime) {
bot_state_t *bs;
char buf[1024], *args;
int j;
trap_EA_ResetInput(client);
//
bs = botstates[client];
if (!bs || !bs->inuse) {
BotAI_Print(PRT_FATAL, "BotAI: client %d is not setup\n", client);
return qfalse;
}
//retrieve the current client state
BotAI_GetClientState( client, &bs->cur_ps );
//retrieve any waiting server commands
while( trap_BotGetServerCommand(client, buf, sizeof(buf)) ) {
//have buf point to the command and args to the command arguments
args = strchr( buf, ' ');
if (!args) continue;
*args++ = '\0';
//remove color espace sequences from the arguments
RemoveColorEscapeSequences( args );
if (!Q_stricmp(buf, "cp "))
{ /*CenterPrintf*/ }
else if (!Q_stricmp(buf, "cs"))
{ /*ConfigStringModified*/ }
else if (!Q_stricmp(buf, "print")) {
//remove first and last quote from the chat message
memmove(args, args+1, strlen(args));
args[strlen(args)-1] = '\0';
trap_BotQueueConsoleMessage(bs->cs, CMS_NORMAL, args);
}
else if (!Q_stricmp(buf, "chat")) {
//remove first and last quote from the chat message
memmove(args, args+1, strlen(args));
args[strlen(args)-1] = '\0';
trap_BotQueueConsoleMessage(bs->cs, CMS_CHAT, args);
}
else if (!Q_stricmp(buf, "tchat")) {
//remove first and last quote from the chat message
memmove(args, args+1, strlen(args));
args[strlen(args)-1] = '\0';
trap_BotQueueConsoleMessage(bs->cs, CMS_CHAT, args);
}
#ifdef MISSIONPACK
else if (!Q_stricmp(buf, "vchat")) {
BotVoiceChatCommand(bs, SAY_ALL, args);
}
else if (!Q_stricmp(buf, "vtchat")) {
BotVoiceChatCommand(bs, SAY_TEAM, args);
}
else if (!Q_stricmp(buf, "vtell")) {
BotVoiceChatCommand(bs, SAY_TELL, args);
}
#endif
else if (!Q_stricmp(buf, "scores"))
{ /*FIXME: parse scores?*/ }
else if (!Q_stricmp(buf, "clientLevelShot"))
{ /*ignore*/ }
}
//add the delta angles to the bot's current view angles
for (j = 0; j < 3; j++) {
bs->viewangles[j] = AngleMod(bs->viewangles[j] + SHORT2ANGLE(bs->cur_ps.delta_angles[j]));
}
//increase the local time of the bot
bs->ltime += thinktime;
//
bs->thinktime = thinktime;
//origin of the bot
VectorCopy(bs->cur_ps.origin, bs->origin);
//eye coordinates of the bot
VectorCopy(bs->cur_ps.origin, bs->eye);
bs->eye[2] += bs->cur_ps.viewheight;
//get the area the bot is in
bs->areanum = BotPointAreaNum(bs->origin);
//the real AI
BotDeathmatchAI(bs, thinktime);
//set the weapon selection every AI frame
trap_EA_SelectWeapon(bs->client, bs->weaponnum);
//subtract the delta angles
for (j = 0; j < 3; j++) {
bs->viewangles[j] = AngleMod(bs->viewangles[j] - SHORT2ANGLE(bs->cur_ps.delta_angles[j]));
}
//everything was ok
return qtrue;
}
/*
==================
BotScheduleBotThink
==================
*/
void BotScheduleBotThink(void) {
int i, botnum;
botnum = 0;
for( i = 0; i < MAX_CLIENTS; i++ ) {
if( !botstates[i] || !botstates[i]->inuse ) {
continue;
}
//initialize the bot think residual time
botstates[i]->botthink_residual = bot_thinktime.integer * botnum / numbots;
botnum++;
}
}
/*
==============
BotWriteSessionData
==============
*/
void BotWriteSessionData(bot_state_t *bs) {
const char *s;
const char *var;
s = va(
"%i %i %i %i %i %i %i %i"
" %f %f %f"
" %f %f %f"
" %f %f %f",
bs->lastgoal_decisionmaker,
bs->lastgoal_ltgtype,
bs->lastgoal_teammate,
bs->lastgoal_teamgoal.areanum,
bs->lastgoal_teamgoal.entitynum,
bs->lastgoal_teamgoal.flags,
bs->lastgoal_teamgoal.iteminfo,
bs->lastgoal_teamgoal.number,
bs->lastgoal_teamgoal.origin[0],
bs->lastgoal_teamgoal.origin[1],
bs->lastgoal_teamgoal.origin[2],
bs->lastgoal_teamgoal.mins[0],
bs->lastgoal_teamgoal.mins[1],
bs->lastgoal_teamgoal.mins[2],
bs->lastgoal_teamgoal.maxs[0],
bs->lastgoal_teamgoal.maxs[1],
bs->lastgoal_teamgoal.maxs[2]
);
var = va( "botsession%i", bs->client );
trap_Cvar_Set( var, s );
}
/*
==============
BotReadSessionData
==============
*/
void BotReadSessionData(bot_state_t *bs) {
char s[MAX_STRING_CHARS];
const char *var;
var = va( "botsession%i", bs->client );
trap_Cvar_VariableStringBuffer( var, s, sizeof(s) );
sscanf(s,
"%i %i %i %i %i %i %i %i"
" %f %f %f"
" %f %f %f"
" %f %f %f",
&bs->lastgoal_decisionmaker,
&bs->lastgoal_ltgtype,
&bs->lastgoal_teammate,
&bs->lastgoal_teamgoal.areanum,
&bs->lastgoal_teamgoal.entitynum,
&bs->lastgoal_teamgoal.flags,
&bs->lastgoal_teamgoal.iteminfo,
&bs->lastgoal_teamgoal.number,
&bs->lastgoal_teamgoal.origin[0],
&bs->lastgoal_teamgoal.origin[1],
&bs->lastgoal_teamgoal.origin[2],
&bs->lastgoal_teamgoal.mins[0],
&bs->lastgoal_teamgoal.mins[1],
&bs->lastgoal_teamgoal.mins[2],
&bs->lastgoal_teamgoal.maxs[0],
&bs->lastgoal_teamgoal.maxs[1],
&bs->lastgoal_teamgoal.maxs[2]
);
}
/*
==============
BotAISetupClient
==============
*/
int BotAISetupClient(int client, struct bot_settings_s *settings, qboolean restart) {
char filename[MAX_PATH], name[MAX_PATH], gender[MAX_PATH];
bot_state_t *bs;
int errnum;
if (!botstates[client]) botstates[client] = G_Alloc(sizeof(bot_state_t));
bs = botstates[client];
if (bs && bs->inuse) {
BotAI_Print(PRT_FATAL, "BotAISetupClient: client %d already setup\n", client);
return qfalse;
}
if (!trap_AAS_Initialized()) {
BotAI_Print(PRT_FATAL, "AAS not initialized\n");
return qfalse;
}
//load the bot character
bs->character = trap_BotLoadCharacter(settings->characterfile, settings->skill);
if (!bs->character) {
BotAI_Print(PRT_FATAL, "couldn't load skill %f from %s\n", settings->skill, settings->characterfile);
return qfalse;
}
//copy the settings
memcpy(&bs->settings, settings, sizeof(bot_settings_t));
//allocate a goal state
bs->gs = trap_BotAllocGoalState(client);
//load the item weights
trap_Characteristic_String(bs->character, CHARACTERISTIC_ITEMWEIGHTS, filename, MAX_PATH);
errnum = trap_BotLoadItemWeights(bs->gs, filename);
if (errnum != BLERR_NOERROR) {
trap_BotFreeGoalState(bs->gs);
return qfalse;
}
//allocate a weapon state
bs->ws = trap_BotAllocWeaponState();
//load the weapon weights
trap_Characteristic_String(bs->character, CHARACTERISTIC_WEAPONWEIGHTS, filename, MAX_PATH);
errnum = trap_BotLoadWeaponWeights(bs->ws, filename);
if (errnum != BLERR_NOERROR) {
trap_BotFreeGoalState(bs->gs);
trap_BotFreeWeaponState(bs->ws);
return qfalse;
}
//allocate a chat state
bs->cs = trap_BotAllocChatState();
//load the chat file
trap_Characteristic_String(bs->character, CHARACTERISTIC_CHAT_FILE, filename, MAX_PATH);
trap_Characteristic_String(bs->character, CHARACTERISTIC_CHAT_NAME, name, MAX_PATH);
errnum = trap_BotLoadChatFile(bs->cs, filename, name);
if (errnum != BLERR_NOERROR) {
trap_BotFreeChatState(bs->cs);
trap_BotFreeGoalState(bs->gs);
trap_BotFreeWeaponState(bs->ws);
return qfalse;
}
//get the gender characteristic
trap_Characteristic_String(bs->character, CHARACTERISTIC_GENDER, gender, MAX_PATH);
//set the chat gender
if (*gender == 'f' || *gender == 'F') trap_BotSetChatGender(bs->cs, CHAT_GENDERFEMALE);
else if (*gender == 'm' || *gender == 'M') trap_BotSetChatGender(bs->cs, CHAT_GENDERMALE);
else trap_BotSetChatGender(bs->cs, CHAT_GENDERLESS);
bs->inuse = qtrue;
bs->client = client;
bs->entitynum = client;
bs->setupcount = 4;
bs->entergame_time = FloatTime();
bs->ms = trap_BotAllocMoveState();
bs->walker = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_WALKER, 0, 1);
numbots++;
if (trap_Cvar_VariableIntegerValue("bot_testichat")) {
trap_BotLibVarSet("bot_testichat", "1");
BotChatTest(bs);
}
//NOTE: reschedule the bot thinking
BotScheduleBotThink();
//if interbreeding start with a mutation
if (bot_interbreed) {
trap_BotMutateGoalFuzzyLogic(bs->gs, 1);
}
// if we kept the bot client
if (restart) {
BotReadSessionData(bs);
}
//bot has been setup succesfully
return qtrue;
}
/*
==============
BotAIShutdownClient
==============
*/
int BotAIShutdownClient(int client, qboolean restart) {
bot_state_t *bs;
bs = botstates[client];
if (!bs || !bs->inuse) {
//BotAI_Print(PRT_ERROR, "BotAIShutdownClient: client %d already shutdown\n", client);
return qfalse;
}
if (restart) {
BotWriteSessionData(bs);
}
if (BotChat_ExitGame(bs)) {
trap_BotEnterChat(bs->cs, bs->client, CHAT_ALL);
}
trap_BotFreeMoveState(bs->ms);
//free the goal state`
trap_BotFreeGoalState(bs->gs);
//free the chat file
trap_BotFreeChatState(bs->cs);
//free the weapon weights
trap_BotFreeWeaponState(bs->ws);
//free the bot character
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -