From 8c90c4e7446d25c8c707a41d8c7d457c2b54d637 Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Thu, 19 Dec 2024 14:01:18 +0100 Subject: [PATCH] Sync fixes for RigidBody3D. --- scenes/maps/testmap.tscn | 2 +- scripts/entities/npc.gd | 11 +++++-- scripts/maps/carryable/carryable.gd | 10 ++++-- scripts/utils/networking.gd | 51 ++++++++++++++++++++++++++--- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/scenes/maps/testmap.tscn b/scenes/maps/testmap.tscn index 8daa951..de8f1b7 100644 --- a/scenes/maps/testmap.tscn +++ b/scenes/maps/testmap.tscn @@ -47,7 +47,7 @@ script = ExtResource("2_qrp84") [node name="NPCCarryables" type="Node3D" parent="."] [node name="Closet" parent="NPCCarryables" instance=ExtResource("2_yvpvm")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 29, 10, -30) +transform = Transform3D(0.642788, -0.766044, 0, 0.766044, 0.642788, 0, 0, 0, 1, 29, 10, -30) [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] transform = Transform3D(-0.899519, 0.375, 0.224144, -0.224144, -0.836516, 0.5, 0.375, 0.399519, 0.836516, 0, 0, 0) diff --git a/scripts/entities/npc.gd b/scripts/entities/npc.gd index 6294177..fda99e1 100644 --- a/scripts/entities/npc.gd +++ b/scripts/entities/npc.gd @@ -32,13 +32,16 @@ func _timer_done() -> void: if angry_meter > 0: angry_meter -= 1 if target is Objective: - ($Label3D as Label3D).text = "%s %ds left" % [(target as Objective).displayed, angry_meter] + Networking.npc_text_sync_call("%s %ds left" % [(target as Objective).displayed, angry_meter], self) else: - ($Label3D as Label3D).text = "" + Networking.npc_text_sync_call("", self) (target as Objective).occupied = false timer.stop() phase = 2 +func set_text(text: String) -> void: + ($Label3D as Label3D).text = text + # determines the closest resting place and navigates to it. func find_objective() -> void: var lowest_distance: float = 99999999999999 @@ -70,6 +73,7 @@ func pick_up(node: Carryable) -> void: node.get_parent().remove_child(node) node.carry() node.position = Vector3(0, 1, -1) + node.rotation = Vector3(0, 0, 0) add_child(node) find_player(99999999999) phase = 3 @@ -78,7 +82,8 @@ func set_down(node: Carryable) -> void: carrying = false node.get_parent().remove_child(node) node.uncarry() - node.position = position + Vector3(0,0,1) + node.rotation = rotation + node.position += position get_tree().root.get_node("/root/"+Game.mapname+"/NPCCarryables").add_child(node) find_player(99999999999) phase = 0 diff --git a/scripts/maps/carryable/carryable.gd b/scripts/maps/carryable/carryable.gd index d8aaf2c..259ad02 100644 --- a/scripts/maps/carryable/carryable.gd +++ b/scripts/maps/carryable/carryable.gd @@ -24,6 +24,10 @@ func _on_body_entered(body: Node) -> void: carried_by = body Networking.pick_up_sync_call(carried_by, self) - if body is Player and carried: - Networking.set_down_sync_call(carried_by, self) - (body as Player).incapacitate() + if body is Player and carried: + Networking.set_down_sync_call(carried_by, self) + (body as Player).incapacitate() + +func _physics_process(_delta: float) -> void: + if Networking.isManager and sleeping and !carried: + Networking.object_sync_call(position, rotation, name) diff --git a/scripts/utils/networking.gd b/scripts/utils/networking.gd index 0220965..5df4974 100644 --- a/scripts/utils/networking.gd +++ b/scripts/utils/networking.gd @@ -103,6 +103,7 @@ func _on_peer_disconnected(id: int) -> void: Log.warning("Failed to send disconnect message to %d!" % found_peer) mutex.unlock() return + mutex.unlock() # The following function sends the roomname selected by the client. @@ -237,6 +238,8 @@ func send_playerlist(status: int, newPeers: Dictionary) -> void: Log.warning("Name %s is already taken in the room!" % Game.username) 3: Log.warning("Sent a playerlist request to a regular peer, not the host!") + else: + Log.warning("Non-manager peer tried to send a manager only request: send_playerlist") ## General Game RPCs @@ -263,6 +266,8 @@ func start_game() -> void: if multiplayer.get_remote_sender_id() == managerID: if get_tree().change_scene_to_file("res://scenes/maps/testmap.tscn") != OK: Log.warning("Couldn't load the map!") + else: + Log.warning("Non-manager peer tried to send a manager only request: start_game") func player_sync_call(position: Vector3, rotation: Vector3) -> void: for peer: int in Networking.get_ids(): @@ -271,14 +276,16 @@ func player_sync_call(position: Vector3, rotation: Vector3) -> void: Log.warning("Couldn't send RPC to %d!" % peer) # Synchronizes the player state. -@rpc("any_peer", "call_remote", "reliable") +@rpc("any_peer", "call_remote", "unreliable") func player_sync(position: Vector3, rotation: Vector3) -> void: if verify_id(multiplayer.get_remote_sender_id()): for peer: String in peers: if peers[peer] == multiplayer.get_remote_sender_id() and has_node("/root/"+Game.mapname+"/"+peer): - var player: CharacterBody3D = get_node("/root/"+Game.mapname+"/"+peer) + var player: Player = get_node("/root/"+Game.mapname+"/"+peer) player.position = position player.rotation = rotation + else: + Log.warning("A non affiliated peer tried to send: player_sync") func npc_sync_call(position: Vector3, rotation: Vector3, node_name: String) -> void: for peer: int in Networking.get_ids(): @@ -287,12 +294,30 @@ func npc_sync_call(position: Vector3, rotation: Vector3, node_name: String) -> v Log.warning("Couldn't send RPC to %d!" % peer) # Synchronizes the npc state. -@rpc("any_peer", "call_remote", "reliable") +@rpc("any_peer", "call_remote", "unreliable") func npc_sync(position: Vector3, rotation: Vector3, node_name: String) -> void: if managerID == multiplayer.get_remote_sender_id(): - var npc: CharacterBody3D = get_node("/root/"+Game.mapname+"/"+node_name) + var npc: NPC = get_node("/root/"+Game.mapname+"/"+node_name) npc.position = position npc.rotation = rotation + else: + Log.warning("Non-manager peer tried to send a manager only request: npc_sync") + +func object_sync_call(position: Vector3, rotation: Vector3, node_name: String) -> void: + for peer: int in Networking.get_ids(): + if peer != multiplayer.get_unique_id(): + if rpc_id(peer, "object_sync", position, rotation, node_name) != OK: + Log.warning("Couldn't send RPC to %d!" % peer) + +# Synchronizes the npc state. +@rpc("any_peer", "call_remote", "unreliable") +func object_sync(position: Vector3, rotation: Vector3, node_name: String) -> void: + if managerID == multiplayer.get_remote_sender_id(): + var carryable: Carryable = get_node("/root/"+Game.mapname+"/NPCCarryables/"+node_name) + carryable.position = position + carryable.rotation = rotation + else: + Log.warning("Non-manager peer tried to send a manager only request: npc_sync") func pick_up_sync_call(npc: NPC, carryable: Carryable) -> void: var npc_path: NodePath = npc.get_path() @@ -301,11 +326,27 @@ func pick_up_sync_call(npc: NPC, carryable: Carryable) -> void: if rpc_id(peer, "pick_up_sync", npc_path, carryable_path) != OK: Log.warning("Couldn't send RPC to %d!" % peer) +# Synchronizes the text above the npc. +@rpc("any_peer", "call_local", "reliable") +func npc_text_sync(text: String, npc: NodePath) -> void: + if managerID == multiplayer.get_remote_sender_id(): + (get_node(npc) as NPC).set_text(text) + else: + Log.warning("Non-manager peer tried to send a manager only request: npc_text_sync") + +func npc_text_sync_call(text: String, npc: NPC) -> void: + var npc_path: NodePath = npc.get_path() + for peer: int in Networking.get_ids(): + if rpc_id(peer, "npc_text_sync", text, npc_path) != OK: + Log.warning("Couldn't send RPC to %d!" % peer) + # Synchronizes the object that is picked up by the npc. @rpc("any_peer", "call_local", "reliable") func pick_up_sync(npc: NodePath, carryable: NodePath) -> void: if managerID == multiplayer.get_remote_sender_id(): (get_node(npc) as NPC).pick_up(get_node(carryable) as Carryable) + else: + Log.warning("Non-manager peer tried to send a manager only request: pick_up_sync") func set_down_sync_call(npc: NPC, carryable: Carryable) -> void: var npc_path: NodePath = npc.get_path() @@ -319,3 +360,5 @@ func set_down_sync_call(npc: NPC, carryable: Carryable) -> void: func set_down_sync(npc: NodePath, carryable: NodePath) -> void: if managerID == multiplayer.get_remote_sender_id(): (get_node(npc) as NPC).set_down(get_node(carryable) as Carryable) + else: + Log.warning("Non-manager peer tried to send a manager only request: set_down_sync")