/// <summary> /// When the user has received a message of spawn player vehicle, /// this creates the player vehicle and removes any thing which shouldn't /// be on it. /// </summary> /// <param name="packet">The message</param> public static void SpawnPlayerVehicle(Packet packet, CSteamID sender) //Both, but never spawns the local vehicle, only executes spawn vehicle messages from other clients { // We don't actually need the "sender" id, unless we're a client and want to check that the packet came from the host // which we're not doing right now. Message_SpawnPlayerVehicle message = (Message_SpawnPlayerVehicle)((PacketSingle)packet).message; if (message.networkID == PlayerManager.localUID) { return; } Debug.Log($"Recived a Spawn Vehicle Message from: {message.csteamID}"); CSteamID spawnerSteamId = new CSteamID(message.csteamID); if (!gameLoaded) { Debug.LogWarning("Our game isn't loaded, adding spawn vehicle to queue"); playersToSpawnQueue.Enqueue(packet); playersToSpawnIdQueue.Enqueue(sender); return; } //foreach (ulong id in spawnedVehicles) //{ // if (id == message.csteamID) // { // Debug.Log("Got a spawnedVehicle message for a vehicle we have already added! Returning...."); // return; // } //} //spawnedVehicles.Add(message.csteamID); Debug.Log("Got a new spawnVehicle uID."); if (Networker.isHost) { //Debug.Log("Generating UIDS for any missiles the new vehicle has"); for (int i = 0; i < message.hpLoadout.Length; i++) { for (int j = 0; j < message.hpLoadout[i].missileUIDS.Length; j++) { if (message.hpLoadout[i].missileUIDS[j] != 0) { //Storing the old one ulong clientsUID = message.hpLoadout[i].missileUIDS[j]; //Generating a new global UID for that missile message.hpLoadout[i].missileUIDS[j] = Networker.GenerateNetworkUID(); //Sending it back to that client NetworkSenderThread.Instance.SendPacketToSpecificPlayer(spawnerSteamId, new Message_RequestNetworkUID(clientsUID, message.hpLoadout[i].missileUIDS[j]), EP2PSend.k_EP2PSendReliable); } } } Debug.Log("Telling other clients about new player and new player about other clients. Player count = " + players.Count); for (int i = 0; i < players.Count; i++) { if (players[i].cSteamID == SteamUser.GetSteamID()) { //Debug.LogWarning("Skiping this one as it's the host"); //Send the host player to the new player. //Debug.Log($"Running host code to tell new player about host vehicle."); GameObject localVehicle = VTOLAPI.GetPlayersVehicleGameObject(); WeaponManager localWeaponManager = localVehicle.GetComponent <WeaponManager>(); List <HPInfo> hpInfos = PlaneEquippableManager.generateHpInfoListFromWeaponManager(localWeaponManager, PlaneEquippableManager.HPInfoListGenerateNetworkType.sender); CountermeasureManager cmManager = localVehicle.GetComponentInChildren <CountermeasureManager>(); List <int> cm = PlaneEquippableManager.generateCounterMeasuresFromCmManager(cmManager); float fuel = PlaneEquippableManager.generateLocalFuelValue(); NetworkSenderThread.Instance.SendPacketToSpecificPlayer(spawnerSteamId, new Message_SpawnPlayerVehicle( players[i].vehicleType, VTMapManager.WorldToGlobalPoint(players[i].vehicle.transform.position), players[i].vehicle.transform.rotation, players[i].cSteamID.m_SteamID, players[i].vehicleUID, hpInfos.ToArray(), cm.ToArray(), fuel, players[i].leftie), EP2PSend.k_EP2PSendReliable); //Debug.Log($"We have told the new player about the host and NOT the other way around."); //Debug.Log($"We don't need to resync the host weapons, that's guaranteed to already be up to date."); continue; } if (players[i].vehicle != null) { PlaneNetworker_Receiver existingPlayersPR = players[i].vehicle.GetComponent <PlaneNetworker_Receiver>(); //We first send the new player to an existing spawned in player NetworkSenderThread.Instance.SendPacketToSpecificPlayer(players[i].cSteamID, message, EP2PSend.k_EP2PSendReliable); //Then we send this current player to the new player. NetworkSenderThread.Instance.SendPacketToSpecificPlayer(spawnerSteamId, new Message_SpawnPlayerVehicle( players[i].vehicleType, VTMapManager.WorldToGlobalPoint(players[i].vehicle.transform.position), players[i].vehicle.transform.rotation, players[i].cSteamID.m_SteamID, players[i].vehicleUID, existingPlayersPR.GenerateHPInfo(), existingPlayersPR.GetCMS(), existingPlayersPR.GetFuel(), players[i].leftie), EP2PSend.k_EP2PSendReliable); //Debug.Log($"We have told {players[i].cSteamID.m_SteamID} about the new player ({message.csteamID}) and the other way round."); //We ask the existing player what their load out just incase the host's player receiver was out of sync. NetworkSenderThread.Instance.SendPacketToSpecificPlayer(players[i].cSteamID, new Message(MessageType.WeaponsSet), EP2PSend.k_EP2PSendReliable); //Debug.Log($"We have asked {players[i].cSteamID.m_SteamID} what their current weapons are, and now waiting for a responce."); // marsh typo response lmao } else { Debug.Log("players[" + i + "].vehicle is null"); } } } if (Networker.isHost) { Debug.Log("Telling connected client about AI units"); AIManager.TellClientAboutAI(spawnerSteamId); } AddToPlayerList(new Player(spawnerSteamId, null, message.vehicle, message.networkID, message.leftie)); GameObject puppet = SpawnRepresentation(message.networkID, message.position, message.rotation, message.leftie); if (puppet != null) { PlaneEquippableManager.SetLoadout(puppet, message.networkID, message.normalizedFuel, message.hpLoadout, message.cmLoadout); } }
/// <summary> /// When the user has received a message of spawn vehicle, /// this creates the vehilc and removes any thing which shouldn't /// be on it. /// </summary> /// <param name="packet">The message</param> public static void SpawnVehicle(Packet packet) { Debug.Log("Recived a Spawn Vehicle Message"); if (!gameLoaded) { Debug.LogWarning("Our game isn't loaded, adding spawn vehicle to queue"); playersToSpawnQueue.Enqueue(packet); return; } Message_SpawnVehicle message = (Message_SpawnVehicle)((PacketSingle)packet).message; if (Networker.isHost) { Debug.Log("Generating UIDS for any missiles the new vehicle has"); for (int i = 0; i < message.hpLoadout.Length; i++) { for (int j = 0; j < message.hpLoadout[i].missileUIDS.Length; j++) { if (message.hpLoadout[i].missileUIDS[j] != 0) { //Storing the old one ulong clientsUID = message.hpLoadout[i].missileUIDS[j]; //Generating a new global UID for that missile message.hpLoadout[i].missileUIDS[j] = Networker.GenerateNetworkUID(); //Sending it back to that client Networker.SendP2P(new CSteamID(message.csteamID), new Message_RequestNetworkUID(clientsUID, message.hpLoadout[i].missileUIDS[j]), EP2PSend.k_EP2PSendReliable); } } } Debug.Log("Telling other clients about new player and new player about other clients. Player count = " + players.Count); for (int i = 0; i < players.Count; i++) { if (players[i].cSteamID == SteamUser.GetSteamID()) { Debug.LogWarning("Skiping this one as it's the host"); continue; } PlaneNetworker_Receiver existingPlayersPR = players[i].vehicle.GetComponent <PlaneNetworker_Receiver>(); //We first send the new player to an existing spawned in player Networker.SendP2P(players[i].cSteamID, message, EP2PSend.k_EP2PSendReliable); //Then we send this current player to the new player. Networker.SendP2P(new CSteamID(message.csteamID), new Message_SpawnVehicle( players[i].vehicleName, VTMapManager.WorldToGlobalPoint(players[i].vehicle.transform.position), new Vector3D(players[i].vehicle.transform.rotation.eulerAngles), players[i].cSteamID.m_SteamID, players[i].vehicleUID, existingPlayersPR.GenerateHPInfo(), existingPlayersPR.GetCMS(), existingPlayersPR.GetFuel()), EP2PSend.k_EP2PSendReliable); Debug.Log($"We have told {players[i].cSteamID.m_SteamID} about the new player ({message.csteamID}) and the other way round"); //We ask the existing player what their load out just incase the host's player receiver was out of sync. Networker.SendP2P(players[i].cSteamID, new Message(MessageType.WeaponsSet), EP2PSend.k_EP2PSendReliable); Debug.Log($"We have asked {players[i].cSteamID.m_SteamID} what their current weapons are, and now waiting for a responce."); } } GameObject newVehicle = null; switch (message.vehicle) { case VTOLVehicles.None: Debug.LogError("Vehcile Enum seems to be none, couldn't spawn player vehicle"); return; case VTOLVehicles.AV42C: newVehicle = GameObject.Instantiate(av42cPrefab, message.position.toVector3, Quaternion.Euler(message.rotation.toVector3)); break; case VTOLVehicles.FA26B: newVehicle = GameObject.Instantiate(fa26bPrefab, message.position.toVector3, Quaternion.Euler(message.rotation.toVector3)); break; case VTOLVehicles.F45A: newVehicle = GameObject.Instantiate(f45Prefab, message.position.toVector3, Quaternion.Euler(message.rotation.toVector3)); break; } Debug.Log("Setting vehicle name"); newVehicle.name = $"Client [{message.csteamID}]"; Debug.Log($"Spawned new vehicle at {newVehicle.transform.position}"); RigidbodyNetworker_Receiver rbNetworker = newVehicle.AddComponent <RigidbodyNetworker_Receiver>(); rbNetworker.networkUID = message.networkID; PlaneNetworker_Receiver planeReceiver = newVehicle.AddComponent <PlaneNetworker_Receiver>(); planeReceiver.networkUID = message.networkID; if (message.vehicle == VTOLVehicles.AV42C || message.vehicle == VTOLVehicles.F45A) { Debug.Log("Adding Tilt Controller to this vehicle " + message.networkID); EngineTiltNetworker_Receiver tiltReceiver = newVehicle.AddComponent <EngineTiltNetworker_Receiver>(); tiltReceiver.networkUID = message.networkID; } Rigidbody rb = newVehicle.GetComponent <Rigidbody>(); AIPilot aIPilot = newVehicle.GetComponent <AIPilot>(); Debug.Log($"Changing {newVehicle.name}'s position and rotation\nPos:{rb.position} Rotation:{rb.rotation.eulerAngles}"); aIPilot.kPlane.SetToKinematic(); aIPilot.kPlane.enabled = false; rb.interpolation = RigidbodyInterpolation.None; aIPilot.commandState = AIPilot.CommandStates.Override; rb.position = message.position.toVector3; rb.rotation = Quaternion.Euler(message.rotation.toVector3); aIPilot.kPlane.enabled = true; aIPilot.kPlane.SetVelocity(Vector3.zero); aIPilot.kPlane.SetToDynamic(); rb.interpolation = RigidbodyInterpolation.Interpolate; Debug.Log($"Finished changing {newVehicle.name}\n Pos:{rb.position} Rotation:{rb.rotation.eulerAngles}"); GameObject parent = new GameObject("Name Tag Holder"); GameObject nameTag = new GameObject("Name Tag"); parent.transform.SetParent(newVehicle.transform); parent.transform.localRotation = Quaternion.Euler(0, 180, 0); nameTag.transform.SetParent(parent.transform); nameTag.AddComponent <Nametag>().SetText( SteamFriends.GetFriendPersonaName(new CSteamID(message.csteamID)), newVehicle.transform, VRHead.instance.transform); WeaponManager weaponManager = newVehicle.GetComponent <WeaponManager>(); if (weaponManager == null) { Debug.LogError("Failed to get weapon manager on " + newVehicle.name); } List <string> hpLoadoutNames = new List <string>(); for (int i = 0; i < message.hpLoadout.Length; i++) { hpLoadoutNames.Add(message.hpLoadout[i].hpName); } Debug.Log("Setting Loadout on this new vehicle spawned"); for (int i = 0; i < hpLoadoutNames.Count; i++) { Debug.Log("HP " + i + " Name: " + hpLoadoutNames[i]); } Loadout loadout = new Loadout(); loadout.normalizedFuel = message.normalizedFuel; loadout.hpLoadout = hpLoadoutNames.ToArray(); loadout.cmLoadout = message.cmLoadout; weaponManager.EquipWeapons(loadout); FuelTank fuelTank = newVehicle.GetComponent <FuelTank>(); if (fuelTank == null) { Debug.LogError("Failed to get fuel tank on " + newVehicle.name); } fuelTank.startingFuel = loadout.normalizedFuel * fuelTank.maxFuel; fuelTank.SetNormFuel(loadout.normalizedFuel); players.Add(new Player(new CSteamID(message.csteamID), newVehicle, message.vehicle, message.networkID)); }