Skip to content

Commit

Permalink
4.5
Browse files Browse the repository at this point in the history
- Support for KSP 1.4.x.
- Added "Vessel state" tracker for triggering playlists when a vessel is destroyed.
- Fixed issues with tracks not being preloaded correctly, preventing the "Credits" screen music from being played.
- Fixed issue with max/min velocity and altitude values not being cleared correctly.
- Fixed issue with music not being played when only one vessel exists.
- Improved memory use.
- Fixed time of day settings not working outside of the KSC view.
  • Loading branch information
pizzaoverhead committed Jun 10, 2018
1 parent 722aaf1 commit fd760e2
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 123 deletions.
2 changes: 1 addition & 1 deletion SoundtrackEditor/AudioLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static AudioClip LoadUnityAudioClip(string filePath)
Utils.Log("Loading Unity clip");
// Load the audio clip into memory.
WWW www = new WWW("file://" + filePath);
AudioClip clip = www.audioClip;
AudioClip clip = UnityEngine.WWWAudioExtensions.GetAudioClip(www);
clip.name = Path.GetFileNameWithoutExtension(filePath);
Utils.Log("Clip name: " + clip.name + ", load state: " + clip.loadState);
return clip;
Expand Down
67 changes: 50 additions & 17 deletions SoundtrackEditor/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ public enum CameraModes
Any = Flight | Map | External | IVA | Internal
}

public static CameraModes ConvertCameraMode(CameraManager.CameraMode mode)
{
switch (mode)
{
case CameraManager.CameraMode.External:
{
return CameraModes.External;
}
case CameraManager.CameraMode.Flight:
{
return CameraModes.Flight;
}
case CameraManager.CameraMode.Internal:
{
return CameraModes.Internal;
}
case CameraManager.CameraMode.IVA:
{
return CameraModes.IVA;
}
case CameraManager.CameraMode.Map:
{
return CameraModes.Map;
}
default:
{
throw new ArgumentException("Soundtrack Editor: Invalid camera mode: " + mode +
"\nCheck for an updated version of Soundtrack Editor.");
}
}
}

public static Vessel.Situations AnyVesselSituation = Vessel.Situations.DOCKED | Vessel.Situations.ESCAPING | Vessel.Situations.FLYING |
Vessel.Situations.LANDED | Vessel.Situations.ORBITING | Vessel.Situations.PRELAUNCH | Vessel.Situations.SPLASHED | Vessel.Situations.SUB_ORBITAL;

Expand Down Expand Up @@ -92,33 +124,34 @@ public static Enums.TimesOfDay TimeToTimeOfDay(double time)
return Enums.TimesOfDay.NightPM;
}

public static CameraModes ConvertCameraMode(CameraManager.CameraMode mode)
[Flags]
public enum VesselState
{
switch (mode)
Inactive = 0x1,
Active = 0x2,
Dead = 0x4,
Any = Inactive | Active | Dead
}

public static Enums.VesselState ConvertVesselState(Vessel.State state)
{
switch (state)
{
case CameraManager.CameraMode.External:
{
return CameraModes.External;
}
case CameraManager.CameraMode.Flight:
case Vessel.State.INACTIVE:
{
return CameraModes.Flight;
return Enums.VesselState.Inactive;
}
case CameraManager.CameraMode.Internal:
case Vessel.State.ACTIVE:
{
return CameraModes.Internal;
return Enums.VesselState.Active;
}
case CameraManager.CameraMode.IVA:
case Vessel.State.DEAD:
{
return CameraModes.IVA;
}
case CameraManager.CameraMode.Map:
{
return CameraModes.Map;
return Enums.VesselState.Dead;
}
default:
{
throw new ArgumentException("Soundtrack Editor: Invalid camera mode: " + mode +
throw new ArgumentException("Soundtrack Editor: Invalid vessel state: " + state +
"\nCheck for an updated version of Soundtrack Editor.");
}
}
Expand Down
14 changes: 14 additions & 0 deletions SoundtrackEditor/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public void AddMinVesselDistance(float minVesselDist)
_minVesselDist = Math.Max(_minVesselDist, minVesselDist);
}

// State: Active, Inactive, Dead
public bool MonitorVesselState { get; set; }

public static EventManager Instance { get; private set; }

public EventManager()
Expand All @@ -115,6 +118,7 @@ public void Update()
private double _previousSrfVel = 0;
private double _previousObtVel = 0;
private double _previousAlt = 0;
private Enums.VesselState _previousVesselState = 0;
private void UpdateSituation()
{
bool changed = false;
Expand Down Expand Up @@ -189,6 +193,16 @@ private void UpdateSituation()
changed = true;
}
}

