----- GET BUILD LOCATION @TODO: If self() is nullptr, this will crash
| 503 | // ----- GET BUILD LOCATION |
| 504 | // @TODO: If self() is nullptr, this will crash |
| 505 | TilePosition Game::getBuildLocation(UnitType type, TilePosition desiredPosition, int maxRange, bool creep) const |
| 506 | { |
| 507 | this->setLastError(); // Reset last error |
| 508 | |
| 509 | // Make sure the type is compatible |
| 510 | if ( !type.isBuilding() ) |
| 511 | { |
| 512 | this->setLastError(Errors::Incompatible_UnitType); |
| 513 | return TilePositions::Invalid; |
| 514 | } |
| 515 | // @TODO: Addon |
| 516 | |
| 517 | // Do type-specific checks |
| 518 | bool trimPlacement = true; |
| 519 | Region pTargRegion = nullptr; |
| 520 | Unit pSpecialUnitTarget = nullptr; |
| 521 | switch ( type ) |
| 522 | { |
| 523 | case UnitTypes::Enum::Protoss_Pylon: |
| 524 | pSpecialUnitTarget = this->getClosestUnit(Position(desiredPosition), IsOwned && !IsPowered); |
| 525 | if ( pSpecialUnitTarget ) |
| 526 | { |
| 527 | desiredPosition = TilePosition(pSpecialUnitTarget->getPosition()); |
| 528 | trimPlacement = false; |
| 529 | } |
| 530 | break; |
| 531 | case UnitTypes::Enum::Terran_Command_Center: |
| 532 | case UnitTypes::Enum::Protoss_Nexus: |
| 533 | case UnitTypes::Enum::Zerg_Hatchery: |
| 534 | case UnitTypes::Enum::Special_Start_Location: |
| 535 | trimPlacement = false; |
| 536 | break; |
| 537 | case UnitTypes::Enum::Zerg_Creep_Colony: |
| 538 | case UnitTypes::Enum::Terran_Bunker: |
| 539 | //if ( Get bunker placement region ) |
| 540 | // trimPlacement = false; |
| 541 | break; |
| 542 | } |
| 543 | |
| 544 | PlacementReserve reserve(maxRange); |
| 545 | ReservePlacement(reserve, type, desiredPosition, creep); |
| 546 | |
| 547 | if ( trimPlacement ) |
| 548 | reserveTemplateSpacing(reserve); |
| 549 | |
| 550 | TilePosition centerPosition = desiredPosition - TilePosition(MAX_RANGE,MAX_RANGE)/2; |
| 551 | if ( pTargRegion != nullptr ) |
| 552 | desiredPosition = TilePosition(pTargRegion->getCenter()); |
| 553 | |
| 554 | // Find the best position |
| 555 | int bestDistance, fallbackDistance; |
| 556 | TilePosition bestPosition, fallbackPosition; |
| 557 | |
| 558 | bestDistance = fallbackDistance = 999999; |
| 559 | bestPosition = fallbackPosition = TilePositions::None; |
| 560 | for ( int passCount = 0; passCount < (pTargRegion ? 2 : 1); ++passCount ) |
| 561 | { |
| 562 | for ( int y = 0; y < MAX_RANGE; ++y ) |
no test coverage detected