osu/osu.Game.Rulesets.Catch/Replays/CatchAutoGenerator.cs

137 lines
5.0 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 09:19:50 +00:00
using System;
using System.Linq;
2020-01-09 04:43:44 +00:00
using osu.Framework.Utils;
2018-04-13 09:19:50 +00:00
using osu.Game.Beatmaps;
2018-11-28 08:20:37 +00:00
using osu.Game.Replays;
2019-03-07 09:30:31 +00:00
using osu.Game.Rulesets.Catch.Beatmaps;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Catch.Objects;
using osu.Game.Rulesets.Catch.UI;
using osu.Game.Rulesets.Replays;
namespace osu.Game.Rulesets.Catch.Replays
{
2019-03-07 09:30:31 +00:00
internal class CatchAutoGenerator : AutoGenerator
2018-04-13 09:19:50 +00:00
{
public const double RELEASE_DELAY = 20;
2019-03-07 09:30:31 +00:00
public new CatchBeatmap Beatmap => (CatchBeatmap)base.Beatmap;
public CatchAutoGenerator(IBeatmap beatmap)
2018-04-13 09:19:50 +00:00
: base(beatmap)
{
2018-11-29 04:22:45 +00:00
Replay = new Replay();
2018-04-13 09:19:50 +00:00
}
protected Replay Replay;
private CatchReplayFrame currentFrame;
2018-04-13 09:19:50 +00:00
public override Replay Generate()
{
// todo: add support for HT DT
const double dash_speed = Catcher.BASE_SPEED;
2018-04-13 09:19:50 +00:00
const double movement_speed = dash_speed / 2;
float lastPosition = 0.5f;
double lastTime = 0;
void moveToNext(CatchHitObject h)
{
float positionChange = Math.Abs(lastPosition - h.X);
double timeAvailable = h.StartTime - lastTime;
2019-06-15 20:41:10 +00:00
// So we can either make it there without a dash or not.
// If positionChange is 0, we don't need to move, so speedRequired should also be 0 (could be NaN if timeAvailable is 0 too)
// The case where positionChange > 0 and timeAvailable == 0 results in PositiveInfinity which provides expected beheaviour.
2019-06-15 17:14:24 +00:00
double speedRequired = positionChange == 0 ? 0 : positionChange / timeAvailable;
2018-04-13 09:19:50 +00:00
bool dashRequired = speedRequired > movement_speed;
bool impossibleJump = speedRequired > movement_speed * 2;
2018-04-13 09:19:50 +00:00
// todo: get correct catcher size, based on difficulty CS.
const float catcher_width_half = CatcherArea.CATCHER_SIZE / CatchPlayfield.BASE_WIDTH * 0.3f * 0.5f;
if (lastPosition - catcher_width_half < h.X && lastPosition + catcher_width_half > h.X)
{
2020-05-05 01:31:11 +00:00
// we are already in the correct range.
2018-04-13 09:19:50 +00:00
lastTime = h.StartTime;
addFrame(h.StartTime, lastPosition);
2018-04-13 09:19:50 +00:00
return;
}
if (impossibleJump)
2018-04-13 09:19:50 +00:00
{
addFrame(h.StartTime, h.X);
2018-04-13 09:19:50 +00:00
}
else if (h.HyperDash)
{
addFrame(h.StartTime - timeAvailable, lastPosition);
addFrame(h.StartTime, h.X);
2018-04-13 09:19:50 +00:00
}
else if (dashRequired)
{
2020-05-05 01:31:11 +00:00
// we do a movement in two parts - the dash part then the normal part...
2018-04-13 09:19:50 +00:00
double timeAtNormalSpeed = positionChange / movement_speed;
double timeWeNeedToSave = timeAtNormalSpeed - timeAvailable;
double timeAtDashSpeed = timeWeNeedToSave / 2;
float midPosition = (float)Interpolation.Lerp(lastPosition, h.X, (float)timeAtDashSpeed / timeAvailable);
2020-05-05 01:31:11 +00:00
// dash movement
addFrame(h.StartTime - timeAvailable + 1, lastPosition, true);
addFrame(h.StartTime - timeAvailable + timeAtDashSpeed, midPosition);
addFrame(h.StartTime, h.X);
2018-04-13 09:19:50 +00:00
}
else
{
double timeBefore = positionChange / movement_speed;
addFrame(h.StartTime - timeBefore, lastPosition);
addFrame(h.StartTime, h.X);
2018-04-13 09:19:50 +00:00
}
lastTime = h.StartTime;
lastPosition = h.X;
}
foreach (var obj in Beatmap.HitObjects)
{
switch (obj)
{
case Fruit _:
moveToNext(obj);
break;
}
foreach (var nestedObj in obj.NestedHitObjects.Cast<CatchHitObject>())
{
switch (nestedObj)
{
case Banana _:
2018-04-13 09:19:50 +00:00
case TinyDroplet _:
case Droplet _:
case Fruit _:
moveToNext(nestedObj);
break;
}
}
}
return Replay;
}
private void addFrame(double time, float? position = null, bool dashing = false)
{
// todo: can be removed once FramedReplayInputHandler correctly handles rewinding before first frame.
if (Replay.Frames.Count == 0)
Replay.Frames.Add(new CatchReplayFrame(time - 1, position, false, null));
var last = currentFrame;
currentFrame = new CatchReplayFrame(time, position, dashing, last);
Replay.Frames.Add(currentFrame);
}
2018-04-13 09:19:50 +00:00
}
}