summary refs log tree commit diff stats
path: root/Source/Panel.h
blob: 0d1af14de22124dbbd08f340c7d491326e45d63c (plain) (blame)
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
#pragma once
#include "json.hpp"
#include "Memory.h"

enum IntersectionFlags {
    IS_ENDPOINT =       0x1,
    IS_STARTPOINT =     0x2,
    IS_GAP =            0x10000,
    HAS_DOT =           0x40020,
    DOT_IS_BLUE =       0x100,
    DOT_IS_ORANGE =     0x200,
    DOT_IS_INVISIBLE =  0x1000,
};

/*
    enum Style {
        SYMMETRICAL = 0x2,
        IS_2COLOR = 0x10,
        HAS_DOTS = 0x4,
        HAS_STARS = 0x40,
        HAS_STONES = 0x100,
        HAS_ERASERS = 0x1000,
        HAS_SHAPERS = 0x2000,
    };
*/

class Endpoint {
public:
    enum class Direction {
        LEFT,
        RIGHT,
        UP,
        DOWN
    };

    Endpoint(int x, int y, Direction dir) {
        _x = x;
        _y = y;
        _dir = dir;
    }

    int GetX() {return _x;}
    void SetX(int x) {_x = x;}
    int GetY() {return _y;}
    void SetY(int y) {_y = y;}
    Direction GetDir() {return _dir;}
    void SetDir(Direction dir) {_dir = dir;}

    nlohmann::json to_json() {
        nlohmann::json json = {{"x", _x}, {"y", _y}};
        if (_dir == Direction::LEFT) json["dir"] = "left";
        if (_dir == Direction::RIGHT) json["dir"] = "right";
        if (_dir == Direction::UP) json["dir"] = "up";
        if (_dir == Direction::DOWN) json["dir"] = "down";
        return json;
    }

private:
    int _x, _y;
    Direction _dir;
};

class Panel {
public:
    Panel(const std::shared_ptr<Memory>& memory, int id);
    // explicit Panel(nlohmann::json json);

    void Write(int id);
    nlohmann::json Serialize();

private:
    // For testing
    Panel() = default;

    void ReadIntersections(int id);
    void WriteIntersections(int id);
    void ReadDecorations(int id);
    void WriteDecorations(int id);

    // TODO: Reflection data
    // TODO: Decoration colors

    std::tuple<int, int> loc_to_xy(int location) {
        int height2 = (_height - 1) / 2;
        int width2 = (_width + 1) / 2;

        int x = 2 * (location % width2);
        int y = 2 * (height2 - location / width2);
        return {x, y};
    }

    int xy_to_loc(int x, int y) {
        int height2 = (_height - 1) / 2;
        int width2 = (_width + 1) / 2;

        int rowsFromBottom = height2 - y/2;
        return rowsFromBottom * width2 + x/2;
    }

    std::tuple<int, int> dloc_to_xy(int location) {
        int height2 = (_height - 3) / 2;
        int width2 = (_width - 1) / 2;

        int x = 2 * (location % width2) + 1;
        int y = 2 * (height2 - location / width2) + 1;
        return {x, y};
    }

    int xy_to_dloc(int x, int y) {
        int height2 = (_height - 3) / 2;
        int width2 = (_width - 1) / 2;

        int rowsFromBottom = height2 - (y - 1)/2;
        return rowsFromBottom * width2 + (x - 1)/2;
    }

    std::shared_ptr<Memory> _memory;

    int _width, _height;

    std::vector<std::vector<int>> _grid;
    std::vector<Endpoint> _endpoints;
    std::vector<std::pair<int ,int>> _startpoints;
    int _style;

    friend class PanelExtractionTests;
};

// V2 stuff here
struct Decoration {
    enum class Type {STONE, STAR, POLY, ERASER, TRIANGLE, RPOLY, YLOP};
    Type type;
    // TODO: Color color;
    uint32_t polyshape;
    int count; // For triangles
};

struct Cell {
    bool start;
    enum class Dir {NONE, LEFT, RIGHT, UP, DOWN};
    Dir end;
    Decoration* decoration;
    enum class Dot {NONE, BLACK, BLUE, YELLOW, INVISIBLE};
    Dot dot;
    enum class Gap {NONE, BREAK, FULL};
    Gap gap;
};

struct Puzzle {
    int16_t height;
    int16_t width;
    Cell** grid;

    enum class Symmetry {NONE, X, Y, XY};
    Symmetry sym;
    bool pillar;
};

class PuzzleSerializer {
public:
    PuzzleSerializer(const std::shared_ptr<Memory>& memory);
    Puzzle ReadPuzzle(int panelId);
    void WritePuzzle(int panelId, const Puzzle& puzzle);

//private:
    enum Shape {
        Stone =      0x100,
        Star =       0x200,
        Poly =       0x400,
        Eraser =     0x500,
        Triangle =   0x600,
        RPoly =     0x1000,
        Ylop =      0x2000,
    };

    enum Color {
        Black =     0x1,
        White =     0x2,
        Gray =      0x3,
        Purple =    0x4,
        Green =     0x5,
        Cyan =      0x6,
        Pink =      0x7,
        Yellow =    0x8,
        Blue =      0x9,
        Orange =    0xA,
    };

    enum Flags {
        IS_ENDPOINT =           0x1,
        IS_STARTPOINT =         0x2,
        DOT_IS_BLUE =         0x100,
        DOT_IS_ORANGE =       0x200,
        DOT_IS_INVISIBLE =   0x1000,
        IS_GAP =            0x10000,
        HAS_DOT =           0x40020,
    };

    std::shared_ptr<Memory> _memory;
};

static nlohmann::json Decoration_to_json(int decoration) {
    nlohmann::json json = {};
    int shape = decoration & 0x00000F00;
    if (shape == PuzzleSerializer::Shape::Stone)      json["type"] = "square";
    if (shape == PuzzleSerializer::Shape::Star)       json["type"] = "star";
    if (shape == PuzzleSerializer::Shape::Poly)       json["type"] = "poly";
    if (shape == PuzzleSerializer::Shape::Eraser)     json["type"] = "eraser";
    if (shape == PuzzleSerializer::Shape::Triangle)   json["type"] = "triangle";

    int color = decoration & 0x0000000F;
    if (color == PuzzleSerializer::Color::Black)      json["color"] = "black";
    if (color == PuzzleSerializer::Color::White)      json["color"] = "white";
    if (color == PuzzleSerializer::Color::Gray)       json["color"] = "gray";
    if (color == PuzzleSerializer::Color::Blue)       json["color"] = "blue";
    if (color == PuzzleSerializer::Color::Green)      json["color"] = "green";

    if (json.empty()) return false;
    return json;
}