Skip to content

Commit

Permalink
Normalise angles in a more consistent manner
Browse files Browse the repository at this point in the history
They are now clamped between -180 and 180. Fixes SquidDev-CC#270
  • Loading branch information
SquidDev committed Jun 13, 2020
1 parent 07ccc4d commit 77f1c5a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,14 +35,10 @@ private MethodsLaser() {

@Nonnull
private static MethodResult fire(@Nonnull final IUnbakedContext<IModuleContainer> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity> {
public MetaEntity() {
Expand Down Expand Up @@ -55,8 +57,8 @@ public static HashMap<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,8 +37,8 @@ private MethodsKinetic() {
}

private static MethodResult launch(@Nonnull final IUnbakedContext<IModuleContainer> 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).");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -158,8 +157,8 @@ private static MethodResult shootSkeleton(@Nonnull final IUnbakedContext<IModule

@Nonnull
private static MethodResult shootBlaze(@Nonnull final IUnbakedContext<IModuleContainer> 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);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/squiddev/plethora/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,29 @@ public static <T, U> List<U> map(@Nonnull Iterable<T> 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;
}
}

0 comments on commit 77f1c5a

Please sign in to comment.