Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions profiling-baseline-50bots.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"timestamp": "2026-03-03T05:53:11.603Z",
"duration_seconds": "63.3",
"timestamp": "2026-03-05T16:36:13.452Z",
"duration_seconds": "63.2",
"bots_requested": 50,
"bots_connected_final": 50,
"connection_stability_percent": "100.0",
"snapshots_total": 81710,
"snapshots_per_bot_per_sec": "25.83",
"data_received_mb": "401.11",
"avg_snapshot_bytes": 5147,
"network_errors": 0,
"bots_connected_final": -49,
"connection_stability_percent": "-98.0",
"snapshots_total": 1,
"snapshots_per_bot_per_sec": "0.00",
"data_received_mb": "0.18",
"avg_snapshot_bytes": 184379,
"network_errors": 49,
"server_tick_rate": 128,
"server_tick_time_target_ms": 7.8,
"client_frame_rate_target": 60,
Expand Down
433 changes: 78 additions & 355 deletions src/apps/AppRuntime.js

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/apps/runtime/AppManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { AppContext } from '../AppContext.js'

export class AppManager {
constructor(runtime) {
this._runtime = runtime
this.apps = new Map()
this.contexts = new Map()
this._appDefs = new Map()
this._updateList = []
}

registerApp(name, appDef) { this._appDefs.set(name, appDef) }

async attachApp(entityId, appName) {
const entity = this._runtime.entities.get(entityId)
const appDef = this._appDefs.get(appName)
if (!entity || !appDef) return
const ctx = new AppContext(entity, this._runtime)
this.contexts.set(entityId, ctx); this.apps.set(entityId, appDef)
await this._runtime._safeCall(appDef.server || appDef, 'setup', [ctx], `setup(${appName})`)
this._rebuildUpdateList()
this._runtime._rebuildCollisionList()
}

detachApp(entityId) {
const appDef = this.apps.get(entityId), ctx = this.contexts.get(entityId)
if (appDef && ctx) this._runtime._safeCall(appDef.server || appDef, 'teardown', [ctx], 'teardown')
this._runtime._eventBus.destroyScope(entityId)
this._runtime.clearTimers(entityId); this.apps.delete(entityId); this.contexts.delete(entityId)
this._rebuildUpdateList()
this._runtime._rebuildCollisionList()
}

_rebuildUpdateList() {
this._updateList = []
for (const [entityId, appDef] of this.apps) {
const ctx = this.contexts.get(entityId); if (!ctx) continue
const server = appDef.server || appDef
if (typeof server.update === 'function') this._updateList.push([entityId, server, ctx])
}
}
}
76 changes: 76 additions & 0 deletions src/apps/runtime/EntityManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
export class EntityManager {
constructor(runtime) {
this._runtime = runtime
this.entities = new Map()
this._nextEntityId = 1
this._staticVersion = 0
this._dynamicEntityIds = new Set()
this._staticEntityIds = new Set()
this._intIdMap = new Map()
this._nextInternalId = 1
this._needsEntityListRebuild = true
this._allEntities = []
this._dynamicEntities = []
}

spawnEntity(id, config = {}) {
const entityId = id || `entity_${this._nextEntityId++}`
const spawnPos = config.position ? [...config.position] : [0, 0, 0]
const entity = {
id: entityId, model: config.model || null,
position: [...spawnPos],
rotation: config.rotation || [0, 0, 0, 1],
scale: config.scale ? [...config.scale] : [1, 1, 1],
velocity: [0, 0, 0], mass: 1, bodyType: 'static', collider: null,
parent: null, children: new Set(),
_appState: null, _appName: config.app || null, _config: config.config || null, custom: null,
_spawnPosition: spawnPos
}
this.entities.set(entityId, entity)
this._needsEntityListRebuild = true
entity._intId = this._nextInternalId++
this._intIdMap.set(entityId, entity._intId)
this._staticVersion++
if (entity.bodyType !== 'static') this._dynamicEntityIds.add(entityId)
else this._staticEntityIds.add(entityId)
this._runtime._log('entity_spawn', { id: entityId, config }, { sourceEntity: entityId })
if (config.parent) {
const p = this.entities.get(config.parent)
if (p) { entity.parent = config.parent; p.children.add(entityId) }
}
if (config.autoTrimesh && entity.model && this._runtime._physics) {
entity.collider = { type: 'trimesh', model: entity.model }
this._runtime._physics.addStaticTrimeshAsync(this._runtime.resolveAssetPath(entity.model), 0, entity.position || [0,0,0])
.then(id => { entity._physicsBodyId = id })
.catch(e => console.error(`[AppRuntime] Failed to create trimesh for ${entity.model}:`, e.message))
}
if (config.app) this._runtime._appManager.attachApp(entityId, config.app).catch(e => console.error(`[AppRuntime] Failed to attach app ${config.app}:`, e.message))
this._runtime._spatialInsert(entity)
return entity
}

destroyEntity(entityId) {
const entity = this.entities.get(entityId); if (!entity) return
this._staticVersion++
this._dynamicEntityIds.delete(entityId)
this._staticEntityIds.delete(entityId)
this._intIdMap.delete(entityId)
this._runtime._activeDynamicIds.delete(entityId)
this._runtime._suspendedEntityIds.delete(entityId)
this._runtime._interactableIds.delete(entityId)
if (entity._physicsBodyId !== undefined) this._runtime._physicsBodyToEntityId.delete(entity._physicsBodyId)
this._runtime._log('entity_destroy', { id: entityId }, { sourceEntity: entityId })
for (const childId of [...entity.children]) this.destroyEntity(childId)
if (entity.parent) { const p = this.entities.get(entity.parent); if (p) p.children.delete(entityId) }
this._runtime._eventBus.destroyScope(entityId)
this._runtime._appManager.detachApp(entityId); this._runtime._spatialRemove(entityId); this.entities.delete(entityId)
this._needsEntityListRebuild = true
}

_rebuildEntityLists() {
if (!this._needsEntityListRebuild) return
this._allEntities = Array.from(this.entities.values())
this._dynamicEntities = this._allEntities.filter(e => e.bodyType !== 'static')
this._needsEntityListRebuild = false
}
}
15 changes: 15 additions & 0 deletions src/apps/runtime/PhysicsLODManager.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/connection/ConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ export class ConnectionManager extends EventEmitter {
}
}

