From 5a9e5cdac416c0810c376f63549ebe6408656381 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Mon, 8 Oct 2018 15:11:07 -0700 Subject: [PATCH] move rendering, fixes #212 --- .../baritone/behavior/PathingBehavior.java | 52 +--------------- .../java/baritone/utils/PathRenderer.java | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+), 51 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 15748432..2092c656 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -42,7 +42,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.chunk.EmptyChunk; import java.util.Collection; -import java.util.Collections; import java.util.HashSet; import java.util.Optional; import java.util.stream.Collectors; @@ -388,56 +387,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, @Override public void onRenderPass(RenderEvent event) { - // System.out.println("Render passing"); - // System.out.println(event.getPartialTicks()); - float partialTicks = event.getPartialTicks(); - if (goal != null && Baritone.settings().renderGoal.value) { - PathRenderer.drawLitDankGoalBox(player(), goal, partialTicks, Baritone.settings().colorGoalBox.get()); - } - if (!Baritone.settings().renderPath.get()) { - return; - } - - //long start = System.nanoTime(); - - - PathExecutor current = this.current; // this should prevent most race conditions? - PathExecutor next = this.next; // like, now it's not possible for current!=null to be true, then suddenly false because of another thread - // TODO is this enough, or do we need to acquire a lock here? - // TODO benchmark synchronized in render loop - - // Render the current path, if there is one - if (current != null && current.getPath() != null) { - int renderBegin = Math.max(current.getPosition() - 3, 0); - PathRenderer.drawPath(current.getPath(), renderBegin, player(), partialTicks, Baritone.settings().colorCurrentPath.get(), Baritone.settings().fadePath.get(), 10, 20); - } - if (next != null && next.getPath() != null) { - PathRenderer.drawPath(next.getPath(), 0, player(), partialTicks, Baritone.settings().colorNextPath.get(), Baritone.settings().fadePath.get(), 10, 20); - } - - //long split = System.nanoTime(); - if (current != null) { - PathRenderer.drawManySelectionBoxes(player(), current.toBreak(), partialTicks, Baritone.settings().colorBlocksToBreak.get()); - PathRenderer.drawManySelectionBoxes(player(), current.toPlace(), partialTicks, Baritone.settings().colorBlocksToPlace.get()); - PathRenderer.drawManySelectionBoxes(player(), current.toWalkInto(), partialTicks, Baritone.settings().colorBlocksToWalkInto.get()); - } - - // If there is a path calculation currently running, render the path calculation process - AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(currentlyRunning -> { - currentlyRunning.bestPathSoFar().ifPresent(p -> { - PathRenderer.drawPath(p, 0, player(), partialTicks, Baritone.settings().colorBestPathSoFar.get(), Baritone.settings().fadePath.get(), 10, 20); - }); - currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> { - - PathRenderer.drawPath(mr, 0, player(), partialTicks, Baritone.settings().colorMostRecentConsidered.get(), Baritone.settings().fadePath.get(), 10, 20); - PathRenderer.drawManySelectionBoxes(player(), Collections.singletonList(mr.getDest()), partialTicks, Baritone.settings().colorMostRecentConsidered.get()); - }); - }); - //long end = System.nanoTime(); - //System.out.println((end - split) + " " + (split - start)); - // if (end - start > 0) { - // System.out.println("Frame took " + (split - start) + " " + (end - split)); - //} + PathRenderer.render(event, this); } @Override diff --git a/src/main/java/baritone/utils/PathRenderer.java b/src/main/java/baritone/utils/PathRenderer.java index ba39a06a..6891a299 100644 --- a/src/main/java/baritone/utils/PathRenderer.java +++ b/src/main/java/baritone/utils/PathRenderer.java @@ -18,12 +18,16 @@ package baritone.utils; import baritone.Baritone; +import baritone.api.event.events.RenderEvent; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalComposite; import baritone.api.pathing.goals.GoalTwoBlocks; import baritone.api.pathing.goals.GoalXZ; import baritone.api.utils.interfaces.IGoalRenderPos; +import baritone.behavior.PathingBehavior; +import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.path.IPath; +import baritone.pathing.path.PathExecutor; import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; @@ -40,6 +44,7 @@ import net.minecraft.util.math.MathHelper; import java.awt.*; import java.util.Collection; +import java.util.Collections; import java.util.List; import static org.lwjgl.opengl.GL11.*; @@ -55,6 +60,61 @@ public final class PathRenderer implements Helper { private PathRenderer() {} + public static void render(RenderEvent event, PathingBehavior behavior) { + // System.out.println("Render passing"); + // System.out.println(event.getPartialTicks()); + float partialTicks = event.getPartialTicks(); + Goal goal = behavior.getGoal(); + EntityPlayerSP player = mc.player; + if (goal != null && Baritone.settings().renderGoal.value) { + PathRenderer.drawLitDankGoalBox(player, goal, partialTicks, Baritone.settings().colorGoalBox.get()); + } + if (!Baritone.settings().renderPath.get()) { + return; + } + + //long start = System.nanoTime(); + + + PathExecutor current = behavior.getCurrent(); // this should prevent most race conditions? + PathExecutor next = behavior.getNext(); // like, now it's not possible for current!=null to be true, then suddenly false because of another thread + // TODO is this enough, or do we need to acquire a lock here? + // TODO benchmark synchronized in render loop + + // Render the current path, if there is one + if (current != null && current.getPath() != null) { + int renderBegin = Math.max(current.getPosition() - 3, 0); + PathRenderer.drawPath(current.getPath(), renderBegin, player, partialTicks, Baritone.settings().colorCurrentPath.get(), Baritone.settings().fadePath.get(), 10, 20); + } + if (next != null && next.getPath() != null) { + PathRenderer.drawPath(next.getPath(), 0, player, partialTicks, Baritone.settings().colorNextPath.get(), Baritone.settings().fadePath.get(), 10, 20); + } + + //long split = System.nanoTime(); + if (current != null) { + PathRenderer.drawManySelectionBoxes(player, current.toBreak(), partialTicks, Baritone.settings().colorBlocksToBreak.get()); + PathRenderer.drawManySelectionBoxes(player, current.toPlace(), partialTicks, Baritone.settings().colorBlocksToPlace.get()); + PathRenderer.drawManySelectionBoxes(player, current.toWalkInto(), partialTicks, Baritone.settings().colorBlocksToWalkInto.get()); + } + + // If there is a path calculation currently running, render the path calculation process + AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(currentlyRunning -> { + currentlyRunning.bestPathSoFar().ifPresent(p -> { + PathRenderer.drawPath(p, 0, player, partialTicks, Baritone.settings().colorBestPathSoFar.get(), Baritone.settings().fadePath.get(), 10, 20); + }); + currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> { + + PathRenderer.drawPath(mr, 0, player, partialTicks, Baritone.settings().colorMostRecentConsidered.get(), Baritone.settings().fadePath.get(), 10, 20); + PathRenderer.drawManySelectionBoxes(player, Collections.singletonList(mr.getDest()), partialTicks, Baritone.settings().colorMostRecentConsidered.get()); + }); + }); + //long end = System.nanoTime(); + //System.out.println((end - split) + " " + (split - start)); + // if (end - start > 0) { + // System.out.println("Frame took " + (split - start) + " " + (end - split)); + //} + } + public static void drawPath(IPath path, int startIndex, EntityPlayerSP player, float partialTicks, Color color, boolean fadeOut, int fadeStart0, int fadeEnd0) { GlStateManager.enableBlend(); GlStateManager.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);