fixing bots

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@6023 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
flowtron
2011-01-18 06:00:21 +00:00
parent 63e847ac75
commit b967a54ed7
6 changed files with 532 additions and 552 deletions
+73 -92
View File
@@ -1,7 +1,7 @@
//
// C++ Implementation: bot
//
// Description:
// Description:
//
// Contains code of the base bot class
//
@@ -38,14 +38,14 @@ CBot::~CBot()
void CBot::Spawn()
{
// Init all bot variabeles
spawnplayer(m_pMyEnt);
// Init all bot variabeles
spawnplayer(m_pMyEnt);
m_pMyEnt->targetyaw = m_pMyEnt->targetpitch = 0.0f;
m_pMyEnt->enemy = NULL;
m_pMyEnt->maxspeed = 22.0f;
m_pMyEnt->pBot = this;
m_pMyEnt->pBot = this;
m_eCurrentBotState = STATE_NORMAL;
m_iShootDelay = m_iChangeWeaponDelay = 0;
m_iCheckEnvDelay = 0;
@@ -55,7 +55,7 @@ void CBot::Spawn()
m_iStuckTime = 0;
m_iStrafeTime = m_iStrafeCheckDelay = 0;
m_iMoveDir = DIR_NONE;
m_pPrevEnemy = NULL;
m_iCombatNavTime = 0;
m_iSPMoveTime = 0;
@@ -72,7 +72,7 @@ void CBot::Spawn()
m_iLookAroundDelay = m_iLookAroundTime = m_iLookAroundUpdateTime = 0;
m_fLookAroundYaw = 0.0f;
m_bLookLeft = false;
m_iLastJumpPad = 0;
m_pTargetEnt = NULL;
while(!m_UnreachableEnts.Empty()) delete m_UnreachableEnts.Pop();
@@ -80,75 +80,56 @@ void CBot::Spawn()
m_iCheckEntsDelay = 0;
m_iCheckTriggersDelay = 0;
m_iLookForWaypointTime = 0;
m_iAimDelay = 0;
m_fYawToTurn = m_fPitchToTurn = 0.0f;
m_vGoal = m_vWaterGoal = g_vecZero;
ResetWaypointVars();
}
void CBot::Think()
{
if (intermission)
return;
// Bot is dead?
if (m_pMyEnt->state == CS_DEAD)
{
if(lastmillis-m_pMyEnt->respawnoffset<1200)
{
m_pMyEnt->move = 0;
moveplayer(m_pMyEnt, 1, true);
}
else if (!m_arena && lastmillis-m_pMyEnt->respawnoffset>5000)
Spawn();
SendBotInfo();
return;
}
CheckItemPickup();
TLinkedList<unreachable_ent_s*>::node_s *p = m_UnreachableEnts.GetFirst(), *tmp;
while(p)
{
if ((lastmillis - p->Entry->time) > 3500)
{
tmp = p;
p = p->next;
delete tmp->Entry;
m_UnreachableEnts.DeleteNode(tmp);
continue;
}
p = p->next;
}
if (!BotManager.IdleBots())
{
MainAI();
}
else
{
ResetMoveSpeed();
}
// Aim to ideal yaw and pitch
AimToIdeal();
// Store current location, to see if the bot is stuck
m_vPrevOrigin = m_pMyEnt->o;
// Don't check for stuck if the bot doesn't want to move
if (!m_pMyEnt->move && !m_pMyEnt->strafe)
m_iStuckCheckDelay = max(m_iStuckCheckDelay, lastmillis+100.0f);
// Move the bot
moveplayer(m_pMyEnt, 1, true);
// Update bot info on all clients
SendBotInfo();
if (intermission) return;
// Bot is dead?
if (m_pMyEnt->state == CS_DEAD)
{
if(lastmillis-m_pMyEnt->respawnoffset<1200)
{
m_pMyEnt->move = 0;
moveplayer(m_pMyEnt, 1, true);
}
else if (!m_arena && lastmillis-m_pMyEnt->respawnoffset>5000) Spawn();
SendBotInfo();
return;
}
CheckItemPickup();
TLinkedList<unreachable_ent_s*>::node_s *p = m_UnreachableEnts.GetFirst(), *tmp;
while(p)
{
if ((lastmillis - p->Entry->time) > 3500)
{
tmp = p;
p = p->next;
delete tmp->Entry;
m_UnreachableEnts.DeleteNode(tmp);
continue;
}
p = p->next;
}
if (!BotManager.IdleBots()) { MainAI(); }
else { ResetMoveSpeed(); }
// Aim to ideal yaw and pitch
AimToIdeal();
// Store current location, to see if the bot is stuck
m_vPrevOrigin = m_pMyEnt->o;
// Don't check for stuck if the bot doesn't want to move
if (!m_pMyEnt->move && !m_pMyEnt->strafe) m_iStuckCheckDelay = max(m_iStuckCheckDelay, lastmillis+100.0f);
// Move the bot
moveplayer(m_pMyEnt, 1, true);
// Update bot info on all clients
SendBotInfo();
}
void CBot::AimToVec(const vec &o)
@@ -177,7 +158,7 @@ void CBot::AimToIdeal()
float YDiff = fabs(m_pMyEnt->targetyaw - m_pMyEnt->yaw);
// How higher the diff, how higher the offsets and aim speed
if (XOffset)
{
if (RandomLong(0, 1))
@@ -198,7 +179,7 @@ void CBot::AimToIdeal()
else
RealYOffset = 0.0f;
if (XDiff >= 1.0f)
AimXSpeed = (AimXSpeed * (XDiff / 80.0f)) + (AimXSpeed * 0.25f);
else
@@ -211,16 +192,16 @@ void CBot::AimToIdeal()
m_fPitchToTurn = fabs((m_pMyEnt->targetpitch + RealXOffset) - m_pMyEnt->pitch);
m_fYawToTurn = fabs((m_pMyEnt->targetyaw + RealYOffset) - m_pMyEnt->yaw);
float flIdealPitch = ChangeAngle(AimXSpeed, m_pMyEnt->targetpitch + RealXOffset, m_pMyEnt->pitch);
float flIdealYaw = ChangeAngle(AimYSpeed, m_pMyEnt->targetyaw + RealYOffset, m_pMyEnt->yaw);
// m_pMyEnt->pitch = WrapXAngle(m_pMyEnt->targetpitch); // Uncomment for instant aiming
// m_pMyEnt->yaw = WrapYZAngle(m_pMyEnt->targetyaw);
m_pMyEnt->pitch = WrapXAngle(flIdealPitch);
m_pMyEnt->yaw = WrapYZAngle(flIdealYaw);
}
m_pMyEnt->yaw = WrapYZAngle(flIdealYaw);
}
// Function code by Botman
float CBot::ChangeAngle(float speed, float ideal, float current)
@@ -230,7 +211,7 @@ float CBot::ChangeAngle(float speed, float ideal, float current)
// find the difference in the current and ideal angle
diff = fabs(current - ideal);
// speed that we can turn during this frame...
speed = speed * (float(BotManager.m_iFrameTime)/1000.0f);
@@ -241,7 +222,7 @@ float CBot::ChangeAngle(float speed, float ideal, float current)
// check if the bot is already facing the idealpitch direction...
if (diff <= 1.0f)
return ideal;
if ((current >= 180) && (ideal >= 180))
{
if (current > ideal)
@@ -261,7 +242,7 @@ float CBot::ChangeAngle(float speed, float ideal, float current)
else if ((current < 180) && (ideal >= 180))
{
current_180 = current + 180;
if (current_180 > ideal)
current += speed;
else
@@ -276,7 +257,7 @@ float CBot::ChangeAngle(float speed, float ideal, float current)
}
return current;
return current;
}
void CBot::SendBotInfo()
@@ -303,7 +284,7 @@ float CBot::GetDistance(entity *e)
bool CBot::SelectGun(int Gun)
{
if(m_pMyEnt->weaponsel->reloading) return false;
if(m_pMyEnt->weaponsel->reloading) return false;
if (m_pMyEnt->gunselect != Gun) audiomgr.playsound(S_GUNCHANGE, m_pMyEnt);
m_pMyEnt->gunselect = Gun;
return true;
@@ -319,21 +300,21 @@ bool CBot::IsVisible(vec o, int Dir, float flDist, bool CheckPlayers, float *pEn
{
static vec angles, end, forward, right, up;
static traceresult_s tr;
end = o;
angles = GetViewAngles();
angles.x = 0;
if (Dir & UP)
angles.x = WrapXAngle(angles.x + 45.0f);
else if (Dir & DOWN)
angles.x = WrapXAngle(angles.x - 45.0f);
if ((Dir & FORWARD) || (Dir & BACKWARD))
{
if (Dir & BACKWARD)
angles.y = WrapYZAngle(angles.y + 180.0f);
if (Dir & LEFT)
{
if (Dir & FORWARD)
@@ -357,17 +338,17 @@ bool CBot::IsVisible(vec o, int Dir, float flDist, bool CheckPlayers, float *pEn
angles.x = WrapXAngle(angles.x + 90.0f);
else if (Dir & DOWN)
angles.x = WrapXAngle(angles.x - 90.0f);
AnglesToVectors(angles, forward, right, up);
forward.mul(flDist);
end.add(forward);
TraceLine(o, end, m_pMyEnt, CheckPlayers, &tr);
if (pEndDist)
*pEndDist = GetDistance(o, tr.end);
return !tr.collided;
}
@@ -386,7 +367,7 @@ void CBot::SetMoveDir(int iMoveDir, bool add)
m_pMyEnt->strafe = -1;
else if (!add)
m_pMyEnt->strafe = 0;
if (iMoveDir & UP)
m_pMyEnt->jumpnext = true;
}
@@ -419,5 +400,5 @@ void CBot::ResetCurrentTask()
break;
}
}
// Bot class end