N5: Replicate building, world damage, tide and loot
Built pieces, props' health, opened chests and the tide's schedule now reach clients as WORLD messages on the reliable channel, and each player is told what they carry as OWN messages. - NetWorld: the WORLD and OWN wire formats, and world_hash(), a digest of everything replicated this way. - WorldSender: one message after each tick in which something changed, the same for everyone, and the whole state for someone joining or a new match. StructureSet and PropSet keep the change lists it reads. - ClientWorld: applies them to the client's world, so pieces and broken props are solid for prediction. A piece's health follows from its building and damage, so building costs nothing on the wire. - Placing ahead of the server: a provisional piece goes up with the command and is replaced by the server's, or taken back once a snapshot shows the server had its chance. Snapshots carry the newest WORLD number for that. - Protocol version 3. A test plays a whole scripted match on a real island with a ClientGame attached and compares the two worlds' hashes throughout. Closes #36 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+58
-3
@@ -1,6 +1,8 @@
|
||||
## Joins the transport to the match: people who connect become participants, their
|
||||
## input packets become commands through ServerLoop.submit(), and everyone is sent
|
||||
## snapshots of what they may see, 15 times a second.
|
||||
## snapshots of what they may see, 15 times a second. Built pieces, props, chests and
|
||||
## the tide's schedule go to everyone reliably as they change (WorldSender), and each
|
||||
## player is told what they carry whenever it changes.
|
||||
##
|
||||
## Call poll() once a frame before running ticks, and after_tick() after each tick.
|
||||
class_name NetGame
|
||||
@@ -9,14 +11,22 @@ extends RefCounted
|
||||
var loop: ServerLoop
|
||||
var net: NetServer
|
||||
var sender: SnapshotSender = SnapshotSender.new()
|
||||
var world: WorldSender = WorldSender.new()
|
||||
## What happened during the last poll(): {"type": "joined" or "left", "handle", "name",
|
||||
## "reason"}, for the server's log.
|
||||
var events: Array[Dictionary] = []
|
||||
## Input packets that couldn't be read, so far.
|
||||
var bad_inputs: int = 0
|
||||
|
||||
## The WORLD message the last after_tick() sent everyone, or empty, and the OWN
|
||||
## messages it sent, by handle. For owners that carry the packets themselves.
|
||||
var world_changes: PackedByteArray = PackedByteArray()
|
||||
var own_changes: Dictionary = {}
|
||||
|
||||
## Handle -> the newest command tick received from them this match.
|
||||
var _last_command: Dictionary = {}
|
||||
## Handle -> what they were last told they carry, as _own_notice() lists it.
|
||||
var _own_sent: Dictionary = {}
|
||||
|
||||
|
||||
func _init(server_loop: ServerLoop, server_net: NetServer) -> void:
|
||||
@@ -24,6 +34,7 @@ func _init(server_loop: ServerLoop, server_net: NetServer) -> void:
|
||||
net = server_net
|
||||
net.max_players = loop.match_flow.rules.max_players
|
||||
net.match_seed = loop.match_flow.current_seed()
|
||||
world.begin(loop.sim)
|
||||
|
||||
|
||||
## Services the network: joins and leaves, then every input packet that arrived.
|
||||
@@ -37,6 +48,7 @@ func poll() -> void:
|
||||
if loop.match_flow.join(handle):
|
||||
adopt(handle)
|
||||
_send_match(handle)
|
||||
net.send(handle, NetProtocol.CHANNEL_CONTROL, world_notice(), true)
|
||||
events.append(event)
|
||||
else:
|
||||
net.kick(handle, NetProtocol.REASON_FULL)
|
||||
@@ -44,6 +56,7 @@ func poll() -> void:
|
||||
loop.match_flow.leave(handle)
|
||||
sender.remove_client(handle)
|
||||
_last_command.erase(handle)
|
||||
_own_sent.erase(handle)
|
||||
events.append(event)
|
||||
for packet: Dictionary in net.packets:
|
||||
take_input(packet["handle"], packet["bytes"])
|
||||
@@ -78,10 +91,13 @@ func take_input(handle: int, bytes: PackedByteArray) -> void:
|
||||
loop.submit(handle, command)
|
||||
|
||||
|
||||
## Tells everyone about a change of phase, and sends snapshots when one is due.
|
||||
## Tells everyone about a change of phase and whatever changed in the world, and sends
|
||||
## snapshots when one is due.
|
||||
func after_tick() -> void:
|
||||
var flow: Match = loop.match_flow
|
||||
var changed: bool = false
|
||||
world_changes = PackedByteArray()
|
||||
own_changes.clear()
|
||||
for event: Dictionary in flow.events:
|
||||
if event["type"] == "phase":
|
||||
changed = true
|
||||
@@ -94,6 +110,24 @@ func after_tick() -> void:
|
||||
if changed:
|
||||
for handle: int in net.handles():
|
||||
_send_match(handle)
|
||||
# The world goes before the snapshots, which say how much of it there is to know.
|
||||
if not world.is_for(loop.sim):
|
||||
# A new match's simulation: everyone starts again from the whole of it.
|
||||
world.begin(loop.sim)
|
||||
_own_sent.clear()
|
||||
world_changes = world.full()
|
||||
else:
|
||||
world_changes = world.changes()
|
||||
if not world_changes.is_empty():
|
||||
for handle: int in net.handles():
|
||||
if sender.has_client(handle):
|
||||
net.send(handle, NetProtocol.CHANNEL_CONTROL, world_changes, true)
|
||||
if flow.in_match():
|
||||
for handle: int in _last_command:
|
||||
var bytes: PackedByteArray = _own_notice(handle)
|
||||
if not bytes.is_empty():
|
||||
own_changes[handle] = bytes
|
||||
net.send(handle, NetProtocol.CHANNEL_CONTROL, bytes, true)
|
||||
if not snapshot_due():
|
||||
return
|
||||
for handle: int in net.handles():
|
||||
@@ -111,7 +145,13 @@ func snapshot_due() -> bool:
|
||||
func snapshot_for(handle: int) -> PackedByteArray:
|
||||
var flow: Match = loop.match_flow
|
||||
var own_id: int = int(flow.entity_of.get(handle, 0)) if flow.in_match() else 0
|
||||
return sender.build(handle, loop.sim, viewer_of(handle), loop.ticks, flow.phase, own_id)
|
||||
return sender.build(handle, loop.sim, viewer_of(handle), loop.ticks, flow.phase, own_id, world.seq)
|
||||
|
||||
|
||||
## The whole world as it stands, as one WORLD message: what someone who has just
|
||||
## joined is sent after the MATCH notice.
|
||||
func world_notice() -> PackedByteArray:
|
||||
return world.full()
|
||||
|
||||
|
||||
## The MATCH notice as things stand: the phase, and the match's index and seed.
|
||||
@@ -134,5 +174,20 @@ func viewer_of(handle: int) -> int:
|
||||
return int(flow.entity_of[handle])
|
||||
|
||||
|
||||
## An OWN message for a participant if what they carry has changed since they were
|
||||
## last told, or nothing.
|
||||
func _own_notice(handle: int) -> PackedByteArray:
|
||||
var flow: Match = loop.match_flow
|
||||
var player: SimEntity = loop.sim.entities.find(int(flow.entity_of.get(handle, 0)))
|
||||
if player == null or player.eliminated:
|
||||
return PackedByteArray()
|
||||
var carried: Array = [player.selected, player.materials, player.slot_items, player.slot_counts, player.slot_rounds, player.slot_tiers, player.ammo]
|
||||
if _own_sent.get(handle, []) == carried:
|
||||
return PackedByteArray()
|
||||
# Copies: the player's own arrays go on changing.
|
||||
_own_sent[handle] = carried.duplicate(true)
|
||||
return NetWorld.encode_own(player, loop.sim.tick)
|
||||
|
||||
|
||||
func _send_match(handle: int) -> void:
|
||||
net.send(handle, NetProtocol.CHANNEL_CONTROL, match_notice(), true)
|
||||
|
||||
Reference in New Issue
Block a user