From 518619603ab3c49b7fdfcf19c439c1a30668164f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 2 Apr 2015 15:28:31 -0600 Subject: Everything works, it could use more stuff. --- src/map.cpp | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/map.cpp (limited to 'src/map.cpp') diff --git a/src/map.cpp b/src/map.cpp new file mode 100644 index 0000000..17f9bd8 --- /dev/null +++ b/src/map.cpp @@ -0,0 +1,140 @@ +#include "map.h" +#include "cell.h" +#include "position.h" + +#include +#include +#include + +Map::Map( const Position &rpMax ) : + iDims( rpMax.getDims() ), + aiSize( 0 ), + acMap( 0 ) +{ + int iTotal = 1; + aiSize = new int[iDims]; + for( int j = 0; j < iDims; j++ ) + { + aiSize[j] = rpMax[j]; + iTotal *= aiSize[j]; + } + acMap = new Cell[iTotal]; +} + +Map::~Map() +{ + delete[] aiSize; + delete[] acMap; +} + +Cell &Map::operator[]( const Position &rpIdx ) const +{ + return acMap[getIndex( rpIdx )]; +} + +Cell &Map::operator[]( const Position &rpIdx ) +{ + return acMap[getIndex( rpIdx )]; +} + +int Map::getDims() const +{ + return iDims; +} + +int Map::getSize( int iDim ) const +{ + return aiSize[iDim]; +} + +int Map::getIndex( const Position &rpIdx ) const +{ + if( !isInside( rpIdx ) ) + throw std::exception(); + + int iIdx = 0; + int iScale = 1; + for( int j = 0; j < iDims; j++ ) + { + iIdx += rpIdx[j]*iScale; + iScale *= aiSize[j]; + } + return iIdx; +} + +bool Map::isInside( const Position &rpIdx ) const +{ + if( rpIdx.getDims() != iDims ) + throw std::exception(); + + for( int j = 0; j < iDims; j++ ) + { + if( rpIdx[j] < 0 ) + return false; + if( rpIdx[j] >= aiSize[j] ) + return false; + } + + return true; +} + +int opposite( int iDir ); + +void Map::connect( int iId1, int iId2 ) +{ + Position p( iDims ); + + Position pMax1, pMax2; + int iDistMax = 0; + int iDirMax = 0; + + int iDim; + for(;;) + { + Cell &c = (*this)[p]; + if( c.iPath == iId1 || c.iPath == iId2 ) + { + // Could be the thing! + for( iDim = 0; iDim < iDims; iDim++ ) + { + Position t( p ); + t[iDim]--; + for( int iDir = 0; iDir < 2; iDir++ ) + { +// int w = (1<<(iDim*2+iDir)); + if( t[iDim] >= 0 && t[iDim] < aiSize[iDim] ) + { + Cell &c2 = (*this)[t]; + if( c.iPath != c2.iPath && + (c2.iPath == iId1 || c2.iPath == iId2) ) + { + int iDist = c.iDist+c2.iDist; + if( iDist > iDistMax ) + { + iDistMax = iDist; + pMax1 = p; + pMax2 = t; + iDirMax = iDim*2+iDir; + } + } + } + t[iDim] += 2; + } + } + } + + for( iDim = 0; iDim < iDims; iDim++ ) + { + if( ++p[iDim] < aiSize[iDim] ) + break; + p[iDim] = 0; + } + if( iDim == iDims ) + break; + } + + (*this)[pMax1].iWalls |= (1<