Lodestone.Common
Loading...
Searching...
No Matches
PalletizedStorage.tpp
Go to the documentation of this file.
1//
2// Created by DexrnZacAttack on 11/28/25 using zPc-i2.
3//
4#ifndef PALLETIZEDSTORAGE_TPP
5#define PALLETIZEDSTORAGE_TPP
6
7// TODO we need to document this whole file since I don't really understand the logic behind it
8
9template<typename T>
10PalletizedStorage<T>::PalletizedStorage(const int capacity) : m_capacity(capacity) {
11 fill(T{});
12}
13
14template<typename T>
15PalletizedStorage<T>::PalletizedStorage(const int capacity, T fillWith) : m_capacity(capacity) {
16 fill(fillWith);
17}
18
19template<typename T>
20void PalletizedStorage<T>::setValue(const int index, T value) {
21 int idx = getPaletteIndex(index);
22 PaletteEntry &cur = m_palette[idx];
23
24 if (cur.references <= 0)
25 throw exception::InvalidPaletteReferenceCountException();
26
27 // we are overwriting the value at the index, therefore the object already present at this index is no longer referenced
28 --cur.references;
29
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);
34 ++e.references;
35 return;
36 }
37 }
38
39 // can we overwrite the current entry
40 if (cur.references == 0) {
41 cur = {
42 std::move(value),
43 1
44 };
45
46 return;
47 }
48
49 // otherwise we need to get an entry and emplace
50 int n = obtainPaletteEntry();
51 m_palette[n] = {
52 std::move(value),
53 1
54 };
55
56 setPaletteIndex(index, n);
57 m_paletteSize++;
58}
59
60template<typename T>
61T PalletizedStorage<T>::getCopy(const int index) const {
62 return m_palette[getPaletteIndex(index)].value;
63}
64
65template<typename T>
66const T &PalletizedStorage<T>::getValue(const int index) const {
67 return m_palette[getPaletteIndex(index)].value;
68}
69
70template<typename T>
71size_t PalletizedStorage<T>::getIndicesSize() const noexcept {
72 return (m_capacity * m_indexBits + 7) >> 3;
73}
74
75template<typename T>
76const T & PalletizedStorage<T>::operator[](const int index) const {
77 return m_palette[getPaletteIndex(index)].value;
78}
79
80template<typename T>
81void PalletizedStorage<T>::fill(T value)
82{
83 m_indices.assign(getIndicesSize(), 0);
84 m_palette.clear();
85 m_palette.resize(1);
86
87 m_palette[0] = { value, m_capacity };
88
89 m_indexBits = 0;
90 m_paletteSize = 1;
91}
92
93template<typename T>
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) {
98 case 0: return 0;
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];
103 case 16: {
104 const int idx = index << 1;
105 return (m_indices[idx] << 8)
106 | (m_indices[idx + 1]);
107 }
108 case 32: {
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]);
114 }
115 default: throw exception::PaletteIndexTooLargeException();
116 }
117}
118
119template<typename T>
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
123 switch (m_indexBits)
124 {
125 case 0:
126 return;
127 case 1:
128 m_indices[index >> 3] = static_cast<unsigned char>((m_indices[index >> 3] & ~(1 << (index & 7))) |
129 ((value & 1) << (index & 7)));
130 return;
131 case 2:
132 m_indices[index >> 2] = static_cast<unsigned char>((m_indices[index >> 2] & ~(3 << ((index & 3) << 1))) |
133 ((value & 3) << ((index & 3) << 1)));
134 return;
135 case 4:
136 m_indices[index >> 1] = static_cast<unsigned char>((m_indices[index >> 1] & ~(15 << ((index & 1) << 2))) |
137 ((value & 15) << ((index & 1) << 2)));
138 return;
139 case 8:
140 m_indices[index] = static_cast<unsigned char>(value);
141 return;
142 case 16: {
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);
146 return;
147 }
148 case 32: {
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);
154
155 return;
156 }
157 default:
158 throw exception::PaletteIndexTooLargeException();
159 }
160}
161
162template<typename T>
163int PalletizedStorage<T>::obtainPaletteEntry() {
164 while (true) {
165 for (int i = 0; i < m_palette.size(); i++)
166 if (m_palette[i].references == 0) return i;
167
168 this->resize();
169 }
170}
171
172template<typename T>
173void PalletizedStorage<T>::resize() {
174 if (m_indexBits >= 32)
175 throw exception::PaletteIndexTooLargeException(); // was originally "how many fucking blocks have you placed????"
176
177 // decode
178 int indices[m_capacity];
179 for (int i = 0; i < m_capacity; ++i)
180 indices[i] = getPaletteIndex(i);
181
182 m_indexBits = (m_indexBits == 0) ? 1 : (m_indexBits << 1); // it implodes if I don't do this
183
184 m_palette.resize(1 << m_indexBits);
185 m_indices.resize(getIndicesSize());
186
187 // re encode
188 for (int i = 0; i < m_capacity; ++i)
189 setPaletteIndex(i, indices[i]);
190}
191#endif // PALLETIZEDSTORAGE_TPP