## Writes what a ClientGame is showing into the client's own copy of the world, so ## that everything which draws a Simulation draws a networked match the same way. ## ## The client's world is a Simulation that is never stepped. ClientWorld keeps its ## pieces, props, chests and tide in line with the server. This adds the rest of the ## picture, afresh every frame: every entity the server has told this client about, ## where it is being drawn now (everyone else from the recent past, the client's own ## player where it was predicted), the barge, the clock and the sea level. Nothing ## here is authoritative, and nothing reads it back but views and the client's own ## check before placing a piece. class_name ClientMirror extends RefCounted ## The entity the camera belongs on: the client's own player, or whoever it is ## spectating. 0 outside a match. var watched_id: int = 0 ## The client's own player while it is in the match, or 0. var own_id: int = 0 ## Where the barge is being drawn, or Vector3.INF when it isn't crossing. var barge_at: Vector3 = Vector3.INF ## The moment being drawn, in simulation ticks. var time: float = 0.0 var _drop: Drop = null var _drop_world: Simulation = null func sync(game: ClientGame) -> void: var world: Simulation = game.world watched_id = 0 own_id = 0 barge_at = Vector3.INF if world == null: return var receiver: SnapshotReceiver = game.receiver var playing: bool = game.playing() var in_match: bool = receiver.own_id != 0 and game.remote.started if playing: # The own player is drawn part way through the newest predicted tick. time = float(game.prediction.next_tick - 1) + game.clock.fraction() elif in_match: time = game.remote.time else: time = float(receiver.sim_tick) world.tick = maxi(floori(time), 0) world.sea_level = world.tide.level_at(world.tick) if world.tide != null else receiver.sea_level if in_match: if _drop == null or _drop_world != world: # The route comes from the seed alone, as the server's does. _drop = Drop.plan(world.ground, SimRng.new(game.world_seed), 0) _drop_world = world _drop.finished = _drop.is_over(world.tick) world.drop = _drop if not _drop.finished: barge_at = _drop.barge_position(world.tick).lerp(_drop.barge_position(world.tick + 1), time - floorf(time)) else: world.drop = null var seen: Dictionary = {} var pickups: Array[int] = [] var projectiles: Array[int] = [] if in_match: for entity_id: int in receiver.entities: var record: PackedInt32Array = receiver.entities[entity_id] var shown: Dictionary = game.remote.sample(entity_id) if shown.is_empty(): continue var archetype: int = record[NetSnapshot.R_ARCHETYPE] var entity: SimEntity = world.entities.find(entity_id) if entity == null: entity = world.entities.restore(entity_id, archetype) elif entity.archetype != archetype: continue seen[entity_id] = true entity.position = shown["position"] match archetype: SimEntity.ARCHETYPE_PLAYER: _show_player(entity, record, shown) SimEntity.ARCHETYPE_PICKUP: entity.item = record[NetSnapshot.R_ITEM] - 1 entity.item_count = record[NetSnapshot.R_COUNT] entity.item_tier = record[NetSnapshot.R_TIER] pickups.append(entity_id) SimEntity.ARCHETYPE_PROJECTILE: entity.item = record[NetSnapshot.R_ITEM] - 1 entity.owner_id = record[NetSnapshot.R_OWNER] projectiles.append(entity_id) if seen.has(receiver.own_id): watched_id = receiver.own_id if playing and seen.has(receiver.own_id): own_id = receiver.own_id _show_own(world.entities.find(own_id), game) for entity: SimEntity in world.entities.entities(): if not seen.has(entity.id): world.entities.remove(entity.id) pickups.sort() projectiles.sort() world.pickups = pickups world.projectiles = projectiles func _show_player(entity: SimEntity, record: PackedInt32Array, shown: Dictionary) -> void: var flags: int = record[NetSnapshot.R_FLAGS] entity.yaw = posmod(roundi(float(shown["yaw"]) / TAU * PlayerCommand.YAW_STEPS), PlayerCommand.YAW_STEPS) entity.pitch = clampi(roundi(float(shown["pitch"]) / (PI * 0.5) * PlayerCommand.PITCH_MAX), -PlayerCommand.PITCH_MAX, PlayerCommand.PITCH_MAX) entity.health = record[NetSnapshot.R_HEALTH] entity.armor = record[NetSnapshot.R_ARMOR] entity.on_ground = flags & NetSnapshot.FLAG_ON_GROUND != 0 entity.crouching = flags & NetSnapshot.FLAG_CROUCHING != 0 entity.swimming = flags & NetSnapshot.FLAG_SWIMMING != 0 entity.eliminated = flags & NetSnapshot.FLAG_ELIMINATED != 0 entity.drop_state = (flags >> NetSnapshot.FLAG_DROP_SHIFT) & 3 # What is in the player's hands, for whoever draws it: the item's code, or -1. entity.item = record[NetSnapshot.R_HELD] - 1 if entity.drop_state == Drop.ABOARD and barge_at != Vector3.INF: entity.position = barge_at ## The client's own player: where and how it was predicted, not where the server last ## had it. func _show_own(entity: SimEntity, game: ClientGame) -> void: var me: SimEntity = game.prediction.player entity.position = game.own_position() entity.yaw = me.yaw entity.pitch = me.pitch entity.on_ground = me.on_ground entity.crouching = me.crouching entity.swimming = me.swimming entity.drop_state = me.drop_state if me.drop_state == Drop.ABOARD and barge_at != Vector3.INF: entity.position = barge_at