private void handleVesselUpdate(KLFVesselUpdate vessel_update) { if (!isInFlight) { //While not in-flight don't create KLF vessel, just store the active vessel status info if (vessel_update.state == State.ACTIVE) { VesselStatusInfo status = new VesselStatusInfo(); status.info = vessel_update; status.ownerName = vessel_update.player; status.vesselName = vessel_update.name; status.orbit = null; status.lastUpdateTime = UnityEngine.Time.realtimeSinceStartup; status.color = KLFVessel.generateActiveColor(status.ownerName); if (playerStatus.ContainsKey(status.ownerName)) playerStatus[status.ownerName] = status; else playerStatus.Add(status.ownerName, status); } return; //Don't handle updates while not flying a ship } //Build the key for the vessel System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(vessel_update.player); sb.Append(vessel_update.id.ToString()); String vessel_key = sb.ToString(); KLFVessel vessel = null; //Try to find the key in the vessel dictionary VesselEntry entry; if (vessels.TryGetValue(vessel_key, out entry)) { vessel = entry.vessel; if (vessel == null || vessel.gameObj == null || vessel.vesselName != vessel_update.name) { //Delete the vessel if it's null or needs to be renamed vessels.Remove(vessel_key); if (vessel != null && vessel.gameObj != null) GameObject.Destroy(vessel.gameObj); vessel = null; } else { //Update the entry's timestamp VesselEntry new_entry = new VesselEntry(); new_entry.vessel = entry.vessel; new_entry.lastUpdateTime = UnityEngine.Time.realtimeSinceStartup; vessels[vessel_key] = new_entry; } } if (vessel == null) { //Add the vessel to the dictionary vessel = new KLFVessel(vessel_update.name, vessel_update.player, vessel_update.id); entry = new VesselEntry(); entry.vessel = vessel; entry.lastUpdateTime = UnityEngine.Time.realtimeSinceStartup; if (vessels.ContainsKey(vessel_key)) vessels[vessel_key] = entry; else vessels.Add(vessel_key, entry); /*Queue this update for the next update call because updating a vessel on the same step as * creating it usually causes problems for some reason */ vesselUpdateQueue.Enqueue(vessel_update); } else applyVesselUpdate(vessel_update, vessel); //Apply the vessel update to the existing vessel }
private KLFVesselUpdate getVesselUpdate(Vessel vessel) { if (vessel == null || vessel.mainBody == null) return null; //Create a KLFVesselUpdate from the vessel data KLFVesselUpdate update = new KLFVesselUpdate(); if (vessel.vesselName.Length <= MAX_VESSEL_NAME_LENGTH) update.name = vessel.vesselName; else update.name = vessel.vesselName.Substring(0, MAX_VESSEL_NAME_LENGTH); update.player = playerName; update.id = vessel.id; Vector3 pos = vessel.mainBody.transform.InverseTransformPoint(vessel.GetWorldPos3D()); Vector3 dir = vessel.mainBody.transform.InverseTransformDirection(vessel.transform.up); Vector3 vel = vessel.mainBody.transform.InverseTransformDirection(vessel.GetObtVelocity()); for (int i = 0; i < 3; i++) { update.pos[i] = pos[i]; update.dir[i] = dir[i]; update.vel[i] = vel[i]; } //Determine situation if (vessel.loaded && vessel.GetTotalMass() <= 0.0) update.situation = Situation.DESTROYED; else { switch (vessel.situation) { case Vessel.Situations.LANDED: update.situation = Situation.LANDED; break; case Vessel.Situations.SPLASHED: update.situation = Situation.SPLASHED; break; case Vessel.Situations.PRELAUNCH: update.situation = Situation.PRELAUNCH; break; case Vessel.Situations.SUB_ORBITAL: if (vessel.orbit.timeToAp < vessel.orbit.period / 2.0) update.situation = Situation.ASCENDING; else update.situation = Situation.DESCENDING; break; case Vessel.Situations.ORBITING: update.situation = Situation.ORBITING; break; case Vessel.Situations.ESCAPING: if (vessel.orbit.timeToPe > 0.0) update.situation = Situation.ENCOUNTERING; else update.situation = Situation.ESCAPING; break; case Vessel.Situations.DOCKED: update.situation = Situation.DOCKED; break; case Vessel.Situations.FLYING: update.situation = Situation.FLYING; break; default: update.situation = Situation.UNKNOWN; break; } } if (vessel == FlightGlobals.ActiveVessel) { update.state = State.ACTIVE; //Set vessel details since it's the active vessel update.detail = getVesselDetail(vessel); } else if (vessel.isCommandable) update.state = State.INACTIVE; else update.state = State.DEAD; update.timeScale = (float)Planetarium.TimeScale; update.bodyName = vessel.mainBody.bodyName; return update; }
private void applyVesselUpdate(KLFVesselUpdate vessel_update, KLFVessel vessel) { //Find the CelestialBody that matches the one in the update CelestialBody update_body = null; if (vessel.mainBody != null && vessel.mainBody.bodyName == vessel_update.bodyName) update_body = vessel.mainBody; //Vessel already has the correct body else { //Find the celestial body in the list of bodies foreach (CelestialBody body in FlightGlobals.Bodies) { if (body.bodyName == vessel_update.bodyName) { update_body = body; break; } } } if (update_body != null) { //Convert float arrays to Vector3s Vector3 pos = new Vector3(vessel_update.pos[0], vessel_update.pos[1], vessel_update.pos[2]); Vector3 dir = new Vector3(vessel_update.dir[0], vessel_update.dir[1], vessel_update.dir[2]); Vector3 vel = new Vector3(vessel_update.vel[0], vessel_update.vel[1], vessel_update.vel[2]); vessel.info = vessel_update; vessel.setOrbitalData(update_body, pos, vel, dir); } if (vessel_update.state == State.ACTIVE) { //Update the player status info VesselStatusInfo status = new VesselStatusInfo(); status.info = vessel_update; status.ownerName = vessel_update.player; status.vesselName = vessel_update.name; if (vessel.orbitValid) status.orbit = vessel.orbitRenderer.driver.orbit; status.lastUpdateTime = UnityEngine.Time.realtimeSinceStartup; status.color = KLFVessel.generateActiveColor(status.ownerName); if (playerStatus.ContainsKey(status.ownerName)) playerStatus[status.ownerName] = status; else playerStatus.Add(status.ownerName, status); } }
private KLFVesselUpdate GetVesselUpdate(Vessel ves) { if (ves == null || ves.mainBody == null) return null; //Create a KLFVesselUpdate from the vessel data KLFVesselUpdate update = new KLFVesselUpdate(); if (ves.vesselName.Length <= MaxVesselNameLength) update.Name = ves.vesselName; else update.Name = ves.vesselName.Substring(0, MaxVesselNameLength); update.Player = PlayerName; update.Id = ves.id; Vector3 pos = ves.mainBody.transform.InverseTransformPoint(ves.GetWorldPos3D()); Vector3 dir = ves.mainBody.transform.InverseTransformDirection(ves.transform.up); Vector3 vel = ves.mainBody.transform.InverseTransformDirection(ves.GetObtVelocity()); for (int i = 0; i < 3; i++) { update.Position[i] = pos[i]; update.Direction[i] = dir[i]; update.Velocity[i] = vel[i]; } //Determine situation if (ves.loaded && ves.GetTotalMass() <= 0.0) update.Situation = Situation.Destroyed; else { switch (ves.situation) { case Vessel.Situations.LANDED: update.Situation = Situation.Landed; break; case Vessel.Situations.SPLASHED: update.Situation = Situation.Splashed; break; case Vessel.Situations.PRELAUNCH: update.Situation = Situation.Prelaunch; break; case Vessel.Situations.SUB_ORBITAL: if (ves.orbit.timeToAp < ves.orbit.period / 2.0) update.Situation = Situation.Ascending; else update.Situation = Situation.Descending; break; case Vessel.Situations.ORBITING: update.Situation = Situation.Orbiting; break; case Vessel.Situations.ESCAPING: if (ves.orbit.timeToPe > 0.0) update.Situation = Situation.Encountering; else update.Situation = Situation.Escaping; break; case Vessel.Situations.DOCKED: update.Situation = Situation.Docked; break; case Vessel.Situations.FLYING: update.Situation = Situation.Flying; break; default: update.Situation = Situation.Unknown; break; } } if (ves == FlightGlobals.ActiveVessel) { update.State = State.Active; //Set vessel details since it's the active vessel update.Detail = GetVesselDetail(ves); } else if (ves.isCommandable) update.State = State.Inactive; else update.State = State.Dead; update.TimeScale = (float)Planetarium.TimeScale; update.BodyName = ves.mainBody.bodyName; return update; }
private void ApplyVesselUpdate(KLFVesselUpdate vesselUpdate, KLFVessel kVes) { //Find the CelestialBody that matches the one in the update CelestialBody updateBody = null; if(kVes.MainBody != null && kVes.MainBody.bodyName == vesselUpdate.BodyName) updateBody = kVes.MainBody; //already correct body else foreach (CelestialBody body in FlightGlobals.Bodies) if (body.bodyName == vesselUpdate.BodyName) { updateBody = body; break; } if (updateBody != null) {//Convert float arrays to Vector3s Vector3 pos = new Vector3(vesselUpdate.Position[0], vesselUpdate.Position[1], vesselUpdate.Position[2]); Vector3 dir = new Vector3(vesselUpdate.Direction[0], vesselUpdate.Direction[1], vesselUpdate.Direction[2]); Vector3 vel = new Vector3(vesselUpdate.Velocity[0], vesselUpdate.Velocity[1], vesselUpdate.Velocity[2]); kVes.Info = vesselUpdate; kVes.SetOrbitalData(updateBody, pos, vel, dir); } if (vesselUpdate.State == State.Active) {//Update the player status info VesselStatusInfo status = new VesselStatusInfo(); status.Info = vesselUpdate; status.User = vesselUpdate.Player; status.VesselName = vesselUpdate.Name; if (kVes.OrbitValid) status.Orbit = kVes.OrbitRender.driver.orbit; status.LastUpdateTime = UnityEngine.Time.realtimeSinceStartup; status.Color = KLFVessel.GenerateActiveColor(status.User); if (PlayerStatus.ContainsKey(status.User)) PlayerStatus[status.User] = status; else PlayerStatus.Add(status.User, status); } }
private void HandleVesselUpdate(KLFVesselUpdate vesselUpdate) { if (!IsInFlight) { //While not in-flight don't create KLF vessel, just store the active vessel status info if (vesselUpdate.State == State.Active) { VesselStatusInfo status = new VesselStatusInfo(); status.Info = vesselUpdate; status.User = vesselUpdate.Player; status.VesselName = vesselUpdate.Name; status.Orbit = null; status.LastUpdateTime = UnityEngine.Time.realtimeSinceStartup; status.Color = KLFVessel.GenerateActiveColor(status.User); if (PlayerStatus.ContainsKey(status.User)) PlayerStatus[status.User] = status; else PlayerStatus.Add(status.User, status); } return; //Don't handle updates while not flying a ship } //Build the key for the vessel System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(vesselUpdate.Player); sb.Append(vesselUpdate.Id.ToString()); String vesselKey = sb.ToString(); KLFVessel kVes = null; //Try to find the key in the vessel dictionary VesselEntry entry; if (Vessels.TryGetValue(vesselKey, out entry)) { kVes = entry.Vessel; if(kVes == null || kVes.GameObj == null || kVes.VesselName != vesselUpdate.Name) {//Delete the vessel if it's null or needs to be renamed Vessels.Remove(vesselKey); if (kVes != null && kVes.GameObj != null) GameObject.Destroy(kVes.GameObj); kVes = null; } else { //Update the entry's timestamp VesselEntry newEntry = new VesselEntry(); newEntry.Vessel = entry.Vessel; newEntry.LastUpdateTime = UnityEngine.Time.realtimeSinceStartup; Vessels[vesselKey] = newEntry; } } if (kVes == null) {//Add the vessel to the dictionary kVes = new KLFVessel(vesselUpdate.Name, vesselUpdate.Player, vesselUpdate.Id); entry = new VesselEntry(); entry.Vessel = kVes; entry.LastUpdateTime = UnityEngine.Time.realtimeSinceStartup; if (Vessels.ContainsKey(vesselKey)) Vessels[vesselKey] = entry; else Vessels.Add(vesselKey, entry); /*Queue this update for the next update call because updating a vessel on the same step as * creating it usually causes problems for some reason */ VesselUpdateQueue.Enqueue(vesselUpdate); } else ApplyVesselUpdate(vesselUpdate, kVes); //Apply the vessel update to the existing vessel }