Skip to content

Commit

Permalink
Add breakpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Mar 1, 2024
1 parent 38c4f4a commit f87dc9a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
36 changes: 36 additions & 0 deletions Source/TAS/Input/FastForward.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;

namespace Celeste64.TAS.Input;

public record FastForward {
private const float DefaultSpeed = 400f;
public const float MinSpeed = 1f / 60f;

public readonly int Frame;
public readonly int Line;
public readonly bool SaveState;
public readonly float Speed;

public FastForward(int frame, string modifiers, int line) {
Frame = frame;
Line = line;

if (modifiers.StartsWith("s", StringComparison.OrdinalIgnoreCase)) {
SaveState = true;
modifiers = modifiers[1..];
} else {
SaveState = false;
}

Speed = float.TryParse(modifiers, out float speed) ? speed : DefaultSpeed;
if (Speed < MinSpeed) {
Speed = MinSpeed;
} else if (Speed > 1f) {
Speed = (int) Math.Round(Speed);
}
}

public override string ToString() {
return "***" + (SaveState ? "S" : "") + Speed;
}
}
27 changes: 18 additions & 9 deletions Source/TAS/Input/InputController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class InputController
{
public readonly List<InputFrame> Inputs = new();
public readonly SortedDictionary<int, List<Command>> Commands = new();
public readonly SortedDictionary<int, FastForward> FastForwards = new();
// public readonly SortedDictionary<int, FastForward> FastForwardLabel = new();
// public readonly Dictionary<string, List<Comment>> Labels = new();

private static readonly Dictionary<string, FileSystemWatcher> watchers = new();
private readonly HashSet<string> usedFiles = new();
Expand All @@ -29,7 +32,12 @@ public class InputController
public InputFrame? Previous => Inputs!.GetValueOrDefault(CurrentFrameInTas - 1);
public InputFrame? Current => Inputs!.GetValueOrDefault(CurrentFrameInTas);
public InputFrame? Next => Inputs!.GetValueOrDefault(CurrentFrameInTas + 1);

public List<Command>? CurrentCommands => Commands.GetValueOrDefault(CurrentFrameInTas);
public FastForward? CurrentFastForward => FastForwards.FirstOrDefault(pair => pair.Key > CurrentFrameInTas).Value ??
FastForwards.LastOrDefault().Value;

public bool ShouldFastForward => CurrentFastForward is { } forward && forward.Frame > CurrentFrameInTas;

public bool CanPlayback => CurrentFrameInTas < Inputs.Count;
public bool NeedsToWait => Manager.IsLoading();
Expand Down Expand Up @@ -199,15 +207,16 @@ public void ReadLines(IEnumerable<string> lines, string filePath, int startLine,
// return;
// }

// if (lineText.StartsWith("***")) {
// FastForward fastForward = new(initializationFrameCount, lineText.Substring(3), studioLine);
// if (FastForwards.TryGetValue(initializationFrameCount, out FastForward oldFastForward) && oldFastForward.SaveState &&
// !fastForward.SaveState) {
// // ignore
// } else {
// FastForwards[initializationFrameCount] = fastForward;
// }
// } else if (lineText.StartsWith("#")) {
// Breakpoints
if (lineText.StartsWith("***")) {
var fastForward = new FastForward(initializationFrameCount, lineText[3..], studioLine);
if (FastForwards.TryGetValue(initializationFrameCount, out var oldFastForward) && oldFastForward.SaveState && !fastForward.SaveState) {
// ignore
} else {
FastForwards[initializationFrameCount] = fastForward;
}
}
// else if (lineText.StartsWith("#")) {
// FastForwardComments[initializationFrameCount] = new FastForward(initializationFrameCount, "", studioLine);
// if (!Comments.TryGetValue(filePath, out var comments)) {
// Comments[filePath] = comments = new List<Comment>();
Expand Down
12 changes: 12 additions & 0 deletions Source/TAS/TASMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ private static void On_App_Tick_Update(orig_App_Tick_Update orig, TimeSpan delta
TASMod.Update();
orig(delta);
}

if (Manager.Running && Manager.Controller.ShouldFastForward)
{
// Fast forward to breakpoint
while (Manager.Running && Manager.Controller.ShouldFastForward)
{
TASMod.Update();
orig(delta);
}
// And pause after that
Manager.NextState = Manager.State.Paused;
}
}

private delegate void orig_Input_Step();
Expand Down

0 comments on commit f87dc9a

Please sign in to comment.