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

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

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

52 lines
1.9 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;
Point first;
Point second;
Point third;
first.x = left;
first.y = cell->heights[0];
first.z = top;
second.x = right;
second.y = cell->heights[1];
second.z = top;
third.x = left;
third.y = cell->heights[2];
third.z = bottom;
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;
}