From 70b39259fa16d232a60fc68e53957fe9457ce278 Mon Sep 17 00:00:00 2001 From: batchprogrammer314 Date: Tue, 29 Jun 2021 16:29:30 -0500 Subject: [PATCH] Fixed some following bugs. --- src/main/java/net/nuggetmc/ai/bot/Bot.java | 53 +++++++++++++++---- .../net/nuggetmc/ai/bot/agent/BotAgent.java | 40 +++++++++----- .../java/net/nuggetmc/ai/utils/MathUtils.java | 9 ++++ .../net/nuggetmc/ai/utils/PlayerUtils.java | 10 ++++ 4 files changed, 90 insertions(+), 22 deletions(-) create mode 100644 src/main/java/net/nuggetmc/ai/utils/PlayerUtils.java diff --git a/src/main/java/net/nuggetmc/ai/bot/Bot.java b/src/main/java/net/nuggetmc/ai/bot/Bot.java index 5cdc288..270ae4f 100644 --- a/src/main/java/net/nuggetmc/ai/bot/Bot.java +++ b/src/main/java/net/nuggetmc/ai/bot/Bot.java @@ -23,6 +23,7 @@ public class Bot extends EntityPlayer { public Vector velocity; + private byte aliveTicks; private byte kbTicks; private byte jumpTicks; private byte groundTicks; @@ -32,11 +33,13 @@ public class Bot extends EntityPlayer { private final double frictionMin = 0.01; private final double kbUp = 0.3; + private Vector offset; + public Bot(MinecraftServer minecraftServer, WorldServer worldServer, GameProfile profile, PlayerInteractManager manager) { super(minecraftServer, worldServer, profile, manager); - velocity = new Vector(0, 0, 0); - kbTicks = 0; + this.velocity = new Vector(0, 0, 0); + this.offset = MathUtils.circleOffset(3); } 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() { return velocity.clone(); } @@ -115,15 +126,21 @@ public class Bot extends EntityPlayer { this.velocity.add(vector); } + public boolean tickDelay(int i) { + return aliveTicks % i == 0; + } + @Override public void tick() { super.tick(); + aliveTicks++; + if (noDamageTicks > 0) --noDamageTicks; if (kbTicks > 0) --kbTicks; if (jumpTicks > 0) --jumpTicks; - if (predictGround()) { + if (isOnGround()) { groundTicks++; } else { groundTicks = 0; @@ -152,7 +169,7 @@ public class Bot extends EntityPlayer { double y; - if (groundTicks > 0) { + if (isOnGround()) { velocity.setY(0); addFriction(); 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(); if (vy > 0) { @@ -183,18 +214,20 @@ public class Bot extends EntityPlayer { AxisAlignedBB box = getBoundingBox(); double[] xVals = new double[] { - box.minX + bbOffset, - box.maxX - bbOffset + box.minX, + box.maxX }; double[] zVals = new double[] { - box.minZ + bbOffset, - box.maxZ - bbOffset + box.minZ, + box.maxZ }; for (double x : xVals) { 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; + } } } diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java b/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java index 6972266..b910363 100644 --- a/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java +++ b/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java @@ -3,6 +3,7 @@ package net.nuggetmc.ai.bot.agent; import net.nuggetmc.ai.PlayerAI; import net.nuggetmc.ai.bot.Bot; import net.nuggetmc.ai.bot.BotManager; +import net.nuggetmc.ai.utils.PlayerUtils; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -10,18 +11,17 @@ import org.bukkit.util.Vector; public class BotAgent { + private PlayerAI plugin; private BotManager manager; - private byte quarterTick = 0; - public BotAgent(BotManager manager) { + this.plugin = PlayerAI.getInstance(); this.manager = manager; - Bukkit.getScheduler().scheduleSyncRepeatingTask(PlayerAI.getInstance(), this::tick, 0, 1); + Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::tick, 0, 1); } private void tick() { - quarterTick = (byte) ((quarterTick + 1) % 5); manager.fetch().forEach(this::tickBot); } @@ -32,11 +32,30 @@ public class BotAgent { if (player == null) return; 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(); - if (quarterTick == 0) { - bot.faceLocation(target); - } + if (bot.tickDelay(5)) bot.faceLocation(player.getLocation()); try { vel.checkFinite(); @@ -46,12 +65,9 @@ public class BotAgent { } if (vel.length() > 1) vel.normalize(); - vel.multiply(0.3); - vel.setY(0.5); + vel.multiply(0.4).setY(0.4); - if (bot.predictGround()) { - bot.jump(vel); - } + bot.jump(vel); } private Player nearestPlayer(Location loc) { diff --git a/src/main/java/net/nuggetmc/ai/utils/MathUtils.java b/src/main/java/net/nuggetmc/ai/utils/MathUtils.java index 01ab353..c710d3d 100644 --- a/src/main/java/net/nuggetmc/ai/utils/MathUtils.java +++ b/src/main/java/net/nuggetmc/ai/utils/MathUtils.java @@ -27,4 +27,13 @@ public class MathUtils { 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); + } } diff --git a/src/main/java/net/nuggetmc/ai/utils/PlayerUtils.java b/src/main/java/net/nuggetmc/ai/utils/PlayerUtils.java new file mode 100644 index 0000000..5919015 --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/utils/PlayerUtils.java @@ -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; + } +}