Guns and Reviving added - Other minor Bugfixes.

This commit is contained in:
Patrick 2024-12-19 17:41:41 +01:00
parent 8c90c4e744
commit 139e9a5134
11 changed files with 317 additions and 47 deletions

View file

@ -1,10 +1,18 @@
## SPDX-License-Identifier: GPL-3.0-or-later
## Copyright (c) 2024 interstellardevelopment.org
# Some info about this script:
# The game manager is always absolutely trusted, if the game manager goes crazy, this is not checked yet.
#TODO: For the future, maybe make some client side anti-cheat that checks if the manager is playing tricks.
# The peers are untrusted. They may cheat, or try something stupid. If at all possible, Direct peer to peer communication should be prevented.
# Rather, such a thing should be done through the game manager if possible.
extends Node
signal playerlist_changed()
var bullet: PackedScene = preload("res://scenes/entities/bullet.tscn")
## Connection Code
# Connect the signals.
@ -278,6 +286,7 @@ func player_sync_call(position: Vector3, rotation: Vector3) -> void:
# Synchronizes the player state.
@rpc("any_peer", "call_remote", "unreliable")
func player_sync(position: Vector3, rotation: Vector3) -> void:
#TODO: Anti-Cheat -> Check for realistic movement, and prevent flying.
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):
@ -309,7 +318,7 @@ func object_sync_call(position: Vector3, rotation: Vector3, node_name: String) -
if rpc_id(peer, "object_sync", position, rotation, node_name) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the npc state.
# Synchronizes the object position.
@rpc("any_peer", "call_remote", "unreliable")
func object_sync(position: Vector3, rotation: Vector3, node_name: String) -> void:
if managerID == multiplayer.get_remote_sender_id():
@ -317,14 +326,7 @@ func object_sync(position: Vector3, rotation: Vector3, node_name: String) -> voi
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()
var carryable_path: NodePath = carryable.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "pick_up_sync", npc_path, carryable_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
Log.warning("Non-manager peer tried to send a manager only request: object_sync")
# Synchronizes the text above the npc.
@rpc("any_peer", "call_local", "reliable")
@ -335,9 +337,15 @@ func npc_text_sync(text: String, npc: NodePath) -> void:
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:
if rpc_id(peer, "npc_text_sync", text, npc.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
func pick_up_sync_call(npc: NPC, carryable: Carryable) -> void:
# We need to get the path before we send out sync calls. This is because the value changes depending on whether or not it already ran locally.
var carryable_path: NodePath = carryable.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "pick_up_sync", npc.get_path(), carryable_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the object that is picked up by the npc.
@ -349,16 +357,69 @@ func pick_up_sync(npc: NodePath, carryable: NodePath) -> void:
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()
# We need to get the path before we send out sync calls. This is because the value changes depending on whether or not it already ran locally.
var carryable_path: NodePath = carryable.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "set_down_sync", npc_path, carryable_path) != OK:
if rpc_id(peer, "set_down_sync", npc.get_path(), carryable_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the object that is picked up by the npc.
# Synchronizes the object that is set down by the npc.
@rpc("any_peer", "call_local", "reliable")
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")
func incapacitate_sync_call(player: Player) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "incapacitate_sync", player.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the player being incapacitated by an npc.
@rpc("any_peer", "call_local", "reliable")
func incapacitate_sync(player: NodePath) -> void:
if managerID == multiplayer.get_remote_sender_id():
(get_node(player) as Player).incapacitate()
else:
Log.warning("Non-manager peer tried to send a manager only request: incapacitate_sync")
func revive_sync_call(player: Player) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "revive_sync", player.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the player being revived by another.
@rpc("any_peer", "call_local", "reliable")
func revive_sync(player: NodePath) -> void:
#TODO: Anti-Cheat -> Check if such a thing should even be possible from the players position!
if verify_id(multiplayer.get_remote_sender_id()):
(get_node(player) as Player).revive()
func guncase_sync_call(status: bool, player: Player) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "guncase_sync", status, player.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the guncase state.
@rpc("any_peer", "call_local", "reliable")
func guncase_sync(status: bool, player_path: NodePath) -> void:
#TODO: Anti-Cheat -> Check if the gun is even available!
if verify_id(multiplayer.get_remote_sender_id()):
(get_node("/root/%s/Guncase" % Game.mapname) as Guncase).held = status
(get_node(player_path) as Player).show_gun()
func bullet_sync_call(position: Vector3, rotation: Vector3) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "bullet_sync", position, rotation) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the bullet spawn.
@rpc("any_peer", "call_local", "reliable")
func bullet_sync(position: Vector3, rotation: Vector3) -> void:
#TODO: Anti-Cheat -> Check if the gun is even equiped!
if verify_id(multiplayer.get_remote_sender_id()):
var bullet_instance: Bullet = bullet.instantiate()
bullet_instance.position = position
bullet_instance.rotation = rotation
get_node("/root/"+Game.mapname).add_child(bullet_instance)