This commit is contained in:
batchprogrammer314
2021-08-21 13:36:10 -05:00
parent d86a42eab6
commit 84f3918c25
20 changed files with 317 additions and 186 deletions

View File

@@ -2,6 +2,8 @@ package net.nuggetmc.ai.bot;
import com.mojang.authlib.GameProfile;
import com.mojang.datafixers.util.Pair;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import net.minecraft.server.v1_16_R3.Chunk;
import net.minecraft.server.v1_16_R3.*;
import net.nuggetmc.ai.TerminatorPlus;
@@ -27,6 +29,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
@@ -105,7 +108,13 @@ public class Bot extends EntityPlayer {
Bot bot = new Bot(nmsServer, nmsWorld, profile, interactManager);
bot.playerConnection = new PlayerConnection(nmsServer, new NetworkManager(EnumProtocolDirection.CLIENTBOUND), bot);
bot.playerConnection = new PlayerConnection(nmsServer, new NetworkManager(EnumProtocolDirection.CLIENTBOUND) {
@Override
public void sendPacket(Packet<?> packet, @Nullable GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) { }
}, bot);
bot.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
bot.getBukkitEntity().setNoDamageTicks(0);
nmsWorld.addEntity(bot);
@@ -234,7 +243,7 @@ public class Bot extends EntityPlayer {
Chunk chunk = world.getChunkAt(i, j);
if (!chunk.loaded) {
chunk.setLoaded(true);
chunk.loaded = true;
}
}
}
@@ -400,8 +409,10 @@ public class Bot extends EntityPlayer {
faceLocation(entity.getLocation());
punch();
double damage = ItemUtils.getLegacyAttackDamage(defaultItem);
if (entity instanceof Damageable) {
((Damageable) entity).damage(ItemUtils.getLegacyAttackDamage(defaultItem), getBukkitEntity());
((Damageable) entity).damage(damage, getBukkitEntity());
}
}
@@ -472,6 +483,8 @@ public class Bot extends EntityPlayer {
}
private void setDead() {
sendPacket(new PacketPlayOutEntityDestroy(getId()));
this.dead = true;
this.defaultContainer.b(this);
if (this.activeContainer != null) {
@@ -481,7 +494,7 @@ public class Bot extends EntityPlayer {
private void dieCheck() {
if (removeOnDeath) {
scheduler.runTask(plugin, () -> plugin.getManager().remove(this));
scheduler.runTask(plugin, () -> plugin.getManager().remove(this)); // maybe making this later will fix the concurrentmodificationexception?
scheduler.runTaskLater(plugin, this::setDead, 30);
this.removeTab();

View File

@@ -111,7 +111,7 @@ public class BotManager implements Listener {
bot.setNeuralNetwork(network == NeuralNetwork.RANDOM ? NeuralNetwork.generateRandomNetwork() : network);
bot.setShield(true);
bot.setDefaultItem(new ItemStack(Material.WOODEN_AXE));
bot.setRemoveOnDeath(false);
//bot.setRemoveOnDeath(false);
}
if (network != null) {
@@ -140,11 +140,12 @@ public class BotManager implements Listener {
}
public void reset() {
bots.forEach(Bot::removeVisually);
bots.clear(); // Not always necessary, but a good security measure
agent.stopAllTasks();
if (!bots.isEmpty()) {
bots.forEach(Bot::removeVisually);
bots.clear(); // Not always necessary, but a good security measure
}
System.gc();
agent.stopAllTasks();
}
public Bot getBot(Player player) { // potentially memory intensive

View File

@@ -55,8 +55,10 @@ public abstract class Agent {
}
public void stopAllTasks() {
taskList.stream().filter(t -> !t.isCancelled()).forEach(BukkitRunnable::cancel);
taskList.clear();
if (!taskList.isEmpty()) {
taskList.stream().filter(t -> !t.isCancelled()).forEach(BukkitRunnable::cancel);
taskList.clear();
}
}
public void setDrops(boolean enabled) {

View File

@@ -1,32 +1,38 @@
package net.nuggetmc.ai.bot.agent.legacyagent;
public enum EnumTargetGoal { // TODO USE ORDINAL!!!!!
NEAREST_REAL_VULNERABLE_PLAYER,
NEAREST_REAL_PLAYER,
NEAREST_BOT_DIFFER,
NEAREST_BOT,
NEAREST_BOT_DIFFER_ALPHA,
NONE;
import java.util.HashMap;
import java.util.Map;
public static EnumTargetGoal of(int n) {
switch (n) {
default:
return NONE;
public enum EnumTargetGoal {
NEAREST_VULNERABLE_PLAYER("Locate the nearest real player that is in either Survival or Adventure mode."),
NEAREST_PLAYER("Locate the nearest real online player, despite the gamemode."),
NEAREST_BOT("Locate the nearest bot."),
NEAREST_BOT_DIFFER("Locate the nearest bot with a different username."),
NEAREST_BOT_DIFFER_ALPHA("Locate the nearest bot with a different username after filtering out non-alpha characters."),
NONE("No target goal.");
case 1:
return NEAREST_REAL_VULNERABLE_PLAYER;
case 2:
return NEAREST_REAL_PLAYER;
case 3:
return NEAREST_BOT_DIFFER;
case 4:
return NEAREST_BOT;
case 5:
return NEAREST_BOT_DIFFER_ALPHA;
private static final Map<String, EnumTargetGoal> VALUES = new HashMap<String, EnumTargetGoal>() {
{
this.put("none", NONE);
this.put("nearestvulnerableplayer", NEAREST_VULNERABLE_PLAYER);
this.put("nearestplayer", NEAREST_PLAYER);
this.put("nearestbot", NEAREST_BOT);
this.put("nearestbotdiffer", NEAREST_BOT_DIFFER);
this.put("nearestbotdifferalpha", NEAREST_BOT_DIFFER_ALPHA);
}
};
private final String description;
EnumTargetGoal(String description) {
this.description = description;
}
public static EnumTargetGoal from(String name) {
return VALUES.get(name);
}
public String description() {
return description;
}
}

View File

@@ -11,7 +11,6 @@ import net.nuggetmc.ai.bot.agent.legacyagent.ai.NeuralNetwork;
import net.nuggetmc.ai.bot.event.BotDamageByPlayerEvent;
import net.nuggetmc.ai.bot.event.BotDeathEvent;
import net.nuggetmc.ai.bot.event.BotFallDamageEvent;
import net.nuggetmc.ai.bot.event.BotKilledByPlayerEvent;
import net.nuggetmc.ai.utils.MathUtils;
import net.nuggetmc.ai.utils.PlayerUtils;
import org.bukkit.*;
@@ -35,12 +34,14 @@ public class LegacyAgent extends Agent {
private final LegacyBlockCheck blockCheck;
private EnumTargetGoal goal;
public boolean offsets = true;
public LegacyAgent(BotManager manager) {
super(manager);
this.goal = EnumTargetGoal.NEAREST_REAL_VULNERABLE_PLAYER;
this.goal = EnumTargetGoal.NEAREST_VULNERABLE_PLAYER;
this.blockCheck = new LegacyBlockCheck(this);
}
@@ -93,17 +94,18 @@ public class LegacyAgent extends Agent {
}
private void tickBot(Bot bot) {
if (!bot.isAlive()) return;
if (!bot.isAlive()) {
return;
}
if (bot.tickDelay(20)) {
center(bot);
}
Location loc = bot.getLocation();
Player player = locateTarget(bot, loc);
Player player = nearestPlayer(bot, loc);
if (player == null) {
// LESSLAG if (bot.tickDelay(20))
stopMining(bot);
return;
}
@@ -114,33 +116,28 @@ public class LegacyAgent extends Agent {
miscellaneousChecks(bot, player);
Player botPlayer = bot.getBukkitEntity();
Location target = offsets ? player.getLocation().add(bot.getOffset()) : player.getLocation();
NeuralNetwork network;
boolean ai = bot.hasNeuralNetwork();
if (ai) {
BotData data = BotData.generate(bot, player);
NeuralNetwork network = ai ? bot.getNeuralNetwork() : null;
network = bot.getNeuralNetwork();
network.feed(data);
} else {
network = null;
if (ai) {
network.feed(BotData.generate(bot, player));
}
if (bot.tickDelay(3) && !miningAnim.containsKey(botPlayer)) {
Location a = botPlayer.getEyeLocation();
Location b = player.getEyeLocation();
Location c1 = player.getLocation();
Location botEyeLoc = botPlayer.getEyeLocation();
Location playerEyeLoc = player.getEyeLocation();
Location playerLoc = player.getLocation();
if (ai) { // force unable to block if they are more than 6/7 blocks away
if (ai) {
if (network.check(BotNode.BLOCK) && loc.distance(player.getLocation()) < 6) {
bot.block(10, 10);
}
}
if (LegacyUtils.checkFreeSpace(a, b) || LegacyUtils.checkFreeSpace(a, c1)) {
if (LegacyUtils.checkFreeSpace(botEyeLoc, playerEyeLoc) || LegacyUtils.checkFreeSpace(botEyeLoc, playerLoc)) {
attack(bot, player, loc);
}
}
@@ -1124,93 +1121,78 @@ public class LegacyAgent extends Agent {
bot.attack(player);
}
private EnumTargetGoal goal;
public void setTargetType(EnumTargetGoal goal) {
this.goal = goal;
}
private Player nearestPlayer(Bot bot, Location loc) {
public Player locateTarget(Bot bot, Location loc) {
Player result = null;
switch (goal) {
case NEAREST_REAL_VULNERABLE_PLAYER:
return nearestRealVulnerablePlayer(loc);
case NEAREST_REAL_PLAYER:
return nearestRealPlayer(loc);
case NEAREST_BOT_DIFFER:
return nearestBotDiffer(bot, loc);
case NEAREST_BOT_DIFFER_ALPHA:
return nearestBotDifferAlpha(bot, loc);
case NEAREST_BOT:
return nearestBot(bot, loc);
default:
return null;
}
}
private Player nearestRealPlayer(Location loc) {
Player result = null;
case NEAREST_PLAYER: {
for (Player player : Bukkit.getOnlinePlayers()) {
if (validateCloserPlayer(player, loc, result)) {
result = player;
}
}
for (Player player : Bukkit.getOnlinePlayers()) {
if (loc.getWorld() != player.getWorld() || player.isDead()) continue;
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player;
break;
}
}
return result;
}
case NEAREST_VULNERABLE_PLAYER: {
for (Player player : Bukkit.getOnlinePlayers()) {
if (!PlayerUtils.isInvincible(player.getGameMode()) && validateCloserPlayer(player, loc, result)) {
result = player;
}
}
private Player nearestRealVulnerablePlayer(Location loc) {
Player result = null;
for (Player player : Bukkit.getOnlinePlayers()) {
if (PlayerUtils.isInvincible(player.getGameMode()) || loc.getWorld() != player.getWorld() || player.isDead()) continue;
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player;
break;
}
}
return result;
}
case NEAREST_BOT: {
for (Bot otherBot : manager.fetch()) {
if (bot != otherBot) {
Player player = otherBot.getBukkitEntity();
private Player nearestBot(Bot bot, Location loc) {
Player result = null;
if (validateCloserPlayer(player, loc, result)) {
result = player;
}
}
}
for (Bot otherBot : manager.fetch()) {
if (bot == otherBot) continue;
Player player = otherBot.getBukkitEntity();
if (loc.getWorld() != player.getWorld() || player.isDead()) continue;
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player;
break;
}
}
return result;
}
case NEAREST_BOT_DIFFER: {
String name = bot.getName();
private Player nearestBotDiffer(Bot bot, Location loc) {
Player result = null;
for (Bot otherBot : manager.fetch()) {
if (bot != otherBot) {
Player player = otherBot.getBukkitEntity();
for (Bot otherBot : manager.fetch()) {
if (bot == otherBot) continue;
if (!name.equals(otherBot.getName()) && validateCloserPlayer(player, loc, result)) {
result = player;
}
}
}
Player player = otherBot.getBukkitEntity();
break;
}
if (!bot.getName().equals(otherBot.getName())) {
if (loc.getWorld() != player.getWorld() || player.isDead()) continue;
case NEAREST_BOT_DIFFER_ALPHA: {
String name = bot.getName().replaceAll("[^A-Za-z]+", "");
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player;
for (Bot otherBot : manager.fetch()) {
if (bot != otherBot) {
Player player = otherBot.getBukkitEntity();
if (!name.equals(otherBot.getName().replaceAll("[^A-Za-z]+", "")) && validateCloserPlayer(player, loc, result)) {
result = player;
}
}
}
}
}
@@ -1218,23 +1200,7 @@ public class LegacyAgent extends Agent {
return result;
}
private Player nearestBotDifferAlpha(Bot bot, Location loc) {
Player result = null;
for (Bot otherBot : manager.fetch()) {
if (bot == otherBot) continue;
Player player = otherBot.getBukkitEntity();
if (!bot.getName().replaceAll("[^A-Za-z]+", "").equals(otherBot.getName().replaceAll("[^A-Za-z]+", ""))) {
if (loc.getWorld() != player.getWorld() || player.isDead()) continue;
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player;
}
}
}
return result;
private boolean validateCloserPlayer(Player player, Location loc, Player result) {
return loc.getWorld() == player.getWorld() && !player.isDead() && (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation()));
}
}

View File

@@ -5,6 +5,10 @@ import org.bukkit.Material;
public class LegacyWorldManager {
/*
* This is where the respawning queue will be managed
*/
public static boolean aboveGround(Location loc) {
int y = 1;

View File

@@ -29,6 +29,7 @@ public class IntelligenceAgent {
* commands /ai stop and /ai pause
* if a session with name already exists keep adding underscores
* /ai conclude or /ai finish
* default anchor location, /ai relocateanchor
*/
private final TerminatorPlus plugin;
@@ -54,7 +55,6 @@ public class IntelligenceAgent {
private Player primary;
private final Set<CommandSender> users;
private final Map<Integer, Set<Map<BotNode, Map<BotDataType, Double>>>> genProfiles;
public IntelligenceAgent(AICommand aiManager, int populationSize, String name, String skin) {
@@ -215,7 +215,7 @@ public class IntelligenceAgent {
Set<Map<BotNode, Map<BotDataType, Double>>> profiles = new HashSet<>();
double mutationSize = MathUtils.getMutationSize(generation);
double mutationSize = Math.pow(Math.E, 2); //MathUtils.getMutationSize(generation);
for (int j = 0; j < populationSize; j++) {
Map<BotNode, Map<BotDataType, Double>> profile = new HashMap<>();
@@ -272,6 +272,8 @@ public class IntelligenceAgent {
}
public void addUser(CommandSender sender) {
if (users.contains(sender)) return;
users.add(sender);
print(sender.getName() + " has been added to the userlist.");
@@ -322,7 +324,14 @@ public class IntelligenceAgent {
}
private void clearBots() {
print("Removing all current bots...");
if (!bots.isEmpty()) {
print("Removing all cached bots...");
bots.values().forEach(Bot::removeVisually);
bots.clear();
}
/*print("Removing all current bots...");
int size = manager.fetch().size();
manager.reset();
@@ -330,6 +339,6 @@ public class IntelligenceAgent {
String formatted = NumberFormat.getNumberInstance(Locale.US).format(size);
print("Removed " + ChatColor.RED + formatted + ChatColor.RESET + " entit" + (size == 1 ? "y" : "ies") + ".");
bots.clear();
bots.clear();*/
}
}

View File

@@ -3,6 +3,7 @@ package net.nuggetmc.ai.bot.agent.legacyagent.ai;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
public class NodeConnections {
@@ -30,7 +31,7 @@ public class NodeConnections {
}
private double generateValue() {
return Math.random() * 20 - 10;
return ThreadLocalRandom.current().nextDouble(-10, 10);
}
public boolean check() {