From 83459d0d899786378e8304c92a5b79ddca92c62f Mon Sep 17 00:00:00 2001 From: LogicParrot Date: Sun, 7 Feb 2016 19:07:14 +0200 Subject: Proper entity destruction in non-ticking chunks --- src/Entities/Entity.h | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) (limited to 'src/Entities/Entity.h') diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h index dbfc019e7..75c77e593 100644 --- a/src/Entities/Entity.h +++ b/src/Entities/Entity.h @@ -252,9 +252,16 @@ public: void SteerVehicle(float a_Forward, float a_Sideways); inline UInt32 GetUniqueID(void) const { return m_UniqueID; } - inline bool IsDestroyed(void) const { return !m_IsInitialized; } - /** Schedules the entity for destroying; if a_ShouldBroadcast is set to true, broadcasts the DestroyEntity packet */ + /** Deprecated. Use IsTicking instead. */ + inline bool IsDestroyed() const {return !IsTicking();} + + /** Returns true if the entity is valid and ticking. Returns false if the entity is not ticking and is about to leave + its current world either via teleportation or destruction. + If this returns false, you must stop using the cEntity pointer you have. */ + bool IsTicking(void) const; + + /** Destroys the entity and schedules it for memory freeing; if a_ShouldBroadcast is set to true, broadcasts the DestroyEntity packet */ void Destroy(bool a_ShouldBroadcast = true); /** Makes this pawn take damage from an attack by a_Attacker. Damage values are calculated automatically and DoTakeDamage() called */ @@ -285,6 +292,9 @@ public: // tolua_end + /** Destroy the entity without scheduling memory freeing. This should only be used by cChunk or cClientHandle for internal memory management. */ + void DestroyNoScheduling(bool a_ShouldBroadcast); + /** Makes this entity take damage specified in the a_TDI. The TDI is sent through plugins first, then applied. If it returns false, the entity hasn't receive any damage. */ @@ -408,12 +418,6 @@ public: virtual bool DoMoveToWorld(cWorld * a_World, bool a_ShouldSendRespawn, Vector3d a_NewPosition); - /** Returns if the entity is travelling away from a specified world */ - bool IsWorldTravellingFrom(cWorld * a_World) const { return (m_WorldTravellingFrom == a_World); } - - /** Sets the world the entity will be leaving */ - void SetWorldTravellingFrom(cWorld * a_World) { m_WorldTravellingFrom = a_World; } - /** Updates clients of changes in the entity. */ virtual void BroadcastMovementUpdate(const cClientHandle * a_Exclude = nullptr); @@ -481,6 +485,16 @@ public: /** Sets the internal world pointer to a new cWorld, doesn't update anything else. */ void SetWorld(cWorld * a_World) { m_World = a_World; } + /** Sets the parent chunk, which is the chunk responsible for ticking this entity. + Only cChunk::AddEntity and cChunk::RemoveEntity cChunk::~cChunk should ever call this. */ + void SetParentChunk(cChunk * a_Chunk); + + /** Returns the chunk responsible for ticking this entity. */ + cChunk * GetParentChunk(); + + /** Set the entity's status to either ticking or not ticking. */ + void SetIsTicking(bool a_IsTicking); + protected: /** Structure storing the portal delay timer and cooldown boolean */ struct sPortalCooldownData @@ -538,14 +552,6 @@ protected: Vector3d m_LastPosition; - /** True when entity is initialised (Initialize()) and false when destroyed pending deletion (Destroy()) */ - bool m_IsInitialized; - - /** World entity is travelling from (such as when using portals). - Set to a valid world pointer by MoveToWorld; reset to nullptr when the entity is removed from the old world. - Can't be a simple boolean as context switches between worlds may leave the new chunk processing (and therefore immediately removing) the entity before the old chunk could remove it. */ - cWorld * m_WorldTravellingFrom; - eEntityType m_EntityType; cWorld * m_World; @@ -606,6 +612,13 @@ protected: virtual void SetSwimState(cChunk & a_Chunk); private: + + /** Whether the entity is ticking or not. If not, it is scheduled for removal or world-teleportation. */ + bool m_IsTicking; + + /** The chunk which is responsible for ticking this entity. */ + cChunk * m_ParentChunk; + /** Measured in degrees, [-180, +180) */ double m_HeadYaw; -- cgit v1.2.3