if (MonitorVesselState)
{
if (_previousVesselState != Enums.ConvertVesselState(v.state))
{
Utils.Log("Vessel state changed");
_previousVesselState = Enums.ConvertVesselState(v.state);
changed = true;
}
}
}
}
else if (SoundtrackEditor.CurrentSituation.scene == Enums.Scenes.SpaceCentre)
Expand Down
4 changes: 2 additions & 2 deletions SoundtrackEditor/GuiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public static void label(string text, object obj)
return f;
}*/

public static string editFloat(string label, string text, out float value)
public static string editFloat(string label, string text, out float value, float defaultValue)
{
GUILayout.BeginHorizontal();
GUILayout.Label(label);
GUILayout.FlexibleSpace();
string newText = GUILayout.TextField(text, GUILayout.Width(200));
GUILayout.EndHorizontal();
if (String.IsNullOrEmpty(text))
value = -float.MinValue;
value = defaultValue;
else
{
Regex numericOnly = new Regex(@"[^\d.-]");
Expand Down
8 changes: 8 additions & 0 deletions SoundtrackEditor/Persistor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ public static List<Playlist> LoadPlaylists()
if (p.playWhen.minVesselDistance != float.MinValue)
EventManager.Instance.AddMinVesselDistance(p.playWhen.minVesselDistance);
}
if (playWhen.HasValue("vesselState"))
{
p.playWhen.vesselState = Enums.Parse<Enums.VesselState>(playWhen.GetValue("vesselState"));
if (p.playWhen.vesselState != Enums.VesselState.Any && p.playWhen.vesselState != 0)
EventManager.Instance.MonitorVesselState = true;
}
}

playlists.Add(p);
Expand Down Expand Up @@ -476,6 +482,8 @@ public static void SavePlaylists(List<Playlist> playlists)
preReq.AddValue("maxVesselDistance", pl.playWhen.maxVesselDistance);
if (pl.playWhen.minVesselDistance != 0)
preReq.AddValue("minVesselDistance", pl.playWhen.minVesselDistance);
if (pl.playWhen.vesselState != Enums.VesselState.Any && pl.playWhen.vesselState != 0)
preReq.AddValue("vesselSituation", pl.playWhen.vesselState);
if (pl.playWhen.scene != Enums.Scenes.Any)
preReq.AddValue("scene", pl.playWhen.scene.ToString().Replace(", ", " | "));
if (pl.playWhen.situation != Enums.AnyVesselSituation)
Expand Down
82 changes: 50 additions & 32 deletions SoundtrackEditor/Playlist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public class Prerequisites
public float minAltitude = float.MinValue;
public float maxVesselDistance = float.MaxValue;
public float minVesselDistance = 0f;
public Enums.VesselState vesselState = Enums.VesselState.Any;

public Prerequisites() {}
public Prerequisites(Prerequisites p)
Expand All @@ -214,6 +215,7 @@ public Prerequisites(Prerequisites p)
minVelocityOrbital = p.minVelocityOrbital;
maxAltitude = p.maxAltitude;
minAltitude = p.minAltitude;
vesselState = p.vesselState;
}

