#include "assets/palette.hpp" #include "assets/rnc.hpp" #include "core/log.hpp" #include #include #include namespace top_octane::assets { bool Palette::load(const Vfs& vfs, const std::string& path) { std::vector raw, data; if (!vfs.read_file(path, raw)) { LOG_ERROR("Failed to read palette: %s", path.c_str()); return false; } if (!rnc_unpack(raw, data) || data.size() < 768) { LOG_ERROR("Invalid palette size in %s: %zu", path.c_str(), data.size()); return false; } for (int i = 0; i < 256; ++i) { // 6-bit VGA DAC (0..63) scaled cleanly to 0..255 uint8_t r6 = std::min(data[i * 3 + 0], 63); uint8_t g6 = std::min(data[i * 3 + 1], 63); uint8_t b6 = std::min(data[i * 3 + 2], 63); colors[i].r = static_cast((r6 << 2) | (r6 >> 4)); colors[i].g = static_cast((g6 << 2) | (g6 >> 4)); colors[i].b = static_cast((b6 << 2) | (b6 >> 4)); colors[i].a = (i == 0) ? 0 : 255; } return true; } uint8_t Palette::find_nearest(uint8_t r, uint8_t g, uint8_t b) const { uint8_t best = 0; int best_dist = 0x7FFFFFFF; for (int i = 0; i < 256; ++i) { int dr = int(colors[i].r) - int(r); int dg = int(colors[i].g) - int(g); int db = int(colors[i].b) - int(b); int d = dr * dr + dg * dg + db * db; if (d < best_dist) { best_dist = d; best = static_cast(i); } } return best; } bool ColorTables::load(const Vfs& vfs, int world_index, const Palette& pal) { char rel[64]; std::snprintf(rel, sizeof(rel), "DATA/TABLE0-%d.DAT", std::clamp(world_index, 0, 5)); std::vector raw, data; if (!vfs.read_file(rel, raw)) { LOG_ERROR("Failed to read color table: %s", rel); return false; } if (!rnc_unpack(raw, data) || data.size() < 83968) { LOG_ERROR("Invalid color table size in %s: %zu", rel, data.size()); return false; } std::memcpy(shade.data(), data.data(), shade.size()); std::memcpy(highlight.data(), data.data() + 0x4000, highlight.size()); std::memcpy(translucency.data(), data.data() + 0x4800, translucency.size()); // Sample row 0 (maximum distance/darkness) across several indices to extract the world's fog tint uint32_t r_sum = 0, g_sum = 0, b_sum = 0; constexpr int samples = 16; for (int i = 0; i < samples; ++i) { uint8_t idx = shade[i * 16 + 1]; r_sum += pal.colors[idx].r; g_sum += pal.colors[idx].g; b_sum += pal.colors[idx].b; } fog_color = Vec3( static_cast(r_sum) / (samples * 255.0f), static_cast(g_sum) / (samples * 255.0f), static_cast(b_sum) / (samples * 255.0f) ); return true; } } // namespace top_octane::assets