.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   Half-Life 2 SDK (http://forums.bots-united.com/forumdisplay.php?f=62)
-   -   Yay - Bots move to given vector location... (http://forums.bots-united.com/showthread.php?t=3326)

stefanhendriks 03-01-2005 17:50

Yay - Bots move to given vector location...
 
It took a bit of fiddling , but i got it working, so here a little tutorial to get it working for you too (based on botmans template), its not completely step by step, but it has lots of code snippets.

first, make sure the viewangles get updated by WriteUserCmd in the hpb_bot2_usercmd.x file:

Code:

// THIS IS OLD....
// buf->WriteOneBit( 0 ); // viewangles[0]
// buf->WriteOneBit( 0 ); // viewangles[1]
// buf->WriteOneBit( 0 ); // viewangles[2]
 
        // Viewangles - THIS IS NEW
        buf->WriteOneBit( 1 );
        buf->WriteBitAngle( cmd->viewangles[ 0 ], 16 );
        buf->WriteOneBit( 1 );
        buf->WriteBitAngle( cmd->viewangles[ 1 ], 16 );
        buf->WriteOneBit( 1 );
        buf->WriteBitAngle( cmd->viewangles[ 2 ], 8 );

ok, that will make life a bit more easier ;)

So, first we need a vector in the bot_t so we get our bot moving to that vector constantly. Since the usercmd calls them viewangles (which is NOT correct) , i call it vViewVector.

Code:

typedef struct
{
edict_t* edict;
float fCreateTime; // time bot was created
float fJoinTime; // time when bot should do stuff to join the game (select team, etc)
 
 
        int                iTeam;                // the time the bot should join
                                                                // FIXME: make it also change when team is forced to another
                                                                //                so it can be used for knowing which team ur on.
 
        Vector vViewVector;
 
} bot_t;
 
 
// AND in CreateBot
 
// STEFAN:
        pBot->vViewVector = Vector(0,0,0);

Ok, so now we have the viewvector, we need a function to calculate the correct angles to get them in the usercommand... this function is called VectorAngles, and is located in mathlib.* (in public/) , though you cannot use them, they give compile errors which i was not able to fix, so what i did, i ripped it and removed the FastSqrt , replaced it with the (slow)/normal sqrt, and wham we got our own function. So copy this piece of code at the TOP (above the BotRunPlayerMove function):

Code:

//-----------------------------------------------------------------------------
// Forward direction vector -> Euler angles
//-----------------------------------------------------------------------------
// COPIED FROM MATHLIB.CPP - STEFAN
void UTIL_VectorAngles( const Vector& forward, QAngle &angles )
{
// Assert( s_bMathlibInitialized );
float tmp, yaw, pitch;
 
if (forward[1] == 0 && forward[0] == 0)
{
yaw = 0;
if (forward[2] > 0)
pitch = 270;
else
pitch = 90;
}
else
{
yaw = (atan2(forward[1], forward[0]) * 180 / M_PI);
if (yaw < 0)
yaw += 360;
 
                // STEFAN: use normal sqrt
tmp = sqrt (forward[0]*forward[0] + forward[1]*forward[1]);
pitch = (atan2(-forward[2], tmp) * 180 / M_PI);
if (pitch < 0)
pitch += 360;
}
 
angles[0] = pitch;
angles[1] = yaw;
angles[2] = 0;
}

go to the BotRunPlayerMove function that botman created; and get this before the processusercmd function:

Code:

        if (pBot->vViewVector != Vector(0,0,0))
        {
                UTIL_VectorAngles( pBot->vViewVector, cmd.viewangles );
 
                // clamp the pitch and yaw (as seen in the sdk)
                if (cmd.viewangles[0] > 180)
                        cmd.viewangles[0] -= 360;
                if (cmd.viewangles[1] > 180)
                        cmd.viewangles[1] -= 360;
}

now, to get a bot going where you want it to go, lets say you have vector vDir, you have to do this:

Code:

                                        Vector botVec=Vector(0,0,0);
                                        gameclients->ClientEarPosition( pPlayerEdict, &botVec );
                                        vDir = vDir - botVec;
                                        pBot->vViewVector = vDir; // ugly hack to set viewdir

so:
1. get the bot vector itself
2. substract your vector with the botvector
3. and finally set vViewVector to vDir

* EDIT:
Let me know if it does , or does not, work for you. I could have forgotten something.

NOTE:
THe bots do not LOOK at the direction, they MOVE at the direction, given the forward speed of the usercmd. (there is no sidespeed set, etc).

I am still looking for a proper 'looking' function.

stefanhendriks 04-01-2005 23:48

Re: Yay - Bots move to given vector location...
 
so, did it work for anyone as well? (or any difficulties?). Or does this silence mean this post is uber l33t? :P

botman 05-01-2005 02:25

Re: Yay - Bots move to given vector location...
 
I was able to get them to move in the proper YAW direction once I added the viewangles stuff (but I was just setting the YAW directly via a ClientCommand thing I added so that I could change it on the fly).

I looked through the SDK dlls code a bit to try to determine what sets the facing direction properly, but never found it.

I won't have time to look at this stuff again until probably late February or early March.

botman

stefanhendriks 05-01-2005 09:45

Re: Yay - Bots move to given vector location...
 
i just hope the looking will be also 'as easy' because frankly i havent seen anything that will help me just yet. I try to find some info on a'fire a bullet' function so i know how they calculate it, but without getting the SDK compiled 100% properly under MSVC i am stuck with searching through files with Windows Commander (which is slow. :S)

Cheeseh 05-01-2005 14:34

Re: Yay - Bots move to given vector location...
 
I gave alfred an email about how to get "EyeAngles" without CBaseEntity/CBasePlayer, just the edict. I'll see if he gives a response, or the bot documentation for the SDK will be released before then :P

stefanhendriks 05-01-2005 14:40

Re: Yay - Bots move to given vector location...
 
rofl yeah, well its good to keep on mailing them :D so far we made quite some progress imo in a 'short' amount of time :D

Cheeseh 06-01-2005 15:14

Re: Yay - Bots move to given vector location...
 
Alfred replied to say that you can only change eye angles if you have access to CBasePlayer (as botman said :p) They're working on how to access eye angles through plugins but have no date.

stefanhendriks 06-01-2005 16:31

Re: Yay - Bots move to given vector location...
 
cheeseh, i tried ur code , but though i could not test it myself. Asp tried it for me , and he said it did not work? Are you sure it works for player entities as well?

Cheeseh 06-01-2005 17:00

Re: Yay - Bots move to given vector location...
 
The origin code? It works for me as a player, maybe it doesn't work for bots. I haven't tried it for getting bots origins though ...

@$3.1415rin 06-01-2005 17:06

Re: Yay - Bots move to given vector location...
 
the bots with stefans code ran always towards this corner at the ct spawn in de_cbble near to that bomb site and that fence there. dunno the coordinates yet there, but I wouldnt be surprised if there would be (0,0,0) :)

