2011-04-06 16:11:08 +00:00
|
|
|
#include "loadland.hpp"
|
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
#include "esm_reader.hpp"
|
|
|
|
#include "esm_writer.hpp"
|
|
|
|
|
2011-04-06 16:11:08 +00:00
|
|
|
namespace ESM
|
|
|
|
{
|
2012-04-01 19:29:49 +00:00
|
|
|
|
2012-04-06 19:04:30 +00:00
|
|
|
void Land::LandData::save(ESMWriter &esm)
|
|
|
|
{
|
|
|
|
// TODO: Make this actually work.
|
|
|
|
|
2012-09-20 16:33:30 +00:00
|
|
|
if (mDataTypes & Land::DATA_VNML) {
|
|
|
|
esm.writeHNT("VNML", mNormals, sizeof(VNML));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDataTypes & Land::DATA_VHGT) {
|
|
|
|
VHGT offsets;
|
|
|
|
offsets.mHeightOffset = mHeights[0] / HEIGHT_SCALE;
|
|
|
|
offsets.mUnknown1 = mUnk1;
|
|
|
|
offsets.mUnknown2 = mUnk2;
|
|
|
|
|
|
|
|
float prevY = mHeights[0], prevX;
|
|
|
|
int number = 0; // avoid multiplication
|
|
|
|
for (int i = 0; i < LAND_SIZE; ++i) {
|
|
|
|
float diff = (mHeights[number] - prevY) / HEIGHT_SCALE;
|
|
|
|
offsets.mHeightData[number] =
|
|
|
|
(diff >= 0) ? (int8_t) (diff + 0.5) : (int8_t) (diff - 0.5);
|
|
|
|
|
|
|
|
prevX = prevY = mHeights[number];
|
|
|
|
++number;
|
|
|
|
|
|
|
|
for (int j = 1; j < LAND_SIZE; ++j) {
|
|
|
|
diff = (mHeights[number] - prevX) / HEIGHT_SCALE;
|
|
|
|
offsets.mHeightData[number] =
|
|
|
|
(diff >= 0) ? (int8_t) (diff + 0.5) : (int8_t) (diff - 0.5);
|
|
|
|
|
|
|
|
prevX = mHeights[number];
|
|
|
|
++number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
esm.writeHNT("VHGT", offsets, sizeof(VHGT));
|
|
|
|
}
|
2012-06-11 18:02:03 +00:00
|
|
|
//esm.writeHNT("WNAM", 0, 81);
|
2012-09-20 16:33:30 +00:00
|
|
|
/* esm.startSubRecord("WNAM");
|
2012-06-11 18:02:03 +00:00
|
|
|
for (int i = 0; i < 81; i++)
|
|
|
|
esm.writeT((char)0x80, 1);
|
|
|
|
esm.endRecord("WNAM");
|
2012-09-20 16:33:30 +00:00
|
|
|
*/
|
|
|
|
if (mDataTypes & Land::DATA_WNAM) {
|
|
|
|
esm.writeHNT("WNAM", mWnam, 81);
|
|
|
|
}
|
2012-06-11 18:02:03 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
if (mDataTypes & Land::DATA_VCLR)
|
|
|
|
esm.writeHNT("VCLR", mColours, 3*LAND_NUM_VERTS);
|
2012-09-20 16:33:30 +00:00
|
|
|
if (mDataTypes & Land::DATA_VTEX) {
|
|
|
|
uint16_t vtex[256];
|
|
|
|
|
|
|
|
int readPos = 0; //bit ugly, but it works
|
|
|
|
for ( int y1 = 0; y1 < 4; y1++ )
|
|
|
|
for ( int x1 = 0; x1 < 4; x1++ )
|
|
|
|
for ( int y2 = 0; y2 < 4; y2++)
|
|
|
|
for ( int x2 = 0; x2 < 4; x2++ )
|
|
|
|
vtex[(y1*4+y2)*16+(x1*4+x2)] = mTextures[readPos++];
|
|
|
|
|
|
|
|
esm.writeHNT("VTEX", vtex, 512);
|
|
|
|
}
|
2012-04-06 19:04:30 +00:00
|
|
|
}
|
|
|
|
|
2012-04-01 19:29:49 +00:00
|
|
|
Land::Land()
|
2012-09-17 07:37:50 +00:00
|
|
|
: mFlags(0)
|
|
|
|
, mX(0)
|
|
|
|
, mY(0)
|
2012-04-01 19:29:49 +00:00
|
|
|
, mEsm(NULL)
|
2012-06-11 18:02:03 +00:00
|
|
|
// , hasData(false)
|
2012-09-17 07:37:50 +00:00
|
|
|
, mDataTypes(0)
|
|
|
|
, mDataLoaded(false)
|
|
|
|
, mLandData(NULL)
|
2012-04-01 19:29:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-04 19:05:19 +00:00
|
|
|
Land::~Land()
|
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
delete mLandData;
|
2012-04-04 19:05:19 +00:00
|
|
|
}
|
|
|
|
|
2011-04-06 16:11:08 +00:00
|
|
|
void Land::load(ESMReader &esm)
|
|
|
|
{
|
2012-03-27 08:20:22 +00:00
|
|
|
mEsm = &esm;
|
|
|
|
|
2011-04-06 16:11:08 +00:00
|
|
|
// Get the grid location
|
|
|
|
esm.getSubNameIs("INTV");
|
|
|
|
esm.getSubHeaderIs(8);
|
2012-09-17 07:37:50 +00:00
|
|
|
esm.getT<int>(mX);
|
|
|
|
esm.getT<int>(mY);
|
2011-04-06 16:11:08 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
esm.getHNT(mFlags, "DATA");
|
2011-04-06 16:11:08 +00:00
|
|
|
|
|
|
|
// Store the file position
|
2012-09-17 07:37:50 +00:00
|
|
|
mContext = esm.getContext();
|
2011-04-06 16:11:08 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
mHasData = false;
|
2011-04-06 16:11:08 +00:00
|
|
|
|
|
|
|
// Skip these here. Load the actual data when the cell is loaded.
|
2012-01-21 16:59:08 +00:00
|
|
|
if (esm.isNextSub("VNML"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(12675);
|
2012-09-17 07:37:50 +00:00
|
|
|
mDataTypes |= DATA_VNML;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
if (esm.isNextSub("VHGT"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(4232);
|
2012-09-17 07:37:50 +00:00
|
|
|
mDataTypes |= DATA_VHGT;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
if (esm.isNextSub("WNAM"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(81);
|
2012-09-17 07:37:50 +00:00
|
|
|
mDataTypes |= DATA_WNAM;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
if (esm.isNextSub("VCLR"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(12675);
|
2012-09-17 07:37:50 +00:00
|
|
|
mDataTypes |= DATA_VCLR;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
if (esm.isNextSub("VTEX"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(512);
|
2012-09-17 07:37:50 +00:00
|
|
|
mDataTypes |= DATA_VTEX;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
2011-04-06 16:11:08 +00:00
|
|
|
|
|
|
|
// We need all three of VNML, VHGT and VTEX in order to use the
|
2012-06-11 18:02:03 +00:00
|
|
|
// landscape. (Though Morrowind seems to accept terrain without VTEX/VCLR entries)
|
2012-09-20 16:33:30 +00:00
|
|
|
mHasData = mDataTypes & (DATA_VNML|DATA_VHGT|DATA_WNAM|DATA_VTEX|DATA_VCLR);
|
2012-01-21 16:59:08 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
mDataLoaded = false;
|
|
|
|
mLandData = NULL;
|
2011-04-06 16:11:08 +00:00
|
|
|
}
|
2012-01-21 16:59:08 +00:00
|
|
|
|
2012-04-06 19:04:30 +00:00
|
|
|
void Land::save(ESMWriter &esm)
|
|
|
|
{
|
2012-04-08 15:04:52 +00:00
|
|
|
esm.startSubRecord("INTV");
|
2012-09-17 07:37:50 +00:00
|
|
|
esm.writeT(mX);
|
|
|
|
esm.writeT(mY);
|
2012-04-08 15:04:52 +00:00
|
|
|
esm.endRecord("INTV");
|
2012-04-06 19:04:30 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
esm.writeHNT("DATA", mFlags);
|
2012-04-06 19:04:30 +00:00
|
|
|
|
2012-04-29 07:26:03 +00:00
|
|
|
// TODO: Land!
|
2012-09-17 07:37:50 +00:00
|
|
|
bool wasLoaded = mDataLoaded;
|
|
|
|
if (mHasData)
|
2012-04-06 19:04:30 +00:00
|
|
|
loadData(); // I think it might be a good idea to have
|
|
|
|
// the data loaded before trying to save it
|
2012-04-29 07:26:03 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
if (mDataLoaded)
|
|
|
|
mLandData->save(esm);
|
2012-04-29 07:26:03 +00:00
|
|
|
|
|
|
|
if (!wasLoaded)
|
2012-06-11 18:02:03 +00:00
|
|
|
unloadData(); // Don't need to keep the data loaded if it wasn't already
|
2011-04-06 16:11:08 +00:00
|
|
|
}
|
2012-01-21 16:59:08 +00:00
|
|
|
|
2012-03-27 08:20:22 +00:00
|
|
|
void Land::loadData()
|
2012-01-21 16:59:08 +00:00
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
if (mDataLoaded)
|
2012-01-21 16:59:08 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
mLandData = new LandData;
|
2012-04-04 19:05:19 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
if (mHasData)
|
2012-01-21 16:59:08 +00:00
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
mEsm->restoreContext(mContext);
|
2012-01-21 16:59:08 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
memset(mLandData->mNormals, 0, LAND_NUM_VERTS * 3);
|
2012-06-11 18:02:03 +00:00
|
|
|
|
2012-09-20 16:33:30 +00:00
|
|
|
if (mEsm->isNextSub("VNML")) {
|
|
|
|
mEsm->getHExact(mLandData->mNormals, sizeof(VNML));
|
|
|
|
}/*
|
2012-03-27 08:20:22 +00:00
|
|
|
if (mEsm->isNextSub("VNML"))
|
2012-01-21 16:59:08 +00:00
|
|
|
{
|
2012-03-27 08:20:22 +00:00
|
|
|
mEsm->skipHSubSize(12675);
|
2012-09-20 16:33:30 +00:00
|
|
|
} */
|
2012-01-21 16:59:08 +00:00
|
|
|
|
2012-09-20 16:33:30 +00:00
|
|
|
if (mEsm->isNextSub("VHGT")) {
|
2012-01-21 16:59:08 +00:00
|
|
|
VHGT rawHeights;
|
|
|
|
|
2012-09-20 16:33:30 +00:00
|
|
|
mEsm->getHExact(&rawHeights, sizeof(VHGT));
|
|
|
|
float currentHeightOffset = rawHeights.mHeightOffset;
|
2012-01-21 16:59:08 +00:00
|
|
|
for (int y = 0; y < LAND_SIZE; y++)
|
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
currentHeightOffset += rawHeights.mHeightData[y * LAND_SIZE];
|
|
|
|
mLandData->mHeights[y * LAND_SIZE] = currentHeightOffset * HEIGHT_SCALE;
|
2012-01-21 16:59:08 +00:00
|
|
|
|
2012-09-20 16:33:30 +00:00
|
|
|
float tempOffset = currentHeightOffset;
|
2012-01-21 16:59:08 +00:00
|
|
|
for (int x = 1; x < LAND_SIZE; x++)
|
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
tempOffset += rawHeights.mHeightData[y * LAND_SIZE + x];
|
|
|
|
mLandData->mHeights[x + y * LAND_SIZE] = tempOffset * HEIGHT_SCALE;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-20 16:33:30 +00:00
|
|
|
mLandData->mUnk1 = rawHeights.mUnknown1;
|
|
|
|
mLandData->mUnk2 = rawHeights.mUnknown2;
|
|
|
|
}
|
2012-01-21 17:59:12 +00:00
|
|
|
|
2012-03-27 08:20:22 +00:00
|
|
|
if (mEsm->isNextSub("WNAM"))
|
2012-01-21 17:59:12 +00:00
|
|
|
{
|
2012-09-20 16:33:30 +00:00
|
|
|
mEsm->getHExact(mLandData->mWnam, 81);
|
2012-01-21 17:59:12 +00:00
|
|
|
}
|
2012-03-27 08:20:22 +00:00
|
|
|
if (mEsm->isNextSub("VCLR"))
|
2012-01-21 17:59:12 +00:00
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
mLandData->mUsingColours = true;
|
|
|
|
mEsm->getHExact(&mLandData->mColours, 3*LAND_NUM_VERTS);
|
2012-02-29 23:05:22 +00:00
|
|
|
}else{
|
2012-09-17 07:37:50 +00:00
|
|
|
mLandData->mUsingColours = false;
|
2012-01-21 17:59:12 +00:00
|
|
|
}
|
2012-06-11 18:02:03 +00:00
|
|
|
if (mEsm->isNextSub("VTEX"))
|
|
|
|
{
|
|
|
|
//TODO fix magic numbers
|
2012-09-20 16:33:30 +00:00
|
|
|
uint16_t vtex[256];
|
2012-06-11 18:02:03 +00:00
|
|
|
mEsm->getHExact(&vtex, 512);
|
|
|
|
|
|
|
|
int readPos = 0; //bit ugly, but it works
|
|
|
|
for ( int y1 = 0; y1 < 4; y1++ )
|
|
|
|
for ( int x1 = 0; x1 < 4; x1++ )
|
|
|
|
for ( int y2 = 0; y2 < 4; y2++)
|
|
|
|
for ( int x2 = 0; x2 < 4; x2++ )
|
2012-09-17 07:37:50 +00:00
|
|
|
mLandData->mTextures[(y1*4+y2)*16+(x1*4+x2)] = vtex[readPos++];
|
2012-01-21 17:59:12 +00:00
|
|
|
}
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
mLandData->mUsingColours = false;
|
2012-09-20 16:33:30 +00:00
|
|
|
memset(mLandData->mTextures, 0, sizeof(mLandData->mTextures));
|
2012-01-21 16:59:08 +00:00
|
|
|
for (int i = 0; i < LAND_NUM_VERTS; i++)
|
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
mLandData->mHeights[i] = -256.0f * HEIGHT_SCALE;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
mLandData->mDataTypes = mDataTypes;
|
|
|
|
mDataLoaded = true;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-04 19:05:19 +00:00
|
|
|
void Land::unloadData()
|
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
if (mDataLoaded)
|
2012-04-04 19:05:19 +00:00
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
delete mLandData;
|
|
|
|
mLandData = NULL;
|
|
|
|
mDataLoaded = false;
|
2012-04-04 19:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 16:11:08 +00:00
|
|
|
}
|