redscript-mc 1.2.24 → 1.2.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/.github/workflows/publish-extension-on-ci.yml +1 -0
  2. package/dist/__tests__/cli.test.js +1 -1
  3. package/dist/__tests__/codegen.test.js +12 -6
  4. package/dist/__tests__/e2e.test.js +6 -6
  5. package/dist/__tests__/lowering.test.js +8 -8
  6. package/dist/__tests__/optimizer.test.js +31 -0
  7. package/dist/__tests__/stdlib-advanced.test.d.ts +4 -0
  8. package/dist/__tests__/stdlib-advanced.test.js +264 -0
  9. package/dist/__tests__/stdlib-math.test.d.ts +7 -0
  10. package/dist/__tests__/stdlib-math.test.js +352 -0
  11. package/dist/__tests__/stdlib-vec.test.d.ts +4 -0
  12. package/dist/__tests__/stdlib-vec.test.js +264 -0
  13. package/dist/ast/types.d.ts +17 -1
  14. package/dist/codegen/mcfunction/index.js +159 -18
  15. package/dist/codegen/var-allocator.d.ts +17 -0
  16. package/dist/codegen/var-allocator.js +33 -3
  17. package/dist/compile.d.ts +14 -0
  18. package/dist/compile.js +62 -5
  19. package/dist/index.js +20 -1
  20. package/dist/ir/types.d.ts +4 -0
  21. package/dist/lexer/index.d.ts +1 -1
  22. package/dist/lexer/index.js +1 -0
  23. package/dist/lowering/index.d.ts +5 -0
  24. package/dist/lowering/index.js +83 -10
  25. package/dist/optimizer/dce.js +21 -5
  26. package/dist/optimizer/passes.js +18 -6
  27. package/dist/optimizer/structure.js +7 -0
  28. package/dist/parser/index.d.ts +5 -0
  29. package/dist/parser/index.js +43 -2
  30. package/dist/runtime/index.d.ts +6 -0
  31. package/dist/runtime/index.js +109 -9
  32. package/editors/vscode/package-lock.json +3 -3
  33. package/editors/vscode/package.json +1 -1
  34. package/package.json +1 -1
  35. package/src/__tests__/cli.test.ts +1 -1
  36. package/src/__tests__/codegen.test.ts +12 -6
  37. package/src/__tests__/e2e.test.ts +6 -6
  38. package/src/__tests__/lowering.test.ts +8 -8
  39. package/src/__tests__/optimizer.test.ts +33 -0
  40. package/src/__tests__/stdlib-advanced.test.ts +259 -0
  41. package/src/__tests__/stdlib-math.test.ts +374 -0
  42. package/src/__tests__/stdlib-vec.test.ts +259 -0
  43. package/src/ast/types.ts +11 -1
  44. package/src/codegen/mcfunction/index.ts +148 -19
  45. package/src/codegen/var-allocator.ts +36 -3
  46. package/src/compile.ts +72 -5
  47. package/src/index.ts +21 -1
  48. package/src/ir/types.ts +2 -0
  49. package/src/lexer/index.ts +2 -1
  50. package/src/lowering/index.ts +96 -10
  51. package/src/optimizer/dce.ts +22 -5
  52. package/src/optimizer/passes.ts +18 -5
  53. package/src/optimizer/structure.ts +6 -1
  54. package/src/parser/index.ts +47 -2
  55. package/src/runtime/index.ts +108 -10
  56. package/src/stdlib/advanced.mcrs +249 -0
  57. package/src/stdlib/math.mcrs +259 -19
  58. package/src/stdlib/vec.mcrs +246 -0