sendBinary(clientId, data, unreliable) {
const client = this.clients.get(clientId)
if (!client || !client.transport.isOpen) return false
try {
if (unreliable) return client.transport.sendUnreliable(data)
return client.transport.send(data)
} catch (err) {
console.error(`[connection] sendBinary error to ${clientId}:`, err.message)
return false
}
}

broadcast(type, payload = {}) {
const data = pack({ type, payload })
const unreliable = isUnreliable(type)
Expand Down
5 changes: 0 additions & 5 deletions src/netcode/LagCompensator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ export class LagCompensator {
entry.velocity[0] = velocity[0]; entry.velocity[1] = velocity[1]; entry.velocity[2] = velocity[2]
if (ring.len < 128) ring.len++
else ring.head = (ring.head + 1) % 128

const cutoff = Date.now() - this.historyWindow
while (ring.len > 0 && ring.buf[ring.head].timestamp < cutoff) {
ring.head = (ring.head + 1) % 128; ring.len--
}
}

getPlayerStateAtTime(playerId, millisAgo) {
Expand Down
20 changes: 8 additions & 12 deletions src/netcode/NetworkState.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,17 @@ export class NetworkState {
}

getSnapshot() {
const players = Array.from(this.players.values())
for (let i = 0; i < players.length; i++) {
const p = players[i]
if (p.crouch === undefined) p.crouch = 0
if (p.lookPitch === undefined) p.lookPitch = 0
if (p.lookYaw === undefined) p.lookYaw = 0
}
return {
tick: this.tick,
timestamp: this.timestamp,
players: this.getAllPlayers().map(p => ({
id: p.id,
position: p.position,
rotation: p.rotation,
velocity: p.velocity,
onGround: p.onGround,
health: p.health,
inputSequence: p.inputSequence,
crouch: p.crouch || 0,
lookPitch: p.lookPitch || 0,
lookYaw: p.lookYaw || 0
}))
players
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/netcode/PhysicsIntegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ export class PhysicsIntegration {
const charId = data.charId
const onGround = data.onGround
const vy = onGround ? (state.velocity[1] > 0 ? state.velocity[1] : 0) : state.velocity[1] + this.config.gravity[1] * deltaTime
this.physicsWorld.setCharacterVelocity(charId, [state.velocity[0], vy, state.velocity[2]])
this.physicsWorld.setCharacterVelocity(charId, state.velocity[0], vy, state.velocity[2])
this.physicsWorld.updateCharacter(charId, deltaTime)
const pos = this.physicsWorld.getCharacterPosition(charId)
const vel = this.physicsWorld.getCharacterVelocity(charId)
this.physicsWorld.getCharacterPosition(charId, state.position)
this.physicsWorld.getCharacterVelocity(charId, state.velocity)
data.onGround = this.physicsWorld.getCharacterGroundState(charId)
state.position = pos
state.velocity = vel
state.onGround = data.onGround
return state
}
Expand Down
3 changes: 2 additions & 1 deletion src/netcode/PlayerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class PlayerManager {
joinTime: Date.now()
}
this.players.set(playerId, player)
this.inputBuffers.set(playerId, [])
player.inputBuffer = []
this.inputBuffers.set(playerId, player.inputBuffer)
this._connectedGen++
return playerId
}
Expand Down
Loading