SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Server.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "common/Thread.h"
7 #include "tree/TreeNode.h"
8 #include <unordered_set>
9 #include "Network.h"
10 #include "zone/Zone.h"
11 #include "AIRegistry.h"
12 #include "AIStateMessage.h"
13 #include "AINamesMessage.h"
14 #include "AIStubTypes.h"
19 #include "tree/TreeNodeParser.h"
20 #include "tree/TreeNodeImpl.h"
21 
22 namespace ai {
23 
24 class AIStateNode;
25 class AIStateNodeStatic;
26 
27 class SelectHandler;
28 class PauseHandler;
29 class ResetHandler;
30 class StepHandler;
31 class ChangeHandler;
32 class AddNodeHandler;
33 class DeleteNodeHandler;
34 class UpdateNodeHandler;
35 class NopHandler;
36 
50 class Server: public INetworkListener {
51 protected:
52  typedef std::unordered_set<Zone*> Zones;
53  typedef Zones::const_iterator ZoneConstIter;
54  typedef Zones::iterator ZoneIter;
55  Zones _zones;
56  AIRegistry& _aiRegistry;
57  Network _network;
58  CharacterId _selectedCharacterId;
59  int64_t _time;
60  SelectHandler *_selectHandler;
61  PauseHandler *_pauseHandler;
62  ResetHandler *_resetHandler;
63  StepHandler *_stepHandler;
64  ChangeHandler *_changeHandler;
65  AddNodeHandler *_addNodeHandler;
66  DeleteNodeHandler *_deleteNodeHandler;
67  UpdateNodeHandler *_updateNodeHandler;
68  NopHandler _nopHandler;
69  std::atomic_bool _pause;
70  // the current active debugging zone
71  std::atomic<Zone*> _zone;
72  ReadWriteLock _lock = {"server"};
73  std::vector<std::string> _names;
74  uint32_t _broadcastMask = 0u;
75 
76  enum EventType {
77  EV_SELECTION,
78  EV_STEP,
79  EV_UPDATESTATICCHRDETAILS,
80  EV_NEWCONNECTION,
81  EV_ZONEADD,
82  EV_ZONEREMOVE,
83  EV_PAUSE,
84  EV_RESET,
85  EV_SETDEBUG,
86 
87  EV_MAX
88  };
89 
90  struct Event {
91  union {
92  CharacterId characterId;
93  int64_t stepMillis;
94  Zone* zone;
95  Client* newClient;
96  bool pauseState;
97  } data;
98  std::string strData = "";
99  EventType type;
100  };
101  std::vector<Event> _events;
102 
103  void resetSelection();
104 
105  void addChildren(const TreeNodePtr& node, std::vector<AIStateNodeStatic>& out) const;
106  void addChildren(const TreeNodePtr& node, AIStateNode& parent, const AIPtr& ai) const;
107 
108  // only call these from the Server::update method
109  void broadcastState(const Zone* zone);
110  void broadcastCharacterDetails(const Zone* zone);
111  void broadcastStaticCharacterDetails(const Zone* zone);
112 
113  void onConnect(Client* client) override;
114  void onDisconnect(Client* client) override;
115 
116  void handleEvents(Zone* zone, bool pauseState);
117  void enqueueEvent(const Event& event);
118 public:
119  Server(AIRegistry& aiRegistry, short port = 10001, const std::string& hostname = "0.0.0.0");
120  virtual ~Server();
121 
125  bool start();
126 
140  bool updateNode(const CharacterId& characterId, int32_t nodeId, const std::string& name, const std::string& type, const std::string& condition);
141 
155  bool addNode(const CharacterId& characterId, int32_t parentNodeId, const std::string& name, const std::string& type, const std::string& condition);
156 
164  bool deleteNode(const CharacterId& characterId, int32_t nodeId);
165 
174  void addZone(Zone* zone);
175 
182  void removeZone(Zone* zone);
183 
189  void setDebug(const std::string& zoneName);
190 
194  void reset();
195 
200  void select(const ClientId& clientId, const CharacterId& id);
201 
205  void pause(const ClientId& clientId, bool pause);
206 
210  void step(int64_t stepMillis = 1L);
211 
215  void update(int64_t deltaTime);
216 };
217 
218 }
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 ...
Definition: ServerImpl.h:400
Definition: SelectHandler.h:12
Definition: Network.h:35
bool start()
Start to listen on the specified port.
Definition: ServerImpl.h:90
The place to register your TreeNode and ICondition factories at.
Definition: AIRegistry.h:64
void removeZone(Zone *zone)
Removes a Zone from the server. After this call the given zone is no longer available for debugging p...
Definition: ServerImpl.h:432
Definition: UpdateNodeHandler.h:14
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 ar...
Definition: ServerImpl.h:363
Definition: StepHandler.h:14
void select(const ClientId &clientId, const CharacterId &id)
Select a particular character (resp. AI instance) and send detail information to all the connected cl...
Definition: ServerImpl.h:457
Definition: ChangeHandler.h:14
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 charac...
Definition: ServerImpl.h:317
Definition: Network.h:43
Definition: IProtocolHandler.h:42
Definition: Server.h:90
Definition: AddNodeHandler.h:13
Definition: Network.h:25
void reset()
Resets the AI states.
Definition: ServerImpl.h:446
Definition: PauseHandler.h:14
void pause(const ClientId &clientId, bool pause)
Will pause/unpause the execution of the behaviour trees for all watched AI instances.
Definition: ServerImpl.h:464
Definition: DeleteNodeHandler.h:14
Definition: ResetHandler.h:11
This is a representation of a behaviour tree node for the serialization.
Definition: AIStubTypes.h:106
void setDebug(const std::string &zoneName)
Activate the debugging for this particular zone. And disables the debugging for every other zone...
Definition: ServerImpl.h:439
void addZone(Zone *zone)
Adds a new zone to this server instance that can be debugged. The server does not own this pointer so...
Definition: ServerImpl.h:425
Definition: Thread.h:14
The server can serialize the state of the AI and broadcast it to all connected clients.
Definition: Server.h:50
void update(int64_t deltaTime)
call this to update the server - should get called somewhere from your game tick
Definition: ServerImpl.h:478
void step(int64_t stepMillis=1L)
Performs one step of the AI in pause mode.
Definition: ServerImpl.h:471