SPN C++ Bot Framework
Loading...
Searching...
No Matches
bot_api.h
1#pragma once
2
3#include <string.h>
4
5#include "ipc_format.h"
6
19class Api
20{
21 public:
22 /*** variables ***/
23 ipc_real_t angle;
24 bool boost;
25
26 /*** functions ***/
36 : angle(0)
37 , boost(false)
38 , m_shm(shm)
39 {}
40
52 const IpcServerConfig* getServerConfig(void) { return &(m_shm->serverConfig); }
53
62 const IpcSelfInfo* getSelfInfo(void) { return &(m_shm->selfInfo); }
63
73 const IpcFoodInfo* getFood(void) { return m_shm->foodInfo; }
74
80 size_t getFoodCount(void) { return m_shm->foodCount; }
81
91 const IpcSegmentInfo* getSegments(void) { return m_shm->segmentInfo; }
92
98 size_t getSegmentCount(void) { return m_shm->segmentCount; }
99
107 const IpcBotInfo* getBots(void) { return m_shm->botInfo; }
108
114 size_t getBotCount(void) { return m_shm->botCount; }
115
122 void clearColors(void) { m_shm->colorCount = 0; }
123
137 bool addColor(uint8_t r, uint8_t g, uint8_t b)
138 {
139 if(m_shm->colorCount >= IPC_COLOR_MAX_COUNT) {
140 return false;
141 }
142
143 m_shm->colors[m_shm->colorCount].r = r;
144 m_shm->colors[m_shm->colorCount].g = g;
145 m_shm->colors[m_shm->colorCount].b = b;
146 m_shm->colorCount++;
147
148 return true;
149 }
150
164 {
165 return m_shm->persistentData;
166 }
167
179 void log(const char *msg)
180 {
181 size_t len = strlen(m_shm->logData);
182
183 if(len >= IPC_COLOR_MAX_COUNT-2) {
184 return;
185 }
186
187 size_t n = IPC_LOG_MAX_BYTES - len - 1;
188
189 if(len != 0) {
190 m_shm->logData[len] = '\n';
191 m_shm->logData[len+1] = '\0';
192 }
193
194 strncat(m_shm->logData, msg, n);
195 }
196
197 private:
198 IpcSharedMemory *m_shm;
199};
A class providing a simplified API for the Bots.
Definition: bot_api.h:20
const IpcSegmentInfo * getSegments(void)
Get a pointer to the Segment list.
Definition: bot_api.h:91
const IpcBotInfo * getBots(void)
Get a pointer to the Bot list.
Definition: bot_api.h:107
const IpcFoodInfo * getFood(void)
Get a pointer to the Food list.
Definition: bot_api.h:73
size_t getFoodCount(void)
Get the length of the Food list.
Definition: bot_api.h:80
size_t getBotCount(void)
Get the length of the Bot list.
Definition: bot_api.h:114
Api(IpcSharedMemory *shm)
Constructor for the API interface object.
Definition: bot_api.h:35
bool addColor(uint8_t r, uint8_t g, uint8_t b)
Add a color to the color list.
Definition: bot_api.h:137
const IpcSelfInfo * getSelfInfo(void)
Get a pointer to the self information.
Definition: bot_api.h:62
void log(const char *msg)
Send a log message.
Definition: bot_api.h:179
void clearColors(void)
Clear the color list.
Definition: bot_api.h:122
ipc_real_t angle
The direction into which the bot should move in the current frame.
Definition: bot_api.h:23
void * getPersistentMemory(void)
Get a pointer to the persistent memory.
Definition: bot_api.h:163
const IpcServerConfig * getServerConfig(void)
Get a pointer to the server config data.
Definition: bot_api.h:52
size_t getSegmentCount(void)
Get the length of the Segment list.
Definition: bot_api.h:98
bool boost
Set this to true to boost in this frame.
Definition: bot_api.h:24
IPC representation of a bot.
Definition: ipc_format.h:79
uint8_t g
Green channel (0-255)
Definition: ipc_format.h:103
uint8_t b
Blue channel (0-255)
Definition: ipc_format.h:104
uint8_t r
Red channel (0-255)
Definition: ipc_format.h:102
IPC representation of a food particle.
Definition: ipc_format.h:68
IPC representation of a snake segment.
Definition: ipc_format.h:87
IPC representation of one's own snake and current world parameters.
Definition: ipc_format.h:17
IPC representation of the static server and world configuration.
Definition: ipc_format.h:40
Shared memory structure.
Definition: ipc_format.h:129
struct IpcSelfInfo selfInfo
Information about your snake (updated every frame).
Definition: ipc_format.h:132
struct IpcBotInfo botInfo[IPC_BOT_MAX_COUNT]
List of bots related to segments in segmentInfo.
Definition: ipc_format.h:138
uint8_t persistentData[IPC_PERSISTENT_MAX_BYTES]
Persistent data: will be saved after your snake dies and restored when it respawns.
Definition: ipc_format.h:151
struct IpcSegmentInfo segmentInfo[IPC_SEGMENT_MAX_COUNT]
List of segments seen by the snake.
Definition: ipc_format.h:141
uint32_t segmentCount
Number of items used in segmentInfo.
Definition: ipc_format.h:140
struct IpcColor colors[IPC_COLOR_MAX_COUNT]
Colors to set for this snake.
Definition: ipc_format.h:144
struct IpcFoodInfo foodInfo[IPC_FOOD_MAX_COUNT]
List of food items seen by the snake.
Definition: ipc_format.h:135
uint32_t botCount
Number of items used in botInfo.
Definition: ipc_format.h:137
uint32_t colorCount
Number of items used in colors.
Definition: ipc_format.h:143
char logData[IPC_LOG_MAX_BYTES]
Log data for the current frame.
Definition: ipc_format.h:146
uint32_t foodCount
Number of items used in foodInfo.
Definition: ipc_format.h:134
struct IpcServerConfig serverConfig
Information about the world and server configuration.
Definition: ipc_format.h:130