stefanhendriks 06-01-2005 17:15

Re: Yay - Bots move to given vector location...
 
it would not suprise me if it did not work for bots... ;)

Cheeseh 06-01-2005 18:47

Re: Yay - Bots move to given vector location...
 
actually I just tried it with the bots, they got the origin ok, maybe you do need to "GetIServerEntity()" because i was still using that : *changes code on post*

botman 07-01-2005 02:26

Re: Yay - Bots move to given vector location...
 
I think when the bots spawn, they rotate to face the direction (YAW) of the spawnpoint, then they stay facing that way for the rest of the game.

Try creating your own little CS map with T and CT spawn points that face 0 and 180 degrees and I bet the bots that spawn there will be facing each other (or facing away from each other, whatever).

botman

stefanhendriks 07-01-2005 11:16

Re: Yay - Bots move to given vector location...
 
botman, that would not suprise me, as the spawn points always have had control about the view angles (in Hl1 aswell). Though this was not 'default' in HL1 with fake clients if i remember correctly :)

Gun-Nut 08-01-2005 01:09

Re: Yay - Bots move to given vector location...
 
got anymore good news?

botman 08-01-2005 02:33

Re: Yay - Bots move to given vector location...
 
I'm sure when something changes, someone will post news. No news means nothing has changed.

botman

