📄 ai_dmnet.c
字号:
trap_BotResetAvoidReach(bs->ms);
//if the bot wants to chat
if (BotChat_Death(bs)) {
bs->respawn_time = FloatTime() + BotChatTime(bs);
bs->respawnchat_time = FloatTime();
}
else {
bs->respawn_time = FloatTime() + 1 + random();
bs->respawnchat_time = 0;
}
//set respawn state
bs->respawn_wait = qfalse;
bs->ainode = AINode_Respawn;
}
/*
==================
AINode_Respawn
==================
*/
int AINode_Respawn(bot_state_t *bs) {
// if waiting for the actual respawn
if (bs->respawn_wait) {
if (!BotIsDead(bs)) {
AIEnter_Seek_LTG(bs, "respawn: respawned");
}
else {
trap_EA_Respawn(bs->client);
}
}
else if (bs->respawn_time < FloatTime()) {
// wait until respawned
bs->respawn_wait = qtrue;
// elementary action respawn
trap_EA_Respawn(bs->client);
//
if (bs->respawnchat_time) {
trap_BotEnterChat(bs->cs, 0, bs->chatto);
bs->enemy = -1;
}
}
if (bs->respawnchat_time && bs->respawnchat_time < FloatTime() - 0.5) {
trap_EA_Talk(bs->client);
}
//
return qtrue;
}
/*
==================
BotSelectActivateWeapon
==================
*/
int BotSelectActivateWeapon(bot_state_t *bs) {
//
if (bs->inventory[INVENTORY_MACHINEGUN] > 0 && bs->inventory[INVENTORY_BULLETS] > 0)
return WEAPONINDEX_MACHINEGUN;
else if (bs->inventory[INVENTORY_SHOTGUN] > 0 && bs->inventory[INVENTORY_SHELLS] > 0)
return WEAPONINDEX_SHOTGUN;
else if (bs->inventory[INVENTORY_PLASMAGUN] > 0 && bs->inventory[INVENTORY_CELLS] > 0)
return WEAPONINDEX_PLASMAGUN;
else if (bs->inventory[INVENTORY_LIGHTNING] > 0 && bs->inventory[INVENTORY_LIGHTNINGAMMO] > 0)
return WEAPONINDEX_LIGHTNING;
#ifdef MISSIONPACK
else if (bs->inventory[INVENTORY_CHAINGUN] > 0 && bs->inventory[INVENTORY_BELT] > 0)
return WEAPONINDEX_CHAINGUN;
else if (bs->inventory[INVENTORY_NAILGUN] > 0 && bs->inventory[INVENTORY_NAILS] > 0)
return WEAPONINDEX_NAILGUN;
#endif
else if (bs->inventory[INVENTORY_RAILGUN] > 0 && bs->inventory[INVENTORY_SLUGS] > 0)
return WEAPONINDEX_RAILGUN;
else if (bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 && bs->inventory[INVENTORY_ROCKETS] > 0)
return WEAPONINDEX_ROCKET_LAUNCHER;
else if (bs->inventory[INVENTORY_BFG10K] > 0 && bs->inventory[INVENTORY_BFGAMMO] > 0)
return WEAPONINDEX_BFG;
else {
return -1;
}
}
/*
==================
BotClearPath
try to deactivate obstacles like proximity mines on the bot's path
==================
*/
void BotClearPath(bot_state_t *bs, bot_moveresult_t *moveresult) {
int i, bestmine;
float dist, bestdist;
vec3_t target, dir;
bsp_trace_t bsptrace;
entityState_t state;
// if there is a dead body wearing kamikze nearby
if (bs->kamikazebody) {
// if the bot's view angles and weapon are not used for movement
if ( !(moveresult->flags & (MOVERESULT_MOVEMENTVIEW | MOVERESULT_MOVEMENTWEAPON)) ) {
//
BotAI_GetEntityState(bs->kamikazebody, &state);
VectorCopy(state.pos.trBase, target);
target[2] += 8;
VectorSubtract(target, bs->eye, dir);
vectoangles(dir, moveresult->ideal_viewangles);
//
moveresult->weapon = BotSelectActivateWeapon(bs);
if (moveresult->weapon == -1) {
// FIXME: run away!
moveresult->weapon = 0;
}
if (moveresult->weapon) {
//
moveresult->flags |= MOVERESULT_MOVEMENTWEAPON | MOVERESULT_MOVEMENTVIEW;
// if holding the right weapon
if (bs->cur_ps.weapon == moveresult->weapon) {
// if the bot is pretty close with it's aim
if (InFieldOfVision(bs->viewangles, 20, moveresult->ideal_viewangles)) {
//
BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, target, bs->entitynum, MASK_SHOT);
// if the mine is visible from the current position
if (bsptrace.fraction >= 1.0 || bsptrace.ent == state.number) {
// shoot at the mine
trap_EA_Attack(bs->client);
}
}
}
}
}
}
if (moveresult->flags & MOVERESULT_BLOCKEDBYAVOIDSPOT) {
bs->blockedbyavoidspot_time = FloatTime() + 5;
}
// if blocked by an avoid spot and the view angles and weapon are used for movement
if (bs->blockedbyavoidspot_time > FloatTime() &&
!(moveresult->flags & (MOVERESULT_MOVEMENTVIEW | MOVERESULT_MOVEMENTWEAPON)) ) {
bestdist = 300;
bestmine = -1;
for (i = 0; i < bs->numproxmines; i++) {
BotAI_GetEntityState(bs->proxmines[i], &state);
VectorSubtract(state.pos.trBase, bs->origin, dir);
dist = VectorLength(dir);
if (dist < bestdist) {
bestdist = dist;
bestmine = i;
}
}
if (bestmine != -1) {
//
// state->generic1 == TEAM_RED || state->generic1 == TEAM_BLUE
//
// deactivate prox mines in the bot's path by shooting
// rockets or plasma cells etc. at them
BotAI_GetEntityState(bs->proxmines[bestmine], &state);
VectorCopy(state.pos.trBase, target);
target[2] += 2;
VectorSubtract(target, bs->eye, dir);
vectoangles(dir, moveresult->ideal_viewangles);
// if the bot has a weapon that does splash damage
if (bs->inventory[INVENTORY_PLASMAGUN] > 0 && bs->inventory[INVENTORY_CELLS] > 0)
moveresult->weapon = WEAPONINDEX_PLASMAGUN;
else if (bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 && bs->inventory[INVENTORY_ROCKETS] > 0)
moveresult->weapon = WEAPONINDEX_ROCKET_LAUNCHER;
else if (bs->inventory[INVENTORY_BFG10K] > 0 && bs->inventory[INVENTORY_BFGAMMO] > 0)
moveresult->weapon = WEAPONINDEX_BFG;
else {
moveresult->weapon = 0;
}
if (moveresult->weapon) {
//
moveresult->flags |= MOVERESULT_MOVEMENTWEAPON | MOVERESULT_MOVEMENTVIEW;
// if holding the right weapon
if (bs->cur_ps.weapon == moveresult->weapon) {
// if the bot is pretty close with it's aim
if (InFieldOfVision(bs->viewangles, 20, moveresult->ideal_viewangles)) {
//
BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, target, bs->entitynum, MASK_SHOT);
// if the mine is visible from the current position
if (bsptrace.fraction >= 1.0 || bsptrace.ent == state.number) {
// shoot at the mine
trap_EA_Attack(bs->client);
}
}
}
}
}
}
}
/*
==================
AIEnter_Seek_ActivateEntity
==================
*/
void AIEnter_Seek_ActivateEntity(bot_state_t *bs, char *s) {
BotRecordNodeSwitch(bs, "activate entity", "", s);
bs->ainode = AINode_Seek_ActivateEntity;
}
/*
==================
AINode_Seek_Activate_Entity
==================
*/
int AINode_Seek_ActivateEntity(bot_state_t *bs) {
bot_goal_t *goal;
vec3_t target, dir, ideal_viewangles;
bot_moveresult_t moveresult;
int targetvisible;
bsp_trace_t bsptrace;
aas_entityinfo_t entinfo;
if (BotIsObserver(bs)) {
BotClearActivateGoalStack(bs);
AIEnter_Observer(bs, "active entity: observer");
return qfalse;
}
//if in the intermission
if (BotIntermission(bs)) {
BotClearActivateGoalStack(bs);
AIEnter_Intermission(bs, "activate entity: intermission");
return qfalse;
}
//respawn if dead
if (BotIsDead(bs)) {
BotClearActivateGoalStack(bs);
AIEnter_Respawn(bs, "activate entity: bot dead");
return qfalse;
}
//
bs->tfl = TFL_DEFAULT;
if (bot_grapple.integer) bs->tfl |= TFL_GRAPPLEHOOK;
// if in lava or slime the bot should be able to get out
if (BotInLavaOrSlime(bs)) bs->tfl |= TFL_LAVA|TFL_SLIME;
// map specific code
BotMapScripts(bs);
// no enemy
bs->enemy = -1;
// if the bot has no activate goal
if (!bs->activatestack) {
BotClearActivateGoalStack(bs);
AIEnter_Seek_NBG(bs, "activate entity: no goal");
return qfalse;
}
//
goal = &bs->activatestack->goal;
// initialize target being visible to false
targetvisible = qfalse;
// if the bot has to shoot at a target to activate something
if (bs->activatestack->shoot) {
//
BotAI_Trace(&bsptrace, bs->eye, NULL, NULL, bs->activatestack->target, bs->entitynum, MASK_SHOT);
// if the shootable entity is visible from the current position
if (bsptrace.fraction >= 1.0 || bsptrace.ent == goal->entitynum) {
targetvisible = qtrue;
// if holding the right weapon
if (bs->cur_ps.weapon == bs->activatestack->weapon) {
VectorSubtract(bs->activatestack->target, bs->eye, dir);
vectoangles(dir, ideal_viewangles);
// if the bot is pretty close with it's aim
if (InFieldOfVision(bs->viewangles, 20, ideal_viewangles)) {
trap_EA_Attack(bs->client);
}
}
}
}
// if the shoot target is visible
if (targetvisible) {
// get the entity info of the entity the bot is shooting at
BotEntityInfo(goal->entitynum, &entinfo);
// if the entity the bot shoots at moved
if (!VectorCompare(bs->activatestack->origin, entinfo.origin)) {
#ifdef DEBUG
BotAI_Print(PRT_MESSAGE, "hit shootable button or trigger\n");
#endif //DEBUG
bs->activatestack->time = 0;
}
// if the activate goal has been activated or the bot takes too long
if (bs->activatestack->time < FloatTime()) {
BotPopFromActivateGoalStack(bs);
// if there are more activate goals on the stack
if (bs->activatestack) {
bs->activatestack->time = FloatTime() + 10;
return qfalse;
}
AIEnter_Seek_NBG(bs, "activate entity: time out");
return qfalse;
}
memset(&moveresult, 0, sizeof(bot_moveresult_t));
}
else {
// if the bot has no goal
if (!goal) {
bs->activatestack->time = 0;
}
// if the bot does not have a shoot goal
else if (!bs->activatestack->shoot) {
//if the bot touches the current goal
if (trap_BotTouchingGoal(bs->origin, goal)) {
#ifdef DEBUG
BotAI_Print(PRT_MESSAGE, "touched button or trigger\n");
#endif //DEBUG
bs->activatestack->time = 0;
}
}
// if the activate goal has been activated or the bot takes too long
if (bs->activatestack->time < FloatTime()) {
BotPopFromActivateGoalStack(bs);
// if there are more activate goals on the stack
if (bs->activatestack) {
bs->activatestack->time = FloatTime() + 10;
return qfalse;
}
AIEnter_Seek_NBG(bs, "activate entity: activated");
return qfalse;
}
//predict obstacles
if (BotAIPredictObstacles(bs, goal))
return qfalse;
//initialize the movement state
BotSetupForMovement(bs);
//move towards the goal
trap_BotMoveToGoal(&moveresult, bs->ms, goal, bs->tfl);
//if the movement failed
if (moveresult.failure) {
//reset the avoid reach, otherwise bot is stuck in current area
trap_BotResetAvoidReach(bs->ms);
//
bs->activatestack->time = 0;
}
//check if the bot is blocked
BotAIBlocked(bs, &moveresult, qtrue);
}
//
BotClearPath(bs, &moveresult);
// if the bot has to shoot to activate
if (bs->activatestack->shoot) {
// if the view angles aren't yet used for the movement
if (!(moveresult.flags & MOVERESULT_MOVEMENTVIEW)) {
VectorSubtract(bs->activatestack->target, bs->eye, dir);
vectoangles(dir, moveresult.ideal_viewangles);
moveresult.flags |= MOVERESULT_MOVEMENTVIEW;
}
// if there's no weapon yet used for the movement
if (!(moveresult.flags & MOVERESULT_MOVEMENTWEAPON)) {
moveresult.flags |= MOVERESULT_MOVEMENTWEAPON;
//
bs->activatestack->weapon = BotSelectActivateWeapon(bs);
if (bs->activatestack->weapon == -1) {
//FIXME: find a decent weapon first
bs->activatestack->weapon = 0;
}
moveresult.weapon = bs->activatestack->weapon;
}
}
// if the ideal view angles are set for movement
if (moveresult.flags & (MOVERESULT_MOVEMENTVIEWSET|MOVERESULT_MOVEMENTVIEW|MOVERESULT_SWIMVIEW)) {
VectorCopy(moveresult.ideal_viewangles, bs->ideal_viewangles);
}
// if waiting for something
else if (moveresult.flags & MOVERESULT_WAITING) {
if (random() < bs->thinktime * 0.8) {
BotRoamGoal(bs, target);
VectorSubtract(target, bs->origin, dir);
vectoangles(dir, bs->ideal_viewangles);
bs->ideal_viewangles[2] *= 0.5;
}
}
else if (!(bs->flags & BFL_IDEALVIEWSET)) {
if (trap_BotMovementViewTarget(bs->ms, goal, bs->tfl, 300, target)) {
VectorSubtract(target, bs->origin, dir);
vectoangles(dir, bs->ideal_viewangles);
}
else {
vectoangles(moveresult.movedir, bs->ideal_viewangles);
}
bs->ideal_viewangles[2] *= 0.5;
}
// if the weapon is used for the bot movement
if (moveresult.flags & MOVERESULT_MOVEMENTWEAPON)
bs->weaponnum = moveresult.weapon;
// if there is an enemy
if (BotFindEnemy(bs, -1)) {
if (BotWantsToRetreat(bs)) {
//keep the current long term goal and retreat
AIEnter_Battle_NBG(bs, "activate entity: found enemy");
}
else {
trap_BotResetLastAvoidReach(bs->ms);
//empty the goal stack
trap_BotEmptyGoalStack(bs->gs);
//go fight
AIEnter_Battle_Fight(bs, "activate entity: found enemy");
}
BotClearActivateGoalStack(bs);
}
return qtrue;
}
/*
==================
AIEnter_Seek_NBG
==================
*/
void AIEnter_Seek_NBG(bot_state_t *bs, char *s) {
bot_goal_t goal;
char buf[144];
if (trap_BotGetTopGoal(bs->gs, &goal)) {
trap_BotGoalName(goal.number, buf, 144);
BotRecordNodeSwitch(bs, "seek NBG", buf, s);
}
else {
BotRecordNodeSwitch(bs, "seek NBG", "no goal", s);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -