@series-inc/rundot-syncplay 6.0.0-rc.71 → 6.0.0-rc.72

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 (38) hide show
  1. package/README.md +10 -0
  2. package/core/deterministic-stdlib.d.ts +21 -1
  3. package/core/native/fixed_math.cpp +161 -0
  4. package/core/native/fixed_math.hpp +12 -0
  5. package/core/native/kinetix-native-main.cpp +39 -1
  6. package/core/native/physics/physics.cpp +986 -18
  7. package/core/native/physics/physics.hpp +72 -0
  8. package/core/native/physics/world.cpp +428 -0
  9. package/core/native/physics/world.hpp +55 -1
  10. package/core/native/physics/world_abi.cpp +237 -1
  11. package/core/native/physics/world_abi.hpp +57 -0
  12. package/core/scripts/build-wasm-physics.mjs +4 -1
  13. package/core/src/kinetix-deterministic-stdlib.mjs +50 -0
  14. package/core/src/physics/abi-constants.mjs +1 -1
  15. package/core/src/physics/physics-profile-wasm-module.generated.mjs +6 -6
  16. package/core/src/physics/physics-profile.wasm +0 -0
  17. package/core/src/physics/physics-wasm-module.generated.mjs +6 -6
  18. package/core/src/physics/physics.wasm +0 -0
  19. package/core/src/physics/world.d.mts +1 -0
  20. package/core/src/physics/world.mjs +200 -6
  21. package/core/src/physics2d/physics2d-wasm-module.generated.mjs +1 -1
  22. package/core/world.d.ts +10 -0
  23. package/dist/index.d.ts +4 -3
  24. package/dist/index.js +2 -2
  25. package/dist/math.d.ts +19 -1
  26. package/dist/math.js +50 -0
  27. package/dist/modules/rts.js +12 -1
  28. package/dist/noise.d.ts +9 -0
  29. package/dist/noise.js +82 -0
  30. package/dist/periodic-domain.d.ts +10 -0
  31. package/dist/periodic-domain.js +13 -0
  32. package/dist/physics/3d.d.ts +31 -0
  33. package/dist/physics/3d.js +87 -13
  34. package/dist/r3f-render-adapter.d.ts +14 -0
  35. package/dist/r3f-render-adapter.js +85 -0
  36. package/dist/spatial/spatial-grid-3d.d.ts +5 -1
  37. package/dist/spatial/spatial-grid-3d.js +105 -3
  38. package/package.json +9 -4
package/README.md CHANGED
@@ -6,6 +6,16 @@ and `/physics/3d`. Both physics APIs use the canonical C++ core compiled to
6
6
  WASM. Syncplay owns inputs, prediction, confirmation, rollback policy, replay
7
7
  logs, late join, secrets, and transport.
8
8
 
9
+ ## Periodic math and presentation
10
+
11
+ The root entry provides periodic fixed math, value noise, fBm, render helpers,
12
+ and an optional periodic xz point grid. `createPhysicsWorld3D({ periodicDomain })`
13
+ adds periodic raycasts, overlaps, and shape casts with an `image` on each hit.
14
+ The domain is flat. These utilities do not add globe curvature or contact
15
+ across a seam. A periodic world needs native world ABI 15 on every peer. Read
16
+ the [periodic-space guide](docs/periodic-space.md) for units, limits, and
17
+ examples.
18
+
9
19
  ## Physics profiling
10
20
 
11
21
  Normal physics initialization loads the production WASM artifact. Game builds
@@ -84,6 +84,26 @@ export interface DeterministicMathApi {
84
84
  clamp(value: number, min: number, max: number): number;
85
85
  }
86
86
 
87
+ /**
88
+ * DeterministicMathApi plus the periodic integer operations. `value`,
89
+ * `reference`, and `min` must be integers in [minFixed, maxFixed]; `period` an
90
+ * integer in [1, 2^31 - 1]. Any other argument throws a RangeError. The image
91
+ * count and the delta can fall outside [minFixed, maxFixed] and are not
92
+ * rejected. Only a nearestPeriodicImage result outside [minFixed, maxFixed], or
93
+ * a wrapPeriodic interval with min + period - 1 > maxFixed, also throws.
94
+ * Nothing saturates.
95
+ */
96
+ export interface DeterministicPeriodicMathApi extends DeterministicMathApi {
97
+ /** min + floorMod(value - min, period), in [min, min + period - 1]. */
98
+ wrapPeriodic(value: number, min: number, period: number): number;
99
+ /** Image count k with value - k * period nearest to reference; ties go negative. */
100
+ periodicImageOffset(reference: number, value: number, period: number): number;
101
+ /** Reduced displacement, in [-floor(period / 2), period - 1 - floor(period / 2)]. */
102
+ periodicDelta(reference: number, value: number, period: number): number;
103
+ /** reference + periodicDelta(reference, value, period). */
104
+ nearestPeriodicImage(reference: number, value: number, period: number): number;
105
+ }
106
+
87
107
  export interface DeterministicRandomSnapshot {
88
108
  readonly state: number;
89
109
  }
