From 91f3bda70960a3a32b0dd1c3f1c70dbb67859ec1 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 24 Nov 2024 18:17:56 +0100 Subject: [PATCH 1/8] if director range is less than 0, do not try convering --- .../combat/features/directors/AADirectors.java | 17 +++++++++-------- .../features/directors/ArrowDirectors.java | 16 +++++++++------- .../features/directors/CannonDirectors.java | 16 ++++++++++------ .../movecraft/combat/utils/MathHelper.java | 2 ++ 4 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java index 1ccbcb67..fb78e591 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java @@ -97,14 +97,15 @@ private void processFireball(@NotNull SmallFireball fireball) { double speed = fireballVector.length() ; // store the speed to add it back in later, since all the values we will be using are "normalized", IE: have a speed of 1 fireballVector = fireballVector.normalize(); // you normalize it for comparison with the new direction to see if we are trying to steer too far - Block targetBlock = DirectorUtils.getDirectorBlock(p, AADirectorRange); - Vector targetVector; - - if (targetBlock == null || targetBlock.getType().isAir()) // the player is looking at nothing, shoot in that general direction - targetVector = p.getLocation().getDirection(); - else { // shoot directly at the block the player is looking at (IE: with convergence) - targetVector = targetBlock.getLocation().toVector().subtract(fireball.getLocation().toVector()); - targetVector = targetVector.normalize(); + // the player is looking at nothing, shoot in that general direction + Vector targetVector = p.getLocation().getDirection(); + + if (AADirectorRange >= 0) { + Block targetBlock = DirectorUtils.getDirectorBlock(p, AADirectorRange); + if (targetBlock != null && !targetBlock.getType().isAir()) { + targetVector = targetBlock.getLocation().toVector().subtract(fireball.getLocation().toVector()); + targetVector = targetVector.normalize(); + } } if (targetVector.getX() - fireballVector.getX() > 0.5) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java index 56d6bfad..c73df0b0 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java @@ -95,13 +95,15 @@ private void processArrow(@NotNull Arrow arrow) { double speed = arrowVector.length(); // store the speed to add it back in later, since all the values we will be using are "normalized", IE: have a speed of 1 arrowVector = arrowVector.normalize(); // you normalize it for comparison with the new direction to see if we are trying to steer too far - Block targetBlock = DirectorUtils.getDirectorBlock(p, ArrowDirectorRange); - Vector targetVector; - if (targetBlock == null || targetBlock.getType().equals(Material.AIR)) // the player is looking at nothing, shoot in that general direction - targetVector = p.getLocation().getDirection(); - else { // shoot directly at the block the player is looking at (IE: with convergence) - targetVector = targetBlock.getLocation().toVector().subtract(arrow.getLocation().toVector()); - targetVector = targetVector.normalize(); + // the player is looking at nothing, shoot in that general direction + Vector targetVector = p.getLocation().getDirection(); + if (ArrowDirectorRange >= 0) { + Block targetBlock = DirectorUtils.getDirectorBlock(p, ArrowDirectorRange); + if (targetBlock != null && !targetBlock.getType().equals(Material.AIR)) { + // shoot directly at the block the player is looking at (IE: with convergence) + targetVector = targetBlock.getLocation().toVector().subtract(arrow.getLocation().toVector()); + targetVector = targetVector.normalize(); + } } if (targetVector.getX() - arrowVector.getX() > 0.5) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java index 98f66510..57c2320a 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java @@ -124,12 +124,16 @@ private void processTNT(@NotNull TNTPrimed tnt) { double horizontalSpeed = tntVector.length(); tntVector = tntVector.normalize(); // you normalize it for comparison with the new direction to see if we are trying to steer too far - Block targetBlock = DirectorUtils.getDirectorBlock(p, CannonDirectorRange); - Vector targetVector; - if (targetBlock == null || targetBlock.getType().equals(Material.AIR)) // the player is looking at nothing, shoot in that general direction - targetVector = p.getLocation().getDirection(); - else // shoot directly at the block the player is looking at (IE: with convergence) - targetVector = targetBlock.getLocation().toVector().subtract(tnt.getLocation().toVector()); + // the player is looking at nothing, shoot in that general direction + Vector targetVector = p.getLocation().getDirection(); + + if (CannonDirectorRange >= 0) { + Block targetBlock = DirectorUtils.getDirectorBlock(p, CannonDirectorRange); + if (targetBlock != null && targetBlock.getType().equals(Material.AIR)) { + // shoot directly at the block the player is looking at (IE: with convergence) + targetVector = targetBlock.getLocation().toVector().subtract(tnt.getLocation().toVector()); + } + } // Remove the y-component from the TargetVector and normalize targetVector = (new Vector(targetVector.getX(), 0, targetVector.getZ())).normalize(); diff --git a/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java new file mode 100644 index 00000000..25c8993a --- /dev/null +++ b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java @@ -0,0 +1,2 @@ +package net.countercraft.movecraft.combat.utils;public class MathHelper { +} From db164811c34e929ffa7bdfd886703b2511e89695 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 24 Nov 2024 18:18:05 +0100 Subject: [PATCH 2/8] implement clamp methods --- .../movecraft/combat/utils/MathHelper.java | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java index 25c8993a..9c33cfe5 100644 --- a/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java +++ b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java @@ -1,2 +1,47 @@ -package net.countercraft.movecraft.combat.utils;public class MathHelper { +package net.countercraft.movecraft.combat.utils; + +public class MathHelper { + + public static double clamp(double value) { + return clamp(Double.MIN_VALUE, Double.MAX_VALUE, value); + } + + public static float clamp(float value) { + return clamp(Float.MIN_VALUE, Float.MAX_VALUE, value); + } + + public static int clamp(int value) { + return clamp(Integer.MIN_VALUE, Integer.MAX_VALUE, value); + } + + public static double clamp(double min, double max, double value) { + if (value > max) { + return max; + } + if (value < min) { + return min; + } + return value; + } + + public static int clamp(int min, int max, int value) { + if (value > max) { + return max; + } + if (value < min) { + return min; + } + return value; + } + + public static float clamp(float min, float max, float value) { + if (value > max) { + return max; + } + if (value < min) { + return min; + } + return value; + } + } From 8017b9193da30800aca22121a642093eb86d333c Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 24 Nov 2024 18:20:31 +0100 Subject: [PATCH 3/8] clamp vectors --- .../movecraft/combat/features/directors/AADirectors.java | 6 ++++++ .../movecraft/combat/features/directors/ArrowDirectors.java | 6 ++++++ .../combat/features/directors/CannonDirectors.java | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java index fb78e591..ac223aa9 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java @@ -4,6 +4,7 @@ import net.countercraft.movecraft.combat.features.directors.events.CraftDirectEvent; import net.countercraft.movecraft.combat.localisation.I18nSupport; import net.countercraft.movecraft.combat.utils.DirectorUtils; +import net.countercraft.movecraft.combat.utils.MathHelper; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.PlayerCraft; @@ -130,6 +131,11 @@ else if (targetVector.getZ() - fireballVector.getZ() < -0.5) fireballVector.setZ(targetVector.getZ()); fireballVector = fireballVector.multiply(speed); // put the original speed back in, but now along a different trajectory + + fireballVector.setX(MathHelper.clamp(fireballVector.getX())); + fireballVector.setY(MathHelper.clamp(fireballVector.getY())); + fireballVector.setZ(MathHelper.clamp(fireballVector.getZ())); + try { fireballVector.checkFinite(); } diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java index c73df0b0..98279946 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java @@ -4,6 +4,7 @@ import net.countercraft.movecraft.combat.features.directors.events.CraftDirectEvent; import net.countercraft.movecraft.combat.localisation.I18nSupport; import net.countercraft.movecraft.combat.utils.DirectorUtils; +import net.countercraft.movecraft.combat.utils.MathHelper; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.PlayerCraft; @@ -128,6 +129,11 @@ else if (targetVector.getZ() - arrowVector.getZ() < -0.5) arrowVector.setZ(targetVector.getZ()); arrowVector = arrowVector.multiply(speed); // put the original speed back in, but now along a different trajectory + + arrowVector.setX(MathHelper.clamp(arrowVector.getX())); + arrowVector.setY(MathHelper.clamp(arrowVector.getY())); + arrowVector.setZ(MathHelper.clamp(arrowVector.getZ())); + try { arrowVector.checkFinite(); } diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java index 57c2320a..e017e3dd 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java @@ -6,6 +6,7 @@ import net.countercraft.movecraft.combat.features.tracking.DamageTracking; import net.countercraft.movecraft.combat.localisation.I18nSupport; import net.countercraft.movecraft.combat.utils.DirectorUtils; +import net.countercraft.movecraft.combat.utils.MathHelper; import net.countercraft.movecraft.craft.Craft; import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.craft.PlayerCraft; @@ -143,6 +144,10 @@ private void processTNT(@NotNull TNTPrimed tnt) { tntVector.setZ(Math.min(Math.max(targetVector.getZ(), tntVector.getZ() - 0.7), tntVector.getZ() + 0.7)); tntVector = tntVector.multiply(horizontalSpeed); // put the original speed back in, but now along a different trajectory + + tntVector.setX(MathHelper.clamp(tntVector.getX())); + tntVector.setZ(MathHelper.clamp(tntVector.getZ())); + tntVector.setY(tnt.getVelocity().getY()); // you leave the original Y (or vertical axis) trajectory as it was try { From 88dd7e21f3fa4119b8ca7c7a1f6afb6dca829b43 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sun, 24 Nov 2024 19:09:54 +0100 Subject: [PATCH 4/8] correct mathhelper (cherry picked from commit acce22630ce1df02fff76094fa058ed9b856935c) --- .../net/countercraft/movecraft/combat/utils/MathHelper.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java index 9c33cfe5..97e3943f 100644 --- a/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java +++ b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java @@ -3,11 +3,13 @@ public class MathHelper { public static double clamp(double value) { - return clamp(Double.MIN_VALUE, Double.MAX_VALUE, value); + // Double.MIN_VALUE represents the lowest POSITIVE double value to match IEEE754 format + return clamp(-Double.MAX_VALUE, Double.MAX_VALUE, value); } + // Same as with doubles! public static float clamp(float value) { - return clamp(Float.MIN_VALUE, Float.MAX_VALUE, value); + return clamp(-Float.MAX_VALUE, Float.MAX_VALUE, value); } public static int clamp(int value) { From 98523e932da7d4c2957212bf21299017a5d67da6 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Sat, 21 Dec 2024 20:13:59 +0100 Subject: [PATCH 5/8] add single method for clamping vectors (cherry picked from commit f5628f70e577613c85080afdbc0a14e8e553b972) --- .../movecraft/combat/utils/MathHelper.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java index 97e3943f..11d082de 100644 --- a/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java +++ b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java @@ -1,5 +1,8 @@ package net.countercraft.movecraft.combat.utils; +import org.bukkit.Axis; +import org.bukkit.util.Vector; + public class MathHelper { public static double clamp(double value) { @@ -46,4 +49,15 @@ public static float clamp(float min, float max, float value) { return value; } + public static void clampVectorModify(final Vector vector) { + vector.setX(clamp(vector.getX())); + vector.setY(clamp(vector.getY())); + vector.setZ(clamp(vector.getZ())); + } + public static Vector clampVector(final Vector vector) { + Vector result = vector.clone(); + clampVector(result); + return result; + } + } From 26c222addacc571d4b99322f4cd97a1eedabe972 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Tue, 24 Dec 2024 13:26:14 +0100 Subject: [PATCH 6/8] fix stackoverflow exception (cherry picked from commit dd99d569adfb77f2d035b3490ccc29e29b65c7af) --- .../net/countercraft/movecraft/combat/utils/MathHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java index 11d082de..7935943d 100644 --- a/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java +++ b/src/main/java/net/countercraft/movecraft/combat/utils/MathHelper.java @@ -56,7 +56,7 @@ public static void clampVectorModify(final Vector vector) { } public static Vector clampVector(final Vector vector) { Vector result = vector.clone(); - clampVector(result); + clampVectorModify(result); return result; } From f4fa3348d4f3dc514dcd05258d06aaee23a56576 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Tue, 24 Dec 2024 13:30:03 +0100 Subject: [PATCH 7/8] use clampVector method --- .../movecraft/combat/features/directors/AADirectors.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java index ac223aa9..7da4c653 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java @@ -132,9 +132,7 @@ else if (targetVector.getZ() - fireballVector.getZ() < -0.5) fireballVector = fireballVector.multiply(speed); // put the original speed back in, but now along a different trajectory - fireballVector.setX(MathHelper.clamp(fireballVector.getX())); - fireballVector.setY(MathHelper.clamp(fireballVector.getY())); - fireballVector.setZ(MathHelper.clamp(fireballVector.getZ())); + MathHelper.clampVectorModify(fireballVector); try { fireballVector.checkFinite(); From 825f711f266eddcb82bc7e393cb6e36bee55bf70 Mon Sep 17 00:00:00 2001 From: DerToaster98 <38782719+DerToaster98@users.noreply.github.com> Date: Tue, 24 Dec 2024 13:30:29 +0100 Subject: [PATCH 8/8] do the same for arrow and cannons director --- .../movecraft/combat/features/directors/ArrowDirectors.java | 4 +--- .../movecraft/combat/features/directors/CannonDirectors.java | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java index 98279946..5e87d539 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/ArrowDirectors.java @@ -130,9 +130,7 @@ else if (targetVector.getZ() - arrowVector.getZ() < -0.5) arrowVector = arrowVector.multiply(speed); // put the original speed back in, but now along a different trajectory - arrowVector.setX(MathHelper.clamp(arrowVector.getX())); - arrowVector.setY(MathHelper.clamp(arrowVector.getY())); - arrowVector.setZ(MathHelper.clamp(arrowVector.getZ())); + MathHelper.clampVectorModify(arrowVector); try { arrowVector.checkFinite(); diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java index e017e3dd..7f7f7058 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java @@ -145,8 +145,7 @@ private void processTNT(@NotNull TNTPrimed tnt) { tntVector = tntVector.multiply(horizontalSpeed); // put the original speed back in, but now along a different trajectory - tntVector.setX(MathHelper.clamp(tntVector.getX())); - tntVector.setZ(MathHelper.clamp(tntVector.getZ())); + MathHelper.clampVectorModify(tntVector); tntVector.setY(tnt.getVelocity().getY()); // you leave the original Y (or vertical axis) trajectory as it was