diff options
Diffstat (limited to 'Source/Panel.h')
-rw-r--r-- | Source/Panel.h | 224 |
1 files changed, 47 insertions, 177 deletions
diff --git a/Source/Panel.h b/Source/Panel.h index 0d1af14..caa7b04 100644 --- a/Source/Panel.h +++ b/Source/Panel.h | |||
@@ -1,145 +1,45 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | #include "json.hpp" | 2 | #include <memory> |
3 | #include "Memory.h" | 3 | #include <vector> |
4 | 4 | ||
5 | enum IntersectionFlags { | 5 | class Memory; |
6 | IS_ENDPOINT = 0x1, | 6 | |
7 | IS_STARTPOINT = 0x2, | 7 | enum Shape { |
8 | IS_GAP = 0x10000, | 8 | Stone = 0x100, |
9 | HAS_DOT = 0x40020, | 9 | Star = 0x200, |
10 | DOT_IS_BLUE = 0x100, | 10 | Poly = 0x400, |
11 | DOT_IS_ORANGE = 0x200, | 11 | Eraser = 0x500, |
12 | DOT_IS_INVISIBLE = 0x1000, | 12 | Triangle = 0x600, |
13 | RPoly = 0x1000, | ||
14 | Ylop = 0x2000, | ||
13 | }; | 15 | }; |
14 | 16 | ||
15 | /* | 17 | enum Color { |
16 | enum Style { | 18 | Black = 0x1, |
17 | SYMMETRICAL = 0x2, | 19 | White = 0x2, |
18 | IS_2COLOR = 0x10, | 20 | Gray = 0x3, |
19 | HAS_DOTS = 0x4, | 21 | Purple = 0x4, |
20 | HAS_STARS = 0x40, | 22 | Green = 0x5, |
21 | HAS_STONES = 0x100, | 23 | Cyan = 0x6, |
22 | HAS_ERASERS = 0x1000, | 24 | Pink = 0x7, |
23 | HAS_SHAPERS = 0x2000, | 25 | Yellow = 0x8, |
24 | }; | 26 | Blue = 0x9, |
25 | */ | 27 | Orange = 0xA, |
26 | |||
27 | class Endpoint { | ||
28 | public: | ||
29 | enum class Direction { | ||
30 | LEFT, | ||
31 | RIGHT, | ||
32 | UP, | ||
33 | DOWN | ||
34 | }; | ||
35 | |||
36 | Endpoint(int x, int y, Direction dir) { | ||
37 | _x = x; | ||
38 | _y = y; | ||
39 | _dir = dir; | ||
40 | } | ||
41 | |||
42 | int GetX() {return _x;} | ||
43 | void SetX(int x) {_x = x;} | ||
44 | int GetY() {return _y;} | ||
45 | void SetY(int y) {_y = y;} | ||
46 | Direction GetDir() {return _dir;} | ||
47 | void SetDir(Direction dir) {_dir = dir;} | ||
48 | |||
49 | nlohmann::json to_json() { | ||
50 | nlohmann::json json = {{"x", _x}, {"y", _y}}; | ||
51 | if (_dir == Direction::LEFT) json["dir"] = "left"; | ||
52 | if (_dir == Direction::RIGHT) json["dir"] = "right"; | ||
53 | if (_dir == Direction::UP) json["dir"] = "up"; | ||
54 | if (_dir == Direction::DOWN) json["dir"] = "down"; | ||
55 | return json; | ||
56 | } | ||
57 | |||
58 | private: | ||
59 | int _x, _y; | ||
60 | Direction _dir; | ||
61 | }; | 28 | }; |
62 | 29 | ||
63 | class Panel { | ||
64 | public: | ||
65 | Panel(const std::shared_ptr<Memory>& memory, int id); | ||
66 | // explicit Panel(nlohmann::json json); | ||
67 | |||
68 | void Write(int id); | ||
69 | nlohmann::json Serialize(); | ||
70 | |||
71 | private: | ||
72 | // For testing | ||
73 | Panel() = default; | ||
74 | |||
75 | void ReadIntersections(int id); | ||
76 | void WriteIntersections(int id); | ||
77 | void ReadDecorations(int id); | ||
78 | void WriteDecorations(int id); | ||
79 | |||
80 | // TODO: Reflection data | ||
81 | // TODO: Decoration colors | ||
82 | |||
83 | std::tuple<int, int> loc_to_xy(int location) { | ||
84 | int height2 = (_height - 1) / 2; | ||
85 | int width2 = (_width + 1) / 2; | ||
86 | |||
87 | int x = 2 * (location % width2); | ||
88 | int y = 2 * (height2 - location / width2); | ||
89 | return {x, y}; | ||
90 | } | ||
91 | |||
92 | int xy_to_loc(int x, int y) { | ||
93 | int height2 = (_height - 1) / 2; | ||
94 | int width2 = (_width + 1) / 2; | ||
95 | |||
96 | int rowsFromBottom = height2 - y/2; | ||
97 | return rowsFromBottom * width2 + x/2; | ||
98 | } | ||
99 | |||
100 | std::tuple<int, int> dloc_to_xy(int location) { | ||
101 | int height2 = (_height - 3) / 2; | ||
102 | int width2 = (_width - 1) / 2; | ||
103 | |||
104 | int x = 2 * (location % width2) + 1; | ||
105 | int y = 2 * (height2 - location / width2) + 1; | ||
106 | return {x, y}; | ||
107 | } | ||
108 | |||
109 | int xy_to_dloc(int x, int y) { | ||
110 | int height2 = (_height - 3) / 2; | ||
111 | int width2 = (_width - 1) / 2; | ||
112 | |||
113 | int rowsFromBottom = height2 - (y - 1)/2; | ||
114 | return rowsFromBottom * width2 + (x - 1)/2; | ||
115 | } | ||
116 | |||
117 | std::shared_ptr<Memory> _memory; | ||
118 | |||
119 | int _width, _height; | ||
120 | |||
121 | std::vector<std::vector<int>> _grid; | ||
122 | std::vector<Endpoint> _endpoints; | ||
123 | std::vector<std::pair<int ,int>> _startpoints; | ||
124 | int _style; | ||
125 | |||
126 | friend class PanelExtractionTests; | ||
127 | }; | ||
128 | |||
129 | // V2 stuff here | ||
130 | struct Decoration { | 30 | struct Decoration { |
131 | enum class Type {STONE, STAR, POLY, ERASER, TRIANGLE, RPOLY, YLOP}; | 31 | Shape type; |
132 | Type type; | 32 | Color color; |
133 | // TODO: Color color; | 33 | int polyshape; |
134 | uint32_t polyshape; | 34 | // For triangles only |
135 | int count; // For triangles | 35 | int count; |
136 | }; | 36 | }; |
137 | 37 | ||
138 | struct Cell { | 38 | struct Cell { |
139 | bool start; | 39 | bool start; |
140 | enum class Dir {NONE, LEFT, RIGHT, UP, DOWN}; | 40 | enum class Dir {NONE, LEFT, RIGHT, UP, DOWN}; |
141 | Dir end; | 41 | Dir end; |
142 | Decoration* decoration; | 42 | std::shared_ptr<Decoration> decoration; |
143 | enum class Dot {NONE, BLACK, BLUE, YELLOW, INVISIBLE}; | 43 | enum class Dot {NONE, BLACK, BLUE, YELLOW, INVISIBLE}; |
144 | Dot dot; | 44 | Dot dot; |
145 | enum class Gap {NONE, BREAK, FULL}; | 45 | enum class Gap {NONE, BREAK, FULL}; |
@@ -149,7 +49,7 @@ struct Cell { | |||
149 | struct Puzzle { | 49 | struct Puzzle { |
150 | int16_t height; | 50 | int16_t height; |
151 | int16_t width; | 51 | int16_t width; |
152 | Cell** grid; | 52 | std::vector<std::vector<Cell>> grid; |
153 | 53 | ||
154 | enum class Symmetry {NONE, X, Y, XY}; | 54 | enum class Symmetry {NONE, X, Y, XY}; |
155 | Symmetry sym; | 55 | Symmetry sym; |
@@ -159,33 +59,11 @@ struct Puzzle { | |||
159 | class PuzzleSerializer { | 59 | class PuzzleSerializer { |
160 | public: | 60 | public: |
161 | PuzzleSerializer(const std::shared_ptr<Memory>& memory); | 61 | PuzzleSerializer(const std::shared_ptr<Memory>& memory); |
162 | Puzzle ReadPuzzle(int panelId); | 62 | Puzzle ReadPuzzle(int id); |
163 | void WritePuzzle(int panelId, const Puzzle& puzzle); | 63 | void WritePuzzle(const Puzzle& p, int id); |
164 | |||
165 | //private: | ||
166 | enum Shape { | ||
167 | Stone = 0x100, | ||
168 | Star = 0x200, | ||
169 | Poly = 0x400, | ||
170 | Eraser = 0x500, | ||
171 | Triangle = 0x600, | ||
172 | RPoly = 0x1000, | ||
173 | Ylop = 0x2000, | ||
174 | }; | ||
175 | |||
176 | enum Color { | ||
177 | Black = 0x1, | ||
178 | White = 0x2, | ||
179 | Gray = 0x3, | ||
180 | Purple = 0x4, | ||
181 | Green = 0x5, | ||
182 | Cyan = 0x6, | ||
183 | Pink = 0x7, | ||
184 | Yellow = 0x8, | ||
185 | Blue = 0x9, | ||
186 | Orange = 0xA, | ||
187 | }; | ||
188 | 64 | ||
65 | private: | ||
66 | // @Bug: Blue and orange are swapped? | ||
189 | enum Flags { | 67 | enum Flags { |
190 | IS_ENDPOINT = 0x1, | 68 | IS_ENDPOINT = 0x1, |
191 | IS_STARTPOINT = 0x2, | 69 | IS_STARTPOINT = 0x2, |
@@ -196,25 +74,17 @@ public: | |||
196 | HAS_DOT = 0x40020, | 74 | HAS_DOT = 0x40020, |
197 | }; | 75 | }; |
198 | 76 | ||
199 | std::shared_ptr<Memory> _memory; | 77 | void ReadIntersections(Puzzle& p, int id); |
200 | }; | 78 | void ReadDecorations(Puzzle& p, int id); |
201 | 79 | void WriteIntersections(const Puzzle& p, int id); | |
202 | static nlohmann::json Decoration_to_json(int decoration) { | 80 | void WriteDecorations(const Puzzle& p, int id); |
203 | nlohmann::json json = {}; | ||
204 | int shape = decoration & 0x00000F00; | ||
205 | if (shape == PuzzleSerializer::Shape::Stone) json["type"] = "square"; | ||
206 | if (shape == PuzzleSerializer::Shape::Star) json["type"] = "star"; | ||
207 | if (shape == PuzzleSerializer::Shape::Poly) json["type"] = "poly"; | ||
208 | if (shape == PuzzleSerializer::Shape::Eraser) json["type"] = "eraser"; | ||
209 | if (shape == PuzzleSerializer::Shape::Triangle) json["type"] = "triangle"; | ||
210 | 81 | ||
211 | int color = decoration & 0x0000000F; | 82 | std::tuple<int, int> loc_to_xy(const Puzzle& p, int location); |
212 | if (color == PuzzleSerializer::Color::Black) json["color"] = "black"; | 83 | int xy_to_loc(const Puzzle& p, int x, int y); |
213 | if (color == PuzzleSerializer::Color::White) json["color"] = "white"; | 84 | // Decoration location |
214 | if (color == PuzzleSerializer::Color::Gray) json["color"] = "gray"; | 85 | std::tuple<int, int> dloc_to_xy(const Puzzle& p, int location); |
215 | if (color == PuzzleSerializer::Color::Blue) json["color"] = "blue"; | 86 | int xy_to_dloc(const Puzzle& p, int x, int y); |
216 | if (color == PuzzleSerializer::Color::Green) json["color"] = "green"; | 87 | Cell::Dot FlagsToDot(int flags); |
217 | 88 | ||
218 | if (json.empty()) return false; | 89 | std::shared_ptr<Memory> _memory; |
219 | return json; | 90 | }; |
220 | } | ||