stefanhendriks 09-01-2005 22:05

Re: Yay - Bots move to given vector location...
 
something changes
http://johannes.lampel.net/pics/shit/de_cbble0005.jpg

SteveC delivered some very helpfull stuff; though its not the neatest way to do it ;)

EDIT:
BUT there is no neater to way to do it as well!

Cheeseh 10-01-2005 21:30

Re: Yay - Bots move to given vector location...
 
** Total Edit ** :P

Alright, well I managed to get SteveC's CBaseEntity trick to work with my bots and got my bots to follow me whilst they look in the same direction (they don't look at me to follow me).

I have an example movie of a bot doing this here:

http://rcbot.bots-united.com/downloa...er/botmove.zip

The movement is actually the same as HL1 but the QAngles were between 0 and 360 instead of -180 and 180, which was confusing me.

PHP Code:

void CBot :: doMove ()
{
    
// Temporary measure to make bot follow me when i make listen serevr
    
setMoveTo(CBotGlobals::entityOrigin(INDEXENT(1)));

    if ( 
moveToIsValid () )
    {
        
float flMove 0.0;
        
float flSide 0.0;

        
m_fIdealMoveSpeed 320;

        
// fAngle is got from world realting to bots origin, not angles
        
float fAngle CBotGlobals::yawAngleFromEdict(m_pEdict,m_vMoveTo);
        
// fl Move is percentage (0 to 1) of forward speed,
        // flSide is percentage (0 to 1) of side speed.
        
        
float radians fAngle 3.141592f 180.0f// degrees to radians
        
flMove cos(radians);
        
flSide sin(radians);

        
m_fForwardSpeed m_fIdealMoveSpeed flMove;

        
// dont want this to override strafe speed if we're trying 
        // to strafe to avoid a wall for instance.
        
if ( m_fStrafeTime engine->Time() )
        {
            
// side speed 
            
m_fSideSpeed m_fIdealMoveSpeed flSide;
        }

        
// moving less than 1.0 units/sec? just stop to 
        // save bot jerking around..
        
if ( fabs(m_fForwardSpeed) < 1.0 )
            
m_fForwardSpeed 0.0;
        if ( 
fabs(m_fSideSpeed) < 1.0 )
            
m_fSideSpeed 0.0;
    }
    else
    {    
        
m_fForwardSpeed 0;
        
// bots side move speed
        
m_fSideSpeed 0;
        
// bots upward move speed (e.g in water)
        
m_fUpSpeed 0;
    }


To get the angle, here is the code i used:

PHP Code:

float CBotGlobals :: yawAngleFromEdict (edict_t *pEntity,Vector vOrigin)
{
    
float fAngle;
    
QAngle qBotAngles entityEyeAngles(pEntity);
    
QAngle qAngles;
    
Vector vAngles;

    
vAngles vOrigin entityOrigin(pEntity);

    
// Just get the angles from world
    
VectorAngles(vAngles,qAngles);
    
    
// NEED to put 0-360 angles from -180 to 180 to get negative and positive values
    
fixFloatAngle(&qBotAngles.y);
    
//fAngle = qAngles.y; // world angles
    
fAngle qBotAngles.qAngles.y;

    
fixFloatAngle(&fAngle);

    return 
fAngle;


you NEED CBaseEntity to get eye angles
PHP Code:

QAngle CBotGlobals :: entityEyeAngles edict_t *pEntity )
{
    
CBaseEntity *pBaseEntity CBaseEntity::Instance(pEntity);

    return 
pBaseEntity->EyeAngles();



stefanhendriks 11-01-2005 00:19

Re: Yay - Bots move to given vector location...
 
yes, i got this working too, you can see it in the ubframework alpha template.

currently my css bots walk around de_cbble using waypoints! yay.

Cheeseh 11-01-2005 00:24

Re: Yay - Bots move to given vector location...
 
crap well when I try it again while changing the viewangles they move all over the place, I think there might be something else to do with the getting the right angle from either world or player forward vector, I'm still a bit unsure

Cheeseh 11-01-2005 00:34

Re: Yay - Bots move to given vector location...
 
ahh god I'm so ignorant :(

The movement is the SAME !!

It's just that EyeAngles returns a angle from 0 to 360 instead of -180 to 180 I think.

So my old code works fine now (changed in post)... it works perfectly too, the last one was a bit dodgy

stefanhendriks 11-01-2005 10:42

Re: Yay - Bots move to given vector location...
 
this is what i use:

Code:

Vector vMoveDir;
        Vector vLookDir;
       
        pBot->vLookAtVector = pBot->vMoveToVector; // ITS THE SAME
        vLookDir = pBot->vLookAtVector;        // look at this
        AngleVectors(AngleWeMoveTo, &vMoveDir); // get movement direction
       
        // move in vMoveDir
        // dot product vMoveDir onto vLookDir to get forward movement
       
        float fForwardDot=DotProduct(vMoveDir, vLookDir); // get the proportion of forwards
       
        Vector vRightLook, vUpLook;
        VectorVectors(vLookDir, vRightLook, vUpLook); // get the vector perpendicular to the forward vector
       
        float fSideDot=DotProduct(vMoveDir, vRightLook); // get the proportion of side
       
       
        cmd.forwardmove=fForwardDot*200.0f;
        cmd.sidemove=fSideDot*200.0f;


SteveC 11-01-2005 18:56

Re: Yay - Bots move to given vector location...
 
Works a treat doesn't it Stefan ;).

Oh and by the way people I think I've worked out how to get the bots to shoot at enemies, (human or bot). But I need to do some testing first, should have a result by the weekend.

stefanhendriks 11-01-2005 21:25

Re: Yay - Bots move to given vector location...
 
got that working already :P

Cheeseh 11-01-2005 22:14

Re: Yay - Bots move to given vector location...
 
Quote:

Originally Posted by SteveC
Works a treat doesn't it Stefan ;).

Oh and by the way people I think I've worked out how to get the bots to shoot at enemies, (human or bot). But I need to do some testing first, should have a result by the weekend.

don't you just get the team Index from playerinfo and see if its different? Cos if they are it's an enemy.

SteveC 11-01-2005 23:24

Re: Yay - Bots move to given vector location...
 
Yeah, but to get the entity list there may be more than one technique, I'm using the edict list.

I guess we'll have a bot build off for a couple of weeks, then work together to produce a kick ass bot, once we know how everything works. Up for it?

stefanhendriks 12-01-2005 00:13

Re: Yay - Bots move to given vector location...
 
hey stevec, thats what Bots-United is for... to work together on kick-ass bots. Or alteast, at a kick-ass-framework

my snippet:

Code:

void BotFindEnemy(bot_t *pBot)
{
        // FIXME: Make this use UTIL_TRACELINE some day?
        Vector vFrom, vTo;       
        Ray_t mRay;                // the ray itself
 CTraceFilterHitAll mTraceFilter;       
        CGameTrace tRay;        // the results
        CBaseEntity *pEnemyBaseEntity=NULL;
        if (pBot->pEnemy)
        {
                BotAttackEnemy(pBot);
                return;
        }
        float fDistance=9999;
        // scan for enemy activities
 for ( int i = 1; i <= 32; i++ )  // EVIL: shouldn't just assume maxplayers is 32!!!
 {
  edict_t *pPlayerEdict = INDEXENT( i );
 
                if (pPlayerEdict)  // valid edict               
                {                         
                        if (pPlayerEdict == pBot->edict)
                                continue; // do not check self
                        pEnemyBaseEntity = CBaseEntity::Instance(pPlayerEdict);// get the player entity
                        if (pEnemyBaseEntity == NULL)
                                continue; // failed
                        // Check if its a player. (thx botman for pointing me out on this <shame>)
                        if (!pEnemyBaseEntity->IsPlayer())
                                continue;
                        if ( !pPlayerEdict->IsFree() ) // valid client
                        {                 
                                // HACK HACK
                         
                                // ok, this COULD be our enemy, so check this team status                               
                                IPlayerInfo* pInfo = NULL;
                               
                                if (playerinfomanager)
                                        pInfo = playerinfomanager->GetPlayerInfo(pPlayerEdict);
               
                                if (pInfo == NULL || !pInfo)
                                        return; // get out, this wont work :S                                        */
                               
                                // Set team (in case the game dll changed it for us)
                                int iTeam = pInfo->GetTeamIndex();
                         
                                if (iTeam != pBot->iTeam)
                                {                                       
                                        // potential enemy
                                        vFrom = pBot->vMyOrigin;
                                        gameclients->ClientEarPosition( pPlayerEdict, &vTo );  // to = his origin
                                        // TODO: How to make sure it is in our viewangles?
                                        QAngle viewangles;
                                        UTIL_VectorAngles( pBot->vLookAtVector, viewangles );
                                        if (FInViewCone(vFrom, vTo, viewangles) == false)
                                                continue;
                                        //bool FInViewCone (Vector vStart, Vector vLookDir, QAngle viewangles)
                                        // CHECK if enemy is behind wall or not
                                       
                                        if (UTIL_DistanceBetween(vFrom, vTo) > fDistance)
                                                continue; // too far
                                        mRay.Init(vFrom,vTo); // init
                                       
                                        enginetrace->TraceRay(mRay, MASK_PLAYERSOLID, &mTraceFilter, &tRay);

                                        //if (pEnemyBaseEntity == NU
                                        // success
                                        if (tRay.fraction == 1.0 || tRay.m_pEnt == pEnemyBaseEntity)
                                                {
                                                // could be our enemy
                                                fDistance = UTIL_DistanceBetween(vFrom, vTo);
                                                // Set as our enemy for now
                                                pBot->pEnemy = pPlayerEdict;
                                        }
                                }
                        }               
                }
 }
}

Note:
my "FInViewCone" function... is a bit of a fuzz. I do not know if it works 100%, here it is. Btw, i did not notice odd behaviour just yet...

Code:

bool FInViewCone (Vector vStart, Vector vLookDir, QAngle viewangles)

  float flDot;
  Vector vMoveDir;
  // We move to this
  AngleVectors(viewangles, &vMoveDir);
 
  Vector vRightLook, vUpLook;       
  VectorVectors(vLookDir, vRightLook, vUpLook); // get the vector perpendicular to the forward vector
 
  Vector vDest = vLookDir-vStart;
  // vLookDir
  flDot = DotProduct(vDest, vLookDir);       
  //Msg("FlDOT=%f ", flDot); 
         
  if (fabs(flDot) > 0.50)  // 60 degree field of view
        {
          return TRUE;
        }
  else
        {
          return FALSE;
        }
}


SteveC 12-01-2005 00:36

Re: Yay - Bots move to given vector location...
 
OK, I'll help where I can then.

If you want to fix the //EVIL on the maxplayers you can get the max players when the ServerActivate function is called in the plugin part of the code.

And I think there is a get FOV function in the CBasePlayer class we can use, but I'll need to check. It will help with FOV when using snipers etc.

stefanhendriks 12-01-2005 08:50

Re: Yay - Bots move to given vector location...
 
About maxplayers, yes i know, i am just really lazy to fix that ;)

