-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced-grid3-holder.ts
271 lines (225 loc) · 7.57 KB
/
advanced-grid3-holder.ts
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
import { ReadonlySimpleVector3 } from "../../../../math/simple-vector3";
import { Grid3MapHolder } from "./grid3-map-holder";
export class AdvancedGrid3Holder<T extends (string | number | Record<string, unknown>)> {
private readonly _data = Grid3MapHolder.initEmpty<T>(this.x, this.y, this.z);
public constructor(
private readonly x: number,
private readonly y: number,
private readonly z: number,
private _origin = {x: 0, y: 0, z: 0},
) {
}
public forEach(callback: (block: T, x: number, y: number, z: number) => void | boolean): void {
this._data.forEach(callback);
}
public get(x: number, y: number, z: number): T {
return this._data.get(x, y, z);
}
public set(x: number, y: number, z: number, item: T): void {
return this._data.set(x, y, z, item);
}
public get origin(): ReadonlySimpleVector3 {
return this._origin;
}
public setOrigin(x: number, y: number, z: number): void {
this._origin = {x, y, z};
}
public fill(value: T | ((x: number, y: number, z: number) => T)): void {
if (typeof value === "function") {
for (let i = 0; i < this.x; i++) {
for (let j = 0; j < this.y; j++) {
for (let k = 0; k < this.z; k++) {
this._data.set(i, j, k, value(i, j, k));
}
}
}
return;
}
for (let i = 0; i < this.x; i++) {
for (let j = 0; j < this.y; j++) {
for (let k = 0; k < this.z; k++) {
this._data.set(i, j, k, value);
}
}
}
}
private setRelativeBlock(x: number, y: number, z: number, block: T): boolean {
if (!this.isRelativePointInside(x, y, z)) {
return false;
}
this._data.set(x, y, z, block);
return true;
}
public swap(
relX1: number,
relY1: number,
relZ1: number,
relX2: number,
relY2: number,
relZ2: number,
): boolean {
if (!this.isRelativePointInside(relX1, relY1, relZ1)) {
return false;
}
if (!this.isRelativePointInside(relX2, relY2, relZ2)) {
return false;
}
// TODO: use this._data.transform;
const block1 = this._data.get(relX1, relY1, relZ1);
const block2 = this._data.get(relX2, relY2, relZ2);
this._data.set(relX1, relY1, relZ1, block2);
this._data.set(relX2, relY2, relZ2, block1);
return true;
}
public mirrorYZ(): void {
const halfX = Math.floor(this.x / 2);
for (let x = 0; x < halfX; x++) {
for (let y = 0; y < this.y; y++) {
for (let z = 0; z < this.z; z++) {
this.swap(x, y, z, this.x - x - 1, y, z);
}
}
}
}
// TODO: optimize this
public rotateCCW(): void {
const newData = Grid3MapHolder.initEmpty<T>(this.x, this.y, this.z);
for (let x = 0; x < this.x; x++) {
const newZ = this.x - x - 1;
for (let z = 0; z < this.z; z++) {
const newX = z;
for (let y = 0; y < this.y; y++) {
newData.set(newX, y, newZ, this._data.get(x, y, z));
}
}
}
this._data.setHolder(newData);
}
// TODO: optimize this
public rotateCW(): void {
const newData = Grid3MapHolder.initEmpty<T>(this.x, this.y, this.z);
for (let x = 0; x < this.x; x++) {
const newZ = x;
for (let z = 0; z < this.z; z++) {
const newX = this.z - z - 1;
for (let y = 0; y < this.y; y++) {
newData.set(newX, y, newZ, this._data.get(x, y, z));
}
}
}
this._data.setHolder(newData);
}
public mirrorXZ(): void {
const halfY = Math.floor(this.y / 2);
for (let x = 0; x < this.x; x++) {
for (let y = 0; y < halfY; y++) {
for (let z = 0; z < this.z; z++) {
this.swap(x, y, z, x, this.y - y - 1, z);
}
}
}
}
public mirrorXY(): void {
const halfZ = Math.floor(this.z / 2);
for (let x = 0; x < this.x; x++) {
for (let y = 0; y < this.y; y++) {
for (let z = 0; z < halfZ; z++) {
this.swap(x, y, z, x, y, this.z - z - 1);
}
}
}
}
public drawLine(
relX1: number,
relY1: number,
relZ1: number,
relX2: number,
relY2: number,
relZ2: number,
provider: (x: number, y: number, z: number) => T,
): void {
// Bresenham-3D algorithm for drawing lines:
const dx = Math.abs(relX2 - relX1);
const dy = Math.abs(relY2 - relY1);
const dz = Math.abs(relZ2 - relZ1);
const sx = (relX1 < relX2) ? 1 : -1;
const sy = (relY1 < relY2) ? 1 : -1;
const sz = (relZ1 < relZ2) ? 1 : -1;
if (dx >= Math.max(dy, dz)) {
let yd = dy - dx / 2;
let zd = dz - dx / 2;
for (; ;) {
this.setRelativeBlock(relX1, relY1, relZ1, provider(relX1, relY1, relZ1));
if (relX1 === relX2) {
break;
}
if (yd >= 0) {
relY1 += sy;
yd -= dx;
}
if (zd >= 0) {
relZ1 += sz;
zd -= dx;
}
// move along x
relX1 += sx;
yd += dy;
zd += dz;
}
} else if (dy >= Math.max(dx, dz)) {
let xd = dx - dy / 2;
let zd = dz - dy / 2;
for (; ;) {
this.setRelativeBlock(relX1, relY1, relZ1, provider(relX1, relY1, relZ1));
if (relY1 === relY2) {
break;
}
if (xd >= 0) {
relX1 += sx;
xd -= dy;
}
if (zd >= 0) {
relZ1 += sz;
zd -= dy;
}
// move along y
relY1 += sy;
xd += dx;
zd += dz;
}
} else {
// z dominant
console.assert(dz >= Math.max(dx, dy));
let xd = dx - dz / 2;
let yd = dy - dz / 2;
for (; ;) {
this.setRelativeBlock(relX1, relY1, relZ1, provider(relX1, relY1, relZ1));
if (relZ1 === relZ2) {
break;
}
if (xd >= 0) {
relX1 += sx;
xd -= dz;
}
if (yd >= 0) {
relY1 += sy;
yd -= dz;
}
// move along z
relZ1 += sz;
xd += dx;
yd += dy;
}
} // if (which dimension is dominant)
}
public isRelativePointInside(x: number, y: number, z: number): boolean {
return x >= 0 && x < this.x
&& y >= 0 && y < this.y
&& z >= 0 && z < this.z;
}
public isPointInside(x: number, y: number, z: number): boolean {
return x >= this._origin.x && x < this._origin.x + this.x
&& y >= this._origin.y && y < this._origin.y + this.y
&& z >= this._origin.z && z < this._origin.z + this.z;
}
}