void Grid::AdjacentCost( void* node, std::vector< StateCost > *neighbors ) { int x, y; const int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 }; const int dy[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }; const float cost[8] = { 1.0f, 1.41f, 1.0f, 1.41f, 1.0f, 1.41f, 1.0f, 1.41f }; NodeToXY( node, &x, &y ); for( int i=0; i<8; ++i ) { int nx = x + dx[i]; int ny = y + dy[i]; int pass = Passable( nx, ny ); if ( pass >0 ) { StateCost nodeCost = { XYToNode( nx, ny ), cost[i] }; neighbors->push_back( nodeCost ); } else{ // Normal floor StateCost nodeCost = { XYToNode( nx, ny ), FLT_MAX }; neighbors->push_back( nodeCost ); } } }
PathComponent::PathPool computePath(const AbsTile& startPos, const AbsTile& lastPos, micropather::MicroPather* pather) { if (startPos == lastPos || !isPositionValid(startPos) || !isPositionValid(lastPos)) return PathComponent::PathPool {}; TealAssert(pather, "Pather is null, cannot compute path !"); // Compute the path with the position and the move component. std::vector<void*> voidPath; float totalCost {}; // In case of debugging int result = pather->Solve(XYToNode(startPos.x, startPos.y), XYToNode(lastPos.x, lastPos.y), &voidPath, &totalCost); // returns the absolute position, not difference. if (result != 0 || voidPath.empty()) return PathComponent::PathPool {}; // Path done, in void*. Let's add it to the entity's path, in integers int oldX {}; int oldY {}; PathComponent::PathPool newPath; for (std::size_t i { 1 }; i < voidPath.size(); ++i) // First tile is actually the start position { auto node = voidPath[i]; unsigned absX {}, absY {}; // Absolute position, not difference NodeToXY(node, absX, absY); int startX { int(startPos.x) }; int startY { int(startPos.y) }; if (i > 1) // If i == 1 we use the initial position { // Else we use the position micropather generated before startX = oldX; startY = oldY; } int diffX { startX - int(absX) }, diffY { startY - int(absY) }; // Difference now, but reversed diffX = -diffX; // Ok diffY = -diffY; newPath.push_back(XYToDir({ diffX, diffY }, isLineEven(startY))); oldX = int(absX); oldY = int(absY); } return newPath; }
int Grid::SetPos( int nx, int ny ) { int result = MicroPather::NO_SOLUTION; if ( Passable( nx, ny ) == 1 ) { float totalCost; if ( showConsidered ) pather->Reset(); result = pather->Solve( XYToNode( playerX, playerY ), XYToNode( nx, ny ), &path, &totalCost ); if ( result == MicroPather::SOLVED ) { playerX = nx; playerY = ny; pathIt++; toDeleteList.clear(); //Log* pathLog = LogManager::getSingleton().createLog( "pathDebug_"+LogManager::number(pathIt)+".log", false, true); //pathLog->logMessage(Print()); ReducePath(); //Log* pathLog2 = LogManager::getSingleton().createLog( "pathReduceDebug_"+LogManager::number(pathIt)+".log", false, true); //pathLog2->logMessage(Print()); pathCell.clear(); for( int k=0; k<path.size(); ++k ) { bool toInsert=true; for(int l=0; l<toDeleteList.size(); l++){ if(k==toDeleteList[l]){ toInsert=false; break; } } if(toInsert){ int x, y; NodeToXY( path[k], &x, &y ); Cell_grid c; c.x = x; c.y = y; pathCell.insert( pathCell.end(), c); } } } } return result; }