2// Created by DexrnZacAttack on 11/28/25 using zPc-i2.
4#ifndef PALLETIZEDSTORAGE_TPP
5#define PALLETIZEDSTORAGE_TPP
7// TODO we need to document this whole file since I don't really understand the logic behind it
10PalletizedStorage<T>::PalletizedStorage(const int capacity) : m_capacity(capacity) {
15PalletizedStorage<T>::PalletizedStorage(const int capacity, T fillWith) : m_capacity(capacity) {
20void PalletizedStorage<T>::setValue(const int index, T value) {
21 int idx = getPaletteIndex(index);
22 PaletteEntry &cur = m_palette[idx];
24 if (cur.references <= 0)
25 throw exception::InvalidPaletteReferenceCountException();
27 // we are overwriting the value at the index, therefore the object already present at this index is no longer referenced
30 // is it already in the palette
31 for (int i = 0; i < m_palette.size(); i++) {
32 if (PaletteEntry &e = m_palette[i]; e.value == value) {
33 setPaletteIndex(index, i);
39 // can we overwrite the current entry
40 if (cur.references == 0) {
49 // otherwise we need to get an entry and emplace
50 int n = obtainPaletteEntry();
56 setPaletteIndex(index, n);
61T PalletizedStorage<T>::getCopy(const int index) const {
62 return m_palette[getPaletteIndex(index)].value;
66const T &PalletizedStorage<T>::getValue(const int index) const {
67 return m_palette[getPaletteIndex(index)].value;
71size_t PalletizedStorage<T>::getIndicesSize() const noexcept {
72 return (m_capacity * m_indexBits + 7) >> 3;
76const T & PalletizedStorage<T>::operator[](const int index) const {
77 return m_palette[getPaletteIndex(index)].value;
81void PalletizedStorage<T>::fill(T value)
83 m_indices.assign(getIndicesSize(), 0);
87 m_palette[0] = { value, m_capacity };
94int PalletizedStorage<T>::getPaletteIndex(const int index) const {
95 // https://github.com/Pannoniae/BlockGame/blob/2e02356c73dd927a09c4299b41a13cbad972a57f/src/world/chunk/PaletteBlockData.cs#L145
96 // Permission was given from Pannoniae to continue porting this code
97 switch (m_indexBits) {
99 case 1: return (m_indices[index >> 3] >> (index & 7)) & 1;
100 case 2: return (m_indices[index >> 2] >> ((index & 3) << 1)) & 3;
101 case 4: return (m_indices[index >> 1] >> ((index & 1) << 2)) & 15;
102 case 8: return m_indices[index];
104 const int idx = index << 1;
105 return (m_indices[idx] << 8)
106 | (m_indices[idx + 1]);
109 const int idx = index << 2;
110 return (m_indices[idx] << 24)
111 | (m_indices[idx + 1] << 16)
112 | (m_indices[idx + 2] << 8)
113 | (m_indices[idx + 3]);
115 default: throw exception::PaletteIndexTooLargeException();
120void PalletizedStorage<T>::setPaletteIndex(const int index, const int value) {
121 // https://github.com/Pannoniae/BlockGame/blob/2e02356c73dd927a09c4299b41a13cbad972a57f/src/world/chunk/PaletteBlockData.cs#L185
122 // Permission was given from Pannoniae to continue porting this code
128 m_indices[index >> 3] = static_cast<unsigned char>((m_indices[index >> 3] & ~(1 << (index & 7))) |
129 ((value & 1) << (index & 7)));
132 m_indices[index >> 2] = static_cast<unsigned char>((m_indices[index >> 2] & ~(3 << ((index & 3) << 1))) |
133 ((value & 3) << ((index & 3) << 1)));
136 m_indices[index >> 1] = static_cast<unsigned char>((m_indices[index >> 1] & ~(15 << ((index & 1) << 2))) |
137 ((value & 15) << ((index & 1) << 2)));
140 m_indices[index] = static_cast<unsigned char>(value);
143 const int idx = index << 1;
144 m_indices[idx ] = static_cast<uint8_t>(value & 0xFF);
145 m_indices[idx + 1] = static_cast<uint8_t>((value >> 8) & 0xFF);
149 const int idx = index << 2;
150 m_indices[idx ] = static_cast<uint8_t>(value & 0xFF);
151 m_indices[idx + 1] = static_cast<uint8_t>((value >> 8) & 0xFF);
152 m_indices[idx + 2] = static_cast<uint8_t>((value >> 16) & 0xFF);
153 m_indices[idx + 3] = static_cast<uint8_t>((value >> 24) & 0xFF);
158 throw exception::PaletteIndexTooLargeException();
163int PalletizedStorage<T>::obtainPaletteEntry() {
165 for (int i = 0; i < m_palette.size(); i++)
166 if (m_palette[i].references == 0) return i;
173void PalletizedStorage<T>::resize() {
174 if (m_indexBits >= 32)
175 throw exception::PaletteIndexTooLargeException(); // was originally "how many fucking blocks have you placed????"
178 int indices[m_capacity];
179 for (int i = 0; i < m_capacity; ++i)
180 indices[i] = getPaletteIndex(i);
182 m_indexBits = (m_indexBits == 0) ? 1 : (m_indexBits << 1); // it implodes if I don't do this
184 m_palette.resize(1 << m_indexBits);
185 m_indices.resize(getIndicesSize());
188 for (int i = 0; i < m_capacity; ++i)
189 setPaletteIndex(i, indices[i]);
191#endif // PALLETIZEDSTORAGE_TPP