SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
ai::Server Class Reference

The server can serialize the state of the AI and broadcast it to all connected clients. More...

#include <Server.h>

Inheritance diagram for ai::Server:
[legend]
Collaboration diagram for ai::Server:
[legend]

Classes

struct  Event
 

Public Member Functions

 Server (AIRegistry &aiRegistry, short port=10001, const std::string &hostname="0.0.0.0")
 
bool start ()
 Start to listen on the specified port.
 
bool updateNode (const CharacterId &characterId, int32_t nodeId, const std::string &name, const std::string &type, const std::string &condition)
 Update the specified node with the given values for the specified ICharacter and all the other characters that are using the same behaviour tree instance. More...
 
bool addNode (const CharacterId &characterId, int32_t parentNodeId, const std::string &name, const std::string &type, const std::string &condition)
 Add a new node with the given values to the specified ICharacter and all the other characters that are using the same behaviour tree instance. More...
 
bool deleteNode (const CharacterId &characterId, int32_t nodeId)
 Delete the specified node from the ICharacter's behaviour tree and all the other characters that are using the same behaviour tree instance. More...
 
void addZone (Zone *zone)
 Adds a new zone to this server instance that can be debugged. The server does not own this pointer so it also doesn't free it. Every Zone that is added here, will be part of the AINamesMessage. More...
 
void removeZone (Zone *zone)
 Removes a Zone from the server. After this call the given zone is no longer available for debugging purposes. More...
 
void setDebug (const std::string &zoneName)
 Activate the debugging for this particular zone. And disables the debugging for every other zone. More...
 
void reset ()
 Resets the AI states.
 
void select (const ClientId &clientId, const CharacterId &id)
 Select a particular character (resp. AI instance) and send detail information to all the connected clients for this entity.
 
void pause (const ClientId &clientId, bool pause)
 Will pause/unpause the execution of the behaviour trees for all watched AI instances.
 
void step (int64_t stepMillis=1L)
 Performs one step of the AI in pause mode.
 
void update (int64_t deltaTime)
 call this to update the server - should get called somewhere from your game tick
 

Protected Types

enum  EventType {
  EV_SELECTION, EV_STEP, EV_UPDATESTATICCHRDETAILS, EV_NEWCONNECTION,
  EV_ZONEADD, EV_ZONEREMOVE, EV_PAUSE, EV_RESET,
  EV_SETDEBUG, EV_MAX
}
 
typedef std::unordered_set
< Zone * > 
Zones
 
typedef Zones::const_iterator ZoneConstIter
 
typedef Zones::iterator ZoneIter
 

Protected Member Functions

void resetSelection ()
 
void addChildren (const TreeNodePtr &node, std::vector< AIStateNodeStatic > &out) const
 
void addChildren (const TreeNodePtr &node, AIStateNode &parent, const AIPtr &ai) const
 
void broadcastState (const Zone *zone)
 
void broadcastCharacterDetails (const Zone *zone)
 
void broadcastStaticCharacterDetails (const Zone *zone)
 
void onConnect (Client *client) override
 
void onDisconnect (Client *client) override
 
void handleEvents (Zone *zone, bool pauseState)
 
void enqueueEvent (const Event &event)
 

Protected Attributes

Zones _zones
 
AIRegistry_aiRegistry
 
Network _network
 
CharacterId _selectedCharacterId
 
int64_t _time
 
SelectHandler_selectHandler
 
PauseHandler_pauseHandler
 
ResetHandler_resetHandler
 
StepHandler_stepHandler
 
ChangeHandler_changeHandler
 
AddNodeHandler_addNodeHandler
 
DeleteNodeHandler_deleteNodeHandler
 
UpdateNodeHandler_updateNodeHandler
 
NopHandler _nopHandler
 
std::atomic_bool _pause
 
std::atomic< Zone * > _zone
 
ReadWriteLock _lock = {"server"}
 
std::vector< std::string > _names
 
uint32_t _broadcastMask = 0u
 
std::vector< Event_events
 

Detailed Description

The server can serialize the state of the AI and broadcast it to all connected clients.

If you start a server, you can add the AI instances to it by calling addZone(). If you do so, make sure to remove it when you remove that particular Zone instance from your world. You should not do that from different threads. The server should only be managed from one thread.

The server will broadcast the world state - that is: It will send out an AIStateMessage to all connected clients. If someone selected a particular AI instance by sending AISelectMessage to the server, it will also broadcast an AICharacterDetailsMessage to all connected clients.

You can only debug one Zone at the same time. The debugging session is shared between all connected clients.

Member Function Documentation

bool ai::Server::addNode ( const CharacterId &  characterId,
int32_t  parentNodeId,
const std::string &  name,
const std::string &  type,
const std::string &  condition 
)
inline

Add a new node with the given values to the specified ICharacter and all the other characters that are using the same behaviour tree instance.

Parameters
[in]characterIdThe id of the ICharacter where we want to add the specified node
[in]parentNodeIdThe id of the TreeNode to attach the new TreeNode as children
[in]nameThe new name for the node
[in]typeThe new node type (including parameters)
[in]conditionThe new condition (including parameters)
See Also
TreeNodeParser
ConditionParser
void ai::Server::addZone ( Zone *  zone)
inline

Adds a new zone to this server instance that can be debugged. The server does not own this pointer so it also doesn't free it. Every Zone that is added here, will be part of the AINamesMessage.

Parameters
zoneThe Zone that should be made available for debugging.
Note
This locks the server instance
bool ai::Server::deleteNode ( const CharacterId &  characterId,
int32_t  nodeId 
)
inline

Delete the specified node from the ICharacter's behaviour tree and all the other characters that are using the same behaviour tree instance.

Parameters
[in]characterIdThe id of the ICharacter where we want to delete the specified node
[in]nodeIdThe id of the TreeNode to delete
void ai::Server::removeZone ( Zone *  zone)
inline

Removes a Zone from the server. After this call the given zone is no longer available for debugging purposes.

Note
This locks the server instance
void ai::Server::setDebug ( const std::string &  zoneName)
inline

Activate the debugging for this particular zone. And disables the debugging for every other zone.

Note
This locks the server instance
bool ai::Server::updateNode ( const CharacterId &  characterId,
int32_t  nodeId,
const std::string &  name,
const std::string &  type,
const std::string &  condition 
)
inline

Update the specified node with the given values for the specified ICharacter and all the other characters that are using the same behaviour tree instance.

Parameters
[in]characterIdThe id of the character where we want to update the specified node
[in]nodeIdThe id of the TreeNode to update with the new values
[in]nameThe new name for the node
[in]typeThe new node type (including parameters)
[in]conditionThe new condition (including parameters)
See Also
TreeNodeParser
ConditionParser

The documentation for this class was generated from the following files: