Skip to content

Commit

Permalink
Merge pull request #3 from artwhaley/AlarmClk
Browse files Browse the repository at this point in the history
Alarm clock
  • Loading branch information
artwhaley authored Aug 28, 2021
2 parents 2f76eba + fc18b85 commit 8fb3600
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 1 deletion.
2 changes: 2 additions & 0 deletions service/SpaceCenter/src/KRPC.SpaceCenter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
<Compile Include="PartHighlightAddon.cs" />
<Compile Include="PilotAddon.cs" />
<Compile Include="ResourceTransferAddon.cs" />
<Compile Include="Services\Alarm.cs" />
<Compile Include="Services\AlarmClock.cs" />
<Compile Include="Services\AutoPilot.cs" />
<Compile Include="Services\Camera.cs" />
<Compile Include="Services\CameraMode.cs" />
Expand Down
143 changes: 143 additions & 0 deletions service/SpaceCenter/src/Services/Alarm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System.Collections.Generic;
using System.Linq;
using KRPC.Service.Attributes;
using KRPC.SpaceCenter.ExtensionMethods;
using KRPC.Utils;

namespace KRPC.SpaceCenter.Services
{
/// <summary>
/// An Alarm. Can be accessed using <see cref="SpaceCenter.AlarmClock"/>.
/// </summary>
[KRPCClass(Service = "SpaceCenter")]
public class Alarm : Equatable<Alarm>
{
/// <summary>
/// Create a alarm object from a KSP alarmTypeBase
/// </summary>
public Alarm(AlarmTypeBase alarm)
{
InternalAlarm = alarm;
}

/// <summary>
/// The KSP Alarm
/// </summary>
public AlarmTypeBase InternalAlarm { get; private set; }

/// <summary>
/// Returns true if the objects are equal.
/// </summary>
public override bool Equals(Alarm other)
{
return !ReferenceEquals(other, null) && InternalAlarm.Id == other.InternalAlarm.Id; //Interna.ContractID == other.InternalContract.ContractID;
}

/// <summary>
/// Hash code for the object.
/// </summary>
public override int GetHashCode()
{
return InternalAlarm.GetHashCode();
}

/// <summary>
/// Type of Alarm
/// </summary>
[KRPCProperty]
public string Type
{
get { UdateAlarm(); return InternalAlarm.TypeName; }
}

/// <summary>
/// Title of the Alarm
/// </summary>
[KRPCProperty]
public string Title
{
get { UdateAlarm(); return InternalAlarm.title; }


}

/// <summary>
/// Description of the contract.
/// </summary>
[KRPCProperty]
public string Description
{
get { UdateAlarm(); return InternalAlarm.description; }
}

/// <summary>
/// Time the Alarm will trigger
/// </summary>
[KRPCProperty]
public double UT
{
get { UdateAlarm(); return InternalAlarm.ut; }
}

/// <summary>
/// Time until the alarm triggers
/// </summary>
[KRPCProperty]
public double TimeTill
{
get { UdateAlarm(); return InternalAlarm.TimeToAlarm; }
}

/// <summary>
/// Seconds betwen the alarm going off and the event it references
/// </summary>
[KRPCProperty]
public double EventOffset
{
get { UdateAlarm(); return InternalAlarm.eventOffset; }
}

/// <summary>
/// Vessel the alarm references
/// </summary>
[KRPCProperty(Nullable = true)]
public Vessel Vessel
{
get
{
UdateAlarm();
KRPC.SpaceCenter.Services.Vessel V = new Vessel(InternalAlarm.Vessel);


return V;
}
}


/// <summary>
/// Unique ID of alarm
/// KSP destroys an old alarm and creates a new one each time an alarm is edited.
/// This ID will remain constant between the old and new alarms though, so this is the value
/// you want to store and each time you want to access an alarm, get the current alarm with the
/// correct ID value.
/// </summary>
[KRPCProperty(Nullable = true)]
public int ID
{
get
{
UdateAlarm();
return (int)InternalAlarm.Id;
}
}


public void UdateAlarm()
{
AlarmTypeBase newalarm;
AlarmClockScenario.TryGetAlarm(InternalAlarm.Id, out newalarm);
InternalAlarm = newalarm;
}

}
}
215 changes: 215 additions & 0 deletions service/SpaceCenter/src/Services/AlarmClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using KRPC.Service.Attributes;
using KRPC.Utils;

