Beco: Difference between revisions
Jump to navigation
Jump to search
imported>Leoetlino No edit summary |
(→Getting data for a given coordinate: replace pseudocode with actual C++ source code (from the decomp project)) |
||
Line 45: | Line 45: | ||
The algorithm that returns the custom data that is associated with a coordinate is very simple. It consists of determining the correct row index in the table, then iterating over segments in that row and returning the segment data when the X coordinate is within its bounds. | The algorithm that returns the custom data that is associated with a coordinate is very simple. It consists of determining the correct row index in the table, then iterating over segments in that row and returning the segment data when the X coordinate is within its bounds. | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
s32 Ecosystem::getMapArea(const EcoMapInfo& info, f32 posX, f32 posZ) const { | |||
posX = sead::Mathf::clamp(posX, -5000.0f, 4999.0f); | |||
posZ = sead::Mathf::clamp(posZ, -4000.0f, 4000.0f); | |||
const auto epsilon = [](float n) { return n >= 0.0f ? 0.5f : -0.5f; }; | |||
s32 x = s32(posX + 5000.0f + epsilon(posX + 5000.0f)); | |||
s32 z = s32(posZ + 4000.0f + epsilon(posZ + 4000.0f)) / info.mHeader->divisor; | |||
s32 row = sead::Mathi::clamp(z, 0, info.mHeader->num_rows - 2); | |||
if (info.mHeader->divisor == 10) | |||
x /= 10; | |||
if (info.mRowOffsets[row] >= info.mRowOffsets[row + 1]) | |||
return -1; | |||
auto* segmentEnd = reinterpret_cast<const Segment*>(info.mRows + 2 * info.mRowOffsets[row + 1]); | |||
auto* segment = reinterpret_cast<const Segment*>(info.mRows + 2 * info.mRowOffsets[row]); | |||
s32 totalLength = 0; | |||
while (true) { | |||
totalLength += segment->length; | |||
if (x < totalLength) | |||
break; | |||
++segment; | |||
if (segment >= segmentEnd) | |||
return -1; | |||
} | |||
return segment->value; | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Source: https://github.com/zeldaret/botw/blob/022f029db1ef03d900fb54bad0807939c9695720/src/KingSystem/Ecosystem/ecoSystem.cpp#L100 | |||
== Usage in ''Breath of the Wild'' == | == Usage in ''Breath of the Wild'' == |