diff options
Diffstat (limited to 'ext/wittle_generator/Panel.cpp')
-rw-r--r-- | ext/wittle_generator/Panel.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/ext/wittle_generator/Panel.cpp b/ext/wittle_generator/Panel.cpp index a005467..54c7283 100644 --- a/ext/wittle_generator/Panel.cpp +++ b/ext/wittle_generator/Panel.cpp | |||
@@ -3,6 +3,8 @@ | |||
3 | #include <fstream> | 3 | #include <fstream> |
4 | #include <sstream> | 4 | #include <sstream> |
5 | 5 | ||
6 | #include "Serializer.h" | ||
7 | |||
6 | template <class T> | 8 | template <class T> |
7 | int find(const std::vector<T>& data, T search, size_t startIndex = 0) { | 9 | int find(const std::vector<T>& data, T search, size_t startIndex = 0) { |
8 | for (size_t i = startIndex; i < data.size(); i++) { | 10 | for (size_t i = startIndex; i < data.size(); i++) { |
@@ -114,3 +116,111 @@ void Panel::Resize(int width, int height) { | |||
114 | for (auto& row : _grid) row.resize(height); | 116 | for (auto& row : _grid) row.resize(height); |
115 | _resized = true; | 117 | _resized = true; |
116 | } | 118 | } |
119 | |||
120 | std::string Panel::Write() { | ||
121 | Serializer serializer; | ||
122 | serializer.writeInt(SERIALIZER_VERSION); | ||
123 | serializer.writeByte(_width); | ||
124 | serializer.writeByte(_height); | ||
125 | serializer.writeString("Generated"); | ||
126 | |||
127 | int genericFlags = 0; | ||
128 | if (symmetry != Symmetry::None) { | ||
129 | genericFlags |= Serializer::Symmetrical; | ||
130 | if (symmetry == Symmetry::Horizontal) { | ||
131 | genericFlags |= Serializer::SymmetryX; | ||
132 | } | ||
133 | if (symmetry == Symmetry::Vertical) { | ||
134 | genericFlags |= Serializer::SymmetryY; | ||
135 | } | ||
136 | } | ||
137 | serializer.writeByte(genericFlags); | ||
138 | |||
139 | for (int x = 0; x < _width; x++) { | ||
140 | for (int y = 0; y < _height; y++) { | ||
141 | int val = _grid[x][y]; | ||
142 | |||
143 | if (x % 2 == 1 && y % 2 == 1) { | ||
144 | // This is a grid cell. | ||
145 | int symbol = val & 0xF00; | ||
146 | if (symbol == Decoration::Triangle) { | ||
147 | serializer.writeByte(Serializer::Triangle); | ||
148 | serializer.writeColor(val & 0xF); | ||
149 | serializer.writeByte((val & 0xF0000) >> 16); | ||
150 | } else if (symbol == Decoration::Star) { | ||
151 | serializer.writeByte(Serializer::Star); | ||
152 | serializer.writeColor(val & 0xF); | ||
153 | } else if (symbol == Decoration::Stone) { | ||
154 | serializer.writeByte(Serializer::Square); | ||
155 | serializer.writeColor(val & 0xF); | ||
156 | } else if (symbol == Decoration::Eraser) { | ||
157 | serializer.writeByte(Serializer::Nega); | ||
158 | serializer.writeColor(val & 0xF); | ||
159 | } else if (symbol == Decoration::Poly) { | ||
160 | serializer.writeByte(Serializer::Poly); | ||
161 | serializer.writeColor(val & 0xF); | ||
162 | // TODO: write polyshape | ||
163 | } else if (symbol == Decoration::Negative) { | ||
164 | serializer.writeByte(Serializer::Ylop); | ||
165 | serializer.writeColor(val & 0xF); | ||
166 | // TODO: write polyshape | ||
167 | } else { | ||
168 | serializer.writeByte(Serializer::Nonce); | ||
169 | } | ||
170 | } else { | ||
171 | serializer.writeByte(Serializer::Line); | ||
172 | // line, dot, gap | ||
173 | serializer.writeByte(Serializer::LineNone); | ||
174 | if (val & Decoration::Dot) { | ||
175 | if (val & IntersectionFlags::DOT_IS_BLUE) { | ||
176 | serializer.writeByte(Serializer::DotBlue); | ||
177 | } else if (val & IntersectionFlags::DOT_IS_ORANGE) { | ||
178 | serializer.writeByte(Serializer::DotYellow); | ||
179 | } else if (val & IntersectionFlags::DOT_IS_INVISIBLE) { | ||
180 | serializer.writeByte(Serializer::DotInvisible); | ||
181 | } else { | ||
182 | serializer.writeByte(Serializer::DotBlack); | ||
183 | } | ||
184 | } else { | ||
185 | serializer.writeByte(Serializer::DotNone); | ||
186 | } | ||
187 | if (val & Decoration::Gap) { | ||
188 | serializer.writeByte(Serializer::GapBreak); | ||
189 | } else { | ||
190 | serializer.writeByte(Serializer::GapNone); | ||
191 | } | ||
192 | } | ||
193 | |||
194 | char startEnd = 0; | ||
195 | for (const Point& pos : _startpoints) { | ||
196 | if (pos.first == x && pos.second == y) { | ||
197 | startEnd |= Serializer::Start; | ||
198 | } | ||
199 | } | ||
200 | for (const Endpoint& endpoint : _endpoints) { | ||
201 | if (endpoint.GetX() == x && endpoint.GetY() == y) { | ||
202 | if (endpoint.GetDir() & Endpoint::LEFT) { | ||
203 | startEnd |= Serializer::EndLeft; | ||
204 | } | ||
205 | if (endpoint.GetDir() & Endpoint::RIGHT) { | ||
206 | startEnd |= Serializer::EndRight; | ||
207 | } | ||
208 | if (endpoint.GetDir() & Endpoint::UP) { | ||
209 | startEnd |= Serializer::EndTop; | ||
210 | } | ||
211 | if (endpoint.GetDir() & Endpoint::DOWN) { | ||
212 | startEnd |= Serializer::EndBottom; | ||
213 | } | ||
214 | } | ||
215 | } | ||
216 | serializer.writeByte(startEnd); | ||
217 | } | ||
218 | } | ||
219 | |||
220 | serializer.writeInt(0); | ||
221 | serializer.writeByte(Serializer::NegationsCancelNegations | | ||
222 | Serializer::PrecisePolyominos | | ||
223 | Serializer::FlashForErrors); | ||
224 | |||
225 | return serializer.str(); | ||
226 | } | ||