Skip to content

Commit

Permalink
[fix] Remove usage of static offsets in the "Fix" tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Essex authored and Jacob Essex committed Aug 3, 2024
1 parent d8adf1b commit 3c7683e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

### Fixed
- Removed use of static offsets when using the "Fix" tool to better support existing mods

## 0.5.2 - 2024-06-15

### Added
Expand Down
32 changes: 30 additions & 2 deletions src/logic/FixPosition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ void PositionUpdater::doFix(bool operationIsDelete) {

sendStatusUpdate(0, "Loaded all files");

int offsetProcess = 0;
std::map<std::string, std::map<int, int>> offsetCountsByObjId;
for (const auto& cellPair: esp.exteriorCells) {
auto& cell = cellPair.second;
logger->info("Getting offsets from cell: {}, {}", cell->getGridPosition().x, cell->getGridPosition().y);
sendStatusUpdate(static_cast<int>(offsetProcess++ / float(esp.exteriorCells.size()) * 100), "Getting offsets from cell: " + std::to_string(cell->getGridPosition().x) + ", " + std::to_string(cell->getGridPosition().y));

for (auto& reference: cell->references) {
auto landHeight = fc.getHeightAt(reference.position.x, reference.position.y);
auto offset = static_cast<int>(round(reference.position.z - landHeight));
offsetCountsByObjId[reference.name][offset]++;
}
}

logger->info("Calculating per object offsets");
sendStatusUpdate(0, "Calculating per object offsets");
std::map<std::string, int> offsetsByObjId;
for (const auto& offsetCountsForObj: offsetCountsByObjId) {
auto maxCount = std::max_element(
std::begin(offsetCountsForObj.second), std::end(offsetCountsForObj.second),
[](const auto& p1, const auto& p2) {
return p1.second < p2.second;
}
);
logger->info("\tOffset for {} is {}", offsetCountsForObj.first, maxCount->first);
offsetsByObjId[offsetCountsForObj.first] = maxCount->first;
}

int cellUpdateProgress = 0;
for (const auto& cellPair: esp.exteriorCells) {
auto& cell = cellPair.second;
Expand All @@ -60,7 +88,7 @@ void PositionUpdater::doFix(bool operationIsDelete) {

if (operationIsDelete) {
auto shouldDelete = [&](const CellReference& reference) {
auto expectedZ = fc.getHeightAt(reference.position.x, reference.position.y) + DEFAULT_GENERATION_OFFSET;
auto expectedZ = fc.getHeightAt(reference.position.x, reference.position.y) + offsetsByObjId[reference.name];
return fabs(expectedZ - reference.position.z) > MAX_ALLOWED_Z_ERROR;
};
auto removedEndIter = std::remove_if(cell->references.begin(), cell->references.end(), shouldDelete);
Expand All @@ -71,7 +99,7 @@ void PositionUpdater::doFix(bool operationIsDelete) {
);
} else {
for (auto& reference : cell->references) {
auto expectedZ = fc.getHeightAt(reference.position.x, reference.position.y) + DEFAULT_GENERATION_OFFSET;
auto expectedZ = fc.getHeightAt(reference.position.x, reference.position.y) + offsetsByObjId[reference.name];
if (fabs(expectedZ - reference.position.z) > MAX_ALLOWED_Z_ERROR) {
auto angle = fc.getAngleAt(reference.position.x, reference.position.y);
reference.position.z = expectedZ;
Expand Down

0 comments on commit 3c7683e

Please sign in to comment.