for FOV, i did a quick search in the base class and the player class but did not find anything yet. I did not use "FOV" as keysearch though. :S Perhaps its there, but i think my function works? :)

Pierre-Marie Baty 12-01-2005 10:45

Re: Yay - Bots move to given vector location...
 
I think I found out how to make bots switch weapons. You must pass in the wpnselect field of the usercmd struct the entity index of the weapon to select. It's not a bitfield like I initially thought. However since I have little clue so far on how to peek around entities, I have not been able to test.

dub 12-01-2005 11:50

Re: Yay - Bots move to given vector location...
 
small fix to check if a dedicated server;)
Code:

    void CUBBOT_Plugin::ClientActive (edict_t *pEntity)
    {
            if (!engine->IsDedicatedServer () && pHostEdict == NULL)
            {
                    Msg ("PLUGIN: Host edict found and set\n");
                    pHostEdict = pEntity;
            }
    }

has any one found out how to show menu`s yet?

edit few engine notes.
-- class CBasePlayer pBot->pPlayerEnt --
pPlayerEnt->MaxSpeed (); // max speed
pPlayerEnt->IsSuitEquipped (); // armor equipped
pPlayerEnt->ArmorValue (); // armor amount
pPlayerEnt->GetFOV (); & pPlayerEnt->GetDefaultFOV (); // get current, default fov

-- CBaseEntity class pBot->pBaseEntity ---
pBaseEntity->GetMaxHealth (); // max health
pBaseEntity->GetHealth (); // current health
pBaseEntity->GetTeamNumber (); // current team
pBaseEntity->TeamID (); // current team`s name
// CS:S : 1 = Spectator, 2 = Terrorist, 3 = Counter-Terrorist
pBaseEntity->InSameTeam (pBaseEnemy); // is client on the same team
enjoy Dubb;)

