1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-07 02:37:25 +08:00
osu-lazer/osu.Game.Rulesets.Mania/Replays/ManiaAutoGenerator.cs

131 lines
4.2 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
using System.Linq;
2018-11-28 16:20:37 +08:00
using osu.Game.Replays;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Mania.Beatmaps;
2019-08-19 22:18:25 +08:00
using osu.Game.Rulesets.Objects;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Replays;
namespace osu.Game.Rulesets.Mania.Replays
{
2019-03-07 17:30:31 +08:00
internal class ManiaAutoGenerator : AutoGenerator
2018-04-13 17:19:50 +08:00
{
public const double RELEASE_DELAY = 20;
public new ManiaBeatmap Beatmap => (ManiaBeatmap)base.Beatmap;
private readonly ManiaAction[] columnActions;
public ManiaAutoGenerator(ManiaBeatmap beatmap)
: base(beatmap)
{
2018-11-29 12:22:45 +08:00
Replay = new Replay();
2018-04-13 17:19:50 +08:00
columnActions = new ManiaAction[Beatmap.TotalColumns];
var normalAction = ManiaAction.Key1;
var specialAction = ManiaAction.Special1;
int totalCounter = 0;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
foreach (var stage in Beatmap.Stages)
{
for (int i = 0; i < stage.Columns; i++)
{
if (stage.IsSpecialColumn(i))
columnActions[totalCounter] = specialAction++;
else
columnActions[totalCounter] = normalAction++;
totalCounter++;
}
}
}
protected Replay Replay;
public override Replay Generate()
{
var pointGroups = generateActionPoints().GroupBy(a => a.Time).OrderBy(g => g.First().Time);
var actions = new List<ManiaAction>();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
foreach (var group in pointGroups)
{
foreach (var point in group)
{
switch (point)
{
case HitPoint _:
actions.Add(columnActions[point.Column]);
break;
2019-04-01 11:16:05 +08:00
case ReleasePoint _:
actions.Remove(columnActions[point.Column]);
break;
}
2018-04-13 17:19:50 +08:00
}
// todo: can be removed once FramedReplayInputHandler correctly handles rewinding before first frame.
if (Replay.Frames.Count == 0)
Replay.Frames.Add(new ManiaReplayFrame(group.First().Time - 1));
2018-04-13 17:19:50 +08:00
Replay.Frames.Add(new ManiaReplayFrame(group.First().Time, actions.ToArray()));
}
return Replay;
}
private IEnumerable<IActionPoint> generateActionPoints()
{
for (int i = 0; i < Beatmap.HitObjects.Count; i++)
2018-04-13 17:19:50 +08:00
{
var currentObject = Beatmap.HitObjects[i];
2019-08-20 02:45:23 +08:00
var nextObjectInColumn = GetNextObject(i); // Get the next object that requires pressing the same button
double endTime = currentObject.GetEndTime();
2019-08-20 02:45:23 +08:00
bool canDelayKeyUp = nextObjectInColumn == null ||
nextObjectInColumn.StartTime > endTime + RELEASE_DELAY;
2019-08-20 02:45:23 +08:00
double calculatedDelay = canDelayKeyUp ? RELEASE_DELAY : (nextObjectInColumn.StartTime - endTime) * 0.9;
yield return new HitPoint { Time = currentObject.StartTime, Column = currentObject.Column };
2019-08-16 18:50:48 +08:00
yield return new ReleasePoint { Time = endTime + calculatedDelay, Column = currentObject.Column };
}
2019-08-19 22:18:25 +08:00
}
2019-08-19 22:18:25 +08:00
protected override HitObject GetNextObject(int currentIndex)
{
2019-08-26 16:45:12 +08:00
int desiredColumn = Beatmap.HitObjects[currentIndex].Column;
2019-08-26 16:45:12 +08:00
for (int i = currentIndex + 1; i < Beatmap.HitObjects.Count; i++)
2019-08-19 22:18:25 +08:00
{
2019-08-26 16:45:12 +08:00
if (Beatmap.HitObjects[i].Column == desiredColumn)
return Beatmap.HitObjects[i];
2018-04-13 17:19:50 +08:00
}
2019-08-19 22:18:25 +08:00
return null;
2018-04-13 17:19:50 +08:00
}
private interface IActionPoint
{
double Time { get; set; }
int Column { get; set; }
}
private struct HitPoint : IActionPoint
{
public double Time { get; set; }
public int Column { get; set; }
}
private struct ReleasePoint : IActionPoint
{
public double Time { get; set; }
public int Column { get; set; }
}
}
}