// TODO: Make sure any new fields are added to Equals, the CTOR and the persistor.
Expand Down Expand Up @@ -284,39 +286,42 @@ public string PrintPrerequisites()
"Body name:\t\t" + (bodyName.Length > 0 ? bodyName : "Any") + "\r\n" +
"Situation:\t" + SituationToText(situation) + "\r\n" +
"Max Orbital Velocity:\t" + (maxVelocityOrbital == float.MaxValue ? "None" : maxVelocityOrbital.ToString()) + "\r\n" +
"Min Orbital Velocity:\t" + (minVelocityOrbital == float.MinValue ? "None" : maxVelocityOrbital.ToString()) + "\r\n" +
"Max Surface Velocity:\t" + (maxVelocitySurface == float.MaxValue ? "None" : maxVelocityOrbital.ToString()) + "\r\n" +
"Min Surface Velocity:\t" + (minVelocitySurface == float.MinValue ? "None" : maxVelocityOrbital.ToString()) + "\r\n" +
"Max Altitude:\t\t" + (maxAltitude == float.MaxValue ? "None" : maxVelocityOrbital.ToString()) + "\r\n" +
"Min Altitude:\t\t" + (minAltitude == float.MinValue ? "None" : maxVelocityOrbital.ToString()) + "\r\n" +
"Min Orbital Velocity:\t" + (minVelocityOrbital == float.MinValue ? "None" : minVelocityOrbital.ToString()) + "\r\n" +
"Max Surface Velocity:\t" + (maxVelocitySurface == float.MaxValue ? "None" : maxVelocitySurface.ToString()) + "\r\n" +
"Min Surface Velocity:\t" + (minVelocitySurface == float.MinValue ? "None" : minVelocitySurface.ToString()) + "\r\n" +
"Max Altitude:\t\t" + (maxAltitude == float.MaxValue ? "None" : maxAltitude.ToString()) + "\r\n" +
"Min Altitude:\t\t" + (minAltitude == float.MinValue ? "None" : minAltitude.ToString()) + "\r\n" +
"Vessel state:\t\t\t" + vesselState + "\r\n" +
"In Atmosphere:\t\t" + inAtmosphere + "\r\n" +
"Time Of Day:\t\t" + timeOfDay;
}

private static StringBuilder situationSB = new StringBuilder();
public static string PrintSituation()
{
string message =
"Scene:\t\t\t" + Enum.GetName(typeof(Enums.Scenes), SoundtrackEditor.CurrentSituation.scene) + "\r\n" +
"Camera mode:\t\t" + (CameraManager.Instance == null ? "No camera" : CameraManager.Instance.currentCameraMode.ToString()) + "\r\n" +
"Paused:\t\t\t" + SoundtrackEditor.CurrentSituation.paused + "\r\n";
situationSB.Length = 0; // StringBuilder.Clear() not available in .NET 3.5.
situationSB.Append("Scene:\t\t\t").AppendLine(Enum.GetName(typeof(Enums.Scenes), SoundtrackEditor.CurrentSituation.scene))
.Append("Camera mode:\t\t").AppendLine(CameraManager.Instance == null ? "No camera" : CameraManager.Instance.currentCameraMode.ToString())
.Append("Paused:\t\t\t").AppendLine(SoundtrackEditor.CurrentSituation.paused.ToString());

if (SoundtrackEditor.CurrentSituation.scene == Enums.Scenes.SpaceCentre)
message += "Time Of Day:\t\t" + SoundtrackEditor.CurrentSituation.timeOfDay + "\r\n";
//if (SoundtrackEditor.CurrentSituation.scene == Enums.Scenes.SpaceCentre)
situationSB.Append("Time Of Day:\t\t").AppendLine(SoundtrackEditor.CurrentSituation.timeOfDay.ToString());

Vessel v = SoundtrackEditor.InitialLoadingComplete ? FlightGlobals.ActiveVessel : null;
if (v != null)
{
message += "Body name:\t\t" + v.mainBody.name + "\r\n" +
"Situation:\t\t\t" + v.situation + "\r\n" +
"Orbital velocity:\t\t" + v.obt_velocity.magnitude + "\r\n" +
"Surface velocity:\t\t" + v.srf_velocity.magnitude + "\r\n" +
"Altitude:\t\t\t" + v.altitude + "\r\n" +
"In Atmosphere:\t\t" + (v.atmDensity > 0);
situationSB.Append("Body name:\t\t").AppendLine(v.mainBody.name)
.Append("Situation:\t\t\t").AppendLine(v.situation.ToString())
.Append("Orbital velocity:\t\t").AppendLine(v.obt_velocity.magnitude.ToString())
.Append("Surface velocity:\t\t").AppendLine(v.srf_velocity.magnitude.ToString())
.Append("Altitude:\t\t\t").AppendLine(v.altitude.ToString())
.Append("In Atmosphere:\t\t").AppendLine((v.atmDensity > 0).ToString())
.Append("State:\t\t").AppendLine(v.state.ToString());
}
else
message += "Vessel:\t\t\tNo vessel";
situationSB.AppendLine("Vessel:\t\t\tNo vessel");

return message;
return situationSB.ToString();
}