stefanhendriks 12-01-2005 13:40

Re: Yay - Bots move to given vector location...
 
great work Dub! Thats something i always wanted to know.

@ pmb, yes i already know you have to do something with that cmd.weaponselect thingy, but how its parsed, thats a clue to me (perhaps an impulse command).

/me digs very quick in the code:

* found this in usercmd.h (snippet)

Code:

// Impulse command issued.
 byte        impulse;               
 // Current weapon id
 int  weaponselect;
 int  weaponsubtype;

means, we can pass through an impulse command. And we can do weaponselect (INT) and weaponsubtype (INT).

Perhaps its the 'menu + submenu' thing. Like, ie, in CS the 4th weapon slot = grenades, and subtype is then a frag/flash/smoke grenade. But this also means you have to keep track of what a bot has because this 'order' changes (depending on what you have bought or not)...

dub 12-01-2005 16:50

Re: Yay - Bots move to given vector location...
 
solution to getting weapon information.

add to the bot_t structure this
Code:

  CBaseCombatCharacter *pBaseCombatChar;
then after the CBaseEntity hack by stevec add this
Code:

  pBot->pBaseCombatChar = pBot->pBaseEntity->MyCombatCharacterPointer ();
to access info about the current weapon all you need to do is this
Code:

  CBaseCombatWeapon *pCurrentWeapon = pBot->pBaseCombatChar->GetActiveWeapon ();
