* Try to drive to a destination. * @param startPos Starting position. * @param destZone Zonetype to drive to. * @return Was drive succesful? * @post Position stack (curMapStackXY) is filled with some intermediate * positions of the drive. * * @bug The stack is popped, but position (and dirLast) is not updated. */
| 295 | * @bug The stack is popped, but position (and dirLast) is not updated. |
| 296 | */ |
| 297 | bool Micropolis::tryDrive(const Position &startPos, ZoneType destZone) |
| 298 | { |
| 299 | Direction2 dirLast = DIR2_INVALID; |
| 300 | Position drivePos(startPos); |
| 301 | |
| 302 | /* Maximum distance to try */ |
| 303 | for (short dist = 0; dist < MAX_TRAFFIC_DISTANCE; dist++) { |
| 304 | |
| 305 | Direction2 dir = tryGo(drivePos, dirLast); |
| 306 | if (dir != DIR2_INVALID) { // we found a road |
| 307 | drivePos.move(dir); |
| 308 | dirLast = rotate180(dir); |
| 309 | |
| 310 | /* Save pos every other move. |
| 311 | * This also relates to |
| 312 | * Micropolis::trafficDensityMap::MAP_BLOCKSIZE |
| 313 | */ |
| 314 | if (dist & 1) { |
| 315 | pushPos(drivePos); |
| 316 | } |
| 317 | |
| 318 | if (driveDone(drivePos, destZone)) { // if destination is reached |
| 319 | return true; /* pass */ |
| 320 | } |
| 321 | |
| 322 | } else { |
| 323 | |
| 324 | if (curMapStackPointer > 0) { /* dead end, backup */ |
| 325 | curMapStackPointer--; |
| 326 | dist += 3; |
| 327 | } else { |
| 328 | return false; /* give up at start */ |
| 329 | } |
| 330 | |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return false; /* gone MAX_TRAFFIC_DISTANCE */ |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /** |