Skip to content
This repository has been archived by the owner on Aug 10, 2018. It is now read-only.

Commit

Permalink
Merge branch 'pathfinder'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Dec 5, 2016
2 parents 9d36757 + 01fac5a commit 2ebe9ee
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 36 deletions.
75 changes: 75 additions & 0 deletions src/tk/wurst_client/ai/GoRandomAI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright © 2014 - 2016 | Wurst-Imperium | All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package tk.wurst_client.ai;

import net.minecraft.util.math.BlockPos;

public class GoRandomAI
{
private RandomPathFinder pathFinder;
private PathProcessor processor;

private boolean done;

public GoRandomAI(BlockPos start, float range)
{
pathFinder = new RandomPathFinder(start, range);
pathFinder.setThinkTime(10);
pathFinder.setFallingAllowed(false);
}

public void update()
{
done = false;

// find path
if(!pathFinder.isDone() && !pathFinder.isFailed())
{
if(processor != null)
processor.lockControls();

pathFinder.think();

if(!pathFinder.isDone() && !pathFinder.isFailed())
return;

pathFinder.formatPath();

// set processor
processor = pathFinder.getProcessor();
}

// check path
if(processor != null
&& !pathFinder.isPathStillValid(processor.getIndex()))
{
pathFinder = new RandomPathFinder(pathFinder);
return;
}

// process path
if(!processor.isFailed() && !processor.isDone())
processor.process();
else
pathFinder = new RandomPathFinder(pathFinder);

if(processor.isDone())
done = true;
}

public final boolean isDone()
{
return done;
}

public void stop()
{
if(processor != null)
processor.stop();
}
}
24 changes: 15 additions & 9 deletions src/tk/wurst_client/ai/PathFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,30 @@ public class PathFinder

private final boolean invulnerable = mc.player.capabilities.isCreativeMode;
private final boolean creativeFlying = mc.player.capabilities.isFlying;
private final boolean flying =
protected final boolean flying =
creativeFlying || wurst.mods.flightMod.isActive();
private final boolean immuneToFallDamage =
invulnerable || wurst.mods.noFallMod.isActive();
private final boolean noSlowdownActive =
wurst.mods.noSlowdownMod.isActive();
private final boolean jesus = wurst.mods.jesusMod.isActive();
private final boolean spider = wurst.mods.spiderMod.isActive();
protected boolean fallingAllowed = true;

private final PathPos start;
protected PathPos current;
private final BlockPos goal;

private final HashMap<PathPos, Float> costMap = new HashMap<>();
private final HashMap<PathPos, PathPos> prevPosMap = new HashMap<>();
protected final HashMap<PathPos, PathPos> prevPosMap = new HashMap<>();
private final PathQueue queue = new PathQueue();

protected int thinkSpeed = 1024;
protected int thinkTime = 200;
private int iterations;

protected boolean done;
private boolean failed;
protected boolean failed;
private final ArrayList<PathPos> path = new ArrayList<>();

public PathFinder(BlockPos goal)
Expand Down Expand Up @@ -215,13 +216,13 @@ && checkHorizontalMovement(current, next))
return false;
}

private boolean isPassable(BlockPos pos)
protected boolean isPassable(BlockPos pos)
{
return canGoThrough(pos) && canGoThrough(pos.up())
&& canGoAbove(pos.down());
}

private boolean canBeSolid(BlockPos pos)
protected boolean canBeSolid(BlockPos pos)
{
Material material = getMaterial(pos);
Block block = getBlock(pos);
Expand Down Expand Up @@ -293,16 +294,16 @@ private boolean canFallBelow(PathPos pos)
return false;

// check if fall damage is off
if(immuneToFallDamage)
if(immuneToFallDamage && fallingAllowed)
return true;

// check if fall ends with slime block
if(getBlock(down2) instanceof BlockSlime)
if(getBlock(down2) instanceof BlockSlime && fallingAllowed)
return true;

// check fall damage
BlockPos prevPos = pos;
for(int i = 0; i <= 3; i++)
for(int i = 0; i <= (fallingAllowed ? 3 : 1); i++)
{
// check if prevPos does not exist, meaning that the pathfinding
// started during the fall and fall damage should be ignored because
Expand Down Expand Up @@ -401,7 +402,7 @@ private float getHeuristic(BlockPos pos)
* ((dx + dy + dz) - 0.5857864376269049F * Math.min(dx, dz));
}

private Block getBlock(BlockPos pos)
protected Block getBlock(BlockPos pos)
{
return mc.world.getBlockState(pos).getBlock();
}
Expand Down Expand Up @@ -528,4 +529,9 @@ public void setThinkTime(int thinkTime)
{
this.thinkTime = thinkTime;
}

public void setFallingAllowed(boolean fallingAllowed)
{
this.fallingAllowed = fallingAllowed;
}
}
94 changes: 94 additions & 0 deletions src/tk/wurst_client/ai/RandomPathFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright © 2014 - 2016 | Wurst-Imperium | All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package tk.wurst_client.ai;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import net.minecraft.block.BlockLiquid;
import net.minecraft.client.Minecraft;
import net.minecraft.util.math.BlockPos;

