PDA

View Full Version : Could bots work?



w00tguy123
24-01-2016, 01:32 AM
I'm trying to figure out if it's feasible to make bots in Angelscript. I didn't get very far because I can't spawn a fake player.

In EntityManager.as there is a function called "AddFakeClient" that seems to do what I want. It looks like it was never finished though, since it fails before Spawn() is called. This adds a new client in the player list, but no player is spawned.

I tried creating a player using g_EntityFuncs.Create("player", origin, angles, false, pEdict), which seems to have worked, but the game quits immediately with these errors in the console:

Host_Error: WriteDest_Parm: not a client
Netchan_Clear() : reliable length not 0, reliable_sequence: 22, incoming_reliable_acknowledged: 1
Netchan_Clear() : reliable length not 0, reliable_sequence: 15, incoming_reliable_acknowledged: 1
ERROR: Angelscript: Active module was not set to null!

So I downloaded the source code for an old version of RCBot (http://rcbot.bots-united.com/downloads/rcbot_svencoop_45_src.zip) to see how metamod coders created bots, and it looks like they use some functions which are not available in AS:


pEdict = (*g_engfuncs.pfnCreateFakeClient)( gBotGlobals.m_Bots[iBotIndex].m_Profile.m_szBotName );

if (FNullEnt( pEdict ))
{
BotMessage( pEntity, 0, "Maximum Players Reached, Can't Create Another Bot!");
return BOT_CVAR_ERROR;
}

char ptr[128]; // allocate space for message from ClientConnect
char *sInfoBuffer; // Bots infobuffer
int index; // bots edict index

pEdict->v.frags = 0; // reset his frag count

#ifdef RCBOT_META_BUILD
CALL_GAME_ENTITY(PLID,"player",VARS(pEdict)); // Olo
#else
// create the player entity by calling MOD's player function
// (from LINK_ENTITY_TO_CLASS for player object)

player( VARS(pEdict) );
#endif

index = ENTINDEX( pEdict );

#ifdef RCBOT_META_BUILD
sInfoBuffer=GET_INFOKEYBUFFER(pEdict);
#else
sInfoBuffer = (*g_engfuncs.pfnGetInfoKeyBuffer)( pEdict );
#endif

// pointer to new bot
pBot = &gBotGlobals.m_Bots[iBotIndex];

...

(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "rate", "3500");
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "cl_updaterate", "20");
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "cl_lw", "1");
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "cl_lc", "1");
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "cl_dlmax", "128");

if ( !gBotGlobals.IsMod(MOD_TS) )
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "_vgui_menus", "0");
else
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "_vgui_menus", "1");

(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "_ah", "0");
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "dm", "0");
(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "tracker", "0");
//(*g_engfuncs.pfnSetClientKeyValue)(index, sInfoBuffer, "name", gBotGlobals.m_Bots[iBotIndex].m_Profile.m_szBotName);

...

#ifdef RCBOT_META_BUILD
// update joining clients because metamod wont call engine client connect?
//gBotGlobals.m_iJoiningClients++;
MDLL_ClientConnect( pEdict, szName, "127.0.0.1", ptr );
ClientConnect( pEdict, szName, "127.0.0.1", ptr );

MDLL_ClientPutInServer( pEdict );
ClientPutInServer( pEdict );

gBotGlobals.m_Clients.ClientConnected(pEdict);
#else
ClientConnect( pEdict, szName, "127.0.0.1", ptr );
// Pieter van Dijk - use instead of DispatchSpawn() - Hip Hip Hurray!
ClientPutInServer( pEdict );
#endif


The ones that seem interesting are "ClientConnect", "ClientPutInServer", and whatever "pfnSetClientKeyValue" could be (I assume there are some client-related functions that aren't exposed).

Is is possible to add fake players in 5.0?

Solokiller
25-01-2016, 05:22 AM
Sorry for the late reply:
The AddFakeClient command is a bare bones usage of the engine function used to create fake clients.
Creating players directly is not supported and should be avoided.

Support for bots can be added directly if that's needed. Then all you'd have to do is something like g_PlayerFuncs.CreateBot. We'll look into this when there's time.

w00tguy123
25-01-2016, 01:46 PM
I don't know yet if players can be controlled with AS (i.e. press their fire button for them), but that would a ton of fun to play with. Thanks for the reply.

Solokiller
01-02-2016, 09:50 AM
I've removed CEngineFuncs::CreateFakeClient and added CPlayerFuncs::CreateBot and BotDisconnect. These methods, combined with CEngineFuncs::RunPlayerMove, are all you need to create bots. I made a simple implementation that makes bots move in random directions, randomly fire their weapons, and has their model colors change randomly.

The next update will have all of this.

w00tguy123
01-02-2016, 01:57 PM
Seriously!? I can't wait to try this out. Thank you!