public bool CheckPaused()
Expand Down Expand Up @@ -398,19 +403,28 @@ public bool CheckInAtmosphere(Vessel v)
return true;
}

public bool CheckVesselState(Vessel v)
{
Enums.VesselState currentState = Enums.ConvertVesselState(v.state);
if ((currentState & vesselState) != currentState)
{
//Utils.Log("Prereq failed: Expected situation " + situation + ", but was " + v.situation);
return false;
}
return true;
}

private CelestialBody _homeBody;
public bool CheckTimeOfDay()
{
if (SoundtrackEditor.CurrentSituation.scene == Enums.Scenes.SpaceCentre)
{
if (_homeBody == null)
_homeBody = FlightGlobals.GetHomeBody();
double localTime = Sun.Instance.GetLocalTimeAtPosition(Utils.KscLatitude, Utils.KscLongitude, _homeBody);
Enums.TimesOfDay tod = Enums.TimeToTimeOfDay(localTime);
if (_homeBody == null)
_homeBody = FlightGlobals.GetHomeBody();
double localTime = Sun.Instance.GetLocalTimeAtPosition(Utils.KscLatitude, Utils.KscLongitude, _homeBody);
Enums.TimesOfDay tod = Enums.TimeToTimeOfDay(localTime);

if ((tod & timeOfDay) != tod)
return false;

if ((tod & timeOfDay) != tod)
return false;
}
return true;
}

Expand All @@ -436,6 +450,7 @@ public bool PrerequisitesMet()
if (!CheckAltitude(v)) return false;
if (!CheckVesselDistance(v)) return false;
if (!CheckInAtmosphere(v)) return false;
if (!CheckVesselState(v)) return false;

//if (p.playAfter
//if (p.playNext
Expand Down Expand Up @@ -475,6 +490,7 @@ public override bool Equals(System.Object obj)
(this.minAltitude == p.minAltitude) &&
(this.maxVesselDistance == p.maxVesselDistance) &&
(this.minVesselDistance == p.minVesselDistance) &&
(this.vesselState == p.vesselState) &&
(this.scene == p.scene) &&
(this.situation == p.situation) &&
(this.cameraMode == p.cameraMode) &&
Expand All @@ -501,6 +517,7 @@ public bool Equals(Prerequisites p)
(this.minAltitude == p.minAltitude) &&
(this.maxVesselDistance == p.maxVesselDistance) &&
(this.minVesselDistance == p.minVesselDistance) &&
(this.vesselState == p.vesselState) &&
(this.scene == p.scene) &&
(this.situation == p.situation) &&
(this.cameraMode == p.cameraMode) &&
Expand All @@ -524,10 +541,11 @@ public override int GetHashCode()
I = minAltitude,
J = maxVesselDistance,
K = minVesselDistance,
L = scene,
M = situation,
N = cameraMode,
O = bodyName
L = vesselState,
M = scene,
N = situation,
O = cameraMode,
P = bodyName
}.GetHashCode();
}
#endregion Equality operator
Expand Down
4 changes: 2 additions & 2 deletions SoundtrackEditor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.3.0.0")]
[assembly: AssemblyFileVersion("4.3.0.0")]
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("4.5.0.0")]
Loading

0 comments on commit fd760e2

Please sign in to comment.