diff --git a/addons/netfox/icons/input-sender.svg b/addons/netfox/icons/input-sender.svg new file mode 100644 index 00000000..3f3f3935 --- /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 00000000..8976192c --- /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/input_sender.gd b/addons/netfox/input_sender.gd new file mode 100644 index 00000000..5a5eb6ca --- /dev/null +++ b/addons/netfox/input_sender.gd @@ -0,0 +1,258 @@ +@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: +## [InputSender] assumes input snapshots arrive as whole. (atomic), if snapshot +## arrives with multiple parts, [InputSender] signals wont be reliable to +## code game logic. + +## Emitted when [InputSender] receives input from remote owner of input_properties. +## [InputSender] handles applying received input internally before emitting this signal. +## Emitted only if [InputSender] has authority. +## Use this signal to code host side logic. +signal network_input(tick : int) + +## Emitted for every tick if local peer has authority over input_property nodes. +## [InputSender] 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). +signal local_input(tick : int) + +## Emitted when [InputSender] doesnt receive anything from client on [signal NetworkTime.on_tick] +## [InputSender] will apply latest known input internally before emitting this signal. +## Emitted only if [InputSender] is authority. +## Use this signal to code host side prediction logic. +signal missing_input(current_tick : int, latest_known_input_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 _last_emitted_tick: int = -1 +var _logger := NetfoxLogger._for_netfox("InputSender") + +# Flag to connect signals only once. +var _signals_connected : bool = false + +func _ready() -> void: + if Engine.is_editor_hint(): + return + + if not NetworkTime.is_initial_sync_done(): + # Wait for time sync to complete + await NetworkTime.after_sync + +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) + + if not NetworkTime.is_initial_sync_done(): + # Wait for time sync to complete + await NetworkTime.after_sync + process_settings.call_deferred() + +## 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() + + # 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) + + if not _signals_connected: + _connect_signals() + _signals_connected = true + +## 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(): + 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) + +## 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() + +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() + +func _connect_signals() -> void: + NetworkTime.on_tick.connect(_on_tick) + +# Applies local snapshot and emits local_input if has authority over input nodes. +# Then +# applies new received network snapshots and emits network_input snapshots if +# [InputSender] is authority, +# If did not receive new network snapshots, applies latest and emits input_missing +# with latest snapshot. +func _on_tick(delta: float, tick: int) -> void: + # First handle local_input signalling. + _apply_and_emit_local_inputs(tick) + + # Move on to the network_input and input_missing signalling. + if not is_multiplayer_authority(): + return + + # Get the latest input data available + # Known issue: If input sender is configured with multiple input nodes, + # Any fresh input from one node will trigger re-emitting of other node's inputs? + # TODO: look at above issue. + var latest_input_tick := NetworkHistoryServer.get_latest_input_sender_for( + _input_properties.get_subjects(), tick) + + if latest_input_tick == _last_emitted_tick: + # There is no new input data available + var latest_snapshot := NetworkHistoryServer._get_input_sender_snapshot(latest_input_tick) + if latest_snapshot: + _logger.trace("No new input is received, will emit input_missing after applying \ + snapshot: %s", [latest_snapshot]) + + _apply_snapshot_for_self(latest_snapshot) + missing_input.emit(tick, latest_input_tick) + else: + # Iterate over fresh inputs and emit a signal with fresh inputs applied. + for i in range(_last_emitted_tick + 1, latest_input_tick + 1): + var snapshot := NetworkHistoryServer._get_input_sender_snapshot(i) + if snapshot: + _apply_snapshot_for_self(snapshot) + network_input.emit(i) + _last_emitted_tick = i + +# 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) + +# If the local peer has authority over input_property node, apply latest inputs +# and emit signal local_input. +func _apply_and_emit_local_inputs(for_tick : int) -> void: + if not _has_authority_over_input_nodes(): + return + + var latest_local_snapshot := NetworkHistoryServer._get_input_sender_snapshot(for_tick) + + if latest_local_snapshot: + _logger.trace("Applying local snapshot and emitting local_inputs: %s", [latest_local_snapshot]) + _apply_snapshot_for_self(latest_local_snapshot) + local_input.emit(for_tick) + +# 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 diff --git a/addons/netfox/netfox.gd b/addons/netfox/netfox.gd index 0ae0c713..96de1c2a 100644 --- a/addons/netfox/netfox.gd +++ b/addons/netfox/netfox.gd @@ -157,7 +157,23 @@ 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/enable_input_broadcast", + "value": false, + "type" : TYPE_BOOL + }, ] const AUTOLOADS: Array[Dictionary] = [ @@ -238,6 +254,12 @@ 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" + }, ] func _enter_tree(): diff --git a/addons/netfox/network-time.gd b/addons/netfox/network-time.gd index b7edb500..86e57158 100644 --- a/addons/netfox/network-time.gd +++ b/addons/netfox/network-time.gd @@ -565,6 +565,8 @@ func _loop() -> void: before_tick_loop.emit() before_tick.emit(ticktime, tick) + NetworkHistoryServer._record_input_sender(tick) + NetworkSynchronizationServer._synchronize_input_sender(tick) on_tick.emit(ticktime, tick) diff --git a/addons/netfox/rollback/rollback-synchronizer.gd b/addons/netfox/rollback/rollback-synchronizer.gd index 7e843b02..495800de 100644 --- a/addons/netfox/rollback/rollback-synchronizer.gd +++ b/addons/netfox/rollback/rollback-synchronizer.gd @@ -78,9 +78,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/network-history-server.gd b/addons/netfox/servers/network-history-server.gd index 092ed78d..9930ef77 100644 --- a/addons/netfox/servers/network-history-server.gd +++ b/addons/netfox/servers/network-history-server.gd @@ -5,8 +5,10 @@ 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. ## [br][br] ## Keeping history lets rollback restore earlier game states for resimulation, ## and enables [_NetworkSynchronizationServer] to send diff states by comparing @@ -15,19 +17,23 @@ class_name _NetworkHistoryServer 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 _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 # Source of truth for history 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) # 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) static var _logger := NetfoxLogger._for_netfox("NetworkHistoryServer") @@ -55,6 +61,14 @@ 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) + ## Deregister a node, no longer tracking any property it had registered using ## any of the [code]register_*()[/code] methods func deregister(node: Node) -> void: @@ -62,14 +76,18 @@ 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) # 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) # 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]: + for value in history.values(): var snapshot := value as _Snapshot snapshot.erase_subject(node) @@ -93,6 +111,11 @@ 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) + ## 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: @@ -123,6 +146,12 @@ 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 _restore_rollback_input(tick: int) -> bool: return _restore_latest(tick, _rb_input_history) @@ -132,6 +161,9 @@ 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) + func _get_rollback_input_snapshot(tick: int) -> _Snapshot: return _rb_input_snapshots.get_at(tick) @@ -141,6 +173,9 @@ 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 _merge_rollback_input(snapshot: _Snapshot) -> bool: _merge_snapshot(snapshot, _rb_input_snapshots, true) return _merge_history(snapshot, _rb_input_history, true) @@ -153,6 +188,10 @@ 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 _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): diff --git a/addons/netfox/servers/network-synchronization-server.gd b/addons/netfox/servers/network-synchronization-server.gd index 816cfabb..743e6fec 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,8 @@ 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 _visibility_filters := {} # Node to PeerVisibilityFilter @@ -36,8 +38,11 @@ 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_sender_enable_broadcast := ProjectSettings.get_setting("netfox/input_sender/enable_input_broadcast", false) as bool -var _input_redundancy := NetworkRollback.input_redundancy + +var _rb_input_redundancy := NetworkRollback.input_redundancy +var _input_sender_redundancy := ProjectSettings.get_setting("netfox/input_sender/input_redundancy", 3) as int var _last_sync_state_sent := _Snapshot.new(0) var _sync_enable_diffs := ProjectSettings.get_setting("netfox/state_synchronizer/enable_diff_states", true) as bool @@ -53,6 +58,7 @@ var _redundant_serializer: _RedundantSnapshotSerializer 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_sync: NetworkCommandServer.Command var _cmd_diff_sync: NetworkCommandServer.Command @@ -60,6 +66,7 @@ var _cmd_diff_sync: NetworkCommandServer.Command static var _logger := NetfoxLogger._for_netfox("NetworkSynchronizationServer") signal _on_input(snapshot: _Snapshot) +signal _on_input_sender(snapshot : _Snapshot) signal _on_state(snapshot: _Snapshot) ## Register a [param property] of [param node] to be synchronized @@ -101,6 +108,19 @@ 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 serializer] to use when transmitting ## [param property param] of [param node] over the network func register_schema(node: Node, property: NodePath, serializer: NetworkSchemaSerializer) -> void: @@ -132,6 +152,8 @@ 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) _visibility_filters.erase(node) _schemas.erase_subject(node) @@ -171,7 +193,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: @@ -294,6 +316,48 @@ 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 server, check if its enabled + ## TODO double check notified peers. + 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) + ## TODO check if this is solid or should be code below. +# notified_peers.add(node.get_multiplayer_authority()) + 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 + # Maybe: Only erase ourselves if this is not host, because listen servers could benefit + # TODO does above comment this make sense? + 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 _init( p_command_server: _NetworkCommandServer = null, p_history_server: _NetworkHistoryServer = null, @@ -321,10 +385,24 @@ 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) _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) +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_input(sender: int, data: PackedByteArray): var buffer := StreamPeerBuffer.new() buffer.data_array = data diff --git a/addons/netfox/simulation/input_sender.gd.uid b/addons/netfox/simulation/input_sender.gd.uid new file mode 100644 index 00000000..ae0701ba --- /dev/null +++ b/addons/netfox/simulation/input_sender.gd.uid @@ -0,0 +1 @@ +uid://dgihodqy5q27e diff --git a/addons/netfox/simulation/simulator.gd b/addons/netfox/simulation/simulator.gd new file mode 100644 index 00000000..3474cf53 --- /dev/null +++ b/addons/netfox/simulation/simulator.gd @@ -0,0 +1,31 @@ +@tool +extends Node +class_name Simulator + +## Simulates for the ticks clients dont have information yet. +## [br][br] +## [Simulator] doesnt participate in RollBack at all, and will simulate only on local clients. [br] +## Its good idea to use [Simulator] whenever you want to give control of something to local player. + +## The root node for resolving node paths in properties. Defaults to the parent node. +@export var root: Node = get_parent() + +@export_group("State") +## Properties that define the game state. +## [br][br] +## State properties are recorded for each tick. +## State is restored when server broadcasts truth, [Simulator] then will accept this +## as true state and apply it. Only then if we have inputs for future ticks it will simulate them. +@export var state_properties: Array[String] + +@onready var _logger: NetfoxLogger = NetfoxLogger._for_netfox("Simulator:" + root.name) + +var _input_properties := _PropertyPool.new() + +func _ready() -> void: + if Engine.is_editor_hint(): + return + + if not NetworkTime.is_initial_sync_done(): + # Wait for time sync to complete + await NetworkTime.after_sync diff --git a/addons/netfox/simulation/simulator.gd.uid b/addons/netfox/simulation/simulator.gd.uid new file mode 100644 index 00000000..b1a238ac --- /dev/null +++ b/addons/netfox/simulation/simulator.gd.uid @@ -0,0 +1 @@ +uid://bi4g87012gvok 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 00000000..aedbfd44 --- /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 00000000..f3a18d45 --- /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 00000000..becc13b3 --- /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 00000000..cc7db0e8 --- /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 00000000..750fa53d --- /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 00000000..23d24796 --- /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 00000000..dc3fad47 --- /dev/null +++ b/examples/server-side-vehicle/scripts/server_side_tank.gd @@ -0,0 +1,137 @@ +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 Input.is_action_just_pressed("weapon_fire"): + # Dont fire on host machine as it will fire already on _on_input_sender_new_input_received + if not multiplayer.is_server(): + _fire(NetworkTime.tick) + + 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) + +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()) + + +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 00000000..8c9475ef --- /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 00000000..60070566 --- /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 00000000..5a3f5dcc --- /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/project.godot b/project.godot index c04de8ae..0845497a 100644 --- a/project.godot +++ b/project.godot @@ -135,6 +135,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]