diff --git a/src/main/java/org/squiddev/plethora/gameplay/modules/methods/MethodsLaser.java b/src/main/java/org/squiddev/plethora/gameplay/modules/methods/MethodsLaser.java index c75d61d9..7d0f61c2 100644 --- a/src/main/java/org/squiddev/plethora/gameplay/modules/methods/MethodsLaser.java +++ b/src/main/java/org/squiddev/plethora/gameplay/modules/methods/MethodsLaser.java @@ -20,6 +20,7 @@ import static dan200.computercraft.api.lua.ArgumentHelper.getFiniteDouble; import static org.squiddev.plethora.gameplay.ConfigGameplay.Laser.*; +import static org.squiddev.plethora.utils.Helpers.normaliseAngle; @Injects public final class MethodsLaser { @@ -34,14 +35,10 @@ private MethodsLaser() { @Nonnull private static MethodResult fire(@Nonnull final IUnbakedContext unbaked, @Nonnull Object[] args) throws LuaException { - final double yaw = getFiniteDouble(args, 0) % 360; - double pitchArg = getFiniteDouble(args, 1) % 360; + double yaw = normaliseAngle(getFiniteDouble(args, 0)); + double pitch = normaliseAngle(getFiniteDouble(args, 1)); final float potency = (float) getFiniteDouble(args, 2); - // Normalise the pitch to be between -180 and 180. - if (pitchArg > 180) pitchArg -= 360; - final double pitch = pitchArg; - ArgumentHelper.assertBetween(potency, minimumPotency, maximumPotency, "Potency out of range (%s)."); final double motionX = -Math.sin(yaw / 180.0f * (float) Math.PI) * Math.cos(pitch / 180.0f * (float) Math.PI); diff --git a/src/main/java/org/squiddev/plethora/integration/vanilla/meta/MetaEntity.java b/src/main/java/org/squiddev/plethora/integration/vanilla/meta/MetaEntity.java index 40fe2a5a..b77df398 100644 --- a/src/main/java/org/squiddev/plethora/integration/vanilla/meta/MetaEntity.java +++ b/src/main/java/org/squiddev/plethora/integration/vanilla/meta/MetaEntity.java @@ -17,6 +17,8 @@ import java.util.HashMap; import java.util.Map; +import static org.squiddev.plethora.utils.Helpers.normaliseAngle; + @Injects public final class MetaEntity extends BaseMetaProvider { public MetaEntity() { @@ -55,8 +57,8 @@ public static HashMap getBasicProperties(@Nonnull Entity entity, result.put("motionY", entity.motionY); result.put("motionZ", entity.motionZ); - result.put("pitch", entity.rotationPitch); - result.put("yaw", entity.rotationYaw); + result.put("pitch", normaliseAngle(entity.rotationPitch)); + result.put("yaw", normaliseAngle(entity.rotationYaw)); if (location != null && location.getWorld() == entity.getEntityWorld()) { Vec3d pos = location.getLoc(); diff --git a/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsKinetic.java b/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsKinetic.java index d9fa9be1..028a63c0 100644 --- a/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsKinetic.java +++ b/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsKinetic.java @@ -23,6 +23,7 @@ import static dan200.computercraft.api.lua.ArgumentHelper.optFiniteDouble; import static org.squiddev.plethora.api.method.ArgumentHelper.assertBetween; import static org.squiddev.plethora.gameplay.ConfigGameplay.Kinetic; +import static org.squiddev.plethora.utils.Helpers.normaliseAngle; @Injects public final class MethodsKinetic { @@ -36,8 +37,8 @@ private MethodsKinetic() { } private static MethodResult launch(@Nonnull final IUnbakedContext context, @Nonnull Object[] args) throws LuaException { - final float yaw = (float) getFiniteDouble(args, 0) % 360; - final float pitch = (float) getFiniteDouble(args, 1) % 360; + final float yaw = (float) normaliseAngle(getFiniteDouble(args, 0)); + final float pitch = (float) normaliseAngle(getFiniteDouble(args, 1)); final float power = (float) getFiniteDouble(args, 2); assertBetween(power, 0, Kinetic.launchMax, "Power out of range (%s)."); diff --git a/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsKineticEntity.java b/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsKineticEntity.java index 67d1f712..60ac714c 100644 --- a/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsKineticEntity.java +++ b/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsKineticEntity.java @@ -42,6 +42,7 @@ import static dan200.computercraft.api.lua.ArgumentHelper.getFiniteDouble; import static org.squiddev.plethora.api.method.ArgumentHelper.assertBetween; import static org.squiddev.plethora.gameplay.ConfigGameplay.Kinetic; +import static org.squiddev.plethora.utils.Helpers.normaliseAngle; /** * Various methods for mobs @@ -59,10 +60,8 @@ private MethodsKineticEntity() { @PlethoraMethod(module = PlethoraModules.KINETIC_S, doc = "-- Look in a set direction") public static void look(@FromSubtarget EntityLivingBase target, double yaw, double pitch) { - yaw %= 360; - pitch %= 360; - - pitch = MathHelper.clamp(pitch, -90, 90); + yaw = normaliseAngle(yaw); + pitch = MathHelper.clamp(normaliseAngle(pitch), -90, 90); if (target instanceof EntityPlayerMP) { NetHandlerPlayServer handler = ((EntityPlayerMP) target).connection; @@ -158,8 +157,8 @@ private static MethodResult shootSkeleton(@Nonnull final IUnbakedContext unbaked, @Nonnull final Object[] args) throws LuaException { - final double yaw = getFiniteDouble(args, 0) % 360; - double pitch = getFiniteDouble(args, 1) % 360; + final double yaw = normaliseAngle(getFiniteDouble(args, 0)); + double pitch = normaliseAngle(getFiniteDouble(args, 1)); final double motionX = -Math.sin(yaw / 180.0f * (float) Math.PI) * Math.cos(pitch / 180.0f * (float) Math.PI); final double motionZ = Math.cos(yaw / 180.0f * (float) Math.PI) * Math.cos(pitch / 180.0f * (float) Math.PI); diff --git a/src/main/java/org/squiddev/plethora/utils/Helpers.java b/src/main/java/org/squiddev/plethora/utils/Helpers.java index 0c9d07b6..2dc14c22 100644 --- a/src/main/java/org/squiddev/plethora/utils/Helpers.java +++ b/src/main/java/org/squiddev/plethora/utils/Helpers.java @@ -312,4 +312,29 @@ public static List map(@Nonnull Iterable list, int size, @Nonnull F return left; }); } + + /** + * Take modulo for double numbers according to lua math, and return a double result. + * + * @param lhs Left-hand-side of the modulo. + * @param rhs Right-hand-side of the modulo. + * @return double value for the result of the modulo, + * using lua's rules for modulo + */ + public static double mod(double lhs, double rhs) { + double mod = lhs % rhs; + return mod * rhs < 0 ? mod + rhs : mod; + } + + /** + * Normalise an angle between -180 and 180. + * + * @param angle The angle to normalise. + * @return The normalised angle. + */ + public static double normaliseAngle(double angle) { + angle = mod(angle, 360); + if (angle > 180) angle -= 360; + return angle; + } }