cs-os/scripts/Networking.gd

364 lines
14 KiB
GDScript3
Raw Normal View History

2024-02-07 23:25:08 +03:00
extends Node
2024-03-07 14:46:59 +03:00
var player_script = load("res://scripts/Player.gd")
2024-03-05 02:46:07 +03:00
var player_model = load("res://scenes/models/player.tscn")
2024-02-07 23:25:08 +03:00
var peer = ENetMultiplayerPeer.new()
2024-03-05 02:46:07 +03:00
var clients:Dictionary = {}
2024-02-14 22:18:05 +03:00
var last_client_id = 1
2024-03-05 00:18:05 +03:00
2024-03-04 20:49:27 +03:00
2024-02-07 23:25:08 +03:00
#######################################SERVER####################################
func StartServer():
var port = int(GameData.server_settings["port"])
var maxclients = int(GameData.server_settings["maxclients"])
2024-02-07 23:25:08 +03:00
if (peer.create_server(port, maxclients) != OK):
2024-03-05 00:18:05 +03:00
print("Couldn't create server. Check if another proccess binds port %s" % str(port))
2024-02-07 23:25:08 +03:00
return
multiplayer.multiplayer_peer = peer
print("Server started")
peer.connect("peer_connected", _Peer_Connected)
peer.connect("peer_disconnected", _Peer_Disconnected)
func _Peer_Connected(client_id):
2024-02-08 21:16:31 +03:00
print("User " + str(client_id) + " has conected")
2024-02-14 22:18:05 +03:00
var internal_id = last_client_id + 1
last_client_id += 1
2024-02-15 18:12:48 +03:00
2024-03-07 14:46:59 +03:00
clients[client_id] = GameData.properties_example.duplicate()
var client = clients[client_id]
client["position"] = Vector3(0, 10, 0)
client["internal_id"] = internal_id
client["is_playable"] = false
client["current_weapon"] = GameData.server_settings["game"]["weapons"]["knife"].duplicate()
2024-02-15 18:12:48 +03:00
var puppet = player_model.instantiate()
puppet.set_properties(clients[client_id])
2024-02-07 23:25:08 +03:00
ServerUtils.server_map.add_child(puppet)
2024-02-15 15:14:21 +03:00
@rpc("any_peer", "reliable", "call_remote")
func client_ready(client_id):
2024-02-17 20:23:18 +03:00
var client = clients[client_id]
2024-02-15 15:14:21 +03:00
var internal_id = client["internal_id"]
NetUtils.send_everyone_except(client_id, [Networking.spawn_puppet, clients[client_id]])
2024-02-25 18:26:42 +03:00
client["ready"] = true
var client_playermodel = ServerUtils.find_playermodel_by_internal_id(internal_id)
if (client["class_type"] == 0):
var class_spawnpoint = ServerUtils.find_class_type_by_number(client["class_type"])["spawnpoint"]
client["position"] = Vector3(class_spawnpoint[0], class_spawnpoint[1], class_spawnpoint[2])
else:
var index = abs(client["class_type"])
if (client["class_type"] > 0):
client["position"] = ServerUtils.team_OS["spawnpoints"].pick_random().get_class_spawnpoint(index)
elif (client["class_type"] < 0):
client["position"] = ServerUtils.team_CS["spawnpoints"].pick_random().get_class_spawnpoint(index)
client_playermodel.set_properties(client)
client_playermodel.teleport.rpc_id(client_id, Vector3(client["position"].x, client["position"].y, client["position"].z))
2024-02-08 21:16:31 +03:00
2024-02-07 23:25:08 +03:00
func _Peer_Disconnected(client_id):
2024-02-14 22:18:05 +03:00
print("User " + str(client_id) + " has disconnected")
2024-02-17 20:23:18 +03:00
var client = clients[client_id]
2024-02-14 22:18:05 +03:00
var internal_id = client["internal_id"]
2024-03-05 02:46:07 +03:00
if (client["class_type"] < 0):
ServerUtils.team_CS["members"].erase(client)
2024-03-05 02:46:07 +03:00
elif (client["class_type"] > 0):
ServerUtils.team_OS["members"].erase(client)
2024-03-05 02:46:07 +03:00
2024-02-14 22:18:05 +03:00
rpc("despawn_puppet", internal_id)
var puppet = ServerUtils.server_map.get_node("player" + str(internal_id))
ServerUtils.server_map.remove_child(puppet)
2024-02-17 20:23:18 +03:00
clients.erase(client_id)
if (clients.size() == 0):
ServerUtils.new_game(null, null) #Not changing anything
2024-02-07 23:25:08 +03:00
@rpc ("any_peer", "reliable", "call_remote")
func get_character_properties(client_id):
2024-02-17 20:23:18 +03:00
if !clients.has(client_id):
2024-02-07 23:25:08 +03:00
return
2024-02-17 20:23:18 +03:00
var to_send = clients[client_id]
2024-02-08 21:16:31 +03:00
rpc_id(client_id, "set_character_properties", to_send.duplicate())
@rpc("any_peer", "call_remote", "unreliable")
2024-02-15 13:10:01 +03:00
func sync_client(client_id, position, rotation):
2024-02-17 20:23:18 +03:00
var client = clients[client_id]
var internal_id = str(client["internal_id"])
var client_playermodel = ServerUtils.find_playermodel_by_internal_id(internal_id)
client_playermodel.position = position
2024-03-05 22:45:27 +03:00
#client_playermodel.find_child("Head").rotation.y = rotation.y
client_playermodel.rotation.y = rotation.y
client_playermodel.find_child("Head").find_child("Camera").rotation.x = rotation.x
2024-02-08 21:16:31 +03:00
client["position"] = position
2024-02-15 13:10:01 +03:00
client["rotation"] = rotation
#NetUtils.send_everyone_except(client_id, ["sync_puppet", internal_id, position, rotation])
NetUtils.send_everyone_except(client_id, [client_playermodel.sync_puppet, internal_id, position, rotation])
2024-02-07 23:25:08 +03:00
@rpc ("any_peer", "call_remote", "reliable")
func get_client_list(client_id):
#var playermodel = ServerUtils.find_playermodel_by_internal_id(clients[client_id]["internal_id"])
2024-02-07 23:25:08 +03:00
for current_client_id in clients.keys():
2024-02-17 20:23:18 +03:00
if (current_client_id == client_id): continue
rpc_id(client_id, "spawn_puppet", clients[current_client_id])
#rpc_id(client_id, playermodel.spawn_puppet, clients[current_client_id])
2024-02-07 23:25:08 +03:00
@rpc ("any_peer", "call_remote", "reliable")
func get_server_settings(client_id):
2024-02-17 20:23:18 +03:00
var client = clients[client_id]
var internal_id = str(client["internal_id"])
var client_playermodel = ServerUtils.find_playermodel_by_internal_id(internal_id)
client_playermodel.set_game_settings.rpc_id(client_id, GameData.server_settings["game"])
2024-02-14 22:18:05 +03:00
2024-02-15 15:14:21 +03:00
@rpc ("any_peer", "call_remote", "reliable")
func set_nickname(client_id, nickname):
2024-02-17 20:23:18 +03:00
clients[client_id]["nickname"] = nickname
2024-02-14 22:18:05 +03:00
2024-02-17 01:57:08 +03:00
@rpc("any_peer", "call_remote", "reliable")
func shot(client_id):
2024-02-17 20:23:18 +03:00
var client = clients[client_id]
2024-02-17 01:57:08 +03:00
var internal_id = client["internal_id"]
var current_weapon = GameData.server_settings["game"]["weapons"].find_key(ServerUtils.find_weapon_by_number(client["current_weapon"]["number"]))
var current_weapon_settings = GameData.server_settings["game"]["weapons"][current_weapon]
2024-02-25 18:26:42 +03:00
var client_playermodel = ServerUtils.find_playermodel_by_internal_id(internal_id)
var raycast:RayCast3D = client_playermodel.get_node("Head/Camera/viewRaycast")
var weapon_raycast:RayCast3D = client_playermodel.get_node("Head/Camera/Hand/" + str(current_weapon) + "/raycast")
2024-03-27 20:07:26 +03:00
print("currect weapon: " + current_weapon)
raycast.target_position.z = -current_weapon_settings["range"]
2024-02-21 02:23:02 +03:00
raycast.force_raycast_update()
raycast.force_update_transform()
var target_point = raycast.get_collision_point()
weapon_raycast.target_position = weapon_raycast.to_local(target_point) * 1.1
2024-03-27 20:47:54 +03:00
weapon_raycast.force_raycast_update()
weapon_raycast.force_update_transform()
weapon_raycast.rotation.y = atan((weapon_raycast.position.x - client_playermodel.position.x) / ((-current_weapon_settings["range"]) - (abs(weapon_raycast.position.z - client_playermodel.position.z))))
2024-02-25 18:26:42 +03:00
weapon_raycast.target_position.y += current_weapon_settings["spreading"] * randf() * sin(randf() * 2 * PI)
weapon_raycast.target_position.z += current_weapon_settings["spreading"] * randf() * cos(randf() * 2 * PI)
2024-02-25 18:26:42 +03:00
weapon_raycast.force_raycast_update()
weapon_raycast.force_update_transform()
2024-02-21 02:23:02 +03:00
var target = weapon_raycast.get_collider()
2024-02-17 01:57:08 +03:00
2024-03-05 22:45:27 +03:00
var time_since_last_shot = (Time.get_ticks_msec() - client["last_shot"]) / 1000.
if (time_since_last_shot < current_weapon_settings["fireRate"] and client["last_shot"] > 0):
return
if (client["current_weapon"]["magazine"] == 0):
return
client["last_shot"] = Time.get_ticks_msec()
2024-02-29 17:43:41 +03:00
if (target is CharacterBody3D):
var target_internal_id = int(target.name.get_slice("player", 1))
var target_client
for checking_client_id in clients.keys():
if (clients[checking_client_id]["internal_id"] == target_internal_id):
target_client = clients[checking_client_id]
var target_client_id = int(clients.find_key(target_client))
var shape_num = weapon_raycast.get_collider_shape()
2024-02-29 20:23:16 +03:00
var shapes = ["head", "body", "hand", "leg"]
var shape = ServerUtils.choose_collision_shape(target, shape_num)
2024-02-29 17:43:41 +03:00
var damage
for s in shapes:
if s in shape.name:
damage = current_weapon_settings["damage"][s]
break
2024-03-05 02:46:07 +03:00
var target_client_team = 1 if target_client["class_type"] > 0 else -1
var client_team = 1 if client["class_type"] > 0 else -1
2024-02-29 17:43:41 +03:00
2024-03-05 02:46:07 +03:00
if (target_client_team == client_team):
2024-03-07 13:24:51 +03:00
if (GameData.server_settings["game"]["gamemodes"][str(ServerUtils.gamemode)]["friendlyfire"]):
2024-03-05 02:46:07 +03:00
target_client["HP"] -= damage
else:
target_client["HP"] -= damage
2024-03-05 22:45:27 +03:00
if (target_client["HP"] <= 0): ###########killed
2024-02-29 17:43:41 +03:00
var index = abs(client["class_type"])
var respawn = Vector3(0, 10 ,0)
2024-03-05 02:46:07 +03:00
if (target_client_team == 1):
ServerUtils.team_CS["round_score"] += 1
respawn = ServerUtils.team_OS["spawnpoints"].pick_random().get_class_spawnpoint(index)
2024-03-05 02:46:07 +03:00
elif (target_client_team == -1):
ServerUtils.team_OS["round_score"] += 1
respawn = ServerUtils.team_CS["spawnpoints"].pick_random().get_class_spawnpoint(index)
2024-03-05 02:46:07 +03:00
print("Score(OS-CS): %s - %s" % [str(ServerUtils.team_OS["round_score"]), str(ServerUtils.team_CS["round_score"])])
2024-02-29 17:43:41 +03:00
target_client["position"] = respawn
target_client["HP"] = 100
target.teleport.rpc_id(target_client_id, target_client["position"])
2024-03-05 22:45:27 +03:00
NetUtils.send_everyone(["kill_notification", client["nickname"], target_client["nickname"]])
ServerUtils.check_gamemode_end_conditions()
2024-02-29 17:43:41 +03:00
target.set_hp.rpc_id(target_client_id, target_client["HP"])
2024-02-29 20:23:16 +03:00
elif (target is StaticBody3D):
#var shapes = ["head", "body"]
#var shape_num = weapon_raycast.get_collider_shape()
#var shape = ServerUtils.choose_collision_shape(target, shape_num)
2024-02-29 20:23:16 +03:00
target.shot.rpc_id(client_id, weapon_raycast.get_collision_point())
NetUtils.send_everyone_except(client_id, [target.shot, weapon_raycast.get_collision_point()])
2024-03-05 02:46:07 +03:00
@rpc("reliable", "call_remote", "any_peer")
func change_weapon(client_id, new_weapon_number):
var client = clients[client_id]
2024-02-21 02:23:02 +03:00
client["reloading"] = false
var internal_id = str(client["internal_id"])
2024-03-10 01:01:27 +03:00
client["current_weapon"] = client["weapons"][ServerUtils.find_weapon_name_by_number(new_weapon_number)]
var client_playermodel = ServerUtils.find_playermodel_by_internal_id(internal_id)
client_playermodel.change_weapon(new_weapon_number)
2024-03-10 01:01:27 +03:00
client_playermodel.change_weapon.rpc_id(client_id, new_weapon_number)
NetUtils.send_everyone_except(client_id, [client_playermodel.change_weapon_puppet, internal_id, new_weapon_number])
@rpc("any_peer", "call_remote", "reliable")
func client_reloading(client_id):
var client = clients[client_id]
var weapons_list = GameData.server_settings["game"]["weapons"]
2024-02-21 02:23:02 +03:00
var current_client_weapon = client["current_weapon"]
var current_weapon_example = weapons_list.find_key(ServerUtils.find_weapon_by_number(current_client_weapon["number"]))
2024-02-21 02:23:02 +03:00
var current_weapon_settings = weapons_list[current_weapon_example]
var reload_time = current_weapon_settings["reload"]
client["reloading"] = true
ServerUtils.reloading_complete(client_id, reload_time)
2024-03-05 02:46:07 +03:00
2024-02-21 04:37:40 +03:00
@rpc("reliable", "any_peer", "call_remote")
func get_map(client_id):
rpc_id(client_id, "receive_map", ServerUtils.map_path)
@rpc("reliable", "any_peer", "call_remote")
func choose_class(client_id, class_id):
var client = clients[client_id]
client["class_type"] = class_id
2024-03-07 14:46:59 +03:00
#client["HP"] = GameData.settings["game"]["classes"][class_id]["HP"]
#here are must be checks if the teams are balanced. WIP.
2024-03-05 02:46:07 +03:00
if class_id > 0:
ServerUtils.team_OS["members"].push_back(client)
2024-03-05 02:46:07 +03:00
elif class_id < 0:
ServerUtils.team_CS["members"].push_back(client)
2024-03-05 02:46:07 +03:00
switch_class.rpc_id(client_id, class_id)
2024-02-07 23:25:08 +03:00
##########################################CLIENT#######################
var player
2024-02-25 18:26:42 +03:00
var menu = preload("res://scenes/HUD/menu.tscn")
2024-02-14 22:18:05 +03:00
var current_map_instance
var choose_team_hud
var playermodel
var puppets_to_spawn = []
var map_path
var map_root_name
2024-02-07 23:25:08 +03:00
@rpc ("reliable", "call_remote")
func set_character_properties(p):
player = p
@rpc("authority", "reliable", "call_remote")
2024-02-15 15:14:21 +03:00
func spawn_puppet(properties):
if (current_map_instance == null): # Player's not ready
puppets_to_spawn.push_back(properties)
return
var puppet = player_model.instantiate()
properties["ready"] = true
puppet.set_properties(properties.duplicate())
current_map_instance.add_child(puppet)
func spawn_puppet_onready(properties):
2024-02-07 23:25:08 +03:00
var puppet = player_model.instantiate()
properties["ready"] = true
puppet.set_properties(properties.duplicate())
2024-02-07 23:25:08 +03:00
2024-02-14 22:18:05 +03:00
current_map_instance.add_child(puppet)
@rpc("authority", "reliable", "call_remote")
func despawn_puppet(internal_id):
if (current_map_instance == null):
return
2024-02-14 22:18:05 +03:00
var puppet = current_map_instance.get_node("player" + str(internal_id))
current_map_instance.remove_child(puppet)
2024-02-07 23:25:08 +03:00
2024-02-21 04:37:40 +03:00
@rpc("authority", "reliable", "call_remote")
func receive_map(p):
map_path = p
map_root_name = (map_path.split("/")[-1]).split(".")[0]
2024-02-25 18:26:42 +03:00
@rpc("authority", "reliable", "call_remote")
func switch_class(class_id):
if (class_id == 128): # team is full, you must chose another
current_map_instance.remove_child(choose_team_hud)
current_map_instance.add_child(choose_team_hud)
player["class_type"] = class_id
rpc_id(1, "client_ready", multiplayer.get_unique_id())
current_map_instance.remove_child(choose_team_hud)
playermodel.set_property("is_playable", true)
playermodel.set_property("ready", true)
2024-02-25 18:26:42 +03:00
func spawn_player():
2024-03-05 02:46:07 +03:00
print(str(player_model))
playermodel = player_model.instantiate()
2024-03-05 02:46:07 +03:00
print(str(playermodel))
2024-03-07 14:46:59 +03:00
var properties = GameData.properties_example.duplicate()
2024-02-25 18:26:42 +03:00
properties = player
properties["is_playable"] = false
properties["ready"] = false
playermodel.set_properties(properties)
2024-02-25 18:26:42 +03:00
current_map_instance.add_child(playermodel)
2024-02-25 18:26:42 +03:00
2024-02-07 23:25:08 +03:00
func ConnectToServer(ip, port):
peer.create_client(ip, port)
multiplayer.multiplayer_peer = peer
if not multiplayer.connected_to_server.is_connected(_Connection_Succseeded):
multiplayer.connected_to_server.connect(_Connection_Succseeded)
if not multiplayer.connection_failed.is_connected(_Connection_Failed):
multiplayer.connection_failed.connect(_Connection_Failed)
if not multiplayer.server_disconnected.is_connected(_Server_Disconnected):
multiplayer.server_disconnected.connect(_Server_Disconnected)
func _Connection_Failed():
print("Failed to connect to server")
multiplayer.multiplayer_peer = null
2024-02-14 22:18:05 +03:00
peer = ENetMultiplayerPeer.new()
multiplayer.multiplayer_peer = peer
2024-02-07 23:25:08 +03:00
func _Connection_Succseeded():
print("Succsessfully connected to the server")
2024-02-15 15:14:21 +03:00
var nickname = GameData.client_settings["nickname"]
rpc_id(1, "set_nickname", multiplayer.get_unique_id(), nickname)
2024-02-07 23:25:08 +03:00
rpc_id(1, "get_character_properties", multiplayer.get_unique_id())
2024-02-21 04:37:40 +03:00
rpc_id(1, "get_map", multiplayer.get_unique_id())
await get_tree().create_timer(0.2).timeout # костыль
2024-02-07 23:25:08 +03:00
2024-02-21 04:37:40 +03:00
get_tree().change_scene_to_file(map_path)
2024-02-14 22:18:05 +03:00
await get_tree().create_timer(1).timeout # костыль
2024-02-21 04:37:40 +03:00
current_map_instance = get_tree().root.get_node(map_root_name)
2024-02-15 18:12:48 +03:00
2024-02-14 22:18:05 +03:00
rpc_id(1, "get_client_list", multiplayer.get_unique_id())
2024-02-15 18:12:48 +03:00
await get_tree().create_timer(0.05).timeout # костыль # tp
rpc_id(1, "get_server_settings", multiplayer.get_unique_id())
2024-02-07 23:25:08 +03:00
2024-02-25 18:26:42 +03:00
await spawn_player()
2024-02-15 15:14:21 +03:00
choose_team_hud = preload("res://scenes/HUD/choose_team.tscn").instantiate()
current_map_instance.add_child(choose_team_hud)
for puppet in puppets_to_spawn:
spawn_puppet_onready(puppet)
2024-02-07 23:25:08 +03:00
func _Server_Disconnected():
print("Server has disconnected")
2024-02-14 22:18:05 +03:00
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
2024-02-25 18:26:42 +03:00
get_tree().change_scene_to_file("res://scenes/HUD/menu.tscn")
2024-02-14 22:18:05 +03:00
multiplayer.multiplayer_peer = null
peer = ENetMultiplayerPeer.new()
multiplayer.multiplayer_peer = peer