Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / client / src / entities.h
blobd7a0c34c5c0691d3d92cb59a03f2d80953ddb0d1
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2019 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #ifndef CL_ENTITIES_H
24 #define CL_ENTITIES_H
27 /////////////
28 // INCLUDE //
29 /////////////
31 // Client
32 #include "ground_fx_manager.h"
33 #include "projectile_manager.h"
34 #include "user_entity.h"
35 // Some constants
36 #include "game_share/mission_desc.h"
37 #include "game_share/inventories.h"
38 // Misc.
39 #include "nel/misc/types_nl.h"
40 #include "nel/misc/stream.h"
41 #include "nel/misc/time_nl.h"
42 #include "nel/misc/vector.h"
43 #include "nel/misc/file.h"
44 #include "nel/misc/aabbox.h"
45 #include "nel/misc/cdb_leaf.h"
46 // 3D
47 #include "nel/3d/u_instance.h"
48 // Std.
49 #include <vector>
52 ///////////
53 // CLASS //
54 ///////////
55 class CEntityCL;
56 class CUserEntity;
57 struct TNewEntityInfo;
60 * Enum for entity selection
62 class CEntityFilterFlag
64 public:
65 enum TFlag
67 NotUser = 1 << 0,
68 Friend = 1 << 1,
69 Enemy = 1 << 2,
70 Alive = 1 << 3,
71 Dead = 1 << 4,
72 Player = 1 << 5, // ok if entity is a Player (not user)
73 NonPlayer = 1 << 6, // ok if entity is a non-player (ie a bot, whaterver Npc fauna etc...)
75 NoFilter = 0 // no filter
80 * Class to make cache entities
82 class CEntityReference
84 public:
85 CEntityReference (uint slot, CEntityCL *entity)
87 Slot = slot;
88 Entity = entity;
91 CEntityCL *Entity;
92 uint Slot;
96 * Class to make cache shape instances
98 class CShapeInstanceReference
100 public:
101 CShapeInstanceReference (NL3D::UInstance instance, const string &text, const string &url, bool bbox_active=true, bool in_ig_zone = false)
103 Instance = instance;
104 ContextText = text;
105 ContextURL = url;
106 BboxActive = bbox_active;
107 Deleted = false;
108 LastDeleted = -1;
109 InIGZone = in_ig_zone;
110 Primitive = NULL;
111 PrimSize = CVector(1.f, 1.f, 1.f);
112 PrimHeight = 1.f;
113 PrimRelativePos = CVector(0.f, 0.f, 0.f);
116 NL3D::UInstance Instance;
117 NLPACS::UMovePrimitive *Primitive;
118 CVector PrimSize;
119 float PrimHeight;
120 CVector PrimRelativePos;
121 string ContextText;
122 string ContextURL;
123 bool BboxActive;
124 bool Deleted;
125 bool InIGZone;
126 sint32 LastDeleted;
130 * Class to manage entities and shapes instances.
131 * \author Guillaume PUZIN
132 * \author Nevrax France
133 * \date 2001
135 class CEntityManager : public NLMISC::IStreamable
137 private:
138 typedef std::vector<CEntityCL *> TEntities;
139 // Contain all entities.
140 TEntities _Entities;
141 // maximum number of entities.
142 uint32 _NbMaxEntity;
143 /// Nb Entities really allocated.
144 uint32 _EntitiesAllocated;
146 /// Entity caches
147 std::vector<CEntityReference> _ActiveEntities;
148 std::vector<CEntityReference> _VisibleEntities;
150 /// Shapes Instances caches
151 typedef std::map<uint16, std::vector<uint32>> TIGZoneShapes;
152 TIGZoneShapes _IgZoneShapes;
153 std::vector<CShapeInstanceReference> _ShapeInstances;
154 sint32 _LastRemovedInstance;
155 bool _InstancesRemoved;
157 typedef struct
159 NLMISC::TGameCycle GC;
160 sint64 Value;
161 } TProperty;
162 typedef std::map<uint32, TProperty> TProperties;
163 typedef std::map<uint32, TProperties> TBackupedChanges;
164 TBackupedChanges _BackupedChanges;
166 // ground FXs
167 CGroundFXManager _GroundFXManager;
168 // Handle of each entity in the ground fx manager
169 std::vector<CGroundFXManager::TEntityHandle> _EntityGroundFXHandle;
171 // For selection. NB: the pointer is just a cache. Must not be accessed
172 CEntityCL *_LastEntityUnderPos;
174 NL3D::UInstance _LastInstanceUnderPos;
176 // DB node pointers used to update some entity flags
177 NLMISC::CRefPtr<NLMISC::CCDBNodeLeaf> _MissionTargetTitleDB[MAX_NUM_MISSIONS][MAX_NUM_MISSION_TARGETS];
178 NLMISC::CRefPtr<NLMISC::CCDBNodeLeaf> _GroupMemberUidDB[8]; // MaxNumPeopleInTeam in people_interaction.h
179 NLMISC::CRefPtr<NLMISC::CCDBNodeLeaf> _GroupMemberNameDB[8]; // MaxNumPeopleInTeam in people_interaction.h
180 NLMISC::CRefPtr<NLMISC::CCDBNodeLeaf> _BeastUidDB[MAX_INVENTORY_ANIMAL];
181 NLMISC::CRefPtr<NLMISC::CCDBNodeLeaf> _BeastStatusDB[MAX_INVENTORY_ANIMAL];
182 NLMISC::CRefPtr<NLMISC::CCDBNodeLeaf> _BeastTypeDB[MAX_INVENTORY_ANIMAL];
184 //////////////
185 //// DEBUG ///
186 uint _NbUser;
187 uint _NbPlayer;
188 uint _NbChar;
189 // For loging the property stage changes of the watched entity
190 struct CStageSetLog
192 bool Enabled;
193 CLFECOMMON::TCLEntityId LastEntityLoged;
194 // start time of recording for the last entity
195 sint32 StartGameCycle;
196 sint64 StartLocalTime;
197 CStageSet::TStageSet StageSet;
199 CStageSetLog()
201 Enabled= false;
202 LastEntityLoged= CLFECOMMON::INVALID_SLOT;
205 CStageSetLog _LogStageChange;
207 private:
208 /// Reset Counters
209 void resetCounters() {_EntitiesAllocated = 0; _NbUser = 0; _NbPlayer = 0; _NbChar = 0;}
212 public:
213 NLMISC_DECLARE_CLASS(CEntityManager);
215 /// Constructor.
216 CEntityManager();
217 /// Destructor.
218 ~CEntityManager();
221 * Initialize some dynamic parameters.
222 * \param uint nbMaxEntity : maximum number of entities allocated.
224 void initialize(uint nbMaxEntity);
226 // get ground fx manager
227 CGroundFXManager &getGroundFXManager() { return _GroundFXManager; }
229 /// Free the class and all the components.
230 void release();
232 /// Release + initialize
233 void reinit();
236 CShapeInstanceReference createInstance(const string& shape, const CVector &pos, const string &text, const string &url, bool haveCollisions, uint16 inIgZone, sint32 &idx);
237 bool deleteInstance(uint32 idx);
238 bool removeInstances();
239 void removeInstancesInIgZone(uint16 igZone);
240 CVector getInstancePos(uint32 idx);
241 bool setInstancePos(uint32 idx, CVector pos);
242 CVector getInstanceRot(uint32 idx);
243 CVector getInstanceScale(uint32 idx);
244 CVector getInstanceColPos(uint32 idx);
245 CVector getInstanceColScale(uint32 idx);
246 double getInstanceColOrient(uint32 idx);
247 CVector getInstanceBBoxMin(uint32 idx);
248 CVector getInstanceBBoxMax(uint32 idx);
249 bool setInstanceRot(uint32 idx, CVector pos);
250 bool setupInstance(uint32 idx, const std::vector<std::string> &keys, const std::vector<std::string> &values);
251 CShapeInstanceReference getShapeInstanceUnderPos(float x, float y, sint32 &idx);
254 * Create an entity according to the slot and the form.
255 * \param uint slot : slot for the entity.
256 * \param uint32 form : form to create the entity.
257 * \param TClientDataSetIndex : persitent id while the entity is connected.
258 * \return CEntityCL * : pointer on the new entity.
260 CEntityCL *create(uint slot, uint32 form, const TNewEntityInfo& newEntityInfo);
263 * Delete an entity.
264 * \return bool : 'true' if the entity has been correctly removed.
266 bool remove(uint slot, bool warning);
267 /// Remove the collision for all entities.
268 void removeCollision();
269 /// Re-load animations (remove and load).
270 void reloadAnims();
273 * Get a pointer on an entity according to the asked slot.
274 * \param uint slot : the asked slot.
276 CEntityCL *entity(uint slot);
279 * Return if there is an entity near a door.
280 * \param float openingDist : near is when you are under the 'openingDist'.
281 * \param const CVector& posDoor1 : first door position.
282 * \param const CVector& posDoor2 : second door position.
283 * \return bool ; 'true' if any entity is near one of the door.
285 bool entitiesNearDoors(float openingDist, const NLMISC::CVector& posDoor1, const NLMISC::CVector& posDoor2);
288 * Get the entity under the (2d) position. Return NULL if no entity under this position.
289 * NB: the UserEntity can NOT be returned. But if the code has find it, isPlayerUnderCursor is set to true
290 * NB: somewhat slow. should be called ONLY ONCE per frame.
291 * NB: unselectable entities are not returned
293 CEntityCL *getEntityUnderPos(float x, float y, float distSelection, bool &isPlayerUnderCursor);
296 * Get the entity (not user) in the camera. Return NULL if not entity under this position.
297 * \param flags a ORed of CEntityFilterFlag::TFlag
298 * \param distSelection don't go beyond
299 * \param precEntity is used for Cyclic management. If found in the select list, get the next one, else get the first.
301 CEntityCL *getEntityInCamera(uint flags, float distSelection, CLFECOMMON::TCLEntityId precEntity);
303 /// Get an entity by name. Returns NULL if the entity is not found.
304 CEntityCL *getEntityByName (uint32 stringId) const;
306 /** Get an entity by name. Returns NULL if the entity is not found.
307 * \param name of the entity to find
308 * \param caseSensitive type of test to perform
309 * \param complete : if true, the name must match the full name of the entity.
311 CEntityCL *getEntityByName (const std::string &name, bool caseSensitive, bool complete) const;
313 * Case insensitive match against entity name. All listed keywords must match.
314 * \param keywords to match
315 * \param onlySelectable : if true, match only entity that can be selected
317 CEntityCL *getEntityByKeywords (const std::vector<std::string> &keywords, bool onlySelectable) const;
318 CEntityCL *getEntityBySheetName (const std::string &sheet) const;
319 /// Get an entity by dataset index. Returns NULL if the entity is not found.
320 CEntityCL *getEntityByCompressedIndex(TDataSetIndex compressedIndex) const;
322 /// Return number of entities allocated.
323 uint nbEntitiesAllocated() const {return _EntitiesAllocated;}
324 /// Return the number of user allocated.
325 uint nbUser() const {return _NbUser;}
326 /// Return the number of player allocated.
327 uint nbPlayer() const {return _NbPlayer;}
328 /// Return the number of character allocated.
329 uint nbChar() const {return _NbChar;}
333 /// Continent has changed.
334 void changeContinent();
337 * Update the entity (position\animation).
339 void updatePreCamera();
340 void updatePostCamera(uint clippedUpdateTime, const std::vector<NLMISC::CPlane> &clippingPlanes, const NLMISC::CVector &camPos);
341 void updatePostRender();
344 * Method to update the visual property 'prop' for the entity in 'slot'.
345 * \param uint slot : slot of the entity to update.
346 * \param uint prop : the property to udapte.
348 void updateVisualProperty(const NLMISC::TGameCycle &gameCycle, const uint &slot, const uint &prop, const NLMISC::TGameCycle &predictedInterval = 0);
353 void applyBackupedProperties(uint slot);
355 /// Return the reference for all entities in the vision. An entity object, for a slot, is not allocated if not visible.
356 TEntities &entities() {return _Entities;}
358 /// Manage PACS Triggers.
359 void managePACSTriggers();
361 void removeColUserOther();
362 void restoreColUserOther();
364 // For Sound Reset/Reload
365 void resetAllSoundAnimId();
367 ///////////
368 // DEBUG //
369 ///////////
370 /// Write a file with the position of all entities.
371 void writeEntities();
372 /// Dump entities state.
373 void dump(class NLMISC::IStream &f);
374 /// Dump entities state (XML Format).
375 void dumpXML(class NLMISC::IStream &f);
376 /// Log Watched Entity Stages Change
377 void startLogStageChange(sint32 currentGameCycle, sint64 currentLocalTime);
378 void logStageChange(sint64 currentLocalTime);
379 void stopLogStageChange();
380 bool isLogingStageChange() const;
381 sint32 getLogStageChangeStartCycle() const;
382 sint64 getLogStageChangeStartLocalTime() const;
384 /// Serialize entities.
385 virtual void serial(NLMISC::IStream &f);
387 // remove all attached fx of all entities (so that they can be reloaded)
388 virtual void removeAllAttachedFX();
391 * If the slot leads to a CCharacterCL compatible with mission icon, and
392 * who isFriend(), perform releaseInSceneInterfaces() and buildInSceneInterface().
393 * Otherwise, do nothing.
395 void refreshInsceneInterfaceOfFriendNPC(uint slot);
397 inline NLMISC::CCDBNodeLeaf *getMissionTargetTitleDB(int mission, int target) { return _MissionTargetTitleDB[mission][target]; }
398 inline NLMISC::CCDBNodeLeaf *getGroupMemberUidDB(int member) { return _GroupMemberUidDB[member]; }
399 inline NLMISC::CCDBNodeLeaf *getGroupMemberNameDB(int member) { return _GroupMemberNameDB[member]; }
400 inline NLMISC::CCDBNodeLeaf *getBeastUidDB(int beast) { return _BeastUidDB[beast]; }
401 inline NLMISC::CCDBNodeLeaf *getBeastStatusDB(int beast) { return _BeastStatusDB[beast]; }
402 inline NLMISC::CCDBNodeLeaf *getBeastTypeDB(int beast) { return _BeastTypeDB[beast]; }
404 private:
406 // NB: don't return unselectable entities
407 void getEntityListForSelection(std::vector<CEntityCL*> &entities, uint flags);
409 void updateEntitiesIsInTeam();
411 /// Log entities Property Stages Change
412 void logPropertyChange(CLFECOMMON::TCLEntityId who, const CStage &oldStage, const CStage &newStage,
413 const NLMISC::CVectorD &precOldPos, const NLMISC::CVectorD &precNewPos, sint32 relGameCycle, sint64 relLocalTime);
416 extern CEntityManager EntitiesMngr;
420 #endif // CL_ENTITIES_H
422 /* End of entities.h */