use of net.countercraft.movecraft.util.hitboxes.HitBox in project Movecraft by APDevTeam.
the class ContactsCommand method onCommand.
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
if (!command.getName().equalsIgnoreCase("contacts")) {
return false;
}
if (!(commandSender instanceof Player)) {
commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Contacts - Must Be Player"));
return true;
}
Player player = (Player) commandSender;
if (CraftManager.getInstance().getCraftByPlayer(player) == null) {
player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("You must be piloting a craft"));
return true;
}
int page;
try {
if (args.length == 0)
page = 1;
else
page = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Paginator - Invalid Page") + " \"" + args[0] + "\"");
return true;
}
TopicPaginator pageinator = new TopicPaginator(I18nSupport.getInternationalisedString("Contacts"));
Craft ccraft = CraftManager.getInstance().getCraftByPlayer(player);
HitBox hitBox = ccraft.getHitBox();
MovecraftLocation center = hitBox.getMidPoint();
for (Craft tcraft : ccraft.getContacts()) {
HitBox tHitBox = tcraft.getHitBox();
if (tHitBox.isEmpty())
continue;
MovecraftLocation tCenter = tHitBox.getMidPoint();
int distsquared = center.distanceSquared(tCenter);
String notification = I18nSupport.getInternationalisedString("Contact");
notification += ": ";
notification += tcraft instanceof SinkingCraft ? ChatColor.RED : tcraft.getDisabled() ? ChatColor.BLUE : "";
notification += tcraft.getName().length() >= 1 ? tcraft.getName() + " (" : "";
notification += tcraft.getType().getStringProperty(CraftType.NAME);
notification += tcraft.getName().length() >= 1 ? ") " : " ";
notification += ChatColor.RESET;
notification += I18nSupport.getInternationalisedString("Contact - Commanded By") + ", ";
notification += tcraft instanceof PilotedCraft ? ((PilotedCraft) tcraft).getPilot().getDisplayName() : "null";
notification += " ";
notification += I18nSupport.getInternationalisedString("Contact - Size") + " ";
notification += tcraft.getOrigBlockCount();
notification += ", " + I18nSupport.getInternationalisedString("Contact - Range") + " ";
notification += (int) Math.sqrt(distsquared);
notification += " " + I18nSupport.getInternationalisedString("Contact - To The");
int diffx = center.getX() - tCenter.getX();
int diffz = center.getZ() - tCenter.getZ();
if (Math.abs(diffx) > Math.abs(diffz))
if (diffx < 0)
notification += " " + I18nSupport.getInternationalisedString("Contact/Subcraft Rotate - East") + ".";
else
notification += " " + I18nSupport.getInternationalisedString("Contact/Subcraft Rotate - West") + ".";
else if (diffz < 0)
notification += " " + I18nSupport.getInternationalisedString("Contact/Subcraft Rotate - South") + ".";
else
notification += " " + I18nSupport.getInternationalisedString("Contact/Subcraft Rotate - North") + ".";
pageinator.addLine(notification);
}
if (pageinator.isEmpty()) {
player.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Contacts - None Found"));
return true;
}
if (!pageinator.isInBounds(page)) {
commandSender.sendMessage(MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Paginator - Invalid page") + "\"" + page + "\"");
return true;
}
for (String line : pageinator.getPage(page)) commandSender.sendMessage(line);
return true;
}
use of net.countercraft.movecraft.util.hitboxes.HitBox in project Movecraft by APDevTeam.
the class DetectionTask method water.
@Deprecated
@NotNull
private Effect water(@NotNull Craft craft) {
final int waterLine = WorldManager.INSTANCE.executeMain(craft::getWaterLine);
if (craft.getType().getBoolProperty(CraftType.BLOCKED_BY_WATER) || craft.getHitBox().getMinY() > waterLine)
return () -> {
};
var badWorld = WorldManager.INSTANCE.executeMain(craft::getWorld);
// The subtraction of the set of coordinates in the HitBox cube and the HitBox itself
final HitBox invertedHitBox = new BitmapHitBox(craft.getHitBox().boundingHitBox()).difference(craft.getHitBox());
// A set of locations that are confirmed to be "exterior" locations
final SetHitBox confirmed = new SetHitBox();
final SetHitBox entireHitbox = new SetHitBox(craft.getHitBox());
// place phased blocks
final Set<Location> overlap = new HashSet<>(craft.getPhaseBlocks().keySet());
overlap.retainAll(craft.getHitBox().asSet().stream().map(l -> l.toBukkit(badWorld)).collect(Collectors.toSet()));
final int minX = craft.getHitBox().getMinX();
final int maxX = craft.getHitBox().getMaxX();
final int minY = craft.getHitBox().getMinY();
final int maxY = overlap.isEmpty() ? craft.getHitBox().getMaxY() : Collections.max(overlap, Comparator.comparingInt(Location::getBlockY)).getBlockY();
final int minZ = craft.getHitBox().getMinZ();
final int maxZ = craft.getHitBox().getMaxZ();
final HitBox[] surfaces = { new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(minX, maxY, maxZ)), new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(maxX, maxY, minZ)), new SolidHitBox(new MovecraftLocation(maxX, minY, maxZ), new MovecraftLocation(minX, maxY, maxZ)), new SolidHitBox(new MovecraftLocation(maxX, minY, maxZ), new MovecraftLocation(maxX, maxY, minZ)), new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(maxX, minY, maxZ)) };
final SetHitBox validExterior = new SetHitBox();
for (HitBox hitBox : surfaces) {
validExterior.addAll(new BitmapHitBox(hitBox).difference(craft.getHitBox()));
}
// Check to see which locations in the from set are actually outside of the craft
// use a modified BFS for multiple origin elements
SetHitBox visited = new SetHitBox();
Queue<MovecraftLocation> queue = Lists.newLinkedList(validExterior);
while (!queue.isEmpty()) {
MovecraftLocation node = queue.poll();
if (visited.contains(node))
continue;
visited.add(node);
// If the node is already a valid member of the exterior of the HitBox, continued search is unitary.
for (MovecraftLocation neighbor : CollectionUtils.neighbors(invertedHitBox, node)) {
queue.add(neighbor);
}
}
confirmed.addAll(visited);
entireHitbox.addAll(invertedHitBox.difference(confirmed));
var waterData = Bukkit.createBlockData(Material.WATER);
return () -> {
for (MovecraftLocation location : entireHitbox) {
if (location.getY() <= waterLine) {
craft.getPhaseBlocks().put(location.toBukkit(badWorld), waterData);
}
}
};
}
use of net.countercraft.movecraft.util.hitboxes.HitBox in project Movecraft by APDevTeam.
the class DetectionTask method get.
@Override
public Effect get() {
frontier();
if (!illegal.isEmpty())
return null;
var result = COMPLETION_VALIDATORS.stream().reduce(DetectionPredicate::and).orElse((a, b, c, d) -> Result.fail()).validate(materials, type, movecraftWorld, player);
result = result.isSucess() ? VISITED_VALIDATORS.stream().reduce(DetectionPredicate::and).orElse((a, b, c, d) -> Result.fail()).validate(visitedMaterials, type, movecraftWorld, player) : result;
if (!result.isSucess()) {
String message = result.getMessage();
return () -> audience.sendMessage(Component.text(message));
}
var hitbox = new BitmapHitBox(legal);
var parents = findParents(hitbox);
var supplied = supplier.apply(type, world, player, parents);
result = supplied.getLeft();
Craft craft = supplied.getRight();
if (type.getBoolProperty(CraftType.MUST_BE_SUBCRAFT) && !(craft instanceof SubCraft)) {
result = Result.failWithMessage(I18nSupport.getInternationalisedString("Detection - Must Be Subcraft"));
}
if (!result.isSucess()) {
String message = result.getMessage();
return () -> audience.sendMessage(Component.text(message));
}
craft.setAudience(audience);
craft.setHitBox(hitbox);
craft.setFluidLocations(new BitmapHitBox(fluid));
craft.setOrigBlockCount(craft.getHitBox().size());
final CraftDetectEvent event = new CraftDetectEvent(craft, startLocation);
WorldManager.INSTANCE.executeMain(() -> Bukkit.getPluginManager().callEvent(event));
if (event.isCancelled())
return () -> craft.getAudience().sendMessage(Component.text(event.getFailMessage()));
return ((Effect) () -> {
// Notify player and console
craft.getAudience().sendMessage(Component.text(String.format("%s Size: %s", I18nSupport.getInternationalisedString("Detection - Successfully piloted craft"), craft.getHitBox().size())));
Movecraft.getInstance().getLogger().info(String.format(I18nSupport.getInternationalisedString("Detection - Success - Log Output"), player == null ? "null" : player.getName(), craft.getType().getStringProperty(CraftType.NAME), craft.getHitBox().size(), craft.getHitBox().getMinX(), craft.getHitBox().getMinZ()));
}).andThen(// TODO: Remove
water(craft)).andThen(// Fire off pilot event
() -> Bukkit.getServer().getPluginManager().callEvent(new CraftPilotEvent(craft, CraftPilotEvent.Reason.PLAYER))).andThen(// Apply post detection effect
postDetection.apply(craft)).andThen(// Add craft to CraftManager
() -> CraftManager.getInstance().add(craft));
}
use of net.countercraft.movecraft.util.hitboxes.HitBox in project Movecraft by APDevTeam.
the class CraftRotateCommand method doUpdate.
@Override
public void doUpdate() {
final Logger logger = Movecraft.getInstance().getLogger();
if (craft.getHitBox().isEmpty()) {
logger.warning("Attempted to move craft with empty HashHitBox!");
CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.EMPTY, false);
return;
}
long time = System.nanoTime();
final Set<Material> passthroughBlocks = new HashSet<>(craft.getType().getMaterialSetProperty(CraftType.PASSTHROUGH_BLOCKS));
if (craft instanceof SinkingCraft) {
passthroughBlocks.addAll(Tags.FLUID);
passthroughBlocks.addAll(Tag.LEAVES.getValues());
passthroughBlocks.addAll(Tags.SINKING_PASSTHROUGH);
}
if (!passthroughBlocks.isEmpty()) {
SetHitBox originalLocations = new SetHitBox();
final MovecraftRotation counterRotation = rotation == MovecraftRotation.CLOCKWISE ? MovecraftRotation.ANTICLOCKWISE : MovecraftRotation.CLOCKWISE;
for (MovecraftLocation movecraftLocation : craft.getHitBox()) {
originalLocations.add(MathUtils.rotateVec(counterRotation, movecraftLocation.subtract(originLocation)).add(originLocation));
}
final HitBox to = craft.getHitBox().difference(originalLocations);
for (MovecraftLocation location : to) {
var data = location.toBukkit(craft.getWorld()).getBlock().getBlockData();
if (passthroughBlocks.contains(data.getMaterial())) {
craft.getPhaseBlocks().put(location.toBukkit(craft.getWorld()), data);
}
}
// The subtraction of the set of coordinates in the HitBox cube and the HitBox itself
final HitBox invertedHitBox = new SetHitBox(craft.getHitBox().boundingHitBox()).difference(craft.getHitBox());
// A set of locations that are confirmed to be "exterior" locations
final SetHitBox exterior = new SetHitBox();
final SetHitBox interior = new SetHitBox();
// place phased blocks
final Set<Location> overlap = new HashSet<>(craft.getPhaseBlocks().keySet());
overlap.retainAll(craft.getHitBox().asSet().stream().map(l -> l.toBukkit(craft.getWorld())).collect(Collectors.toSet()));
final int minX = craft.getHitBox().getMinX();
final int maxX = craft.getHitBox().getMaxX();
final int minY = craft.getHitBox().getMinY();
final int maxY = overlap.isEmpty() ? craft.getHitBox().getMaxY() : Collections.max(overlap, Comparator.comparingInt(Location::getBlockY)).getBlockY();
final int minZ = craft.getHitBox().getMinZ();
final int maxZ = craft.getHitBox().getMaxZ();
final HitBox[] surfaces = { new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(minX, maxY, maxZ)), new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(maxX, maxY, minZ)), new SolidHitBox(new MovecraftLocation(maxX, minY, maxZ), new MovecraftLocation(minX, maxY, maxZ)), new SolidHitBox(new MovecraftLocation(maxX, minY, maxZ), new MovecraftLocation(maxX, maxY, minZ)), new SolidHitBox(new MovecraftLocation(minX, minY, minZ), new MovecraftLocation(maxX, minY, maxZ)) };
// Valid exterior starts as the 6 surface planes of the HitBox with the locations that lie in the HitBox removed
final SetHitBox validExterior = new SetHitBox();
for (HitBox hitBox : surfaces) {
validExterior.addAll(hitBox.difference(craft.getHitBox()));
}
// Check to see which locations in the from set are actually outside of the craft
for (MovecraftLocation location : validExterior) {
if (craft.getHitBox().contains(location) || exterior.contains(location)) {
continue;
}
// use a modified BFS for multiple origin elements
SetHitBox visited = new SetHitBox();
Queue<MovecraftLocation> queue = new LinkedList<>();
queue.add(location);
while (!queue.isEmpty()) {
MovecraftLocation node = queue.poll();
// If the node is already a valid member of the exterior of the HitBox, continued search is unitary.
for (MovecraftLocation neighbor : CollectionUtils.neighbors(invertedHitBox, node)) {
if (visited.contains(neighbor)) {
continue;
}
visited.add(neighbor);
queue.add(neighbor);
}
}
exterior.addAll(visited);
}
interior.addAll(invertedHitBox.difference(exterior));
final WorldHandler handler = Movecraft.getInstance().getWorldHandler();
for (MovecraftLocation location : invertedHitBox.difference(exterior)) {
var data = location.toBukkit(craft.getWorld()).getBlock().getBlockData();
if (!passthroughBlocks.contains(data.getMaterial())) {
continue;
}
craft.getPhaseBlocks().put(location.toBukkit(craft.getWorld()), data);
}
// translate the craft
handler.rotateCraft(craft, originLocation, rotation);
// trigger sign events
sendSignEvents();
// place confirmed blocks if they have been un-phased
for (MovecraftLocation location : exterior) {
Location bukkit = location.toBukkit(craft.getWorld());
if (!craft.getPhaseBlocks().containsKey(bukkit)) {
continue;
}
var phaseBlock = craft.getPhaseBlocks().remove(bukkit);
handler.setBlockFast(bukkit, phaseBlock);
craft.getPhaseBlocks().remove(bukkit);
}
for (MovecraftLocation location : originalLocations.boundingHitBox()) {
Location bukkit = location.toBukkit(craft.getWorld());
if (!craft.getHitBox().inBounds(location) && craft.getPhaseBlocks().containsKey(bukkit)) {
var phaseBlock = craft.getPhaseBlocks().remove(bukkit);
handler.setBlockFast(bukkit, phaseBlock);
}
}
for (MovecraftLocation location : interior) {
Location bukkit = location.toBukkit(craft.getWorld());
var data = bukkit.getBlock().getBlockData();
if (passthroughBlocks.contains(data.getMaterial())) {
craft.getPhaseBlocks().put(bukkit, data);
handler.setBlockFast(bukkit, Material.AIR.createBlockData());
}
}
} else {
// translate the craft
Movecraft.getInstance().getWorldHandler().rotateCraft(craft, originLocation, rotation);
// trigger sign events
sendSignEvents();
}
if (!craft.isNotProcessing())
craft.setProcessing(false);
time = System.nanoTime() - time;
if (Settings.Debug)
logger.info("Total time: " + (time / 1e6) + " milliseconds. Moving with cooldown of " + craft.getTickCooldown() + ". Speed of: " + String.format("%.2f", craft.getSpeed()));
}
use of net.countercraft.movecraft.util.hitboxes.HitBox in project Movecraft by APDevTeam.
the class AsyncManager method processFadingBlocks.
private void processFadingBlocks() {
if (Settings.FadeWrecksAfter == 0)
return;
long ticksElapsed = (System.currentTimeMillis() - lastFadeCheck) / 50;
if (ticksElapsed <= Settings.FadeTickCooldown)
return;
List<HitBox> processed = new ArrayList<>();
for (Map.Entry<HitBox, Long> entry : wrecks.entrySet()) {
if (Settings.FadeWrecksAfter * 1000L > System.currentTimeMillis() - entry.getValue())
continue;
final HitBox hitBox = entry.getKey();
final Map<Location, BlockData> phaseBlocks = wreckPhases.get(hitBox);
final World world = wreckWorlds.get(hitBox);
List<UpdateCommand> commands = new ArrayList<>();
int fadedBlocks = 0;
if (!processedFadeLocs.containsKey(world))
processedFadeLocs.put(world, new HashSet<>());
int maxFadeBlocks = (int) (hitBox.size() * (Settings.FadePercentageOfWreckPerCycle / 100.0));
// Iterate hitbox as a set to get more random locations
for (MovecraftLocation location : hitBox.asSet()) {
if (processedFadeLocs.get(world).contains(location))
continue;
if (fadedBlocks >= maxFadeBlocks)
break;
final Location bLoc = location.toBukkit(world);
if ((Settings.FadeWrecksAfter + Settings.ExtraFadeTimePerBlock.getOrDefault(bLoc.getBlock().getType(), 0)) * 1000L > System.currentTimeMillis() - entry.getValue())
continue;
fadedBlocks++;
processedFadeLocs.get(world).add(location);
BlockData phaseBlock = phaseBlocks.getOrDefault(bLoc, Material.AIR.createBlockData());
commands.add(new BlockCreateCommand(world, location, phaseBlock));
}
MapUpdateManager.getInstance().scheduleUpdates(commands);
if (!processedFadeLocs.get(world).containsAll(hitBox.asSet()))
continue;
processed.add(hitBox);
processedFadeLocs.get(world).removeAll(hitBox.asSet());
}
for (HitBox hitBox : processed) {
wrecks.remove(hitBox);
wreckPhases.remove(hitBox);
wreckWorlds.remove(hitBox);
}
lastFadeCheck = System.currentTimeMillis();
}
Aggregations