N11: Build ghost, client flood and shot effects in the playable client
- Build ghost: ClientSession shows a BuildGhost in build mode while the client plays, asking Building.check() of its own world and predicted player with the mouse's newest look, so it agrees with the server. - Flooded basins: ClientMirror.follow_flood() spreads the client's flood as the server's tide tick does, from the tide and the pieces it has. WaterView passes a mask of flooded cells (Flood.pours says when it changed) to the sea shader, which leaves dry cells undrawn. - Shots, impacts and swings: a new FX message (NetFx) goes unreliably on the state channel next to each snapshot, with what happened within NET_FX_RADIUS_METRES of what the client watches or involving that player. ClientGame keeps them in `fx` for views and sound. They change no state, so a lost one is a missed flash; sending them reliably would add to the traffic a bad link can least afford (N8). The fuzzer covers the new parser. - Captures: client_build and client_build_refused show the ghost in its allowed and refused tints; client_basin rings shore ground with breakwaters and plays until the tide stands over it, and the server, the client's flood and the drawn sea all keep the basin dry. - Tests: NetFx round trips, limits and malformed input; a landed player's shots and impacts reach a client, each matching a server event; the client keeps a ringed basin dry through the first rise, floods it with no ring, catches up after a long frame; the water mask follows pours. The held item is still not drawn; that needs the models (A4, #44). Closes #78 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -51,6 +51,11 @@ func collect_seeds() -> void:
|
||||
seeds.append(bytes)
|
||||
if tick % 10 == 0:
|
||||
seeds.append(game.snapshot_for(1))
|
||||
seeds.append(NetFx.encode([
|
||||
{"kind": NetFx.SHOT, "tick": 90, "player": 3, "item": 2, "position": Vector3(100.0, 12.0, 200.0), "yaw": 1.0, "pitch": -0.2},
|
||||
{"kind": NetFx.IMPACT, "tick": 91, "player": 3, "item": 2, "position": Vector3(110.0, 11.0, 210.0), "hit": Weapons.HIT_PLAYER, "target": 7},
|
||||
{"kind": NetFx.HARVEST, "tick": 92, "player": 4, "item": -1, "position": Vector3(50.0, 9.0, 60.0), "prop": 12, "piece": 0},
|
||||
] as Array[Dictionary]))
|
||||
var command: PlayerCommand = PlayerCommand.from_input(12, Vector2(0.3, 1.0), 1.0, 0.2, PlayerCommand.BUTTON_FIRE, 2, 1)
|
||||
seeds.append(NetSnapshot.encode_input(40, [command, command] as Array[PlayerCommand]))
|
||||
seeds.append(NetProtocol.encode_hello("Fuzzer"))
|
||||
@@ -100,7 +105,7 @@ func feed(bytes: PackedByteArray) -> void:
|
||||
var results: Array = [
|
||||
NetProtocol.decode_hello(bytes), NetProtocol.decode_welcome(bytes), NetProtocol.decode_reject(bytes),
|
||||
NetSnapshot.decode_input(bytes), NetSnapshot.decode_snapshot(bytes), NetSnapshot.decode_match(bytes),
|
||||
NetWorld.decode_world(bytes), NetWorld.decode_own(bytes),
|
||||
NetWorld.decode_world(bytes), NetWorld.decode_own(bytes), NetFx.decode(bytes),
|
||||
NetDiscovery.decode_query(bytes), NetDiscovery.decode_info(bytes), NetDiscovery.decode_register(bytes),
|
||||
NetDiscovery.decode_challenge(bytes), NetDiscovery.decode_list(bytes),
|
||||
]
|
||||
@@ -152,5 +157,6 @@ func _mutate(bytes: PackedByteArray) -> void:
|
||||
var types: Array[int] = [
|
||||
NetProtocol.MSG_HELLO, NetProtocol.MSG_WELCOME, NetProtocol.MSG_REJECT, NetSnapshot.MSG_MATCH,
|
||||
NetWorld.MSG_WORLD, NetWorld.MSG_OWN, NetSnapshot.MSG_SNAPSHOT, NetSnapshot.MSG_INPUT,
|
||||
NetFx.MSG_FX,
|
||||
]
|
||||
bytes[0] = types[rng.randi_range(0, types.size() - 1)]
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
extends TestCase
|
||||
|
||||
## Issue N11: shots, impacts and swings on the wire (NetFx).
|
||||
|
||||
|
||||
func _records() -> Array[Dictionary]:
|
||||
return [
|
||||
{"kind": NetFx.SHOT, "tick": 400, "player": 3, "item": 2, "position": Vector3(100.25, 12.5, 200.0), "yaw": 1.2345, "pitch": -0.25},
|
||||
{"kind": NetFx.IMPACT, "tick": 401, "player": 3, "item": 2, "position": Vector3(130.0, 11.0, 220.0), "hit": Weapons.HIT_PLAYER, "target": 700},
|
||||
{"kind": NetFx.HARVEST, "tick": 402, "player": 4, "item": -1, "position": Vector3(50.0, 9.0, 60.0), "prop": 12, "piece": 0},
|
||||
{"kind": NetFx.HARVEST, "tick": 402, "player": 5, "item": -1, "position": Vector3(51.0, 9.0, 61.0), "prop": -1, "piece": 33},
|
||||
]
|
||||
|
||||
|
||||
func test_records_survive_the_trip() -> void:
|
||||
var bytes: PackedByteArray = NetFx.encode(_records())
|
||||
var message: Dictionary = NetFx.decode(bytes)
|
||||
assert_false(message.is_empty())
|
||||
assert_eq(message["tick"], 402)
|
||||
var events: Array = message["events"]
|
||||
assert_eq(events.size(), 4)
|
||||
for index: int in 4:
|
||||
var sent: Dictionary = _records()[index]
|
||||
var got: Dictionary = events[index]
|
||||
for key: String in sent:
|
||||
if key == "position":
|
||||
assert_true((got[key] as Vector3).distance_to(sent[key]) <= 0.06, "%s within 6 cm" % key)
|
||||
elif key == "yaw" or key == "pitch":
|
||||
assert_near(float(got[key]), float(sent[key]), 0.0001)
|
||||
else:
|
||||
assert_eq(got[key], sent[key], "event %d %s" % [index, key])
|
||||
assert_true(NetFx.aim(events[0]).is_normalized())
|
||||
|
||||
|
||||
func test_an_event_from_the_simulation_becomes_a_record() -> void:
|
||||
var direction: Vector3 = Vector3(0.3, -0.2, 0.9).normalized()
|
||||
var shot: Dictionary = NetFx.from_event({"type": "shot", "player": 9, "item": 4, "from": Vector3(1.0, 2.0, 3.0), "direction": direction}, 77)
|
||||
assert_eq(shot["kind"], NetFx.SHOT)
|
||||
assert_true(NetFx.aim(shot).distance_to(direction) < 0.0001, "the aim comes back")
|
||||
var swing: Dictionary = NetFx.from_event({"type": "harvest", "player": 9, "prop": 5, "material": 0, "amount": 3}, 77, {9: Vector3(4.0, 5.0, 6.0)})
|
||||
assert_eq(swing["position"], Vector3(4.0, 5.0, 6.0))
|
||||
assert_true(NetFx.from_event({"type": "pickup", "player": 9}, 77).is_empty(), "only FX events")
|
||||
|
||||
|
||||
func test_too_many_keep_the_newest_and_the_byte_limit() -> void:
|
||||
var many: Array[Dictionary] = []
|
||||
for index: int in 300:
|
||||
many.append({"kind": NetFx.SHOT, "tick": 1000 + index / 10, "player": index, "item": 1, "position": Vector3(index, 1.0, 2.0), "yaw": 0.0, "pitch": 0.0})
|
||||
var bytes: PackedByteArray = NetFx.encode(many)
|
||||
assert_true(bytes.size() <= Tuning.NET_SNAPSHOT_MAX_BYTES, "%d bytes" % bytes.size())
|
||||
var events: Array = NetFx.decode(bytes)["events"]
|
||||
assert_true(events.size() > 40)
|
||||
assert_eq(int(events[events.size() - 1]["player"]), 299, "the newest is kept")
|
||||
assert_eq(NetFx.encode([] as Array[Dictionary]).size(), 0)
|
||||
|
||||
|
||||
func test_malformed_messages_are_refused() -> void:
|
||||
var bytes: PackedByteArray = NetFx.encode(_records())
|
||||
for cut: int in bytes.size():
|
||||
assert_true(NetFx.decode(bytes.slice(0, cut)).is_empty(), "cut at %d" % cut)
|
||||
var longer: PackedByteArray = bytes.duplicate()
|
||||
longer.append(0)
|
||||
assert_true(NetFx.decode(longer).is_empty(), "trailing bytes")
|
||||
var bad_kind: PackedByteArray = bytes.duplicate()
|
||||
bad_kind[NetFx.HEADER_BYTES] = 9
|
||||
assert_true(NetFx.decode(bad_kind).is_empty(), "unknown kind")
|
||||
@@ -0,0 +1 @@
|
||||
uid://dyrisqacc7jfk
|
||||
Reference in New Issue
Block a user