From 2c0ca0b4c3152765f97e9bb3894b55694e979f32 Mon Sep 17 00:00:00 2001 From: tycho Date: Tue, 15 Dec 2015 10:46:32 +0000 Subject: Changed cPath to have a reset method. Also reverts "Changed raw cPath to an unique_ptr, fixes memory leak" This reverts commit 1515d37684b469f212bb9858cca6128d74e591b6. --- src/Mobs/Path.cpp | 90 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 28 deletions(-) (limited to 'src/Mobs/Path.cpp') diff --git a/src/Mobs/Path.cpp b/src/Mobs/Path.cpp index c0cffbeb4..f3a0d6cf2 100644 --- a/src/Mobs/Path.cpp +++ b/src/Mobs/Path.cpp @@ -39,34 +39,7 @@ cPath::cPath( m_Chunk(&a_Chunk), m_BadChunkFound(false) { - // TODO: if src not walkable OR dest not walkable, then abort. - // Borrow a new "isWalkable" from ProcessIfWalkable, make ProcessIfWalkable also call isWalkable - - a_BoundingBoxWidth = 1; // Until we improve physics, if ever. - - m_BoundingBoxWidth = CeilC(a_BoundingBoxWidth); - m_BoundingBoxHeight = CeilC(a_BoundingBoxHeight); - m_HalfWidth = a_BoundingBoxWidth / 2; - - int HalfWidthInt = FloorC(a_BoundingBoxWidth / 2); - m_Source.x = FloorC(a_StartingPoint.x - HalfWidthInt); - m_Source.y = FloorC(a_StartingPoint.y); - m_Source.z = FloorC(a_StartingPoint.z - HalfWidthInt); - - m_Destination.x = FloorC(a_EndingPoint.x - HalfWidthInt); - m_Destination.y = FloorC(a_EndingPoint.y); - m_Destination.z = FloorC(a_EndingPoint.z - HalfWidthInt); - - if (GetCell(m_Source)->m_IsSolid || GetCell(m_Destination)->m_IsSolid) - { - m_Status = ePathFinderStatus::PATH_NOT_FOUND; - return; - } - - m_NearestPointToTarget = GetCell(m_Source); - m_Status = ePathFinderStatus::CALCULATING; - - ProcessCell(GetCell(m_Source), nullptr, 0); + ResetImpl(a_StartingPoint, a_EndingPoint, a_BoundingBoxWidth, a_BoundingBoxHeight); } cPath::cPath() : m_IsValid(false) @@ -497,3 +470,64 @@ cPathCell * cPath::GetCell(const Vector3i & a_Location) return &m_Map[a_Location]; } } + + + + + +void cPath::ResetImpl( + const Vector3d & a_StartingPoint, const Vector3d & a_EndingPoint, + double a_BoundingBoxWidth, double a_BoundingBoxHeight +) +{ + // TODO: if src not walkable OR dest not walkable, then abort. + // Borrow a new "isWalkable" from ProcessIfWalkable, make ProcessIfWalkable also call isWalkable + + a_BoundingBoxWidth = 1; // Until we improve physics, if ever. + + m_BoundingBoxWidth = CeilC(a_BoundingBoxWidth); + m_BoundingBoxHeight = CeilC(a_BoundingBoxHeight); + m_HalfWidth = a_BoundingBoxWidth / 2; + + int HalfWidthInt = FloorC(a_BoundingBoxWidth / 2); + m_Source.x = FloorC(a_StartingPoint.x - HalfWidthInt); + m_Source.y = FloorC(a_StartingPoint.y); + m_Source.z = FloorC(a_StartingPoint.z - HalfWidthInt); + + m_Destination.x = FloorC(a_EndingPoint.x - HalfWidthInt); + m_Destination.y = FloorC(a_EndingPoint.y); + m_Destination.z = FloorC(a_EndingPoint.z - HalfWidthInt); + + if (GetCell(m_Source)->m_IsSolid || GetCell(m_Destination)->m_IsSolid) + { + m_Status = ePathFinderStatus::PATH_NOT_FOUND; + return; + } + + m_NearestPointToTarget = GetCell(m_Source); + m_Status = ePathFinderStatus::CALCULATING; + + ProcessCell(GetCell(m_Source), nullptr, 0); +} + + + + + +void cPath::Reset( + cChunk & a_Chunk, + const Vector3d & a_StartingPoint, const Vector3d & a_EndingPoint, int a_MaxSteps, + double a_BoundingBoxWidth, double a_BoundingBoxHeight, + int a_MaxUp, int a_MaxDown +) +{ + m_Map.clear(); + m_OpenList = decltype(m_OpenList){}; + m_StepsLeft = a_MaxSteps; + m_IsValid = true; + m_CurrentPoint = 0; // GetNextPoint increments this to 1, but that's fine, since the first cell is always a_StartingPoint + m_Chunk = &a_Chunk; + m_BadChunkFound = false; + ResetImpl(a_StartingPoint, a_EndingPoint, a_BoundingBoxWidth, a_BoundingBoxHeight); +} + -- cgit v1.2.3