Game state bug fixes and better error handling.

Error handling (related to #53):
- Catch all exceptions caused by creating HitObjects and throw a more informative error with the hit object information. This also allows multiple errors to be reported, instead of crashing at the first error.
- Added a fallback "DummyObject" HitObject to replace hit objects causing errors, which should allow the game to function despite any errors.
- Added a toString() method to OsuHitObject (resembling the raw format).

Bug fixes (caused by #52):
- Game is no longer paused when focus is lost during lead-in time or during breaks.
- Replay frames are no longer recorded when the game is paused.
- Pulsing cursor animation now works even during lead-in time.
- skipIntro() during replays now works properly.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-03-19 19:03:07 -04:00
parent e6576bd7f9
commit cb8c7c399c
3 changed files with 220 additions and 69 deletions

View File

@ -18,6 +18,9 @@
package itdelatrisu.opsu; package itdelatrisu.opsu;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/** /**
* Data type representing a hit object. * Data type representing a hit object.
*/ */
@ -29,6 +32,13 @@ public class OsuHitObject {
TYPE_NEWCOMBO = 4, // not an object TYPE_NEWCOMBO = 4, // not an object
TYPE_SPINNER = 8; TYPE_SPINNER = 8;
/** Hit object type names. */
private static final String
CIRCLE = "circle",
SLIDER = "slider",
SPINNER = "spinner",
UNKNOWN = "unknown object";
/** Hit sound types (bits). */ /** Hit sound types (bits). */
public static final byte public static final byte
SOUND_NORMAL = 0, SOUND_NORMAL = 0,
@ -174,14 +184,12 @@ public class OsuHitObject {
this.hitSound = Byte.parseByte(tokens[4]); this.hitSound = Byte.parseByte(tokens[4]);
// type-specific fields // type-specific fields
if ((type & OsuHitObject.TYPE_CIRCLE) > 0) { int additionIndex;
if (tokens.length > 5) { if ((type & OsuHitObject.TYPE_CIRCLE) > 0)
String[] additionTokens = tokens[5].split(":"); additionIndex = 5;
this.addition = new byte[additionTokens.length]; else if ((type & OsuHitObject.TYPE_SLIDER) > 0) {
for (int j = 0; j < additionTokens.length; j++) additionIndex = 10;
this.addition[j] = Byte.parseByte(additionTokens[j]);
}
} else if ((type & OsuHitObject.TYPE_SLIDER) > 0) {
// slider curve type and coordinates // slider curve type and coordinates
String[] sliderTokens = tokens[5].split("\\|"); String[] sliderTokens = tokens[5].split("\\|");
this.sliderType = sliderTokens[0].charAt(0); this.sliderType = sliderTokens[0].charAt(0);
@ -210,17 +218,21 @@ public class OsuHitObject {
} }
} }
} else { //if ((type & OsuHitObject.TYPE_SPINNER) > 0) { } else { //if ((type & OsuHitObject.TYPE_SPINNER) > 0) {
additionIndex = 6;
// some 'endTime' fields contain a ':' character (?) // some 'endTime' fields contain a ':' character (?)
int index = tokens[5].indexOf(':'); int index = tokens[5].indexOf(':');
if (index != -1) if (index != -1)
tokens[5] = tokens[5].substring(0, index); tokens[5] = tokens[5].substring(0, index);
this.endTime = Integer.parseInt(tokens[5]); this.endTime = Integer.parseInt(tokens[5]);
if (tokens.length > 6) { }
String[] additionTokens = tokens[6].split(":");
this.addition = new byte[additionTokens.length]; // addition
for (int j = 0; j < additionTokens.length; j++) if (tokens.length > additionIndex) {
this.addition[j] = Byte.parseByte(additionTokens[j]); String[] additionTokens = tokens[additionIndex].split(":");
} this.addition = new byte[additionTokens.length];
for (int j = 0; j < additionTokens.length; j++)
this.addition[j] = Byte.parseByte(additionTokens[j]);
} }
} }
@ -261,6 +273,20 @@ public class OsuHitObject {
*/ */
public int getType() { return type; } public int getType() { return type; }
/**
* Returns the name of the hit object type.
*/
public String getTypeName() {
if (isCircle())
return CIRCLE;
else if (isSlider())
return SLIDER;
else if (isSpinner())
return SPINNER;
else
return UNKNOWN;
}
/** /**
* Returns the hit sound type. * Returns the hit sound type.
* @return the sound type (SOUND_* bitmask) * @return the sound type (SOUND_* bitmask)
@ -424,4 +450,59 @@ public class OsuHitObject {
return addition[1]; return addition[1];
return 0; return 0;
} }
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
NumberFormat nf = new DecimalFormat("###.#####");
// common fields
sb.append(nf.format(x)); sb.append(',');
sb.append(nf.format(y)); sb.append(',');
sb.append(time); sb.append(',');
sb.append(type); sb.append(',');
sb.append(hitSound); sb.append(',');
// type-specific fields
if (isCircle())
;
else if (isSlider()) {
sb.append(getSliderType());
sb.append('|');
for (int i = 0; i < sliderX.length; i++) {
sb.append(nf.format(sliderX[i])); sb.append(':');
sb.append(nf.format(sliderY[i])); sb.append('|');
}
sb.setCharAt(sb.length() - 1, ',');
sb.append(repeat); sb.append(',');
sb.append(pixelLength); sb.append(',');
if (edgeHitSound != null) {
for (int i = 0; i < edgeHitSound.length; i++) {
sb.append(edgeHitSound[i]); sb.append('|');
}
sb.setCharAt(sb.length() - 1, ',');
}
if (edgeAddition != null) {
for (int i = 0; i < edgeAddition.length; i++) {
sb.append(edgeAddition[i][0]); sb.append(':');
sb.append(edgeAddition[i][1]); sb.append('|');
}
sb.setCharAt(sb.length() - 1, ',');
}
} else if (isSpinner()) {
sb.append(endTime);
sb.append(',');
}
// addition
if (addition != null) {
for (int i = 0; i < addition.length; i++) {
sb.append(addition[i]);
sb.append(':');
}
} else
sb.setLength(sb.length() - 1);
return sb.toString();
}
} }

View File

@ -0,0 +1,61 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014, 2015 Jeffrey Han
*
* opsu! is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu! is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
*/
package itdelatrisu.opsu.objects;
import itdelatrisu.opsu.OsuHitObject;
import org.newdawn.slick.Graphics;
/**
* Dummy hit object, used when another HitObject class cannot be created.
*/
public class DummyObject implements HitObject {
/** The associated OsuHitObject. */
private OsuHitObject hitObject;
/** The scaled starting x, y coordinates. */
private float x, y;
/**
* Constructor.
* @param hitObject the associated OsuHitObject
*/
public DummyObject(OsuHitObject hitObject) {
this.hitObject = hitObject;
this.x = hitObject.getScaledX();
this.y = hitObject.getScaledY();
}
@Override
public void draw(Graphics g, int trackPosition) {}
@Override
public boolean update(boolean overlap, int delta, int mouseX, int mouseY, boolean keyPressed, int trackPosition) {
return (trackPosition > hitObject.getTime());
}
@Override
public boolean mousePressed(int x, int y, int trackPosition) { return false; }
@Override
public float[] getPointAt(int trackPosition) { return new float[] { x, y }; }
@Override
public int getEndTime() { return hitObject.getTime(); }
}

View File

@ -39,6 +39,7 @@ import itdelatrisu.opsu.audio.SoundEffect;
import itdelatrisu.opsu.db.OsuDB; import itdelatrisu.opsu.db.OsuDB;
import itdelatrisu.opsu.db.ScoreDB; import itdelatrisu.opsu.db.ScoreDB;
import itdelatrisu.opsu.objects.Circle; import itdelatrisu.opsu.objects.Circle;
import itdelatrisu.opsu.objects.DummyObject;
import itdelatrisu.opsu.objects.HitObject; import itdelatrisu.opsu.objects.HitObject;
import itdelatrisu.opsu.objects.Slider; import itdelatrisu.opsu.objects.Slider;
import itdelatrisu.opsu.objects.Spinner; import itdelatrisu.opsu.objects.Spinner;
@ -530,12 +531,45 @@ public class Game extends BasicGameState {
skipButton.hoverUpdate(delta, mouseX, mouseY); skipButton.hoverUpdate(delta, mouseX, mouseY);
int trackPosition = MusicController.getPosition(); int trackPosition = MusicController.getPosition();
// returning from pause screen: must click previous mouse position
if (pauseTime > -1) {
// paused during lead-in or break, or "relax" or "autopilot": continue immediately
if ((pausedMouseX < 0 && pausedMouseY < 0) ||
(GameMod.RELAX.isActive() || GameMod.AUTOPILOT.isActive())) {
pauseTime = -1;
if (!isLeadIn())
MusicController.resume();
}
// focus lost: go back to pause screen
else if (!container.hasFocus()) {
game.enterState(Opsu.STATE_GAMEPAUSEMENU);
pausePulse = 0f;
}
// advance pulse animation
else {
pausePulse += delta / 750f;
if (pausePulse > 1f)
pausePulse = 0f;
}
return;
}
// replays: skip intro
if (isReplay && replaySkipTime > -1 && trackPosition >= replaySkipTime) {
if (skipIntro())
trackPosition = MusicController.getPosition();
}
// "flashlight" mod: calculate visible area radius
updateFlashlightRadius(delta, trackPosition);
// stop updating during song lead-in // stop updating during song lead-in
if (isLeadIn()) { if (isLeadIn()) {
leadInTime -= delta; leadInTime -= delta;
if (!isLeadIn()) if (!isLeadIn())
MusicController.resume(); MusicController.resume();
updateFlashlightRadius(delta, trackPosition);
return; return;
} }
@ -563,53 +597,7 @@ public class Game extends BasicGameState {
mouseY = replayY; mouseY = replayY;
} }
// "flashlight" mod: calculate visible area radius
updateFlashlightRadius(delta, trackPosition);
// returning from pause screen: must click previous mouse position
if (pauseTime > -1) {
// paused during lead-in or break, or "relax" or "autopilot": continue immediately
if ((pausedMouseX < 0 && pausedMouseY < 0) ||
(GameMod.RELAX.isActive() || GameMod.AUTOPILOT.isActive())) {
pauseTime = -1;
if (!isLeadIn())
MusicController.resume();
}
// focus lost: go back to pause screen
else if (!container.hasFocus()) {
game.enterState(Opsu.STATE_GAMEPAUSEMENU);
pausePulse = 0f;
}
// advance pulse animation
else {
pausePulse += delta / 750f;
if (pausePulse > 1f)
pausePulse = 0f;
}
return;
}
data.updateDisplays(delta); data.updateDisplays(delta);
// replays: skip intro
if (isReplay && replaySkipTime > 0 && trackPosition > replaySkipTime) {
skipIntro();
replaySkipTime = -1;
}
// pause game if focus lost
if (!container.hasFocus() && !GameMod.AUTO.isActive() && !isReplay) {
if (pauseTime < 0) {
pausedMouseX = mouseX;
pausedMouseY = mouseY;
pausePulse = 0f;
}
if (MusicController.isPlaying() || isLeadIn())
pauseTime = trackPosition;
game.enterState(Opsu.STATE_GAMEPAUSEMENU);
}
} }
/** /**
@ -706,6 +694,18 @@ public class Game extends BasicGameState {
} }
} }
// pause game if focus lost
if (!container.hasFocus() && !GameMod.AUTO.isActive() && !isReplay) {
if (pauseTime < 0) {
pausedMouseX = mouseX;
pausedMouseY = mouseY;
pausePulse = 0f;
}
if (MusicController.isPlaying() || isLeadIn())
pauseTime = trackPosition;
game.enterState(Opsu.STATE_GAMEPAUSEMENU);
}
// drain health // drain health
data.changeHealth(delta * -1 * GameData.HP_DRAIN_MULTIPLIER); data.changeHealth(delta * -1 * GameData.HP_DRAIN_MULTIPLIER);
if (!data.isAlive()) { if (!data.isAlive()) {
@ -970,7 +970,7 @@ public class Game extends BasicGameState {
* @param trackPosition the track position * @param trackPosition the track position
*/ */
private void gameKeyReleased(int keys, int x, int y, int trackPosition) { private void gameKeyReleased(int keys, int x, int y, int trackPosition) {
if (!isReplay && keys != ReplayFrame.KEY_NONE) { if (!isReplay && keys != ReplayFrame.KEY_NONE && !isLeadIn() && pauseTime == -1) {
lastKeysPressed &= ~keys; // clear keys bits lastKeysPressed &= ~keys; // clear keys bits
addReplayFrameAndRun(x, y, lastKeysPressed, trackPosition); addReplayFrameAndRun(x, y, lastKeysPressed, trackPosition);
} }
@ -1026,12 +1026,21 @@ public class Game extends BasicGameState {
comboEnd = true; comboEnd = true;
Color color = osu.combo[hitObject.getComboIndex()]; Color color = osu.combo[hitObject.getComboIndex()];
if (hitObject.isCircle())
hitObjects[i] = new Circle(hitObject, this, data, color, comboEnd); try {
else if (hitObject.isSlider()) if (hitObject.isCircle())
hitObjects[i] = new Slider(hitObject, this, data, color, comboEnd); hitObjects[i] = new Circle(hitObject, this, data, color, comboEnd);
else if (hitObject.isSpinner()) else if (hitObject.isSlider())
hitObjects[i] = new Spinner(hitObject, this, data); hitObjects[i] = new Slider(hitObject, this, data, color, comboEnd);
else if (hitObject.isSpinner())
hitObjects[i] = new Spinner(hitObject, this, data);
} catch (Exception e) {
// try to handle the error gracefully: substitute in a dummy HitObject
ErrorHandler.error(String.format("Failed to create %s at index %d:\n%s",
hitObject.getTypeName(), i, hitObject.toString()), e, true);
hitObjects[i] = new DummyObject(hitObject);
continue;
}
} }
// load the first timingPoint // load the first timingPoint
@ -1063,7 +1072,7 @@ public class Game extends BasicGameState {
for (replayIndex = 0; replayIndex < replay.frames.length; replayIndex++) { for (replayIndex = 0; replayIndex < replay.frames.length; replayIndex++) {
ReplayFrame frame = replay.frames[replayIndex]; ReplayFrame frame = replay.frames[replayIndex];
if (frame.getY() < 0) { // skip time (?) if (frame.getY() < 0) { // skip time (?)
if (frame.getTime() > 0) if (frame.getTime() >= 0 && replayIndex > 0)
replaySkipTime = frame.getTime(); replaySkipTime = frame.getTime();
} else if (frame.getTime() == 0) { } else if (frame.getTime() == 0) {
replayX = frame.getScaledX(); replayX = frame.getScaledX();