Skip to content

Commit

Permalink
Remove double counters for gameplay SliderPath
Browse files Browse the repository at this point in the history
Parsed `SliderPath.calculatedPath` and `SliderPath.cumulativeLength` are guaranteed to be of the same length.
  • Loading branch information
Rian8337 committed Feb 2, 2025
1 parent f39101f commit bfc90df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 40 deletions.
43 changes: 10 additions & 33 deletions src/ru/nsu/ccfit/zuev/osu/game/GameHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;

import ru.nsu.ccfit.zuev.osu.Constants;
import ru.nsu.ccfit.zuev.skins.OsuSkin;
Expand Down Expand Up @@ -59,11 +58,11 @@ public static void setOverallDifficulty(final float overallDifficulty) {
public static LinePath convertSliderPath(final SliderPath sliderPath) {
var renderPath = new LinePath();

if (sliderPath.pointCount == 0) {
if (sliderPath.anchorCount == 0) {
return renderPath;
}

for (int i = 0; i < sliderPath.pointCount; ++i) {
for (int i = 0; i < sliderPath.anchorCount; ++i) {

var x = sliderPath.getX(i);
var y = sliderPath.getY(i);
Expand All @@ -72,7 +71,7 @@ public static LinePath convertSliderPath(final SliderPath sliderPath) {
}

renderPath.measure();
renderPath.bufferLength(sliderPath.getLength(sliderPath.lengthCount - 1));
renderPath.bufferLength(sliderPath.getLength(sliderPath.anchorCount - 1));
renderPath = renderPath.fitToLinePath();
renderPath.measure();

Expand All @@ -93,16 +92,10 @@ public static SliderPath convertSliderPath(final Slider slider) {
float realWidthScale = (float) Constants.MAP_ACTUAL_WIDTH / Constants.MAP_WIDTH;
float realHeightScale = (float) Constants.MAP_ACTUAL_HEIGHT / Constants.MAP_HEIGHT;

for (var i = 0; i < calculatedPath.size(); i++) {

for (int i = 0; i < calculatedPath.size(); i++) {
var p = calculatedPath.get(i);
path.setPoint(i, p.x * realWidthScale, p.y * realHeightScale);

if (i < cumulativeLength.size()) {
path.setLength(i, cumulativeLength.get(i).floatValue());
} else {
path.setLength(i, -1f);
}
path.set(i, p.x * realWidthScale, p.y * realHeightScale, cumulativeLength.get(i).floatValue());
}

return path;
Expand Down Expand Up @@ -284,36 +277,20 @@ public static class SliderPath {
private static final int offsetY = 1;
private static final int offsetLength = 2;

private float[] data;

public int lengthCount = 0;
public int pointCount = 0;
private final float[] data;
public int anchorCount = 0;

public SliderPath(int anchorPointCount) {
data = new float[anchorPointCount * strip];
}

public void setPoint(int index, float x, float y) {
public void set(int index, float x, float y, float length) {
data[index * strip + offsetX] = x;
data[index * strip + offsetY] = y;
pointCount++;
}

public void setLength(int index, float length) {

var targetIndex = index * strip + offsetLength;

// This condition can be triggered if there's a mismatch between the number of points
// and the number of lengths. Should never happen in practice, but it's better to be safe.
if (targetIndex >= data.length) {
data = Arrays.copyOf(data, data.length + strip);
}

data[targetIndex] = length;
lengthCount++;
data[index * strip + offsetLength] = length;
anchorCount++;
}


public float getX(int index) {
return data[index * strip + offsetX];
}
Expand Down
14 changes: 7 additions & 7 deletions src/ru/nsu/ccfit/zuev/osu/game/GameplaySlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void init(final GameObjectListener listener, final Scene scene,
}

// End circle
pathEndPosition.set(getAbsolutePathPosition(path.pointCount - 1));
pathEndPosition.set(getAbsolutePathPosition(path.anchorCount - 1));

tailCirclePiece.setScale(scale);
tailCirclePiece.setCircleColor(comboColor.r(), comboColor.g(), comboColor.b());
Expand Down Expand Up @@ -288,7 +288,7 @@ public void init(final GameObjectListener listener, final Scene scene,
endArrow.setAlpha(0);
endArrow.setScale(scale);

PointF previousPoint = getAbsolutePathPosition(path.pointCount - 2);
PointF previousPoint = getAbsolutePathPosition(path.anchorCount - 2);
endArrow.setRotation(MathUtils.radToDeg(Utils.direction(pathEndPosition.x, pathEndPosition.y, previousPoint.x, previousPoint.y)));

if (Config.isSnakingInSliders()) {
Expand Down Expand Up @@ -384,7 +384,7 @@ public void init(final GameObjectListener listener, final Scene scene,
}

private PointF getPositionAt(final float percentage, final boolean updateBallAngle, final boolean updateEndArrowRotation) {
if (path.pointCount < 2) {
if (path.anchorCount < 2) {
tmpPoint.set(position);
return tmpPoint;
}
Expand All @@ -411,8 +411,8 @@ private PointF getPositionAt(final float percentage, final boolean updateBallAng

// Directly taken from library-owned SliderPath
int left = 0;
int right = path.lengthCount - 2;
float currentLength = percentage * path.getLength(path.lengthCount - 1);
int right = path.anchorCount - 2;
float currentLength = percentage * path.getLength(path.anchorCount - 1);

while (left <= right) {
int pivot = left + ((right - left) >> 1);
Expand Down Expand Up @@ -806,8 +806,8 @@ public void update(final float dt) {
preStageFinish = true;
}

if (path.pointCount >= 2) {
PointF lastPoint = getAbsolutePathPosition(path.pointCount - 2);
if (path.anchorCount >= 2) {
PointF lastPoint = getAbsolutePathPosition(path.anchorCount - 2);
endArrow.setRotation(MathUtils.radToDeg(Utils.direction(pathEndPosition.x, pathEndPosition.y, lastPoint.x, lastPoint.y)));
}

Expand Down

0 comments on commit bfc90df

Please sign in to comment.