From 4d32ba131a812b953d37aee199dc6df365786aed Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 9 Aug 2018 16:05:14 -0500 Subject: [PATCH 1/6] Fix deprecation warning --- src/main/java/baritone/bot/behavior/impl/PathingBehavior.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java index b696f1ef..b5fc3a0b 100644 --- a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java @@ -281,7 +281,7 @@ public class PathingBehavior extends Behavior { double d0 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) partialTicks; double d1 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialTicks; double d2 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) partialTicks; - AxisAlignedBB toDraw = block.getSelectedBoundingBox(state, Minecraft.getMinecraft().world, blockpos).expand(0.0020000000949949026D, 0.0020000000949949026D, 0.0020000000949949026D).offset(-d0, -d1, -d2); + AxisAlignedBB toDraw = state.getSelectedBoundingBox(Minecraft.getMinecraft().world, blockpos).expand(0.0020000000949949026D, 0.0020000000949949026D, 0.0020000000949949026D).offset(-d0, -d1, -d2); Tessellator tessellator = Tessellator.getInstance(); BufferBuilder buffer = tessellator.getBuffer(); buffer.begin(3, DefaultVertexFormats.POSITION); From 2b6d86dcdd2fd19cef21b93936c41a43fd6ffa9c Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 9 Aug 2018 16:35:42 -0500 Subject: [PATCH 2/6] Move fromAngleAndDirection to GoalXZ --- .../bot/behavior/impl/PathingBehavior.java | 9 +-------- .../java/baritone/bot/pathing/goals/GoalXZ.java | 15 ++++++++++++--- src/main/java/baritone/bot/utils/Helper.java | 4 ++++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java index b5fc3a0b..7678d4d0 100644 --- a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java @@ -108,20 +108,13 @@ public class PathingBehavior extends Behavior { return; } if (msg.toLowerCase().startsWith("thisway")) { - goal = fromAngleAndDirection(Double.parseDouble(msg.substring(7).trim())); + goal = GoalXZ.fromDirection(playerFeetAsVec(), player().rotationYaw, Double.parseDouble(msg.substring(7).trim())); displayChatMessageRaw("Goal: " + goal); event.cancel(); return; } } - public GoalXZ fromAngleAndDirection(double distance) { - double theta = ((double) player().rotationYaw) * Math.PI / 180D; - double x = player().posX - Math.sin(theta) * distance; - double z = player().posZ + Math.cos(theta) * distance; - return new GoalXZ((int) x, (int) z); - } - /** * In a new thread, pathfind to target blockpos * diff --git a/src/main/java/baritone/bot/pathing/goals/GoalXZ.java b/src/main/java/baritone/bot/pathing/goals/GoalXZ.java index 324b2371..8b3ad4c6 100644 --- a/src/main/java/baritone/bot/pathing/goals/GoalXZ.java +++ b/src/main/java/baritone/bot/pathing/goals/GoalXZ.java @@ -17,7 +17,9 @@ package baritone.bot.pathing.goals; +import baritone.bot.utils.Utils; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; /** * Useful for long-range goals that don't have a specific Y level. @@ -54,6 +56,11 @@ public class GoalXZ implements Goal { return calculate(xDiff, zDiff); } + @Override + public String toString() { + return "Goal{x=" + x + ",z=" + z + "}"; + } + public static double calculate(double xDiff, double zDiff) { return calculate(xDiff, zDiff, 0); } @@ -93,8 +100,10 @@ public class GoalXZ implements Goal { return (diagonal + straight) * WALK_ONE_BLOCK_COST; } - @Override - public String toString() { - return "Goal{x=" + x + ",z=" + z + "}"; + public static GoalXZ fromDirection(Vec3d origin, float yaw, double distance) { + double theta = Utils.degToRad(yaw); + double x = origin.x - Math.sin(theta) * distance; + double z = origin.z + Math.cos(theta) * distance; + return new GoalXZ((int) x, (int) z); } } diff --git a/src/main/java/baritone/bot/utils/Helper.java b/src/main/java/baritone/bot/utils/Helper.java index 677528fa..46853203 100755 --- a/src/main/java/baritone/bot/utils/Helper.java +++ b/src/main/java/baritone/bot/utils/Helper.java @@ -50,6 +50,10 @@ public interface Helper { return new BlockPos(player().posX, player().posY, player().posZ); } + default Vec3d playerFeetAsVec() { + return new Vec3d(player().posX, player().posY, player().posZ); + } + default Vec3d playerHead() { return new Vec3d(player().posX, player().posY + player().getEyeHeight(), player().posZ); } From 80ef73c279e16b9175249a1874784f9b256df4e8 Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 9 Aug 2018 16:48:10 -0500 Subject: [PATCH 3/6] Move path rendering methods to new class --- .../bot/behavior/impl/PathingBehavior.java | 160 +++--------------- .../java/baritone/bot/utils/PathRenderer.java | 143 ++++++++++++++++ src/main/java/baritone/bot/utils/Utils.java | 4 + 3 files changed, 169 insertions(+), 138 deletions(-) create mode 100644 src/main/java/baritone/bot/utils/PathRenderer.java diff --git a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java index 7678d4d0..c5034f13 100644 --- a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java @@ -31,23 +31,10 @@ import baritone.bot.pathing.goals.GoalXZ; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.path.IPath; import baritone.bot.pathing.path.PathExecutor; -import baritone.bot.utils.BlockStateInterface; -import net.minecraft.block.Block; -import net.minecraft.block.state.IBlockState; -import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.EntityPlayerSP; -import net.minecraft.client.renderer.BufferBuilder; -import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.vertex.DefaultVertexFormats; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; -import net.minecraft.util.math.AxisAlignedBB; +import baritone.bot.utils.PathRenderer; import net.minecraft.util.math.BlockPos; -import org.lwjgl.opengl.GL11; import java.awt.*; -import java.util.List; import java.util.Optional; public class PathingBehavior extends Behavior { @@ -73,14 +60,6 @@ public class PathingBehavior extends Behavior { } } - public PathExecutor getExecutor() { - return current; - } - - public Optional getPath() { - return Optional.ofNullable(current).map(PathExecutor::getPath); - } - @Override public void onSendChatMessage(ChatEvent event) { String msg = event.getMessage(); @@ -115,6 +94,14 @@ public class PathingBehavior extends Behavior { } } + public PathExecutor getExecutor() { + return current; + } + + public Optional getPath() { + return Optional.ofNullable(current).map(PathExecutor::getPath); + } + /** * In a new thread, pathfind to target blockpos * @@ -128,7 +115,8 @@ public class PathingBehavior extends Behavior { } findPath(start).map(PathExecutor::new).ifPresent(path -> current = path); - /*isThereAnythingInProgress = false; + /* + isThereAnythingInProgress = false; if (!currentPath.goal.isInGoal(currentPath.end)) { if (talkAboutIt) { Out.gui("I couldn't get all the way to " + goal + ", but I'm going to get as close as I can. " + currentPath.numNodes + " nodes considered", Out.Mode.Standard); @@ -136,7 +124,8 @@ public class PathingBehavior extends Behavior { planAhead(); } else if (talkAboutIt) { Out.gui(, Out.Mode.Debug); - }*/ + } + */ if (talkAboutIt && current != null && current.getPath() != null) { displayChatMessageRaw("Finished finding a path from " + start + " to " + goal + ". " + current.getPath().getNumNodesConsidered() + " nodes considered"); } @@ -165,23 +154,23 @@ public class PathingBehavior extends Behavior { @Override public void onRenderPass(RenderEvent event) { - //System.out.println("Render passing"); - //System.out.println(event.getPartialTicks()); + // System.out.println("Render passing"); + // System.out.println(event.getPartialTicks()); float partialTicks = event.getPartialTicks(); long start = System.currentTimeMillis(); // Render the current path, if there is one if (current != null && current.getPath() != null) { int renderBegin = Math.max(current.getPosition() - 3, 0); - drawPath(current.getPath(), renderBegin, player(), partialTicks, Color.RED); + PathRenderer.drawPath(current.getPath(), renderBegin, player(), partialTicks, Color.RED); } long split = System.currentTimeMillis(); getPath().ifPresent(path -> { for (Movement m : path.movements()) { for (BlockPos pos : m.toPlace()) - drawSelectionBox(player(), pos, partialTicks, Color.GREEN); + PathRenderer.drawSelectionBox(player(), pos, partialTicks, Color.GREEN); for (BlockPos pos : m.toBreak()) { - drawSelectionBox(player(), pos, partialTicks, Color.RED); + PathRenderer.drawSelectionBox(player(), pos, partialTicks, Color.RED); } } }); @@ -189,120 +178,15 @@ public class PathingBehavior extends Behavior { // If there is a path calculation currently running, render the path calculation process AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(currentlyRunning -> { currentlyRunning.bestPathSoFar().ifPresent(p -> { - drawPath(p, 0, player(), partialTicks, Color.BLUE); + PathRenderer.drawPath(p, 0, player(), partialTicks, Color.BLUE); currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> { - drawPath(mr, 0, player(), partialTicks, Color.CYAN); - drawSelectionBox(player(), mr.getDest(), partialTicks, Color.CYAN); + PathRenderer.drawPath(mr, 0, player(), partialTicks, Color.CYAN); + PathRenderer.drawSelectionBox(player(), mr.getDest(), partialTicks, Color.CYAN); }); }); }); long end = System.currentTimeMillis(); // if (end - start > 0) - // System.out.println("Frame took " + (split - start) + " " + (end - split)); - } - - private static BlockPos diff(BlockPos a, BlockPos b) { - return new BlockPos(a.getX() - b.getX(), a.getY() - b.getY(), a.getZ() - b.getZ()); - } - - public void drawPath(IPath path, int startIndex, EntityPlayerSP player, float partialTicks, Color color) { - - GlStateManager.enableBlend(); - GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); - GlStateManager.color(color.getColorComponents(null)[0], color.getColorComponents(null)[1], color.getColorComponents(null)[2], 0.4F); - GL11.glLineWidth(3.0F); - GlStateManager.disableTexture2D(); - GlStateManager.depthMask(false); - - - List positions = path.positions(); - int next; - for (int i = startIndex; i < positions.size() - 1; i = next) { - BlockPos start = positions.get(i); - - next = i + 1; - BlockPos end = positions.get(next); - BlockPos direction = diff(start, end); - while (next + 1 < positions.size() && direction.equals(diff(end, positions.get(next + 1)))) { - next++; - end = positions.get(next); - } - double x1 = start.getX(); - double y1 = start.getY(); - double z1 = start.getZ(); - double x2 = end.getX(); - double y2 = end.getY(); - double z2 = end.getZ(); - drawLine(player, x1, y1, z1, x2, y2, z2, partialTicks); - } - //GlStateManager.color(0.0f, 0.0f, 0.0f, 0.4f); - GlStateManager.depthMask(true); - GlStateManager.enableTexture2D(); - GlStateManager.disableBlend(); - } - - public static void drawLine(EntityPlayer player, double bp1x, double bp1y, double bp1z, double bp2x, double bp2y, double bp2z, float partialTicks) { - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder buffer = tessellator.getBuffer(); - double d0 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) partialTicks; - double d1 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialTicks; - double d2 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) partialTicks; - buffer.begin(3, DefaultVertexFormats.POSITION); - buffer.pos(bp1x + 0.5D - d0, bp1y + 0.5D - d1, bp1z + 0.5D - d2).endVertex(); - buffer.pos(bp2x + 0.5D - d0, bp2y + 0.5D - d1, bp2z + 0.5D - d2).endVertex(); - buffer.pos(bp2x + 0.5D - d0, bp2y + 0.53D - d1, bp2z + 0.5D - d2).endVertex(); - buffer.pos(bp1x + 0.5D - d0, bp1y + 0.53D - d1, bp1z + 0.5D - d2).endVertex(); - buffer.pos(bp1x + 0.5D - d0, bp1y + 0.5D - d1, bp1z + 0.5D - d2).endVertex(); - tessellator.draw(); - } - - public static void drawSelectionBox(EntityPlayer player, BlockPos blockpos, float partialTicks, Color color) { - GlStateManager.enableBlend(); - GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); - GlStateManager.color(color.getColorComponents(null)[0], color.getColorComponents(null)[1], color.getColorComponents(null)[2], 0.4F); - GL11.glLineWidth(5.0F); - GlStateManager.disableTexture2D(); - GlStateManager.depthMask(false); - float f = 0.002F; - //BlockPos blockpos = movingObjectPositionIn.getBlockPos(); - IBlockState state = BlockStateInterface.get(blockpos); - Block block = state.getBlock(); - if (block.equals(Blocks.AIR)) { - block = Blocks.DIRT; - } - //block.setBlockBoundsBasedOnState(Minecraft.getMinecraft().world, blockpos); - double d0 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) partialTicks; - double d1 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialTicks; - double d2 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) partialTicks; - AxisAlignedBB toDraw = state.getSelectedBoundingBox(Minecraft.getMinecraft().world, blockpos).expand(0.0020000000949949026D, 0.0020000000949949026D, 0.0020000000949949026D).offset(-d0, -d1, -d2); - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder buffer = tessellator.getBuffer(); - buffer.begin(3, DefaultVertexFormats.POSITION); - buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex(); - buffer.pos(toDraw.maxX, toDraw.minY, toDraw.minZ).endVertex(); - buffer.pos(toDraw.maxX, toDraw.minY, toDraw.maxZ).endVertex(); - buffer.pos(toDraw.minX, toDraw.minY, toDraw.maxZ).endVertex(); - buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex(); - tessellator.draw(); - buffer.begin(3, DefaultVertexFormats.POSITION); - buffer.pos(toDraw.minX, toDraw.maxY, toDraw.minZ).endVertex(); - buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.minZ).endVertex(); - buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.maxZ).endVertex(); - buffer.pos(toDraw.minX, toDraw.maxY, toDraw.maxZ).endVertex(); - buffer.pos(toDraw.minX, toDraw.maxY, toDraw.minZ).endVertex(); - tessellator.draw(); - buffer.begin(1, DefaultVertexFormats.POSITION); - buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex(); - buffer.pos(toDraw.minX, toDraw.maxY, toDraw.minZ).endVertex(); - buffer.pos(toDraw.maxX, toDraw.minY, toDraw.minZ).endVertex(); - buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.minZ).endVertex(); - buffer.pos(toDraw.maxX, toDraw.minY, toDraw.maxZ).endVertex(); - buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.maxZ).endVertex(); - buffer.pos(toDraw.minX, toDraw.minY, toDraw.maxZ).endVertex(); - buffer.pos(toDraw.minX, toDraw.maxY, toDraw.maxZ).endVertex(); - tessellator.draw(); - GlStateManager.depthMask(true); - GlStateManager.enableTexture2D(); - GlStateManager.disableBlend(); + // System.out.println("Frame took " + (split - start) + " " + (end - split)); } } diff --git a/src/main/java/baritone/bot/utils/PathRenderer.java b/src/main/java/baritone/bot/utils/PathRenderer.java new file mode 100644 index 00000000..04b1629b --- /dev/null +++ b/src/main/java/baritone/bot/utils/PathRenderer.java @@ -0,0 +1,143 @@ +/* + * This file is part of Baritone. + * + * Baritone 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. + * + * Baritone 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 Baritone. If not, see . + */ + +package baritone.bot.utils; + +import baritone.bot.pathing.path.IPath; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.entity.EntityPlayerSP; +import net.minecraft.client.renderer.BufferBuilder; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.client.renderer.vertex.DefaultVertexFormats; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraft.util.math.AxisAlignedBB; +import net.minecraft.util.math.BlockPos; +import org.lwjgl.opengl.GL11; + +import java.awt.*; +import java.util.List; + +import static org.lwjgl.opengl.GL11.*; + +/** + * @author Brady + * @since 8/9/2018 4:39 PM + */ +public final class PathRenderer implements Helper { + + private PathRenderer() {} + + public static void drawPath(IPath path, int startIndex, EntityPlayerSP player, float partialTicks, Color color) { + GlStateManager.enableBlend(); + GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); + GlStateManager.color(color.getColorComponents(null)[0], color.getColorComponents(null)[1], color.getColorComponents(null)[2], 0.4F); + GL11.glLineWidth(3.0F); + GlStateManager.disableTexture2D(); + GlStateManager.depthMask(false); + + List positions = path.positions(); + int next; + for (int i = startIndex; i < positions.size() - 1; i = next) { + BlockPos start = positions.get(i); + + next = i + 1; + BlockPos end = positions.get(next); + BlockPos direction = Utils.diff(start, end); + while (next + 1 < positions.size() && direction.equals(Utils.diff(end, positions.get(next + 1)))) { + next++; + end = positions.get(next); + } + double x1 = start.getX(); + double y1 = start.getY(); + double z1 = start.getZ(); + double x2 = end.getX(); + double y2 = end.getY(); + double z2 = end.getZ(); + drawLine(player, x1, y1, z1, x2, y2, z2, partialTicks); + } + // GlStateManager.color(0.0f, 0.0f, 0.0f, 0.4f); + GlStateManager.depthMask(true); + GlStateManager.enableTexture2D(); + GlStateManager.disableBlend(); + } + + public static void drawLine(EntityPlayer player, double bp1x, double bp1y, double bp1z, double bp2x, double bp2y, double bp2z, float partialTicks) { + Tessellator tessellator = Tessellator.getInstance(); + BufferBuilder buffer = tessellator.getBuffer(); + double d0 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) partialTicks; + double d1 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialTicks; + double d2 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) partialTicks; + buffer.begin(GL_LINE_STRIP, DefaultVertexFormats.POSITION); + buffer.pos(bp1x + 0.5D - d0, bp1y + 0.5D - d1, bp1z + 0.5D - d2).endVertex(); + buffer.pos(bp2x + 0.5D - d0, bp2y + 0.5D - d1, bp2z + 0.5D - d2).endVertex(); + buffer.pos(bp2x + 0.5D - d0, bp2y + 0.53D - d1, bp2z + 0.5D - d2).endVertex(); + buffer.pos(bp1x + 0.5D - d0, bp1y + 0.53D - d1, bp1z + 0.5D - d2).endVertex(); + buffer.pos(bp1x + 0.5D - d0, bp1y + 0.5D - d1, bp1z + 0.5D - d2).endVertex(); + tessellator.draw(); + } + + public static void drawSelectionBox(EntityPlayer player, BlockPos blockpos, float partialTicks, Color color) { + GlStateManager.enableBlend(); + GlStateManager.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO); + GlStateManager.color(color.getColorComponents(null)[0], color.getColorComponents(null)[1], color.getColorComponents(null)[2], 0.4F); + GL11.glLineWidth(5.0F); + GlStateManager.disableTexture2D(); + GlStateManager.depthMask(false); + float f = 0.002F; + // BlockPos blockpos = movingObjectPositionIn.getBlockPos(); + IBlockState state = BlockStateInterface.get(blockpos); + if (state.getBlock().equals(Blocks.AIR)) { + state = Blocks.DIRT.getDefaultState(); + } + // block.setBlockBoundsBasedOnState(Minecraft.getMinecraft().world, blockpos); + double d0 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) partialTicks; + double d1 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialTicks; + double d2 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) partialTicks; + AxisAlignedBB toDraw = state.getSelectedBoundingBox(mc.world, blockpos).expand(f, f, f).offset(-d0, -d1, -d2); + Tessellator tessellator = Tessellator.getInstance(); + BufferBuilder buffer = tessellator.getBuffer(); + buffer.begin(GL_LINE_STRIP, DefaultVertexFormats.POSITION); + buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex(); + buffer.pos(toDraw.maxX, toDraw.minY, toDraw.minZ).endVertex(); + buffer.pos(toDraw.maxX, toDraw.minY, toDraw.maxZ).endVertex(); + buffer.pos(toDraw.minX, toDraw.minY, toDraw.maxZ).endVertex(); + buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex(); + tessellator.draw(); + buffer.begin(GL_LINE_STRIP, DefaultVertexFormats.POSITION); + buffer.pos(toDraw.minX, toDraw.maxY, toDraw.minZ).endVertex(); + buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.minZ).endVertex(); + buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.maxZ).endVertex(); + buffer.pos(toDraw.minX, toDraw.maxY, toDraw.maxZ).endVertex(); + buffer.pos(toDraw.minX, toDraw.maxY, toDraw.minZ).endVertex(); + tessellator.draw(); + buffer.begin(GL_LINES, DefaultVertexFormats.POSITION); + buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex(); + buffer.pos(toDraw.minX, toDraw.maxY, toDraw.minZ).endVertex(); + buffer.pos(toDraw.maxX, toDraw.minY, toDraw.minZ).endVertex(); + buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.minZ).endVertex(); + buffer.pos(toDraw.maxX, toDraw.minY, toDraw.maxZ).endVertex(); + buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.maxZ).endVertex(); + buffer.pos(toDraw.minX, toDraw.minY, toDraw.maxZ).endVertex(); + buffer.pos(toDraw.minX, toDraw.maxY, toDraw.maxZ).endVertex(); + tessellator.draw(); + GlStateManager.depthMask(true); + GlStateManager.enableTexture2D(); + GlStateManager.disableBlend(); + } +} diff --git a/src/main/java/baritone/bot/utils/Utils.java b/src/main/java/baritone/bot/utils/Utils.java index 151f3ddb..25803767 100755 --- a/src/main/java/baritone/bot/utils/Utils.java +++ b/src/main/java/baritone/bot/utils/Utils.java @@ -103,4 +103,8 @@ public final class Utils { public static double radToDeg(double rad) { return rad * 180.0 / Math.PI; } + + public static BlockPos diff(BlockPos a, BlockPos b) { + return new BlockPos(a.getX() - b.getX(), a.getY() - b.getY(), a.getZ() - b.getZ()); + } } From ef3c9999c8969325efacbdd8e8c774b1006b336a Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 9 Aug 2018 17:07:35 -0500 Subject: [PATCH 4/6] LookBehaviorUtils cleanups --- .../bot/behavior/impl/LookBehaviorUtils.java | 72 +++++++++---------- src/main/java/baritone/bot/utils/Utils.java | 14 +++- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java b/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java index 37a9afd5..0d2ca2fc 100644 --- a/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java +++ b/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java @@ -17,7 +17,6 @@ package baritone.bot.behavior.impl; -import baritone.bot.pathing.movement.MovementHelper; import baritone.bot.utils.BlockStateInterface; import baritone.bot.utils.Helper; import baritone.bot.utils.Rotation; @@ -27,15 +26,20 @@ import net.minecraft.util.math.*; import java.util.Optional; +import static baritone.bot.utils.Utils.DEG_TO_RAD; + public final class LookBehaviorUtils implements Helper { - public static final Vec3d[] BLOCK_SIDE_MULTIPLIERS = new Vec3d[]{ - new Vec3d(0, 0.5, 0.5), - new Vec3d(1, 0.5, 0.5), - new Vec3d(0.5, 0, 0.5), - new Vec3d(0.5, 1, 0.5), - new Vec3d(0.5, 0.5, 0), - new Vec3d(0.5, 0.5, 1) + /** + * Offsets from the root block position to the center of each side. + */ + private static final Vec3d[] BLOCK_SIDE_MULTIPLIERS = new Vec3d[] { + new Vec3d(0.5, 0, 0.5), // Down + new Vec3d(0.5, 1, 0.5), // Up + new Vec3d(0.5, 0.5, 0), // North + new Vec3d(0.5, 0.5, 1), // South + new Vec3d(0, 0.5, 0.5), // West + new Vec3d(1, 0.5, 0.5) // East }; /** @@ -45,10 +49,10 @@ public final class LookBehaviorUtils implements Helper { * @return vector of the rotation */ public static Vec3d calcVec3dFromRotation(Rotation rotation) { - float f = MathHelper.cos(-rotation.getFirst() * 0.017453292F - (float) Math.PI); - float f1 = MathHelper.sin(-rotation.getFirst() * 0.017453292F - (float) Math.PI); - float f2 = -MathHelper.cos(-rotation.getSecond() * 0.017453292F); - float f3 = MathHelper.sin(-rotation.getSecond() * 0.017453292F); + float f = MathHelper.cos(-rotation.getFirst() * (float) DEG_TO_RAD - (float) Math.PI); + float f1 = MathHelper.sin(-rotation.getFirst() * (float) DEG_TO_RAD - (float) Math.PI); + float f2 = -MathHelper.cos(-rotation.getSecond() * (float) DEG_TO_RAD); + float f3 = MathHelper.sin(-rotation.getSecond() * (float) DEG_TO_RAD); return new Vec3d((double) (f1 * f2), (double) f3, (double) (f * f2)); } @@ -60,16 +64,13 @@ public final class LookBehaviorUtils implements Helper { if (possibleRotation.isPresent()) return possibleRotation; - IBlockState bstate = BlockStateInterface.get(pos); - AxisAlignedBB bbox = bstate.getBoundingBox(mc.world, pos); - for (Vec3d vecMult : BLOCK_SIDE_MULTIPLIERS) { - double xDiff = bbox.minX * vecMult.x + bbox.maxX * (1 - vecMult.x);//lol - double yDiff = bbox.minY * vecMult.y + bbox.maxY * (1 - vecMult.y); - double zDiff = bbox.minZ * vecMult.z + bbox.maxZ * (1 - vecMult.z); - double x = pos.getX() + xDiff; - double y = pos.getY() + yDiff; - double z = pos.getZ() + zDiff; - possibleRotation = reachableRotation(pos, new Vec3d(x, y, z)); + IBlockState state = BlockStateInterface.get(pos); + AxisAlignedBB aabb = state.getBoundingBox(mc.world, pos); + for (Vec3d sideOffset : BLOCK_SIDE_MULTIPLIERS) { + double xDiff = aabb.minX * sideOffset.x + aabb.maxX * (1 - sideOffset.x); + double yDiff = aabb.minY * sideOffset.y + aabb.maxY * (1 - sideOffset.y); + double zDiff = aabb.minZ * sideOffset.z + aabb.maxZ * (1 - sideOffset.z); + possibleRotation = reachableRotation(pos, new Vec3d(pos).add(xDiff, yDiff, zDiff)); if (possibleRotation.isPresent()) return possibleRotation; } @@ -77,25 +78,26 @@ public final class LookBehaviorUtils implements Helper { } private static RayTraceResult rayTraceTowards(Rotation rotation) { - double blockReachDistance = (double) mc.playerController.getBlockReachDistance(); - Vec3d vec3 = mc.player.getPositionEyes(1.0F); - Vec3d vec31 = calcVec3dFromRotation(rotation); - Vec3d vec32 = vec3.add(vec31.x * blockReachDistance, - vec31.y * blockReachDistance, - vec31.z * blockReachDistance); - return mc.world.rayTraceBlocks(vec3, vec32, false, false, true); + double blockReachDistance = mc.playerController.getBlockReachDistance(); + Vec3d start = mc.player.getPositionEyes(1.0F); + Vec3d direction = calcVec3dFromRotation(rotation); + Vec3d end = start.add( + direction.x * blockReachDistance, + direction.y * blockReachDistance, + direction.z * blockReachDistance + ); + return mc.world.rayTraceBlocks(start, end, false, false, true); } /** * Checks if coordinate is reachable with the given block-face rotation offset * * @param pos - * @param offset + * @param offsetPos * @return */ - protected static Optional reachableRotation(BlockPos pos, Vec3d offset) { - Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F), - offset); + protected static Optional reachableRotation(BlockPos pos, Vec3d offsetPos) { + Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F), offsetPos); RayTraceResult result = rayTraceTowards(rotation); if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK @@ -111,8 +113,7 @@ public final class LookBehaviorUtils implements Helper { * @return */ protected static Optional reachableCenter(BlockPos pos) { - Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F), - Utils.calcCenterFromCoords(pos, mc.world)); + Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F), Utils.calcCenterFromCoords(pos, mc.world)); RayTraceResult result = rayTraceTowards(rotation); if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK @@ -133,5 +134,4 @@ public final class LookBehaviorUtils implements Helper { } return Optional.empty(); } - } diff --git a/src/main/java/baritone/bot/utils/Utils.java b/src/main/java/baritone/bot/utils/Utils.java index 25803767..16764e50 100755 --- a/src/main/java/baritone/bot/utils/Utils.java +++ b/src/main/java/baritone/bot/utils/Utils.java @@ -30,6 +30,16 @@ import net.minecraft.world.World; */ public final class Utils { + /** + * Constant that a degree value is multiplied by to get the equivalent radian value + */ + public static final double DEG_TO_RAD = Math.PI / 180.0; + + /** + * Constant that a radian value is multiplied by to get the equivalent degree value + */ + public static final double RAD_TO_DEG = 180.0 / Math.PI; + public static Rotation calcRotationFromCoords(BlockPos orig, BlockPos dest) { return calcRotationFromVec3d(vec3dFromBlockPos(orig), vec3dFromBlockPos(dest)); } @@ -97,11 +107,11 @@ public final class Utils { } public static double degToRad(double deg) { - return deg * Math.PI / 180.0; + return deg * DEG_TO_RAD; } public static double radToDeg(double rad) { - return rad * 180.0 / Math.PI; + return rad * RAD_TO_DEG; } public static BlockPos diff(BlockPos a, BlockPos b) { From 4ea4e5fa430e3c390d2b681602e649ba2a5b2230 Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 9 Aug 2018 18:24:46 -0500 Subject: [PATCH 5/6] Simplify LookBehaviorUtils's reachableCenter method --- .../bot/behavior/impl/LookBehaviorUtils.java | 12 +++--------- src/main/java/baritone/bot/utils/Utils.java | 6 ++++-- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java b/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java index 0d2ca2fc..443b594e 100644 --- a/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java +++ b/src/main/java/baritone/bot/behavior/impl/LookBehaviorUtils.java @@ -70,7 +70,7 @@ public final class LookBehaviorUtils implements Helper { double xDiff = aabb.minX * sideOffset.x + aabb.maxX * (1 - sideOffset.x); double yDiff = aabb.minY * sideOffset.y + aabb.maxY * (1 - sideOffset.y); double zDiff = aabb.minZ * sideOffset.z + aabb.maxZ * (1 - sideOffset.z); - possibleRotation = reachableRotation(pos, new Vec3d(pos).add(xDiff, yDiff, zDiff)); + possibleRotation = reachableOffset(pos, new Vec3d(pos).add(xDiff, yDiff, zDiff)); if (possibleRotation.isPresent()) return possibleRotation; } @@ -96,7 +96,7 @@ public final class LookBehaviorUtils implements Helper { * @param offsetPos * @return */ - protected static Optional reachableRotation(BlockPos pos, Vec3d offsetPos) { + protected static Optional reachableOffset(BlockPos pos, Vec3d offsetPos) { Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F), offsetPos); RayTraceResult result = rayTraceTowards(rotation); if (result != null @@ -113,13 +113,7 @@ public final class LookBehaviorUtils implements Helper { * @return */ protected static Optional reachableCenter(BlockPos pos) { - Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F), Utils.calcCenterFromCoords(pos, mc.world)); - RayTraceResult result = rayTraceTowards(rotation); - if (result != null - && result.typeOfHit == RayTraceResult.Type.BLOCK - && result.getBlockPos().equals(pos)) - return Optional.of(rotation); - return Optional.empty(); + return reachableOffset(pos, Utils.calcCenterFromCoords(pos, mc.world)); } /** diff --git a/src/main/java/baritone/bot/utils/Utils.java b/src/main/java/baritone/bot/utils/Utils.java index 16764e50..acad8c85 100755 --- a/src/main/java/baritone/bot/utils/Utils.java +++ b/src/main/java/baritone/bot/utils/Utils.java @@ -79,9 +79,11 @@ public final class Utils { double xDiff = (bbox.minX + bbox.maxX) / 2; double yDiff = (bbox.minY + bbox.maxY) / 2; double zDiff = (bbox.minZ + bbox.maxZ) / 2; - return new Vec3d(orig.getX() + xDiff, + return new Vec3d( + orig.getX() + xDiff, orig.getY() + yDiff, - orig.getZ() + zDiff); + orig.getZ() + zDiff + ); } public static Vec3d getBlockPosCenter(BlockPos pos) { From 9fd207040e67195eef9bce8b085fcfe78298b211 Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 9 Aug 2018 18:26:17 -0500 Subject: [PATCH 6/6] Fix unnecessary unboxing/reboxing of Floats in Rotation --- src/main/java/baritone/bot/utils/Rotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baritone/bot/utils/Rotation.java b/src/main/java/baritone/bot/utils/Rotation.java index 3e6cb08f..76f2095d 100644 --- a/src/main/java/baritone/bot/utils/Rotation.java +++ b/src/main/java/baritone/bot/utils/Rotation.java @@ -21,7 +21,7 @@ import net.minecraft.util.Tuple; public class Rotation extends Tuple { - public Rotation(float yaw, float pitch) { + public Rotation(Float yaw, Float pitch) { super(yaw, pitch); } }