.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   Half-Life 2 SDK (http://forums.bots-united.com/forumdisplay.php?f=62)
-   -   How to do correct movement (http://forums.bots-united.com/showthread.php?t=3369)

SteveC 09-01-2005 01:10

How to do correct movement
 
To get the bots to move correctly in my plugin, http://forums.bots-united.com/showthread.php?t=3366 , I used the following function;

Quote:

void CBasePlayer:: ProcessUsercmds( CUserCmd *cmds, int numcmds, int totalcmds,
int dropped_packets, bool paused )

To get to this you need to get the CBasePlayer pointer, which I did with the following code;

Quote:

// entity index
iBotIndex=ENTINDEX(pEdict);
// base entity
pBaseEnt = CBaseEntity::Instance(pEdict);
// get the player entity
if ( pBaseEnt && pBaseEnt->IsPlayer() )
{
pPlayerEnt = static_cast< CBasePlayer * >( pBaseEnt );
}
if (!pPlayerEnt) return false;
Where pEdict is the bot edict_t, pBaseEnt is a CBaseEntity pointer, and pPlayer is the CBasePlayer pointer. I can't remember which files I needed to include to get this to work, but I think it was 'cbase.h' as it links to most things.

SteveC 09-01-2005 01:13

Re: How to do correct movement
 
To build the CUserCmd I used the following code;

Quote:

// look
if (pClosestEnemy)
{
// look at enemy
Vector vAim=vClosestEnemy-pBaseEnt->EyePosition();
VectorAngles(vAim, mBotCmd.viewangles);
}
else if (tFront.fraction<0.5f)
{
mBotCmd.viewangles.y+=90.0f+((float)rand() / RAND_MAX) * 180.0f; // turn 180 (ish)
}
else if (tLeft.fraction<0.5f)
{
mBotCmd.viewangles.y+=22.5f+((float)rand() / RAND_MAX) * 45.0f; // turn +45 (ish)
}
else if (tRight.fraction<0.5f)
{
mBotCmd.viewangles.y-=22.5f+((float)rand() / RAND_MAX) * 45.0f; // turn -45 (ish)
}
// forward
if (pClosestEnemy)
{
mBotCmd.forwardmove=0.0f; // stop to shoot at enemy
}
else
{
mBotCmd.forwardmove=200.0f;
}
//shoot
if (((float)rand() / RAND_MAX)>0.97)
{
mBotCmd.buttons|=IN_ATTACK;
}
else
{
mBotCmd.buttons&=~IN_ATTACK;;
}
//jump
if (((float)rand() / RAND_MAX)>0.97)
{
mBotCmd.buttons|=IN_JUMP;
}
else
{
mBotCmd.buttons&=~IN_JUMP;
}
// issue the command
pPlayerEnt->ProcessUsercmds(&mBotCmd,1,1,0,false);


SteveC 09-01-2005 01:21

Re: How to do correct movement
 
The most critical part to get them to turn, and move the way they are facing is to use this line of code;
Quote:


// this changes the fixangle from absolute (which isn't coded for) to none
// note: has to be run here because it doesn't work in the addbot routine?!?!?
if (pPlayerEnt->PlayerData()->fixangle!=FIXANGLE_NONE) pPlayerEnt->PlayerData()->fixangle=FIXANGLE_NONE;


As the comment says the bots by default use FIXANGLE_ABS, but the HL2 movement code isn't coded to support that type, so I change it to FIXANGLE_NONE. However I haven't worked out exactly where it needs to be changed, so I check it each frame before issuing the movement command. A bit of a bodge I know ;) .

I think that's everything to get the correct movement, with full animation, physics interaction, and possibly ladder use.

I'll post my full code sometime tomorrow probably, just incase I've missed something.

Cheeseh 09-01-2005 02:23

Re: How to do correct movement
 
How did you manage to compile with CBasePlayer ? The server & plugin builds are different and don't allow having cbasePlayer/CBaseEntity defined because there are hundereds of compile errors when I try. When I also contacted Alfred (valve) he said you cannot access these in plugins and so did botman, so please share your 'secret' :P

(ps: I'm sure all of us would be intrigued to know) :)

SteveC 09-01-2005 11:09

Re: How to do correct movement
 
1 Attachment(s)
I'm no C++ linker expert (if you know someone who is, let me know :) ), so I don't know what I did that is special. I think the main difference between my plugin and the sample one is that I included cbase.h.
I cannot access all the functions of the CBasePlayer, I get compile errors with many, and if include baseplayer.cpp in the project then all hell breaks loose as you say.
One thing I did find was that the class CBotManager is a friend class to CBasePlayer, I think that means you can then access private variables of CBasePlayer. However there is no CBotManager defined anywhere in the SDK, so I define one, but it doesn't seem to help much so I don't know if just defining a class of the right name is enough to become the friend. It could be useful in the future, but you don't need it to do what I'm doing now.

Also I found out how to use the Vector class routines (includes the pesky fast sqaure root), you just need to include the mathlib, but then I got exceptions because it wasn't initialised. Initialising it lets it all work, but surely it must be already initialised for the main engine? I just found it a bit strange.

Anyway attached is the source for the current build now it's been tidied up. There are still many problems to solve, like how to find enemies.

stefanhendriks 09-01-2005 11:39

Re: How to do correct movement
 
very interesting. I already had my own way of 'moving to a direction'. I am particularry interested in the aiming part of the bot. I want to do it as much the 'less hacky' way as possible.

I'll see if i can access cBaseEntity without hacking to much around. The above code looks a lot less hacky then the one i saw before... unless i am mistaken of course.

SteveC 09-01-2005 11:45

Re: How to do correct movement
 
No hacking in the code, well not intentionally anyway. The worst thing is casting the CBaseEntity to the CBasePlayer, but that was copied directly out of valves code.

I hope it works for you too, otherwise I've no idea what I've done to get it to work. o_O

stefanhendriks 09-01-2005 11:58

Re: How to do correct movement
 
well, i wonder if it will still work after a change in CSS, or that you have to recompile or whatever. Because that is my main concern, the continuity of a project ;)

@$3.1415rin 09-01-2005 11:58

Re: How to do correct movement
 
looks like the only problem would be if a mod would change the CBasePlayer class, but as I already wondered in HL1, why shouldnt this be done by inheritance ? I mean, if nobody would change it, everything would be fine, but in HL1, almost everyone did ...

SteveC 09-01-2005 12:13

Re: How to do correct movement
 
Am I correct in assuming that it would only be a problem if they changed the number/definitions of the functions, they can change the content of the functions and it would be fine? Also you can still get to alot of stuff from the CBaseEntity, but I guess they could change that too, if they were really determined.


All times are GMT +2. The time now is 01:50.

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