rodecomp/Source/Contents/Ground/TerrainPlane.cpp
ragnarok-clanker eb5857b71a Decompile fn_07840 (C3dAttr::PlaneHeight)
Batch #15760 by Yommy: 0 byte-identical, 1 confirmed by peer review.

- fn_07840 C3dAttr::PlaneHeight: logic confirmed by peer review

Peer-reviewed-by: eAthena, bgamez23
Accepted without human review: peers confirmed the logic ("same" ahead by at least 3, with 70% of the votes).
2026-11-10 11:41:53 +00:00

52 lines
2 KiB
C++

#include "Resource/World/TerrainResource.h"
// OFFSET: 0x00749040
// FUNCTION: fn_07840
float C3dAttr::PlaneHeight(float x, float z) const {
const float size = static_cast<float>(this->cellSize);
const int columns = this->width;
const int rows = this->height;
const int column = static_cast<int>(((static_cast<float>(columns) * size) * 0.5f + x) / size);
const int row = static_cast<int>(((static_cast<float>(rows) * size) * 0.5f + z) / size);
const TerrainHeightCell* cell = this->CellAt(column, row);
if (!cell) {
return 0.0f;
}
struct Point {
Point() : x(0), y(0), z(0) {
}
float x;
float y;
float z;
};
Point corners[4];
const int dx = column - columns / 2;
const int dz = row - rows / 2;
const float left = static_cast<float>(dx) * size;
const float right = static_cast<float>(dx + 1) * size;
const float top = static_cast<float>(dz) * size;
const float bottom = static_cast<float>(dz + 1) * size;
corners[0].x = left;
corners[0].y = cell->heights[0];
corners[0].z = top;
corners[1].x = right;
corners[1].y = cell->heights[1];
corners[1].z = top;
corners[2].x = left;
corners[2].y = cell->heights[2];
corners[2].z = bottom;
const Point& first = corners[0];
const Point& second = corners[1];
const Point& third = corners[2];
const float normalX = ((third.z - first.z) * second.y + (second.z - third.z) * first.y) + (first.z - second.z) * third.y;
const float normalZ = ((third.y - first.y) * second.x + (second.y - third.y) * first.x) + (first.y - second.y) * third.x;
const float constant = (((first.z * third.y - third.z * first.y) * second.x + (third.z * second.y - second.z * third.y) * first.x) +
(second.z * first.y - first.z * second.y) * third.x) *
-1.0f;
const float normalY = ((second.x - third.x) * first.z + (third.x - first.x) * second.z) + (first.x - second.x) * third.z;
return ((normalX * x + normalZ * z) + constant) * -1.0f / normalY;
}