/// <summary> /// Here we copy the flight state we received and apply to the specific vessel. /// This method is called by ksp as it's a delegate. It's called on every FixedUpdate /// </summary> private void LunaOnVesselFlyByWire(Guid id, FlightCtrlState st) { if (FlightStatesDictionary.TryGetValue(id, out var value)) { if (value.CanInterpolate) { if (VesselCommon.IsSpectating) { st.CopyFrom(value.GetInterpolatedValue()); } else { //If we are close to a vessel and we both are in space don't copy the //input controls as then the vessel jitters, specially if the other player has SAS on if (FlightGlobals.ActiveVessel.situation > Vessel.Situations.FLYING) { var interpolatedState = value.GetInterpolatedValue(); st.mainThrottle = interpolatedState.mainThrottle; st.gearDown = interpolatedState.gearDown; st.gearUp = interpolatedState.gearUp; st.headlight = interpolatedState.headlight; st.killRot = interpolatedState.killRot; } } } } }
/// <summary> /// Removes the vessel from the dictionaries /// </summary> private void RemoveVesselFromSystem(Vessel vesselToRemove) { try { vesselToRemove.OnFlyByWire -= FlyByWireDictionary[vesselToRemove.id]; } catch (Exception) { // ignored } FlyByWireDictionary.Remove(vesselToRemove.id); FlightStatesDictionary.TryRemove(vesselToRemove.id, out _); }
/// <summary> /// Here we copy the flight state we received and apply to the specific vessel. /// This method is called by ksp as it's a delegate. It's called on every FixedUpdate /// </summary> public void LunaOnVesselFlyByWire(Guid id, FlightCtrlState st) { if (!Enabled || !FlightStateSystemReady) { return; } if (FlightStatesDictionary.TryGetValue(id, out var value)) { if (VesselCommon.IsSpectating) { st.CopyFrom(value.GetInterpolatedValue(st)); } else { //If we are close to a vessel and we both are in space don't copy the //input controls as then the vessel jitters, specially if the other player has SAS on if (FlightGlobals.ActiveVessel?.situation > Vessel.Situations.FLYING) { var interpolatedState = value.GetInterpolatedValue(st); st.mainThrottle = interpolatedState.mainThrottle; st.gearDown = interpolatedState.gearDown; st.gearUp = interpolatedState.gearUp; st.headlight = interpolatedState.headlight; st.killRot = interpolatedState.killRot; } else { st.CopyFrom(value.GetInterpolatedValue(st)); } } } else { var vessel = FlightGlobals.FindVessel(id); if (vessel != null && !FlightGlobals.FindVessel(id).packed) { AddVesselToSystem(vessel); } } }
/// <summary> /// Clear the delegates and the dictionaries /// </summary> public void ClearSystem() { foreach (var keyVal in FlyByWireDictionary) { var vessel = FlightGlobals.FindVessel(keyVal.Key); if (vessel != null) { try { vessel.OnFlyByWire -= FlyByWireDictionary[keyVal.Key]; } catch (Exception) { // ignored } } } FlyByWireDictionary.Clear(); FlightStatesDictionary.Clear(); }
public void AddVesselToSystem(Vessel vessel) { if (vessel == null || vessel.isEVA) { return; } if (!FlyByWireDictionary.ContainsKey(vessel.id)) { //We must never have our own active and controlled vessel in the dictionary if (!VesselCommon.IsSpectating && FlightGlobals.ActiveVessel?.id == vessel.id) { return; } FlightStatesDictionary.TryAdd(vessel.id, new VesselFlightStateUpdate()); FlyByWireDictionary.Add(vessel.id, st => LunaOnVesselFlyByWire(vessel.id, st)); vessel.OnFlyByWire += FlyByWireDictionary[vessel.id]; } }
/// <summary> /// Adds the loaded and unpacked vessels to the dictionary /// </summary> private void AddLoadedUnpackedVesselsToDictionary() { if (Enabled && FlightStateSystemReady) { var vesselsToAdd = FlightGlobals.VesselsLoaded .Where(v => !v.isEVA && !v.packed && !FlyByWireDictionary.Keys.Contains(v.id)); foreach (var vesselToAdd in vesselsToAdd) { //We must never have our own active and controlled vessel in the dictionary if (!VesselCommon.IsSpectating && FlightGlobals.ActiveVessel?.id == vesselToAdd.id) { continue; } FlightStatesDictionary.TryAdd(vesselToAdd.id, new VesselFlightStateUpdate()); FlyByWireDictionary.Add(vesselToAdd.id, st => LunaOnVesselFlyByWire(vesselToAdd.id, st)); vesselToAdd.OnFlyByWire += FlyByWireDictionary[vesselToAdd.id]; } } }
/// <summary> /// Removes the vessel from the dictionaries /// </summary> public void RemoveVesselFromSystem(Vessel vesselToRemove) { if (vesselToRemove == null) { return; } try { vesselToRemove.OnFlyByWire -= FlyByWireDictionary[vesselToRemove.id]; } catch (Exception) { // ignored } if (FlyByWireDictionary.ContainsKey(vesselToRemove.id)) { FlyByWireDictionary.Remove(vesselToRemove.id); } FlightStatesDictionary.TryRemove(vesselToRemove.id, out _); }