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:
+25
-14
@@ -165,25 +165,36 @@ static func _traps_a_player(sim: Simulation, boxes: Array[AABB]) -> bool:
|
||||
for entity: SimEntity in sim.entities.entities():
|
||||
if entity.archetype != SimEntity.ARCHETYPE_PLAYER or entity.health <= 0:
|
||||
continue
|
||||
var feet: Vector3 = entity.position
|
||||
var height: float = Movement.body_height(entity)
|
||||
for box: AABB in boxes:
|
||||
if not _body_overlaps(box, feet, height):
|
||||
continue
|
||||
if box.end.y - feet.y > Tuning.STEP_HEIGHT:
|
||||
return true
|
||||
if would_trap(entity, boxes):
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
## True if `player`'s body overlaps the boxes by more than a step.
|
||||
static func would_trap(player: SimEntity, boxes: Array[AABB]) -> bool:
|
||||
var feet: Vector3 = player.position
|
||||
var height: float = Movement.body_height(player)
|
||||
for box: AABB in boxes:
|
||||
if not _body_overlaps(box, feet, height):
|
||||
continue
|
||||
if box.end.y - feet.y > Tuning.STEP_HEIGHT:
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
static func _lift_players(sim: Simulation, boxes: Array[AABB]) -> void:
|
||||
for entity: SimEntity in sim.entities.entities():
|
||||
if entity.archetype != SimEntity.ARCHETYPE_PLAYER:
|
||||
continue
|
||||
var feet: Vector3 = entity.position
|
||||
var height: float = Movement.body_height(entity)
|
||||
for box: AABB in boxes:
|
||||
if _body_overlaps(box, feet, height) and box.end.y > entity.position.y:
|
||||
entity.position.y = box.end.y
|
||||
if entity.archetype == SimEntity.ARCHETYPE_PLAYER:
|
||||
lift(entity, boxes)
|
||||
|
||||
|
||||
## Lifts `player` onto any low step of the boxes it is standing in.
|
||||
static func lift(player: SimEntity, boxes: Array[AABB]) -> void:
|
||||
var feet: Vector3 = player.position
|
||||
var height: float = Movement.body_height(player)
|
||||
for box: AABB in boxes:
|
||||
if _body_overlaps(box, feet, height) and box.end.y > player.position.y:
|
||||
player.position.y = box.end.y
|
||||
|
||||
|
||||
static func _body_overlaps(box: AABB, feet: Vector3, height: float) -> bool:
|
||||
|
||||
@@ -45,6 +45,9 @@ var health: PackedInt32Array = PackedInt32Array()
|
||||
var solid_ids: PackedInt32Array = PackedInt32Array()
|
||||
## Prop IDs in the order they were destroyed, for views to catch up on.
|
||||
var destroyed: PackedInt32Array = PackedInt32Array()
|
||||
## Props damaged since whoever sends them to clients last cleared this. A set: the
|
||||
## values mean nothing. Not match state.
|
||||
var changed: Dictionary = {}
|
||||
|
||||
var _ground: SimGround
|
||||
## SimGround solid ID -> prop ID, for props whose box is in `_ground`.
|
||||
@@ -105,6 +108,7 @@ func damage(prop: int, amount: int) -> bool:
|
||||
if prop < 0 or prop >= count() or not is_standing(prop) or amount <= 0:
|
||||
return false
|
||||
health[prop] = maxi(health[prop] - amount, 0)
|
||||
changed[prop] = true
|
||||
if health[prop] > 0:
|
||||
return false
|
||||
destroyed.append(prop)
|
||||
|
||||
@@ -79,6 +79,9 @@ var pieces: Dictionary = {}
|
||||
var neighbours: Dictionary = {}
|
||||
## Removed piece IDs, in the order they went, for views to catch up on.
|
||||
var removed: PackedInt32Array = PackedInt32Array()
|
||||
## IDs of pieces placed or damaged since whoever sends them to clients last cleared
|
||||
## this. A set: the values mean nothing. Not match state.
|
||||
var changed: Dictionary = {}
|
||||
|
||||
## Props, whose boxes don't count as ground. Set by Simulation.
|
||||
var props: PropSet = null
|
||||
@@ -256,10 +259,12 @@ func touching(boxes: Array[AABB], ground: SimGround) -> PackedInt32Array:
|
||||
|
||||
|
||||
## Adds a piece, its solids and its neighbour links. The caller has validated it.
|
||||
func add(kind: int, material: int, slot: Vector4i, facing: int, owner: int, tick: int, ground: SimGround) -> Piece:
|
||||
## A network client, which is told each piece's ID by the server, passes it as
|
||||
## `piece_id`; the server leaves it out and the next free ID is used.
|
||||
func add(kind: int, material: int, slot: Vector4i, facing: int, owner: int, tick: int, ground: SimGround, piece_id: int = 0) -> Piece:
|
||||
var piece: Piece = Piece.new()
|
||||
piece.id = _next_id
|
||||
_next_id += 1
|
||||
piece.id = piece_id if piece_id > 0 else _next_id
|
||||
_next_id = maxi(_next_id, piece.id + 1)
|
||||
piece.kind = kind
|
||||
piece.material = material
|
||||
piece.slot = slot
|
||||
@@ -283,6 +288,7 @@ func add(kind: int, material: int, slot: Vector4i, facing: int, owner: int, tick
|
||||
piece.solid_ids.append(solid_id)
|
||||
_by_solid[solid_id] = piece.id
|
||||
pieces[piece.id] = piece
|
||||
changed[piece.id] = true
|
||||
_building.append(piece.id)
|
||||
_by_slot[slot] = piece.id
|
||||
neighbours[piece.id] = links
|
||||
@@ -311,6 +317,7 @@ func remove(piece_id: int, ground: SimGround) -> bool:
|
||||
_building.remove_at(building_at)
|
||||
_by_slot.erase(piece.slot)
|
||||
pieces.erase(piece_id)
|
||||
changed.erase(piece_id)
|
||||
removed.append(piece_id)
|
||||
return true
|
||||
|
||||
@@ -338,6 +345,34 @@ static func built_gain(piece: Piece, elapsed: int) -> int:
|
||||
return (piece.max_health - piece.start_health) * elapsed / piece.build_ticks
|
||||
|
||||
|
||||
## The damage a piece has taken so far: what building has given it, less what it has.
|
||||
static func damage_taken(piece: Piece) -> int:
|
||||
return piece.start_health + built_gain(piece, piece.build_ticks - piece.build_ticks_left) - piece.health
|
||||
|
||||
|
||||
## Puts a piece's building and health where another copy of the match says they are:
|
||||
## `elapsed` ticks into building, with `damage` taken. For a network client, which is
|
||||
## told these rather than stepping the pieces itself.
|
||||
func set_progress(piece_id: int, elapsed: int, damage_so_far: int) -> void:
|
||||
var piece: Piece = pieces.get(piece_id)
|
||||
if piece == null:
|
||||
return
|
||||
elapsed = clampi(elapsed, 0, piece.build_ticks)
|
||||
piece.build_ticks_left = piece.build_ticks - elapsed
|
||||
piece.health = maxi(piece.start_health + built_gain(piece, elapsed) - damage_so_far, 0)
|
||||
var at: int = _building.find(piece_id)
|
||||
if piece.build_ticks_left > 0 and at < 0:
|
||||
_building.append(piece_id)
|
||||
_building.sort()
|
||||
elif piece.build_ticks_left == 0 and at >= 0:
|
||||
_building.remove_at(at)
|
||||
|
||||
|
||||
## IDs of pieces still building, ascending.
|
||||
func building() -> PackedInt32Array:
|
||||
return _building
|
||||
|
||||
|
||||
## Advances building by one tick: each unfinished piece gains its share of health.
|
||||
func advance_building() -> void:
|
||||
if _building.is_empty():
|
||||
@@ -367,6 +402,7 @@ func damage(piece_id: int, amount: int, cause: String, ground: SimGround, events
|
||||
return false
|
||||
piece.health = maxi(piece.health - amount, 0)
|
||||
if piece.health > 0:
|
||||
changed[piece_id] = true
|
||||
return false
|
||||
destroy(piece_id, cause, ground, events)
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user