Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / server / src / ai_service / ai_instance.h
blobb2a71cd3f8149971d8b04da1e4c0f05094e581da
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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 //#include "ai_entity_id.h" // this must be BEFORE the RYAI_AI_H test
21 #ifndef RYAI_AI_INSTANCE_H
22 #define RYAI_AI_INSTANCE_H
24 #include "child_container.h"
25 #include "server_share/msg_ai_service.h"
26 #include "ai_entity_matrix.h"
27 #include "ai_entity.h"
28 #include "continent.h"
29 #include "ai_grp.h"
30 #include "service_dependencies.h"
32 #include "sheets.h"
34 class CBot;
35 class CGroup;
36 class CManager;
38 template <class T>
39 class CAIEntityMatrix;
40 class CAIEntityMatrixIteratorTblRandom;
41 class CAIEntityMatrixIteratorTblLinear;
43 class CAIEntityPhysical;
44 class CAIEntity;
45 class CMgrPet;
46 class CManagerPlayer;
47 class CGroupNpc;
48 class CPersistentOfPhysical;
49 class CManagerPlayer;
50 class CAIS;
51 class CContinent;
52 class CCell;
53 class COutpostSquadFamily;
54 class CBotPlayer;
55 class CBotEasterEgg;
57 class CAIInstance
58 : public NLMISC::CDbgRefCount<CAIInstance>
59 , public NLMISC::CRefCount
60 , public CChild<CAIS>
61 , public CAIEntity
62 , public IManagerParent
63 , public CServiceEvent::CHandler
65 public:
66 CAIInstance(CAIS* owner);
67 virtual ~CAIInstance();
69 typedef CHashMap<NLMISC::TStringId, NLMISC::CDbgPtr<CNpcZone>, NLMISC::CStringIdHashMapTraits> TZoneList;
70 TZoneList zoneList;
71 void addZone(std::string const& zoneName, CNpcZone* zone);
72 void removeZone(std::string const& zoneName, CNpcZone* zone);
73 CNpcZone* getZone(NLMISC::TStringId zoneName);
74 // Trig Event if player in zone
75 void updateZoneTrigger(CBotPlayer* player);
77 // overloads for IManagerParent virtuals
78 CAIInstance* getAIInstance() const { return const_cast<CAIInstance*>(this); }
79 CCellZone* getCellZone() { return NULL; }
80 virtual std::string getIndexString() const;
81 virtual std::string getOneLineInfoString() const;
82 virtual std::vector<std::string> getMultiLineInfoString() const;
84 std::string getManagerIndexString(CManager const* manager) const;
86 void groupDead(CGroup* grp) { }
88 void serviceEvent(CServiceEvent const& info);
90 //-------------------------------------------------------------------
91 // classic init(), update() and release()
93 // the update routine called once per tick
94 // this is the routine that calls the managers' updates
95 void update();
97 //-------------------------------------------------------------------
98 // managing the set of managers
100 // factory for creating new managers and for adding them to the _managers map
101 CManager* newMgr(AITYPES::TMgrType type, uint32 alias, std::string const& name, std::string const& mapName, std::string const& filename);
103 // a method that parse a supposed know type of manager:group:bot hierarchy and return the element as CAIEntity.
104 CManager* tryToGetManager(char const* str);
105 CGroup* tryToGetGroup(char const* str);
107 CAIEntity* tryToGetEntity(char const* str, CAIS::TSearchType searchType);
109 // erase a manager (free resources, free id, etc, etc
110 // asserts if the id is invalid (<0 or >1023)
111 // displays a warning and returns cleanly if the id is unused
112 void deleteMgr(sint mgrId);
114 //-------------------------------------------------------------------
115 // the previous interfaces for searching the data structures for named objects are transfered in CAIEntityId
116 // as its one of their object behavior. a solution to build id directly was added.
117 CMgrPet* getPetMgr() { return _PetManager; }
118 CManagerPlayer* getPlayerMgr() { return _PlayerManager; }
120 //-------------------------------------------------------------------
121 // Interface to kami management
122 void registerKamiDeposit(uint32 alias, CGroupNpc* grp);
123 void unregisterKamiDeposit(uint32 alias);
125 //-------------------------------------------------------------------
126 // Interface to the vision management matrices
128 // read accessors for getting hold of the vision matrices and their associated iterator tables
129 CAIEntityMatrix<CPersistentOfPhysical>& playerMatrix() { return _PlayerMatrix; }
130 CAIEntityMatrix<CPersistentOfPhysical>& botMatrix() { return _BotMatrix; }
132 CAliasCont<CManager>& managers() { return _Managers; }
134 CCont<CContinent>& continents() { return _Continents; }
135 CCont<CContinent> const& continents() const { return _Continents; }
137 // Methods to retreive location in the dynamic system.
138 CContinent* locateContinentForPos(CAIVector const& pos);
139 CRegion* locateRegionForPos(CAIVector const& pos);
140 CCellZone* locateCellZoneForPos(CAIVector const& pos);
141 CCell* locateCellForPos(CAIVector const& pos);
144 //-------------------------------------------------------------------
145 // Mission name/alias retreiver
146 /** Add a mission name and alias info. If alias is already mapped to a mission name, replace the mapping.
147 * Many alias can be mapped to the same name, but a name can only belong to one alias.
149 void addMissionInfo(std::string const& missionName, uint32 alias);
150 /** Return all mission alias that have a given mission name.
151 * n log n search
153 void findMissionAlias(std::vector<uint32>& result, std::string const& missionName)
155 std::map<std::string, std::vector<uint32> >::iterator it(_MissionToAlias.find(missionName));
156 if (it != _MissionToAlias.end())
157 result.insert(result.end(), it->second.begin(), it->second.end());
159 /** Return the name of the mission for a given alias.
160 * Return an empty string if no mission have this alias.
161 * This search is not optimized, linar time search !
163 std::string const& findMissionName(uint32 alias);
165 //-------------------------------------------------------------------
166 // group name/alias retreiver
167 void addGroupInfo(CGroup* grp);
168 void addGroupInfo(CGroup* grp, const std::string &name, uint32 alias);
169 void removeGroupInfo(CGroup* grp, CAliasTreeOwner* grpAliasTreeOwner);
170 CGroup* findGroup(uint32 alias);
171 void findGroup(std::vector<CGroup*>& result, std::string const& name);
173 /// Time warp management. This method is called when time as warped more than 600ms
174 bool advanceUserTimer(uint32 nbTicks);
176 bool spawn();
177 bool despawn();
179 uint32 getInstanceNumber() const { return _InstanceNumber; }
180 std::string const& getContinentName() const { return _ContinentName; }
181 std::string& getContinentName() { return _ContinentName; }
183 void initInstance(std::string const& continentName, uint32 instanceNumber);
185 /// Main squad family accessor
186 COutpostSquadFamily *getSquadFamily() { return _SquadFamily; }
188 /// Store mapping 'name:variant' -> squad to be used later by getSquadByVariantName()
189 void registerSquadVariant(const std::string& nameAndVariant, CGroupDesc<COutpostSquadFamily> *squad )
191 if ( ! _SquadVariantNameToGroupDesc.insert( std::make_pair( NLMISC::toLower( nameAndVariant ), squad ) ).second )
192 nlwarning( "Duplicate squad template / squad variant '%s'", nameAndVariant.c_str() );
195 /// Clear mapping 'name:variant' -> squad when it's not useful anymore
196 void clearSquadVariantNames()
198 _SquadVariantNameToGroupDesc.clear();
201 /// Get a squad by name:variant (works only during primitive parsing), or NULL if not found. Not case-sensitive.
202 CGroupDesc<COutpostSquadFamily> *getSquadByVariantName(const std::string& nameAndVariant)
204 std::map<std::string, NLMISC::CSmartPtr< CGroupDesc<COutpostSquadFamily> > >::iterator it = _SquadVariantNameToGroupDesc.find( NLMISC::toLower( nameAndVariant ) );
205 if ( it != _SquadVariantNameToGroupDesc.end() )
206 return (*it).second;
207 else
208 return NULL;
211 CGroupNpc* eventCreateNpcGroup(uint nbBots, NLMISC::CSheetId const& sheetId, CAIVector const& pos, double dispersionRadius, bool spawnBots, double orientation, const std::string &botsName, const std::string &look, sint32 cell=0);
212 bool eventCreateNpcBot(CGroupNpc* grp, uint nbBots, bool spawnBots, NLMISC::CSheetId const& sheetId, CAIVector const& pos, const std::string &name, double orientation, double dispersionRadius, const std::string &look);
214 /// create a new easter egg
215 CBotEasterEgg* createEasterEgg(uint32 easterEggId, NLMISC::CSheetId const& sheetId, std::string const& botName, double x, double y, double z, double heading, const std::string& look);
216 /// destroy an easter egg
217 void destroyEasterEgg(uint32 easterEggId);
218 /// get an easter egg by ID
219 CBotEasterEgg* getEasterEgg(uint32 easterEggId);
221 private:
222 void sendInstanceInfoToEGS();
224 private:
225 /// @name AI service hierarchy
226 //@{
227 /// The set of continents
228 CCont<CContinent> _Continents;
229 /// the set of managers and the service's root alias description tree node
230 CAliasCont<CManager> _Managers;
231 //@}
233 static NLLIGO::CLigoConfig _LigoConfig;
235 /// The ai instance continent name (multi ai system)
236 std::string _ContinentName;
237 /// The ai instance number (multi ai system)
238 uint32 _InstanceNumber;
240 CAIEntity* tryToGetEntity(char const* str);
242 // we must share pets and players .. later :)
243 /// pet manager
244 CMgrPet* _PetManager;
245 /// player Manager.
246 CManagerPlayer* _PlayerManager;
247 /// event npc Manager.
248 CMgrNpc* _EventNpcManager;
250 uint32 _LastGroupAlias;
251 uint32 _LastSpawnAlias;
252 uint32 _LastStateAlias;
254 /// easter egg manager
255 NLMISC::CRefPtr<CMgrNpc> _EasterEggManager;
257 /// easter egg group
258 NLMISC::CRefPtr<CGroupNpc> _EasterEggGroup;
260 /// map of kami groups by alias (for kami groups associated with deposits)
261 std::map<uint, CGroupNpc*> _KamiDeposits;
263 /// matrices used for vision generation and their associated iterator tables
264 CAIEntityMatrix<CPersistentOfPhysical> _PlayerMatrix;
265 CAIEntityMatrix<CPersistentOfPhysical> _BotMatrix;
267 // Mission name to alias container.
268 std::map<std::string, std::vector<uint32> > _MissionToAlias;
270 /// Group name and alias container.
271 std::map<std::string, std::vector<NLMISC::CDbgPtr<CGroup> > > _GroupFromNames;
272 std::map<uint32, NLMISC::CDbgPtr<CGroup> > _GroupFromAlias;
274 // Container for all the squad templates
275 NLMISC::CSmartPtr<COutpostSquadFamily> _SquadFamily;
277 // Squad look-up (only valid during primitive parsing)
278 std::map< std::string, NLMISC::CSmartPtr< CGroupDesc<COutpostSquadFamily> > > _SquadVariantNameToGroupDesc;
281 #endif