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/>.
23 #include "child_container.h"
24 #include "server_share/msg_ai_service.h"
25 #include "nel/misc/random.h"
26 #include "nel/misc/variable.h"
27 #include "ai_entity_matrix.h"
28 #include "service_dependencies.h"
29 #include "game_share/task_list.h"
38 class CFamilyBehavior
;
40 extern bool IOSHasMirrorReady
;
41 extern bool EGSHasMirrorReady
;
43 extern const std::string disengageString
;
44 extern const std::string egsString
;
46 extern NLMISC::CVariable
<uint32
> TotalMaxPlayer
;
47 extern NLMISC::CVariable
<uint32
> TotalMaxPet
;
48 extern NLMISC::CVariable
<uint32
> TotalMaxFauna
;
49 extern NLMISC::CVariable
<uint32
> TotalMaxNpc
;
50 extern NLMISC::CVariable
<uint32
> TotalMaxFx
;
52 extern NLMISC::CVariable
<std::string
> BotRepopFx
;
55 template <class T
> class CAIEntityMatrix
;
56 class CAIEntityMatrixIteratorTblRandom
;
57 class CAIEntityMatrixIteratorTblLinear
;
61 class CAIEntityPhysical
;
66 class CPersistentOfPhysical
;
69 int getInt64FromStr (const char* str
);
72 -----------------------------------------------------------------------------
74 AI is the singleton manager class for the high speed ai system
76 -----------------------------------------------------------------------------
82 class CWarnBadInstanceMsgImp
: public CWarnBadInstanceMsg
85 void callback(const std::string
&name
, NLNET::TServiceId id
);
89 class CAIS
: public CServiceEvent::CHandler
94 static CAIS
&instance();
96 static bool instanceCreated() { return _Instance
!= NULL
; }
98 static std::string
getIndexString() { return NLMISC::toString("AIS_%u", NLNET::IService::getInstance()->getServiceId().get()); }
100 //-------------------------------------------------------------------
103 //-------------------------------------------------------------------
104 // classic init(), update() and release()
106 /** create an AI instance, return the instance index in the AIList
107 * Return std::numeric_limits<uint32>::max() if the instance number is already in use.
109 uint32
createAIInstance(const std::string
&continentName
, uint32 instanceNumber
);
110 /** destroy an AI Instance (useful for ring creation / destruction of session)
111 @param instanceNumber is the AiInstance Id
112 @param displayWarningIfInstanceNotExist If false nothing happends when the specified instance do not exist
114 void destroyAIInstance(uint32 instanceNumber
, bool displayWarningIfInstanceNotExist
);
116 // the update routine called once per tick
117 // this is the routine that calls the managers' updates
120 // This routine is called once every 1000 ticks
121 // updates the persistent ai script variables values on the Backup Service
122 void updatePersistentVariables();
124 // release the singleton before program exit
127 void serviceEvent (const CServiceEvent
&info
);
129 // Management of deleted root alias
130 bool markTagForDelete(const std::string
&filename
);
131 void deleteTaggedAlias(const std::string
&filename
);
133 //-------------------------------------------------------------------
134 // dealing with backups (save and restore)
136 // provoke a general 'save to backup' across the whole service
139 inline void setClientCreatureDebug (bool clientCreatureDebug
)
141 _ClientCreatureDebug
=clientCreatureDebug
;
144 inline bool clientCreatureDebug ()
146 return _ClientCreatureDebug
;
151 //-------------------------------------------------------------------
152 // Interface to bot chat - callbacks called when bots start or
153 // stop chatting with player(s)
154 void beginBotChat (const TDataSetRow
&bot
, const TDataSetRow
&player
);
155 void endBotChat (const TDataSetRow
&bot
, const TDataSetRow
&player
);
156 void beginDynChat (const TDataSetRow
&bot
);
157 void endDynChat (const TDataSetRow
&bot
);
159 //-------------------------------------------------------------------
160 // manageing the set of managers
162 // a method that parse a supposed know type of manager:group:bot hierarchy and return the element as CAIEntity.
163 CAIEntityPhysical
*tryToGetEntityPhysical (const char *str
);
164 CAIInstance
*tryToGetAIInstance (const char *str
);
166 // dynamic system branch
167 CContinent
*tryToGetContinent (const char *str
);
168 CRegion
*tryToGetRegion (const char *str
);
169 CCellZone
*tryToGetCellZone (const char *str
);
170 CFamilyBehavior
*tryToGetFamilyBehavior (const char *str
);
173 CManager
*tryToGetManager (const char *str
);
174 CGroup
*tryToGetGroup (const char *str
);
175 CBot
*tryToGetBot (const char *str
);
176 CAIEntity
*tryToGetAIEntity (const char *str
);
178 //-------------------------------------------------------------------
179 // the previous interfaces for searching the data structures for named objects are transfered in CAIEntityId
180 // as its one of their object behavior. a solution to build id directly was added.
182 // to update the map.
183 friend class CAIEntityPhysical
;
184 CAIEntityPhysical
*getEntityPhysical(const TDataSetRow
&);
186 //-------------------------------------------------------------------
187 // Interface to the vision management matrices
189 // read accessors for getting hold of the vision matrices and their associated iterator tables
190 inline const CAIEntityMatrixIteratorTblRandom
*matrixIterator2x2();
191 inline const CAIEntityMatrixIteratorTblRandom
*matrixIterator3x3();
192 const CAIEntityMatrixIteratorTblLinear
*bestLinearMatrixIteratorTbl(uint32 distInMeters
);
194 //-------------------------------------------------------------------
195 // Interface to the random number generator
196 static inline sint32
randPlusMinus(uint16 mod
);
197 static inline float frand(double mod
=1.0);
198 static inline float frandPlusMinus(double mod
);
199 static inline uint32
rand32();
200 // WARNING : this rand has a 'coherent' behavior : ie it return value between 0 and mod-1
201 static inline uint32
rand32(uint32 mod
);
202 // WARNING : this rand has a 'coherent' behavior : ie it return value between 0 and mod-1
203 static inline uint32
rand16(uint32 mod
);
205 /// Time warp managment. This method is called when time as warped more than 600ms
206 bool advanceUserTimer(uint32 nbTicks
);
208 /// Retreive emot number given it's name, return std::numeric_limits<uint32>::max() if not found
209 uint32
getEmotNumber(const std::string
&name
);
211 CCont
<CAIInstance
> &AIList () { return _AIInstances
; }
212 CCont
<CAIInstance
> &aiinstances() { return _AIInstances
; } ///< This is a synonym for AIList, but should replace it as it's more coherent with general 'nomenclature'
214 CAIInstance
*getAIInstance(uint32 instanceNumber
);
216 CFaunaBotDescription
&getFaunaDescription()
218 return _FaunaDescriptionList
;
220 CChangeCreatureHPMsg
&getCreatureChangeHP()
222 return _CreatureChangeHPList
;
225 CChangeCreatureMaxHPMsg
&getCreatureChangeMaxHP()
227 return _CreatureChangeMaxHPList
;
246 CCounter(const uint32 max
=std::numeric_limits
<uint32
>::max()):_Total(0),_Max(max
)
250 void setMax(const uint32 max
) { _Max
=max
; }
251 void inc() { _Total
++; }
252 void dec() { _Total
--; }
253 uint32
getTotal() const { return _Total
; }
254 bool remainToMax (uint32 nbMore
=1) const { return (_Total
+nbMore
)<_Max
; }
261 CCounter _PetBotCounter
;
262 CCounter _NpcBotCounter
;
263 CCounter _FaunaBotCounter
;
265 // message from EGS about bad aiinstance
266 void warnBadInstanceMsgImp(const std::string
&serviceName
, NLNET::TServiceId serviceId
, CWarnBadInstanceMsgImp
&msg
);
268 void addTickedTask(uint32 tick
, CTask
<uint32
>* task
) { _TickedTaskList
.addTaskAt(tick
, task
); }
273 // private constructor
276 // initialise the singleton
279 /// Singleton instance
280 static CAIS
*_Instance
;
282 CAIEntity
* tryToGetEntity (const char* str
, TSearchType searchType
=AI_UNDEFINED
);
283 // Global map of entity in this shard
284 CHashMap
<int,NLMISC::CDbgPtr
<CAIEntityPhysical
> > _CAIEntityByDataSetRow
;
286 // the random number generator
287 static NLMISC::CRandom _random
;
289 CAIEntityMatrixIteratorTblRandom _matrixIterator2x2
;
290 CAIEntityMatrixIteratorTblRandom _matrixIterator3x3
;
291 std::vector
<CAIEntityMatrixIteratorTblLinear
*> _matrixIteratorsByDistance
;
293 // Faunas descriptions to be sent each frame
294 CFaunaBotDescription _FaunaDescriptionList
;
295 CChangeCreatureHPMsg _CreatureChangeHPList
;
296 CChangeCreatureMaxHPMsg _CreatureChangeMaxHPList
;
298 /// The emot identifiers
299 std::map
<std::string
, uint32
> _EmotNames
;
300 CCont
<CAIInstance
> _AIInstances
;
302 uint32 _TotalBotsSpawned
;
303 bool _ClientCreatureDebug
;
304 CTaskList
<uint32
> _TickedTaskList
;
309 #include "ai_inline.h"