/bot count and region fixes

-Fix bugs with region setting
-/bot count: Count bots by name
-/bot info gets closest bot
This commit is contained in:
ThisTestUser
2022-10-03 16:50:18 -04:00
parent 42c8c448c7
commit 08dd248fba
6 changed files with 38 additions and 13 deletions

View File

@@ -16,7 +16,7 @@ public interface BotManager {
void add(Terminator bot); void add(Terminator bot);
Terminator getFirst(String name); Terminator getFirst(String name, Location target);
List<String> fetchNames(); List<String> fetchNames();

View File

@@ -24,6 +24,7 @@ public enum EnumTargetGoal {
this.put("nearestbot", NEAREST_BOT); this.put("nearestbot", NEAREST_BOT);
this.put("nearestbotdiffer", NEAREST_BOT_DIFFER); this.put("nearestbotdiffer", NEAREST_BOT_DIFFER);
this.put("nearestbotdifferalpha", NEAREST_BOT_DIFFER_ALPHA); this.put("nearestbotdifferalpha", NEAREST_BOT_DIFFER_ALPHA);
this.put("player", PLAYER);
} }
}; };

View File

@@ -1496,24 +1496,19 @@ public class LegacyAgent extends Agent {
return false; return false;
double regionDistResult = result == null ? 0 : getWeightedRegionDist(result.getLocation()); double regionDistResult = result == null ? 0 : getWeightedRegionDist(result.getLocation());
return loc.getWorld() == entity.getWorld() && !entity.isDead() return loc.getWorld() == entity.getWorld() && !entity.isDead()
&& (result == null || (loc.distance(entity.getLocation()) + regionDistEntity) < (loc.distance(result.getLocation())) + regionDistResult); && (result == null || (loc.distanceSquared(entity.getLocation()) + regionDistEntity) < (loc.distanceSquared(result.getLocation())) + regionDistResult);
} }
private double getWeightedRegionDist(Location loc) { private double getWeightedRegionDist(Location loc) {
if (region == null) if (region == null)
return 0; return 0;
double diffX = Math.min(0, Math.abs(region.getCenterX() - loc.getX()) - region.getWidthX() * 0.5); double diffX = Math.max(0, Math.abs(region.getCenterX() - loc.getX()) - region.getWidthX() * 0.5);
double diffY = Math.max(0, Math.abs(region.getCenterY() - loc.getY()) - region.getHeight() * 0.5); double diffY = Math.max(0, Math.abs(region.getCenterY() - loc.getY()) - region.getHeight() * 0.5);
double diffZ = Math.max(0, Math.abs(region.getCenterZ() - loc.getZ()) - region.getWidthZ() * 0.5); double diffZ = Math.max(0, Math.abs(region.getCenterZ() - loc.getZ()) - region.getWidthZ() * 0.5);
if (regionWeightX == 0 && regionWeightY == 0 && regionWeightZ == 0) { if (regionWeightX == 0 && regionWeightY == 0 && regionWeightZ == 0)
if (diffX > 0 || diffY > 0 || diffZ > 0) if (diffX > 0 || diffY > 0 || diffZ > 0)
return Double.MAX_VALUE; return Double.MAX_VALUE;
} else { return diffX * diffX * regionWeightX + diffY * diffY * regionWeightY + diffZ * diffZ * regionWeightZ;
diffX *= regionWeightX;
diffY *= regionWeightY;
diffZ *= regionWeightZ;
}
return diffX * diffX + diffY * diffY + diffZ * diffZ;
} }
@Override @Override

View File

@@ -56,7 +56,17 @@ public class BotManagerImpl implements BotManager, Listener {
} }
@Override @Override
public Terminator getFirst(String name) { public Terminator getFirst(String name, Location target) {
if (target != null) {
Terminator closest = null;
for (Terminator bot : bots) {
if (name.equals(bot.getBotName()) && (closest == null
|| target.distanceSquared(bot.getLocation()) < target.distanceSquared(closest.getLocation()))) {
closest = bot;
}
}
return closest;
}
for (Terminator bot : bots) { for (Terminator bot : bots) {
if (name.equals(bot.getBotName())) { if (name.equals(bot.getBotName())) {
return bot; return bot;

View File

@@ -121,7 +121,7 @@ public class AICommand extends CommandInstance implements AIManager {
scheduler.runTaskAsynchronously(plugin, () -> { scheduler.runTaskAsynchronously(plugin, () -> {
try { try {
Terminator bot = manager.getFirst(name); Terminator bot = manager.getFirst(name, (sender instanceof Player pl) ? pl.getLocation() : null);
if (bot == null) { if (bot == null) {
sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!"); sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!");

View File

@@ -27,6 +27,8 @@ import org.bukkit.util.Vector;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.*; import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
public class BotCommand extends CommandInstance { public class BotCommand extends CommandInstance {
@@ -190,7 +192,7 @@ public class BotCommand extends CommandInstance {
scheduler.runTaskAsynchronously(plugin, () -> { scheduler.runTaskAsynchronously(plugin, () -> {
try { try {
Terminator bot = manager.getFirst(name); Terminator bot = manager.getFirst(name, (sender instanceof Player pl) ? pl.getLocation() : null);
if (bot == null) { if (bot == null) {
sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!"); sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!");
@@ -232,6 +234,23 @@ public class BotCommand extends CommandInstance {
return args.length == 2 ? manager.fetchNames() : null; return args.length == 2 ? manager.fetchNames() : null;
} }
@Command(
name = "count",
desc = "Counts the amount of bots on screen by name."
)
public void count(CommandSender sender) {
List<String> names = manager.fetchNames();
Map<String, Integer> freqMap = names.stream().collect(Collectors.toMap(s -> s, s -> 1, Integer::sum));
List<Entry<String, Integer>> entries = freqMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(Collectors.toList());
sender.sendMessage(ChatUtils.LINE);
entries.forEach(en -> sender.sendMessage(ChatColor.GREEN + en.getKey()
+ ChatColor.RESET + " - " + ChatColor.BLUE + en.getValue().toString() + ChatColor.RESET));
sender.sendMessage("Total bots: " + ChatColor.BLUE + freqMap.values().stream().reduce(0, Integer::sum) + ChatColor.RESET);
sender.sendMessage(ChatUtils.LINE);
}
@Command( @Command(
name = "reset", name = "reset",
desc = "Remove all loaded bots." desc = "Remove all loaded bots."