Skip to content

Commit f806a5e

Browse files
authored
[Serialiser] Clamp yaw in serialiser (#230)
- Fixes #226
2 parents a840663 + ec00c15 commit f806a5e

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

Diff for: src/main/java/com/minecrafttas/tasmod/playback/filecommands/builtin/OptionsFileCommandExtension.java

+6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ public void onPlayback(long tick, InputContainer inputContainer) {
102102
}
103103
}
104104

105+
@Override
106+
public void onRecord(long tick, InputContainer inputContainer) {
107+
// TODO Auto-generated method stub
108+
super.onRecord(tick, inputContainer);
109+
}
110+
105111
@Override
106112
public void onClear() {
107113
try {

Diff for: src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/SerialiserFlavorBase.java

+46-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public abstract class SerialiserFlavorBase implements Registerable {
105105
*/
106106
protected boolean processExtensions = true;
107107

108+
/**
109+
* Rotation counter for clamping the yaw
110+
*/
111+
protected int yawRotations = 0;
112+
108113
/*==============================================
109114
_____ _ _ _
110115
/ ____| (_) | (_)
@@ -488,7 +493,7 @@ protected List<String> serialiseCameraAngle(VirtualCameraAngle cameraAngle) {
488493
* @return The serialised camera angle subtick
489494
*/
490495
protected String serialiseCameraAngleSubtick(VirtualCameraAngle cameraAngleSubtick) {
491-
return String.format("%s;%s", cameraAngleSubtick.getYaw(), cameraAngleSubtick.getPitch());
496+
return String.format("%s;%s", clampYaw(cameraAngleSubtick.getYaw()), cameraAngleSubtick.getPitch());
492497
}
493498

494499
/**
@@ -1402,6 +1407,8 @@ protected VirtualCameraAngle deserialiseCameraAngle(List<String> cameraAngleStri
14021407
if (!"null".equals(cameraPitchString))
14031408
cameraPitch = deserialiseRelativeFloat("camera pitch", cameraPitchString, previousPitch);
14041409

1410+
cameraYaw = unclampYaw(cameraYaw, previousYaw);
1411+
14051412
out.updateFromState(cameraPitch, cameraYaw);
14061413

14071414
previousYaw = cameraYaw;
@@ -1749,6 +1756,44 @@ protected String charListToString(List<Character> charList) {
17491756
return charString;
17501757
}
17511758

1759+
/**
1760+
* <p>Clamps the yaw to a value between -180 and 180
1761+
* @param yaw The yaw to clamp
1762+
* @return The clamped yaw
1763+
*/
1764+
protected Float clampYaw(Float yaw) {
1765+
if (yaw == null)
1766+
return yaw;
1767+
1768+
while (yaw >= 180)
1769+
yaw -= 360;
1770+
while (yaw < -180)
1771+
yaw += 360;
1772+
return yaw;
1773+
}
1774+
1775+
/**
1776+
* <p>Unclamping the yaw from a clamped value
1777+
* <p>Makes it so 170 and a previous value of -170 will return -190,<br>
1778+
* removing the -180 180 clamp. Uses {@link #yawRotations}
1779+
* @param yaw The yaw to unclamp
1780+
* @param previous The previous yaw to compare against.
1781+
* @return The unclamped yaw
1782+
*/
1783+
protected Float unclampYaw(Float yaw, Float previous) {
1784+
if (previous == null || yaw == null)
1785+
return yaw;
1786+
1787+
float clampedPrevious = clampYaw(previous);
1788+
if (clampedPrevious >= 0 && (clampedPrevious - yaw) > 180) {
1789+
yawRotations++;
1790+
}
1791+
if (clampedPrevious < 0 && (clampedPrevious - yaw) < -180) {
1792+
yawRotations--;
1793+
}
1794+
return yaw + (360 * yawRotations);
1795+
}
1796+
17521797
@Override
17531798
public String toString() {
17541799
return getExtensionName();

Diff for: src/test/java/tasmod/playback/tasfile/SerialiserFlavorBaseTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,20 @@ void testJoinNotEmpty() {
10621062
assertEquals(expected, actual);
10631063
}
10641064

1065+
/**
1066+
* Testing {@link #clampYaw(Float)} to force the yaw between -180 and 180
1067+
*/
1068+
@Test
1069+
void testYawClamping() {
1070+
assertEquals(-170f, clampYaw(190f));
1071+
assertEquals(160f, clampYaw(-200f));
1072+
}
1073+
1074+
@Test
1075+
void testYawUnclamping() {
1076+
assertEquals(-190f, unclampYaw(170f, -170f));
1077+
}
1078+
10651079
private <T extends Serializable> void assertBigArrayList(BigArrayList<T> expected, BigArrayList<T> actual) {
10661080
assertIterableEquals(convertBigArrayListToArrayList(expected), convertBigArrayListToArrayList(actual));
10671081
}
@@ -1078,5 +1092,4 @@ private <T extends Serializable> ArrayList<T> convertBigArrayListToArrayList(Big
10781092
public SerialiserFlavorBase clone() {
10791093
return new SerialiserFlavorBaseTest();
10801094
}
1081-
10821095
}

0 commit comments

Comments
 (0)