| 1 | /*************************************************************************** |
| 2 | pathfinder.cpp - description |
| 3 | ------------------- |
| 4 | begin : may 17th, 2004 |
| 5 | copyright : (C) 2004-2007 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: pathfinder.cpp 462 2011-07-03 15:49:40Z neoneurone $ |
| 9 | ***************************************************************************/ |
| 10 | |
| 11 | /*************************************************************************** |
| 12 | * * |
| 13 | * This program is free software; you can redistribute it and/or modify * |
| 14 | * it under the terms of the GNU General Public License as published by * |
| 15 | * the Free Software Foundation; either version 2 of the License, or * |
| 16 | * any later version. * |
| 17 | * * |
| 18 | ***************************************************************************/ |
| 19 | |
| 20 | // Useful enumerations |
| 21 | #include "opencity_direction.h" |
| 22 | #include "opencity_structure_type.h" |
| 23 | |
| 24 | // OpenCity headers |
| 25 | #include "buildinglayer.h" |
| 26 | #include "destination.h" |
| 27 | #include "pathstructure.h" |
| 28 | #include "map.h" |
| 29 | #include "pathfinder.h" |
| 30 | |
| 31 | // Standard headers |
| 32 | #include <algorithm> |
| 33 | |
| 34 | // Local defines |
| 35 | #define OC_PATHFINDER_MAX_LENGTH 0xFFFF0000 |
| 36 | |
| 37 | |
| 38 | //======================================================================== |
| 39 | /** This is an utility structure used during the pathfinding process |
| 40 | */ |
| 41 | struct PathFinderNode { |
| 42 | int iOwnLinear; ///< The OC linear coordinates of this node. |
| 43 | int iFatherLinear; ///< The OC linear coordinates of the precedent node, = -1 for none |
| 44 | uint uiEvaluation; ///< An evaluation to destination |
| 45 | PathStructure* ppath; ///< The PathStructure of this node |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | //======================================================================== |
| 50 | /** This is a functor, it's used for sorting node |
| 51 | */ |
| 52 | static bool |
| 53 | pathfinderCompareTraffic( const PathFinderNode & rcA, const PathFinderNode & rcB ) |
| 54 | { |
| 55 | assert( rcA.ppath != NULL ); |
| 56 | assert( rcB.ppath != NULL ); |
| 57 | |
| 58 | return rcA.ppath->GetLength() > rcB.ppath->GetLength(); |
| 59 | /* |
| 60 | uint uiA = rcA.ppath->GetLength(); |
| 61 | uint uiB = rcB.ppath->GetLength(); |
| 62 | |
| 63 | // Same length |
| 64 | if ( uiA == uiB ) { |
| 65 | if ( rcA.uiEvaluation < rcB.uiEvaluation) { |
| 66 | return false; |
| 67 | } |
| 68 | else { |
| 69 | return true; |
| 70 | } |
| 71 | } else |
| 72 | if ( uiA < uiB) { |
| 73 | return false; |
| 74 | } |
| 75 | else { |
| 76 | // Greater length, then put it at the beginning |
| 77 | return true; |
| 78 | } |
| 79 | */ |
| 80 | |
| 81 | // Impossible here |
| 82 | assert( 0 ); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | //======================================================================== |
| 87 | /** This is a functor, it's used for sorting node |
| 88 | */ |
| 89 | static bool |
| 90 | pathfinderCompareDistance( const PathFinderNode& rcA, const PathFinderNode& rcB ) |
| 91 | { |
| 92 | return rcA.uiEvaluation > rcB.uiEvaluation; |
| 93 | /* |
| 94 | assert( rcA.ppath != NULL ); |
| 95 | assert( rcB.ppath != NULL ); |
| 96 | |
| 97 | uint uiA = rcA.ppath->GetLength(); |
| 98 | uint uiB = rcB.ppath->GetLength(); |
| 99 | |
| 100 | // Same length |
| 101 | if ( rcA.uiEvaluation == rcB.uiEvaluation ) { |
| 102 | if ( uiA < uiB ) { |
| 103 | return false; |
| 104 | } |
| 105 | else { |
| 106 | return true; |
| 107 | } |
| 108 | } else |
| 109 | if ( rcA.uiEvaluation < rcB.uiEvaluation ) { |
| 110 | return false; |
| 111 | } |
| 112 | else { |
| 113 | // Greater length, then put it at the beginning |
| 114 | return true; |
| 115 | } |
| 116 | */ |
| 117 | |
| 118 | // Impossible here |
| 119 | assert( 0 ); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | //======================================================================== |
| 124 | /** This is an evaluating function. It calculates the euclidian |
| 125 | distance square between two coordinates |
| 126 | */ |
| 127 | inline const uint |
| 128 | pathfinderEvaluate( |
| 129 | const uint & rcuiW1, |
| 130 | const uint & rcuiH1, |
| 131 | const uint & rcuiW2, |
| 132 | const uint & rcuiH2) |
| 133 | { |
| 134 | return ((rcuiW2-rcuiW1)*(rcuiW2-rcuiW1) + (rcuiH2-rcuiH1)*(rcuiH2-rcuiH1)); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /*=====================================================================*/ |
| 139 | PathFinder::PathFinder( |
| 140 | SDL_mutex* const mutex, |
| 141 | BuildingLayer* const pblayer, |
| 142 | Map* const map, |
| 143 | const uint & rcuiCityWidth, |
| 144 | const uint & rcuiCityHeight ): |
| 145 | pmutex( mutex ), |
| 146 | pbuildlayer( pblayer ), |
| 147 | pmap( map ), |
| 148 | uiWidth( rcuiCityWidth ), |
| 149 | uiHeight( rcuiCityHeight ) |
| 150 | { |
| 151 | OPENCITY_DEBUG( "ctor" ); |
| 152 | |
| 153 | // some assert for programming errors checking |
| 154 | assert( mutex != NULL ); |
| 155 | assert( pblayer != NULL ); |
| 156 | assert( map != NULL ); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /*=====================================================================*/ |
| 161 | PathFinder::~PathFinder() |
| 162 | { |
| 163 | OPENCITY_DEBUG( "dtor" ); |
| 164 | } |
| 165 | |
| 166 | //======================================================================== |
| 167 | /** Find the shortest path given the OpenCity W1, H1 |
| 168 | and W2, H2 coordinates. The algorithm is adapted from the famous |
| 169 | DIJKSTRA algorithm. |
| 170 | \n |
| 171 | <b>Algorithm</b>\n |
| 172 | <pre> |
| 173 | 1) Initialize all the OC_ROAD structures |
| 174 | 2) Insert the starting point to the "processing" list |
| 175 | 3) WHILE the processing list is not empty and not boolFound DO |
| 176 | 3a)Sort the processing list |
| 177 | 3b)Pop the node with the minimum length (and not marked) |
| 178 | 3c)IF the node is the arrival point THEN |
| 179 | boolFound = TRUE |
| 180 | FI |
| 181 | 3d)FOR each neighbour OC_ROAD DO |
| 182 | newLength = the length of the current node |
| 183 | + the traffic of this neighbour; |
| 184 | IF newLength is less than the length of the neighbour THEN |
| 185 | i)the length of the neighbour is = newLength |
| 186 | ii)father of the neighbour is = this node |
| 187 | iii)insert the neighbour to the processing list |
| 188 | FI |
| 189 | DONE |
| 190 | 4) Mark the current node as processed |
| 191 | </pre> |
| 192 | |
| 193 | \param rcuiW1, rcuiH1, rcuiW2, rcuiH2 = must be the valid |
| 194 | OC W,H coordinates and there must be PathStructure at |
| 195 | the designated W1,H1 and W2, H2 coordinates |
| 196 | \param rvdest = the vector which contains the path requested if it exists |
| 197 | \param enumType = do we look for shortest path in term of distance or traffic ? |
| 198 | \param uiMaxLength = limit the number of loops in the algorithm to uiMaxLength. |
| 199 | It means that the maximum length of the returned path is uiMaxLength units long. |
| 200 | And the most important thing is that the returned path may _not_ be the optimal path. |
| 201 | \return false IF error, true otherwise |
| 202 | */ |
| 203 | const bool |
| 204 | PathFinder::findShortestPath( |
| 205 | const uint & rcuiW1, |
| 206 | const uint & rcuiH1, |
| 207 | const uint & rcuiW2, |
| 208 | const uint & rcuiH2, |
| 209 | std::vector<Destination> & rvdest, |
| 210 | const PATH_TYPE & enumType, |
| 211 | uint uiMaxLength ) |
| 212 | { |
| 213 | uint uiLinear; |
| 214 | uint uiW, uiH; |
| 215 | uint uiWN, uiHN; // N as Neighbour |
| 216 | uint uiLength; |
| 217 | uint uiMaxLinear = this->pbuildlayer->GetMaxLinear(); |
| 218 | Structure* pstruct; |
| 219 | PathStructure * ppathstruct; |
| 220 | |
| 221 | PathFinderNode node; |
| 222 | PathFinderNode nodeDone; |
| 223 | std::vector<PathFinderNode> vProcessing; |
| 224 | std::vector<PathFinderNode> vDone; |
| 225 | bool boolFound; |
| 226 | |
| 227 | // Dynamic compare functor |
| 228 | bool (*pFunctor)(const PathFinderNode&, const PathFinderNode&) = NULL; |
| 229 | |
| 230 | // Variables used to rebuild the "destination vector" from start to stop |
| 231 | int iFatherLinear; |
| 232 | Destination destLast, destTemp; |
| 233 | |
| 234 | // WARNING the Start point is a PathStructure |
| 235 | // do we need dynamic casting for automatic checking ? |
| 236 | ppathstruct = dynamic_cast<PathStructure*>(pbuildlayer->GetStructure( rcuiW1, rcuiH1 )); |
| 237 | if ( ppathstruct == NULL ) { |
| 238 | OPENCITY_DEBUG("WARNING: Starting point NULL"); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | // Block all the other threads while we play with the structures |
| 243 | SDL_LockMutex( this->pmutex ); |
| 244 | |
| 245 | // Initialize the functor |
| 246 | switch (enumType) { |
| 247 | case OC_DISTANCE: |
| 248 | pFunctor = pathfinderCompareDistance; |
| 249 | break; |
| 250 | |
| 251 | case OC_TRAFFIC: |
| 252 | pFunctor = pathfinderCompareTraffic; |
| 253 | break; |
| 254 | |
| 255 | default: |
| 256 | OPENCITY_DEBUG("Unrecognized path type enumeration."); |
| 257 | assert(0); |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | // For each OC_ROAD structure |
| 262 | for ( uiLinear = 0; uiLinear <= uiMaxLinear; uiLinear++ ) { |
| 263 | pstruct = pbuildlayer->GetLinearStructure( uiLinear ); |
| 264 | if ((pstruct != NULL) and (pstruct->GetCode() == OC_STRUCTURE_ROAD )) { |
| 265 | // Clear the "mark" |
| 266 | pstruct->Unset( OC_STRUCTURE_MARK ); |
| 267 | // Set the maximum "length" |
| 268 | ((PathStructure*)pstruct)->SetLength(OC_PATHFINDER_MAX_LENGTH ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // The length value of the starting point is 0 (minimum) |
| 273 | ppathstruct->SetLength( 0 ); |
| 274 | |
| 275 | // Insert the starting point to the processing vector |
| 276 | node.iOwnLinear = this->uiWidth * rcuiH1 + rcuiW1; |
| 277 | node.iFatherLinear = -1; // there's no precedent node |
| 278 | node.uiEvaluation = pathfinderEvaluate(rcuiW1, rcuiH1, rcuiW2, rcuiH2); |
| 279 | node.ppath = ppathstruct; |
| 280 | vProcessing.push_back( node ); |
| 281 | |
| 282 | // Loop until we finish or we found the arrival point |
| 283 | boolFound = false; |
| 284 | |
| 285 | while (!boolFound and (!vProcessing.empty()) and (vDone.size() < uiMaxLength)) { |
| 286 | // Sort the list so that the node with minimum "length" value |
| 287 | // and not "marked" is placed at the bottom (back) |
| 288 | std::sort( vProcessing.begin(), vProcessing.end(), pFunctor ); |
| 289 | |
| 290 | // get the sorted node with minimum length, and not marked |
| 291 | // NOTE: everything contained in the vProcessing is not marked ! |
| 292 | node = vProcessing.back(); |
| 293 | vProcessing.pop_back(); |
| 294 | |
| 295 | // Conversion Linear <-> WH |
| 296 | uiH = node.iOwnLinear / uiWidth; |
| 297 | uiW = node.iOwnLinear % uiWidth; |
| 298 | |
| 299 | // IF the arrival point has been found THEN |
| 300 | if ((uiW == rcuiW2) && (uiH == rcuiH2)) { |
| 301 | boolFound = true; |
| 302 | vDone.push_back( node ); |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | //---------------------- WARNING: repeated procedure begin --------------------- |
| 307 | // Get the neighbour in the NORTH |
| 308 | if ((this->pmap->GetNeighbourWH(uiW, uiH, uiWN, uiHN, OC_DIR_O_N ) == true) |
| 309 | &&((pstruct = pbuildlayer->GetStructure( uiWN, uiHN )) != NULL) |
| 310 | && (pstruct->GetCode() == OC_STRUCTURE_ROAD) |
| 311 | && (pstruct->IsSet(OC_STRUCTURE_MARK) == false)) { |
| 312 | //debug cout << "NORTH " << endl; |
| 313 | ppathstruct = (PathStructure*)pstruct; |
| 314 | uiLength = node.ppath->GetLength() + (uint)ppathstruct->GetTraffic(); |
| 315 | // Is the new path shorter ? |
| 316 | if (ppathstruct->GetLength() > uiLength) { |
| 317 | ppathstruct->SetLength( uiLength ); |
| 318 | // Now push the neighbour to the processing list |
| 319 | nodeDone.iOwnLinear = uiHN * uiWidth + uiWN; |
| 320 | nodeDone.iFatherLinear = node.iOwnLinear; |
| 321 | nodeDone.uiEvaluation = pathfinderEvaluate(uiWN, uiHN, rcuiW2, rcuiH2); |
| 322 | nodeDone.ppath = ppathstruct; |
| 323 | vProcessing.push_back( nodeDone ); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // Get the neighbour in the EAST |
| 328 | if ((this->pmap->GetNeighbourWH(uiW, uiH, uiWN, uiHN, OC_DIR_O_E ) == true) |
| 329 | &&((pstruct = pbuildlayer->GetStructure( uiWN, uiHN )) != NULL) |
| 330 | && (pstruct->GetCode() == OC_STRUCTURE_ROAD) |
| 331 | && (pstruct->IsSet(OC_STRUCTURE_MARK) == false)) { |
| 332 | //debug cout << "EAST " << endl; |
| 333 | ppathstruct = (PathStructure*)pstruct; |
| 334 | uiLength = node.ppath->GetLength() + (uint)ppathstruct->GetTraffic(); |
| 335 | // Is the new path shorter ? |
| 336 | if (ppathstruct->GetLength() > uiLength) { |
| 337 | ppathstruct->SetLength( uiLength ); |
| 338 | // now push the neighbour to the processing list |
| 339 | nodeDone.iOwnLinear = uiHN * uiWidth + uiWN; |
| 340 | nodeDone.iFatherLinear = node.iOwnLinear; |
| 341 | nodeDone.uiEvaluation = pathfinderEvaluate(uiWN, uiHN, rcuiW2, rcuiH2); |
| 342 | nodeDone.ppath = ppathstruct; |
| 343 | vProcessing.push_back( nodeDone ); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Get the neighbour in the SOUTH |
| 348 | if ((this->pmap->GetNeighbourWH(uiW, uiH, uiWN, uiHN, OC_DIR_O_S ) == true) |
| 349 | &&((pstruct = pbuildlayer->GetStructure( uiWN, uiHN )) != NULL) |
| 350 | && (pstruct->GetCode() == OC_STRUCTURE_ROAD) |
| 351 | && (pstruct->IsSet(OC_STRUCTURE_MARK) == false)) { |
| 352 | //debug cout << "SOUTH " << endl; |
| 353 | ppathstruct = (PathStructure*)pstruct; |
| 354 | uiLength = node.ppath->GetLength() + (uint)ppathstruct->GetTraffic(); |
| 355 | // Is the new path shorter ? |
| 356 | if (ppathstruct->GetLength() > uiLength) { |
| 357 | ppathstruct->SetLength( uiLength ); |
| 358 | // now push the neighbour to the processing list |
| 359 | nodeDone.iOwnLinear = uiHN * uiWidth + uiWN; |
| 360 | nodeDone.iFatherLinear = node.iOwnLinear; |
| 361 | nodeDone.uiEvaluation = pathfinderEvaluate(uiWN, uiHN, rcuiW2, rcuiH2); |
| 362 | nodeDone.ppath = ppathstruct; |
| 363 | vProcessing.push_back( nodeDone ); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Get the neighbour in the WEST |
| 368 | if ((this->pmap->GetNeighbourWH(uiW, uiH, uiWN, uiHN, OC_DIR_O_W ) == true) |
| 369 | &&((pstruct = pbuildlayer->GetStructure( uiWN, uiHN )) != NULL) |
| 370 | && (pstruct->GetCode() == OC_STRUCTURE_ROAD) |
| 371 | && (pstruct->IsSet(OC_STRUCTURE_MARK) == false)) { |
| 372 | //debug cout << "WEST " << endl; |
| 373 | ppathstruct = (PathStructure*)pstruct; |
| 374 | uiLength = node.ppath->GetLength() + (uint)ppathstruct->GetTraffic(); |
| 375 | // Is the new path shorter ? |
| 376 | if (ppathstruct->GetLength() > uiLength) { |
| 377 | ppathstruct->SetLength( uiLength ); |
| 378 | // now push the neighbour to the processing list |
| 379 | nodeDone.iOwnLinear = uiHN * uiWidth + uiWN; |
| 380 | nodeDone.iFatherLinear = node.iOwnLinear; |
| 381 | nodeDone.uiEvaluation = pathfinderEvaluate(uiWN, uiHN, rcuiW2, rcuiH2); |
| 382 | nodeDone.ppath = ppathstruct; |
| 383 | vProcessing.push_back( nodeDone ); |
| 384 | } |
| 385 | } |
| 386 | //---------------------- repeated procedure end --------------------- |
| 387 | |
| 388 | // Mark this node as "done" then insert it into the "done" list |
| 389 | node.ppath->Set( OC_STRUCTURE_MARK ); |
| 390 | vDone.push_back( node ); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | // Build the destination vector from start to stop |
| 395 | if (boolFound or (vDone.size() >= uiMaxLength)) { |
| 396 | // Put the last node to the list |
| 397 | int i = vDone.size()-1; |
| 398 | node = vDone[ i ]; |
| 399 | iFatherLinear = node.iFatherLinear; |
| 400 | destLast._uiW = node.iOwnLinear % uiWidth; |
| 401 | destLast._uiL = node.iOwnLinear / uiWidth; |
| 402 | destLast._iHMin = pmap->GetSquareMinHeight( destLast._uiW, destLast._uiL ); |
| 403 | destLast._iHMax = pmap->GetSquareMaxHeight( destLast._uiW, destLast._uiL ); |
| 404 | destLast._uiTime = 1; |
| 405 | destLast._ubTraffic = |
| 406 | ((PathStructure*)pbuildlayer-> |
| 407 | GetLinearStructure( node.iOwnLinear ))->GetTraffic(); |
| 408 | |
| 409 | rvdest.push_back( destLast ); |
| 410 | |
| 411 | while (--i >= 0) { |
| 412 | node = vDone[i]; |
| 413 | // IF this is NOT the father of the precedent node |
| 414 | if (node.iOwnLinear != iFatherLinear) |
| 415 | continue; |
| 416 | |
| 417 | iFatherLinear = node.iFatherLinear; |
| 418 | destTemp._uiW = node.iOwnLinear % uiWidth; |
| 419 | destTemp._uiL = node.iOwnLinear / uiWidth; |
| 420 | destTemp._iHMin = pmap->GetSquareMinHeight( destTemp._uiW, destTemp._uiL ); |
| 421 | destTemp._iHMax = pmap->GetSquareMaxHeight( destTemp._uiW, destTemp._uiL ); |
| 422 | destTemp._uiTime = 1; |
| 423 | destTemp._ubTraffic = |
| 424 | ((PathStructure*)pbuildlayer-> |
| 425 | GetLinearStructure( node.iOwnLinear ))->GetTraffic(); |
| 426 | |
| 427 | // NOTE: we're going from the back to the front |
| 428 | destTemp._eDir = Destination::GetDir( destTemp, destLast ); |
| 429 | rvdest.push_back( destTemp ); |
| 430 | destLast = destTemp; |
| 431 | } |
| 432 | |
| 433 | // Reverse the destination vector so that the starting point is at the beginning |
| 434 | reverse( rvdest.begin(), rvdest.end() ); |
| 435 | } // if boolFound |
| 436 | |
| 437 | #ifndef PATHFINDER_NDEBUG |
| 438 | //debug, print out the found path |
| 439 | if ((boolFound == true) or (vDone.size() >= uiMaxLength)) { |
| 440 | PATHFINDER_DEBUG( "Shortest path found, number of nodes: " << rvdest.size() ); |
| 441 | // initialize the "iFatherLinear" variable |
| 442 | // as the stop point's linear index |
| 443 | iFatherLinear = vDone[ vDone.size()-1 ].iOwnLinear; |
| 444 | for (int i = vDone.size()-1; i > -1; i--) { |
| 445 | node = vDone[i]; |
| 446 | // is this node is the father of the precedent node ? |
| 447 | if (node.iOwnLinear == iFatherLinear) { |
| 448 | // convert Linear <-> WH |
| 449 | uiH = node.iOwnLinear / uiWidth; |
| 450 | uiW = node.iOwnLinear % uiWidth; |
| 451 | cout << "W / H: " << uiW << " / " << uiH; |
| 452 | cout << " Length: " << node.ppath->GetLength(); |
| 453 | cout << " Traffic: " << (int)node.ppath->GetTraffic(); |
| 454 | cout << endl; |
| 455 | iFatherLinear = node.iFatherLinear; |
| 456 | } |
| 457 | } |
| 458 | //debug: print out all the processed WH |
| 459 | PATHFINDER_DEBUG( "All processed nodes, number of nodes : " << vDone.size() ); |
| 460 | for (unsigned int i = 0; i < vDone.size(); i++) { |
| 461 | node = vDone[i]; |
| 462 | // convert Linear <-> WH |
| 463 | uiH = node.iOwnLinear / uiWidth; |
| 464 | uiW = node.iOwnLinear % uiWidth; |
| 465 | cout << "W / H: " << uiW << " / " << uiH; |
| 466 | cout << " Length: " << node.ppath->GetLength(); |
| 467 | cout << " Traffic: " << (int)node.ppath->GetTraffic(); |
| 468 | cout << " Evaluation: " << node.uiEvaluation; |
| 469 | cout << endl; |
| 470 | } |
| 471 | } |
| 472 | #endif |
| 473 | |
| 474 | // Let the other threads run now |
| 475 | SDL_UnlockMutex( this->pmutex ); |
| 476 | |
| 477 | return boolFound; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 |