about summary refs log tree commit diff stats
path: root/ext/wittle_generator/Panel.h
blob: 95832b5468d919959aae4b3055eceb84fc931724 (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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#ifndef PANEL_H_FC471D68
#define PANEL_H_FC471D68

#include <stdint.h>

#include <tuple>
#include <vector>

struct Point {
  int first;
  int second;
  Point() {
    first = 0;
    second = 0;
  };
  Point(int x, int y) {
    first = x;
    second = y;
  }
  Point operator+(const Point& p) {
    return {first + p.first, second + p.second};
  }
  Point operator*(int d) { return {first * d, second * d}; }
  Point operator/(int d) { return {first / d, second / d}; }
  bool operator==(const Point& p) const {
    return first == p.first && second == p.second;
  };
  bool operator!=(const Point& p) const {
    return first != p.first || second != p.second;
  };
  friend bool operator<(const Point& p1, const Point& p2) {
    if (p1.first == p2.first) return p1.second < p2.second;
    return p1.first < p2.first;
  };
};

class Decoration {
 public:
  enum Shape : int {
    Exit = 0x600001,
    Start = 0x600002,
    Stone = 0x100,
    Star = 0x200,
    Poly = 0x400,
    Eraser = 0x500,
    Triangle = 0x600,
    Triangle1 = 0x10600,
    Triangle2 = 0x20600,
    Triangle3 = 0x30600,
    Triangle4 = 0x40600,
    Arrow = 0x700,
    Arrow1 = 0x1700,
    Arrow2 = 0x2700,
    Arrow3 = 0x3700,
    Can_Rotate = 0x1000,
    Negative = 0x2000,
    Gap = 0x100000,
    Gap_Row = 0x300000,
    Gap_Column = 0x500000,
    Dot = 0x20,
    Dot_Row = 0x240020,
    Dot_Column = 0x440020,
    Dot_Intersection = 0x600020,
    Empty = 0xA00,
  };
  enum Color : int {
    None = 0,
    Black = 0x1,
    White = 0x2,
    Red = 0x3,  // Doesn't work sadly
    Purple = 0x4,
    Green = 0x5,
    Cyan = 0x6,
    Magenta = 0x7,
    Yellow = 0x8,
    Blue = 0x9,
    Orange = 0xA,
    X = 0xF,
  };
};

enum IntersectionFlags : int {
  ROW = 0x200000,
  COLUMN = 0x400000,
  INTERSECTION = 0x600000,
  ENDPOINT = 0x1,
  STARTPOINT = 0x2,
  OPEN = 0x3,      // Puzzle loader flag - not to be written out
  PATH = 0x4,      // Generator use only
  NO_POINT = 0x8,  // Points that nothing connects to
  GAP = 0x100000,
  DOT = 0x20,
  DOT_IS_BLUE = 0x100,
  DOT_IS_ORANGE = 0x200,
  DOT_IS_INVISIBLE = 0x1000,
  DOT_SMALL = 0x2000,
  DOT_MEDIUM = 0x4000,
  DOT_LARGE = 0x8000,
};

class Endpoint {
 public:
  enum Direction {
    NONE = 0,
    LEFT = 1,
    RIGHT = 2,
    UP = 4,
    DOWN = 8,
    UP_LEFT = 5,
    UP_RIGHT = 6,
    DOWN_LEFT = 9,
    DOWN_RIGHT = 10
  };

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

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

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

struct Color {
  float r;
  float g;
  float b;
  float a;
  friend bool operator<(const Color& lhs, const Color& rhs) {
    return lhs.r * 8 + lhs.g * 4 + lhs.b * 2 + lhs.a >
           rhs.r * 8 + rhs.g * 4 + rhs.b * 2 + rhs.a;
  }
};

struct SolutionPoint {
  int pointA, pointB, pointC, pointD;
  float f1x, f1y, f2x, f2y, f3x, f3y, f4x, f4y;
  int endnum;
};

class Panel {
 public:
  void SetSymbol(int x, int y, Decoration::Shape symbol,
                 Decoration::Color color);
  void SetShape(int x, int y, int shape, bool rotate, bool negative,
                Decoration::Color color);
  void ClearSymbol(int x, int y);
  void SetGridSymbol(int x, int y, Decoration::Shape symbol,
                     Decoration::Color color = Decoration::Color::None);
  void ClearGridSymbol(int x, int y);
  void Resize(int width, int height);
  void ClearStartpoints() { _startpoints.clear(); }
  void ClearExits() { _endpoints.clear(); }

  int width() const { return _width; }
  int height() const { return _height; }

  int get(int x, int y) { return _grid[x][y]; }
  void set(int x, int y, int val) { _grid[x][y] = val; }

  void SetInvisibleSymmetry(bool val) { _invisible_symmetry = val; }

  std::string Write();

  enum Style {
    SYMMETRICAL = 0x2,  // Not on the town symmetry puzzles? IDK why.
    NO_BLINK = 0x4,
    HAS_DOTS = 0x8,
    IS_2COLOR = 0x10,
    HAS_STARS = 0x40,
    HAS_TRIANGLES = 0x80,
    HAS_STONES = 0x100,
    HAS_ERASERS = 0x1000,
    HAS_SHAPERS = 0x2000,
    IS_PIVOTABLE = 0x8000,
  };

  enum Symmetry {  // NOTE - Not all of these are valid symmetries for certain
                   // puzzles
    None,
    Horizontal,
    Vertical,
    Rotational,
    RotateLeft,
    RotateRight,
    FlipXY,
    FlipNegXY,
    ParallelH,
    ParallelV,
    ParallelHFlip,
    ParallelVFlip,
    PillarParallel,
    PillarHorizontal,
    PillarVertical,
    PillarRotational
  };
  Symmetry symmetry;

  float pathWidth;
  enum ColorMode {
    Default,
    Reset,
    Alternate,
    WriteColors,
    Treehouse,
    TreehouseAlternate
  };
  ColorMode colorMode;
  bool decorationsOnly;
  bool enableFlash;

  Point get_sym_point(int x, int y, Symmetry symmetry) {
    switch (symmetry) {
      case None:
        return Point(x, y);
      case Symmetry::Horizontal:
        return Point(x, _height - 1 - y);
      case Symmetry::Vertical:
        return Point(_width - 1 - x, y);
      case Symmetry::Rotational:
        return Point(_width - 1 - x, _height - 1 - y);
      case Symmetry::RotateLeft:
        return Point(y, _width - 1 - x);
      case Symmetry::RotateRight:
        return Point(_height - 1 - y, x);
      case Symmetry::FlipXY:
        return Point(y, x);
      case Symmetry::FlipNegXY:
        return Point(_height - 1 - y, _width - 1 - x);
      case Symmetry::ParallelH:
        return Point(x, y == _height / 2
                            ? _height / 2
                            : (y + (_height + 1) / 2) % (_height + 1));
      case Symmetry::ParallelV:
        return Point(x == _width / 2 ? _width / 2
                                     : (x + (_width + 1) / 2) % (_width + 1),
                     y);
      case Symmetry::ParallelHFlip:
        return Point(_width - 1 - x,
                     y == _height / 2
                         ? _height / 2
                         : (y + (_height + 1) / 2) % (_height + 1));
      case Symmetry::ParallelVFlip:
        return Point(x == _width / 2 ? _width / 2
                                     : (x + (_width + 1) / 2) % (_width + 1),
                     _height - 1 - y);
      case Symmetry::PillarParallel:
        return Point(x + _width / 2, y);
      case Symmetry::PillarHorizontal:
        return Point(x + _width / 2, _height - 1 - y);
      case Symmetry::PillarVertical:
        return Point(_width / 2 - x, y);
      case Symmetry::PillarRotational:
        return Point(_width / 2 - x, _height - 1 - y);
    }
    return Point(x, y);
  }

  Point get_sym_point(int x, int y) { return get_sym_point(x, y, symmetry); }
  Point get_sym_point(Point p) {
    return get_sym_point(p.first, p.second, symmetry);
  }
  Point get_sym_point(Point p, Symmetry symmetry) {
    return get_sym_point(p.first, p.second, symmetry);
  }
  Endpoint::Direction get_sym_dir(Endpoint::Direction direction,
                                  Symmetry symmetry) {
    int dirIndex;
    if (direction == Endpoint::Direction::LEFT) dirIndex = 0;
    if (direction == Endpoint::Direction::RIGHT) dirIndex = 1;
    if (direction == Endpoint::Direction::UP) dirIndex = 2;
    if (direction == Endpoint::Direction::DOWN) dirIndex = 3;
    std::vector<Endpoint::Direction> mapping;
    switch (symmetry) {
      case Symmetry::Horizontal:
        mapping = {Endpoint::Direction::LEFT, Endpoint::Direction::RIGHT,
                   Endpoint::Direction::DOWN, Endpoint::Direction::UP};
        break;
      case Symmetry::Vertical:
        mapping = {Endpoint::Direction::RIGHT, Endpoint::Direction::LEFT,
                   Endpoint::Direction::UP, Endpoint::Direction::DOWN};
        break;
      case Symmetry::Rotational:
        mapping = {Endpoint::Direction::RIGHT, Endpoint::Direction::LEFT,
                   Endpoint::Direction::DOWN, Endpoint::Direction::UP};
        break;
      case Symmetry::RotateLeft:
        mapping = {Endpoint::Direction::DOWN, Endpoint::Direction::UP,
                   Endpoint::Direction::LEFT, Endpoint::Direction::RIGHT};
        break;
      case Symmetry::RotateRight:
        mapping = {Endpoint::Direction::UP, Endpoint::Direction::DOWN,
                   Endpoint::Direction::RIGHT, Endpoint::Direction::LEFT};
        break;
      case Symmetry::FlipXY:
        mapping = {Endpoint::Direction::UP, Endpoint::Direction::DOWN,
                   Endpoint::Direction::LEFT, Endpoint::Direction::RIGHT};
        break;
      case Symmetry::FlipNegXY:
        mapping = {Endpoint::Direction::DOWN, Endpoint::Direction::UP,
                   Endpoint::Direction::RIGHT, Endpoint::Direction::LEFT};
        break;
      case Symmetry::ParallelH:
        mapping = {Endpoint::Direction::LEFT, Endpoint::Direction::RIGHT,
                   Endpoint::Direction::UP, Endpoint::Direction::DOWN};
        break;
      case Symmetry::ParallelV:
        mapping = {Endpoint::Direction::LEFT, Endpoint::Direction::RIGHT,
                   Endpoint::Direction::UP, Endpoint::Direction::DOWN};
        break;
      case Symmetry::ParallelHFlip:
        mapping = {Endpoint::Direction::RIGHT, Endpoint::Direction::LEFT,
                   Endpoint::Direction::UP, Endpoint::Direction::DOWN};
        break;
      case Symmetry::ParallelVFlip:
        mapping = {Endpoint::Direction::LEFT, Endpoint::Direction::RIGHT,
                   Endpoint::Direction::DOWN, Endpoint::Direction::UP};
        break;
      default:
        mapping = {Endpoint::Direction::LEFT, Endpoint::Direction::RIGHT,
                   Endpoint::Direction::UP, Endpoint::Direction::DOWN};
        break;
    }
    return mapping[dirIndex];
  }

  int get_num_grid_points() { return ((_width + 1) / 2) * ((_height + 1) / 2); }
  int get_num_grid_blocks() { return (_width / 2) * (_height / 2); }
  int get_parity() { return (get_num_grid_points() + 1) % 2; }

 private:
  std::pair<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::pair<int, int> dloc_to_xy(int location) {
    int height2 = (_height - 3) / 2;
    int width2 = _width / 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 / 2;

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

  int locate_segment(int x, int y, std::vector<int>& connections_a,
                     std::vector<int>& connections_b) {
    for (int i = 0; i < connections_a.size(); i++) {
      std::pair<int, int> coord1 = loc_to_xy(connections_a[i]);
      std::pair<int, int> coord2 = loc_to_xy(connections_b[i]);
      int x1 = coord1.first, y1 = coord1.second, x2 = coord2.first,
          y2 = coord2.second;
      if ((x1 == x - 1 && x2 == x + 1 && y1 == y && y2 == y) ||
          (y1 == y - 1 && y2 == y + 1 && x1 == x && x2 == x)) {
        return i;
      }
    }
    return -1;
  }

  bool break_segment(int x, int y, std::vector<int>& connections_a,
                     std::vector<int>& connections_b,
                     std::vector<float>& intersections,
                     std::vector<int>& intersectionFlags) {
    int i = locate_segment(x, y, connections_a, connections_b);
    if (i == -1) {
      return false;
    }
    int other_connection = connections_b[i];
    connections_b[i] = static_cast<int>(intersectionFlags.size());
    connections_a.push_back(static_cast<int>(intersectionFlags.size()));
    connections_b.push_back(other_connection);
    intersections.push_back(static_cast<float>(minx + x * unitWidth));
    intersections.push_back(
        static_cast<float>(miny + (_height - 1 - y) * unitHeight));
    intersectionFlags.push_back(_grid[x][y]);
    return true;
  }

  bool break_segment_gap(int x, int y, std::vector<int>& connections_a,
                         std::vector<int>& connections_b,
                         std::vector<float>& intersections,
                         std::vector<int>& intersectionFlags) {
    int i = locate_segment(x, y, connections_a, connections_b);
    if (i == -1) {
      return false;
    }
    int other_connection = connections_b[i];
    connections_b[i] = static_cast<int>(intersectionFlags.size() + 1);
    connections_a.push_back(other_connection);
    connections_b.push_back(static_cast<int>(intersectionFlags.size()));
    if (!(_grid[x][y] & IntersectionFlags::GAP)) {
      _grid[x][y] |=
          (x % 2 == 0 ? IntersectionFlags::COLUMN : IntersectionFlags::ROW);
      connections_a.push_back(static_cast<int>(intersectionFlags.size()));
      connections_b.push_back(static_cast<int>(intersectionFlags.size() + 1));
    }
    double xOffset = _grid[x][y] & IntersectionFlags::ROW ? 0.5 : 0;
    double yOffset = _grid[x][y] & IntersectionFlags::COLUMN ? 0.5 : 0;
    intersections.push_back(
        static_cast<float>(minx + (x + xOffset) * unitWidth));
    intersections.push_back(
        static_cast<float>(miny + (_height - 1 - y - yOffset) * unitHeight));
    intersections.push_back(
        static_cast<float>(minx + (x - xOffset) * unitWidth));
    intersections.push_back(
        static_cast<float>(miny + (_height - 1 - y + yOffset) * unitHeight));
    intersectionFlags.push_back(_grid[x][y]);
    intersectionFlags.push_back(_grid[x][y]);
    return true;
  }

  int _width, _height;

  std::vector<std::vector<int>> _grid;
  std::vector<Point> _startpoints;
  std::vector<Endpoint> _endpoints;
  float minx, miny, maxx, maxy, unitWidth, unitHeight;
  int _style;
  bool _resized;
  bool _invisible_symmetry = false;
};

#endif /* end of include guard: PANEL_H_FC471D68 */