rodecomp/Source/Resource/World/GroundResource.cpp
ragnarok-clanker ffbeee0f2a Maintenance: Forced maintainer submission (resubmission of 14678 after td sync to 8deac7a3; replaces 14460 and 14473). Native-run fix
Batch #14681 by ragnarok-clanker: forced by an admin (6 lower score(s), 70 changed function(s)); checked with 555 compiled unit(s), including every includer of Source/Client/Mode/CLoginMode.h, Source/Client/Session.h, Source/Contents/Actors/GameActor.h, Source/Contents/Actors/GrannyGameActor.h, Source/Contents/Actors/Pc.h, Source/Contents/Actors/RenderObject.h, Source/Contents/EffectResourceSwitcher.h, Source/Contents/Effects/CartCannon.h, Source/Contents/Effects/EZ2STREffectEx.h, Source/Contents/Effects/EffectActor.h, Source/Contents/Effects/MonsterSkillEffects.h, Source/Contents/Effects/RagEffect.h, Source/Contents/Effects/RectUpEffect.h, Source/Contents/Ground/GroundGeometry.h, Source/Network/Connection/Connection.h, Source/Resource/Audio/WaveResource.h, Source/Resource/Bitmap/BitmapResource.h, Source/Resource/EZeffectResource.h, Source/Resource/EmblemResource.h, Source/Resource/GifResource.h, Source/Resource/ImfResource.h, Source/Resource/Models/AnimatedModelResource.h, Source/Resource/Models/GrannyModelResource.h, Source/Resource/Models/ModelResource.h, Source/Resource/PaletteResource.h, Source/Resource/RmaResource.h, Source/Resource/Sprites/ActionResource.h, Source/Resource/Sprites/SpriteResource.h, Source/Resource/World/GroundResource.h, Source/Resource/World/TerrainResource.h, Source/Resource/World/WorldResource.h, Source/UI/Buttons/UIBmpRadioBtn.h, Source/UI/Buttons/UITextButton.h, Source/UI/Item/UIItemPurchaseWnd.h, Source/UI/Item/UIItemSellWnd.h, Source/UI/Item/UIItemSellWnd2.h, Source/UI/Item/UIItemStoreWnd.h, Source/UI/Item/UIItemWnd.h, Source/UI/Item/UIParaItemShopWnd.h, Source/UI/Items/UIItemRepairWnd.h, Source/UI/Items/UIMakeTargetListWnd.h, Source/UI/Items/UIMakeTargetProcessWnd.h, Source/UI/Items/UIWeaponRefineWnd.h, Source/UI/Rodex/UIRodexInboxWnd.h, Source/UI/Shop/UIMerchantItemShopWnd.h, Source/UI/Shop/UIMerchantMirrorItemWnd.h, Source/UI/Shop/UIMerchantShopMakeWnd.h, Source/UI/StylingShop/UIStylingShopWnd.h, Source/UI/UIChangeMaterialWnd.h, Source/UI/UIIngredientInfoWnd.h, Source/UI/UIItemReformWnd.h, Source/UI/UILapineScrollBar.h, Source/UI/Window/UIHotKeyWnd.h, Source/UI/Window/UINewSelectCharWnd.h.
2026-11-07 14:05:21 +00:00

184 lines
5.8 KiB
C++

#include "Resource/World/GroundResource.h"
#include "Resource/ResourceMgr.h"
#include <cstring>
// OFFSET: 0x0074E1C0
// FUNCTION: fn_07906
CGndRes::CGndRes() : loaded(0), width(0), height(0), cellSize(10.0f) {
}
// OFFSET: 0x0074E390
// 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;
}
char magic[4];
this->m_file.Read(magic, sizeof(magic));
if (std::memcmp(magic, "GRGN", 4) != 0) {
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 std::uint32_t*>(this->m_file.ReadPtr(static_cast<std::uint32_t>(this->lightmapCount) << 4));
std::uint32_t count;
this->m_file.Read(&count, sizeof(count));
this->legacyLightmaps = static_cast<const GroundLegacyLightmap*>(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: 0x0074E4B0
// FUNCTION: fn_07909
void GroundLegacyLightmap::Decode(unsigned char* output) const {
for (int index = 0; index < 64; ++index) {
const int bit = index * 5;
const int shift = bit % 8;
const unsigned char* packed = this->bits + bit / 8;
const int mask = 248 >> shift;
unsigned char value;
if (shift < 4) {
value = static_cast<unsigned char>((packed[0] & mask) << shift);
} else {
value = static_cast<unsigned char>(((packed[0] & mask) << shift) + (((248 << (8 - shift)) & packed[1]) >> (8 - shift)));
}
output[index] = value;
}
}
// 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;
}
const std::uint32_t* indices = this->legacyLightmapIndices + 4 * index;
unsigned char brightness[64];
unsigned char red[64];
unsigned char green[64];
unsigned char blue[64];
this->legacyLightmaps[indices[0]].Decode(brightness);
this->legacyLightmaps[indices[1]].Decode(red);
this->legacyLightmaps[indices[2]].Decode(green);
this->legacyLightmaps[indices[3]].Decode(blue);
for (int pixel = 0; pixel < 64; ++pixel) {
const unsigned transposed = static_cast<unsigned>(pixel) / 8 + 8 * (pixel & 7);
output[transposed] = brightness[pixel];
output[64 + 3 * transposed] = red[pixel];
output[65 + 3 * transposed] = green[pixel];
output[66 + 3 * transposed] = blue[pixel];
}
}
// INLINED
CGndRes::CGndRes(const char* extension, const char* prefix) {
ResourceMgr::Instance().Register(extension, prefix, this);
}
// GLOBAL: gl_25556
CGndRes g_gndPrototype("gnd", "");