MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / getTrackProgressAtCursor

Function getTrackProgressAtCursor

src/OpenLoco/src/Ui/Windows/Vehicle.cpp:4576–4606  ·  view source on GitHub ↗

0x004A43E4

Source from the content-addressed store, hash-verified

4574
4575 // 0x004A43E4
4576 static uint16_t getTrackProgressAtCursor(const Point& cursorLoc, Ui::Viewport& viewport, const TrackElement& trackElement, const World::Pos3& loc)
4577 {
4578 // Get the coordinates of the first tile of the possibly multi-tile track
4579 const auto& trackDataArr = World::TrackData::getTrackPiece(trackElement.trackId());
4580 const auto& trackData = trackDataArr[trackElement.sequenceIndex()];
4581 auto trackOffset2 = Math::Vector::rotate(World::Pos2(trackData.x, trackData.y), trackElement.rotation());
4582 auto trackOffset = World::Pos3(trackOffset2.x, trackOffset2.y, trackData.z);
4583 auto trackFirstTile = loc - trackOffset;
4584
4585 // Get the movement info for this specific track id
4586 uint16_t trackAndDirection = trackElement.rotation() | (trackElement.trackId() << 3);
4587 const auto moveInfoArr = World::TrackData::getTrackSubPositon(trackAndDirection);
4588
4589 // This iterates the movement info trying to find the distance along the track that is as close as possible
4590 // to the cursors location.
4591 int32_t bestDistance = std::numeric_limits<int32_t>::max();
4592 uint16_t bestProgress = 0;
4593 for (const auto& moveInfo : moveInfoArr)
4594 {
4595 auto potentialLoc = trackFirstTile + moveInfo.loc;
4596 auto viewPos = World::gameToScreen(potentialLoc, viewport.getRotation());
4597 auto uiPos = viewport.viewportToScreen(viewPos);
4598 auto distance = Math::Vector::manhattanDistance2D(uiPos, cursorLoc);
4599 if (distance < bestDistance)
4600 {
4601 bestDistance = distance;
4602 bestProgress = std::distance(&*moveInfoArr.begin(), &moveInfo);
4603 }
4604 }
4605 return bestProgress;
4606 }
4607
4608 // 0x004A40C5
4609 static std::optional<GameCommands::VehiclePlacementArgs> getTrackAtCursor(const int16_t x, const int16_t y)

Callers 1

getTrackAtCursorFunction · 0.85

Calls 11

getTrackPieceFunction · 0.85
getTrackSubPositonFunction · 0.85
gameToScreenFunction · 0.85
manhattanDistance2DFunction · 0.85
trackIdMethod · 0.80
viewportToScreenMethod · 0.80
rotateFunction · 0.70
sequenceIndexMethod · 0.45
rotationMethod · 0.45
getRotationMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected