Batch #1084 by ragnarok-clanker: 0 byte-identical, 8 confirmed by peer review. - fn_07899 CGndRes::Create: logic confirmed by peer review - fn_07900 CGndRes::Load: logic confirmed by peer review - fn_07906 CGndRes::CGndRes: logic confirmed by peer review - fn_07911 CGndRes::Parse: logic confirmed by peer review - fn_07912 CGndRes::ReadWater: logic confirmed by peer review - fn_07828 C3dAttr::Create: logic confirmed by peer review - fn_07829 C3dAttr::Load: logic confirmed by peer review - fn_07833 C3dAttr::C3dAttr: logic confirmed by peer review Reviewed-by: ragnarok-clanker
165 lines
5.3 KiB
C++
165 lines
5.3 KiB
C++
#include "Resource/World/GroundResource.h"
|
|
|
|
#include <cstring>
|
|
|
|
// OFFSET: 0x0074E1C0
|
|
// FUNCTION: fn_07906
|
|
CGndRes::CGndRes() : loaded(0), width(0), height(0), cellSize(10.0f) {
|
|
}
|
|
|
|
// OFFSET: 0x0074E390
|
|
// OFFSET: 0x0074DD10
|
|
// FUNCTION: fn_07908
|
|
CGndRes::~CGndRes() = default;
|
|
|
|
// OFFSET: 0x0074DB10
|
|
// FUNCTION: fn_07899
|
|
CGndRes* CGndRes::Create() {
|
|
return new CGndRes;
|
|
}
|
|
|
|
// OFFSET: 0x0074DC80
|
|
// FUNCTION: fn_07900
|
|
bool CGndRes::Load(const char* name) {
|
|
if (!this->m_file.Open(name, 0, true, false)) {
|
|
return false;
|
|
}
|
|
|
|
std::uint32_t magic;
|
|
this->m_file.Read(&magic, sizeof(magic));
|
|
|
|
if (magic != 0x4E475247u) {
|
|
return false;
|
|
}
|
|
|
|
this->loaded = 1;
|
|
return this->Parse();
|
|
}
|
|
|
|
// OFFSET: 0x0074E650
|
|
// FUNCTION: fn_07911
|
|
bool CGndRes::Parse() {
|
|
this->m_file.Read(&this->major, 1);
|
|
this->m_file.Read(&this->minor, 1);
|
|
|
|
if (this->major != 1 || this->minor <= 6) {
|
|
return false;
|
|
}
|
|
|
|
this->m_file.Read(&this->width, sizeof(this->width));
|
|
this->m_file.Read(&this->height, sizeof(this->height));
|
|
this->m_file.Read(&this->cellSize, sizeof(this->cellSize));
|
|
this->m_file.Read(&this->textureCount, sizeof(this->textureCount));
|
|
std::uint32_t nameBytes;
|
|
this->m_file.Read(&nameBytes, sizeof(nameBytes));
|
|
this->textureNames.resize(static_cast<std::uint32_t>(this->textureCount));
|
|
|
|
for (int index = 0; index < this->textureCount; ++index) {
|
|
this->textureNames[index] = static_cast<const char*>(this->m_file.ReadPtr(nameBytes));
|
|
}
|
|
|
|
this->m_file.Read(&this->lightmapCount, sizeof(this->lightmapCount));
|
|
std::uint32_t unused[3];
|
|
this->m_file.Read(&unused[0], sizeof(unused[0]));
|
|
this->m_file.Read(&unused[1], sizeof(unused[1]));
|
|
this->m_file.Read(&unused[2], sizeof(unused[2]));
|
|
if (this->minor >= 7) {
|
|
this->lightmaps = static_cast<const unsigned char*>(this->m_file.ReadPtr(static_cast<std::uint32_t>(this->lightmapCount) << 8));
|
|
} else {
|
|
this->legacyLightmapIndices = static_cast<const unsigned char*>(this->m_file.ReadPtr(static_cast<std::uint32_t>(this->lightmapCount) << 4));
|
|
std::uint32_t count;
|
|
this->m_file.Read(&count, sizeof(count));
|
|
this->legacyLightmapBytes = static_cast<const unsigned char*>(this->m_file.ReadPtr(40u * count));
|
|
}
|
|
this->m_file.Read(&this->tileCount, sizeof(this->tileCount));
|
|
this->tiles = static_cast<const unsigned char*>(this->m_file.ReadPtr(40u * static_cast<std::uint32_t>(this->tileCount)));
|
|
this->cells = static_cast<const unsigned char*>(this->m_file.ReadPtr(28u * static_cast<std::uint32_t>(this->width) * static_cast<std::uint32_t>(this->height)));
|
|
|
|
if (this->minor >= 8) {
|
|
this->ReadWater();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// OFFSET: 0x0074E870
|
|
// FUNCTION: fn_07912
|
|
void CGndRes::ReadWater() {
|
|
GroundWater record;
|
|
this->m_file.Read(&record.parameters, sizeof(record.parameters));
|
|
this->m_file.Read(&this->waterWidth, sizeof(this->waterWidth));
|
|
this->m_file.Read(&this->waterHeight, sizeof(this->waterHeight));
|
|
const float count = static_cast<float>(this->waterWidth * this->waterHeight);
|
|
const auto reserveCount = count < 0.0f ? static_cast<std::uint32_t>(-1) : static_cast<std::uint32_t>(count);
|
|
this->waterHeights.reserve(reserveCount);
|
|
this->water.reserve(reserveCount);
|
|
|
|
if (this->major != 1) {
|
|
return;
|
|
}
|
|
|
|
if (this->minor == 8) {
|
|
this->waterDefault = record.parameters;
|
|
float level = 0.0f;
|
|
|
|
for (int index = 0; static_cast<float>(index) < count; ++index) {
|
|
this->m_file.Read(&level, sizeof(level));
|
|
this->waterHeights.push_back(level);
|
|
record.parameters.level = level;
|
|
std::memset(record.unknown_0018, 0, sizeof(record.unknown_0018));
|
|
this->water.push_back(record);
|
|
}
|
|
} else if (this->minor == 9) {
|
|
for (int index = 0; static_cast<float>(index) < count; ++index) {
|
|
this->m_file.Read(&record.parameters, sizeof(record.parameters));
|
|
std::memset(record.unknown_0018, 0, sizeof(record.unknown_0018));
|
|
this->water.push_back(record);
|
|
this->waterHeights.push_back(record.parameters.level);
|
|
}
|
|
}
|
|
}
|
|
|
|
// OFFSET: 0x0074DCE0
|
|
// FUNCTION: fn_07901
|
|
void CGndRes::Unload() {
|
|
this->textureNames.clear();
|
|
this->lightmaps = nullptr;
|
|
this->tiles = nullptr;
|
|
this->cells = nullptr;
|
|
}
|
|
|
|
// OFFSET: 0x0074E540
|
|
// FUNCTION: fn_07910
|
|
void CGndRes::Lightmap(int index, unsigned char* output) const {
|
|
if (this->minor >= 7) {
|
|
std::memcpy(output, this->lightmaps + (static_cast<std::uint32_t>(index) << 8), 256);
|
|
return;
|
|
}
|
|
|
|
std::uint32_t indices[4];
|
|
std::memcpy(indices, this->legacyLightmapIndices + 16u * static_cast<std::uint32_t>(index), sizeof(indices));
|
|
unsigned char decoded[4][64];
|
|
|
|
for (unsigned channel = 0; channel < 4; ++channel) {
|
|
const unsigned char* packed = this->legacyLightmapBytes + 40u * indices[channel];
|
|
|
|
for (unsigned bit = 0, pixel = 0; bit < 320; bit += 5, ++pixel) {
|
|
const unsigned shift = bit % 8;
|
|
unsigned value = ((248u >> shift) & packed[bit / 8]) << shift;
|
|
|
|
if (shift >= 4) {
|
|
value += ((248u << (8 - shift)) & packed[bit / 8 + 1]) >> (8 - shift);
|
|
}
|
|
|
|
decoded[channel][pixel] = static_cast<unsigned char>(value);
|
|
}
|
|
}
|
|
|
|
for (unsigned pixel = 0; pixel < 64; ++pixel) {
|
|
const unsigned transposed = pixel / 8 + 8 * (pixel & 7);
|
|
output[transposed] = decoded[0][pixel];
|
|
output[64 + 3 * transposed] = decoded[1][pixel];
|
|
output[65 + 3 * transposed] = decoded[2][pixel];
|
|
output[66 + 3 * transposed] = decoded[3][pixel];
|
|
}
|
|
}
|