Skip to content
Merged
4 changes: 2 additions & 2 deletions Assets/__Scripts/Beatmap/Animations/ObjectAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void SetData(BaseGrid obj)
{
continue;
}
var bpmChangeGridContainer = BeatmapObjectContainerCollection.GetCollectionForType<BPMChangeGridContainer>(ObjectType.BpmChange);
var map = BeatSaberSongContainer.Instance.Map;
foreach (var ce in events.Where(ev => ev.Type == "AssignPathAnimation"))
{
foreach (var jprop in ce.Data)
Expand All @@ -196,7 +196,7 @@ public void SetData(BaseGrid obj)
TimeEnd = time_end,
};
if (p.Transition != 0) {
p.Transition = bpmChangeGridContainer.JsonTimeToSongBpmTime(ce.JsonTime + p.Transition) - ce.SongBpmTime;
p.Transition = (float)map.JsonTimeToSongBpmTime(ce.JsonTime + p.Transition) - ce.SongBpmTime;
}
AddPointDef(p, jprop.Key);
}
Expand Down
8 changes: 4 additions & 4 deletions Assets/__Scripts/Beatmap/Base/BaseArc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public BaseArc()

public BaseArc(BaseArc other)
{
SetTimes(other.JsonTime, other.SongBpmTime);
SetTimes(other.JsonTime);
Color = other.Color;
PosX = other.PosX;
PosY = other.PosY;
CutDirection = other.CutDirection;
HeadControlPointLengthMultiplier = other.HeadControlPointLengthMultiplier;
SetTailTimes(other.TailJsonTime, other.TailSongBpmTime);
SetTailTimes(other.TailJsonTime);
TailPosX = other.TailPosX;
TailPosY = other.TailPosY;
TailCutDirection = other.TailCutDirection;
Expand All @@ -52,13 +52,13 @@ public BaseArc(BaseArc other)

public BaseArc(BaseNote start, BaseNote end)
{
SetTimes(start.JsonTime, start.SongBpmTime);
SetTimes(start.JsonTime);
Color = start.Color;
PosX = start.PosX;
PosY = start.PosY;
CutDirection = start.CutDirection;
HeadControlPointLengthMultiplier = 1f;
SetTailTimes(end.JsonTime, end.SongBpmTime);
SetTailTimes(end.JsonTime);
TailPosX = end.PosX;
TailPosY = end.PosY;
TailCutDirection = end.CutDirection;
Expand Down
2 changes: 1 addition & 1 deletion Assets/__Scripts/Beatmap/Base/BaseBpmEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public BaseBpmEvent() {}

public BaseBpmEvent(BaseBpmEvent other)
{
SetTimes(other.JsonTime, other.SongBpmTime);
SetTimes(other.JsonTime);
Bpm = other.Bpm;
CustomData = other.CustomData.Clone();
}
Expand Down
8 changes: 4 additions & 4 deletions Assets/__Scripts/Beatmap/Base/BaseChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public BaseChain()

public BaseChain(BaseChain other)
{
SetTimes(other.JsonTime, other.SongBpmTime);
SetTimes(other.JsonTime);
Color = other.Color;
PosX = other.PosX;
PosY = other.PosY;
CutDirection = other.CutDirection;
SetTailTimes(other.TailJsonTime, other.TailSongBpmTime);
SetTailTimes(other.TailJsonTime);
TailPosX = other.TailPosX;
TailPosY = other.TailPosY;
SliceCount = other.SliceCount;
Expand All @@ -48,12 +48,12 @@ public BaseChain(BaseChain other)

public BaseChain(BaseNote start, BaseNote end)
{
SetTimes(start.JsonTime, start.SongBpmTime);
SetTimes(start.JsonTime);
Color = start.Color;
PosX = start.PosX;
PosY = start.PosY;
CutDirection = start.CutDirection;
SetTailTimes(end.JsonTime, end.SongBpmTime);
SetTailTimes(end.JsonTime);
TailPosX = end.PosX;
TailPosY = end.PosY;
SliceCount = 5;
Expand Down
96 changes: 96 additions & 0 deletions Assets/__Scripts/Beatmap/Base/BaseDifficulty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,102 @@ public List<BaseLightTranslationEventBoxGroup<BaseLightTranslationEventBox>>
new List<BaseObject>(CustomEvents),
};

#region BPM Time Conversion Logic

private float? songBpm;

public void BootstrapBpmEvents(float songBpm)
{
// remove invalid bpm events
BpmEvents.RemoveAll(x => x.JsonTime < 0);
BpmEvents.RemoveAll(x => x.Bpm < 0);

if (!BpmEvents.Any()) return;

BpmEvents.Sort();

// insert beat 0 bpm event if needed
if (BpmEvents.First().JsonTime > 0)
{
var newBpmEvent = new BaseBpmEvent(0, songBpm);
BpmEvents.Insert(0, newBpmEvent);
}

BaseBpmEvent lastBpmEvent = null;
foreach (var bpmEvent in BpmEvents)
{
if (lastBpmEvent is null)
{
bpmEvent.songBpmTime = bpmEvent.JsonTime;
}
else
{
bpmEvent.songBpmTime = lastBpmEvent.songBpmTime + (bpmEvent.JsonTime - lastBpmEvent.JsonTime) * (songBpm / lastBpmEvent.Bpm);
}

lastBpmEvent = bpmEvent;
}

this.songBpm = songBpm;
}

public float? JsonTimeToSongBpmTime(float jsonTime)
{
if (songBpm is null) return null;
var lastBpmEvent = FindLastBpmEventByJsonTime(jsonTime, inclusive: false);
if (lastBpmEvent is null)
{
return jsonTime;
}
return lastBpmEvent.SongBpmTime + (jsonTime - lastBpmEvent.JsonTime) * (songBpm / lastBpmEvent.Bpm);
}

public float? SongBpmTimeToJsonTime(float songBpmTime)
{
if (songBpm is null) return null;
var lastBpmEvent = FindLastBpmEventBySongBpmTime(songBpmTime, inclusive: false);
if (lastBpmEvent is null)
{
return songBpmTime;
}
return lastBpmEvent.JsonTime + (songBpmTime - lastBpmEvent.SongBpmTime) * (lastBpmEvent.Bpm / songBpm);
}

public BaseBpmEvent FindLastBpmEventByJsonTime(float jsonTime, bool inclusive = false)
{
return BpmEvents.LastOrDefault(x => inclusive ? x.JsonTime <= jsonTime : x.JsonTime < jsonTime);
}

public BaseBpmEvent FindLastBpmEventBySongBpmTime(float songBpmTime, bool inclusive = false)
{
if (songBpm is null) return null;
return BpmEvents.LastOrDefault(x => inclusive ? x.SongBpmTime <= songBpmTime : x.SongBpmTime < songBpmTime);
}

public float? BpmAtJsonTime(float jsonTime)
{
return FindLastBpmEventByJsonTime(jsonTime, inclusive: true)?.Bpm ?? songBpm;
}

public float? BpmAtSongBpmTime(float songBpmTime)
{
return FindLastBpmEventBySongBpmTime(songBpmTime, inclusive: true)?.Bpm ?? songBpm;
}

public void RecomputeAllObjectSongBpmTimes()
{
foreach (var objList in AllBaseObjectProperties())
{
if (objList is null) continue;
foreach (var obj in objList)
{
obj.RecomputeSongBpmTime();
}
}
}

#endregion

public void ConvertCustomBpmToOfficial()
{
var songBpm = BeatSaberSongContainer.Instance.Info.BeatsPerMinute;
Expand Down
2 changes: 1 addition & 1 deletion Assets/__Scripts/Beatmap/Base/BaseEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public BaseEvent()

public BaseEvent(BaseEvent other)
{
SetTimes(other.JsonTime, other.SongBpmTime);
SetTimes(other.JsonTime);
Type = other.Type;
Value = other.Value;
FloatValue = other.FloatValue;
Expand Down
4 changes: 2 additions & 2 deletions Assets/__Scripts/Beatmap/Base/BaseGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ protected BaseGrid(float jsonTime, float songBpmTime, int posX, int posY, JSONNo

public float EditorScale { get; private set; }

public virtual float SpawnSongBpmTime { get { return SongBpmTime - Hjd; } }
public virtual float DespawnSongBpmTime { get { return SongBpmTime + Hjd; } }
public virtual float SpawnSongBpmTime => SongBpmTime - Hjd;
public virtual float DespawnSongBpmTime => SongBpmTime + Hjd;

public virtual JSONNode CustomAnimation { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Assets/__Scripts/Beatmap/Base/BaseNJSEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public BaseNJSEvent()

public BaseNJSEvent(BaseNJSEvent other)
{
SetTimes(other.JsonTime, other.SongBpmTime);
SetTimes(other.JsonTime);
UsePrevious = other.UsePrevious;
Easing = other.Easing;
RelativeNJS = other.RelativeNJS;
Expand Down
2 changes: 1 addition & 1 deletion Assets/__Scripts/Beatmap/Base/BaseNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public BaseNote()

public BaseNote(BaseNote other)
{
SetTimes(other.JsonTime, other.SongBpmTime);
SetTimes(other.JsonTime);
PosX = other.PosX;
PosY = other.PosY;
Color = other.Color;
Expand Down
25 changes: 9 additions & 16 deletions Assets/__Scripts/Beatmap/Base/BaseObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class BaseObject : BaseItem, ICustomData, IHeckObject, IChromaOb
public virtual void Serialize(NetDataWriter writer)
{
writer.Put(jsonTime);
writer.Put(songBpmTime);
writer.Put((float)songBpmTime);
writer.Put(CustomData?.ToString());
}

Expand Down Expand Up @@ -49,28 +49,21 @@ public float JsonTime
get => jsonTime;
set
{
var bpmChangeGridContainer = BeatmapObjectContainerCollection.GetCollectionForType<BPMChangeGridContainer>(ObjectType.BpmChange);
songBpmTime = bpmChangeGridContainer?.JsonTimeToSongBpmTime(value) ?? value;
var map = BeatSaberSongContainer.Instance.Map;
songBpmTime = map?.JsonTimeToSongBpmTime(value);
jsonTime = value;
}
}

private float songBpmTime;
public float SongBpmTime
{
get => songBpmTime;
set
{
var bpmChangeGridContainer = BeatmapObjectContainerCollection.GetCollectionForType<BPMChangeGridContainer>(ObjectType.BpmChange);
jsonTime = bpmChangeGridContainer?.SongBpmTimeToJsonTime(value) ?? value;
songBpmTime = value;
}
}
// should only be set directly when initializing
// read from SongBpmTime instead, and write to JsonTime to update this
// really should be private but we need to set this from BaseDifficulty on init
internal float? songBpmTime;
public float SongBpmTime => (float)songBpmTime;

public void SetTimes(float jsonTime, float songBpmTime)
public void SetTimes(float jsonTime)
{
this.jsonTime = jsonTime;
this.songBpmTime = songBpmTime;
RecomputeSongBpmTime();
}

Expand Down
24 changes: 17 additions & 7 deletions Assets/__Scripts/Beatmap/Base/BaseObstacle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public BaseObstacle()

private BaseObstacle(BaseObstacle other)
{
SetTimes(other.JsonTime, other.SongBpmTime);
SetTimes(other.JsonTime);
PosX = other.PosX;
InternalPosY = other.PosY;
InternalType = other.Type;
Expand Down Expand Up @@ -99,11 +99,22 @@ public int Height
set => InternalHeight = value;
}

public float Duration { get; set; }
public float DurationSongBpm { get; set; }
private float duration;
public float Duration
{
get => duration;
set
{
var map = BeatSaberSongContainer.Instance.Map;
durationSongBpm = map?.JsonTimeToSongBpmTime(value + JsonTime) - songBpmTime;
duration = value;
}
}
private float? durationSongBpm;
public float DurationSongBpm => (float)durationSongBpm;
public int Width { get; set; }
public override float DespawnSongBpmTime { get { return SongBpmTime + DurationSongBpm + Hjd; } }

public override float DespawnSongBpmTime => SongBpmTime + DurationSongBpm + Hjd;

public virtual JSONNode CustomSize { get; set; }

Expand Down Expand Up @@ -277,8 +288,7 @@ public ObstacleBounds GetShape()
public override void RecomputeSongBpmTime()
{
base.RecomputeSongBpmTime();
DurationSongBpm = (BeatmapObjectContainerCollection.GetCollectionForType<BPMChangeGridContainer>(ObjectType.BpmChange)
?.JsonTimeToSongBpmTime(JsonTime + Duration) ?? (JsonTime + Duration)) - SongBpmTime;
Duration = Duration;
}

protected void InferType() =>
Expand Down
27 changes: 8 additions & 19 deletions Assets/__Scripts/Beatmap/Base/BaseSlider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,17 @@ public float TailJsonTime
get => tailJsonTime;
set
{
var bpmChangeGridContainer = BeatmapObjectContainerCollection.GetCollectionForType<BPMChangeGridContainer>(ObjectType.BpmChange);
tailSongBpmTime = bpmChangeGridContainer?.JsonTimeToSongBpmTime(value) ?? value;
var map = BeatSaberSongContainer.Instance.Map;
tailSongBpmTime = map?.JsonTimeToSongBpmTime(value);
tailJsonTime = value;
}
}
private float tailSongBpmTime { get; set; }
public float TailSongBpmTime
{
get => tailSongBpmTime;
set
{
var bpmChangeGridContainer = BeatmapObjectContainerCollection.GetCollectionForType<BPMChangeGridContainer>(ObjectType.BpmChange);
tailJsonTime = bpmChangeGridContainer?.SongBpmTimeToJsonTime(value) ?? value;
tailSongBpmTime = value;
}
}
private float? tailSongBpmTime;
public float TailSongBpmTime => (float)tailSongBpmTime;

public void SetTailTimes(float jsonTime, float songBpmTime)
public void SetTailTimes(float jsonTime)
{
this.tailJsonTime = jsonTime;
this.tailSongBpmTime = songBpmTime;
TailJsonTime = jsonTime;
}

public int TailPosX { get; set; }
Expand Down Expand Up @@ -140,9 +130,8 @@ public override void Apply(BaseObject originalData)
public virtual void SwapHeadAndTail()
{
var tempJsonTime = JsonTime;
var tempJsonSongBpmTime = SongBpmTime;
SetTimes(tailJsonTime, tailSongBpmTime);
SetTailTimes(tempJsonTime, tempJsonSongBpmTime);
SetTimes(tailJsonTime);
SetTailTimes(tempJsonTime);
(PosX, TailPosX) = (TailPosX, PosX);
(PosY, TailPosY) = (TailPosY, PosY);
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/__Scripts/Beatmap/Base/BaseWaypoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public BaseWaypoint()

public BaseWaypoint(BaseWaypoint other)
{
SetTimes(other.JsonTime, other.SongBpmTime);
SetTimes(other.JsonTime);
PosX = other.PosX;
PosY = other.PosY;
OffsetDirection = other.OffsetDirection;
Expand Down
2 changes: 1 addition & 1 deletion Assets/__Scripts/Beatmap/Base/Customs/BaseBookmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public BaseBookmark()

protected BaseBookmark(BaseBookmark other)
{
SetTimes(other.JsonTime, other.SongBpmTime);
SetTimes(other.JsonTime);
Name = other.Name;
Color = other.Color;
}
Expand Down
Loading