|
|
|
@@ -0,0 +1,104 @@
|
|
|
|
|
## Capture scenario for the network client (issue N10): a headless server listening
|
|
|
|
|
## on loopback UDP in this process, and the real ClientSession connected to it, played
|
|
|
|
|
## by its autoplay script. The picture is the client's own: its copy of the world, its
|
|
|
|
|
## predicted player, everyone else as the server's snapshots have them.
|
|
|
|
|
##
|
|
|
|
|
## Stages: barge (a few seconds into the drop), glide, landed.
|
|
|
|
|
extends Node3D
|
|
|
|
|
|
|
|
|
|
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
|
|
|
|
|
const HOST: String = "127.0.0.1"
|
|
|
|
|
const LIMIT_ROUNDS: int = 200 * Tuning.TICK_RATE_HZ
|
|
|
|
|
|
|
|
|
|
@export var stage: String = "barge"
|
|
|
|
|
@export var match_seed: int = 2
|
|
|
|
|
@export var bots: int = 23
|
|
|
|
|
|
|
|
|
|
var _loop: ServerLoop
|
|
|
|
|
var _net: NetServer
|
|
|
|
|
var _game: NetGame
|
|
|
|
|
var _session: ClientSession
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
|
var rules: MatchRules = MatchRules.new()
|
|
|
|
|
rules.max_players = bots + 1
|
|
|
|
|
rules.lobby_ticks = 6
|
|
|
|
|
rules.countdown_ticks = 6
|
|
|
|
|
_loop = ServerLoop.new(match_seed, 0, bots, rules)
|
|
|
|
|
_net = NetServer.new()
|
|
|
|
|
if _net.listen(0, HOST) != OK:
|
|
|
|
|
push_error("client capture: the server couldn't listen on loopback")
|
|
|
|
|
return
|
|
|
|
|
_game = NetGame.new(_loop, _net)
|
|
|
|
|
_session = ClientSession.new()
|
|
|
|
|
_session.input.autoplay = true
|
|
|
|
|
if _session.connect_to(HOST, _net.port(), "Mara") != OK:
|
|
|
|
|
push_error("client capture: the client couldn't open a connection")
|
|
|
|
|
return
|
|
|
|
|
add_child(_session)
|
|
|
|
|
# The picture is of one moment, so the session isn't left running behind it.
|
|
|
|
|
_session.set_process(false)
|
|
|
|
|
var me: SimEntity = _session.game.prediction.player
|
|
|
|
|
var since: int = -1
|
|
|
|
|
for _round: int in LIMIT_ROUNDS:
|
|
|
|
|
_game.poll()
|
|
|
|
|
_loop.run_tick()
|
|
|
|
|
_game.after_tick()
|
|
|
|
|
_session.pump(DT)
|
|
|
|
|
var reached: bool = false
|
|
|
|
|
if _session.game.playing():
|
|
|
|
|
match stage:
|
|
|
|
|
"barge":
|
|
|
|
|
reached = me.drop_state == Drop.ABOARD and _session.game.prediction.next_tick > 3 * Tuning.TICK_RATE_HZ
|
|
|
|
|
"glide":
|
|
|
|
|
reached = me.drop_state == Drop.GLIDING
|
|
|
|
|
"landed":
|
|
|
|
|
reached = me.drop_state == Drop.NONE
|
|
|
|
|
if reached and since < 0:
|
|
|
|
|
since = 0
|
|
|
|
|
elif since >= 0:
|
|
|
|
|
since += 1
|
|
|
|
|
if since >= (2 * Tuning.TICK_RATE_HZ if stage != "barge" else 1):
|
|
|
|
|
break
|
|
|
|
|
if since < 0:
|
|
|
|
|
push_error("client capture: never reached stage '%s'" % stage)
|
|
|
|
|
return
|
|
|
|
|
_session.view.settle()
|
|
|
|
|
var others: int = 0
|
|
|
|
|
var camera: Camera3D = _session.view.camera
|
|
|
|
|
for entity: SimEntity in _session.game.world.entities.entities():
|
|
|
|
|
if entity.archetype != SimEntity.ARCHETYPE_PLAYER or entity.id == _session.mirror.own_id or entity.eliminated:
|
|
|
|
|
continue
|
|
|
|
|
var figure: Node3D = _session.view.entities.figure_of(entity.id)
|
|
|
|
|
var to: Vector3 = (figure.position if figure != null else entity.position) + Vector3(0.0, 1.0, 0.0) - camera.position
|
|
|
|
|
if to.length() < 150.0 and to.normalized().dot(-camera.basis.z) > 0.8:
|
|
|
|
|
others += 1
|
|
|
|
|
var flow: Match = _loop.match_flow
|
|
|
|
|
_caption("NETWORK CLIENT · %s · %s · seed %d\njoined %s over UDP as \"%s\" · %d players drawn, %d others ahead of the camera\nsimulation tick %d on the server, %d predicted here · %d snapshots, %d world messages, %d corrections" % [
|
|
|
|
|
stage.to_upper(), flow.phase_name(), match_seed, HOST, _net.name_of(_session.link.handle),
|
|
|
|
|
_session.view.entities.figure_count(), others, _loop.sim.tick, _session.game.prediction.next_tick,
|
|
|
|
|
_session.game.receiver.applied, _session.game.replica.applied, _session.game.prediction.mispredictions,
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _exit_tree() -> void:
|
|
|
|
|
if _session != null:
|
|
|
|
|
_session.leave()
|
|
|
|
|
if _net != null:
|
|
|
|
|
_net.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _caption(text: String) -> void:
|
|
|
|
|
var settings: LabelSettings = LabelSettings.new()
|
|
|
|
|
settings.font_size = 20
|
|
|
|
|
settings.font_color = Color(0.96, 0.94, 0.88)
|
|
|
|
|
settings.outline_size = 6
|
|
|
|
|
settings.outline_color = Color(0.08, 0.09, 0.1)
|
|
|
|
|
var label: Label = Label.new()
|
|
|
|
|
label.label_settings = settings
|
|
|
|
|
label.text = text
|
|
|
|
|
label.position = Vector2(18.0, 14.0)
|
|
|
|
|
var layer: CanvasLayer = CanvasLayer.new()
|
|
|
|
|
layer.add_child(label)
|
|
|
|
|
add_child(layer)
|