enjoy Dubb;)

stefanhendriks 12-01-2005 17:26

Re: Yay - Bots move to given vector location...
 
hmm yes, still remains an ugly hack :) any files we need to include with the project as well? or is that it? :)

*EDIT:
i think you can change weapons via that way as well right? But, in CSS this would probably be different compared to the HL2 SDK code... did you test this code you posted here?

dub 12-01-2005 17:36

Re: Yay - Bots move to given vector location...
 
untested the hack, having problem`s compiling with
CBaseCombatWeapon class included.

stefanhendriks 12-01-2005 18:48

Re: Yay - Bots move to given vector location...
 
i was afraid for that ;)

stefanhendriks 12-01-2005 18:49

Re: Yay - Bots move to given vector location...
 
About the pBaseEnt->GetFOV

it does nothing but return a floating point if iw as not mistaken... does not help us much yet ;)

dub 12-01-2005 19:36

Re: Yay - Bots move to given vector location...
 
stefan did you manage to compile accessing pPlayerEnt->GetFOV ()?
i keep getting unresolved external compiler error.
Code:

ubbot.obj : error LNK2001: unresolved external symbol "public: int __thiscall CBasePlayer::GetFOV(void)const " (?GetFOV@CBasePlayer@@QBEHXZ)
maybe because it is for the client dll only unsure???:(.

SteveC 12-01-2005 19:39

Re: Yay - Bots move to given vector location...
 
Good work Dub!

For finding out what you can access within the classes you need to check for members which are either fully defined within the header file, or are declared with the virtual state. Then you include the header file only and don't need to worry about the linking to the cpp file. However this technique only works because they have derived the 'working' classes from the ones we can see. E.G. for CS the player class is actually CSPlayer. It's not a hack, but it will fail if a mod doesn't follow valves layout to the letter.

Also, isn't the float that GetFOV() returns related to the angle of the viewing cone, either degrees or possibly radians?

dub 12-01-2005 19:59

Re: Yay - Bots move to given vector location...
 
thanks steve for the tips, much appreciated :D.
just a quick question, does this mean we can`t use GetFOV () because it`s not declared virtual or in the baseentity header?


All times are GMT +2. The time now is 19:24.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.