1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <sstream>
6
7#include "position.h"
8
9class WalkMatrix {
10public:
11
12 void updatePosition(const Position& newPos)
13 {
14 int32_t xdiff = pos.x - newPos.x;
15 int32_t ydiff = pos.y - newPos.y;
16 if (pos.z != newPos.z || std::abs(xdiff) > 6 || std::abs(ydiff) > 6) {
17 pos = newPos;
18 return clear();
19 }
20 pos = newPos;
21
22 if (xdiff < 0) {
23 for (int x = 0; x < 7; ++x) {
24 for (int y = 0; y < 7; ++y) {
25 if (x - xdiff > 6) {
26 matrix[x][y] = 0;
27 } else {
28 matrix[x][y] = matrix[x - xdiff][y];
29 }
30 }
31 }
32 }
33 if (xdiff > 0) {
34 for (int x = 6; x >= 0; --x) {
35 for (int y = 6; y >= 0; --y) {
36 if (x - xdiff < 0) {
37 matrix[x][y] = 0;
38 } else {
39 matrix[x][y] = matrix[x - xdiff][y];
40 }
41 }
42 }
43 }
44 if (ydiff < 0) {
45 for (int x = 0; x < 7; ++x) {
46 for (int y = 0; y < 7; ++y) {
47 if (y - ydiff > 6) {
48 matrix[x][y] = 0;
49 } else {
50 matrix[x][y] = matrix[x][y - ydiff];
51 }
52 }
53 }
54 }
55 if (ydiff > 0) {
56 for (int x = 6; x >= 0; --x) {
57 for (int y = 6; y >= 0; --y) {
58 if (y - ydiff < 0) {
59 matrix[x][y] = 0;
60 } else {
61 matrix[x][y] = matrix[x][y - ydiff];
62 }
63 }
64 }
65 }
66 }
67
68 bool inRange(const Position& pos2)
69 {
70 if (pos.z != pos2.z) return false;
71 int32_t xdiff = pos.x - pos2.x;
72 int32_t ydiff = pos.y - pos2.y;
73 if (std::abs(xdiff) > 3 || std::abs(ydiff) > 3) return false;
74 return true;
75 }
76
77 int32_t update(const Position& pos2, int32_t value = 0)
78 {
79 if (pos.z != pos2.z) return 0;
80 int32_t xdiff = pos.x - pos2.x;
81 int32_t ydiff = pos.y - pos2.y;
82 if (std::abs(xdiff) > 3 || std::abs(ydiff) > 3) return 0;
83 if (value != 0) {
84 matrix[3 - xdiff][3 - ydiff] = value;
85 return value;
86 }
87 counter += 1;
88 matrix[3 - xdiff][3 - ydiff] = counter;
89 return counter;
90 }
91
92 int32_t get(const Position& pos2)
93 {
94 if (pos.z != pos2.z) return 0;
95 int32_t xdiff = pos.x - pos2.x;
96 int32_t ydiff = pos.y - pos2.y;
97 if (std::abs(xdiff) > 3 || std::abs(ydiff) > 3) return 0;
98 return matrix[3 - xdiff][3 - ydiff];
99 }
100
101 void clear()
102 {
103 for (auto& x : matrix) {
104 for (auto& y : x) {
105 y = 0;
106 }
107 }
108 }
109
110 uint32_t reset(uint32_t value = 0)
111 {
112 if (value == 0) {
113 counter += 1;
114 value = counter;
115 }
116 for (auto& x : matrix) {
117 for (auto& y : x) {
118 y = value;
119 }
120 }
121 return value;
122 }
123
124 std::string dump()
125 {
126 std::stringstream ss;
127 for (int y = 0; y < 7; ++y) {
128 for (int x = 0; x < 7; ++x) {
129 ss << matrix[x][y] << "\t";
130 }
131 ss << "\n";
132 }
133 return ss.str();
134 }
135
136private:
137 Position pos;
138 int32_t matrix[7][7] = { { 0 } };
139 int32_t counter = 0;
140};