1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
#include "PuzzlerSerializer.h"
#include "Memory.h"
#pragma warning (disable:26451)
#pragma warning (disable:26812)
PuzzleSerializer::PuzzleSerializer(const std::shared_ptr<Memory>& memory) : _memory(memory) {}
Puzzle PuzzleSerializer::ReadPuzzle(int id) {
int width = 2 * _memory->ReadPanelData<int>(id, GRID_SIZE_X, 1)[0] - 1;
int height = 2 * _memory->ReadPanelData<int>(id, GRID_SIZE_Y, 1)[0] - 1;
if (width < 0 || height < 0) return Puzzle(); // @Error: Grid size should be always positive? Looks like the starting panels break this rule, though.
Puzzle p;
p.NewGrid(width, height);
ReadIntersections(p, id);
ReadDecorations(p, id);
return p;
}
void PuzzleSerializer::ReadIntersections(Puzzle& p, int id) {
int numIntersections = _memory->ReadPanelData<int>(id, NUM_DOTS, 1)[0];
std::vector<int> intersectionFlags = _memory->ReadArray<int>(id, DOT_FLAGS, numIntersections);
int numConnections = _memory->ReadPanelData<int>(id, NUM_CONNECTIONS, 1)[0];
std::vector<int> connections_a = _memory->ReadArray<int>(id, DOT_CONNECTION_A, numConnections);
std::vector<int> connections_b = _memory->ReadArray<int>(id, DOT_CONNECTION_B, numConnections);
std::vector<float> intersectionLocations = _memory->ReadArray<float>(id, DOT_POSITIONS, numIntersections*2);
// @Cleanup: Change defaults?
for (int x=0; x<p.width; x++) {
for (int y=0; y<p.height; y++) {
if (x%2 == y%2) continue;
p.grid[x][y].gap = Cell::Gap::FULL;
}
}
for (int j=0; j<numIntersections; j++) {
if (intersectionFlags[connections_a[j]] & Flags::IS_ENDPOINT) break;
if (intersectionFlags[connections_b[j]] & Flags::IS_ENDPOINT) break;
float x1 = intersectionLocations[2*connections_a[j]];
float y1 = intersectionLocations[2*connections_a[j]+1];
float x2 = intersectionLocations[2*connections_b[j]];
float y2 = intersectionLocations[2*connections_b[j]+1];
auto [x, y] = loc_to_xy(p, connections_a[j]);
if (x1 < x2) x++;
else if (x1 > x2) x--;
else if (y1 < y2) y--;
else if (y1 > y2) y++;
p.grid[x][y].gap = Cell::Gap::NONE;
}
// This iterates bottom-top, left-right
int i = 0;
for (;; i++) {
int flags = intersectionFlags[i];
auto [x, y] = loc_to_xy(p, i);
if (y < 0) break;
if (flags & Flags::IS_STARTPOINT) {
p.grid[x][y].start = true;
}
p.grid[x][y].dot = FlagsToDot(flags);
if (flags & Flags::IS_FULL_GAP) {
p.grid[x][y].gap = Cell::Gap::FULL;
}
}
// Iterate the remaining intersections (endpoints, dots, gaps)
for (; i < numIntersections; i++) {
int location = FindConnection(i, connections_a, connections_b);
if (location == -1) continue; // @Error: Unable to find connection point
// (x1, y1) location of this intersection
// (x2, y2) location of the connected intersection
float x1 = intersectionLocations[2*i];
float y1 = intersectionLocations[2*i+1];
float x2 = intersectionLocations[2*location];
float y2 = intersectionLocations[2*location+1];
auto [x, y] = loc_to_xy(p, location);
if (intersectionFlags[i] & Flags::IS_ENDPOINT) {
// Our x coordinate is less than the target's
if (x1 < x2) p.grid[x][y].end = Cell::Dir::LEFT;
else if (x1 > x2) p.grid[x][y].end = Cell::Dir::RIGHT;
// Note that Y coordinates are reversed: 0.0 (bottom) 1.0 (top)
else if (y1 < y2) p.grid[x][y].end = Cell::Dir::DOWN;
else if (y1 > y2) p.grid[x][y].end = Cell::Dir::UP;
} else if (intersectionFlags[i] & Flags::HAS_DOT) {
if (x1 < x2) x--;
else if (x1 > x2) x++;
else if (y1 < y2) y++;
else if (y1 > y2) y--;
p.grid[x][y].dot = FlagsToDot(intersectionFlags[i]);
} else if (intersectionFlags[i] & Flags::HAS_ONE_CONN) {
if (x1 < x2) x--;
else if (x1 > x2) x++;
else if (y1 < y2) y++;
else if (y1 > y2) y--;
p.grid[x][y].gap = Cell::Gap::BREAK;
}
}
}
void PuzzleSerializer::ReadDecorations(Puzzle& p, int id) {
int numDecorations = _memory->ReadPanelData<int>(id, NUM_DECORATIONS, 1)[0];
std::vector<int> decorations = _memory->ReadArray<int>(id, DECORATIONS, numDecorations);
if (numDecorations > 0) p.hasDecorations = true;
for (int i=0; i<numDecorations; i++) {
auto [x, y] = dloc_to_xy(p, i);
auto d = std::make_shared<Decoration>();
p.grid[x][y].decoration = d;
d->type = static_cast<Type>(decorations[i] & 0xFF00);
switch(d->type) {
case Type::Poly:
case Type::RPoly:
case Type::Ylop:
d->polyshape = decorations[i] & 0xFFFF0000;
break;
case Type::Triangle:
d->count = decorations[i] & 0x000F0000;
break;
}
d->color = static_cast<Color>(decorations[i] & 0xF);
}
}
void PuzzleSerializer::WritePuzzle(const Puzzle& p, int id) {
_memory->WritePanelData<int>(id, GRID_SIZE_X, {(p.width + 1)/2});
_memory->WritePanelData<int>(id, GRID_SIZE_Y, {(p.height + 1)/2});
WriteIntersections(p, id);
if (p.hasDecorations) WriteDecorations(p, id);
_memory->WritePanelData<int>(id, NEEDS_REDRAW, {1});
}
void PuzzleSerializer::WriteIntersections(const Puzzle& p, int id) {
std::vector<float> intersectionLocations;
std::vector<int> intersectionFlags;
std::vector<int> connections_a;
std::vector<int> connections_b;
float min = 0.1f;
float max = 0.9f;
float width_interval = (max - min) / (p.width/2);
float height_interval = (max - min) / (p.height/2);
float horiz_gap_size = width_interval / 2;
float verti_gap_size = height_interval / 2;
// @Cleanup: If I write directly to locations, then I can simplify this gross loop iterator.
// int numIntersections = (p.width / 2 + 1) * (p.height / 2 + 1);
// Grided intersections
for (int y=p.height-1; y>=0; y-=2) {
for (int x=0; x<p.width; x+=2) {
intersectionLocations.push_back(min + (x/2) * width_interval);
intersectionLocations.push_back(max - (y/2) * height_interval);
int flags = 0;
if (p.grid[x][y].start) {
flags |= Flags::IS_STARTPOINT;
}
if (p.grid[x][y].gap == Cell::Gap::FULL) {
flags |= Flags::IS_FULL_GAP;
}
switch (p.grid[x][y].dot) {
case Cell::Dot::BLACK:
flags |= Flags::HAS_DOT;
break;
case Cell::Dot::BLUE:
flags |= Flags::HAS_DOT | Flags::DOT_IS_BLUE;
break;
case Cell::Dot::YELLOW:
flags |= Flags::HAS_DOT | Flags::DOT_IS_ORANGE;
break;
case Cell::Dot::INVISIBLE:
flags |= Flags::HAS_DOT | Flags::DOT_IS_INVISIBLE;
break;
}
int numConnections = 0;
if (p.grid[x][y].end != Cell::Dir::NONE) numConnections++;
// Create connections for this intersection for bottom/left only.
// Bottom connection
if (y > 0 && p.grid[x][y-1].gap == Cell::Gap::NONE) {
connections_a.push_back(xy_to_loc(p, x, y-2));
connections_b.push_back(xy_to_loc(p, x, y));
flags |= Flags::HAS_VERTI_CONN;
numConnections++;
}
// Top connection
if (y < p.height - 1 && p.grid[x][y+1].gap == Cell::Gap::NONE) {
flags |= Flags::HAS_VERTI_CONN;
numConnections++;
}
// Left connection
if (x > 0 && p.grid[x-1][y].gap == Cell::Gap::NONE) {
connections_a.push_back(xy_to_loc(p, x-2, y));
connections_b.push_back(xy_to_loc(p, x, y));
flags |= Flags::HAS_HORIZ_CONN;
numConnections++;
}
// Right connection
if (x < p.width - 1 && p.grid[x+1][y].gap == Cell::Gap::NONE) {
flags |= Flags::HAS_HORIZ_CONN;
numConnections++;
}
if (numConnections == 1) flags |= HAS_ONE_CONN;
intersectionFlags.push_back(flags);
}
}
// Endpoints
for (int x=0; x<p.width; x++) {
for (int y=0; y<p.height; y++) {
if (p.grid[x][y].end == Cell::Dir::NONE) continue;
connections_a.push_back(xy_to_loc(p, x, y)); // Target to connect to
connections_b.push_back(static_cast<int>(intersectionFlags.size())); // This endpoint
float xPos = min + (x/2) * width_interval;
float yPos = max - (y/2) * height_interval;
switch (p.grid[x][y].end) {
case Cell::Dir::LEFT:
xPos -= .05f;
break;
case Cell::Dir::RIGHT:
xPos += .05f;
break;
case Cell::Dir::UP:
yPos += .05f; // Y position goes from 0 (bottom) to 1 (top), so this is reversed.
break;
case Cell::Dir::DOWN:
yPos -= .05f;
break;
}
intersectionLocations.push_back(xPos);
intersectionLocations.push_back(yPos);
intersectionFlags.push_back(Flags::IS_ENDPOINT);
}
}
// Dots
for (int x=0; x<p.width; x++) {
for (int y=0; y<p.height; y++) {
if (x%2 == y%2) continue; // Cells are invalid, intersections are already handled.
if (p.grid[x][y].dot == Cell::Dot::NONE) continue;
// We need to introduce a new segment --
// Locate the segment we're breaking
for (int i=0; i<connections_a.size(); i++) {
auto [x1, y1] = loc_to_xy(p, connections_a[i]);
auto [x2, y2] = loc_to_xy(p, connections_b[i]);
if ((x1+1 == x && x2-1 == x && y1 == y && y2 == y) ||
(y1+1 == y && y2-1 == y && x1 == x && x2 == x)) {
int other_connection = connections_b[i];
connections_b[i] = static_cast<int>(intersectionFlags.size()); // This endpoint
connections_a.push_back(other_connection);
connections_b.push_back(static_cast<int>(intersectionFlags.size())); // This endpoint
break;
}
}
// Add this dot to the end
float xPos = min + (x/2.0f) * width_interval;
float yPos = max - (y/2.0f) * height_interval;
intersectionLocations.push_back(xPos);
intersectionLocations.push_back(yPos);
int flags = Flags::HAS_DOT;
switch (p.grid[x][y].dot) {
case Cell::Dot::BLACK:
break;
case Cell::Dot::BLUE:
flags |= DOT_IS_BLUE;
break;
case Cell::Dot::YELLOW:
flags |= DOT_IS_ORANGE;
break;
case Cell::Dot::INVISIBLE:
flags |= DOT_IS_INVISIBLE;
break;
}
intersectionFlags.push_back(flags);
}
}
// Gaps
for (int x=0; x<p.width; x++) {
for (int y=0; y<p.height; y++) {
if (x%2 == y%2) continue; // Cells are invalid, intersections are already handled.
if (p.grid[x][y].gap != Cell::Gap::BREAK) continue;
float xPos = min + (x/2.0f) * width_interval;
float yPos = max - (y/2.0f) * height_interval;
// Reminder: Y goes from 0.0 (bottom) to 1.0 (top)
if (x%2 == 0) { // Vertical gap
connections_a.push_back(xy_to_loc(p, x, y-1));
connections_b.push_back(static_cast<int>(intersectionFlags.size())); // This endpoint
intersectionLocations.push_back(xPos);
intersectionLocations.push_back(yPos + verti_gap_size / 2);
intersectionFlags.push_back(Flags::HAS_ONE_CONN | Flags::HAS_VERTI_CONN);
connections_a.push_back(xy_to_loc(p, x, y+1));
connections_b.push_back(static_cast<int>(intersectionFlags.size())); // This endpoint
intersectionLocations.push_back(xPos);
intersectionLocations.push_back(yPos - verti_gap_size / 2);
intersectionFlags.push_back(Flags::HAS_ONE_CONN | Flags::HAS_VERTI_CONN);
} else if (y%2 == 0) { // Horizontal gap
connections_a.push_back(xy_to_loc(p, x-1, y));
connections_b.push_back(static_cast<int>(intersectionFlags.size())); // This endpoint
intersectionLocations.push_back(xPos - horiz_gap_size / 2);
intersectionLocations.push_back(yPos);
intersectionFlags.push_back(Flags::HAS_ONE_CONN | Flags::HAS_HORIZ_CONN);
connections_a.push_back(xy_to_loc(p, x+1, y));
connections_b.push_back(static_cast<int>(intersectionFlags.size())); // This endpoint
intersectionLocations.push_back(xPos + horiz_gap_size / 2);
intersectionLocations.push_back(yPos);
intersectionFlags.push_back(Flags::HAS_ONE_CONN | Flags::HAS_HORIZ_CONN);
}
}
}
_memory->WritePanelData<int>(id, NUM_DOTS, {static_cast<int>(intersectionFlags.size())});
_memory->WriteArray<float>(id, DOT_POSITIONS, intersectionLocations);
_memory->WriteArray<int>(id, DOT_FLAGS, intersectionFlags);
_memory->WritePanelData<int>(id, NUM_CONNECTIONS, {static_cast<int>(connections_a.size())});
_memory->WriteArray<int>(id, DOT_CONNECTION_A, connections_a);
_memory->WriteArray<int>(id, DOT_CONNECTION_B, connections_b);
}
void PuzzleSerializer::WriteDecorations(const Puzzle& p, int id) {
std::vector<int> decorations;
for (int y=p.height-2; y>0; y-=2) {
for (int x=1; x<p.width-1; x+=2) {
auto d = p.grid[x][y].decoration;
if (d) {
decorations.push_back(d->color | d->type | d->count | d->polyshape);
} else {
decorations.push_back(0);
}
}
}
_memory->WritePanelData<int>(id, NUM_DECORATIONS, {static_cast<int>(decorations.size())});
_memory->WriteArray<int>(id, DECORATIONS, decorations);
}
std::tuple<int, int> PuzzleSerializer::loc_to_xy(const Puzzle& p, int location) const {
int height2 = (p.height - 1) / 2;
int width2 = (p.width + 1) / 2;
int x = 2 * (location % width2);
int y = 2 * (height2 - location / width2);
return {x, y};
}
int PuzzleSerializer::xy_to_loc(const Puzzle& p, int x, int y) const {
int height2 = (p.height - 1) / 2;
int width2 = (p.width + 1) / 2;
int rowsFromBottom = height2 - y/2;
return rowsFromBottom * width2 + x/2;
}
std::tuple<int, int> PuzzleSerializer::dloc_to_xy(const Puzzle& p, int location) const {
int height2 = (p.height - 3) / 2;
int width2 = (p.width - 1) / 2;
int x = 2 * (location % width2) + 1;
int y = 2 * (height2 - location / width2) + 1;
return {x, y};
}
int PuzzleSerializer::xy_to_dloc(const Puzzle& p, int x, int y) const {
int height2 = (p.height - 3) / 2;
int width2 = (p.width - 1) / 2;
int rowsFromBottom = height2 - (y - 1)/2;
return rowsFromBottom * width2 + (x - 1)/2;
}
Cell::Dot PuzzleSerializer::FlagsToDot(int flags) const {
if (!(flags & Flags::HAS_DOT)) return Cell::Dot::NONE;
if (flags & Flags::DOT_IS_BLUE) {
return Cell::Dot::BLUE;
} else if (flags & Flags::DOT_IS_ORANGE) {
return Cell::Dot::YELLOW;
} else if (flags & Flags::DOT_IS_INVISIBLE) {
return Cell::Dot::INVISIBLE;
} else {
return Cell::Dot::BLACK;
}
}
int PuzzleSerializer::FindConnection(int i, const std::vector<int>& connections_a, const std::vector<int>& connections_b) const {
for (int j=0; j<connections_a.size(); j++) {
if (connections_a[j] == i) return connections_b[j];
if (connections_b[j] == i) return connections_a[j];
}
return -1;
}
|