1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef RYAI_AI_MANAGER_H
20 #define RYAI_AI_MANAGER_H
22 #define RYAI_AI_MANAGER_MAX_MANAGERS 1024
24 #include "nel/misc/types_nl.h"
25 #include "nel/misc/stream.h"
26 #include "nel/net/unified_network.h"
27 #include "ai_share/ai_actions.h"
31 ---------------------------------------------------------------------------
33 This class defines both the singleton that manages the managers and the
34 class type of the managers themselves. The singleton interface can be found
35 at the end of the class
37 ---------------------------------------------------------------------------
43 //===================================================================
44 // *** SUB-CLASSES FOR MGR DEFINITON DATA TREE ***
45 //===================================================================
51 SMgrDfnElm(uint64 action
,const std::vector
<CAIActions::CArg
> &args
): Action(action
)
53 Args
.resize(args
.size());
54 for (uint i
=0;i
<args
.size();++i
)
57 SMgrDfnElm(const SMgrDfnElm
&other
): Action(other
.Action
), Args(other
.Args
) {}
59 void serial(NLMISC::IStream
&f
)
61 //f.xmlPushBegin("CMD");
62 std::string s
=getAction();
63 //f.xmlSetAttrib("Action");
71 void serialToString(std::string
&s
) throw()
74 s
+=NLMISC::toString("%*s",sizeof(uint64
),"");
75 ((uint64
*)&(s
[s
.size()]))[-1]=Action
;
76 for (uint i
=0;i
<Args
.size();++i
)
77 Args
[i
].serialToString(s
);
80 std::string
getAction()
83 for (uint i
=0;i
<8 && ((char *)&Action
)[i
];++i
) s
+=((char *)&Action
)[i
];
87 void setAction(std::string action
)
90 for (uint i
=0;i
<8 && action
[i
];++i
) ((char *)&Action
)[i
]=action
[i
];
94 std::vector
<CAIActions::CArg
> Args
;
97 struct SMgrDfnNode
: public NLMISC::CRefCount
100 SMgrDfnNode(const std::string
&name
)
103 SMgrDfnNode(const SMgrDfnNode
&other
)
109 void serial(NLMISC::IStream
&f
)
119 for (uint i
=0; i
<count
; ++i
)
121 Child
[i
] = new SMgrDfnNode
;
127 for (uint i
=0; i
<count
; ++i
)
134 void serialToString(std::string
&s
) throw()
137 for (i
=0;i
<Data
.size();++i
)
138 Data
[i
].serialToString(s
);
139 for (i
=0;i
<Child
.size();++i
)
140 Child
[i
]->serialToString(s
);
144 std::vector
<SMgrDfnElm
> Data
;
145 std::vector
<NLMISC::CSmartPtr
<SMgrDfnNode
> > Child
;
147 bool Visited
; // used to identify modified nodes in file re-parse operations
151 //===================================================================
152 // *** START OF THE INSTANTIATED CLASS ***
153 //===================================================================
157 //---------------------------------------------------
158 // INSTANTIATED CLASS: Public methods
161 // a few read accessors (static properties)
163 // the manager id (0..255)
165 // the manager name .. ie the source file name minus extension
166 const std::string
&name() const;
168 // the CPU load rating of the manager for auto-load ballancing purposes
169 uint
weightCPU() const;
170 // the RAM load rating of the manager for auto-load ballancing purposes
171 uint
weightRAM() const;
175 // a few read accessors (state of the files on disk)
177 // indicates whether newer source files than object files have been located
178 bool needCompile() const;
179 // indicate whether an object file has been located in the object directory
180 bool objExists() const;
184 // a few read accessors (relating to assignment to & execution by an ai service)
186 // has the manager been opened (it may still be waiting to be assigned)
188 // has the manager been assigned to a service
189 bool isAssigned() const;
190 // is the manager up and running on the assigned service
193 // the id of the service to which the manager is assigned
194 NLNET::TServiceId
serviceId() const;
198 // a few basic actions (relating to disk files)
200 // compile the source files to generate new object files
202 // delete the object files (but not the save files)
207 // a few basic actions (relating to assignment to & execution by an ai service)
209 // open the manager on an unspecified service
210 // (may be queued until a service is available)
212 // assign manager to a specified service and begin execution
213 void assign(NLNET::TServiceId serviceId
);
214 // stop execution on the current service and assign to a new service
215 void reassign(NLNET::TServiceId serviceId
);
216 // stop execution of a manager
221 // a few basic actions (miscelaneous)
223 // display information about the state of the manager
224 void display() const;
228 // a few write accessors (miscelaneous)
230 // set the name assigned to manager
231 // if no name previously assigned then reset all manager properties
232 // if a name already exists and does not match new name then do nohing and return false
233 bool set(const std::string
&name
);
234 // set the state of the needCompile flag
235 void setNeedCompile(bool val
);
236 // set the state of the objFileExists flag
237 void setObjFileExists(bool val
);
238 // set the state of the isUp flag
239 void setIsUp(bool val
);
240 // set the state of the isOpen flag
241 void setIsOpen(bool val
);
245 //---------------------------------------------------
246 // INSTANTIATED CLASS: Private methods
248 // default constructor - may only be instantiated by the singleton
250 // reset properties to initial values
254 //---------------------------------------------------
255 // INSTANTIATED CLASS: Private data
257 // manager name (file name of source file minus extension)
259 // cpu rating for load ballancing
261 // ram rating for load ballancing
263 // do we need to recompile
265 // does an object file exist
267 // is the manager open (at least queued in CAIService singleton)
269 // is manager assigned to a service
271 // is manager up and running on the assigned service
273 // id of the service manager is assigned to
274 NLNET::TServiceId _service
;
278 //---------------------------------------------------
279 // INSTANTIATED CLASS: Public data
281 // the tree of definition data read from (and written to) data files
282 SMgrDfnNode MgrDfnRootNode
;
285 //===================================================================
286 // *** END OF THE INSTANTIATED CLASS *** START OF THE SINGLETON ***
287 //===================================================================
291 //---------------------------------------------------
292 // SINGLETON: Public methods
294 // get the number of valid handles (ie the maximum number of managers allowed)
295 static uint
maxManagers() { return RYAI_AI_MANAGER_MAX_MANAGERS
; }
297 // get the number of allocated managers
298 static uint
numManagers();
300 // get a pointer to the manager with given handle (0..maxManagers-1)
301 static CAIManager
*getManagerById(sint id
);
303 // get a pointer to the manager with given index (0..numManagers-1)
304 static CAIManager
*getManagerByIdx(uint idx
);
306 // get the handle for the manager of given name and optionally create a new
307 // handle if none found - return -1 if none found or no free slots
308 static sint
nameToId(std::string name
, bool assignNewIfNotFound
=false);
310 // clear file name assignments for managers that aren't currently running on
312 static void liberateUnassignedManagers();
315 //---------------------------------------------------
316 // SINGLETON: Private methods
320 //---------------------------------------------------
321 // SINGLETON: Private data
322 static CAIManager _managers
[RYAI_AI_MANAGER_MAX_MANAGERS
];
325 //===================================================================
326 // *** END OF THE SINGLETON ***
327 //===================================================================