diff --git a/addons/netfox/icons/input-sender.svg b/addons/netfox/icons/input-sender.svg new file mode 100644 index 000000000..3f3f39358 --- /dev/null +++ b/addons/netfox/icons/input-sender.svg @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + diff --git a/addons/netfox/icons/input-sender.svg.import b/addons/netfox/icons/input-sender.svg.import new file mode 100644 index 000000000..8976192c0 --- /dev/null +++ b/addons/netfox/icons/input-sender.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://dd227x8br84rs" +path="res://.godot/imported/input-sender.svg-7b3cd669dc50ce8229dd58ca509f298a.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://addons/netfox/icons/input-sender.svg" +dest_files=["res://.godot/imported/input-sender.svg-7b3cd669dc50ce8229dd58ca509f298a.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/addons/netfox/icons/simulator.svg b/addons/netfox/icons/simulator.svg new file mode 100644 index 000000000..4cf290d18 --- /dev/null +++ b/addons/netfox/icons/simulator.svg @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + diff --git a/addons/netfox/icons/simulator.svg.import b/addons/netfox/icons/simulator.svg.import new file mode 100644 index 000000000..462d8e81c --- /dev/null +++ b/addons/netfox/icons/simulator.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://dyh2wa862n5kx" +path="res://.godot/imported/simulator.svg-a031f70114a4770a3b287d54c5d39a10.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://addons/netfox/icons/simulator.svg" +dest_files=["res://.godot/imported/simulator.svg-a031f70114a4770a3b287d54c5d39a10.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/addons/netfox/input-sender.gd b/addons/netfox/input-sender.gd new file mode 100644 index 000000000..52f0c0c12 --- /dev/null +++ b/addons/netfox/input-sender.gd @@ -0,0 +1,269 @@ +@tool +extends Node +class_name InputSender + +## Stores inputs and sends them to host. +## [br][br] +## +## [InputSender] is a multi purpose node to use on networked games, +## It provides signals to code host and client side logic. +## [InputSender] signals are tied and emitted on [signal NetworkTime.on_tick]. +## +## @experimental: +## InputSenderServer assumes input snapshots arrive as whole. (atomic), if snapshot +## arrives with multiple parts, [InputSender] signals wont be reliable to +## code game logic. + +## Emitted if host received input from remote owner of input_properties. +## Emitted for inputs that arrive within missing-input-history range. +## (see project settings netfox/input-sender/missing-input-history) +## InputSenderServer handles applying received input internally before emitting this signal. +## Use this signal to code host side logic. +signal network_input(tick : int) + +## Emitted if local peer has authority over input_property nodes. +## This signal is emitted for host players too. +## InputSenderServer will apply latest local inputs for this tick internally before +## emitting this signal. +## Use this signal to code client side logic which doesnt interfere with actual game state. +## Examples: Playing a sound, showing a visual effect. +## Dont use this signal to code same game logic on client side as it will not likely +## be same with remote host machine, it will cause syncing issues if you are already +## using some other method to syncronize game state (Syncronizers/Simulators). +signal local_input(tick : int) + +## Emitted if input is missing. +## Input is considered missing if host did not receive input for a tick more than +## missing-input-history. (See project settings netfox/input-sender). +## Host will try to find and apply previous known inputs before emitting this signal. +## If host cant find any previous known input, latest_known_input_tick will be -1. +## Also see late_input signal. An input will be considered missing at first, +## but later it might arrive late. In that sceneario late_input signal will be emitted too. +## Use this signal to code host side prediction logic. +signal missing_input(for_tick : int, latest_known_input_tick : int) + +## Emitted when input arrived so late that it past the missing history size. +## (see project settings netfox/input-sender/missing-input-history) +## InputSenderServer will apply received input state internally before emitting this signal. +## If a late input also past the history limit of input-sender, it will be dropped. +signal late_input(for_tick : int) + +## The root node for resolving node paths in inputs. Defaults to the parent node. +@export var root: Node = get_parent() + +@export_group("Input") +## Properties that define the input for the game simulation. +## [br][br] +## Input properties drive the simulation, which in turn results in updated state +## properties. Input is recorded after every network tick. +@export var input_properties: Array[String] + +# Make sure this exists from the get-go, just not in the scene tree +## Decides which peers will receive updates +var visibility_filter := PeerVisibilityFilter.new() + +var _input_properties := _PropertyPool.new() +var _properties_dirty: bool = false + +var _logger := NetfoxLogger._for_netfox("InputSender") + +# We need these to de-couple input-senders working logic from recording. +# InputSender applies state and emits signals, but this can change saved and synced +# input properties, to prevent that, input-sender will save its pre-logic-inputs +# and apply them whenever logic ends. +# +# TODO we are moving to server pattern, this might be not neccessary after that. +var _saved_inputs_snapshot : _PropertySnapshot +var _property_cache: PropertyCache +var _property_entries: Array[PropertyEntry] = [] + +func _ready() -> void: + if Engine.is_editor_hint(): + return + + # DANGER we dont do deferred call here, because Simulator node depends on + # has_authority_over_input to determine its register logic. + # Therefore it is needed that input-sender is registered and ready to + # return the result of that helper function. + process_settings() + + # Reprocess authority on connect + if NetworkEvents.enabled: + # User might change `multiplayer` - `NetworkEvents` handles that + NetworkEvents.on_client_start.connect(func(__): process_settings()) + else: + multiplayer.connected_to_server.connect(process_settings) + +func _enter_tree() -> void: + if Engine.is_editor_hint(): + return + + if not visibility_filter: + visibility_filter = PeerVisibilityFilter.new() + + if not visibility_filter.get_parent(): + add_child(visibility_filter) + +func _exit_tree(): + if Engine.is_editor_hint(): + return + + for node in _input_properties.get_subjects(): + NetworkSynchronizationServer.deregister(node) + NetworkIdentityServer.deregister_node(node) + NetworkHistoryServer.deregister(node) + + InputSenderServer._deregister_input_sender(self) + +## Process settings. +## [br][br] +## Call this after any change to configuration. Updates based on authority too +## ( calls process_authority ). +func process_settings() -> void: + process_authority() + + _property_cache = PropertyCache.new(root) + _property_entries.clear() + + _saved_inputs_snapshot = _PropertySnapshot.new() + + for property in input_properties: + var property_entry = _property_cache.get_entry(property) + _property_entries.push_back(property_entry) + + # Register identifiers + for node in _input_properties.get_subjects(): + NetworkIdentityServer.register_node(node) + + # Register visibility filter + for node in _input_properties.get_subjects(): + NetworkSynchronizationServer.register_visibility_filter(node, visibility_filter) + +## Process settings based on authority. +## [br][br] +## Call this whenever the authority of input node changes. +## Make sure to do this at the same time on all peers. +func process_authority(): + InputSenderServer._deregister_input_sender(self) + + for node in _input_properties.get_subjects(): + for property in _input_properties.get_properties_of(node): + NetworkHistoryServer.deregister_input_sender(node, property) + NetworkSynchronizationServer.deregister_input_sender(node, property) + + # Process authority + _input_properties.set_from_paths(root, input_properties) + + # Register new recorded inputs + for node in _input_properties.get_subjects(): + for property in _input_properties.get_properties_of(node): + NetworkHistoryServer.register_input_sender(node, property) + NetworkSynchronizationServer.register_input_sender(node, property) + + InputSenderServer._register_input_sender(self) + +## Add an input property. +## [br][br] +## Settings will be automatically updated. The [param node] may be a string or +## [NodePath] pointing to a node, or an actual [Node] instance. If the given +## property is already tracked, this method does nothing. +func add_input(node: Variant, property: String) -> void: + var property_path := PropertyEntry.make_path(root, node, property) + if not property_path or input_properties.has(property_path): + return + + input_properties.push_back(property_path) + _properties_dirty = true + _reprocess_settings.call_deferred() + +## Helper function to determine if [InputSender] has authority over its input_properties +## This function iterates over input_properties subjects and checks if they have authority. +## If none of them has authority or no input_node is configured this will return false, +## If any of them has authority this will return true instantly. +## Its developers responsibility to always make sure input_nodes have same configuration. +## TODO make sure to document this responsibility to developer. +func has_authority_over_input_nodes() -> bool: + for subject in _input_properties.get_subjects(): + + # ObjectPool does not guarentee every subject is node. + if not subject is Node: + continue + + # Found input node, check if it has authority + if subject.is_multiplayer_authority(): + return true + + # Did not find any node, or none of them has authority. + return false + +## Get latest input data available for this [InputSender]. +## Used by [Simulator] node internally. +func get_latest_received_information_tick(current_tick : int) -> int: + return NetworkHistoryServer.get_latest_input_sender_for( + _input_properties.get_subjects(), + current_tick) + +func _notification(what: int) -> void: + if what == NOTIFICATION_EDITOR_PRE_SAVE: + update_configuration_warnings() + elif what == NOTIFICATION_PREDELETE: + for node in _input_properties.get_subjects(): + NetworkSynchronizationServer.deregister(node) + NetworkIdentityServer.deregister_node(node) + NetworkHistoryServer.deregister(node) + +func _get_configuration_warnings() -> PackedStringArray: + if not root: + root = get_parent() + + # Check if root exists. + if not root: + return ["No valid root node found!"] + + var result := PackedStringArray() + + result.append_array(_NetfoxEditorUtils.gather_properties(root, "_get_input_sender_input_properties", + func(node, prop): + add_input(node, prop) + )) + + if _input_properties.is_empty() and input_properties.is_empty(): + return ["Input properties are not configured!"] + + return result + +func _reprocess_settings() -> void: + if not _properties_dirty or Engine.is_editor_hint(): + return + + _properties_dirty = false + process_settings() + +# Helper function to apply given snapshot for only this node. +# TODO Applying whole snapshot and iterating over ticks would be nicer +# if we decide to have singleton for this +func _apply_snapshot_for_self(snapshot : _Snapshot) -> void: + _logger.trace("Applying snapshot for self :%s", [snapshot]) + for subject in _input_properties.get_subjects(): + for property in _input_properties.get_properties_of(subject): + + if snapshot.has_property(subject, property): + var value := snapshot.get_property(subject, property) + # TODO is this should be node.set_indexed ?? + subject.set_indexed(property, value) + +## Predicts inputs for given tick. +func predict_inputs() -> void: + pass + +# Helper function to save current input_properties. +# Used internally by InputSenderServer/SimulatorServer to record state before +# overwriting properties and emitting signals. +func _save_properties() -> void: + _saved_inputs_snapshot = _PropertySnapshot.extract(_property_entries) + +# Helper function to restore input_properties. +# Used internally by InputSenderServer/SimulatorServer to restore state after +# overwriting properties. +func _restore_properties() -> void: + _saved_inputs_snapshot.apply(_property_cache) diff --git a/addons/netfox/netfox.gd b/addons/netfox/netfox.gd index e7592fba2..cc5590ae8 100644 --- a/addons/netfox/netfox.gd +++ b/addons/netfox/netfox.gd @@ -169,7 +169,51 @@ var SETTINGS: Array[Dictionary] = [ "name": "netfox/events/enabled", "value": true, "type": TYPE_BOOL - } + }, + # Input Sender + { + "name": "netfox/input_sender/input_redundancy", + "value": 3, + "type" : TYPE_INT + }, + { + "name": "netfox/input_sender/history_limit", + "value": 64, + "type" : TYPE_INT + }, + { + "name": "netfox/input_sender/missing_input_history", + "value": 16, + "type" : TYPE_INT + }, + { + "name": "netfox/input_sender/enable_input_broadcast", + "value": false, + "type" : TYPE_BOOL + }, + # Simulator + { + "name": "netfox/simulator/full_state_interval", + "value": 24, + "type": TYPE_INT, + "hint": PROPERTY_HINT_RANGE, + "hint_string": "0,60,or_greater" + }, + { + "name": "netfox/simulator/enable_diff_states", + "value": true, + "type": TYPE_BOOL + }, + { + "name": "netfox/simulator/history_limit", + "value": 64, + "type" : TYPE_INT + }, + { + "name": "netfox/simulator/host_delay_ticks", + "value": 8, + "type" : TYPE_INT + }, ] const AUTOLOADS: Array[Dictionary] = [ @@ -220,7 +264,15 @@ const AUTOLOADS: Array[Dictionary] = [ { "name": "InterpolationServer", "path": ROOT + "/servers/interpolation-server.gd" - } + }, + { + "name": "InputSenderServer", + "path": ROOT + "/servers/input-sender-server.gd" + }, + { + "name": "SimulatorServer", + "path": ROOT + "/servers/simulator-server.gd" + }, ] const TYPES: Array[Dictionary] = [ @@ -254,6 +306,18 @@ const TYPES: Array[Dictionary] = [ "script": ROOT + "/rollback/predictive-synchronizer.gd", "icon": ROOT + "/icons/predictive-synchronizer.svg" }, + { + "name": "InputSender", + "base": "Node", + "script": ROOT + "/input-sender.gd", + "icon": ROOT + "/icons/input-sender.svg" + }, + { + "name": "Simulator", + "base": "Node", + "script": ROOT + "/simulator.gd", + "icon": ROOT + "/icons/simulator.svg" + }, ] func _enter_tree(): diff --git a/addons/netfox/network-time.gd b/addons/netfox/network-time.gd index 6f5a7df67..e46d93722 100644 --- a/addons/netfox/network-time.gd +++ b/addons/netfox/network-time.gd @@ -573,6 +573,9 @@ func _loop() -> void: # Record data for rollback NetworkRollback._after_tick(tick) + # Simulate, record, synchronize. + SimulatorServer._after_tick(tick) + # Record data for StateSynchronizer NetworkHistoryServer._record_sync_state(tick + 1) NetworkSynchronizationServer._synchronize_sync_state(tick + 1) @@ -609,6 +612,10 @@ func _after_tick_loop() -> void: # Run rollback loop NetworkRollback._rollback() + # Restore state for Simulator before emitting after_tick_loop so that + # listeners can run logic after state is set. + NetworkHistoryServer._restore_simulator(tick) + # Emit signal after_tick_loop.emit() diff --git a/addons/netfox/rollback/rollback-synchronizer.gd b/addons/netfox/rollback/rollback-synchronizer.gd index df2c81196..956042fc2 100644 --- a/addons/netfox/rollback/rollback-synchronizer.gd +++ b/addons/netfox/rollback/rollback-synchronizer.gd @@ -86,9 +86,9 @@ func process_settings() -> void: for node in _sim_nodes + _state_properties.get_subjects() + _input_properties.get_subjects(): RollbackSimulationServer.deregister_node(node) _sim_nodes.clear() - process_authority() + # Register nodes for simulation and liveness var managed_nodes := [root] + _collect_managed_nodes(root) _logger.debug("Filtering managed nodes: %s", [managed_nodes]) diff --git a/addons/netfox/servers/input-sender-server.gd b/addons/netfox/servers/input-sender-server.gd new file mode 100644 index 000000000..4e23499c8 --- /dev/null +++ b/addons/netfox/servers/input-sender-server.gd @@ -0,0 +1,281 @@ +extends Node +class_name _InputSenderServer + +# @public class + +## Handles [InputSender] related operations. + +## InputSenderServer assumes input snapshots arrive as whole. (atomic), if snapshot +## arrives with multiple parts, [InputSender] signals wont be reliable to +## code game logic. + +static var _logger := NetfoxLogger._for_netfox("InputSenderServer") + +var _history_server : NetworkHistoryServer = null +var _synchronization_server : NetworkSynchronizationServer = null + +var _missing_inputs_history_size : int = ProjectSettings.get_setting("netfox/input_sender/missing_input_history", 16) +var _input_sender_history_size : int = ProjectSettings.get_setting("netfox/input_sender/history_limit", 64) + +# Ticks that we received new input snapshots. +var _ticks_that_has_new_snapshot : Array[int] = [] + +# Maps InputSender -> InputSenderHistory (inner class, look at the end of this script.) +var _per_input_sender_history : Dictionary = {} + +func _ready(): + # Ensure dependencies + if not _history_server: _history_server = NetworkHistoryServer + if not _synchronization_server: _synchronization_server = NetworkSynchronizationServer + + # Record inputs similiar to rollback so that users can use same methods + # on their input scripts. + # For good reasons, We do logic and run InputSender signals on NetworkTime.on_tick + # This means local players wont have input information for their first tick. + # Its not really like await processframe but this will only add slight delay that wont have + # Important effects on the game itself. + NetworkTime.after_tick.connect(func(_dt, tick): + _history_server._record_input_sender(tick) + _synchronization_server._synchronize_input_sender(tick) + ) + + _synchronization_server._on_input_sender.connect(_on_received_input_snapshot) + + # About when we should process our inputs: + # It doesnt make sense to process them when we receive them, as it will break the + # work order of [Simulator]s or [TickInterpolator]s, or we might want this to work + # with physics. It would be unreliable. + # Therefore we need to process them in a fixed point. + # Processing them on every tick doesnt make sense, since host may run multiple + # ticks and we probably wont even have new inputs to process anyway. + # In conculusion it makes sense to process them after a tick loop run. + # + # But if we run logic on after_tick_loop, our effect on the game state is lost. + # Because most setups tends to use state-syncronizer with input-sender. + # state-syncronzier records on every tick and restores after tick loop. + # + # To make sure our effect isnt lost to this order, we run logic on tick. + NetworkTime.on_tick.connect(_on_tick) + +# Whenever we receive input snapshot, we investigate and fetch _earliest_input tick +# This is later used to iterate over saved snapshots and emit signals. +func _on_received_input_snapshot(snapshot : _Snapshot) -> void: + if snapshot.is_empty(): + return + + if not _ticks_that_has_new_snapshot.has(snapshot.tick): + _ticks_that_has_new_snapshot.push_back(snapshot.tick) + +# After a tick has been run +# Handle new snapshots +# Handle missing inputs. +# Handle local inputs. +# +# Since we are modifying InputSenders before emitting their signals, +# Their input properties will be messed up, this means users inputs wont be +# recorded properly on NetworkTime.after_tick. +# To prevent that, this function records their current state and restores at the +# end of this function. +func _on_tick(_delta : float, tick : int) -> void: + # First save input-sender states. + _save_input_sender_states() + + _handle_new_snapshots(tick) + + # We need to emit missing_input signal for old ticks. + var missing_tick := tick - _missing_inputs_history_size + if missing_tick >= 0: + _handle_missing_tick(missing_tick) + + # DANGER + # We do tick -1 for current local inputs, this is explained on _ready comments. + _handle_local_inputs(tick - 1) + + # Logic is done, restore input-sender states. + _restore_input_sender_states() + + # Now remove older history data that we keep. + _trim_input_sender_histories(tick) + +# Handles new input-sender snapshots. +# This function iterates over _ticks_that_has_new_snapshot. +# If tick is within range of missing-input-history, +# applies/emits network_input. +# If its not within range, +# it applies/emits late_input. +# Clears _ticks_that_has_new_snapshot after done. +func _handle_new_snapshots(current_tick : int) -> void: + if _ticks_that_has_new_snapshot.is_empty(): + return + + # So ticks are processed in ascending order. + _ticks_that_has_new_snapshot.sort() + + + for i in _ticks_that_has_new_snapshot: + + # This line saves processing because we fetch snapshot once per tick. + var snapshot := _history_server._get_input_sender_snapshot(i) + if not snapshot: + # This situation shouldnt happen anyway. + continue + + # Iterate over input senders and check if there is new input in tick + for input_sender in _per_input_sender_history.keys(): + if not _is_there_new_input_for_input_sender(input_sender, i, snapshot): + # No new input, we already processed this one. + continue + + # Did not received before + # If tick i past the missing_history_size, consider it late. + input_sender._apply_snapshot_for_self(snapshot) + if i <= current_tick - _missing_inputs_history_size: + # Its a late input. + input_sender.late_input.emit(i) + else: + # Its within range, consider it as network input. + input_sender.network_input.emit(i) + + # Set as handled anyway for both situation. + _set_input_received_for_input_sender(input_sender, i) + + _ticks_that_has_new_snapshot.clear() + +# Iterates over snapshot for given tick +# Checks _per_input_sender_history for given tick. +# If input_sender did not received anything for this tick +# It assumes input is missing and emits input_missing. +# Call this function after calling _handle_new_snapshots. +# Param for tick must be the value of current_tick - missing_input_history_size. +func _handle_missing_tick(for_tick : int) -> void: + + for input_sender in _per_input_sender_history.keys(): + var input_sender_history := _per_input_sender_history[input_sender] as InputSenderHistory + + if not input_sender_history.did_receive_for_tick(for_tick): + # We didnt receive anything for this input sender for this tick. + # Try to find latest input and emit missing_input. + + var latest_tick := _history_server.get_latest_input_sender_for( + input_sender._input_properties.get_subjects(), for_tick) + + if latest_tick >= 0: + var snapshot := _history_server._get_input_sender_snapshot(latest_tick) + if snapshot: + input_sender._apply_snapshot_for_self(snapshot) + else: + # If no snapshot, we need to emit signal with -1 + latest_tick = -1 + + # We may be able to find snapshot or not, emit anyway + input_sender.missing_input.emit(for_tick, latest_tick) + +# Iterates over input-senders and emits local input with latest recorded input available. +# Only does so if input-sender is authoritative peer. + +# Duplicated comment from _ready: +# +# "For good reasons, We do logic and run InputSender signals on NetworkTime.on_tick +# This means local players wont have input information for their first tick. +# Its not really like await processframe but this will only add slight delay that wont have +# important effects on the game itself." +func _handle_local_inputs(for_tick : int) -> void: + + # Fetch snapshot once before loop to prevent unneccessary fetches. + var snapshot := _history_server._get_input_sender_snapshot(for_tick) + if not snapshot: + # This shouldnt happen anyway. + return + + for input_sender in _per_input_sender_history.keys(): + if input_sender.has_authority_over_input_nodes(): + # Its authoritative player. + + # We know that snapshots are recorded for each tick on NetworkTime.after_tick. + # We can use it, no need to query get_latest_for here. + input_sender._apply_snapshot_for_self(snapshot) + input_sender.local_input.emit(for_tick) + +# Saves all input-sender input properties. +# This is done before messing with properties. +func _save_input_sender_states() -> void: + for input_sender in _per_input_sender_history.keys(): + input_sender._save_properties() + +# Restores all input-sender input properties. +# This is done after messing with properties. +func _restore_input_sender_states() -> void: + for input_sender in _per_input_sender_history.keys(): + input_sender._restore_properties() + +func _register_input_sender(input_sender : InputSender) -> void: + _per_input_sender_history[input_sender] = InputSenderHistory.new() + +func _deregister_input_sender(input_sender : InputSender) -> void: + _per_input_sender_history.erase(input_sender) + +# Helper that sets input received for param input sender on given tick. +func _set_input_received_for_input_sender(input_sender : InputSender, tick : int) -> void: + var history : InputSenderHistory = _per_input_sender_history[input_sender] as InputSenderHistory + if history: + history.set_as_received_for_tick(tick) + +## Check if there is a new input available for given input sender on given tick within matching snapshot. +func _is_there_new_input_for_input_sender(input_sender : InputSender, tick : int, snapshot : _Snapshot) -> bool: + var history : InputSenderHistory = _per_input_sender_history[input_sender] as InputSenderHistory + var received_before := history.did_receive_for_tick(tick) + + if received_before: + return false + + # We need to check if snapshot has this input senders properties + if snapshot: + for property_entry in input_sender._property_entries: + if snapshot.has_property(property_entry.node, property_entry.property): + # There is new information available indeed + return true + + # No new information + return false + +# Erases old input sender history data that we dont need anymore. +func _trim_input_sender_histories(current_tick : int) -> void: + # Since we cant merge older history than input-sender-history + # we can erase data older than that. + + var erase_ticks_older_than_inclusive : int = current_tick - _input_sender_history_size + + if erase_ticks_older_than_inclusive > 0: + for input_sender_history in _per_input_sender_history.values(): + input_sender_history.erase_old_ticks(erase_ticks_older_than_inclusive) + +func _init(p_history_server: _NetworkHistoryServer = null, p_synchronization_server: _NetworkSynchronizationServer = null): + _history_server = p_history_server + _synchronization_server = p_synchronization_server + +## Inner class to remember and compare +## Did we receive any input before for given tick? +## stored for specific InputSender. +class InputSenderHistory extends RefCounted: + + # Maps ticks to bool (we received input before = true, never received = false) + var _history : Dictionary = {} + + ## Set param tick as received + func set_as_received_for_tick(tick : int) -> void: + _history[tick] = true + + ## Check if we received input for given tick. + ## Returns false if we dont have information for that tick. + func did_receive_for_tick(tick : int) -> bool: + return _history.get(tick, false) + + ## Erase old ticks that we dont need anymore. + func erase_old_ticks(older_than_inclusive : int) -> void: + var to_erase : Array[int] = [] + for tick in _history.keys(): + if tick <= older_than_inclusive: + to_erase.push_back(tick) + + for erase_tick in to_erase: + _history.erase(erase_tick) diff --git a/addons/netfox/servers/network-history-server.gd b/addons/netfox/servers/network-history-server.gd index 6d511f022..05a2edca3 100644 --- a/addons/netfox/servers/network-history-server.gd +++ b/addons/netfox/servers/network-history-server.gd @@ -5,19 +5,25 @@ class_name _NetworkHistoryServer ## Tracks the history of objects' properties ## -## Specifically, history is stored for rollback state properties, rollback input -## properties, and synchronized state properties. +## History is stored for [br] +## 1- rollback state and inputs, +## 2- syncronized states, +## 3- input_sender inputs, +## 4- simulator states. ## [br][br] -## Keeping history lets rollback restore earlier game states for resimulation, -## and enables [_NetworkSynchronizationServer] to send diff states by comparing -## against historical data. +## Keeping history is needed for rewind operations and it also enables comparing, +## sending diff states. var _rb_input_properties := _PropertyPool.new() var _rb_state_properties := _PropertyPool.new() var _sync_state_properties := _PropertyPool.new() +var _input_sender_properties := _PropertyPool.new() +var _simulator_properties := _PropertyPool.new() var _rb_history_size := NetworkRollback.history_limit var _sync_history_size := ProjectSettings.get_setting("netfox/state_synchronizer/history_limit", 64) as int +var _input_sender_history_size := ProjectSettings.get_setting("netfox/input_sender/history_limit", 64) as int +var _simulator_history_size := ProjectSettings.get_setting("netfox/simulator/history_limit", 64) as int var _ignored_subjects := _Set.new() @@ -25,11 +31,15 @@ var _ignored_subjects := _Set.new() var _rb_input_history := _PerObjectHistory.new(_rb_history_size) var _rb_state_history := _PerObjectHistory.new(_rb_history_size) var _sync_history := _PerObjectHistory.new(_sync_history_size) +var _input_sender_history := _PerObjectHistory.new(_input_sender_history_size) +var _simulator_history := _PerObjectHistory.new(_simulator_history_size) # Cached snapshots for syncing var _rb_input_snapshots := _HistoryBuffer.new(_rb_history_size) var _rb_state_snapshots := _HistoryBuffer.new(_rb_history_size) var _sync_state_snapshots := _HistoryBuffer.new(_sync_history_size) +var _input_sender_snapshots := _HistoryBuffer.new(_input_sender_history_size) +var _simulator_snapshots := _HistoryBuffer.new(_simulator_history_size) static var _logger := NetfoxLogger._for_netfox("NetworkHistoryServer") @@ -57,6 +67,22 @@ func register_sync_state(node: Node, property: NodePath) -> void: func deregister_sync_state(node: Node, property: NodePath) -> void: _sync_state_properties.erase(node, property) +## Register a input_sender input property +func register_input_sender(node: Node, property: NodePath) -> void: + _input_sender_properties.add(node, property) + +## Deregister a input_sender input propert +func deregister_input_sender(node: Node, property: NodePath) -> void: + _input_sender_properties.erase(node, property) + +## Register a simulator property +func register_simulator(node: Node, property: NodePath) -> void: + _simulator_properties.add(node, property) + +## Deregister a simulator property +func deregister_simulator(node: Node, property: NodePath) -> void: + _simulator_properties.erase(node, property) + ## Deregister a node, no longer tracking any property it had registered using ## any of the [code]register_*()[/code] methods func deregister(node: Node) -> void: @@ -64,14 +90,20 @@ func deregister(node: Node) -> void: _rb_state_properties.erase_subject(node) _rb_input_properties.erase_subject(node) _sync_state_properties.erase_subject(node) + _input_sender_properties.erase_subject(node) + _simulator_properties.erase_subject(node) # Erase from per-object history _rb_state_history.erase_subject(node) _rb_input_history.erase_subject(node) _sync_history.erase_subject(node) + _input_sender_history.erase_subject(node) + _simulator_history.erase_subject(node) # Erase from per-tick history - for history in [_rb_state_snapshots, _rb_input_snapshots, _sync_state_snapshots]: + for history in [_rb_state_snapshots, _rb_input_snapshots,\ + _sync_state_snapshots, _input_sender_snapshots, _simulator_snapshots]: + for value in history.values(): var snapshot := value as _Snapshot snapshot.erase_subject(node) @@ -111,6 +143,48 @@ func get_state_age_for(subjects: Array, tick: int) -> int: func get_latest_input_for(subjects: Array, tick: int) -> int: return _get_latest_for(subjects, tick, _rb_input_history) +## Get the latest tick where any of the [param subjects] had input_sender data +## available +func get_latest_input_sender_for(subjects: Array, tick: int) -> int: + return _get_latest_for(subjects, tick, _input_sender_history) + +## Get the latest tick where any of the [param subjects] had simulator data +## available +func get_latest_simulator_for(subjects: Array, tick: int) -> int: + return _get_latest_for(subjects, tick, _simulator_history) + +## Get the latest tick where any of the [param subjects] had simulator snapshot available. +## Ignores tick where we dont have full snapshot. (missing properties) +func get_latest_simulator_for_snapshot(subjects: Array, tick: int) -> int: + + var full_subject_ticks : Array[int] = [] + + for index in range(_simulator_snapshots.get_earliest_index(), _simulator_snapshots.get_latest_index() + 1): + var snapshot := _simulator_snapshots.get_at(index) as _Snapshot + + if not snapshot: + continue + + var not_has_any_subject : bool = false + + for subject in subjects: + if not snapshot.has_subject(subject): + not_has_any_subject = true + break + + if not not_has_any_subject: + # We have all subjects in this snapshot. + full_subject_ticks.push_back(snapshot.tick) + + + var latest_tick : int = -1 + if full_subject_ticks.size() > 0: + var latest_in_arr = full_subject_ticks.max() + if latest_in_arr: + latest_tick = latest_in_arr + + return latest_tick + ## Return how old is the latest rollback input data for any of the ## [param subjects], in ticks func get_input_age_for(subjects: Array, tick: int) -> int: @@ -161,6 +235,48 @@ func _record_sync_state(tick: int) -> void: return subject.is_multiplayer_authority() ) +func _record_input_sender(tick: int) -> void: + _record(tick, _input_sender_history, _input_sender_snapshots, _input_sender_properties,\ + true, func(subject: Node): + return subject.is_multiplayer_authority() + ) + +func _record_simulator(tick: int) -> void: + _record(tick, _simulator_history, _simulator_snapshots, _simulator_properties, true, func(subject: Node): + return subject.is_multiplayer_authority() + ) + +# Records given simulator for given tick even though it doesnt have authority. +# This function is used by SimulatorServer to record simulators that we own +# input but not state. +# As a design perspective, a game should have max 1-2 simulators like this. +# So this is not really expensive and not usually used on servers. +# No auth history is overriden. +# No snapshot is written. +func _record_individual_simulator(simulator : Simulator, tick: int) -> void: + var property_pool := _PropertyPool.new() + property_pool.set_from_paths(simulator.root, simulator.state_properties) + + for subject in property_pool.get_subjects(): + assert(subject is Node, "Only nodes supported for now!") + + var is_auth := subject.is_multiplayer_authority() as bool + + if _simulator_history.is_auth(tick, subject): + continue + + var subject_snapshot := _simulator_history.ensure_snapshot(tick, subject, true) + if subject_snapshot == null: + _logger.warning("Dropping recorded tick @%d for subject %s as out-of-bounds", [tick, subject]) + continue + + assert(not property_pool.get_properties_of(subject).is_empty(), "Subject present in property pool without properties! Please report a bug!") + for property in property_pool.get_properties_of(subject): + subject_snapshot.record_property(property) + subject_snapshot.set_auth(is_auth) + + _logger.trace("Recorded simulator state @%d: %s", [tick, subject_snapshot]) + func _restore_rollback_input(tick: int) -> bool: return _restore_latest(tick, _rb_input_history) @@ -170,6 +286,15 @@ func _restore_rollback_state(tick: int) -> bool: func _restore_synchronizer_state(tick: int) -> bool: return _restore_latest(tick, _sync_history) +func _restore_input_sender(tick: int) -> bool: + return _restore_latest(tick, _input_sender_history) + +# Restores non authoritative simulators to known last state. +func _restore_simulator(tick: int) -> bool: + return _restore_latest(tick, _simulator_history, func(subject: Node) -> bool: + return not subject.is_multiplayer_authority() +) + func _get_rollback_input_snapshot(tick: int) -> _Snapshot: return _rb_input_snapshots.get_at(tick) @@ -179,6 +304,12 @@ func _get_rollback_state_snapshot(tick: int) -> _Snapshot: func _get_synchronizer_state_snapshot(tick: int) -> _Snapshot: return _sync_state_snapshots.get_at(tick) +func _get_input_sender_snapshot(tick: int) -> _Snapshot: + return _input_sender_snapshots.get_at(tick) + +func _get_simulator_snapshot(tick: int) -> _Snapshot: + return _simulator_snapshots.get_at(tick) + func _merge_rollback_input(snapshot: _Snapshot) -> bool: _merge_snapshot(snapshot, _rb_input_snapshots, true) return _merge_history(snapshot, _rb_input_history, true) @@ -191,6 +322,14 @@ func _merge_synchronizer_state(snapshot: _Snapshot) -> bool: _merge_snapshot(snapshot, _sync_state_snapshots, true) return _merge_history(snapshot, _sync_history) +func _merge_input_sender(snapshot: _Snapshot) -> bool: + _merge_snapshot(snapshot, _input_sender_snapshots, true) + return _merge_history(snapshot, _input_sender_history, true) + +func _merge_simulator(snapshot: _Snapshot) -> bool: + _merge_snapshot(snapshot, _simulator_snapshots, true) + return _merge_history(snapshot, _simulator_history, true) + func _record(tick: int, history: _PerObjectHistory, snapshots: _HistoryBuffer, property_pool: _PropertyPool, only_auth: bool, auth_filter: Callable) -> void: var snapshot := snapshots.get_at(tick, _Snapshot.new(tick)) as _Snapshot if not snapshots.has_at(tick): @@ -235,10 +374,19 @@ func _record(tick: int, history: _PerObjectHistory, snapshots: _HistoryBuffer, p _rb_state_history: _logger.trace("Recorded state @%d: %s", [tick, snapshot]) -func _restore_latest(tick: int, history: _PerObjectHistory) -> bool: +# Restore latest for given history. Optional filter can be used to filter subjects. +# Return false from filter function to not restore. +# Return true from filter function to restore. +# func(subject: Node) -> bool: +# return not subject.is_multiplayer_authority() +func _restore_latest(tick: int, history: _PerObjectHistory, filter: Callable = Callable()) -> bool: var any_applied := false for subject in history.subjects(): + # Optional filter. example: to only restore subjects we dont have authority over. + if filter.is_valid() and not filter.call(subject): + continue + # Grab latest snapshot up to tick var snapshot := history.get_latest_snapshot(tick, subject) @@ -248,6 +396,7 @@ func _restore_latest(tick: int, history: _PerObjectHistory) -> bool: any_applied = true match history: + _simulator_history: _logger.trace("Restored simulation state @%d: %s", [tick, snapshot]) _rb_input_history: _logger.trace("Restored input @%d: %s", [tick, snapshot]) _rb_state_history: _logger.trace("Restored state @%d: %s", [tick, snapshot]) diff --git a/addons/netfox/servers/network-synchronization-server.gd b/addons/netfox/servers/network-synchronization-server.gd index 430b2ac95..981aee5da 100644 --- a/addons/netfox/servers/network-synchronization-server.gd +++ b/addons/netfox/servers/network-synchronization-server.gd @@ -5,10 +5,10 @@ class_name _NetworkSynchronizationServer ## Synchronizes properties over the network ## -## Handles synchronization of rollback and state properties ( -## [RollbackSynchronizer] and [StateSynchronizer] ), while respecting visibility +## Handles synchronization of states and inputs while respecting visibility ## filters and schemas for serialization. ## [br][br] +## [RollbackSynchronizer], [StateSynchronizer], [InputSender], [Simulator] uses this class internally. ## Packets are sent per tick, instead of per object. So for every simulated ## rollback tick, a packet is sent with states, and for every recorded input, ## a packet is sent with the inputs. @@ -29,6 +29,10 @@ var _rb_owned_input_properties := _PropertyPool.new() var _rb_owned_state_properties := _PropertyPool.new() var _sync_state_properties := _PropertyPool.new() var _sync_owned_state_properties := _PropertyPool.new() +var _input_sender_properties := _PropertyPool.new() +var _input_sender_owned_properties := _PropertyPool.new() +var _simulator_properties := _PropertyPool.new() +var _simulator_owned_properties := _PropertyPool.new() var _visibility_filters := {} # Node to PeerVisibilityFilter @@ -36,8 +40,7 @@ var _rb_enable_input_broadcast := ProjectSettings.get_setting("netfox/rollback/e var _rb_enable_diffs := NetworkRollback.enable_diff_states var _rb_full_interval := ProjectSettings.get_setting("netfox/rollback/full_state_interval", 24) as int var _rb_full_scheduler := _IntervalScheduler.new(_rb_full_interval) - -var _input_redundancy := NetworkRollback.input_redundancy +var _rb_input_redundancy := NetworkRollback.input_redundancy var _last_sync_state_sent := _Snapshot.new(0) var _sync_enable_diffs := ProjectSettings.get_setting("netfox/state_synchronizer/enable_diff_states", true) as bool @@ -48,6 +51,14 @@ var _sync_full_scheduler := _IntervalScheduler.new(_sync_full_interval) # https://stackoverflow.com/a/35697810 var _max_packet_size := ProjectSettings.get_setting("netfox/general/max_sync_packet_size", 508) as int +var _input_sender_enable_broadcast := ProjectSettings.get_setting("netfox/input_sender/enable_input_broadcast", false) as bool +var _input_sender_redundancy := ProjectSettings.get_setting("netfox/input_sender/input_redundancy", 3) as int + +var _last_simulator_state_sent := _Snapshot.new(0) +var _simulator_enable_diffs := ProjectSettings.get_setting("netfox/simulator/enable_diff_states", true) as bool +var _simulator_full_interval := ProjectSettings.get_setting("netfox/simulator/full_state_interval", 24) as int +var _simulator_full_scheduler := _IntervalScheduler.new(_simulator_full_interval) + var _schemas := _NetworkSchema.new() var _dense_serializer: _DenseSnapshotSerializer @@ -58,6 +69,11 @@ var _cmd_full_state: NetworkCommandServer.Command var _cmd_diff_state: NetworkCommandServer.Command var _cmd_input: NetworkCommandServer.Command +var _cmd_input_sender: NetworkCommandServer.Command + +var _cmd_full_simulator: NetworkCommandServer.Command +var _cmd_diff_simulator: NetworkCommandServer.Command + var _cmd_full_sync: NetworkCommandServer.Command var _cmd_diff_sync: NetworkCommandServer.Command @@ -65,6 +81,8 @@ static var _logger := NetfoxLogger._for_netfox("NetworkSynchronizationServer") signal _on_input(snapshot: _Snapshot) signal _on_state(snapshot: _Snapshot) +signal _on_input_sender(snapshot : _Snapshot) +signal _on_simulator(snapshot: _Snapshot) ## Register a [param property] of [param node] to be synchronized ## as rollback state @@ -105,6 +123,32 @@ func deregister_sync_state(node: Node, property: NodePath) -> void: _sync_state_properties.erase(node, property) _sync_owned_state_properties.erase(node, property) +## Register a [param property] of [param node] to be synchronized +## as input_sender input +func register_input_sender(node: Node, property: NodePath) -> void: + _input_sender_properties.add(node, property) + if node.is_multiplayer_authority(): + _input_sender_owned_properties.add(node, property) + +## Deregister a [param property] of [param node] from being synchronized +## as input_sender input +func deregister_input_sender(node: Node, property: NodePath) -> void: + _input_sender_properties.erase(node, property) + _input_sender_owned_properties.erase(node, property) + +## Register a [param property] of [param node] to be syncronized +## as simulator state. +func register_simulator(node: Node, property: NodePath) -> void: + _simulator_properties.add(node, property) + if node.is_multiplayer_authority(): + _simulator_owned_properties.add(node, property) + +## Deregister a [param property] of [param node] from being syncronized +## as simulator state. +func deregister_simulator(node: Node, property: NodePath) -> void: + _simulator_properties.erase(node, property) + _simulator_owned_properties.erase(node, property) + ## Register a [param serializer] to use when transmitting ## [param property param] of [param node] over the network func register_schema(node: Node, property: NodePath, serializer: NetworkSchemaSerializer) -> void: @@ -136,6 +180,10 @@ func deregister(node: Node) -> void: _rb_owned_input_properties.erase_subject(node) _sync_state_properties.erase_subject(node) _sync_owned_state_properties.erase_subject(node) + _input_sender_properties.erase_subject(node) + _input_sender_owned_properties.erase_subject(node) + _simulator_properties.erase_subject(node) + _simulator_owned_properties.erase_subject(node) _visibility_filters.erase(node) _schemas.erase_subject(node) @@ -175,7 +223,7 @@ func _synchronize_input(tick: int) -> void: notified_peers.erase(multiplayer.get_unique_id()) # Prepare snapshot package - for offset in _input_redundancy: + for offset in _rb_input_redundancy: # Grab snapshot from NetworkHistoryServer var snapshot := NetworkHistoryServer._get_rollback_input_snapshot(tick - offset) if not snapshot: @@ -286,6 +334,75 @@ func _synchronize_sync_state(tick: int) -> void: # NOTE: This is a shared instance, theoretically shouldn't screw things up _last_sync_state_sent = snapshot +func _synchronize_input_sender(tick: int) -> void: + # We don't own inputs, nothing to synchronize + if _input_sender_owned_properties.is_empty(): + return + + var snapshots := [] as Array[_Snapshot] + var notified_peers := _Set.new() + + # By default input sender only sends input to host, check if its enabled. + if not _input_sender_enable_broadcast: + # Input broadcast is off, only send inputs to host. + for node in _input_sender_owned_properties.get_subjects(): + notified_peers.add(1) + else: + # If input broadcast is on, send inputs to everyone + for peer in multiplayer.get_peers(): + notified_peers.add(peer) + + # Make sure to not send input to ourselves + # Even if host is also a player, host should be able to reach stored inputs via + # recorded snapshots. + notified_peers.erase(multiplayer.get_unique_id()) + + # Prepare snapshot package + for offset in _input_sender_redundancy: + # Grab snapshot from NetworkHistoryServer + var snapshot := NetworkHistoryServer._get_input_sender_snapshot(tick - offset) + if not snapshot: + break + + _logger.trace("Submitting input_sender inputs: %s", [snapshot]) + snapshots.append(snapshot) + + _logger.trace("Submitting input_sender inputs to peers: %s", [notified_peers]) + for peer in notified_peers: + var data := _redundant_serializer.write_for(peer, snapshots, _input_sender_owned_properties) + _cmd_input_sender.send(data, peer) + +func _synchronize_simulator(tick: int) -> void: + # We don't own state, nothing to synchronize + if _simulator_owned_properties.is_empty(): + _logger.trace("No owned simulator property to synchronize, returning.") + return + + var snapshot := NetworkHistoryServer._get_simulator_snapshot(tick) + + if not snapshot: + # No data for tick + return + + if snapshot.is_empty(): + # Nothing to send + return + + # Figure out whether to send full- or diff state + var is_full := _simulator_full_scheduler.is_now() + if not _simulator_enable_diffs: + is_full = true + + if is_full: + # Send full states + for peer in multiplayer.get_peers(): + var filter := func(subject): return _is_node_visible_to(peer, subject) + + var packets := _dense_serializer.write_for(peer, snapshot, _simulator_owned_properties, filter) + for packet in packets: + _logger.trace("Submitting simulator full state:%s to peer:%s", [snapshot, peer]) + _cmd_full_simulator.send(packet, peer) + func _init( p_command_server: _NetworkCommandServer = null, p_history_server: _NetworkHistoryServer = null, @@ -317,7 +434,13 @@ func _ready(): _cmd_full_state = _command_server.register_command(_handle_full_state, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE) _cmd_diff_state = _command_server.register_command(_handle_diff_state, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE) _cmd_input = _command_server.register_command(_handle_input, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE) - + + _cmd_input_sender = _command_server.register_command(_handle_input_sender, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE) + + # TODO which one makes sense ordered or not ? + _cmd_full_simulator = _command_server.register_command(_handle_full_simulator, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE) + _cmd_diff_simulator = _command_server.register_command(_handle_diff_simulator, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE) + _cmd_full_sync = _command_server.register_command(_handle_full_sync, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE_ORDERED) _cmd_diff_sync = _command_server.register_command(_handle_diff_sync, MultiplayerPeer.TRANSFER_MODE_UNRELIABLE_ORDERED) @@ -371,6 +494,41 @@ func _handle_diff_sync(sender: int, data: PackedByteArray): NetworkHistoryServer._merge_synchronizer_state(snapshot) _logger.trace("Ingested sync diff: %s", [snapshot]) +func _handle_input_sender(sender : int, data : PackedByteArray) -> void: + var buffer := StreamPeerBuffer.new() + buffer.data_array = data + + var snapshots := _redundant_serializer.read_from(sender, _input_sender_properties, buffer, true) + + for snapshot in snapshots: + snapshot.sanitize(sender) + + _logger.trace("Ingesting input_sender inputs: %s", [snapshot]) + if NetworkHistoryServer._merge_input_sender(snapshot): + _on_input_sender.emit(snapshot) + +func _handle_full_simulator(sender : int, data : PackedByteArray) -> void: + var buffer := StreamPeerBuffer.new() + buffer.data_array = data + + var snapshot := _dense_serializer.read_from(sender, _simulator_properties, buffer, true) + snapshot.sanitize(sender) + + _logger.trace("Ingested simulator full state: %s", [snapshot]) + if NetworkHistoryServer._merge_simulator(snapshot): + _on_simulator.emit(snapshot) + +func _handle_diff_simulator(sender : int, data : PackedByteArray) -> void: + var buffer := StreamPeerBuffer.new() + buffer.data_array = data + + var snapshot := _sparse_serializer.read_from(sender, _simulator_properties, buffer) + snapshot.sanitize(sender) + + _logger.trace("Ingested simulator diff state: %s", [snapshot]) + if NetworkHistoryServer._merge_simulator(snapshot): + _on_simulator.emit(snapshot) + func _ingest_state(sender: int, snapshot: _Snapshot) -> void: snapshot.sanitize(sender) diff --git a/addons/netfox/servers/simulator-server.gd b/addons/netfox/servers/simulator-server.gd new file mode 100644 index 000000000..f6ee5c82d --- /dev/null +++ b/addons/netfox/servers/simulator-server.gd @@ -0,0 +1,477 @@ +extends Node +class_name _SimulatorServer + +# @public class + +## Handles [Simulator] related operations. + +## TODO: We should find a better name for Simulator word. +## +## [Before reading this server, please read InputSender, InputSenderServer, Simulator.] + +## Insight +## +## +## Depending on simulator authority there are 4 options we can operate on: +## +## 1- We have authority over both simulator input/state = host simulator +## 2- We have authority over input but not state = local authoritative simulator +## 3- We have authority over state but not input = host puppet +## 4- We dont have any authority over input/state = client puppet +## +## 1- host simulator: +## - Advance the simulation with the inputs tick - 1. +## (input-sender inputs are recorded on after tick). +## - Save the simulator state for current tick as invidiual fashion. +## - Broadcast it togather with 2. +## +## 2- local authoritative simulator: +## - We have inputs available locally up to tick -1. +## (input-sender inputs are recorded on after tick). +## - Apply the latest authoritative state received from host. (the truth) +## - Iterate over inputs and reach the current tick - 1. +## +## 3- host puppet. +## - If we dont have inputs buffered yet, dont run the simulation, simply skip. +## - If we have inputs buffered, run the simulation with indexed inputs for 1 simulation tick. +## - Record the simulator state for given simulation tick. +## - Broadcast them. +## +## 4- client puppet. +## - Apply the latest authoritative state received from host. (the truth) +## +## +## By restoring to latest state we already handle 4. +## We dont need to keep history of non-host simulators. + + +var _history_server : _NetworkHistoryServer = null +var _synchronization_server : _NetworkSynchronizationServer = null +var _logger := NetfoxLogger._for_netfox("SimulatorServer") + +# History size for simulation. +var _simulation_history_size : int = ProjectSettings.get_setting("netfox/simulator/history_limit", 64) + +# Host side buffering/delay tick count for simulation. +var _simulation_host_delay_ticks : int = ProjectSettings.get_setting("netfox/simulator/host_delay_ticks", 8) + +# Node to array of ticks +var _simulated_ticks := {} + +# Projectiles that are alive. +var _living_projectiles : Array[Node] = [] + +# Helper to record and restore simulation aware collision-shapes/areas. +var _simulated_collision_recorder : _SimulatedCollisionRecorder = _SimulatedCollisionRecorder.new() + +# Fresh registered projectiles. +# Mapped by tick -> array of projectiles (type of Node) that are estimated to be fired on mapped tick. +# See register_projectile method. +var _fresh_registered_projectiles_by_tick : Dictionary[int, Array] = {} + +# Grouped simulators depending on their authority modes. +# Better readability on code / we only check authority on register. +var _host_simulators : Array[Simulator] = [] +var _local_authoritative_simulators : Array[Simulator] = [] +var _host_puppet_simulators : Array[Simulator] = [] +var _client_puppet_simulators : Array[Simulator] = [] + +func _init(p_history_server: _NetworkHistoryServer = null, p_synchronization_server: _NetworkSynchronizationServer = null): + _history_server = p_history_server + _synchronization_server = p_synchronization_server + +func _ready(): + # Ensure dependencies + if not _history_server: _history_server = NetworkHistoryServer + if not _synchronization_server: _synchronization_server = NetworkSynchronizationServer + + _simulated_collision_recorder.initialize(get_tree()) + +# Register a simulator node. +# Will check for authority over inputs and categorize by it. +# Its Simulator's responsibility to only register if input-sender is configured. +func register_simulator(simulator : Simulator) -> void: + if simulator.is_multiplayer_authority(): + if simulator.listened_input_sender.has_authority_over_input_nodes(): + _host_simulators.push_back(simulator) + else: + _host_puppet_simulators.push_back(simulator) + else: + if simulator.listened_input_sender.has_authority_over_input_nodes(): + _local_authoritative_simulators.push_back(simulator) + else: + _client_puppet_simulators.push_back(simulator) + + +# Deregister a simulator node. +func deregister_simulator(simulator : Simulator) -> void: + _host_simulators.erase(simulator) + _host_puppet_simulators.erase(simulator) + _local_authoritative_simulators.erase(simulator) + _client_puppet_simulators.erase(simulator) + + _simulated_ticks.erase(simulator) + +## Register a projectile to be stepped by simulation. +## This method should only be called on host. +func register_projectile(projectile : Node, firing_peer_id : int, fired_tick : int) -> void: + + var has_step_method := projectile.has_method("step") + var has_is_alive_method := projectile.has_method("is_alive") + + if not has_step_method or not has_is_alive_method: + _logger.error( + "Error registering projectile %s : Projectiles must implement step and is_alive methods.", + [projectile.name] + ) + return + + ## Only allow Offline peer and ENet peer to register, because only Enet has rtt statistic functions. + ## Offline is for convenience. + var enet_peer := multiplayer.multiplayer_peer + if enet_peer is not ENetMultiplayerPeer and enet_peer is not OfflineMultiplayerPeer: + _logger.error( + "Error registering projectile %s : Multiplayer peer is either null or not type of ENet.\n" + + "Only ENetMultiplayerPeer is supported for now.", [projectile.name] + ) + return + + var firing_peer : ENetPacketPeer = null + + if enet_peer is ENetMultiplayerPeer: + firing_peer = enet_peer.get_peer(firing_peer_id) as ENetPacketPeer + + # Allow valid peers only with the exception being server id. + # OfflinePeer will return id 1 so should be fine. + if not firing_peer and firing_peer_id != 1: + _logger.error( + "Error registering projectile %s: Firing peer #%s is not found on active peers.", + [projectile.name, firing_peer_id] + ) + return + + var rtt_ms := 0.0 + var half_rtt_sec := 0.0 + var half_rtt_ticks := 0.0 + var estimated_firing_tick := fired_tick - _simulation_host_delay_ticks + + # If firing peer is valid get stats and estimate peers simulation tick. + if firing_peer: + rtt_ms = firing_peer.get_statistic(ENetPacketPeer.PEER_ROUND_TRIP_TIME) + half_rtt_sec = (rtt_ms * 0.5) / 1000.0 + half_rtt_ticks = half_rtt_sec / NetworkTime.ticktime + + # Dont forget that we synchronize current_tick - _simulation_host_delay_ticks + estimated_firing_tick = fired_tick - roundi(half_rtt_ticks) - _simulation_host_delay_ticks + + if estimated_firing_tick < 0: + _logger.error( + "Error registering projectile %s: Calculated estimated_firing_tick as negative. Not registering.\n" + + "half rtt: %s, fired tick: %s, estimated firing tick: %s", + [projectile.name, half_rtt_sec, fired_tick, estimated_firing_tick] + ) + return + + if estimated_firing_tick < NetworkTime.tick - _simulation_history_size: + _logger.error( + "Error registering projectile %s: Calculated estimated_firing_tick is older than history.\n" + + "Returning without registering. half rtt: %s, fired tick: %s, estimated firing tick: %s", + [projectile.name, half_rtt_sec, fired_tick, estimated_firing_tick] + ) + return + + _logger.debug( + "Registered projectile %s with stats:\n" + + "firing_peer: %s, fired tick: %s, half rtt: %s, estimated firing tick: %s", + [projectile.name, firing_peer_id, fired_tick, half_rtt_sec, estimated_firing_tick] + ) + + var existing_projectile_arr := _fresh_registered_projectiles_by_tick.get_or_add(estimated_firing_tick, []) as Array + existing_projectile_arr.push_back(projectile) + +func _after_tick(tick : int) -> void: + + _catch_up_fresh_projectiles(tick) + + _handle_host_simulators() + _handle_host_puppet_simulators() + _handle_local_authoritative_simulators() + + _step_living_projectiles(tick) + + if NetworkTime.tick - _simulation_host_delay_ticks >= 0: + # History server only records owned simulator state properties. + _history_server._record_simulator(NetworkTime.tick - _simulation_host_delay_ticks) + _synchronization_server._synchronize_simulator(NetworkTime.tick - _simulation_host_delay_ticks) + + _simulated_collision_recorder.record_tick(NetworkTime.tick - _simulation_host_delay_ticks) + + # Trim old history that we dont need. + var trim_tick := tick - _simulation_history_size + if trim_tick >= 0: + _trim_ticks_simulated(trim_tick) + _simulated_collision_recorder.trim_ticks(trim_tick) + +## 1- host simulator: +## - Advance the simulation with the inputs tick - 1. +## (input-sender inputs are recorded on after tick). +## - Save the simulator state for current tick. +## - Broadcast it togather with 2. +func _handle_host_simulators() -> void: + var current_tick := NetworkTime.tick + + for simulator in _host_simulators: + + # Save input properties before messing them up. + simulator.listened_input_sender._save_properties() + + # Retrieve the input history and apply manually. + var input_history := _history_server._input_sender_history + for subject in simulator.listened_input_sender._input_properties.get_subjects(): + input_history.ensure_snapshot(current_tick - 1, subject, true).apply() + + var is_fresh := _is_tick_fresh_for(simulator, current_tick) + simulator._run_simulation(NetworkTime.ticktime, current_tick, is_fresh) + _history_server._record_individual_simulator(simulator, current_tick) + _set_tick_simulated_for(simulator, current_tick) + + # Restore messed up properties. + simulator.listened_input_sender._restore_properties() + +## 2- local authoritative simulator: +## - We have inputs available locally up to tick -1. +## (input-sender inputs are recorded on after tick). +## - Apply the latest authoritative state received from host. (the truth) +## - Iterate over inputs and reach the current tick - 1. +func _handle_local_authoritative_simulators() -> void: + var current_tick := NetworkTime.tick + + for simulator in _local_authoritative_simulators: + + var latest_truth_tick := _history_server.get_latest_simulator_for_snapshot( + simulator._state_properties.get_subjects(), + current_tick) + + var latest_truth_snapshot := _history_server._get_simulator_snapshot(latest_truth_tick) + + # Save inputs before messing input properties. + simulator.listened_input_sender._save_properties() + + if latest_truth_tick < 0 or not latest_truth_snapshot: + _logger.warning( + "Couldnt find any truth from host.\n" + + "Running simulation for only current tick." + ) + + # Retrieve the input history and apply manually. + var input_history := _history_server._input_sender_history + for subject in simulator.listened_input_sender._input_properties.get_subjects(): + input_history.ensure_snapshot(current_tick - 1, subject, true).apply() + + var is_fresh := _is_tick_fresh_for(simulator, current_tick) + simulator._run_simulation(NetworkTime.ticktime, current_tick, is_fresh) + _history_server._record_individual_simulator(simulator, current_tick) + _set_tick_simulated_for(simulator, current_tick) + + # Restore messed up properties. + simulator.listened_input_sender._restore_properties() + continue + + simulator._apply_snapshot_for_self(latest_truth_snapshot) + # Retrieve the input history to apply it manually. + var input_history := _history_server._input_sender_history + for i in range(latest_truth_tick + 1, current_tick + 1): + + for subject in simulator.listened_input_sender._input_properties.get_subjects(): + var snapshot : _ObjectSnapshot = input_history.ensure_snapshot(i - 1, subject, false) + if snapshot: + snapshot.apply() + + var is_fresh := _is_tick_fresh_for(simulator, i) + simulator._run_simulation(NetworkTime.ticktime, i, is_fresh) + _history_server._record_individual_simulator(simulator, i) + _set_tick_simulated_for(simulator, i) + + # Restore messed up properties. + simulator.listened_input_sender._restore_properties() + +## 3- host puppet. +## - If we dont have inputs buffered yet, dont run the simulation, simply skip. +## - If we have inputs buffered, run the simulation with indexed inputs for 1 simulation tick. +## - Record the simulator state for given simulation tick. +## - Broadcast them. +func _handle_host_puppet_simulators() -> void: + var current_tick := NetworkTime.tick + + var simulated_tick := current_tick - _simulation_host_delay_ticks + + for simulator in _host_puppet_simulators: + + var latest_input_tick := _history_server.get_latest_input_sender_for( + simulator.listened_input_sender._input_properties.get_subjects(), + simulated_tick - 1 + ) + + var is_fresh := _is_tick_fresh_for(simulator, simulated_tick) + + if latest_input_tick == simulated_tick - 1: + # We have inputs for this tick, run the simulation. + var input_snapshot := _history_server._get_input_sender_snapshot(latest_input_tick) + if not input_snapshot: + _logger.error("No input snapshot found at latest input tick, this shouldnt happen.") + continue + + _logger.trace("Running simulation for %s", [simulator]) + + + simulator.listened_input_sender._apply_snapshot_for_self(input_snapshot) + simulator._run_simulation(NetworkTime.ticktime, simulated_tick, is_fresh) + + else: + # We need to predict this frame. + _logger.warning("No buffered input found, predicting inputs.") + + simulator.listened_input_sender.predict_inputs() + simulator._run_simulation(NetworkTime.ticktime, simulated_tick, is_fresh) + + _set_tick_simulated_for(simulator, simulated_tick) + +# Steps every already-caught-up living projectile by a single tick. +# No need to restore world state for living projectiles. +func _step_living_projectiles(current_tick: int) -> void: + var simulated_tick := current_tick - _simulation_host_delay_ticks + + # Iterate in reverse because we are also removing. + var i := _living_projectiles.size() - 1 + while i >= 0: + var projectile : Node = _living_projectiles[i] + + if not is_instance_valid(projectile): + _logger.warning("While stepping living projectiles: projectile at index %s no longer valid, dropping", [i]) + _living_projectiles.remove_at(i) + else: + _logger.trace("Stepping living projectile: %s at tick #%s", [projectile.name, simulated_tick]) + projectile.step(NetworkTime.ticktime, simulated_tick) + + if not projectile.is_alive(): + _logger.trace("After stepping living projectile: %s projectile died at tick %s", + [projectile.name, simulated_tick] + ) + + _living_projectiles.remove_at(i) + + i -= 1 + +# Steps the freshly registered projectiles up to current_tick - _simulation_host_delay_ticks (not included). +func _catch_up_fresh_projectiles(current_tick: int) -> void: + if _fresh_registered_projectiles_by_tick.is_empty(): + return + + var simulated_tick := current_tick - _simulation_host_delay_ticks + var oldest_tick : int = _fresh_registered_projectiles_by_tick.keys().min() + + # Ensure oldest tick is not older than our history window. + oldest_tick = maxi(oldest_tick, current_tick - _simulation_history_size) + + _logger.trace( + "Catching up fresh projectiles: oldest_tick=%s, simulated_tick=%s, pending_ticks=%s", + [oldest_tick, simulated_tick, _fresh_registered_projectiles_by_tick.keys()] + ) + + # Temp array to hold projectiles. + var fresh_projectiles : Array[Node] = [] + + # We need to capture current state of collision aware nodes. Because we will mess with their + # states during our operation. At the end of the operation we will restore to this state snapshot. + var current_collision_state_snapshot_dict := _simulated_collision_recorder.capture_current_state() + + for tick in range(oldest_tick, simulated_tick): + # Restore simulation aware collision nodes. + _simulated_collision_recorder.restore_tick(tick) + + # Get projectiles registered at this tick and add it our fresh_projectiles array. + var registered_projectiles_arr_at_tick := _fresh_registered_projectiles_by_tick.get(tick, []) as Array + if not registered_projectiles_arr_at_tick.is_empty(): + _logger.trace( + "Tick %s: %s registered projectiles are joining the projectile catch up phase: %s", + [tick, registered_projectiles_arr_at_tick.size(), + registered_projectiles_arr_at_tick.map( + func(p): return _get_projectile_debug_name(p)) + ] + ) + + fresh_projectiles.append_array(registered_projectiles_arr_at_tick) + _fresh_registered_projectiles_by_tick.erase(tick) + + # While iterating over ticks, step every fresh projectile we have. + var i := fresh_projectiles.size() - 1 + while i >= 0: + + var projectile : Node = fresh_projectiles[i] + + if is_instance_valid(projectile): + projectile.step(NetworkTime.ticktime, tick) + _logger.trace("Projectile %s running catch up step at tick: #%s", [projectile.name, tick]) + # Erase from our list if projectile is not alive anymore. + if not projectile.is_alive(): + _logger.trace("Projectile %s is not alive, erasing at tick: #%s", [projectile.name, tick]) + fresh_projectiles.remove_at(i) + else: + _logger.trace("During projectile catch up, projectile at index %s no longer valid, dropping", [i]) + fresh_projectiles.remove_at(i) + + i -= 1 + + # Anything registered for tick exact simulated_tick had no history to replay. + # They join living unstepped and gets its first step together with _step_living_projectiles. + var registered_at_simulated_tick := _fresh_registered_projectiles_by_tick.get(simulated_tick, []) as Array + if not registered_at_simulated_tick.is_empty(): + _logger.trace( + "At Simulated tick #%s, %s projectile(s) joining to living projectiles without catch up.\n" + + "They will be stepped together with living projectiles on this tick. Projectile names %s", + [simulated_tick, registered_at_simulated_tick.size(), + registered_at_simulated_tick.map( + func(p): return _get_projectile_debug_name(p)) + ] + ) + + fresh_projectiles.append_array(registered_at_simulated_tick) + _fresh_registered_projectiles_by_tick.erase(simulated_tick) + + if not fresh_projectiles.is_empty(): + _logger.trace( + "Projectile catch up is over: %s projectile(s) promoted to living: %s", + [fresh_projectiles.size(), fresh_projectiles.map( + func(p): return _get_projectile_debug_name(p)) + ] + ) + + # Append our living projectiles list, they are part of the current simulation at this point, + # and they will be handled with function _step_living_projectiles. + _living_projectiles.append_array(fresh_projectiles) + + # Since we changed states of simulation aware collision nodes by calling + # _simulated_collision_recorder.restore_tick, now we should restore them to current state which + # we recorded before doing this operation. + _simulated_collision_recorder.restore_snapshot(current_collision_state_snapshot_dict) + +func _is_tick_fresh_for(node: Node, tick: int) -> bool: + if not _simulated_ticks.has(node): + return true + var ticks := _simulated_ticks.get(node) as Array[int] + return not ticks.has(tick) + +func _set_tick_simulated_for(node: Node, tick: int) -> void: + if not _simulated_ticks.has(node): + _simulated_ticks[node] = [tick] as Array[int] + else: + _simulated_ticks[node].append(tick) + +func _trim_ticks_simulated(beginning: int) -> void: + for object in _simulated_ticks: + _simulated_ticks[object] = _simulated_ticks[object]\ + .filter(func(tick): return tick >= beginning) + +# Helper to get projectile debug name. +func _get_projectile_debug_name(p) -> String: + return p.name if is_instance_valid(p) else "" diff --git a/addons/netfox/simulated-collision-recorder.gd b/addons/netfox/simulated-collision-recorder.gd new file mode 100644 index 000000000..2ceec4baa --- /dev/null +++ b/addons/netfox/simulated-collision-recorder.gd @@ -0,0 +1,253 @@ +extends RefCounted +class_name _SimulatedCollisionRecorder + +## Helper class for recording simulation aware areas and collision shapes. +## Supports CollisionShape3D/2D, Area3D/2D. +## +## Used by [_SimulatorServer] internally. + +const SIMULATION_AWARE_GROUP := &"simulation_aware_collision" + +var _logger := NetfoxLogger._for_netfox("SimulatedCollisionRecorder") + +# tick -> Dictionary[Node, _CollisionSnapshot] +var _snapshots_by_tick : Dictionary[int, Dictionary] = {} +var _tracked_nodes : Array[Node] = [] + +## Inner base struct for recording collision snapshot per type. +## Specific types extends this base struct. +## Having this solves 2 problems: 1- no unncessary memory, 2- no unneccessary checks for type. +## Each derived struct only stores the fields relevant to its own node type. +class _CollisionSnapshot: + func restore_node(_node: Node) -> void: + push_error("_CollisionSnapshot.restore_node is abstract, must be overridden.") + +class _ShapeSnapshot3D extends _CollisionSnapshot: + var global_transform : Transform3D + var disabled : bool + + func _init(node: CollisionShape3D) -> void: + global_transform = node.global_transform + disabled = node.disabled + + func restore_node(node: Node) -> void: + node.global_transform = global_transform + node.disabled = disabled + +class _ShapeSnapshot2D extends _CollisionSnapshot: + var global_transform : Transform2D + var disabled : bool + + func _init(node: CollisionShape2D) -> void: + global_transform = node.global_transform + disabled = node.disabled + + func restore_node(node: Node) -> void: + node.global_transform = global_transform + node.disabled = disabled + +class _AreaSnapshot3D extends _CollisionSnapshot: + var global_transform : Transform3D + var monitoring : bool + var monitorable : bool + var collision_layer : int + + func _init(node: Area3D) -> void: + global_transform = node.global_transform + monitoring = node.monitoring + monitorable = node.monitorable + collision_layer = node.collision_layer + + func restore_node(node: Node) -> void: + node.global_transform = global_transform + node.monitoring = monitoring + node.monitorable = monitorable + node.collision_layer = collision_layer + +class _AreaSnapshot2D extends _CollisionSnapshot: + var global_transform : Transform2D + var monitoring : bool + var monitorable : bool + var collision_layer : int + + func _init(node: Area2D) -> void: + global_transform = node.global_transform + monitoring = node.monitoring + monitorable = node.monitorable + collision_layer = node.collision_layer + + func restore_node(node: Node) -> void: + node.global_transform = global_transform + node.monitoring = monitoring + node.monitorable = monitorable + node.collision_layer = collision_layer + +## Initialize needs to be called by owner node. +## Because refcounted classes doesnt have access to scene tree. +func initialize(tree: SceneTree) -> void: + tree.node_added.connect(_on_node_added) + + var existing_nodes := tree.get_nodes_in_group(SIMULATION_AWARE_GROUP) + _logger.trace( + "Initializing collision recorder, found %s existing node(s) in group %s", + [existing_nodes.size(), SIMULATION_AWARE_GROUP] + ) + + for node in existing_nodes: + _track(node) + +func _on_node_added(node: Node) -> void: + if node.is_in_group(SIMULATION_AWARE_GROUP): + _track(node) + +func _track(node: Node) -> void: + if node in _tracked_nodes: + return + + if not _is_supported(node): + _logger.warning( + "Node %s is in %s group but isn't a supported type (CollisionShape3D/2D, Area3D/2D), ignoring." + \ + "Only supported types should be in this group.", + [node.name, SIMULATION_AWARE_GROUP] + ) + return + + _tracked_nodes.push_back(node) + node.tree_exiting.connect(_untrack.bind(node), CONNECT_ONE_SHOT) + _logger.trace("Tracking node %s, total tracked: %s", [node.name, _tracked_nodes.size()]) + +# Called when node's tree_exiting is fired so we can untrack now. +# Old snapshot dicts aren't erased here, we remove invalid nodes while we iterate on record_tick(). +func _untrack(node: Node) -> void: + _tracked_nodes.erase(node) + _logger.trace("Untracked node %s (tree_exiting), total tracked: %s", [node.name, _tracked_nodes.size()]) + +# Check if node is type of supported ones. +func _is_supported(node: Node) -> bool: + return node is CollisionShape3D \ + or node is CollisionShape2D \ + or node is Area3D \ + or node is Area2D + +# Takes the snapshot for a node with corresponding snapshot type and returns it. +func _take_snapshot_for_node(node: Node) -> _CollisionSnapshot: + if node is CollisionShape3D: + return _ShapeSnapshot3D.new(node) + elif node is CollisionShape2D: + return _ShapeSnapshot2D.new(node) + elif node is Area3D: + return _AreaSnapshot3D.new(node) + elif node is Area2D: + return _AreaSnapshot2D.new(node) + return null + +## Record the given tick for simulation aware collision nodes. +func record_tick(tick: int) -> void: + var snapshot_dict : Dictionary[Node, _CollisionSnapshot] = {} + + var i := _tracked_nodes.size() - 1 + while i >= 0: + var node := _tracked_nodes[i] + + # While we are iterating tracked_nodes, remove if anyone is invalid. + + if not is_instance_valid(node): + _logger.trace("while recording tick(%s): dropping invalid node at index %s", [tick, i]) + _tracked_nodes.remove_at(i) + i -= 1 + continue + + snapshot_dict[node] = _take_snapshot_for_node(node) + i -= 1 + + _logger.trace("recorded tick(%s): recorded %s node(s)", [tick, snapshot_dict.size()]) + _snapshots_by_tick[tick] = snapshot_dict + +## Restore the collision aware nodes to given tick. +## If any of the collision aware nodes was not alive for given tick, +## This function will disable them. +func restore_tick(tick: int) -> void: + if not _snapshots_by_tick.has(tick): + _logger.warning( + "No snapshot recorded for tick %s (maybe older history or never recorded). Skipping restore.", + [tick] + ) + return + + var snapshot_dict := _snapshots_by_tick[tick] as Dictionary + var restored_count := 0 + var excluded_count := 0 + + for node in _tracked_nodes: + if not is_instance_valid(node): + continue + + if snapshot_dict.has(node): + var snap : _CollisionSnapshot = snapshot_dict[node] + snap.restore_node(node) + restored_count += 1 + else: + # Not tracked yet at this historical tick -> didn't exist in the sim yet. + _exclude_from_collision(node) + excluded_count += 1 + + _logger.trace( + "restored tick(%s): restored %s node(s), excluded %s node(s) not yet alive at this tick", + [tick, restored_count, excluded_count] + ) + +## Excludes a node from all collision queries. Not snapshot-driven - a node with no +## recorded snapshot at this tick has no data to restore from, only a fact to enforce +## ("this didn't exist yet"), so this stays a plain type check against the live node. +func _exclude_from_collision(node: Node) -> void: + _logger.trace("Excluding node %s from collision (not alive at this historical tick)", [node.name]) + if node is CollisionShape3D or node is CollisionShape2D: + node.disabled = true + elif node is Area3D or node is Area2D: + node.monitoring = false + node.monitorable = false + node.collision_layer = 0 + +## Capture the current state of simulation aware collision nodes. +## This is used to restore the latest after our operation is done. +## Internally [_SimulatorServer] will restore to this state by calling restore_snapshot. +func capture_current_state() -> Dictionary: + var live : Dictionary[Node, _CollisionSnapshot] = {} + var i := _tracked_nodes.size() - 1 + while i >= 0: + var node := _tracked_nodes[i] + if not is_instance_valid(node): + _logger.trace("capturing current state(): dropping invalid node at index %s", [i]) + _tracked_nodes.remove_at(i) + i -= 1 + continue + + live[node] = _take_snapshot_for_node(node) + i -= 1 + + _logger.trace("captured current state(): captured %s node(s)", [live.size()]) + return live + + +## Restore simulation aware collision nodes to given snapshot dict. +func restore_snapshot(snapshot_dict: Dictionary) -> void: + var restored_count := 0 + + for node in snapshot_dict: + + if not is_instance_valid(node): + continue + + var collision_snapshot : _CollisionSnapshot = snapshot_dict[node] + collision_snapshot.restore_node(node) + restored_count += 1 + + _logger.trace("restored snapshot(): restored %s node(s) to captured live state", [restored_count]) + +## Trim ticks that we dont need anymore. +## Will trim every tick older than param beginning. (not inclusive) +func trim_ticks(beginning: int) -> void: + for tick in _snapshots_by_tick.keys(): + if tick < beginning: + _snapshots_by_tick.erase(tick) + _logger.trace("trimming tick#%s", [tick]) diff --git a/addons/netfox/simulator.gd b/addons/netfox/simulator.gd new file mode 100644 index 000000000..7e2f31723 --- /dev/null +++ b/addons/netfox/simulator.gd @@ -0,0 +1,261 @@ +@tool +extends Node +class_name Simulator + +## @experimental [Simulator] name is a wip. [br] +## Simulates network logic depending on network authority. Make sure to read +## them before using [Simulator].[br] +## +## There are 3 seperate workflows [Simulator] operate on. [br][br] +## +## 1- Host - this [Simulator] has network authority, but [InputSender]'s +## input_node (your custom player_input.gdcript code) belongs to some other peer. +## This would be your typical server (host) but doesnt have to be if you are going +## for some custom solution (example: mesh network).[br] +## +## On host [Simulator] runs _simulated_tick functions with new inputs which +## is received by [InputSender]. After running _simulated_tick with new received +## inputs, [Simulator] broadcasts ground truth (state properties) to peers. +## +## 2- Authoritative peer - this [Simulator] doesnt have network authority, but +## [InputSender]s input_node (your custom player_input.gdscript code) belongs to +## local peer. This would be your typical player. [br] +## +## On authoritative peer, [Simulator] runs _simulated_tick with [InputSender]'s +## fresh local inputs (inputs that may or may not have been sent to server at this point). +## After applying true state, [Simulator] re-runs _simulated_tick to reach current +## game state. [br][br] +## +## 3- Puppet peer - both [Simulator] and [InputSender]s input_node (your custom +## player_input.gdscript code) doesnt have authority. This is how you see remote +## players when you are playing the game. For example your friend is a puppet player +## in your game. [br] +## +## On puppet peers, [Simulator] only applies truth received from host +## For most games this will be enough. Even with [InputSender] broadcast toggled on from +## project settings, there is no point in re-running _simulated_ticks because server +## sends states with inputs at the same time. For puppet peers we simply dont know +## their future inputs. [br][br] +## +## TODO: Simulator can have option to predict if input_broadcast is on for inputsender. [br] + +## The root node for resolving node paths in properties. Defaults to the parent node. +@export var root: Node = get_parent() + +## [Simulator] needs [InputSender] assigned to work with at the first place. +## Any authority change to [InputSender]'s input node (example PlayerInput) requires +## calling [method Simulator.process_settings]. +## Changing or assigning [InputSender] during runtime is not recommended by design, but also +## requires call to [method Simulator.process_settings]. +@export var listened_input_sender : InputSender = null + +@export_group("State") +## Properties that define the game state. +## [br][br] +## State properties are recorded for each tick. +## State is restored when host broadcasts truth, [Simulator] then will accept this +## as true state and apply it.[Simulator] will call _simulated_tick for the t. +@export var state_properties: Array[String] + +# Simulated nodes. +var _sim_nodes := [] as Array[Node] + +## Decides which peers will receive updates +var visibility_filter := PeerVisibilityFilter.new() + +@onready var _logger: NetfoxLogger = NetfoxLogger._for_netfox("Simulator:" + root.name) + +var _state_properties := _PropertyPool.new() + +var _properties_dirty: bool = false + +# Dictionary (root node) -> (managing simulator) +# Used to check for foreign roots when gathering simulated nodes. +static var _managed_roots := {} + +func _ready() -> void: + if Engine.is_editor_hint(): + return + + process_settings.call_deferred() + + # Reprocess authority on connect + if NetworkEvents.enabled: + # User might change `multiplayer` - `NetworkEvents` handles that + NetworkEvents.on_client_start.connect(func(__): process_settings()) + else: + multiplayer.connected_to_server.connect(process_settings) + +func _enter_tree() -> void: + if Engine.is_editor_hint(): + return + + _managed_roots[root] = self + + if not visibility_filter: + visibility_filter = PeerVisibilityFilter.new() + + if not visibility_filter.get_parent(): + add_child(visibility_filter) + +func _exit_tree() -> void: + if Engine.is_editor_hint(): + return + + _managed_roots.erase(root) + + + for node in _sim_nodes + _state_properties.get_subjects(): + NetworkSynchronizationServer.deregister(node) + NetworkIdentityServer.deregister_node(node) + NetworkHistoryServer.deregister(node) + + SimulatorServer.deregister_simulator(self) + + +func _notification(what: int) -> void: + if what == NOTIFICATION_EDITOR_PRE_SAVE: + update_configuration_warnings() + +func _get_configuration_warnings() -> PackedStringArray: + if not root: + root = get_parent() + + # Explore state and input properties + if not root: + return ["No valid root node found!"] + + var result := PackedStringArray() + result.append_array(_NetfoxEditorUtils.gather_properties(root, "_get_simulator_state_properties", + func(node, prop): + add_state(node, prop) + )) + + return result + +## Process settings. +## [br][br] +## Call this after any change to configuration. Updates based on authority too +## ( calls process_authority ). +func process_settings() -> void: + _sim_nodes.clear() + + process_authority() + + # Gather simulated nodes. + var managed_nodes := [root] + _collect_managed_nodes(root) + _logger.debug("Filtering managed nodes: %s", [managed_nodes]) + for node in managed_nodes: + if node.has_method("_simulated_tick"): + _sim_nodes.push_back(node) + + # Register identifiers + for node in _state_properties.get_subjects(): + NetworkIdentityServer.register_node(node) + + # Register visibility filter + for node in _state_properties.get_subjects(): + NetworkSynchronizationServer.register_visibility_filter(node, visibility_filter) + +## Process settings based on authority. +## [br][br] +## Call this whenever the authority of input node changes. +## Make sure to do this at the same time on all peers. +func process_authority(): + # First de-register. + SimulatorServer.deregister_simulator(self) + + for node in _state_properties.get_subjects(): + for property in _state_properties.get_properties_of(node): + NetworkHistoryServer.deregister_simulator(node, property) + NetworkSynchronizationServer.deregister_simulator(node, property) + + # Process authority + _state_properties.set_from_paths(root, state_properties) + + if not listened_input_sender: + _logger.error("Simulator needs listened_input_sender configured and valid.") + return + + # Register state properties. + for node in _state_properties.get_subjects(): + for property in _state_properties.get_properties_of(node): + NetworkHistoryServer.register_simulator(node, property) + NetworkSynchronizationServer.register_simulator(node, property) + + SimulatorServer.register_simulator(self) + +## Add a state property. +## [br][br] +## Settings will be automatically updated. The [param node] may be a string or +## [NodePath] pointing to a node, or an actual [Node] instance. If the given +## property is already tracked, this method does nothing. +func add_state(node: Variant, property: String): + var property_path := PropertyEntry.make_path(root, node, property) + if not property_path or state_properties.has(property_path): + return + + state_properties.push_back(property_path) + _properties_dirty = true + _reprocess_settings.call_deferred() + +## Check if this [Simulator] has authority over its inputs via listened_input_sender +## This helper is used by SimulatorServer internally. +func has_authority_over_inputs() -> bool: + if not listened_input_sender: + return false + + return listened_input_sender.has_authority_over_input_nodes() + +func _reprocess_settings() -> void: + if not _properties_dirty or Engine.is_editor_hint(): + return + + _properties_dirty = false + + process_settings() + +# Helper function to apply given snapshot for only this node. +# TODO (same todo with input_sender)? +# Applying whole snapshot and iterating over ticks would be nicer +# if we decide to have singleton for this +func _apply_snapshot_for_self(snapshot : _Snapshot) -> void: + _logger.trace("Applying snapshot for self :%s", [snapshot]) + for subject in _state_properties.get_subjects(): + for property in _state_properties.get_properties_of(subject): + + if snapshot.has_property(subject, property): + var value := snapshot.get_property(subject, property) + # TODO is this should be node.set_indexed ?? + subject.set_indexed(property, value) + +# Helper function to run simulation with given parameters. +# This function is used by SimulatorServer internally. +func _run_simulation(delta : float, tick : int, is_fresh : bool) -> void: + for node in _sim_nodes: + if node: + node.call("_simulated_tick", delta, tick, is_fresh) + +# Find managed nodes recursively from given root, ignoring branches managed by +# a different [Simulator]. +func _collect_managed_nodes(root: Node) -> Array[Node]: + var result: Array[Node] = [] + for child in root.get_children(): + if _is_foreign_simulator_root(child): + continue + result.append(child) + result.append_array(_collect_managed_nodes(child)) + return result + +# Returns true if the node is the root of a different [Simulator]. +func _is_foreign_simulator_root(node: Node) -> bool: + if not _managed_roots.has(node): + # No simulator, treat node as root + return false + + if _managed_roots[node] == self: + # Node is our own root + return false + + # Node is foreign root + return true diff --git a/examples/server-side-vehicle/scenes/server_side_tank.tscn b/examples/server-side-vehicle/scenes/server_side_tank.tscn new file mode 100644 index 000000000..f804fa18d --- /dev/null +++ b/examples/server-side-vehicle/scenes/server_side_tank.tscn @@ -0,0 +1,313 @@ +[gd_scene load_steps=12 format=3 uid="uid://f1annxuory74"] + +[ext_resource type="Script" path="res://addons/netfox/input-sender.gd" id="1_c04if"] +[ext_resource type="Script" path="res://examples/server-side-vehicle/scripts/server_side_tank.gd" id="1_jtlcb"] +[ext_resource type="PackedScene" uid="uid://uqytq0drkxtf" path="res://examples/server-side-vehicle/scenes/tank_shell.tscn" id="2_ea71k"] +[ext_resource type="PackedScene" uid="uid://bsavthtpx4joi" path="res://examples/server-side-vehicle/scenes/server_side_vehicle_info_panel.tscn" id="2_o1mnl"] +[ext_resource type="Script" path="res://examples/server-side-vehicle/scripts/tank_input.gd" id="3_8ufv1"] +[ext_resource type="Script" path="res://addons/netfox/state-synchronizer.gd" id="4_qj6bi"] + +[sub_resource type="BoxShape3D" id="BoxShape3D_tqf64"] +size = Vector3(2.9885, 1.6003, 4.71182) + +[sub_resource type="BoxShape3D" id="BoxShape3D_qe0pd"] +size = Vector3(2.01, 0.775, 2) + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_sclqb"] +albedo_color = Color(0.188235, 0.188235, 0.188235, 1) +metallic = 0.7 + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_m7l4t"] +albedo_color = Color(0.462745, 0.462745, 0.462745, 1) +metallic = 0.79 + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_g1au6"] +albedo_color = Color(0.466667, 0.466667, 0.466667, 1) +metallic = 0.45 + +[node name="ServerSideTank" type="VehicleBody3D" node_paths=PackedStringArray("turret", "shell_spawn_point", "regular_camera", "focus_camera")] +mass = 750.0 +center_of_mass_mode = 1 +center_of_mass = Vector3(0, 1.2, 0) +linear_damp = 0.13 +angular_damp = 0.1 +script = ExtResource("1_jtlcb") +turret = NodePath("Turret") +shell_spawn_point = NodePath("Turret/Marker3D") +shell_scene = ExtResource("2_ea71k") +regular_camera = NodePath("Turret/RegularCamera3D") +focus_camera = NodePath("Turret/FocusCamera3D") +info_panel_scene = ExtResource("2_o1mnl") + +[node name="InputSender" type="Node" parent="." node_paths=PackedStringArray("root")] +script = ExtResource("1_c04if") +root = NodePath("..") +input_properties = Array[String](["TankInput:movement", "TankInput:brake", "TankInput:mouse_movement", "TankInput:fire"]) + +[node name="TankInput" type="Node" parent="."] +script = ExtResource("3_8ufv1") + +[node name="StateSynchronizer" type="Node" parent="." node_paths=PackedStringArray("root")] +script = ExtResource("4_qj6bi") +root = NodePath("..") +properties = Array[String]([":engine_force", ":brake", ":steering", ":global_transform", "Turret:transform", ":_last_fire_tick", ":score"]) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.29733, 0.0841393) +shape = SubResource("BoxShape3D_tqf64") + +[node name="CollisionShape3D2" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.44697, -0.240236) +shape = SubResource("BoxShape3D_qe0pd") + +[node name="Body" type="CSGMesh3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.0007, 0.0944731) +material_override = SubResource("StandardMaterial3D_sclqb") + +[node name="CSGBox3D" type="CSGBox3D" parent="Body"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.690109, 0) +size = Vector3(3, 1.62169, 4.90454) + +[node name="CSGBox3D2" type="CSGBox3D" parent="Body"] +transform = Transform3D(1, 0, 0, 0, 0.561549, 0.827443, 0, -0.827443, 0.561549, 0, 0, 2.24295) +operation = 2 +size = Vector3(3.44184, 2.19637, 1) + +[node name="CSGBox3D3" type="CSGBox3D" parent="Body"] +transform = Transform3D(1, 0, 0, 0, 0.913183, -0.40755, 0, 0.40755, 0.913183, 0, -0.000799894, -2.73253) +operation = 2 +size = Vector3(3.35521, 1.22864, 1) + +[node name="Turret" type="CSGMesh3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.4627, 0) +material_override = SubResource("StandardMaterial3D_m7l4t") + +[node name="CSGBox3D" type="CSGBox3D" parent="Turret"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.24471) +size = Vector3(2, 0.75, 2) + +[node name="Barrel" type="CSGCylinder3D" parent="Turret"] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 2.6253) +radius = 0.15 +height = 4.0 +sides = 16 + +[node name="FocusCamera3D" type="Camera3D" parent="Turret"] +transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0.209147, 4.3057) +fov = 45.0 + +[node name="RegularCamera3D" type="Camera3D" parent="Turret"] +transform = Transform3D(-1, -2.26267e-08, 8.44439e-08, 0, 0.965926, 0.258819, -8.74228e-08, 0.258819, -0.965926, 0, 2.3423, -2.76887) +fov = 90.0 + +[node name="Marker3D" type="Marker3D" parent="Turret"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 4.83633) + +[node name="VehicleWheel3D" type="VehicleWheel3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.6, 0.6, 2.14) +use_as_traction = true +use_as_steering = true +wheel_roll_influence = 1.0 +wheel_radius = 0.6 +suspension_travel = 0.1 +suspension_stiffness = 50.0 +damping_compression = 0.3 +damping_relaxation = 0.5 + +[node name="Wheel" type="CSGMesh3D" parent="VehicleWheel3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.000132322) +material_override = SubResource("StandardMaterial3D_g1au6") + +[node name="CSGCylinder3D" type="CSGCylinder3D" parent="VehicleWheel3D/Wheel"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.6 +height = 0.5 +sides = 16 + +[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="VehicleWheel3D/Wheel"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.2 +height = 0.8 +sides = 3 + +[node name="VehicleWheel3D2" type="VehicleWheel3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.6, 0.6, 0.766) +use_as_traction = true +wheel_roll_influence = 1.0 +wheel_radius = 0.6 +suspension_travel = 0.1 +suspension_stiffness = 50.0 +damping_compression = 0.3 +damping_relaxation = 0.5 + +[node name="Wheel2" type="CSGMesh3D" parent="VehicleWheel3D2"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.000470459) +material_override = SubResource("StandardMaterial3D_g1au6") + +[node name="CSGCylinder3D" type="CSGCylinder3D" parent="VehicleWheel3D2/Wheel2"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.6 +height = 0.5 +sides = 16 + +[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="VehicleWheel3D2/Wheel2"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.2 +height = 0.8 +sides = 3 + +[node name="VehicleWheel3D3" type="VehicleWheel3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.6, 0.6, -0.618) +use_as_traction = true +wheel_roll_influence = 1.0 +wheel_radius = 0.6 +suspension_travel = 0.1 +suspension_stiffness = 50.0 +damping_compression = 0.3 +damping_relaxation = 0.5 + +[node name="Wheel3" type="CSGMesh3D" parent="VehicleWheel3D3"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -3.03984e-06) +material_override = SubResource("StandardMaterial3D_g1au6") + +[node name="CSGCylinder3D" type="CSGCylinder3D" parent="VehicleWheel3D3/Wheel3"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.6 +height = 0.5 +sides = 16 + +[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="VehicleWheel3D3/Wheel3"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.2 +height = 0.8 +sides = 3 + +[node name="VehicleWheel3D4" type="VehicleWheel3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.6, 0.6, -1.9835) +use_as_traction = true +wheel_roll_influence = 1.0 +wheel_radius = 0.6 +suspension_travel = 0.1 +suspension_stiffness = 50.0 +damping_compression = 0.3 +damping_relaxation = 0.5 + +[node name="Wheel4" type="CSGMesh3D" parent="VehicleWheel3D4"] +material_override = SubResource("StandardMaterial3D_g1au6") + +[node name="CSGCylinder3D" type="CSGCylinder3D" parent="VehicleWheel3D4/Wheel4"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.6 +height = 0.5 +sides = 16 + +[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="VehicleWheel3D4/Wheel4"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.2 +height = 0.8 +sides = 3 + +[node name="VehicleWheel3D5" type="VehicleWheel3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.6, 0.6, 2.13987) +use_as_traction = true +use_as_steering = true +wheel_roll_influence = 1.0 +wheel_radius = 0.6 +suspension_travel = 0.1 +suspension_stiffness = 50.0 +damping_compression = 0.3 +damping_relaxation = 0.5 + +[node name="Wheel5" type="CSGMesh3D" parent="VehicleWheel3D5"] +material_override = SubResource("StandardMaterial3D_g1au6") + +[node name="CSGCylinder3D" type="CSGCylinder3D" parent="VehicleWheel3D5/Wheel5"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.6 +height = 0.5 +sides = 16 + +[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="VehicleWheel3D5/Wheel5"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.2 +height = 0.8 +sides = 3 + +[node name="VehicleWheel3D6" type="VehicleWheel3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.6, 0.6, 0.76647) +use_as_traction = true +wheel_roll_influence = 1.0 +wheel_radius = 0.6 +suspension_travel = 0.1 +suspension_stiffness = 50.0 +damping_compression = 0.3 +damping_relaxation = 0.5 + +[node name="Wheel6" type="CSGMesh3D" parent="VehicleWheel3D6"] +material_override = SubResource("StandardMaterial3D_g1au6") + +[node name="CSGCylinder3D" type="CSGCylinder3D" parent="VehicleWheel3D6/Wheel6"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.6 +height = 0.5 +sides = 16 + +[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="VehicleWheel3D6/Wheel6"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.2 +height = 0.8 +sides = 3 + +[node name="VehicleWheel3D7" type="VehicleWheel3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.6, 0.6, -0.618003) +use_as_traction = true +wheel_roll_influence = 1.0 +wheel_radius = 0.6 +suspension_travel = 0.1 +suspension_stiffness = 50.0 +damping_compression = 0.3 +damping_relaxation = 0.5 + +[node name="Wheel7" type="CSGMesh3D" parent="VehicleWheel3D7"] +material_override = SubResource("StandardMaterial3D_g1au6") + +[node name="CSGCylinder3D" type="CSGCylinder3D" parent="VehicleWheel3D7/Wheel7"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.6 +height = 0.5 +sides = 16 + +[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="VehicleWheel3D7/Wheel7"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.2 +height = 0.8 +sides = 3 + +[node name="VehicleWheel3D8" type="VehicleWheel3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.6, 0.6, -1.9835) +use_as_traction = true +wheel_roll_influence = 1.0 +wheel_radius = 0.6 +suspension_travel = 0.1 +suspension_stiffness = 50.0 +damping_compression = 0.3 +damping_relaxation = 0.5 + +[node name="Wheel8" type="CSGMesh3D" parent="VehicleWheel3D8"] +material_override = SubResource("StandardMaterial3D_g1au6") + +[node name="CSGCylinder3D" type="CSGCylinder3D" parent="VehicleWheel3D8/Wheel8"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.6 +height = 0.5 +sides = 16 + +[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="VehicleWheel3D8/Wheel8"] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0, 0, 0) +radius = 0.2 +height = 0.8 +sides = 3 + +[connection signal="local_input" from="InputSender" to="." method="_on_input_sender_local_input"] +[connection signal="missing_input" from="InputSender" to="." method="_on_input_sender_missing_input"] +[connection signal="network_input" from="InputSender" to="." method="_on_input_sender_network_input"] diff --git a/examples/server-side-vehicle/scenes/server_side_vehicle_example.tscn b/examples/server-side-vehicle/scenes/server_side_vehicle_example.tscn new file mode 100644 index 000000000..f3a18d45c --- /dev/null +++ b/examples/server-side-vehicle/scenes/server_side_vehicle_example.tscn @@ -0,0 +1,33 @@ +[gd_scene load_steps=5 format=3 uid="uid://g5axutycr4rn"] + +[ext_resource type="PackedScene" uid="uid://badtpsxn5lago" path="res://examples/shared/ui/network-popup.tscn" id="1_h2lt4"] +[ext_resource type="PackedScene" uid="uid://cf4xd6s672bo6" path="res://examples/server-side-vehicle/scenes/tank_arena.tscn" id="2_t7ktp"] +[ext_resource type="Script" path="res://examples/server-side-vehicle/scripts/player_spawner.gd" id="3_55dug"] +[ext_resource type="PackedScene" uid="uid://f1annxuory74" path="res://examples/server-side-vehicle/scenes/server_side_tank.tscn" id="4_rnbcl"] + +[node name="ServerSideVehicleExample" type="Node"] + +[node name="Network Popup" parent="." instance=ExtResource("1_h2lt4")] + +[node name="TankArena" parent="." instance=ExtResource("2_t7ktp")] + +[node name="Network" type="Node" parent="."] + +[node name="PlayerSpawner" type="Node" parent="Network" node_paths=PackedStringArray("spawn_points")] +script = ExtResource("3_55dug") +player_scene = ExtResource("4_rnbcl") +spawn_points = [NodePath("../../SpawnPoints/Marker3D"), NodePath("../../SpawnPoints/Marker3D2"), NodePath("../../SpawnPoints/Marker3D3"), NodePath("../../SpawnPoints/Marker3D4")] + +[node name="SpawnPoints" type="Node3D" parent="."] + +[node name="Marker3D" type="Marker3D" parent="SpawnPoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 20.285, 1, 16.224) + +[node name="Marker3D2" type="Marker3D" parent="SpawnPoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 26.6316, 1, -26.2491) + +[node name="Marker3D3" type="Marker3D" parent="SpawnPoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -19.381, 1, -30.0326) + +[node name="Marker3D4" type="Marker3D" parent="SpawnPoints"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.68788, 1, 12.6846) diff --git a/examples/server-side-vehicle/scenes/server_side_vehicle_info_panel.tscn b/examples/server-side-vehicle/scenes/server_side_vehicle_info_panel.tscn new file mode 100644 index 000000000..becc13b34 --- /dev/null +++ b/examples/server-side-vehicle/scenes/server_side_vehicle_info_panel.tscn @@ -0,0 +1,71 @@ +[gd_scene load_steps=2 format=3 uid="uid://bsavthtpx4joi"] + +[ext_resource type="Script" path="res://examples/server-side-vehicle/scripts/server_side_vehicle_info_panel.gd" id="1_1v7fb"] + +[node name="ServerSideVehicleInfoPanel" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +mouse_filter = 2 +script = ExtResource("1_1v7fb") + +[node name="MarginContainer" type="MarginContainer" parent="."] +layout_mode = 1 +anchors_preset = 2 +anchor_top = 1.0 +anchor_bottom = 1.0 +offset_left = 32.0 +offset_top = -192.0 +offset_right = 156.0 +offset_bottom = -32.0 +grow_vertical = 0 + +[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"] +layout_mode = 2 + +[node name="PeerLabel" type="Label" parent="MarginContainer/VBoxContainer"] +layout_mode = 2 +text = "Server Side Vehicle Example +Peer#" + +[node name="InfoLabel" type="Label" parent="MarginContainer/VBoxContainer"] +layout_mode = 2 +text = "Controls: +Mouse (use alt+tab) -> Move Turret +F / Mouse Wheel -> Zoom +WASD : Move +Space : Brake +Left Click: Shoot" + +[node name="MarginContainer2" type="MarginContainer" parent="."] +layout_mode = 1 +anchors_preset = 7 +anchor_left = 0.5 +anchor_top = 1.0 +anchor_right = 0.5 +anchor_bottom = 1.0 +offset_left = -161.0 +offset_top = -161.0 +offset_right = 161.0 +offset_bottom = -104.0 +grow_horizontal = 2 +grow_vertical = 0 + +[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer2"] +layout_mode = 2 + +[node name="ReloadLabel" type="Label" parent="MarginContainer2/VBoxContainer"] +layout_mode = 2 +text = "Reloading" +horizontal_alignment = 1 + +[node name="ReloadProgressBar" type="ProgressBar" parent="MarginContainer2/VBoxContainer"] +layout_mode = 2 + +[node name="ScoreLabel" type="Label" parent="MarginContainer2/VBoxContainer"] +layout_mode = 2 +text = "Score: 0" +horizontal_alignment = 1 diff --git a/examples/server-side-vehicle/scenes/tank_arena.tscn b/examples/server-side-vehicle/scenes/tank_arena.tscn new file mode 100644 index 000000000..cc7db0e81 --- /dev/null +++ b/examples/server-side-vehicle/scenes/tank_arena.tscn @@ -0,0 +1,353 @@ +[gd_scene load_steps=11 format=3 uid="uid://cf4xd6s672bo6"] + +[sub_resource type="PhysicalSkyMaterial" id="PhysicalSkyMaterial_e1ky4"] +rayleigh_coefficient = 4.0 +turbidity = 324.81 +ground_color = Color(0.396078, 0.356863, 0.290196, 1) + +[sub_resource type="Sky" id="Sky_7krho"] +sky_material = SubResource("PhysicalSkyMaterial_e1ky4") + +[sub_resource type="Environment" id="Environment_gm8cr"] +background_mode = 2 +sky = SubResource("Sky_7krho") +fog_light_color = Color(0.54902, 0.584314, 0.741176, 1) +fog_light_energy = 2.1 + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_lr3b6"] +albedo_color = Color(0.576471, 0.239216, 0.341176, 1) + +[sub_resource type="BoxShape3D" id="BoxShape3D_47cjw"] +size = Vector3(75, 1, 75) + +[sub_resource type="BoxShape3D" id="BoxShape3D_1e1cg"] +size = Vector3(1, 4, 76) + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_e5vwx"] +albedo_color = Color(0.305882, 0.478431, 0.835294, 1) + +[sub_resource type="BoxShape3D" id="BoxShape3D_8or65"] +size = Vector3(1, 4, 10) + +[sub_resource type="BoxShape3D" id="BoxShape3D_av0i6"] +size = Vector3(1, 4, 35) + +[sub_resource type="BoxShape3D" id="BoxShape3D_ktkvh"] +size = Vector3(1, 2, 5) + +[node name="TankArena" type="Node3D"] + +[node name="WorldEnvironment" type="WorldEnvironment" parent="."] +environment = SubResource("Environment_gm8cr") + +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] +transform = Transform3D(0.923136, -0.326969, -0.202263, -0.0453632, -0.615034, 0.787194, -0.381787, -0.717513, -0.582593, 0, 1.32503, 0) +light_color = Color(0.996078, 0.992157, 0.988235, 1) +shadow_enabled = true + +[node name="DirectionalLight3D2" type="DirectionalLight3D" parent="."] +transform = Transform3D(0.483696, 0.725813, 0.489116, -0.00508283, -0.5565, 0.830832, 0.875221, -0.404356, -0.265487, 0, 1.32503, 0) +light_color = Color(0.94902, 0.870588, 0.854902, 1) +light_energy = 0.5 +shadow_enabled = true + +[node name="Ground" type="StaticBody3D" parent="."] + +[node name="Ground" type="CSGBox3D" parent="Ground"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0) +material_override = SubResource("StandardMaterial3D_lr3b6") +size = Vector3(75, 1, 75) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Ground"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0) +shape = SubResource("BoxShape3D_47cjw") + +[node name="Walls" type="Node3D" parent="."] + +[node name="RegularWall" type="StaticBody3D" parent="Walls"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 38, 2, 0) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall"] +shape = SubResource("BoxShape3D_1e1cg") + +[node name="RegularWall11" type="CSGBox3D" parent="Walls/RegularWall"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 76) + +[node name="RegularWall11" type="StaticBody3D" parent="Walls"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -38, 2, 0) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall11"] +shape = SubResource("BoxShape3D_1e1cg") + +[node name="RegularWall11" type="CSGBox3D" parent="Walls/RegularWall11"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 76) + +[node name="RegularWall12" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0, 2, -38) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall12"] +shape = SubResource("BoxShape3D_1e1cg") + +[node name="RegularWall11" type="CSGBox3D" parent="Walls/RegularWall12"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 76) + +[node name="RegularWall13" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0, 2, 38) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall13"] +shape = SubResource("BoxShape3D_1e1cg") + +[node name="RegularWall11" type="CSGBox3D" parent="Walls/RegularWall13"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 76) + +[node name="RegularWall18" type="StaticBody3D" parent="Walls"] +transform = Transform3D(0.752783, 0, -0.658269, 0, 1, 0, 0.658269, 0, 0.752783, -25.36, 2, 29.0161) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/RegularWall18"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 10) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall18"] +shape = SubResource("BoxShape3D_8or65") + +[node name="RegularWall19" type="StaticBody3D" parent="Walls"] +transform = Transform3D(0.0668312, 0, -0.997764, 0, 1, 0, 0.997764, 0, 0.0668312, 32.9951, 2, 13.5674) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/RegularWall19"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 10) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall19"] +shape = SubResource("BoxShape3D_8or65") + +[node name="LongWall" type="StaticBody3D" parent="Walls"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -20.8742, 2, 0) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/LongWall"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 35) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/LongWall"] +shape = SubResource("BoxShape3D_av0i6") + +[node name="LongWall2" type="StaticBody3D" parent="Walls"] +transform = Transform3D(0.861292, 0, -0.50811, 0, 1, 0, 0.50811, 0, 0.861292, 6.95699, 2, -22.6634) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/LongWall2"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 35) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/LongWall2"] +shape = SubResource("BoxShape3D_av0i6") + +[node name="RegularWall10" type="StaticBody3D" parent="Walls"] +transform = Transform3D(0.866025, 0, -0.5, 0, 1, 0, 0.5, 0, 0.866025, 7.36388, 2, 7.70559) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/RegularWall10"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 10) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall10"] +shape = SubResource("BoxShape3D_8or65") + +[node name="ShortWall12" type="StaticBody3D" parent="Walls"] +transform = Transform3D(0.827414, 0, -0.561592, 0, 1, 0, 0.561592, 0, 0.827414, 27.1415, 2, 15.854) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall12"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall12"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="RegularWall20" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.0668311, 0, 0.997764, 0, 1, 0, -0.997764, 0, -0.0668311, -33.3546, 2, -22.8314) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/RegularWall20"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 10) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall20"] +shape = SubResource("BoxShape3D_8or65") + +[node name="ShortWall13" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.827414, 0, 0.561593, 0, 1, 0, -0.561593, 0, -0.827414, -27.5009, 2, -25.118) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall13"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall13"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="ShortWall2" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-5.96046e-08, 0, -1, 0, 1, 0, 1, 0, -5.96046e-08, 11.9641, 2, 3.66263) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall2"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall2"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="ShortWall3" type="StaticBody3D" parent="Walls"] +transform = Transform3D(1, 0, -1.58933e-08, 0, 1, 0, 1.58933e-08, 0, 1, 4.91346, 2, 14.2493) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall3"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall3"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="RegularWall14" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.587997, 0, 0.808863, 0, 1, 0, -0.808863, 0, -0.587997, -10.7772, 2, -23.9271) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/RegularWall14"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 10) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall14"] +shape = SubResource("BoxShape3D_8or65") + +[node name="ShortWall4" type="StaticBody3D" parent="Walls"] +transform = Transform3D(0.406498, 0, 0.913652, 0, 1, 0, -0.913652, 0, 0.406498, -16.6237, 2, -22.1033) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall4"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall4"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="ShortWall5" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.913652, 0, 0.406497, 0, 1, 0, -0.406497, 0, -0.913652, -5.87835, 2, -28.9097) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall5"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall5"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="RegularWall16" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.757463, 0, -0.652879, 0, 1, 0, 0.652879, 0, -0.757463, -13.2699, 2, 21.8568) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/RegularWall16"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 10) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall16"] +shape = SubResource("BoxShape3D_8or65") + +[node name="ShortWall8" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.944141, 0, 0.329542, 0, 1, 0, -0.329542, 0, -0.944141, -10.9688, 2, 27.5324) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall8"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall8"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="ShortWall9" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.329542, 0, -0.944141, 0, 1, 0, 0.944141, 0, -0.329542, -18.6406, 2, 17.3868) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall9"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall9"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="RegularWall17" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.757463, 0, -0.652879, 0, 1, 0, 0.652879, 0, -0.757463, 12.3245, 2, 21.8568) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/RegularWall17"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 10) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall17"] +shape = SubResource("BoxShape3D_8or65") + +[node name="ShortWall10" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.944141, 0, 0.329542, 0, 1, 0, -0.329542, 0, -0.944141, 14.6257, 2, 27.5324) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall10"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall10"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="ShortWall11" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.329542, 0, -0.944141, 0, 1, 0, 0.944141, 0, -0.329542, 6.95385, 2, 17.3868) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall11"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall11"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="RegularWall15" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.871008, 0, -0.491269, 0, 1, 0, 0.491269, 0, -0.871008, 25.1636, 2, -16.4828) + +[node name="RegularWall9" type="CSGBox3D" parent="Walls/RegularWall15"] +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 4, 10) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/RegularWall15"] +shape = SubResource("BoxShape3D_8or65") + +[node name="ShortWall6" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.860955, 0, 0.508681, 0, 1, 0, -0.508681, 0, -0.860955, 26.3044, 2, -10.4656) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall6"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall6"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="ShortWall7" type="StaticBody3D" parent="Walls"] +transform = Transform3D(-0.508681, 0, -0.860955, 0, 1, 0, 0.860955, 0, -0.508681, 20.7763, 2, -21.9212) + +[node name="ShortWall" type="CSGBox3D" parent="Walls/ShortWall7"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +material_override = SubResource("StandardMaterial3D_e5vwx") +size = Vector3(1, 2, 5) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Walls/ShortWall7"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0) +shape = SubResource("BoxShape3D_ktkvh") + +[node name="Camera3D" type="Camera3D" parent="."] +transform = Transform3D(0.965926, 0.0449435, -0.254887, 0, 0.984808, 0.173648, 0.258819, -0.167731, 0.951251, -13.068, 8.651, 29.624) +current = true diff --git a/examples/server-side-vehicle/scenes/tank_shell.tscn b/examples/server-side-vehicle/scenes/tank_shell.tscn new file mode 100644 index 000000000..750fa53d0 --- /dev/null +++ b/examples/server-side-vehicle/scenes/tank_shell.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=4 format=3 uid="uid://uqytq0drkxtf"] + +[ext_resource type="Script" path="res://examples/server-side-vehicle/scripts/tank_shell.gd" id="1_8exh1"] + +[sub_resource type="BoxMesh" id="BoxMesh_qktr7"] +size = Vector3(0.2, 0.2, 1) + +[sub_resource type="BoxShape3D" id="BoxShape3D_rtbd2"] +size = Vector3(0.2, 0.2, 1) + +[node name="TankShell" type="Node3D"] +script = ExtResource("1_8exh1") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +mesh = SubResource("BoxMesh_qktr7") + +[node name="Area3D" type="Area3D" parent="."] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Area3D"] +shape = SubResource("BoxShape3D_rtbd2") + +[connection signal="body_entered" from="Area3D" to="." method="_on_area_3d_body_entered"] diff --git a/examples/server-side-vehicle/scripts/player_spawner.gd b/examples/server-side-vehicle/scripts/player_spawner.gd new file mode 100644 index 000000000..23d247964 --- /dev/null +++ b/examples/server-side-vehicle/scripts/player_spawner.gd @@ -0,0 +1,74 @@ +extends Node + +## Example player spawner script for server side tank example. + +@export var player_scene: PackedScene +@export var spawn_points: Array[Marker3D] = [] + +var avatars: Dictionary = {} + +func _ready(): + NetworkEvents.on_client_start.connect(_handle_connected) + NetworkEvents.on_server_start.connect(_handle_host) + NetworkEvents.on_peer_join.connect(_handle_new_peer) + NetworkEvents.on_peer_leave.connect(_handle_leave) + NetworkEvents.on_client_stop.connect(_handle_stop) + NetworkEvents.on_server_stop.connect(_handle_stop) + +func _handle_connected(id: int): + # Spawn an avatar for us + _spawn(id) + +func _handle_host(): + # Spawn own avatar on host machine + _spawn(1) + +func _handle_new_peer(id: int): + # Spawn an avatar for new player + _spawn(id) + +func _handle_leave(id: int): + if not avatars.has(id): + return + + var avatar = avatars[id] as Node + avatar.queue_free() + avatars.erase(id) + +func _handle_stop(): + # Remove all avatars on game end + for avatar in avatars.values(): + avatar.queue_free() + avatars.clear() + +func _spawn(id: int): + var avatar = player_scene.instantiate() as Node + avatars[id] = avatar + avatar.name += " #%d" % id + avatar.position = get_next_spawn_point(id) + + # Avatar is always owned by server + avatar.set_multiplayer_authority(1) + + print("Spawned avatar %s at %s" % [avatar.name, multiplayer.get_unique_id()]) + + # Avatar's input object is owned by player + var input = avatar.find_child("TankInput") + if input != null: + input.set_multiplayer_authority(id) + print("Set input(%s) ownership to %s" % [input.name, id]) + + add_child(avatar) + +func get_next_spawn_point(peer_id: int, spawn_idx: int = 0) -> Vector3: + # The same data is used to calculate the index on all peers + # As a result, spawn points are the same, even without sync + var idx := peer_id * 37 + spawn_idx * 19 + idx = hash(idx) + idx = idx % spawn_points.size() + + return spawn_points[idx].global_position + +# Used when tanks die and we need to reposition them. +func get_random_spawn_point() -> Vector3: + return spawn_points.pick_random().global_position diff --git a/examples/server-side-vehicle/scripts/server_side_tank.gd b/examples/server-side-vehicle/scripts/server_side_tank.gd new file mode 100644 index 000000000..577fef997 --- /dev/null +++ b/examples/server-side-vehicle/scripts/server_side_tank.gd @@ -0,0 +1,138 @@ +extends VehicleBody3D + +## Script example for server side coded tank. + +@export_category("movement") +@export var engine_power := 450.0 +@export var brake_force := 45.0 +@export var max_steering_angle := 75.0 +@export var steering_lerp_factor := 0.05 +@export_category("turret_settings") +@export var turret : Node3D +@export var traverse_speed := 0.0004 +@export var tilt_speed := 0.0001 +@export var tilt_lower_limit := -30.0 +@export var tilt_upper_limit := 30.0 +@export var shell_spawn_point : Marker3D = null +@export var shell_scene : PackedScene = null +@export var fire_cooldown_tick : int = 180 +@export_category("camera") +@export var regular_camera : Camera3D = null +@export var focus_camera : Camera3D = null +@export_category("ui") +@export var info_panel_scene : PackedScene = null + +@onready var input_sender : InputSender = $InputSender as InputSender +@onready var tank_input : Node = $TankInput as Node + +var score := 0 + +@onready var _turret_default_transform : Transform3D = self.turret.transform +var _turret_traverse := 0.0 # yaw +var _turret_tilt := 0.0 # pitch +var _last_fire_tick := 0 + + +# Called when the node enters the scene tree for the first time. +func _ready(): + _last_fire_tick = NetworkTime.tick + if tank_input.get_multiplayer_authority() == multiplayer.get_unique_id(): + if info_panel_scene: + var panel := info_panel_scene.instantiate() + add_child(panel) + print("Setting camera true on %s" %[name]) + regular_camera.current = true + +func _unhandled_input(event): + # Dont process local inputs on other players tanks + if not tank_input.is_multiplayer_authority(): + return + + if event.is_action_pressed("focus"): + if focus_camera.current: + focus_camera.current = false + regular_camera.current = true + else: + regular_camera.current = false + focus_camera.current = true + +# Moves vehicle on hosts. +func _handle_movement(movement : Vector2) -> void: + if not is_multiplayer_authority(): + return + + if movement.y != 0.0: + if movement.y < 0: + engine_force = engine_power + else: + engine_force = -engine_power + + else: + # No move input + engine_force = 0 + + # Brake + if tank_input.brake: + brake = brake_force + else: + brake = 0.0 + + # Steering + steering = lerp(steering, deg_to_rad(max_steering_angle) * -movement.x, steering_lerp_factor) + +# Moves the turret on the host. +func _move_turret(mouse_input : Vector2) -> void: + # Return if not host. + if not is_multiplayer_authority(): + return + + _turret_traverse -= mouse_input.x * traverse_speed + _turret_tilt += mouse_input.y * tilt_speed + + turret.basis = _turret_default_transform.basis + turret.basis = turret.basis.rotated(Vector3.UP, _turret_traverse) + + _turret_tilt = clamp(_turret_tilt, deg_to_rad(tilt_lower_limit), deg_to_rad(tilt_upper_limit)) + turret.basis = turret.basis.rotated(turret.basis.x, _turret_tilt) + +# Fires only on the host.. +func _fire(tick : int) -> void: + if tick - _last_fire_tick < fire_cooldown_tick: + return + + print("Firing!") + _last_fire_tick = tick + var shell := shell_scene.instantiate() as Node3D + get_tree().root.add_child(shell) + shell.global_transform = shell_spawn_point.global_transform + shell.firing_tank = self + +# Called only on server. +func die() -> void: + if not is_multiplayer_authority(): + return + + var player_spawner = get_parent() + global_position = player_spawner.get_random_spawn_point() + _turret_tilt = 0 + _turret_traverse = 0 + +func _on_input_sender_local_input(tick): + print("Input sender local input is emitted on peer:%s" %multiplayer.get_unique_id()) + _handle_movement(tank_input.movement) + _move_turret(tank_input.mouse_movement) + + if tank_input.fire: + _fire(tick) + + +func _on_input_sender_missing_input(_current_tick, _latest_known_input_tick): + print("Input is missing on :%s" %name) + + +func _on_input_sender_network_input(tick): + _handle_movement(tank_input.movement) + _move_turret(tank_input.mouse_movement) + + if tank_input.fire: + _fire(tick) diff --git a/examples/server-side-vehicle/scripts/server_side_vehicle_info_panel.gd b/examples/server-side-vehicle/scripts/server_side_vehicle_info_panel.gd new file mode 100644 index 000000000..8c9475ef5 --- /dev/null +++ b/examples/server-side-vehicle/scripts/server_side_vehicle_info_panel.gd @@ -0,0 +1,26 @@ +extends Control + +# Server side vehicle info panel +@onready var peer_label : Label = $MarginContainer/VBoxContainer/PeerLabel as Label +@onready var reload_progress_bar = $MarginContainer2/VBoxContainer/ReloadProgressBar +@onready var reload_label = $MarginContainer2/VBoxContainer/ReloadLabel +@onready var score_label = $MarginContainer2/VBoxContainer/ScoreLabel + +# Called when the node enters the scene tree for the first time. +func _ready(): + peer_label.text = "Server Side Vehicle Example \n Peer#" + str(multiplayer.get_unique_id()) + +func _process(_delta): + var tank = get_parent() + if not tank: + return + + var percentage = (NetworkTime.tick - tank._last_fire_tick) as float / tank.fire_cooldown_tick as float + reload_progress_bar.value = percentage * 100.0 + + if reload_progress_bar.value > 99: + reload_label.text = "Loaded" + else: + reload_label.text = "Loading" + + score_label.text = "Score: " + str(tank.score) diff --git a/examples/server-side-vehicle/scripts/tank_input.gd b/examples/server-side-vehicle/scripts/tank_input.gd new file mode 100644 index 000000000..600705666 --- /dev/null +++ b/examples/server-side-vehicle/scripts/tank_input.gd @@ -0,0 +1,68 @@ +extends Node + +## ServerSideTank input script + +var movement := Vector2.ZERO +var brake := false +var mouse_movement := Vector2.ZERO +var fire := false + +var _mouse_movement_buffer := Vector2.ZERO +var _fire_buffer := false + +func _ready(): + NetworkTime.before_tick_loop.connect(_gather) + NetworkTime.after_tick.connect(func(_dt, _t): _gather_always()) + +func _process(_delta) -> void: + if not is_multiplayer_authority(): + return + + if Input.is_action_just_pressed("weapon_fire"): + _fire_buffer = true + +func _notification(what): + if not is_multiplayer_authority(): + return + + if what == NOTIFICATION_WM_WINDOW_FOCUS_IN: + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + +func _gather(): + if not is_multiplayer_authority(): + return + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var mx = Input.get_axis("move_west", "move_east") + var mz = Input.get_axis("move_north", "move_south") + movement = Vector2(mx, mz) + brake = Input.is_action_pressed("move_jump") + + mouse_movement = _mouse_movement_buffer if _mouse_movement_buffer else Vector2.ZERO + _mouse_movement_buffer = Vector2.ZERO + +func _gather_always(): + if not is_multiplayer_authority(): + return + + fire = _fire_buffer + _fire_buffer = false + +func _input(event: InputEvent) -> void: + if not is_multiplayer_authority(): + return + + if event.is_action_pressed("escape"): + if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: + Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) + else: + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + +func _unhandled_input(event: InputEvent) -> void: + if not is_multiplayer_authority(): + return + + if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED and event is InputEventMouseMotion: + _mouse_movement_buffer.x += event.relative.x + _mouse_movement_buffer.y += event.relative.y diff --git a/examples/server-side-vehicle/scripts/tank_shell.gd b/examples/server-side-vehicle/scripts/tank_shell.gd new file mode 100644 index 000000000..5a3f5dcc9 --- /dev/null +++ b/examples/server-side-vehicle/scripts/tank_shell.gd @@ -0,0 +1,25 @@ +extends Node3D + +# Tank Shell script + +# Only can kill tanks on server. + +@export var speed := 50.0 + +# Set this from firing tank +var firing_tank : Node = null + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + global_position += global_transform.basis.z * speed * delta + +func _on_area_3d_body_entered(body): + if multiplayer.is_server(): + if body.has_method("die"): + body.die() + if firing_tank: + print("%s killed another tank +1 score!" %firing_tank.name) + firing_tank.score += 1 + + + queue_free() diff --git a/examples/simulated-player/scenes/simulated_player.tscn b/examples/simulated-player/scenes/simulated_player.tscn new file mode 100644 index 000000000..431aeeb8e --- /dev/null +++ b/examples/simulated-player/scenes/simulated_player.tscn @@ -0,0 +1,35 @@ +[gd_scene load_steps=7 format=3 uid="uid://co2srxfbf8dac"] + +[ext_resource type="Script" path="res://examples/simulated-player/scripts/simulated_player.gd" id="1_4n6wb"] +[ext_resource type="Script" path="res://examples/simulated-player/scripts/simulated_player_input.gd" id="2_8xdxw"] +[ext_resource type="Script" path="res://addons/netfox/input-sender.gd" id="3_1fmuj"] +[ext_resource type="Script" path="res://addons/netfox/simulator.gd" id="4_mmxoi"] + +[sub_resource type="CapsuleMesh" id="CapsuleMesh_icq3d"] + +[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_05200"] + +[node name="SimulatedPlayer" type="CharacterBody3D"] +collision_layer = 3 +collision_mask = 3 +script = ExtResource("1_4n6wb") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +mesh = SubResource("CapsuleMesh_icq3d") + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +shape = SubResource("CapsuleShape3D_05200") + +[node name="Input" type="Node" parent="."] +script = ExtResource("2_8xdxw") + +[node name="InputSender" type="Node" parent="." node_paths=PackedStringArray("root")] +script = ExtResource("3_1fmuj") +root = NodePath("..") +input_properties = Array[String](["Input:movement", "Input:jump"]) + +[node name="Simulator" type="Node" parent="." node_paths=PackedStringArray("root", "listened_input_sender")] +script = ExtResource("4_mmxoi") +root = NodePath("..") +listened_input_sender = NodePath("../InputSender") +state_properties = Array[String]([":global_transform", ":velocity"]) diff --git a/examples/simulated-player/scripts/simulated_player.gd b/examples/simulated-player/scripts/simulated_player.gd new file mode 100644 index 000000000..f267c5a65 --- /dev/null +++ b/examples/simulated-player/scripts/simulated_player.gd @@ -0,0 +1,38 @@ +extends CharacterBody3D + +const SPEED = 150 +const JUMP_VELOCITY = 6.0 + +# Get the gravity from the project settings to be synced with RigidBody nodes. +var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") + +@onready var input = $Input + +func _simulated_tick(delta : float, _tick : int, _is_fresh : bool): + # Add the gravity. + if not is_on_floor(): + velocity.y -= gravity * delta + + # Handle Jump. + if input.jump and is_on_floor(): + velocity.y = JUMP_VELOCITY + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var input_dir = Vector2(input.movement.x, input.movement.z) + var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + velocity.x = direction.x * SPEED * delta + velocity.z = direction.z * SPEED * delta + else: + velocity.x = move_toward(velocity.x, 0, SPEED * delta) + velocity.z = move_toward(velocity.z, 0, SPEED * delta) + +# print("input_jump is :%s" %input.jump) +# print("input_movement is :%s" %input.movement) +# print("velocity is :%s" %velocity) +# print("position is before move and slide: %s" %position) + velocity *= NetworkTime.physics_factor + move_and_slide() + velocity /= NetworkTime.physics_factor +# print("position is after move and slide: %s" %position) diff --git a/examples/simulated-player/scripts/simulated_player_input.gd b/examples/simulated-player/scripts/simulated_player_input.gd new file mode 100644 index 000000000..ac41bb3c4 --- /dev/null +++ b/examples/simulated-player/scripts/simulated_player_input.gd @@ -0,0 +1,22 @@ +extends Node + +# Input script for simulated player example. + +var movement: Vector3 = Vector3.ZERO +var jump: bool = false + +func _ready(): + NetworkTime.before_tick_loop.connect(_gather) + +func _gather(): + if not is_multiplayer_authority(): + return + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var mx = Input.get_axis("ui_left", "ui_right") + var mz = Input.get_axis("ui_up", "ui_down") + movement = Vector3(mx, 0, mz) + + jump = Input.is_action_pressed("move_jump") +# print("inputs: movement %s, jump %s" %[movement, jump]) diff --git a/examples/simulated-player/simulated_player_example.tscn b/examples/simulated-player/simulated_player_example.tscn new file mode 100644 index 000000000..d06bf5bc8 --- /dev/null +++ b/examples/simulated-player/simulated_player_example.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=7 format=3 uid="uid://1m61lxyrckhr"] + +[ext_resource type="PackedScene" uid="uid://cngy6hs8ohodj" path="res://examples/shared/scenes/map-square.tscn" id="1_t21fn"] +[ext_resource type="PackedScene" uid="uid://cncdbq72u50j3" path="res://examples/shared/scenes/environment.tscn" id="2_epr86"] +[ext_resource type="PackedScene" uid="uid://badtpsxn5lago" path="res://examples/shared/ui/network-popup.tscn" id="3_q107i"] +[ext_resource type="PackedScene" uid="uid://bpf1jdr255nr0" path="res://examples/shared/ui/time-display.tscn" id="4_qcrnb"] +[ext_resource type="Script" path="res://examples/shared/scripts/player-spawner.gd" id="5_3uvn6"] +[ext_resource type="PackedScene" uid="uid://co2srxfbf8dac" path="res://examples/simulated-player/scenes/simulated_player.tscn" id="6_15sfs"] + +[node name="SimulatedPlayerExample" type="Node3D"] + +[node name="Square Map" parent="." instance=ExtResource("1_t21fn")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -3.80249, 0) + +[node name="Environment" parent="." instance=ExtResource("2_epr86")] + +[node name="Network Popup" parent="." instance=ExtResource("3_q107i")] + +[node name="Time Display" parent="." instance=ExtResource("4_qcrnb")] + +[node name="Players" type="Node" parent="." node_paths=PackedStringArray("spawn_root")] +script = ExtResource("5_3uvn6") +player_scene = ExtResource("6_15sfs") +spawn_root = NodePath(".") diff --git a/project.godot b/project.godot index 38dc8ae37..c14a71052 100644 --- a/project.godot +++ b/project.godot @@ -136,6 +136,13 @@ escape={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"echo":false,"script":null) ] } +focus={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":102,"echo":false,"script":null) +, Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":4,"canceled":false,"pressed":false,"double_click":false,"script":null) +, Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":5,"canceled":false,"pressed":false,"double_click":false,"script":null) +] +} [netfox]