render optim

This commit is contained in:
Leijurv 2018-08-09 16:37:08 -07:00
parent d3891a8f11
commit 71c36a7b0b
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 92 additions and 47 deletions

View File

@ -28,7 +28,6 @@ import baritone.bot.pathing.calc.IPathFinder;
import baritone.bot.pathing.goals.Goal; import baritone.bot.pathing.goals.Goal;
import baritone.bot.pathing.goals.GoalBlock; import baritone.bot.pathing.goals.GoalBlock;
import baritone.bot.pathing.goals.GoalXZ; import baritone.bot.pathing.goals.GoalXZ;
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.BlockStateInterface;
@ -47,6 +46,8 @@ import net.minecraft.util.math.BlockPos;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import java.awt.*; import java.awt.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -175,23 +176,18 @@ public class PathingBehavior extends Behavior {
//System.out.println("Render passing"); //System.out.println("Render passing");
//System.out.println(event.getPartialTicks()); //System.out.println(event.getPartialTicks());
float partialTicks = event.getPartialTicks(); float partialTicks = event.getPartialTicks();
long start = System.currentTimeMillis(); long start = System.nanoTime();
// 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); drawPath(current.getPath(), renderBegin, player(), partialTicks, Color.RED);
} }
long split = System.currentTimeMillis(); long split = System.nanoTime();
getPath().ifPresent(path -> { if (current != null) {
for (Movement m : path.movements()) { drawManySelectionBoxes(player(), current.toBreak(), partialTicks, Color.RED);
for (BlockPos pos : m.toPlace()) drawManySelectionBoxes(player(), current.toPlace(), partialTicks, Color.GREEN);
drawSelectionBox(player(), pos, partialTicks, Color.GREEN); }
for (BlockPos pos : m.toBreak()) {
drawSelectionBox(player(), pos, partialTicks, Color.RED);
}
}
});
// 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 -> {
@ -199,11 +195,12 @@ public class PathingBehavior extends Behavior {
drawPath(p, 0, player(), partialTicks, Color.BLUE); drawPath(p, 0, player(), partialTicks, Color.BLUE);
currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> { currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> {
drawPath(mr, 0, player(), partialTicks, Color.CYAN); drawPath(mr, 0, player(), partialTicks, Color.CYAN);
drawSelectionBox(player(), mr.getDest(), partialTicks, Color.CYAN); drawManySelectionBoxes(player(), Arrays.asList(mr.getDest()), partialTicks, Color.CYAN);
}); });
}); });
}); });
long end = System.currentTimeMillis(); long end = System.nanoTime();
//System.out.println((end - split) + " " + (split - start));
// 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));
} }
@ -263,7 +260,7 @@ public class PathingBehavior extends Behavior {
tessellator.draw(); tessellator.draw();
} }
public static void drawSelectionBox(EntityPlayer player, BlockPos blockpos, float partialTicks, Color color) { public static void drawManySelectionBoxes(EntityPlayer player, Collection<BlockPos> positions, float partialTicks, Color color) {
GlStateManager.enableBlend(); GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
GlStateManager.color(color.getColorComponents(null)[0], color.getColorComponents(null)[1], color.getColorComponents(null)[2], 0.4F); GlStateManager.color(color.getColorComponents(null)[0], color.getColorComponents(null)[1], color.getColorComponents(null)[2], 0.4F);
@ -272,41 +269,47 @@ public class PathingBehavior extends Behavior {
GlStateManager.depthMask(false); GlStateManager.depthMask(false);
float f = 0.002F; float f = 0.002F;
//BlockPos blockpos = movingObjectPositionIn.getBlockPos(); //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 d0 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) partialTicks;
double d1 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialTicks; double d1 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) partialTicks;
double d2 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (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);
Tessellator tessellator = Tessellator.getInstance(); Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.getBuffer(); BufferBuilder buffer = tessellator.getBuffer();
buffer.begin(3, DefaultVertexFormats.POSITION); for (BlockPos pos : positions) {
buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex(); IBlockState state = BlockStateInterface.get(pos);
buffer.pos(toDraw.maxX, toDraw.minY, toDraw.minZ).endVertex(); Block block = state.getBlock();
buffer.pos(toDraw.maxX, toDraw.minY, toDraw.maxZ).endVertex(); AxisAlignedBB toDraw;
buffer.pos(toDraw.minX, toDraw.minY, toDraw.maxZ).endVertex(); if (block.equals(Blocks.AIR)) {
buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex(); toDraw = Blocks.DIRT.getSelectedBoundingBox(state, Minecraft.getMinecraft().world, pos);
tessellator.draw(); } else {
buffer.begin(3, DefaultVertexFormats.POSITION); toDraw = state.getSelectedBoundingBox(Minecraft.getMinecraft().world, pos);
buffer.pos(toDraw.minX, toDraw.maxY, toDraw.minZ).endVertex(); }
buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.minZ).endVertex(); toDraw = toDraw.expand(0.0020000000949949026D, 0.0020000000949949026D, 0.0020000000949949026D).offset(-d0, -d1, -d2);
buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.maxZ).endVertex(); buffer.begin(3, DefaultVertexFormats.POSITION);
buffer.pos(toDraw.minX, toDraw.maxY, toDraw.maxZ).endVertex(); 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();
tessellator.draw(); buffer.pos(toDraw.maxX, toDraw.minY, toDraw.maxZ).endVertex();
buffer.begin(1, DefaultVertexFormats.POSITION); buffer.pos(toDraw.minX, toDraw.minY, toDraw.maxZ).endVertex();
buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex(); buffer.pos(toDraw.minX, toDraw.minY, toDraw.minZ).endVertex();
buffer.pos(toDraw.minX, toDraw.maxY, toDraw.minZ).endVertex(); tessellator.draw();
buffer.pos(toDraw.maxX, toDraw.minY, toDraw.minZ).endVertex(); buffer.begin(3, DefaultVertexFormats.POSITION);
buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.minZ).endVertex(); buffer.pos(toDraw.minX, toDraw.maxY, toDraw.minZ).endVertex();
buffer.pos(toDraw.maxX, toDraw.minY, toDraw.maxZ).endVertex(); buffer.pos(toDraw.maxX, toDraw.maxY, toDraw.minZ).endVertex();
buffer.pos(toDraw.maxX, toDraw.maxY, 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();
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(); tessellator.draw();
GlStateManager.depthMask(true); GlStateManager.depthMask(true);
GlStateManager.enableTexture2D(); GlStateManager.enableTexture2D();

View File

@ -29,6 +29,10 @@ import net.minecraft.init.Blocks;
import net.minecraft.util.Tuple; import net.minecraft.util.Tuple;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static baritone.bot.pathing.movement.MovementState.MovementStatus.*; import static baritone.bot.pathing.movement.MovementState.MovementStatus.*;
/** /**
@ -45,6 +49,9 @@ public class PathExecutor extends Behavior {
private int ticksAway; private int ticksAway;
private int ticksOnCurrent; private int ticksOnCurrent;
private boolean failed; private boolean failed;
private boolean recalcBP = true;
private HashSet<BlockPos> toBreak = new HashSet<>();
private HashSet<BlockPos> toPlace = new HashSet<>();
public PathExecutor(IPath path) { public PathExecutor(IPath path) {
this.path = path; this.path = path;
@ -136,12 +143,39 @@ public class PathExecutor extends Behavior {
} }
} }
}*/ }*/
long start = System.currentTimeMillis();
for (int i = pathPosition - 10; i < pathPosition + 10; i++) { for (int i = pathPosition - 10; i < pathPosition + 10; i++) {
if (i >= 0 && i < path.movements().size()) { if (i >= 0 && i < path.movements().size()) {
path.movements().get(i).toBreakCached = null; Movement m = path.movements().get(i);
path.movements().get(i).toPlaceCached = null; HashSet<BlockPos> prevBreak = new HashSet<>(m.toBreak());
HashSet<BlockPos> prevPlace = new HashSet<>(m.toPlace());
m.toBreakCached = null;
m.toPlaceCached = null;
m.toBreak();
m.toPlace();
if (!prevBreak.equals(new HashSet<>(m.toBreak()))) {
recalcBP = true;
}
if (!prevPlace.equals(new HashSet<>(m.toPlace()))) {
recalcBP = true;
}
} }
} }
if (recalcBP) {
HashSet<BlockPos> newBreak = new HashSet<>();
HashSet<BlockPos> newPlace = new HashSet<>();
for (int i = 0; i < path.movements().size(); i++) {
newBreak.addAll(path.movements().get(i).toBreak());
newPlace.addAll(path.movements().get(i).toPlace());
}
toBreak = newBreak;
toPlace = newPlace;
recalcBP = false;
}
long end = System.currentTimeMillis();
if (end - start > 0) {
displayChatMessageRaw("Recalculating break and place took " + (end - start) + "ms");
}
Movement movement = path.movements().get(pathPosition); Movement movement = path.movements().get(pathPosition);
if (movement.recalculateCost() >= ActionCosts.COST_INF) { if (movement.recalculateCost() >= ActionCosts.COST_INF) {
displayChatMessageRaw("Something has changed in the world and this movement has become impossible. Cancelling."); displayChatMessageRaw("Something has changed in the world and this movement has become impossible. Cancelling.");
@ -192,4 +226,12 @@ public class PathExecutor extends Behavior {
public boolean finished() { public boolean finished() {
return pathPosition >= path.length(); return pathPosition >= path.length();
} }
public Set<BlockPos> toBreak() {
return Collections.unmodifiableSet(toBreak);
}
public Set<BlockPos> toPlace() {
return Collections.unmodifiableSet(toPlace);
}
} }