2022-01-30 10:07:39 +00:00
|
|
|
/*
|
2024-08-12 18:32:27 +00:00
|
|
|
Copyright (C) 2015 - 2024 cc9cii
|
2022-01-30 10:07:39 +00:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
2024-08-12 18:32:27 +00:00
|
|
|
cc9cii cc9cii@hotmail.com
|
2022-01-30 10:07:39 +00:00
|
|
|
|
|
|
|
Much of the information on the data structures are based on the information
|
|
|
|
from Tes4Mod:Mod_File_Format and Tes5Mod:File_Formats but also refined by
|
|
|
|
trial & error. See http://en.uesp.net/wiki for details.
|
|
|
|
|
|
|
|
*/
|
|
|
|
#include "loadland.hpp"
|
|
|
|
|
2023-05-30 17:49:03 +00:00
|
|
|
#include <cstdint>
|
2024-08-12 18:32:27 +00:00
|
|
|
#include <cassert>
|
2022-01-30 10:07:39 +00:00
|
|
|
#include <stdexcept>
|
2024-08-12 18:32:27 +00:00
|
|
|
#include <iostream>
|
2022-01-30 10:07:39 +00:00
|
|
|
|
2023-05-26 08:02:51 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
|
2022-01-30 10:07:39 +00:00
|
|
|
#include "reader.hpp"
|
2023-05-14 22:34:17 +00:00
|
|
|
// #include "writer.hpp"
|
2022-01-30 10:07:39 +00:00
|
|
|
|
2024-08-12 18:32:27 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::uint32_t getDefaultTexture(bool isTES4, bool isFONV, bool isTES5)
|
|
|
|
{
|
|
|
|
// WARN: guessed for FO3/FONV (might be Dirt02)
|
|
|
|
if (isTES4)
|
|
|
|
return 0x000008C0; // TerrainHDDirt01.dds (LTEX)
|
|
|
|
else if (isFONV)
|
|
|
|
return 0x00000A0D; // Landscape\Dirt01.dds (TXST 0x00004453)
|
|
|
|
else if (isTES5)
|
|
|
|
return 0x00000C16; // Landscape\Dirt02.dds (TXST 0x00000C0F)
|
|
|
|
else // FO3
|
|
|
|
return 0x00000A0D; // Landscape\Dirt01.dds (prob. same as FONV)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:07:39 +00:00
|
|
|
// overlap north
|
|
|
|
//
|
|
|
|
// 32
|
|
|
|
// 31
|
|
|
|
// 30
|
|
|
|
// overlap .
|
|
|
|
// west .
|
|
|
|
// .
|
|
|
|
// 2
|
|
|
|
// 1
|
|
|
|
// 0
|
|
|
|
// 0 1 2 ... 30 31 32
|
|
|
|
//
|
|
|
|
// overlap south
|
|
|
|
//
|
|
|
|
void ESM4::Land::load(ESM4::Reader& reader)
|
|
|
|
{
|
2023-07-30 19:55:36 +00:00
|
|
|
mId = reader.getFormIdFromHeader();
|
2022-01-30 10:07:39 +00:00
|
|
|
mFlags = reader.hdr().record.flags;
|
2024-08-12 18:32:27 +00:00
|
|
|
|
|
|
|
std::uint32_t esmVer = reader.esmVersion();
|
|
|
|
bool isTES4 = (esmVer == ESM::VER_080 || esmVer == ESM::VER_100);
|
|
|
|
bool isFONV = (esmVer == ESM::VER_132 || esmVer == ESM::VER_133 || esmVer == ESM::VER_134);
|
|
|
|
bool isTES5 = (esmVer == ESM::VER_094 || esmVer == ESM::VER_170); // WARN: FO3 is also VER_094
|
|
|
|
// WARN: below workaround assumes the data directory path has "Fallout" somewhere
|
2024-08-12 18:38:44 +00:00
|
|
|
//if (esmVer == ESM::VER_094 && reader.getContext().filename.find("allout") != std::string::npos)
|
|
|
|
// isTES5 = false; // FIXME: terrible hack
|
2024-08-12 18:32:27 +00:00
|
|
|
|
2022-05-01 12:46:47 +00:00
|
|
|
mDataTypes = 0;
|
2023-07-30 19:55:36 +00:00
|
|
|
mCell = reader.currCell();
|
2022-01-30 10:07:39 +00:00
|
|
|
TxtLayer layer;
|
|
|
|
std::int8_t currentAddQuad = -1; // for VTXT following ATXT
|
|
|
|
|
|
|
|
while (reader.getSubRecordHeader())
|
|
|
|
{
|
|
|
|
const ESM4::SubRecordHeader& subHdr = reader.subRecordHeader();
|
|
|
|
switch (subHdr.typeId)
|
|
|
|
{
|
2024-03-18 00:21:57 +00:00
|
|
|
case ESM::fourCC("DATA"):
|
2022-01-30 10:07:39 +00:00
|
|
|
{
|
|
|
|
reader.get(mLandFlags);
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 00:21:57 +00:00
|
|
|
case ESM::fourCC("VNML"): // vertex normals, 33x33x(1+1+1) = 3267
|
2022-01-30 10:07:39 +00:00
|
|
|
{
|
|
|
|
reader.get(mVertNorm);
|
|
|
|
mDataTypes |= LAND_VNML;
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 00:21:57 +00:00
|
|
|
case ESM::fourCC("VHGT"): // vertex height gradient, 4+33x33+3 = 4+1089+3 = 1096
|
2022-01-30 10:07:39 +00:00
|
|
|
{
|
|
|
|
reader.get(mHeightMap);
|
|
|
|
mDataTypes |= LAND_VHGT;
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 00:21:57 +00:00
|
|
|
case ESM::fourCC("VCLR"): // vertex colours, 24bit RGB, 33x33x(1+1+1) = 3267
|
2022-01-30 10:07:39 +00:00
|
|
|
{
|
|
|
|
reader.get(mVertColr);
|
|
|
|
mDataTypes |= LAND_VCLR;
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 00:21:57 +00:00
|
|
|
case ESM::fourCC("BTXT"):
|
2022-01-30 10:07:39 +00:00
|
|
|
{
|
|
|
|
BTXT base;
|
|
|
|
if (reader.getExact(base))
|
|
|
|
{
|
2023-05-27 14:07:04 +00:00
|
|
|
if (base.quadrant >= 4)
|
|
|
|
throw std::runtime_error("base texture quadrant index error");
|
2022-01-30 10:07:39 +00:00
|
|
|
|
|
|
|
reader.adjustFormId(base.formId);
|
|
|
|
mTextures[base.quadrant].base = std::move(base);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 00:21:57 +00:00
|
|
|
case ESM::fourCC("ATXT"):
|
2022-01-30 10:07:39 +00:00
|
|
|
{
|
|
|
|
if (currentAddQuad != -1)
|
|
|
|
{
|
2024-08-12 18:32:27 +00:00
|
|
|
// NOTE: sometimes there are no VTXT following an ATXT
|
|
|
|
//Log(Debug::Verbose) << "ESM4::Land VTXT empty layer " << layer.texture.layerIndex
|
|
|
|
//<< " FormId " << ESM::FormId::toString(mFormId) << std::endl;
|
|
|
|
if (!layer.texture.formId)
|
|
|
|
layer.texture.formId = getDefaultTexture(isTES4, isFONV, isTES5);
|
|
|
|
|
|
|
|
layer.data.resize(1); // just one spot
|
|
|
|
layer.data.back().position = 0; // this corner
|
|
|
|
layer.data.back().opacity = 0.f; // transparent
|
|
|
|
|
|
|
|
assert(layer.texture.layerIndex == mTextures[currentAddQuad].layers.size()
|
|
|
|
&& "additional texture skipping layer");
|
|
|
|
|
2022-01-30 10:07:39 +00:00
|
|
|
mTextures[currentAddQuad].layers.push_back(layer);
|
|
|
|
}
|
2024-08-12 18:32:27 +00:00
|
|
|
|
2022-01-30 10:07:39 +00:00
|
|
|
reader.get(layer.texture);
|
|
|
|
reader.adjustFormId(layer.texture.formId);
|
2023-05-27 14:07:04 +00:00
|
|
|
if (layer.texture.quadrant >= 4)
|
|
|
|
throw std::runtime_error("additional texture quadrant index error");
|
2024-08-12 18:32:27 +00:00
|
|
|
|
2022-01-30 10:07:39 +00:00
|
|
|
currentAddQuad = layer.texture.quadrant;
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 00:21:57 +00:00
|
|
|
case ESM::fourCC("VTXT"):
|
2022-01-30 10:07:39 +00:00
|
|
|
{
|
2023-05-27 14:07:04 +00:00
|
|
|
if (currentAddQuad == -1)
|
|
|
|
throw std::runtime_error("VTXT without ATXT found");
|
2022-01-30 10:07:39 +00:00
|
|
|
|
2023-08-12 12:01:28 +00:00
|
|
|
const std::uint16_t count = reader.subRecordHeader().dataSize / sizeof(ESM4::Land::VTXT);
|
2023-05-27 14:07:04 +00:00
|
|
|
if ((reader.subRecordHeader().dataSize % sizeof(ESM4::Land::VTXT)) != 0)
|
|
|
|
throw std::runtime_error("ESM4::LAND VTXT data size error");
|
2022-01-30 10:07:39 +00:00
|
|
|
|
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
layer.data.resize(count);
|
|
|
|
std::vector<ESM4::Land::VTXT>::iterator it = layer.data.begin();
|
|
|
|
for (; it != layer.data.end(); ++it)
|
|
|
|
reader.get(*it);
|
|
|
|
}
|
|
|
|
|
2024-08-12 18:32:27 +00:00
|
|
|
assert(layer.texture.layerIndex == mTextures[currentAddQuad].layers.size()
|
|
|
|
&& "additional texture skipping layer");
|
|
|
|
|
|
|
|
mTextures[currentAddQuad].layers.push_back(layer);
|
2022-01-30 10:07:39 +00:00
|
|
|
|
|
|
|
currentAddQuad = -1;
|
|
|
|
layer.data.clear();
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 00:21:57 +00:00
|
|
|
case ESM::fourCC("VTEX"): // only in Oblivion?
|
2022-01-30 10:07:39 +00:00
|
|
|
{
|
2023-08-12 12:01:28 +00:00
|
|
|
const std::uint16_t count = reader.subRecordHeader().dataSize / sizeof(ESM::FormId32);
|
2023-07-30 19:55:36 +00:00
|
|
|
if ((reader.subRecordHeader().dataSize % sizeof(ESM::FormId32)) != 0)
|
2023-05-27 14:07:04 +00:00
|
|
|
throw std::runtime_error("ESM4::LAND VTEX data size error");
|
2022-01-30 10:07:39 +00:00
|
|
|
|
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
mIds.resize(count);
|
2023-07-30 19:55:36 +00:00
|
|
|
for (ESM::FormId& id : mIds)
|
|
|
|
reader.getFormId(id);
|
2022-01-30 10:07:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 00:21:57 +00:00
|
|
|
case ESM::fourCC("MPCD"): // FO4
|
2023-08-16 20:06:46 +00:00
|
|
|
reader.skipSubRecordData();
|
|
|
|
break;
|
2022-01-30 10:07:39 +00:00
|
|
|
default:
|
|
|
|
throw std::runtime_error("ESM4::LAND::load - Unknown subrecord " + ESM::printName(subHdr.typeId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 18:32:27 +00:00
|
|
|
//if (mCell.toUint32() == 0x00005e1f)
|
|
|
|
//std::cout << "vilverin exterior" << std::endl;
|
|
|
|
|
2022-01-30 10:07:39 +00:00
|
|
|
if (currentAddQuad != -1)
|
|
|
|
{
|
2024-08-12 18:32:27 +00:00
|
|
|
// not sure if it happens here as well, if so just ignore
|
2023-08-12 12:01:28 +00:00
|
|
|
Log(Debug::Verbose) << "ESM4::Land VTXT empty layer " << layer.texture.layerIndex << " quad "
|
|
|
|
<< static_cast<unsigned>(layer.texture.quadrant);
|
2022-01-30 10:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
2024-08-12 18:32:27 +00:00
|
|
|
// just use some defaults
|
2023-04-22 15:07:54 +00:00
|
|
|
if (mTextures[i].base.formId == 0)
|
2024-08-12 18:32:27 +00:00
|
|
|
mTextures[i].base.formId = getDefaultTexture(isTES4, isFONV, isTES5);
|
2022-01-30 10:07:39 +00:00
|
|
|
}
|
2023-05-15 21:48:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 10:07:39 +00:00
|
|
|
// void ESM4::Land::save(ESM4::Writer& writer) const
|
|
|
|
//{
|
|
|
|
// }
|
|
|
|
|
|
|
|
// void ESM4::Land::blank()
|
|
|
|
//{
|
|
|
|
// }
|