Implementing NextBot into your mod

From Valve Developer Community
Jump to: navigation, search

Template:Prod

Important Note: This tutorial has been written for the multiplayer branch of Source SDK 2013, used with Visual Studio 2013. The singleplayer branch, along with older versions of the SDK and other versions of Visual Studio, have been untested, but the process should be relatively the same.

The NextBot AI system is not built-in to the Source SDK by default, but a source code leak allows for the system to be implemented by hand. The process isn't perfect, but this should give modders the ability to utilize the system and understand how it works.

Setting up the Serverside Files

To get started, you'll need to download the repository containing the NextBot system's code here. What you'll end up with is a .zip folder containing all of the essential server code the system needs to function.

Extract the contents of the .zip to your mod's src/game/server folder, and be sure to rename the resulting "NextBot-master" folder to simply "NextBot". What you should end up with is your server code containing a designated folder for all NextBot-related files.

A visualization of how the NextBot system's code should be organized in your mod's server project.

Next, you'll need to access your mod's solution (preferably everything.sln) in Visual Studio. From here, access the "Server (your mod's name)" project, open the "Source Files" folder, and begin putting in NextBot files/folders as illustrated on the image to the right. This set-up should be identical to how your mod's src/game/server/NextBot folder is organized.

Code fixes

There are a couple important fixes that have to be done before the NextBot system can properly work serverside.

In several files of the SDK, there is an #ifdef NEXT_BOT requirement that blocks off certain pieces of NextBot code from running. This will cause, among other side-effects, all NextBot entities to be incapable of movement. You'll need to look up this requirement in the files (try pressing CTRL+F and searching the entire solution for the phrase "#ifdef NEXT_BOT") and delete any found instances of these requirements. For example, in basecombatcharacter.cpp, around line 42:

#include "NextBot/NextBotManager.h" // We need to remove the #ifdef requirement surrounding this line, as it will stop a piece of NextBot code from working

After eradicating this requirement, there may be at least one error encountered in multiplay_gamerules.cpp around line 1823, involving bots reacting to a player's voice commands. This piece of code is not essential to having the NextBot system work, so it's up to you on how you want to deal with it; if you feel like you're not going to need it in your mod, feel free to delete it.

Once everything is in order, you can build the solution. If no more errors occur from that point on, then you're good to go.

Setting up the Clientside Files

You may notice at this point that the NextBot code doesn't contain any clientside files. If you were to try running your mod and going into a server after compiling, you would automatically be kicked for lacking a "DT_NextBot" class. Fortunately, there is a rudimentary solution that will at the very least stop the server from instantly disallowing clients to join.

In Visual Studio, you'll need to access the "Client (your mod's name)" project and open its own "Source Files" folder. In there, add two brand new files called c_NextBot.h and c_NextBot.cpp. These two clientside files are going to be barebones, since we currently have no idea what the system's original programmers put into them, but this should allow you to access your mod's server(s) and properly play the game.

This code is what should be in c_NextBot.h:

#ifndef C_NEXTBOT_H
#define C_NEXTBOT_H
#ifdef _WIN32
#pragma once
#endif


#include "c_basecombatcharacter.h"

// NOTE: Moved all controller code into c_basestudiomodel
class C_NextBotCombatCharacter : public C_BaseCombatCharacter
{
	DECLARE_CLASS(C_NextBotCombatCharacter, C_BaseCombatCharacter);

public:
	DECLARE_CLIENTCLASS();

	C_NextBotCombatCharacter();

private:
	C_NextBotCombatCharacter(const C_NextBotCombatCharacter &); // not defined, not accessible
};


#endif // C_NEXTBOT_H

This code is what should be in c_NextBot.cpp:

#include "cbase.h"
#include "c_NextBot.h"
#include "engine/ivdebugoverlay.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

IMPLEMENT_CLIENTCLASS_DT(C_NextBotCombatCharacter, DT_NextBot, NextBotCombatCharacter)
END_RECV_TABLE()

C_NextBotCombatCharacter::C_NextBotCombatCharacter()
{
}

After building your solution with these new files, you should be able to play a map as you would normally.

Testing

Once all is said and done, with hopefully no additional errors encountered, boot up a map of your choosing and enable cheats in the console. Generate a navigation mesh for your map, if one doesn't already exist. Use the command simple_bot_add to spawn an instance of simple bot where your crosshair is pointing. If that entity is aimlessly moving around the map, you have successfully implemented the NextBot system into your mod!