#include "Chunk.hpp"

#include <iostream>
#include <algorithm>
#include "../../core/data/math.hpp"

using namespace world;

Chunk::Chunk(std::istream& str, bool rle) {
    if(rle) {
        uint16_t i = 0;
        while(!str.eof()) {
            uint16_t count;
            Voxel voxel;
            str.read(reinterpret_cast<char *>(&count), sizeof(count));
            str.read(reinterpret_cast<char *>(&voxel), sizeof(voxel));
            str.peek();
            for (; count > 0; count--) {
                voxels[i] = voxel;
                i++;
            }
        }
        assert(i == CHUNK_SIZE && "Mismatch data length");
    } else {
        for(auto& voxel: voxels) {
            str.read(reinterpret_cast<char *>(&voxel), sizeof(voxel));
        }
    }
}
Chunk::~Chunk() { }

const Voxel &Chunk::getAt(const chunk_voxel_pos &pos) const {
    return get(glm::toIdx(pos));
}