baritone/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java

180 lines
6.3 KiB
Java
Raw Normal View History

2018-08-05 05:15:18 +00:00
package baritone.bot.behavior.impl;
2018-08-05 03:19:32 +00:00
2018-08-05 05:15:18 +00:00
import baritone.bot.behavior.Behavior;
2018-08-05 22:38:11 +00:00
import baritone.bot.event.events.ChatEvent;
2018-08-05 21:48:10 +00:00
import baritone.bot.event.events.RenderEvent;
2018-08-05 22:38:11 +00:00
import baritone.bot.pathing.calc.AStarPathFinder;
import baritone.bot.pathing.calc.IPathFinder;
import baritone.bot.pathing.goals.Goal;
import baritone.bot.pathing.goals.GoalBlock;
2018-08-05 03:28:32 +00:00
import baritone.bot.pathing.path.IPath;
2018-08-05 03:25:05 +00:00
import baritone.bot.pathing.path.PathExecutor;
2018-08-05 22:38:11 +00:00
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.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import org.lwjgl.opengl.GL11;
import java.awt.*;
import java.util.List;
2018-08-05 03:25:05 +00:00
2018-08-05 03:28:32 +00:00
public class PathingBehavior extends Behavior {
2018-08-05 05:25:08 +00:00
2018-08-05 03:28:32 +00:00
public static final PathingBehavior INSTANCE = new PathingBehavior();
2018-08-05 05:25:08 +00:00
private PathingBehavior() {}
2018-08-05 03:25:05 +00:00
private PathExecutor current;
2018-08-05 22:38:11 +00:00
private Goal goal;
2018-08-05 03:28:32 +00:00
@Override
2018-08-05 23:56:21 +00:00
public void onTick() {
// System.out.println("Ticking");
2018-08-05 03:28:32 +00:00
if (current == null) {
return;
}
2018-08-05 23:56:21 +00:00
// current.onTick();
2018-08-05 03:28:32 +00:00
if (current.failed() || current.finished()) {
current = null;
}
}
public PathExecutor getExecutor() {
return current;
}
public IPath getPath() {
2018-08-05 21:20:55 +00:00
if (current == null) {
return null;
}
2018-08-05 03:28:32 +00:00
return current.getPath();
}
2018-08-05 21:48:10 +00:00
2018-08-05 22:38:11 +00:00
@Override
public void onSendChatMessage(ChatEvent event) {
String msg = event.getMessage();
if (msg.equals("goal")) {
goal = new GoalBlock(playerFeet());
2018-08-05 23:56:21 +00:00
displayChatMessageRaw("Goal: " + goal);
2018-08-05 22:38:11 +00:00
event.cancel();
return;
}
if (msg.equals("path")) {
findPathInNewThread(playerFeet(), true);
event.cancel();
return;
}
}
/**
* In a new thread, pathfind to target blockpos
*
* @param start
* @param talkAboutIt
*/
public void findPathInNewThread(final BlockPos start, final boolean talkAboutIt) {
2018-08-05 23:56:21 +00:00
new Thread(() -> {
if (talkAboutIt) {
displayChatMessageRaw("Starting to search for path from " + start + " to " + goal);
}
2018-08-05 22:38:11 +00:00
2018-08-05 23:56:21 +00:00
try {
IPath path = findPath(start);
if (path != null) {
current = new PathExecutor(path);
2018-08-05 22:38:11 +00:00
}
2018-08-05 23:56:21 +00:00
} catch (Exception ignored) {}
/*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);
2018-08-05 22:38:11 +00:00
}
2018-08-05 23:56:21 +00:00
planAhead();
} else if (talkAboutIt) {
Out.gui("Finished finding a path from " + start + " to " + goal + ". " + currentPath.numNodes + " nodes considered", Out.Mode.Debug);
}*/
}).start();
2018-08-05 22:38:11 +00:00
}
/**
* Actually do the pathing
*
* @param start
* @return
*/
private IPath findPath(BlockPos start) {
if (goal == null) {
2018-08-05 23:56:21 +00:00
displayChatMessageRaw("no goal");
2018-08-05 22:38:11 +00:00
return null;
}
try {
IPathFinder pf = new AStarPathFinder(start, goal);
IPath path = pf.calculate();
return path;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
2018-08-05 21:48:10 +00:00
@Override
public void onRenderPass(RenderEvent event) {
2018-08-05 23:56:21 +00:00
// System.out.println("Render passing");
// System.out.println(event.getPartialTicks());
2018-08-05 22:38:11 +00:00
drawPath(player(), event.getPartialTicks(), Color.RED);
}
public void drawPath(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);
IPath path = getPath();
if (path != null) {
List<BlockPos> positions = path.positions();
for (int i = 0; i < positions.size() - 1; i++) {
BlockPos a = positions.get(i);
BlockPos b = positions.get(i + 1);
double x1 = a.getX();
double y1 = a.getY();
double z1 = a.getZ();
double x2 = b.getX();
double y2 = b.getY();
double z2 = b.getZ();
drawLine(player, x1, y1, z1, x2, y2, z2, partialTicks);
}
}
2018-08-05 23:56:21 +00:00
// GlStateManager.color(0.0f, 0.0f, 0.0f, 0.4f);
2018-08-05 22:38:11 +00:00
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 worldrenderer = 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;
worldrenderer.begin(3, DefaultVertexFormats.POSITION);
worldrenderer.pos(bp1x + 0.5D - d0, bp1y + 0.5D - d1, bp1z + 0.5D - d2).endVertex();
worldrenderer.pos(bp2x + 0.5D - d0, bp2y + 0.5D - d1, bp2z + 0.5D - d2).endVertex();
worldrenderer.pos(bp2x + 0.5D - d0, bp2y + 0.53D - d1, bp2z + 0.5D - d2).endVertex();
worldrenderer.pos(bp1x + 0.5D - d0, bp1y + 0.53D - d1, bp1z + 0.5D - d2).endVertex();
worldrenderer.pos(bp1x + 0.5D - d0, bp1y + 0.5D - d1, bp1z + 0.5D - d2).endVertex();
tessellator.draw();
2018-08-05 21:48:10 +00:00
}
2018-08-05 03:19:32 +00:00
}