@@ -0,0 +1,246 @@
1
+ // vec.mcrs — 2D and 3D vector math for RedScript datapacks.
2
+ //
3
+ // Fixed-point convention: scale = 1000 (same as math.mcrs)
4
+ // A unit vector has components in [-1000, 1000].
5
+ // A position / displacement in block coordinates uses raw integers.
6
+ //
7
+ // Dependency: import "math.mcrs" before this file (or compile with librarySources).
8
+ // Requires: abs, sqrt_fixed, sin_fixed, cos_fixed, lerp, mulfix, length2d_fixed
9
+ //
10
+ // Phase 1: 2D geometry (dot, cross, distance, length, normalize, lerp)
11
+ // Phase 2: 2D direction (rotate, atan2)
12
+ // Phase 3: 3D geometry (dot, cross, length, distance, manhattan, chebyshev)
13
+
14
+ module library;
15
+
16
+ // ─── Phase 1: 2D geometry ────────────────────────────────────────────────────
17
+
18
+ // Dot product of two 2D integer vectors.
19
+ // dot2d(3, 4, 3, 4) == 25
20
+ fn dot2d(ax: int, ay: int, bx: int, by: int) -> int {
21
+ return ax * bx + ay * by;
22
+ }
23
+
24
+ // Z-component of the 3D cross product (ax*by − ay*bx).
25
+ // Positive: b is counter-clockwise from a.
26
+ // cross2d(1, 0, 0, 1) == 1
27
+ fn cross2d(ax: int, ay: int, bx: int, by: int) -> int {
28
+ return ax * by - ay * bx;
29
+ }
30
+
31
+ // Euclidean length of a 2D integer vector, result in fixed-point (×1000).
32
+ // length2d_fixed(3, 4) == 5000 (√25 × 1000)
33
+ // Note: |x|, |y| ≤ ~1000 to avoid overflow; scale larger vectors down first.
34
+ fn length2d_fixed(x: int, y: int) -> int {
35
+ return sqrt_fixed((x * x + y * y) * 1000);
36
+ }
37
+
38
+ // Euclidean distance between two integer-coordinate points, result ×1000.
39
+ // distance2d_fixed(0, 0, 3, 4) == 5000
40
+ fn distance2d_fixed(x1: int, y1: int, x2: int, y2: int) -> int {
41
+ let dx: int = x1 - x2;
42
+ let dy: int = y1 - y2;
43
+ return length2d_fixed(dx, dy);
44
+ }
45
+
46
+ // Manhattan distance (taxicab): |dx| + |dy|.
47
+ // manhattan(0, 0, 3, 4) == 7
48
+ fn manhattan(x1: int, y1: int, x2: int, y2: int) -> int {
49
+ let dx: int = x1 - x2;
50
+ let dy: int = y1 - y2;
51
+ if (dx < 0) { dx = 0 - dx; }
52
+ if (dy < 0) { dy = 0 - dy; }
53
+ return dx + dy;
54
+ }
55
+
56
+ // Chebyshev distance (chess-king): max(|dx|, |dy|).
57
+ // chebyshev(0, 0, 3, 4) == 4
58
+ fn chebyshev(x1: int, y1: int, x2: int, y2: int) -> int {
59
+ let dx: int = x1 - x2;
60
+ let dy: int = y1 - y2;
61
+ if (dx < 0) { dx = 0 - dx; }
62
+ if (dy < 0) { dy = 0 - dy; }
63
+ if (dx > dy) { return dx; }
64
+ return dy;
65
+ }
66
+
67
+ // X-component of the normalized unit vector (× 1000).
68
+ // normalize2d_x(3, 4) == 600 (3/5 × 1000)
69
+ // Returns 0 for the zero vector.
70
+ fn normalize2d_x(x: int, y: int) -> int {
71
+ let len: int = length2d_fixed(x, y);
72
+ if (len == 0) { return 0; }
73
+ return x * 1000000 / len;
74
+ }
75
+
76
+ // Y-component of the normalized unit vector (× 1000).
77
+ // normalize2d_y(3, 4) == 800 (4/5 × 1000)
78
+ fn normalize2d_y(x: int, y: int) -> int {
79
+ let len: int = length2d_fixed(x, y);
80
+ if (len == 0) { return 0; }
81
+ return y * 1000000 / len;
82
+ }
83
+
84
+ // Linearly interpolate between vectors a=(ax,ay) and b=(bx,by), return x.
85
+ // t in [0, 1000] (fixed-point).
86
+ // lerp2d_x(0, 0, 100, 200, 500) == 50
87
+ fn lerp2d_x(ax: int, ay: int, bx: int, by: int, t: int) -> int {
88
+ return ax + (bx - ax) * t / 1000;
89
+ }
90
+
91
+ // Linearly interpolate between vectors a and b, return y.
92
+ // lerp2d_y(0, 0, 100, 200, 500) == 100
93
+ fn lerp2d_y(ax: int, ay: int, bx: int, by: int, t: int) -> int {
94
+ return ay + (by - ay) * t / 1000;
95
+ }
96
+
97
+ // ─── Phase 2: 2D direction ────────────────────────────────────────────────────
98
+
99
+ // tan lookup table for atan2_fixed binary search
100
+ fn _atan_init() {
101
+ storage_set_array("math:tables", "tan",
102
+ "[0, 17, 35, 52, 70, 87, 105, 123, 141, 158, 176, 194, 213, 231, 249, 268, 287, 306, 325, 344, 364, 384, 404, 424, 445, 466, 488, 510, 532, 554, 577, 601, 625, 649, 675, 700, 727, 754, 781, 810, 839, 869, 900, 933, 966, 1000]"
103
+ );
104
+ }
105
+
106
+ // Angle of the vector (y, x) in integer degrees [0, 359].
107
+ // Uses binary search on the tan table → O(log 46) per call.
108
+ //
109
+ // atan2_fixed(0, 1) == 0 (east)
110
+ // atan2_fixed(1, 0) == 90
111
+ // atan2_fixed(0, -1) == 180 (west)
112
+ // atan2_fixed(-1, 0) == 270
113
+ // atan2_fixed(1, 1) == 45
114
+ //
115
+ // Inputs clamped to ≤ 46340 to avoid overflow. For larger vectors:
116
+ // atan2_fixed(dy / 1000, dx / 1000)
117
+ @require_on_load(_atan_init)
118
+ fn atan2_fixed(y: int, x: int) -> int {
119
+ if (x == 0) {
120
+ if (y > 0) { return 90; }
121
+ if (y < 0) { return 270; }
122
+ return 0;
123
+ }
124
+ if (y == 0) {
125
+ if (x > 0) { return 0; }
126
+ return 180;
127
+ }
128
+
129
+ let ax: int = x;
130
+ if (ax < 0) { ax = 0 - ax; }
131
+ let ay: int = y;
132
+ if (ay < 0) { ay = 0 - ay; }
133
+
134
+ if (ax > 46340) { ax = 46340; }
135
+ if (ay > 46340) { ay = 46340; }
136
+
137
+ let swapped: int = 0;
138
+ if (ay > ax) {
139
+ let tmp: int = ax;
140
+ ax = ay;
141
+ ay = tmp;
142
+ swapped = 1;
143
+ }
144
+
145
+ let lo: int = 0;
146
+ let hi: int = 45;
147
+ while (lo < hi) {
148
+ let mid: int = (lo + hi + 1) / 2;
149
+ let t: int = storage_get_int("math:tables", "tan", mid);
150
+ if (t * ax > ay * 1000) {
151
+ hi = mid - 1;
152
+ } else {
153
+ lo = mid;
154
+ }
155
+ }
156
+ let theta: int = lo;
157
+ if (swapped == 1) { theta = 90 - theta; }
158
+
159
+ if (x > 0) {
160
+ if (y >= 0) { return theta; }
161
+ return 360 - theta;
162
+ }
163
+ if (y >= 0) { return 180 - theta; }
164
+ return 180 + theta;
165
+ }
166
+
167
+ // Rotate a 2D vector (x, y) by deg degrees, return the new X component.
168
+ // Uses sin/cos table from math.mcrs — @require_on_load(_math_init) is
169
+ // automatically pulled in because this function calls sin_fixed/cos_fixed.
170
+ // rotate2d_x(1000, 0, 90) == 0 (unit vector east, rotated 90° = north)
171
+ // rotate2d_x(1000, 0, 45) == 707 (cos 45° × 1000)
172
+ fn rotate2d_x(x: int, y: int, deg: int) -> int {
173
+ // x' = x*cos(deg) - y*sin(deg) (all in fixed-point, /1000 via mulfix)
174
+ return mulfix(x, cos_fixed(deg)) - mulfix(y, sin_fixed(deg));
175
+ }
176
+
177
+ // Rotate a 2D vector (x, y) by deg degrees, return the new Y component.
178
+ // rotate2d_y(1000, 0, 90) == 1000 (unit vector east, rotated 90° = north)
179
+ fn rotate2d_y(x: int, y: int, deg: int) -> int {
180
+ // y' = x*sin(deg) + y*cos(deg)
181
+ return mulfix(x, sin_fixed(deg)) + mulfix(y, cos_fixed(deg));
182
+ }
183
+
184
+ // ─── Phase 3: 3D geometry ────────────────────────────────────────────────────
185
+
186
+ // Dot product of two 3D integer vectors.
187
+ fn dot3d(ax: int, ay: int, az: int, bx: int, by: int, bz: int) -> int {
188
+ return ax * bx + ay * by + az * bz;
189
+ }
190
+
191
+ // Cross product X component: ay*bz − az*by
192
+ fn cross3d_x(ax: int, ay: int, az: int, bx: int, by: int, bz: int) -> int {
193
+ return ay * bz - az * by;
194
+ }
195
+
196
+ // Cross product Y component: az*bx − ax*bz
197
+ fn cross3d_y(ax: int, ay: int, az: int, bx: int, by: int, bz: int) -> int {
198
+ return az * bx - ax * bz;
199
+ }
200
+
201
+ // Cross product Z component: ax*by − ay*bx
202
+ fn cross3d_z(ax: int, ay: int, az: int, bx: int, by: int, bz: int) -> int {
203
+ return ax * by - ay * bx;
204
+ }
205
+
206
+ // Euclidean length of a 3D integer vector, result in fixed-point (×1000).
207
+ // length3d_fixed(1, 1, 1) == 1732 (√3 × 1000)
208
+ // Note: |x|, |y|, |z| ≤ ~800 to keep x²+y²+z² ≤ ~2,000,000 (safe for *1000).
209
+ fn length3d_fixed(x: int, y: int, z: int) -> int {
210
+ return sqrt_fixed((x * x + y * y + z * z) * 1000);
211
+ }
212
+
213
+ // Euclidean distance between two 3D integer points, result ×1000.
214
+ fn distance3d_fixed(x1: int, y1: int, z1: int, x2: int, y2: int, z2: int) -> int {
215
+ let dx: int = x1 - x2;
216
+ let dy: int = y1 - y2;
217
+ let dz: int = z1 - z2;
218
+ return length3d_fixed(dx, dy, dz);
219
+ }
220
+
221
+ // Manhattan distance in 3D.
222
+ // manhattan3d(0,0,0, 1,2,3) == 6
223
+ fn manhattan3d(x1: int, y1: int, z1: int, x2: int, y2: int, z2: int) -> int {
224
+ let dx: int = x1 - x2;
225
+ let dy: int = y1 - y2;
226
+ let dz: int = z1 - z2;
227
+ if (dx < 0) { dx = 0 - dx; }
228
+ if (dy < 0) { dy = 0 - dy; }
229
+ if (dz < 0) { dz = 0 - dz; }
230
+ return dx + dy + dz;
231
+ }
232
+
233
+ // Chebyshev distance in 3D.
234
+ // chebyshev3d(0,0,0, 3,1,2) == 3
235
+ fn chebyshev3d(x1: int, y1: int, z1: int, x2: int, y2: int, z2: int) -> int {
236
+ let dx: int = x1 - x2;
237
+ let dy: int = y1 - y2;
238
+ let dz: int = z1 - z2;
239
+ if (dx < 0) { dx = 0 - dx; }
240
+ if (dy < 0) { dy = 0 - dy; }
241
+ if (dz < 0) { dz = 0 - dz; }
242
+ let m: int = dx;
243
+ if (dy > m) { m = dy; }
244
+ if (dz > m) { m = dz; }
245
+ return m;
246
+ }