Move path rendering methods to new class

This commit is contained in:
Brady 2018-08-09 16:48:10 -05:00
parent 2b6d86dcdd
commit 80ef73c279
No known key found for this signature in database
GPG Key ID: 73A788379A197567
3 changed files with 169 additions and 138 deletions

View File

@ -31,23 +31,10 @@ import baritone.bot.pathing.goals.GoalXZ;
import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.Movement;
import baritone.bot.pathing.path.IPath; import baritone.bot.pathing.path.IPath;
import baritone.bot.pathing.path.PathExecutor; import baritone.bot.pathing.path.PathExecutor;
import baritone.bot.utils.BlockStateInterface; import baritone.bot.utils.PathRenderer;
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 net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import org.lwjgl.opengl.GL11;
import java.awt.*; import java.awt.*;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public class PathingBehavior extends Behavior { public class PathingBehavior extends Behavior {
@ -73,14 +60,6 @@ public class PathingBehavior extends Behavior {
} }
} }
public PathExecutor getExecutor() {
return current;
}
public Optional<IPath> getPath() {
return Optional.ofNullable(current).map(PathExecutor::getPath);
}
@Override @Override
public void onSendChatMessage(ChatEvent event) { public void onSendChatMessage(ChatEvent event) {
String msg = event.getMessage(); String msg = event.getMessage();
@ -115,6 +94,14 @@ public class PathingBehavior extends Behavior {
} }
} }
public PathExecutor getExecutor() {
return current;
}
public Optional<IPath> getPath() {
return Optional.ofNullable(current).map(PathExecutor::getPath);
}
/** /**
* In a new thread, pathfind to target blockpos * 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); findPath(start).map(PathExecutor::new).ifPresent(path -> current = path);
/*isThereAnythingInProgress = false; /*
isThereAnythingInProgress = false;
if (!currentPath.goal.isInGoal(currentPath.end)) { if (!currentPath.goal.isInGoal(currentPath.end)) {
if (talkAboutIt) { 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); 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(); planAhead();
} else if (talkAboutIt) { } else if (talkAboutIt) {
Out.gui(, Out.Mode.Debug); Out.gui(, Out.Mode.Debug);
}*/ }
*/
if (talkAboutIt && current != null && current.getPath() != null) { if (talkAboutIt && current != null && current.getPath() != null) {
displayChatMessageRaw("Finished finding a path from " + start + " to " + goal + ". " + current.getPath().getNumNodesConsidered() + " nodes considered"); displayChatMessageRaw("Finished finding a path from " + start + " to " + goal + ". " + current.getPath().getNumNodesConsidered() + " nodes considered");
} }
@ -173,15 +162,15 @@ public class PathingBehavior extends Behavior {
// Render the current path, if there is one // Render the current path, if there is one
if (current != null && current.getPath() != null) { if (current != null && current.getPath() != null) {
int renderBegin = Math.max(current.getPosition() - 3, 0); 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(); long split = System.currentTimeMillis();
getPath().ifPresent(path -> { getPath().ifPresent(path -> {
for (Movement m : path.movements()) { for (Movement m : path.movements()) {
for (BlockPos pos : m.toPlace()) for (BlockPos pos : m.toPlace())
drawSelectionBox(player(), pos, partialTicks, Color.GREEN); PathRenderer.drawSelectionBox(player(), pos, partialTicks, Color.GREEN);
for (BlockPos pos : m.toBreak()) { for (BlockPos pos : m.toBreak()) {
drawSelectionBox(player(), pos, partialTicks, Color.RED); PathRenderer.drawSelectionBox(player(), pos, partialTicks, Color.RED);
} }
} }
}); });
@ -189,10 +178,10 @@ public class PathingBehavior extends Behavior {
// If there is a path calculation currently running, render the path calculation process // If there is a path calculation currently running, render the path calculation process
AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(currentlyRunning -> { AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(currentlyRunning -> {
currentlyRunning.bestPathSoFar().ifPresent(p -> { currentlyRunning.bestPathSoFar().ifPresent(p -> {
drawPath(p, 0, player(), partialTicks, Color.BLUE); PathRenderer.drawPath(p, 0, player(), partialTicks, Color.BLUE);
currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> { currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> {
drawPath(mr, 0, player(), partialTicks, Color.CYAN); PathRenderer.drawPath(mr, 0, player(), partialTicks, Color.CYAN);
drawSelectionBox(player(), mr.getDest(), partialTicks, Color.CYAN); PathRenderer.drawSelectionBox(player(), mr.getDest(), partialTicks, Color.CYAN);
}); });
}); });
}); });
@ -200,109 +189,4 @@ public class PathingBehavior extends Behavior {
// if (end - start > 0) // if (end - start > 0)
// System.out.println("Frame took " + (split - start) + " " + (end - split)); // 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<BlockPos> 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();
}
} }

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<BlockPos> 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();
}
}

View File

@ -103,4 +103,8 @@ public final class Utils {
public static double radToDeg(double rad) { public static double radToDeg(double rad) {
return rad * 180.0 / Math.PI; 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());
}
} }