game: ensure loaded magica voxel model data fits in palette

This commit is contained in:
Crypto City 2021-09-18 21:11:29 +00:00
parent cf76b9fd3e
commit 86067d2576

View File

@ -2,6 +2,7 @@
#include "misc_log_ex.h"
#include "int-util.h"
#include "file_io_utils.h"
#include "cc/cc_palette.h"
#include "magica.h"
#define CHUNKID(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
@ -217,6 +218,28 @@ bool load_magica_data(std::list<VoxelModel> &mm, const std::string &data)
if (!read_chunk(mm, ss, &expected_chunk, chunk_children_bytes))
return false;
// ensure all blocks fall inside the palette
std::map<uint8_t, uint8_t> remap;
for (auto &m: mm)
{
for (uint8_t &e: m.data)
{
const auto i = remap.find(e);
if (i != remap.end())
{
e = i->second;
}
else
{
if (e >= m.palette.size())
{
m.palette.push_back(cc::BLOCK_VARIANT_SANDSTONE_BASIC);
e = remap[e] = m.palette.size() - 1;
}
}
}
}
MINFO(mm.size() << " Magica models loaded");
return true;
}