@@ -92,7 +112,7 @@ export function createDeterministicMath(
92
112
  fixedScale?: number,
93
113
  minFixed?: number,
94
114
  maxFixed?: number,
95
- ): DeterministicMathApi;
115
+ ): DeterministicPeriodicMathApi;
96
116
 
97
117
  export class DeterministicRandom {
98
118
  state: number;
@@ -1,6 +1,7 @@
1
1
  #include "fixed_math.hpp"
2
2
 
3
3
  #include <cmath>
4
+ #include <cstdint>
4
5
  #include <limits>
5
6
 
6
7
  namespace rundot::fixedmath {
@@ -86,6 +87,39 @@ double sinTurnsImpl(const Config& c, double turns) {
86
87
  return normalizeZero(clampOne(c, -sinQuarter(c, c.fixedScale - t)));
87
88
  }
88
89
 
90
+ // The periodic operations bound their own arguments; isFixed checks only
91
+ // integrality and accepts 2^54, so it cannot carry the exact-int32 premise.
92
+ bool isPeriodicFixed(const Config& c, double v) {
93
+ return isFixed(v) && v >= c.minFixed && v <= c.maxFixed;
94
+ }
95
+
96
+ bool isPeriod(double v) { return isFixed(v) && v >= 1.0 && v <= 2147483647.0; }
97
+
98
+ // Floor modulus for |x| < 2^53 and period >= 1: exact, in [0, period), never -0.
99
+ // std::fmod is exact and takes the dividend's sign, like the JS % operator.
100
+ double floorModPeriod(double x, double period) {
101
+ return std::fmod(std::fmod(x, period) + period, period);
102
+ }
103
+
104
+ // A displacement reduced by one period count: delta = d - imageCount * period.
105
+ struct PeriodicReduction {
106
+ double delta;
107
+ double imageCount;
108
+ };
109
+
110
+ // The one place that reduces a displacement. Both arguments are in int32, so d
111
+ // is exact, and every step below is an exact integer-valued double operation.
112
+ PeriodicReduction reducePeriodic(const Config& c, double reference, double value, double period, bool& ok) {
113
+ if (!isPeriodicFixed(c, reference) || !isPeriodicFixed(c, value) || !isPeriod(period)) {
114
+ ok = false;
115
+ return PeriodicReduction{0.0, 0.0};
116
+ }
117
+ const double d = value - reference;
118
+ const double half = (period - std::fmod(period, 2.0)) / 2.0;
119
+ const double delta = floorModPeriod(d + half, period) - half;
120
+ return PeriodicReduction{normalizeZero(delta), normalizeZero((d - delta) / period)};
121
+ }
122
+
89
123
  // Math.PI is the nearest double to pi in both languages, so these constants are
90
124
  // bit-identical to the reference's this.toFixed(Math.PI / 4) and friends.
91
125
  constexpr double kPi = 3.141592653589793;
@@ -451,6 +485,34 @@ double angleDeltaTurns(const Config& c, double a, double b, bool& ok) {
451
485
  return d;
452
486
  }
453
487
 
488
+ double wrapPeriodic(const Config& c, double value, double min, double period, bool& ok) {
489
+ if (!isPeriodicFixed(c, value) || !isPeriodicFixed(c, min) || !isPeriod(period) ||
490
+ min + period - 1.0 > c.maxFixed) {
491
+ ok = false;
492
+ return 0.0;
493
+ }
494
+ return normalizeZero(min + floorModPeriod(value - min, period));
495
+ }
496
+
497
+ double periodicImageOffset(const Config& c, double reference, double value, double period, bool& ok) {
498
+ return reducePeriodic(c, reference, value, period, ok).imageCount;
499
+ }
500
+
501
+ double periodicDelta(const Config& c, double reference, double value, double period, bool& ok) {
502
+ return reducePeriodic(c, reference, value, period, ok).delta;
503
+ }
504
+
505
+ double nearestPeriodicImage(const Config& c, double reference, double value, double period, bool& ok) {
506
+ bool admitted = true;
507
+ const PeriodicReduction r = reducePeriodic(c, reference, value, period, admitted);
508
+ const double image = normalizeZero(reference + r.delta);
509
+ if (!admitted || image < c.minFixed || image > c.maxFixed) {
510
+ ok = false;
511
+ return 0.0;
512
+ }
513
+ return image;
514
+ }
515
+
454
516
  double turnsToRadians(const Config& c, double t, bool& ok) {
455
517
  if (!isFixed(t)) {
456
518
  ok = false;
@@ -745,4 +807,103 @@ double clamp(const Config& c, double value, double lo, double hi, bool& ok) {
745
807
  return jsMin(hi, jsMax(lo, value));
746
808
  }
747
809
 
810
+ namespace {
811
+
812
+ // Math.imul: the low 32 bits of the product. The 64-bit multiply has no integer
813
+ // promotion, and the unsigned narrowing wraps by definition.
814
+ std::uint32_t imul(std::uint32_t a, std::uint32_t b) {
815
+ return static_cast<std::uint32_t>(static_cast<std::uint64_t>(a) * b);
816
+ }
817
+
818
+ // `v | 0` for an integer v in int32: the two's-complement bits.
819
+ std::uint32_t int32Bits(double v) { return static_cast<std::uint32_t>(static_cast<std::int32_t>(v)); }
820
+
821
+ std::uint32_t hashUint32(std::uint32_t h) {
822
+ h = imul(h ^ (h >> 16), 0x45d9f3bu);
823
+ h = imul(h ^ (h >> 16), 0x45d9f3bu);
824
+ return h ^ (h >> 16);
825
+ }
826
+
827
+ std::uint32_t hash2(double x, double y) {
828
+ return hashUint32(imul(int32Bits(x), 0x1f1f1f1fu) ^ imul(int32Bits(y), 0x8da6b343u));
829
+ }
830
+
831
+ // Same grouping as the reference: h / 2^32 and h * 2 * scale are exact.
832
+ double hashToFixed(const Config& c, std::uint32_t h) {
833
+ return std::trunc((static_cast<double>(h) / 4294967296.0) * (2 * c.fixedScale)) - c.fixedScale;
834
+ }
835
+
836
+ bool isInt32(double v) { return isFixed(v) && v >= -2147483648.0 && v <= 2147483647.0; }
837
+
838
+ // Admitted domain: a createDeterministicMath configuration whose range contains
839
+ // [-scale, scale], so no noise corner value, smoothstep, lerp, or mul result
840
+ // saturates; x and y in that range; seed in int32; periods in [1, 2^31 - 1].
841
+ bool isNoiseInput(const Config& c, double x, double y, double seed, double px, double py) {
842
+ return isFixed(c.fixedScale) && c.fixedScale > 0.0 && std::fmod(c.fixedScale, 4.0) == 0.0 &&
843
+ c.fixedScale <= 1048576.0 && isInt32(c.minFixed) && isInt32(c.maxFixed) &&
844
+ c.minFixed <= -c.fixedScale && c.maxFixed >= c.fixedScale && isPeriodicFixed(c, x) &&
845
+ isPeriodicFixed(c, y) && isInt32(seed) && isPeriod(px) && isPeriod(py);
846
+ }
847
+
848
+ // fBm adds: octaves in [1, 30], seed + octaves - 1 <= 2^31 - 1, and for each axis
849
+ // period * scale * 2^(octaves - 1) <= maxFixed + 1 (exact: a power-of-two multiple).
850
+ bool isOctaveDomain(const Config& c, double seed, double octaves, double px, double py) {
851
+ if (!isFixed(octaves) || octaves < 1.0 || octaves > 30.0 || seed + octaves - 1.0 > 2147483647.0) return false;
852
+ const double top = static_cast<double>(1u << static_cast<unsigned>(octaves - 1.0));
853
+ return px * c.fixedScale * top <= c.maxFixed + 1.0 && py * c.fixedScale * top <= c.maxFixed + 1.0;
854
+ }
855
+
856
+ double periodicValueNoise(const Config& c, double x, double y, double seed, double px, double py, bool& ok) {
857
+ const double ix = std::floor(x / c.fixedScale);
858
+ const double iy = std::floor(y / c.fixedScale);
859
+ const double fx = x - ix * c.fixedScale;
860
+ const double fy = y - iy * c.fixedScale;
861
+ const double x0 = floorModPeriod(ix, px);
862
+ const double x1 = floorModPeriod(ix + 1, px);
863
+ const double y0 = floorModPeriod(iy, py);
864
+ const double y1 = floorModPeriod(iy + 1, py);
865
+ const std::uint32_t seedHash = hashUint32(int32Bits(seed));
866
+ const auto corner = [&](double cx, double cy) { return hashToFixed(c, hashUint32(hash2(cx, cy) ^ seedHash)); };
867
+ const double tx = smoothstep(c, 0.0, c.fixedScale, fx, ok);
868
+ const double ty = smoothstep(c, 0.0, c.fixedScale, fy, ok);
869
+ const double top = lerp(c, corner(x0, y0), corner(x1, y0), tx, ok);
870
+ const double bot = lerp(c, corner(x0, y1), corner(x1, y1), tx, ok);
871
+ return lerp(c, top, bot, ty, ok);
872
+ }
873
+
874
+ } // namespace
875
+
876
+ double valueNoise2DPeriodic(const Config& c, double xFixed, double yFixed, double seed, double periodCellsX,
877
+ double periodCellsY, bool& ok) {
878
+ if (!isNoiseInput(c, xFixed, yFixed, seed, periodCellsX, periodCellsY)) {
879
+ ok = false;
880
+ return 0.0;
881
+ }
882
+ return periodicValueNoise(c, xFixed, yFixed, seed, periodCellsX, periodCellsY, ok);
883
+ }
884
+
885
+ double fbm2DPeriodic(const Config& c, double xFixed, double yFixed, double seed, double octaves,
886
+ double periodCellsX, double periodCellsY, bool& ok) {
887
+ if (!isNoiseInput(c, xFixed, yFixed, seed, periodCellsX, periodCellsY) ||
888
+ !isOctaveDomain(c, seed, octaves, periodCellsX, periodCellsY)) {
889
+ ok = false;
890
+ return 0.0;
891
+ }
892
+ const double wx = floorModPeriod(xFixed, periodCellsX * c.fixedScale);
893
+ const double wy = floorModPeriod(yFixed, periodCellsY * c.fixedScale);
894
+ double sum = 0.0;
895
+ double amp = c.fixedScale;
896
+ double norm = 0.0;
897
+ double mult = 1.0;
898
+ for (double o = 0.0; o < octaves; o += 1.0) {
899
+ const double n =
900
+ periodicValueNoise(c, wx * mult, wy * mult, seed + o, periodCellsX * mult, periodCellsY * mult, ok);
901
+ sum += mul(c, n, amp, ok);
902
+ norm += amp;
903
+ amp = std::trunc(amp / 2);
904
+ mult *= 2.0;
905
+ }
906
+ return std::trunc((sum * c.fixedScale) / norm);
907
+ }
908
+
748
909
  } // namespace rundot::fixedmath
@@ -66,6 +66,12 @@ double atan2Turns(const Config& c, double y, double x, bool& ok);
66
66
  double atan2Signed(const Config& c, double y, double x, bool& ok);
67
67
  double wrapTurns(const Config& c, double t, bool& ok);
68
68
  double angleDeltaTurns(const Config& c, double a, double b, bool& ok);
69
+ // Periodic integer operations (src/math.ts). value, reference, and min must be
70
+ // integers in [c.minFixed, c.maxFixed]; period an integer in [1, 2^31 - 1].
71
+ double wrapPeriodic(const Config& c, double value, double min, double period, bool& ok);
72
+ double periodicImageOffset(const Config& c, double reference, double value, double period, bool& ok);
73
+ double periodicDelta(const Config& c, double reference, double value, double period, bool& ok);
74
+ double nearestPeriodicImage(const Config& c, double reference, double value, double period, bool& ok);
69
75
  double turnsToRadians(const Config& c, double t, bool& ok);
70
76
  double radiansToTurns(const Config& c, double r, bool& ok);
71
77
  double hypot(const Config& c, double x, double y, bool& ok);
@@ -106,6 +112,12 @@ Quat quatLerp(const Config& c, Quat a, Quat b, double t, bool& ok);
106
112
 
107
113
  double clamp(const Config& c, double value, double lo, double hi, bool& ok);
108
114
 
115
+ // Periodic value noise and fBm (src/noise.ts). See the domain in fixed_math.cpp.
116
+ double valueNoise2DPeriodic(const Config& c, double xFixed, double yFixed, double seed, double periodCellsX,
117
+ double periodCellsY, bool& ok);
118
+ double fbm2DPeriodic(const Config& c, double xFixed, double yFixed, double seed, double octaves,
119
+ double periodCellsX, double periodCellsY, bool& ok);
120
+
109
121
  } // namespace rundot::fixedmath
110
122
 
111
123
  #endif // RUNDOT_FIXED_MATH_HPP
@@ -655,6 +655,28 @@ int runMathVectors() {
655
655
  // formatting into the gate and truncate clamp's non-integer results. Output is
656
656
  // one pattern, a comma-separated list for vectors/quaternions, `nan`, or ERR
657
657
  // where the TypeScript reference throws.
658
+ //
659
+ // The periodic ops carry their configuration first:
660
+ // "<op> <fixedScale> <minFixed> <maxFixed> <arg>...". Their bounds depend on the
661
+ // configured range, so the default config cannot stand in for it. Returns the
662
+ // word count of the line after the op, or 0 for an op without a config.
663
+ std::size_t configFirstArity(const std::string& op) {
664
+ if (op == "wrapPeriodic" || op == "periodicImageOffset" || op == "periodicDelta" ||
665
+ op == "nearestPeriodicImage") return 6;
666
+ if (op == "valueNoise2DPeriodic") return 8;
667
+ if (op == "fbm2DPeriodic") return 9;
668
+ return 0;
669
+ }
670
+
671
+ // Mirrors the createDeterministicMath checks. The JS corpus cannot build a math
672
+ // instance for any other config, so a line that carries one is a harness bug.
673
+ bool isAdmittedConfig(const rundot::fixedmath::Config& c) {
674
+ const auto isInteger = [](double v) { return std::isfinite(v) && std::floor(v) == v; };
675
+ return isInteger(c.fixedScale) && c.fixedScale > 0.0 && std::fmod(c.fixedScale, 4.0) == 0.0 &&
676
+ c.fixedScale <= 1048576.0 && isInteger(c.minFixed) && isInteger(c.maxFixed) &&
677
+ c.minFixed < c.maxFixed && c.minFixed >= -2147483648.0 && c.maxFixed <= 2147483647.0;
678
+ }
679
+
658
680
  int runFixedVectors() {
659
681
  namespace fx = rundot::fixedmath;
660
682
  const fx::Config cfg = fx::defaultConfig();
@@ -684,7 +706,8 @@ int runFixedVectors() {
684
706
  return false;
685
707
  };
686
708
  std::size_t want = 0;
687
- if (op == "remap") want = 5;
709
+ if (configFirstArity(op) != 0) want = configFirstArity(op);
710
+ else if (op == "remap") want = 5;
688
711
  else if (op == "quatLerp") want = 9;
689
712
  else if (op == "vec3Lerp") want = 7;
690
713
  else if (op == "vec3Add" || op == "vec3Sub" || op == "vec3Dot" || op == "vec3Cross" ||
@@ -778,6 +801,21 @@ int runFixedVectors() {
778
801
  } else if (op == "quatLerp") {
779
802
  const fx::Quat q = fx::quatLerp(cfg, {a[0], a[1], a[2], a[3]}, {a[4], a[5], a[6], a[7]}, a[8], ok);
780
803
  out = vectorBits(q.x) + "," + vectorBits(q.y) + "," + vectorBits(q.z) + "," + vectorBits(q.w);
804
+ } else if (configFirstArity(op) != 0) {
805
+ const fx::Config custom{a[0], a[1], a[2]};
806
+ if (!isAdmittedConfig(custom)) {
807
+ std::cerr << "KINETIX_FIXED_VECTORS_BAD_CONFIG op=" << op << '\n';
808
+ return 1;
809
+ }
810
+ if (op == "wrapPeriodic") scalar(fx::wrapPeriodic(custom, a[3], a[4], a[5], ok));
811
+ else if (op == "periodicImageOffset") scalar(fx::periodicImageOffset(custom, a[3], a[4], a[5], ok));
812
+ else if (op == "periodicDelta") scalar(fx::periodicDelta(custom, a[3], a[4], a[5], ok));
813
+ else if (op == "nearestPeriodicImage") scalar(fx::nearestPeriodicImage(custom, a[3], a[4], a[5], ok));
814
+ else if (op == "valueNoise2DPeriodic") {
815
+ scalar(fx::valueNoise2DPeriodic(custom, a[3], a[4], a[5], a[6], a[7], ok));
816
+ } else {
817
+ scalar(fx::fbm2DPeriodic(custom, a[3], a[4], a[5], a[6], a[7], a[8], ok));
818
+ }
781
819
  } else {
782
820
  std::cerr << "KINETIX_FIXED_VECTORS_UNKNOWN_OP " << op << '\n';
783
821
  return 1;