public class RandomPathFinder extends PathFinder
{
private final BlockPos center;
private final float rangeSq;
private final int blockRange;
private final Random random;

public RandomPathFinder(BlockPos center, float range)
{
super(new BlockPos(Minecraft.getMinecraft().player));
this.center = center;
this.rangeSq = (float)Math.pow(range, 2);
blockRange = (int)Math.ceil(range);
random = new Random();
}

public RandomPathFinder(RandomPathFinder pathFinder)
{
super(new BlockPos(Minecraft.getMinecraft().player));
thinkSpeed = pathFinder.thinkSpeed;
thinkTime = pathFinder.thinkTime;
fallingAllowed = pathFinder.fallingAllowed;
center = pathFinder.center;
rangeSq = pathFinder.rangeSq;
blockRange = pathFinder.blockRange;
random = pathFinder.random;
}

@Override
protected boolean isPassable(BlockPos pos)
{
if(Math.abs(center.getX() - pos.getX()) > blockRange)
return false;
if(Math.abs(center.getY() - pos.getY()) > blockRange)
return false;
if(Math.abs(center.getZ() - pos.getZ()) > blockRange)
return false;

return super.isPassable(pos);
}

@Override
protected boolean checkDone()
{
return false;
}

private void setCurrentToRandomNode()
{
int currentIndex = 0;
int randomIndex = random.nextInt(prevPosMap.size());

for(Iterator<PathPos> itr = prevPosMap.keySet().iterator(); itr
.hasNext(); currentIndex++)
if(currentIndex == randomIndex)
{
current = itr.next();
break;
}else
itr.next();
}

@Override
public ArrayList<PathPos> formatPath()
{
done = true;
failed = false;

do
setCurrentToRandomNode();
while(center.distanceSq(current) > rangeSq
|| (!flying && !canBeSolid(current.down()))
|| getBlock(current.up()) instanceof BlockLiquid);

return super.formatPath();
}
}
57 changes: 30 additions & 27 deletions src/tk/wurst_client/mods/AntiAfkMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,61 @@
import java.util.Random;

import net.minecraft.util.math.BlockPos;
import tk.wurst_client.ai.GoRandomAI;
import tk.wurst_client.events.listeners.UpdateListener;
import tk.wurst_client.utils.BlockUtils;

@Mod.Info(name = "AntiAFK",
description = "Walks around randomly to hide you from AFK detectors.\n"
+ "Needs 3x3 blocks of free space.",
description = "Walks around randomly to hide you from AFK detectors.",
tags = "AFKBot,anti afk,afk bot",
help = "Mods/AntiAFK")
@Mod.Bypasses(ghostMode = false)
public class AntiAfkMod extends Mod implements UpdateListener
{
private BlockPos block;
private Random random;
private BlockPos nextBlock;
private GoRandomAI ai;
private int timer;
private Random random = new Random();

@Override
public void onEnable()
{
try
{
block = new BlockPos(mc.player);
}catch(Exception e)
{
e.printStackTrace();
}
random = new Random();
ai = new GoRandomAI(new BlockPos(mc.player), 16F);
wurst.events.add(UpdateListener.class, this);
}

@Override
public void onUpdate()
{
updateMS();
if(hasTimePassedM(3000) || nextBlock == null)
// check if player died
if(mc.player.getHealth() <= 0)
{
setEnabled(false);
return;
}

if(timer > 0)
{
timer--;
mc.gameSettings.keyBindJump.pressed = mc.player.isInWater();
return;
}

// walk around
ai.update();

// wait 2 - 3 seconds (40 - 60 ticks)
if(ai.isDone())
{
if(block == null)
onEnable();
nextBlock =
block.add(random.nextInt(3) - 1, 0, random.nextInt(3) - 1);
updateLastMS();
ai.stop();
timer = 40 + random.nextInt(21);
}
BlockUtils.faceBlockClientHorizontally(nextBlock);
if(BlockUtils.getHorizontalPlayerBlockDistance(nextBlock) > 0.75)
mc.gameSettings.keyBindForward.pressed = true;
else
mc.gameSettings.keyBindForward.pressed = false;
}

@Override
public void onDisable()
{
wurst.events.remove(UpdateListener.class, this);
mc.gameSettings.keyBindForward.pressed = false;

if(ai != null)
ai.stop();
}
}

0 comments on commit 2ebe9ee

Please sign in to comment.