@series-inc/rundot-syncplay 6.0.0-rc.25 → 6.0.0-rc.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.
@@ -23,7 +23,7 @@ export const physicsProfileWasmMetadata = Object.freeze({
23
23
  "sourceHash": "13bc7e6c473b9a773bbb0f77d1c2126df59a074759ebb5efb90b8dde1ff4d10a",
24
24
  "wasmSha256": "d398c126e4da59324d6e28f14132173a9f980f9fc429f632baffacbae4744fef",
25
25
  "artifact": "profile",
26
- "packageVersion": "6.0.0-rc.25",
26
+ "packageVersion": "6.0.0-rc.26",
27
27
  "compilerFlags": [
28
28
  "-std=c++20",
29
29
  "-O3",
@@ -23,7 +23,7 @@ export const physicsWasmMetadata = Object.freeze({
23
23
  "sourceHash": "4bb02a55da437ff3075d5350d48f319bd8b89a6dd31d9a34d9b0cb3677ed4914",
24
24
  "wasmSha256": "76ea5a82d4e84d0e3bc3fd93dd54992b54c09614f7024e4f8b5bf91445db1955",
25
25
  "artifact": "production",
26
- "packageVersion": "6.0.0-rc.25",
26
+ "packageVersion": "6.0.0-rc.26",
27
27
  "compilerFlags": [
28
28
  "-std=c++20",
29
29
  "-O3",
@@ -50,7 +50,7 @@ export const physics2dWasmMetadata = Object.freeze({
50
50
  "sourceHash": "faa090a03a6fddaf79c704fb1d76f810955ff35597449c8cec9541a75905a37e",
51
51
  "wasmSha256": "49cb90541deed40f5fff1015bb578e13ec9a793d51931c1227e3e5a11bbfa3f7",
52
52
  "artifact": "production",
53
- "packageVersion": "6.0.0-rc.25",
53
+ "packageVersion": "6.0.0-rc.26",
54
54
  "compilerFlags": [
55
55
  "-std=c++20",
56
56
  "-O3",
@@ -1,5 +1,5 @@
1
1
  import { snapshotEditResult, materializeEditResult } from "./edit-result-graph.js";
2
- import { createNativePhysicsWorld, initNativePhysicsWorld, bindNativePhysicsWorldFacade, } from '../../core/src/physics/world.mjs';
2
+ import { createNativePhysicsWorld, initNativePhysicsWorld, } from '../../core/src/physics/world.mjs';
3
3
  import * as worldModule from '../../core/src/physics/world.mjs';
4
4
  const nativePhysicsSelectedArtifact = worldModule.nativePhysicsSelectedArtifact;
5
5
  const installedRuntimeWorlds3D = new WeakSet();
@@ -1669,12 +1669,262 @@ function transformVelocityAcrossPortal(entryEnd, exitEnd, velocity) {
1669
1669
  const rel = computePortalRelativeTransform(entryEnd, exitEnd);
1670
1670
  return rotateVec3(rel.rotation, velocity);
1671
1671
  }
1672
+ const PORTAL_TRAILER_MAGIC = 0x504f5254; // 'PORT'
1673
+ const PORTAL_TRAILER_VERSION = 1;
1674
+ const PORTAL_TRAILER_FOOTER_BYTES = 9;
1675
+ function serializeWithPortalTrailer(rawBytes, portalState) {
1676
+ if (!portalState || (portalState.pairs.length === 0 && portalState.priorBodyPlanes.length === 0)) {
1677
+ return rawBytes;
1678
+ }
1679
+ const payloadStr = JSON.stringify(portalState);
1680
+ const textEncoder = new TextEncoder();
1681
+ const payloadBytes = textEncoder.encode(payloadStr);
1682
+ const payloadLen = payloadBytes.length;
1683
+ const result = new Uint8Array(rawBytes.length + payloadLen + PORTAL_TRAILER_FOOTER_BYTES);
1684
+ result.set(rawBytes, 0);
1685
+ result.set(payloadBytes, rawBytes.length);
1686
+ const view = new DataView(result.buffer, result.byteOffset, result.byteLength);
1687
+ const trailerOffset = rawBytes.length + payloadLen;
1688
+ view.setUint32(trailerOffset, payloadLen, true);
1689
+ view.setUint32(trailerOffset + 4, PORTAL_TRAILER_MAGIC, false);
1690
+ result[trailerOffset + 8] = PORTAL_TRAILER_VERSION;
1691
+ return result;
1692
+ }
1693
+ function deserializeWithPortalTrailer(bytes) {
1694
+ if (bytes.length >= PORTAL_TRAILER_FOOTER_BYTES) {
1695
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1696
+ const magicOffset = bytes.length - 5;
1697
+ const magic = view.getUint32(magicOffset, false);
1698
+ const version = bytes[bytes.length - 1];
1699
+ if (magic === PORTAL_TRAILER_MAGIC && version === PORTAL_TRAILER_VERSION) {
1700
+ const payloadLenOffset = bytes.length - PORTAL_TRAILER_FOOTER_BYTES;
1701
+ const payloadLen = view.getUint32(payloadLenOffset, true);
1702
+ const expectedTotal = payloadLen + PORTAL_TRAILER_FOOTER_BYTES;
1703
+ if (bytes.length >= expectedTotal) {
1704
+ const rawBytes = bytes.subarray(0, bytes.length - expectedTotal);
1705
+ const payloadBytes = bytes.subarray(bytes.length - expectedTotal, bytes.length - PORTAL_TRAILER_FOOTER_BYTES);
1706
+ try {
1707
+ const textDecoder = new TextDecoder();
1708
+ const parsed = JSON.parse(textDecoder.decode(payloadBytes));
1709
+ return { rawBytes, portalState: parsed };
1710
+ }
1711
+ catch {
1712
+ // If parse fails, fall through
1713
+ }
1714
+ }
1715
+ }
1716
+ }
1717
+ return { rawBytes: bytes, portalState: null };
1718
+ }
1719
+ function wrapRuntimeController(rawController, portalManager, portalCheckpoints) {
1720
+ const wrapped = {
1721
+ ...rawController,
1722
+ captureCheckpoint() {
1723
+ const cp = rawController.captureCheckpoint();
1724
+ portalCheckpoints.set(cp, portalManager.captureState());
1725
+ return cp;
1726
+ },
1727
+ restoreCheckpoint(checkpoint) {
1728
+ if (checkpoint && typeof checkpoint === 'object') {
1729
+ const saved = portalCheckpoints.get(checkpoint);
1730
+ if (saved) {
1731
+ portalManager.restoreState(saved);
1732
+ }
1733
+ else {
1734
+ portalManager.priorBodyPlanes.clear();
1735
+ portalManager.poseJumpBodyIds.clear();
1736
+ portalManager.lastCrossings = [];
1737
+ }
1738
+ }
1739
+ return rawController.restoreCheckpoint(checkpoint);
1740
+ },
1741
+ serializeCheckpoint(checkpoint) {
1742
+ const rawBytes = rawController.serializeCheckpoint(checkpoint);
1743
+ const saved = checkpoint && typeof checkpoint === 'object' ? portalCheckpoints.get(checkpoint) : null;
1744
+ return serializeWithPortalTrailer(rawBytes, saved);
1745
+ },
1746
+ importCheckpoint(bytes) {
1747
+ const { rawBytes, portalState } = deserializeWithPortalTrailer(bytes);
1748
+ const cp = rawController.importCheckpoint(rawBytes);
1749
+ if (portalState) {
1750
+ portalCheckpoints.set(cp, portalState);
1751
+ }
1752
+ return cp;
1753
+ },
1754
+ releaseCheckpoint(checkpoint) {
1755
+ if (checkpoint && typeof checkpoint === 'object') {
1756
+ portalCheckpoints.delete(checkpoint);
1757
+ }
1758
+ return rawController.releaseCheckpoint(checkpoint);
1759
+ },
1760
+ digestCheckpoint(checkpoint) {
1761
+ const rawDigest = rawController.digestCheckpoint(checkpoint);
1762
+ const saved = checkpoint && typeof checkpoint === 'object' ? portalCheckpoints.get(checkpoint) : null;
1763
+ return portalManager.computeDigest(rawDigest, saved);
1764
+ },
1765
+ strongDigestCheckpoint(checkpoint) {
1766
+ const rawStrong = rawController.strongDigestCheckpoint(checkpoint);
1767
+ const saved = checkpoint && typeof checkpoint === 'object' ? portalCheckpoints.get(checkpoint) : null;
1768
+ return portalManager.computeStrongDigest(rawStrong, saved);
1769
+ },
1770
+ };
1771
+ return Object.freeze(wrapped);
1772
+ }
1672
1773
  class PortalSimulatorManager {
1673
1774
  nextId = 1;
1674
1775
  pairs = new Map();
1675
1776
  poseJumpBodyIds = new Set();
1676
1777
  priorBodyPlanes = new Map();
1677
1778
  lastCrossings = [];
1779
+ scratchPoses = new Float64Array(0);
1780
+ scratchVelocities = new Float64Array(0);
1781
+ scratchIds = new BigUint64Array(0);
1782
+ scratchStatus = new Uint8Array(0);
1783
+ ensureScratchBuffers(capacity) {
1784
+ if (this.scratchIds.length < capacity) {
1785
+ this.scratchPoses = new Float64Array(capacity * 7);
1786
+ this.scratchVelocities = new Float64Array(capacity * 6);
1787
+ this.scratchIds = new BigUint64Array(capacity);
1788
+ this.scratchStatus = new Uint8Array(capacity);
1789
+ }
1790
+ }
1791
+ captureState() {
1792
+ const pairsCopy = [];
1793
+ for (const p of this.pairs.values()) {
1794
+ pairsCopy.push({
1795
+ id: p.id,
1796
+ portalA: {
1797
+ position: { ...p.portalA.position },
1798
+ rotation: { ...p.portalA.rotation },
1799
+ opening: { ...p.portalA.opening },
1800
+ hostBodyId: p.portalA.hostBodyId,
1801
+ },
1802
+ portalB: {
1803
+ position: { ...p.portalB.position },
1804
+ rotation: { ...p.portalB.rotation },
1805
+ opening: { ...p.portalB.opening },
1806
+ hostBodyId: p.portalB.hostBodyId,
1807
+ },
1808
+ open: p.open,
1809
+ });
1810
+ }
1811
+ return {
1812
+ nextId: this.nextId,
1813
+ pairs: pairsCopy,
1814
+ priorBodyPlanes: Array.from(this.priorBodyPlanes.entries()),
1815
+ poseJumpBodyIds: Array.from(this.poseJumpBodyIds).map((id) => id.toString()),
1816
+ lastCrossings: this.lastCrossings.map((c) => ({
1817
+ bodyId: c.bodyId,
1818
+ portalId: c.portalId,
1819
+ enteredEnd: c.enteredEnd,
1820
+ exitEnd: c.exitEnd,
1821
+ turnedVelocity: { ...c.turnedVelocity },
1822
+ })),
1823
+ };
1824
+ }
1825
+ restoreState(state) {
1826
+ this.nextId = state.nextId;
1827
+ this.pairs.clear();
1828
+ for (const p of state.pairs) {
1829
+ this.pairs.set(p.id, {
1830
+ id: p.id,
1831
+ portalA: {
1832
+ position: { ...p.portalA.position },
1833
+ rotation: { ...p.portalA.rotation },
1834
+ opening: { ...p.portalA.opening },
1835
+ hostBodyId: p.portalA.hostBodyId,
1836
+ },
1837
+ portalB: {
1838
+ position: { ...p.portalB.position },
1839
+ rotation: { ...p.portalB.rotation },
1840
+ opening: { ...p.portalB.opening },
1841
+ hostBodyId: p.portalB.hostBodyId,
1842
+ },
1843
+ open: p.open,
1844
+ });
1845
+ }
1846
+ this.priorBodyPlanes.clear();
1847
+ for (const [k, v] of state.priorBodyPlanes) {
1848
+ this.priorBodyPlanes.set(k, v);
1849
+ }
1850
+ this.poseJumpBodyIds.clear();
1851
+ for (const idStr of state.poseJumpBodyIds) {
1852
+ this.poseJumpBodyIds.add(BigInt(idStr));
1853
+ }
1854
+ this.lastCrossings = state.lastCrossings.map((c) => ({
1855
+ bodyId: BigInt(c.bodyId),
1856
+ portalId: c.portalId,
1857
+ enteredEnd: c.enteredEnd,
1858
+ exitEnd: c.exitEnd,
1859
+ turnedVelocity: { ...c.turnedVelocity },
1860
+ }));
1861
+ }
1862
+ computeDigest(rawDigest, state) {
1863
+ const s = state ?? this.captureState();
1864
+ if (s.pairs.length === 0 && s.priorBodyPlanes.length === 0) {
1865
+ return rawDigest;
1866
+ }
1867
+ let hash = 0xcbf29ce484222325n ^ rawDigest;
1868
+ const FNV_PRIME = 0x100000001b3n;
1869
+ const MASK_64 = 0xffffffffffffffffn;
1870
+ function mixString(str) {
1871
+ for (let i = 0; i < str.length; i++) {
1872
+ hash = ((hash ^ BigInt(str.charCodeAt(i))) * FNV_PRIME) & MASK_64;
1873
+ }
1874
+ }
1875
+ function mixFloat(n) {
1876
+ const buf = new Float64Array([n]);
1877
+ const u8 = new Uint8Array(buf.buffer);
1878
+ for (let i = 0; i < 8; i++) {
1879
+ hash = ((hash ^ BigInt(u8[i])) * FNV_PRIME) & MASK_64;
1880
+ }
1881
+ }
1882
+ function mixInt(n) {
1883
+ hash = ((hash ^ BigInt(n)) * FNV_PRIME) & MASK_64;
1884
+ }
1885
+ const sortedPairs = [...s.pairs].sort((a, b) => a.id - b.id);
1886
+ mixInt(sortedPairs.length);
1887
+ for (const p of sortedPairs) {
1888
+ mixInt(p.id);
1889
+ mixInt(p.open ? 1 : 0);
1890
+ for (const end of [p.portalA, p.portalB]) {
1891
+ mixFloat(end.position.x);
1892
+ mixFloat(end.position.y);
1893
+ mixFloat(end.position.z);
1894
+ mixFloat(end.rotation.x);
1895
+ mixFloat(end.rotation.y);
1896
+ mixFloat(end.rotation.z);
1897
+ mixFloat(end.rotation.w);
1898
+ mixFloat(end.opening.width);
1899
+ mixFloat(end.opening.height);
1900
+ if (end.hostBodyId !== undefined)
1901
+ mixInt(BigInt(end.hostBodyId));
1902
+ }
1903
+ }
1904
+ const sortedPlanes = [...s.priorBodyPlanes].sort(([ka], [kb]) => ka.localeCompare(kb));
1905
+ mixInt(sortedPlanes.length);
1906
+ for (const [k, z] of sortedPlanes) {
1907
+ mixString(k);
1908
+ mixFloat(z);
1909
+ }
1910
+ return hash;
1911
+ }
1912
+ computeStrongDigest(rawStrongDigest, state) {
1913
+ const s = state ?? this.captureState();
1914
+ if (s.pairs.length === 0 && s.priorBodyPlanes.length === 0) {
1915
+ return rawStrongDigest;
1916
+ }
1917
+ const digest64 = this.computeDigest(0n, s);
1918
+ const result = new Uint8Array(rawStrongDigest);
1919
+ const view = new DataView(result.buffer, result.byteOffset, result.byteLength);
1920
+ const low = Number(digest64 & 0xffffffffn);
1921
+ const high = Number((digest64 >> 32n) & 0xffffffffn);
1922
+ view.setUint32(0, view.getUint32(0, true) ^ low, true);
1923
+ view.setUint32(4, view.getUint32(4, true) ^ high, true);
1924
+ view.setUint32(result.length - 8, view.getUint32(result.length - 8, true) ^ low, true);
1925
+ view.setUint32(result.length - 4, view.getUint32(result.length - 4, true) ^ high, true);
1926
+ return result;
1927
+ }
1678
1928
  createPortalPair(def) {
1679
1929
  if (!def.portalA || !def.portalB) {
1680
1930
  throw new SyncplayError('PortalPairDefinition requires portalA and portalB.', {
@@ -1807,87 +2057,171 @@ class PortalSimulatorManager {
1807
2057
  }
1808
2058
  }
1809
2059
  }
1810
- stepPortals(world, raw) {
2060
+ stepPortals(world, raw, bodyCapacity) {
1811
2061
  if (this.pairs.size === 0) {
1812
2062
  this.lastCrossings = [];
1813
2063
  return [];
1814
2064
  }
2065
+ let hasOpen = false;
2066
+ for (const p of this.pairs.values()) {
2067
+ if (p.open) {
2068
+ hasOpen = true;
2069
+ break;
2070
+ }
2071
+ }
2072
+ if (!hasOpen) {
2073
+ this.lastCrossings = [];
2074
+ return [];
2075
+ }
1815
2076
  const crossings = [];
1816
- const topology = world.info();
1817
- if (topology.bodyCount === 0)
1818
- return crossings;
1819
2077
  const teleportedThisStep = new Set();
1820
- const bodyIds = raw ? raw.topology().bodies : [];
1821
- for (const pair of this.pairs.values()) {
1822
- if (!pair.open)
1823
- continue;
1824
- for (const [entryKey, exitKey, entryLabel, exitLabel] of [
1825
- ['portalA', 'portalB', 'A', 'B'],
1826
- ['portalB', 'portalA', 'B', 'A'],
1827
- ]) {
1828
- const entryEnd = pair[entryKey];
1829
- const exitEnd = pair[exitKey];
1830
- const entryRotNorm = quatNormalize(entryEnd.rotation);
1831
- const halfW = entryEnd.opening.width * 0.5;
1832
- const halfH = entryEnd.opening.height * 0.5;
1833
- const checkBody = (bodyId) => {
1834
- if (teleportedThisStep.has(bodyId))
1835
- return;
1836
- let handle;
1837
- let motion;
1838
- try {
1839
- handle = world.resolveBody(bodyId);
1840
- motion = world.readMotion(handle);
1841
- }
1842
- catch {
1843
- return;
1844
- }
1845
- if (motion.kind === 'static')
1846
- return;
1847
- const trackingKey = `${pair.id}:${entryLabel}:${bodyId}`;
1848
- const localPos = invRotateVec3(entryRotNorm, subVec3(motion.position, entryEnd.position));
1849
- const priorZ = this.priorBodyPlanes.get(trackingKey);
1850
- this.priorBodyPlanes.set(trackingKey, localPos.z);
1851
- // Within rectangular aperture bounds
1852
- if (Math.abs(localPos.x) <= halfW && Math.abs(localPos.y) <= halfH) {
1853
- // Crossed from entrance (z > 0) to aperture exit (z <= 0)
1854
- const crossed = priorZ !== undefined && priorZ > 0 && localPos.z <= 0;
1855
- if (crossed) {
1856
- // Perform momentum-turning teleportation
1857
- const newPose = transformPoseAcrossPortal(entryEnd, exitEnd, motion);
1858
- const turnedVel = transformVelocityAcrossPortal(entryEnd, exitEnd, motion.linearVelocity);
1859
- const turnedAngVel = transformVelocityAcrossPortal(entryEnd, exitEnd, motion.angularVelocity);
1860
- const exitNorm = normalizeVec3(rotateVec3(quatNormalize(exitEnd.rotation), { x: 0, y: 0, z: 1 }));
1861
- const nudgedPos = addVec3(newPose.position, scaleVec3(exitNorm, 0.05));
1862
- if (raw) {
2078
+ if (raw) {
2079
+ this.ensureScratchBuffers(bodyCapacity ?? 128);
2080
+ const res = raw.writeBodyMotionInto(this.scratchPoses, this.scratchVelocities, this.scratchIds, this.scratchStatus);
2081
+ const count = res.count;
2082
+ if (count === 0) {
2083
+ this.lastCrossings = [];
2084
+ return [];
2085
+ }
2086
+ for (const pair of this.pairs.values()) {
2087
+ if (!pair.open)
2088
+ continue;
2089
+ for (const [entryKey, exitKey, entryLabel, exitLabel] of [
2090
+ ['portalA', 'portalB', 'A', 'B'],
2091
+ ['portalB', 'portalA', 'B', 'A'],
2092
+ ]) {
2093
+ const entryEnd = pair[entryKey];
2094
+ const exitEnd = pair[exitKey];
2095
+ const entryRotNorm = quatNormalize(entryEnd.rotation);
2096
+ const halfW = entryEnd.opening.width * 0.5;
2097
+ const halfH = entryEnd.opening.height * 0.5;
2098
+ const maxDim = Math.max(entryEnd.opening.width, entryEnd.opening.height);
2099
+ const trackingRadius = maxDim * 1.5 + 2.0;
2100
+ const trackingRadiusSq = trackingRadius * trackingRadius;
2101
+ for (let i = 0; i < count; i++) {
2102
+ if ((this.scratchStatus[i] & 0x03) === 0)
2103
+ continue;
2104
+ const bodyId = this.scratchIds[i];
2105
+ if (teleportedThisStep.has(bodyId))
2106
+ continue;
2107
+ const posX = this.scratchPoses[i * 7];
2108
+ const posY = this.scratchPoses[i * 7 + 1];
2109
+ const posZ = this.scratchPoses[i * 7 + 2];
2110
+ const dx = posX - entryEnd.position.x;
2111
+ const dy = posY - entryEnd.position.y;
2112
+ const dz = posZ - entryEnd.position.z;
2113
+ if (dx * dx + dy * dy + dz * dz > trackingRadiusSq)
2114
+ continue;
2115
+ const trackingKey = `${pair.id}:${entryLabel}:${bodyId}`;
2116
+ const localPos = invRotateVec3(entryRotNorm, { x: dx, y: dy, z: dz });
2117
+ const priorZ = this.priorBodyPlanes.get(trackingKey);
2118
+ this.priorBodyPlanes.set(trackingKey, localPos.z);
2119
+ if (Math.abs(localPos.x) <= halfW && Math.abs(localPos.y) <= halfH) {
2120
+ const crossed = priorZ !== undefined && priorZ > 0 && localPos.z <= 0;
2121
+ if (crossed) {
2122
+ const motionPose = {
2123
+ position: { x: posX, y: posY, z: posZ },
2124
+ orientation: {
2125
+ x: this.scratchPoses[i * 7 + 3],
2126
+ y: this.scratchPoses[i * 7 + 4],
2127
+ z: this.scratchPoses[i * 7 + 5],
2128
+ w: this.scratchPoses[i * 7 + 6],
2129
+ },
2130
+ };
2131
+ const linVel = {
2132
+ x: this.scratchVelocities[i * 6],
2133
+ y: this.scratchVelocities[i * 6 + 1],
2134
+ z: this.scratchVelocities[i * 6 + 2],
2135
+ };
2136
+ const angVel = {
2137
+ x: this.scratchVelocities[i * 6 + 3],
2138
+ y: this.scratchVelocities[i * 6 + 4],
2139
+ z: this.scratchVelocities[i * 6 + 5],
2140
+ };
2141
+ const newPose = transformPoseAcrossPortal(entryEnd, exitEnd, motionPose);
2142
+ const turnedVel = transformVelocityAcrossPortal(entryEnd, exitEnd, linVel);
2143
+ const turnedAngVel = transformVelocityAcrossPortal(entryEnd, exitEnd, angVel);
2144
+ const exitNorm = normalizeVec3(rotateVec3(quatNormalize(exitEnd.rotation), { x: 0, y: 0, z: 1 }));
2145
+ const nudgedPos = addVec3(newPose.position, scaleVec3(exitNorm, 0.05));
2146
+ const handle = world.resolveBody(bodyId);
1863
2147
  raw.setBodyTransform(handle, { x: nudgedPos.x, y: nudgedPos.y, z: nudgedPos.z }, newPose.orientation);
1864
2148
  raw.setBodyLinearVelocity(handle, { x: turnedVel.x, y: turnedVel.y, z: turnedVel.z });
1865
2149
  raw.setBodyAngularVelocity(handle, { x: turnedAngVel.x, y: turnedAngVel.y, z: turnedAngVel.z });
2150
+ raw.wakeBody(handle);
2151
+ this.poseJumpBodyIds.add(bodyId);
2152
+ teleportedThisStep.add(bodyId);
2153
+ this.priorBodyPlanes.delete(trackingKey);
2154
+ this.priorBodyPlanes.set(`${pair.id}:${exitLabel}:${bodyId}`, 0.05);
2155
+ crossings.push({
2156
+ bodyId,
2157
+ portalId: pair.id,
2158
+ enteredEnd: entryLabel,
2159
+ exitEnd: exitLabel,
2160
+ turnedVelocity: turnedVel,
2161
+ });
1866
2162
  }
1867
- else {
2163
+ }
2164
+ }
2165
+ }
2166
+ }
2167
+ }
2168
+ else {
2169
+ const topology = world.info();
2170
+ for (const pair of this.pairs.values()) {
2171
+ if (!pair.open)
2172
+ continue;
2173
+ for (const [entryKey, exitKey, entryLabel, exitLabel] of [
2174
+ ['portalA', 'portalB', 'A', 'B'],
2175
+ ['portalB', 'portalA', 'B', 'A'],
2176
+ ]) {
2177
+ const entryEnd = pair[entryKey];
2178
+ const exitEnd = pair[exitKey];
2179
+ const entryRotNorm = quatNormalize(entryEnd.rotation);
2180
+ const halfW = entryEnd.opening.width * 0.5;
2181
+ const halfH = entryEnd.opening.height * 0.5;
2182
+ const checkBody = (bodyId) => {
2183
+ if (teleportedThisStep.has(bodyId))
2184
+ return;
2185
+ let handle;
2186
+ let motion;
2187
+ try {
2188
+ handle = world.resolveBody(bodyId);
2189
+ motion = world.readMotion(handle);
2190
+ }
2191
+ catch {
2192
+ return;
2193
+ }
2194
+ if (motion.kind === 'static')
2195
+ return;
2196
+ const trackingKey = `${pair.id}:${entryLabel}:${bodyId}`;
2197
+ const localPos = invRotateVec3(entryRotNorm, subVec3(motion.position, entryEnd.position));
2198
+ const priorZ = this.priorBodyPlanes.get(trackingKey);
2199
+ this.priorBodyPlanes.set(trackingKey, localPos.z);
2200
+ if (Math.abs(localPos.x) <= halfW && Math.abs(localPos.y) <= halfH) {
2201
+ const crossed = priorZ !== undefined && priorZ > 0 && localPos.z <= 0;
2202
+ if (crossed) {
2203
+ const newPose = transformPoseAcrossPortal(entryEnd, exitEnd, motion);
2204
+ const turnedVel = transformVelocityAcrossPortal(entryEnd, exitEnd, motion.linearVelocity);
2205
+ const turnedAngVel = transformVelocityAcrossPortal(entryEnd, exitEnd, motion.angularVelocity);
2206
+ const exitNorm = normalizeVec3(rotateVec3(quatNormalize(exitEnd.rotation), { x: 0, y: 0, z: 1 }));
2207
+ const nudgedPos = addVec3(newPose.position, scaleVec3(exitNorm, 0.05));
1868
2208
  world.setBodyTransform(handle, { position: nudgedPos, orientation: newPose.orientation });
1869
2209
  world.setBodyLinearVelocity(handle, turnedVel);
1870
2210
  world.setBodyAngularVelocity(handle, turnedAngVel);
2211
+ this.poseJumpBodyIds.add(bodyId);
2212
+ teleportedThisStep.add(bodyId);
2213
+ this.priorBodyPlanes.delete(trackingKey);
2214
+ this.priorBodyPlanes.set(`${pair.id}:${exitLabel}:${bodyId}`, 0.05);
2215
+ crossings.push({
2216
+ bodyId,
2217
+ portalId: pair.id,
2218
+ enteredEnd: entryLabel,
2219
+ exitEnd: exitLabel,
2220
+ turnedVelocity: turnedVel,
2221
+ });
1871
2222
  }
1872
- this.poseJumpBodyIds.add(bodyId);
1873
- teleportedThisStep.add(bodyId);
1874
- this.priorBodyPlanes.delete(trackingKey);
1875
- this.priorBodyPlanes.set(`${pair.id}:${exitLabel}:${bodyId}`, 0.05);
1876
- crossings.push({
1877
- bodyId,
1878
- portalId: pair.id,
1879
- enteredEnd: entryLabel,
1880
- exitEnd: exitLabel,
1881
- turnedVelocity: turnedVel,
1882
- });
1883
2223
  }
1884
- }
1885
- };
1886
- if (bodyIds.length > 0) {
1887
- for (const rawId of bodyIds)
1888
- checkBody(BigInt(rawId));
1889
- }
1890
- else {
2224
+ };
1891
2225
  for (let i = 1; i <= topology.bodyCapacity; i++)
1892
2226
  checkBody(BigInt(i));
1893
2227
  }
@@ -2033,6 +2367,7 @@ function facade(raw, definition) {
2033
2367
  contactDampingRatio: definition.contactDampingRatio,
2034
2368
  });
2035
2369
  const portalManager = new PortalSimulatorManager();
2370
+ const portalCheckpoints = new WeakMap();
2036
2371
  const world = {
2037
2372
  edit(build) {
2038
2373
  return invokeWorld("edit", () => {
@@ -2380,9 +2715,9 @@ function facade(raw, definition) {
2380
2715
  portalManager.poseJumpBodyIds.clear();
2381
2716
  portalManager.lastCrossings = [];
2382
2717
  raw.step(stepOptions);
2383
- portalManager.stepPortals(world, raw);
2718
+ portalManager.stepPortals(world, raw, definition.capacity.bodies);
2384
2719
  }),
2385
- stepPortals: () => invokeWorld("step", () => portalManager.stepPortals(world, raw)),
2720
+ stepPortals: () => invokeWorld("step", () => portalManager.stepPortals(world, raw, definition.capacity.bodies)),
2386
2721
  applyImpulse: (handle, impulse, point) => invokeWorld("apply-impulse", () => {
2387
2722
  const value = vector(impulse, "impulse");
2388
2723
  const applicationPoint = point === undefined ? undefined : vector(point, "point");
@@ -2575,26 +2910,63 @@ function facade(raw, definition) {
2575
2910
  raw.validateBody(query.excludeBody);
2576
2911
  return castHits(raw.closestPoint(queryInput(query)));
2577
2912
  }),
2578
- captureCheckpoint: () => invokeWorld("checkpoint", () => raw.captureCheckpoint()),
2913
+ captureCheckpoint: () => invokeWorld("checkpoint", () => {
2914
+ const cp = raw.captureCheckpoint();
2915
+ portalCheckpoints.set(cp, portalManager.captureState());
2916
+ return cp;
2917
+ }),
2579
2918
  restoreCheckpoint: (checkpoint) => invokeWorld("restore", () => {
2580
- portalManager.priorBodyPlanes.clear();
2581
- portalManager.poseJumpBodyIds.clear();
2582
- portalManager.lastCrossings = [];
2919
+ const saved = portalCheckpoints.get(checkpoint);
2920
+ if (saved) {
2921
+ portalManager.restoreState(saved);
2922
+ }
2923
+ else {
2924
+ portalManager.priorBodyPlanes.clear();
2925
+ portalManager.poseJumpBodyIds.clear();
2926
+ portalManager.lastCrossings = [];
2927
+ }
2583
2928
  return raw.restoreCheckpoint(checkpoint);
2584
2929
  }),
2585
- serializeCheckpoint: (checkpoint) => invokeWorld("checkpoint", () => raw.serializeCheckpoint(checkpoint)),
2930
+ serializeCheckpoint: (checkpoint) => invokeWorld("checkpoint", () => {
2931
+ const rawBytes = raw.serializeCheckpoint(checkpoint);
2932
+ const saved = portalCheckpoints.get(checkpoint);
2933
+ return serializeWithPortalTrailer(rawBytes, saved);
2934
+ }),
2586
2935
  checkpointInfo: (checkpoint) => invokeWorld("checkpoint-info", () => raw.checkpointInfo(checkpoint)),
2587
2936
  writeCheckpointBodyMotionInto: (checkpoint, poses, velocities, ids, status) => invokeWorld("checkpoint-motion", () => raw.writeCheckpointBodyMotionInto(checkpoint, poses, velocities, ids, status)),
2588
- digestCheckpoint: (checkpoint) => invokeWorld("checkpoint", () => raw.digestCheckpoint(checkpoint)),
2589
- importCheckpoint: (bytes) => invokeWorld("checkpoint", () => raw.importCheckpoint(bytes)),
2937
+ digestCheckpoint: (checkpoint) => invokeWorld("checkpoint", () => {
2938
+ const rawDigest = raw.digestCheckpoint(checkpoint);
2939
+ const saved = portalCheckpoints.get(checkpoint);
2940
+ return portalManager.computeDigest(rawDigest, saved);
2941
+ }),
2942
+ importCheckpoint: (bytes) => invokeWorld("checkpoint", () => {
2943
+ const { rawBytes, portalState } = deserializeWithPortalTrailer(bytes);
2944
+ const checkpoint = raw.importCheckpoint(rawBytes);
2945
+ if (portalState) {
2946
+ portalCheckpoints.set(checkpoint, portalState);
2947
+ }
2948
+ return checkpoint;
2949
+ }),
2590
2950
  restore: (bytes) => invokeWorld("restore", () => {
2591
- portalManager.priorBodyPlanes.clear();
2592
- portalManager.poseJumpBodyIds.clear();
2593
- portalManager.lastCrossings = [];
2594
- return raw.restore(bytes);
2951
+ const { rawBytes, portalState } = deserializeWithPortalTrailer(bytes);
2952
+ if (portalState) {
2953
+ portalManager.restoreState(portalState);
2954
+ }
2955
+ else {
2956
+ portalManager.priorBodyPlanes.clear();
2957
+ portalManager.poseJumpBodyIds.clear();
2958
+ portalManager.lastCrossings = [];
2959
+ }
2960
+ return raw.restore(rawBytes);
2961
+ }),
2962
+ releaseCheckpoint: (checkpoint) => invokeWorld("checkpoint", () => {
2963
+ portalCheckpoints.delete(checkpoint);
2964
+ return raw.releaseCheckpoint(checkpoint);
2965
+ }),
2966
+ digest: () => invokeWorld("digest", () => {
2967
+ const rawDigest = raw.digest();
2968
+ return portalManager.computeDigest(rawDigest, portalManager.captureState());
2595
2969
  }),
2596
- releaseCheckpoint: (checkpoint) => invokeWorld("checkpoint", () => raw.releaseCheckpoint(checkpoint)),
2597
- digest: () => invokeWorld("digest", () => raw.digest()),
2598
2970
  writeBodyTransformsInto: (outTransforms, outStatusMask) => invokeWorld("write-body-transforms", () => {
2599
2971
  if (!(outTransforms instanceof Float32Array)) {
2600
2972
  throw new SyncplayError("outTransforms must be a Float32Array.", { code: "BUFFER_OVERFLOW", operation: "write-body-transforms" });
@@ -2636,7 +3008,29 @@ function facade(raw, definition) {
2636
3008
  }),
2637
3009
  dispose: () => invokeWorld("dispose", () => raw.dispose()),
2638
3010
  };
2639
- bindNativePhysicsWorldFacade(world, raw);
3011
+ const runtimeAttachmentKey = Symbol.for('rundot.syncplay.physics3d.runtimeAttachment/1');
3012
+ const rawDescriptor = Object.getOwnPropertyDescriptor(raw, runtimeAttachmentKey);
3013
+ if (rawDescriptor?.value) {
3014
+ Object.defineProperty(world, runtimeAttachmentKey, {
3015
+ value(expectedDigest) {
3016
+ if (this !== world)
3017
+ fail('physics.world-invalid', 'attach', 'use the original physics world');
3018
+ const liveDigest = world.digest();
3019
+ if (expectedDigest !== liveDigest) {
3020
+ throw new SyncplayError('attachNativePhysicsWorld: INITIAL_STATE_MISMATCH', {
3021
+ code: 'physics.initial-state-mismatch',
3022
+ operation: 'attachNativePhysicsWorld',
3023
+ });
3024
+ }
3025
+ const rawDigest = raw.digest();
3026
+ const rawController = rawDescriptor.value.call(raw, rawDigest);
3027
+ return wrapRuntimeController(rawController, portalManager, portalCheckpoints);
3028
+ },
3029
+ configurable: true,
3030
+ enumerable: false,
3031
+ writable: false,
3032
+ });
3033
+ }
2640
3034
  registerInstalledRuntimeWorld3D(installedRuntimeWorlds3D, world);
2641
3035
  return Object.freeze(world);
2642
3036
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@series-inc/rundot-syncplay",
3
- "version": "6.0.0-rc.25",
3
+ "version": "6.0.0-rc.26",
4
4
  "description": "Kinetix orchestration, rollback, replay, late-join, and deterministic networking for RUN.game",
5
5
  "repository": {
6
6
  "type": "git",