/** * @function BuildManifold */ void HP2D::BuildManifold( Vertice _v0 ) { Vertice* va; Vertice vt; Vertice* vb; std::vector<Vertice> S; std::vector<Vertice*> B; InsertQueue( _v0 ); do{ va = PopQueue(); S = Successors( va ); for( int i = 0; i < S.size(); i++ ) { vt = S[i]; InsertVertice( vt ); InsertEdge( &vt, va ); if( vt.GetDist() < mDIST_MAX ) { InsertQueue( vt ); } B = GetAdjacent( va->Adjacent() ); for( unsigned j = 0; j < B.size(); j++ ) { vb = B[j]; if( InSet( vb->GetPos(), vt.Neighbors() ) ) { InsertEdge( &vt, vb ); } } } } while( GetSizeQueue() > 0 ); }
/// Get a sequential list of vertices for Start to Stop, if one exists bool clGraph::FindPath( int Start, int Stop, LArray<int>& Path ) { Path.clear(); /// Try one: BFS LArray<bool> Visited( FVertices.size() ); bool* v = Visited.begin(); for ( size_t p = 0 ; p < FVertices.size() ; p++ ) { *v++ = false; } /// stack for visited nodes LArray<int> Q; Path.push_back( Start ); Path.push_back( Start ); // twic, because first is popped Q.push_back( Start ); Visited[Start] = true; LArray<int> Adj; while ( !Q.empty() ) { int x = Q.back(); if ( x == Stop ) { return true; } Q.pop_back(); Path.pop_back(); GetAdjacent( x, Adj ); int N = static_cast<int>( Adj.size() ); for ( int i = 0 ; i < N ; i++ ) { int v = Adj[i]; if ( !Visited[v] ) { Q.push_back( v ); Visited[v] = true; Path.push_back( v ); } } } return false; }
bool BlockInfo::IsRemoveable() { int adjCount = 0; if (!GetAdjacent(EGridDirection::GD_UP)->bIsSolid) adjCount++; if (!GetAdjacent(EGridDirection::GD_RIGHT)->bIsSolid) adjCount++; if (adjCount == 2) return false; if (!GetAdjacent(EGridDirection::GD_DOWN)->bIsSolid) adjCount++; if (adjCount >= 2) return false; if (adjCount == 1 && !GetAdjacent(EGridDirection::GD_LEFT)->bIsSolid) return false; return true; }
void BlockInfo::GenerateHallway(FRandomStream* randStream) { bool canRemove = IsRemoveable(); bIsSolid = false; if (!canRemove) return; bool needsGeneration = true; do { //get a random direction int direction = randStream->RandRange(0, 3); EGridDirection::Type gridDirection; switch (direction) { case 0: gridDirection = EGridDirection::GD_UP; break; case 1: gridDirection = EGridDirection::GD_RIGHT; break; case 2: gridDirection = EGridDirection::GD_DOWN; break; case 3: gridDirection = EGridDirection::GD_LEFT; break } BlockInfo* randBlock = GetAdjacent(gridDirection);//get a block in a random direction if (!randBlock || randBlock->IsEdge())//check if that block is removeable { continue; } randBlock->GenerateHallway(randStream); needsGeneration = false; } while (needsGeneration); }
void BlockInfo::Generate(FRandomStream* randStream) { bIsSolid = false; bool needsGeneration = true; int genTryCount = 0; do { //get a random direction int direction = randStream->RandRange(0, 3); EGridDirection::Type gridDirection; switch (direction) { case 0: gridDirection = EGridDirection::GD_UP; break; case 1: gridDirection = EGridDirection::GD_RIGHT; break; case 2: gridDirection = EGridDirection::GD_DOWN; break; case 3: gridDirection = EGridDirection::GD_LEFT; break } BlockInfo* randBlock = GetAdjacent(gridDirection);//get a block in a random direction if (!randBlock || !randBlock->IsRemoveable() || randBlock->IsEdge())//check if that block is removeable { genTryCount++; continue; } randBlock->Generate(randStream); genTryCount++; } while (needsGeneration && genTryCount < 4); }