SPN C++ Bot Framework
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
Api Class Reference

A class providing a simplified API for the Bots. More...

#include <bot_api.h>

Public Member Functions

 Api (IpcSharedMemory *shm)
 Constructor for the API interface object. More...
 
const IpcServerConfiggetServerConfig (void)
 Get a pointer to the server config data. More...
 
const IpcSelfInfogetSelfInfo (void)
 Get a pointer to the self information. More...
 
const IpcFoodInfogetFood (void)
 Get a pointer to the Food list. More...
 
size_t getFoodCount (void)
 Get the length of the Food list. More...
 
const IpcSegmentInfogetSegments (void)
 Get a pointer to the Segment list. More...
 
size_t getSegmentCount (void)
 Get the length of the Segment list. More...
 
const IpcBotInfogetBots (void)
 Get a pointer to the Bot list. More...
 
size_t getBotCount (void)
 Get the length of the Bot list. More...
 
void clearColors (void)
 Clear the color list. More...
 
bool addColor (uint8_t r, uint8_t g, uint8_t b)
 Add a color to the color list. More...
 
void * getPersistentMemory (void)
 Get a pointer to the persistent memory. More...
 
void log (const char *msg)
 Send a log message. More...
 

Public Attributes

ipc_real_t angle
 The direction into which the bot should move in the current frame.
 
bool boost
 Set this to true to boost in this frame.
 

Detailed Description

A class providing a simplified API for the Bots.

The main purpose of this class is to abstract away the layout details of the shared memory used to interface with the gameserver. Methods are provided to extract relevant data from the shared memory and the movement parameters for the current frame can be easily set using public member variables.

Most methods return pointers pointing into the shared memory to minimize the overhead. The returned structures are described in ipc_format.h .

Constructor & Destructor Documentation

◆ Api()

Api::Api ( IpcSharedMemory shm)
inline

Constructor for the API interface object.

Initialized the internal variables and stores the pointer to the shared memory. No need to call this from the bot code.

Parameters
shmPointer to the (already set up) shared memory.

Member Function Documentation

◆ addColor()

bool Api::addColor ( uint8_t  r,
uint8_t  g,
uint8_t  b 
)
inline

Add a color to the color list.

You should do this during your bot's initialization. Colors cannot be changed afterwards.

Parameters
rThe red channel (0-255).
gThe green channel (0-255).
bThe blue channel (0-255).
Returns
Whether the color was added successfully. False if the maximum number of colors was reached.

◆ clearColors()

void Api::clearColors ( void  )
inline

Clear the color list.

You need to add new colors using addColor() or your snake will have lame default colors!

◆ getBotCount()

size_t Api::getBotCount ( void  )
inline

Get the length of the Bot list.

Returns
The length of the array returned by getBots().

◆ getBots()

const IpcBotInfo * Api::getBots ( void  )
inline

Get a pointer to the Bot list.

The length of the list can be determined using getBotCount().

Returns
A pointer to the Bot information in the shared memory.

◆ getFood()

const IpcFoodInfo * Api::getFood ( void  )
inline

Get a pointer to the Food list.

The Food list is sorted by distance from your snake's head.

The length of the list can be determined using getFoodCount().

Returns
A pointer to the Food information in the shared memory.

◆ getFoodCount()

size_t Api::getFoodCount ( void  )
inline

Get the length of the Food list.

Returns
The length of the array returned by getFood().

◆ getPersistentMemory()

void * Api::getPersistentMemory ( void  )
inline

Get a pointer to the persistent memory.

You can use persistent memory to remember things across multiple lives of your snake. It is saved after your snake dies (even when your code crashes) and restored when it respawns.

Note that the size this memory is very limited (given by the IPC_PERSISTENT_MAX_BYTES constant). Use it wisely.

Returns
A pointer to the persistent memory.

◆ getSegmentCount()

size_t Api::getSegmentCount ( void  )
inline

Get the length of the Segment list.

Returns
The length of the array returned by getSegments().

◆ getSegments()

const IpcSegmentInfo * Api::getSegments ( void  )
inline

Get a pointer to the Segment list.

The Segment list is sorted by distance from your snake's head.

The length of the list can be determined using getSegmentCount().

Returns
A pointer to the Segment information in the shared memory.

◆ getSelfInfo()

const IpcSelfInfo * Api::getSelfInfo ( void  )
inline

Get a pointer to the self information.

This is not a list, but a pointer to a single structure containing information about your snake and parameters of the world.

Returns
A pointer to the self information in the shared memory.

◆ getServerConfig()

const IpcServerConfig * Api::getServerConfig ( void  )
inline

Get a pointer to the server config data.

This is not a list, but a pointer to a single structure containing information about the server configuration and static world information.

It is only written once, so if you overwrite it by accident, it will only be reset when your bot restarts.

Returns
A pointer to the self information in the shared memory.

◆ log()

void Api::log ( const char *  msg)
inline

Send a log message.

These messages will appear on the web interface and in the World update stream when you provide your viewer key to the server.

Rate limiting is enforced by the gameserver, so messages are dropped when you send too many of them.

Parameters
msgPointer to the null-terminated message string.