namespace KRPC.SpaceCenter.Services
{
/// <summary>
/// Contracts manager.
/// Obtained by calling <see cref="SpaceCenter.ContractManager"/>.
/// </summary>
[KRPCClass(Service = "SpaceCenter")]
public class AlarmClock : Equatable<AlarmClock>
{
internal AlarmClock()
{
}

/// <summary>
/// Returns true if the objects are equal.
/// </summary>
public override bool Equals(AlarmClock other)
{
return !ReferenceEquals(other, null);
}

/// <summary>
/// Hash code for the object.
/// </summary>
public override int GetHashCode()
{
return 0;
}

/// <summary>
/// Make a Simple Alarm
/// Parameter 'time' is the number of seconds from now that the alarm should trigger.
/// </summary>
[KRPCMethod]
public Alarm MakeRawAlarm(double time, string title="Raw Alarm", string description = "")
{
AlarmTypeRaw alarm = new AlarmTypeRaw
{
title = title,
description = description,
actions =
{
warp = AlarmActions.WarpEnum.KillWarp,
message = AlarmActions.MessageEnum.Yes
},
ut = Planetarium.GetUniversalTime() + time
};
AlarmClockScenario.AddAlarm(alarm);
return new Alarm(alarm);
}

/// <summary>
/// Make a Simple Alarm linked to a Vessel
/// Parameter 'time' is the number of seconds from now that the alarm should trigger.
/// </summary>
[KRPCMethod]
public Alarm MakeRawAlarmVessel(double time, Vessel V, string title="Raw Alarm", string description="")
{
AlarmTypeRaw alarm = new AlarmTypeRaw
{
title = title,
description = description,
ut = Planetarium.GetUniversalTime() + time,
actions =
{
warp = AlarmActions.WarpEnum.KillWarp,
message = AlarmActions.MessageEnum.Yes
},
vesselId = V.InternalVessel.persistentId
};
AlarmClockScenario.AddAlarm(alarm);
return new Alarm(alarm);
}


/// <summary>
/// Create an alarm for the given vessel's next Apoapsis
/// </summary>
[KRPCMethod]
public Alarm MakeApaAlarm(Vessel V, double offset = 60, string title="APA Alarm", string description="")
{
AlarmTypeApoapsis alarm = new AlarmTypeApoapsis
{
title = title,
description = description,
eventOffset = offset,
actions =
{
warp = AlarmActions.WarpEnum.KillWarp,
message = AlarmActions.MessageEnum.Yes
},
vesselId = V.InternalVessel.persistentId
};
AlarmClockScenario.AddAlarm(alarm);
return new Alarm(alarm);
}

/// <summary>
/// Create an alarm for the given vessel's next Periapsis
/// </summary>
[KRPCMethod]
public Alarm MakePeaAlarm(Vessel V, double offset = 60, string title="PEA Alarm", string description = "")
{
AlarmTypePeriapsis alarm = new AlarmTypePeriapsis
{
title = title,
description = description,
actions =
{
warp = AlarmActions.WarpEnum.KillWarp,
message = AlarmActions.MessageEnum.Yes
},
vesselId = V.InternalVessel.persistentId
};
alarm.eventOffset = offset;
AlarmClockScenario.AddAlarm(alarm);
return new Alarm(alarm);
}

/// <summary>
/// Create an alarm for the given vessel and maneuver node
/// </summary>
[KRPCMethod]
public Alarm MakeManeuverAlarm(Vessel V, Node Man, double offset = 60, bool AddBurnTime = true, string title="Maneuver Alarm", string description="" )
{
AlarmTypeManeuver alarm = new AlarmTypeManeuver
{
title = title,
description = description,
eventOffset = offset,
useBurnTimeMargin = AddBurnTime,
actions =
{
warp = AlarmActions.WarpEnum.KillWarp,
message = AlarmActions.MessageEnum.Yes
},
vesselId = V.InternalVessel.persistentId,
Maneuver = Man.InternalNode
};

AlarmClockScenario.AddAlarm(alarm);
return new Alarm(alarm);
}

/// <summary>
/// Create an alarm for the given vessel's next SOI change
/// </summary>
[KRPCMethod]
public Alarm MakeSOIAlarm(Vessel V, double offset = 60, string title="SOI Change", string description="" )
{
AlarmTypeSOI alarm = new AlarmTypeSOI
{
title = title,
description = description,
eventOffset = offset,
actions =
{
warp = AlarmActions.WarpEnum.KillWarp,
message = AlarmActions.MessageEnum.Yes
},
vesselId = V.InternalVessel.persistentId
};
AlarmClockScenario.AddAlarm(alarm);
return new Alarm(alarm);
}

//Not working - to be dealt with later
///// <summary>
///// Create an alarm for the given vessel's next transfer window
///// </summary>
//[KRPCMethod]
//public Alarm MakeWindowAlarm(Vessel V, CelestialBody Target, string title="Transfer Window", string description="")
//{
// AlarmTypeTransferWindow alarm = new AlarmTypeTransferWindow
// {
// title = title,
// description = description,
// actions =
// {
// warp = AlarmActions.WarpEnum.KillWarp,
// message = AlarmActions.MessageEnum.Yes
// },
// vesselId = V.InternalVessel.persistentId,
// dest = Target.InternalBody,
// source = V.InternalVessel.orbit.referenceBody
//};

// AlarmClockScenario.AddAlarm(alarm);
// return new Alarm(alarm);
//}

/// <summary>
/// Returns a list of all alarms
/// </summary>
[KRPCMethod]
public IList<Alarm> GetAlarms()
{
IList<Alarm> Alarms = new List<Alarm>();
for (int i = 0; i < AlarmClockScenario.Instance.alarms.Count; i++)
{
AlarmTypeBase a = AlarmClockScenario.Instance.alarms.At(i);
Alarm A = new Alarm(a);
Alarms.Add(A);

}
return Alarms;
}
}
}
10 changes: 9 additions & 1 deletion service/SpaceCenter/src/Services/SpaceCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,15 @@ public static ContractManager ContractManager
get { return new ContractManager(); }
}


/// <summary>
/// The Alarm Clock Module.
/// </summary>
[KRPCProperty]
public static AlarmClock AlarmClock
{
get { return new AlarmClock(); }
}

static string GetFullCraftDirectory (string craftDirectory)
{
return KSPUtil.ApplicationRootPath + "saves/" + HighLogic.SaveFolder + "/Ships/" + craftDirectory;
Expand Down

0 comments on commit 8fb3600

Please sign in to comment.