diff options
Diffstat (limited to 'Source/Panel.cpp')
-rw-r--r-- | Source/Panel.cpp | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/Source/Panel.cpp b/Source/Panel.cpp new file mode 100644 index 0000000..0f62664 --- /dev/null +++ b/Source/Panel.cpp | |||
@@ -0,0 +1,134 @@ | |||
1 | #include "Panel.h" | ||
2 | #include "Random.h" | ||
3 | |||
4 | Decoration::Decoration(int shape) { | ||
5 | _shape = shape; | ||
6 | } | ||
7 | |||
8 | int Decoration::GetValue() { | ||
9 | return _shape; | ||
10 | } | ||
11 | |||
12 | Intersection::Intersection(float x, float y, int flags) { | ||
13 | _x = x; | ||
14 | _y = y; | ||
15 | _flags = flags; | ||
16 | } | ||
17 | |||
18 | int Intersection::GetValue() { | ||
19 | return _flags; | ||
20 | } | ||
21 | |||
22 | Panel::Panel(int id) { | ||
23 | _width = _memory.ReadPanelData<int>(id, GRID_SIZE_X, 1)[0]; | ||
24 | _height = _memory.ReadPanelData<int>(id, GRID_SIZE_Y, 1)[0]; | ||
25 | |||
26 | ReadIntersections(id); | ||
27 | ReadDecorations(id); | ||
28 | } | ||
29 | |||
30 | void Panel::Write(int id) { | ||
31 | WriteIntersections(id); | ||
32 | WriteDecorations(id); | ||
33 | |||
34 | _memory.WritePanelData<int>(id, GRID_SIZE_X, {_width}); | ||
35 | _memory.WritePanelData<int>(id, GRID_SIZE_Y, {_height}); | ||
36 | } | ||
37 | |||
38 | void Panel::Random() { | ||
39 | for (auto& row : _decorations) { | ||
40 | for (auto& cell : row) { | ||
41 | cell._shape &= 0xFFFFFFF0; | ||
42 | cell._shape |= Random::RandInt(1, 10); | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | |||
47 | void Panel::ReadDecorations(int id) { | ||
48 | int numDecorations = _memory.ReadPanelData<int>(id, NUM_DECORATIONS, 1)[0]; | ||
49 | std::vector<int> decorations = _memory.ReadArray<int>(id, DECORATIONS, numDecorations); | ||
50 | _decorations.resize(_width - 1); | ||
51 | for (int x=0; x<_width-1; x++) { | ||
52 | _decorations[x].resize(_height - 1); | ||
53 | for (int y=0; y<_height-1; y++) { | ||
54 | int i = x * (_height - 1) + y; | ||
55 | _decorations[x][y] = Decoration(decorations[i]); | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | |||
60 | void Panel::WriteDecorations(int id) { | ||
61 | std::vector<int> flattenedDecorations; | ||
62 | for (std::vector<Decoration> row : _decorations) { | ||
63 | for (Decoration decoration : row) { | ||
64 | flattenedDecorations.push_back(decoration.GetValue()); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | _memory.WritePanelData<int>(id, NUM_DECORATIONS, {static_cast<int>(flattenedDecorations.size())}); | ||
69 | _memory.WriteArray<int>(id, DECORATIONS, flattenedDecorations); | ||
70 | } | ||
71 | |||
72 | void Panel::ReadIntersections(int id) { | ||
73 | int numIntersections = _memory.ReadPanelData<int>(id, NUM_DOTS, 1)[0]; | ||
74 | std::vector<float> intersections = _memory.ReadArray<float>(id, DOT_POSITIONS, numIntersections*2); | ||
75 | std::vector<int> intersectionFlags = _memory.ReadArray<int>(id, DOT_FLAGS, numIntersections); | ||
76 | _intersections.resize(_width); | ||
77 | int i=0; | ||
78 | for (int y=0; y<_height; y++) { | ||
79 | for (int x=0; x<_width; x++) { | ||
80 | _intersections[x].resize(_height); | ||
81 | _intersections[x][y] = Intersection(intersections[2*i], intersections[2*i+1], intersectionFlags[i]); | ||
82 | i++; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | // Iterate the remaining intersections (either endpoints or gaps) | ||
87 | for (; i < numIntersections; i++) { | ||
88 | if (intersectionFlags[i] & Intersection::Flags::IS_ENDPOINT) { | ||
89 | _endpoints.push_back(Intersection(intersections[2*i], intersections[2*i+1], intersectionFlags[i])); | ||
90 | } | ||
91 | if (intersectionFlags[i] & Intersection::Flags::IS_GAP) { | ||
92 | _gaps.push_back(Intersection(intersections[2*i], intersections[2*i+1], intersectionFlags[i])); | ||
93 | } | ||
94 | } | ||
95 | } | ||
96 | |||
97 | void Panel::WriteIntersections(int id) { | ||
98 | std::vector<float> intersections; | ||
99 | std::vector<int> intersectionFlags; | ||
100 | std::pair<std::vector<int>, std::vector<int>> connections; | ||
101 | |||
102 | for (int y=0; y<_height; y++) { | ||
103 | for (int x=0; x<_width; x++) { | ||
104 | Intersection intersection = _intersections[x][y]; | ||
105 | intersections.push_back(intersection._x); | ||
106 | intersections.push_back(intersection._y); | ||
107 | intersectionFlags.push_back(intersection._flags); | ||
108 | if (y > 0) { | ||
109 | connections.first.push_back(y * _width + x); | ||
110 | connections.second.push_back((y - 1) * _width + x); | ||
111 | } | ||
112 | if (x > 0) { | ||
113 | connections.first.push_back(y * _width + x); | ||
114 | connections.second.push_back(y * _width + (x - 1)); | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | |||
119 | // Endpoints go here :( | ||
120 | |||
121 | int a = _memory.ReadPanelData<int>(id, NUM_DOTS, 1)[0]; | ||
122 | std::vector<float> b = _memory.ReadArray<float>(id, DOT_POSITIONS, a*2); | ||
123 | std::vector<int> c = _memory.ReadArray<int>(id, DOT_FLAGS, a); | ||
124 | std::pair<std::vector<int>, std::vector<int>> d; | ||
125 | d.first = _memory.ReadArray<int>(id, DOT_CONNECTION_A, a); | ||
126 | d.second = _memory.ReadArray<int>(id, DOT_CONNECTION_B, a); | ||
127 | |||
128 | _memory.WritePanelData<int>(id, NUM_DOTS, {_width * _height}); | ||
129 | _memory.WriteArray<float>(id, DOT_POSITIONS, intersections); | ||
130 | _memory.WriteArray<int>(id, DOT_FLAGS, intersectionFlags); | ||
131 | _memory.WriteArray<int>(id, DOT_CONNECTION_A, connections.first); | ||
132 | _memory.WriteArray<int>(id, DOT_CONNECTION_B, connections.second); | ||
133 | } | ||
134 | |||