From baf357d2eaba1e016b60ac912f60fb6d11b3195e Mon Sep 17 00:00:00 2001 From: kevmo Date: Sun, 9 Apr 2006 00:36:52 +0000 Subject: [PATCH] # Began work on new pathfinder Created CTerrain::getSlope(x,y), and added a new order type to support high-level pathing. This was SVN commit r3735. --- source/graphics/Terrain.cpp | 35 +++++++++++++++++++++ source/graphics/Terrain.h | 2 ++ source/simulation/Entity.cpp | 5 +++ source/simulation/Entity.h | 1 + source/simulation/EntityOrders.h | 1 + source/simulation/EntityStateProcessing.cpp | 20 ++++++++++++ source/simulation/PathfindEngine.cpp | 14 +++++++++ source/simulation/PathfindEngine.h | 1 + 8 files changed, 79 insertions(+) diff --git a/source/graphics/Terrain.cpp b/source/graphics/Terrain.cpp index 718020c166..2ad476678d 100755 --- a/source/graphics/Terrain.cpp +++ b/source/graphics/Terrain.cpp @@ -182,6 +182,41 @@ float CTerrain::getVertexGroundLevel(int x, int z) const return HEIGHT_SCALE * m_Heightmap[z*m_MapSize + x]; } +float CTerrain::getSlope(float x, float y) const +{ + x /= (float)CELL_SIZE; + y /= (float)CELL_SIZE; + + int xi = (int)floor(x); + int yi = (int)floor(y); + + if (xi < 0) + { + xi = 0; + } + else if (xi >= (int)m_MapSize-1) + { + xi = m_MapSize - 2; + } + + if (yi < 0) + { + yi = 0; + } + else if (yi >= (int)m_MapSize-1) + { + yi = m_MapSize - 2; + } + + float h00 = m_Heightmap[yi*m_MapSize + xi]; + float h01 = m_Heightmap[yi*m_MapSize + xi + m_MapSize]; + float h10 = m_Heightmap[yi*m_MapSize + xi + 1]; + float h11 = m_Heightmap[yi*m_MapSize + xi + m_MapSize + 1]; + + return MAX(MAX(h00, h01), MAX(h10, h11)) - + MIN(MIN(h00, h01), MIN(h10, h11)); +} + float CTerrain::getExactGroundLevel(float x, float y) const { x /= (float)CELL_SIZE; diff --git a/source/graphics/Terrain.h b/source/graphics/Terrain.h index b364334375..cafb5edd8c 100755 --- a/source/graphics/Terrain.h +++ b/source/graphics/Terrain.h @@ -42,6 +42,8 @@ public: float getExactGroundLevel( float x, float y ) const ; inline float getExactGroundLevel( const CVector2D& v ) const { return( getExactGroundLevel( v.x, v.y ) ); } + float getSlope(float x, float y) const ; + // resize this terrain such that each side has given number of patches void Resize(u32 size); diff --git a/source/simulation/Entity.cpp b/source/simulation/Entity.cpp index 13e6b9bcd9..5c38217ad7 100755 --- a/source/simulation/Entity.cpp +++ b/source/simulation/Entity.cpp @@ -471,6 +471,11 @@ void CEntity::update( size_t timestep ) break; updateCollisionPatch(); return; + case CEntityOrder::ORDER_GOTO_WAYPOINT: + if ( processGotoWaypoint( current, timestep ) ) + break; + updateCollisionPatch(); + return; case CEntityOrder::ORDER_GOTO: case CEntityOrder::ORDER_RUN: if( processGoto( current, timestep ) ) diff --git a/source/simulation/Entity.h b/source/simulation/Entity.h index 5337d66474..2eee4c2f10 100755 --- a/source/simulation/Entity.h +++ b/source/simulation/Entity.h @@ -225,6 +225,7 @@ private: bool processGotoNoPathing( CEntityOrder* current, size_t timestep_milli ); bool processGoto( CEntityOrder* current, size_t timestep_milli ); + bool processGotoWaypoint( CEntityOrder* current, size_t timestep_milli ); bool processPatrol( CEntityOrder* current, size_t timestep_milli ); diff --git a/source/simulation/EntityOrders.h b/source/simulation/EntityOrders.h index cba4326f37..d0585154b8 100755 --- a/source/simulation/EntityOrders.h +++ b/source/simulation/EntityOrders.h @@ -85,6 +85,7 @@ public: ORDER_GOTO_NOPATHING, ORDER_GOTO_SMOOTHED, ORDER_GOTO_COLLISION, + ORDER_GOTO_WAYPOINT, ORDER_GOTO, ORDER_RUN, ORDER_PATROL, diff --git a/source/simulation/EntityStateProcessing.cpp b/source/simulation/EntityStateProcessing.cpp index 7dacc503e2..985d2bac11 100755 --- a/source/simulation/EntityStateProcessing.cpp +++ b/source/simulation/EntityStateProcessing.cpp @@ -592,6 +592,26 @@ bool CEntity::processGoto( CEntityOrder* current, size_t UNUSED(timestep_millis) return( true ); } +bool CEntity::processGotoWaypoint( CEntityOrder* current, size_t UNUSED(timestep_milli) ) +{ + CVector2D pos( m_position.X, m_position.Z ); + CVector2D path_to = current->m_data[0].location; + m_orderQueue.pop_front(); + float Distance = ( path_to - pos ).length(); + + // Let's just check we're going somewhere... + if( Distance < 0.1f ) + { + m_isRunning = false; + m_shouldRun = false; + return( false ); + } + + g_Pathfinder.requestLowLevelPath( me, path_to ); + + return( true ); +} + bool CEntity::processPatrol( CEntityOrder* current, size_t UNUSED(timestep_millis) ) { // float timestep=timestep_millis/1000.0f; diff --git a/source/simulation/PathfindEngine.cpp b/source/simulation/PathfindEngine.cpp index 9c53cb1861..16fda2c2d2 100755 --- a/source/simulation/PathfindEngine.cpp +++ b/source/simulation/PathfindEngine.cpp @@ -13,6 +13,20 @@ CPathfindEngine::CPathfindEngine() void CPathfindEngine::requestPath( HEntity entity, const CVector2D& destination ) { +// pathSparse( entity, destination ); + /* TODO: Add code to generate high level path + For now, just the one high level waypoint to the final + destination is added + */ + CEntityOrder waypoint; + waypoint.m_type = CEntityOrder::ORDER_GOTO_WAYPOINT; + waypoint.m_data[0].location = destination; + entity->m_orderQueue.push_front( waypoint ); +} + +void CPathfindEngine::requestLowLevelPath( HEntity entity, const CVector2D& destination ) +{ + /* Temporary - will be replaced by low-level A* */ pathSparse( entity, destination ); } diff --git a/source/simulation/PathfindEngine.h b/source/simulation/PathfindEngine.h index 8468bbeee5..53f46fc47f 100755 --- a/source/simulation/PathfindEngine.h +++ b/source/simulation/PathfindEngine.h @@ -30,6 +30,7 @@ class CPathfindEngine : public Singleton public: CPathfindEngine(); void requestPath( HEntity entity, const CVector2D& destination ); + void requestLowLevelPath( HEntity entity, const CVector2D& destination ); void requestContactPath( HEntity entity, CEntityOrder* current ); };