Fixed some following bugs.

This commit is contained in:
batchprogrammer314
2021-06-29 16:29:30 -05:00
parent 68ba0aa28c
commit 70b39259fa
4 changed files with 90 additions and 22 deletions

View File

@@ -23,6 +23,7 @@ public class Bot extends EntityPlayer {
public Vector velocity; public Vector velocity;
private byte aliveTicks;
private byte kbTicks; private byte kbTicks;
private byte jumpTicks; private byte jumpTicks;
private byte groundTicks; private byte groundTicks;
@@ -32,11 +33,13 @@ public class Bot extends EntityPlayer {
private final double frictionMin = 0.01; private final double frictionMin = 0.01;
private final double kbUp = 0.3; private final double kbUp = 0.3;
private Vector offset;
public Bot(MinecraftServer minecraftServer, WorldServer worldServer, GameProfile profile, PlayerInteractManager manager) { public Bot(MinecraftServer minecraftServer, WorldServer worldServer, GameProfile profile, PlayerInteractManager manager) {
super(minecraftServer, worldServer, profile, manager); super(minecraftServer, worldServer, profile, manager);
velocity = new Vector(0, 0, 0); this.velocity = new Vector(0, 0, 0);
kbTicks = 0; this.offset = MathUtils.circleOffset(3);
} }
public static Bot createBot(Location loc, String name, String skin) { public static Bot createBot(Location loc, String name, String skin) {
@@ -96,6 +99,14 @@ public class Bot extends EntityPlayer {
} }
} }
public void setOffset(Vector vector) {
this.offset = vector;
}
public Vector getOffset() {
return offset;
}
public Vector getVelocity() { public Vector getVelocity() {
return velocity.clone(); return velocity.clone();
} }
@@ -115,15 +126,21 @@ public class Bot extends EntityPlayer {
this.velocity.add(vector); this.velocity.add(vector);
} }
public boolean tickDelay(int i) {
return aliveTicks % i == 0;
}
@Override @Override
public void tick() { public void tick() {
super.tick(); super.tick();
aliveTicks++;
if (noDamageTicks > 0) --noDamageTicks; if (noDamageTicks > 0) --noDamageTicks;
if (kbTicks > 0) --kbTicks; if (kbTicks > 0) --kbTicks;
if (jumpTicks > 0) --jumpTicks; if (jumpTicks > 0) --jumpTicks;
if (predictGround()) { if (isOnGround()) {
groundTicks++; groundTicks++;
} else { } else {
groundTicks = 0; groundTicks = 0;
@@ -152,7 +169,7 @@ public class Bot extends EntityPlayer {
double y; double y;
if (groundTicks > 0) { if (isOnGround()) {
velocity.setY(0); velocity.setY(0);
addFriction(); addFriction();
y = 0; y = 0;
@@ -172,7 +189,21 @@ public class Bot extends EntityPlayer {
} }
} }
public boolean predictGround() { public void attack(org.bukkit.entity.Entity entity) {
faceLocation(entity.getLocation());
punch();
attack(((CraftEntity) entity).getHandle());
}
public void punch() {
PacketPlayOutAnimation packet = new PacketPlayOutAnimation(this, 0);
for (Player player : Bukkit.getOnlinePlayers()) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
}
}
@Override
public boolean isOnGround() {
double vy = velocity.getY(); double vy = velocity.getY();
if (vy > 0) { if (vy > 0) {
@@ -183,18 +214,20 @@ public class Bot extends EntityPlayer {
AxisAlignedBB box = getBoundingBox(); AxisAlignedBB box = getBoundingBox();
double[] xVals = new double[] { double[] xVals = new double[] {
box.minX + bbOffset, box.minX,
box.maxX - bbOffset box.maxX
}; };
double[] zVals = new double[] { double[] zVals = new double[] {
box.minZ + bbOffset, box.minZ,
box.maxZ - bbOffset box.maxZ
}; };
for (double x : xVals) { for (double x : xVals) {
for (double z : zVals) { for (double z : zVals) {
return world.getBlockAt(new Location(world, x, locY() - 0.01, z)).getType().isSolid(); if (world.getBlockAt(new Location(world, x, locY() - 0.01, z)).getType().isSolid()) {
return true;
}
} }
} }

View File

@@ -3,6 +3,7 @@ package net.nuggetmc.ai.bot.agent;
import net.nuggetmc.ai.PlayerAI; import net.nuggetmc.ai.PlayerAI;
import net.nuggetmc.ai.bot.Bot; import net.nuggetmc.ai.bot.Bot;
import net.nuggetmc.ai.bot.BotManager; import net.nuggetmc.ai.bot.BotManager;
import net.nuggetmc.ai.utils.PlayerUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -10,18 +11,17 @@ import org.bukkit.util.Vector;
public class BotAgent { public class BotAgent {
private PlayerAI plugin;
private BotManager manager; private BotManager manager;
private byte quarterTick = 0;
public BotAgent(BotManager manager) { public BotAgent(BotManager manager) {
this.plugin = PlayerAI.getInstance();
this.manager = manager; this.manager = manager;
Bukkit.getScheduler().scheduleSyncRepeatingTask(PlayerAI.getInstance(), this::tick, 0, 1); Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::tick, 0, 1);
} }
private void tick() { private void tick() {
quarterTick = (byte) ((quarterTick + 1) % 5);
manager.fetch().forEach(this::tickBot); manager.fetch().forEach(this::tickBot);
} }
@@ -32,11 +32,30 @@ public class BotAgent {
if (player == null) return; if (player == null) return;
Location target = player.getLocation(); Location target = player.getLocation();
if (manager.fetch().size() > 1) {
target.add(bot.getOffset());
}
// Make the XZ offsets stored in the bot object (so they don't form a straight line),
// and make it so when mining and stuff, the offset is not taken into account
if (bot.tickDelay(3)) attack(bot, player, loc);
move(bot, player, loc, target);
}
private void attack(Bot bot, Player player, Location loc) {
if (!PlayerUtils.isVulnerableGamemode(player.getGameMode())
|| player.getNoDamageTicks() >= 5
|| loc.distance(player.getLocation()) >= 4) return;
bot.attack(player);
}
private void move(Bot bot, Player player, Location loc, Location target) {
Vector vel = target.toVector().subtract(loc.toVector()).normalize(); Vector vel = target.toVector().subtract(loc.toVector()).normalize();
if (quarterTick == 0) { if (bot.tickDelay(5)) bot.faceLocation(player.getLocation());
bot.faceLocation(target);
}
try { try {
vel.checkFinite(); vel.checkFinite();
@@ -46,12 +65,9 @@ public class BotAgent {
} }
if (vel.length() > 1) vel.normalize(); if (vel.length() > 1) vel.normalize();
vel.multiply(0.3); vel.multiply(0.4).setY(0.4);
vel.setY(0.5);
if (bot.predictGround()) { bot.jump(vel);
bot.jump(vel);
}
} }
private Player nearestPlayer(Location loc) { private Player nearestPlayer(Location loc) {

View File

@@ -27,4 +27,13 @@ public class MathUtils {
return out; return out;
} }
public static Vector circleOffset(double r) {
double rad = 2 * Math.random() * Math.PI;
double x = r * Math.random() * Math.cos(rad);
double z = r * Math.random() * Math.sin(rad);
return new Vector(x, 0, z);
}
} }

View File

@@ -0,0 +1,10 @@
package net.nuggetmc.ai.utils;
import org.bukkit.GameMode;
public class PlayerUtils {
public static boolean isVulnerableGamemode(GameMode mode) {
return mode == GameMode.SURVIVAL || mode == GameMode.ADVENTURE;
}
}