Search in sources :

Example 51 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class CraftTranslateCommand method waterlog.

@Deprecated(forRemoval = true)
private void waterlog() {
    final int minX = craft.getHitBox().getMinX();
    final int maxX = craft.getHitBox().getMaxX();
    final int minY = craft.getHitBox().getMinY();
    final int maxY = craft.getHitBox().getMaxY();
    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 surface : surfaces) {
        for (var location : surface) {
            if (!craft.getHitBox().contains(location)) {
                validExterior.add(location);
            }
        }
    }
    var hull = hullSearch(validExterior);
    for (var location : hull) {
        var block = location.toBukkit(world).getBlock();
        var data = block.getBlockData();
        if (!(data instanceof Waterlogged)) {
            continue;
        }
        var shouldFlood = craft.getPhaseBlocks().containsKey(location.toBukkit(world)) && craft.getPhaseBlocks().get(location.toBukkit(world)).getMaterial().equals(Material.WATER);
        if (shouldFlood == ((Waterlogged) data).isWaterlogged()) {
            continue;
        }
        ((Waterlogged) data).setWaterlogged(shouldFlood);
        block.setBlockData(data);
    }
}
Also used : SolidHitBox(net.countercraft.movecraft.util.hitboxes.SolidHitBox) HitBox(net.countercraft.movecraft.util.hitboxes.HitBox) SetHitBox(net.countercraft.movecraft.util.hitboxes.SetHitBox) SolidHitBox(net.countercraft.movecraft.util.hitboxes.SolidHitBox) SetHitBox(net.countercraft.movecraft.util.hitboxes.SetHitBox) Waterlogged(org.bukkit.block.data.Waterlogged) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

Example 52 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class AsyncManager method processCruise.

private void processCruise() {
    for (Craft craft : CraftManager.getInstance()) {
        if (craft == null || !craft.isNotProcessing() || !craft.getCruising())
            continue;
        long ticksElapsed = (System.currentTimeMillis() - craft.getLastCruiseUpdate()) / 50;
        World w = craft.getWorld();
        // time pass more slowly there
        if (craft.getType().getBoolProperty(CraftType.HALF_SPEED_UNDERWATER) && craft.getHitBox().getMinY() < w.getSeaLevel())
            ticksElapsed >>= 1;
        // check direct controls to modify movement
        boolean bankLeft = false;
        boolean bankRight = false;
        boolean dive = false;
        if (craft instanceof PlayerCraft && ((PlayerCraft) craft).getPilotLocked()) {
            Player pilot = ((PlayerCraft) craft).getPilot();
            if (pilot.isSneaking())
                dive = true;
            if (pilot.getInventory().getHeldItemSlot() == 3)
                bankLeft = true;
            if (pilot.getInventory().getHeldItemSlot() == 5)
                bankRight = true;
        }
        int tickCoolDown;
        if (cooldownCache.containsKey(craft)) {
            tickCoolDown = cooldownCache.get(craft);
        } else {
            tickCoolDown = craft.getTickCooldown();
            cooldownCache.put(craft, tickCoolDown);
        }
        // Account for banking and diving in speed calculations by changing the tickCoolDown
        int cruiseSkipBlocks = (int) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_CRUISE_SKIP_BLOCKS, w);
        if (craft.getCruiseDirection() != CruiseDirection.UP && craft.getCruiseDirection() != CruiseDirection.DOWN) {
            if (bankLeft || bankRight) {
                if (!dive) {
                    tickCoolDown *= (Math.sqrt(Math.pow(1 + cruiseSkipBlocks, 2) + Math.pow(cruiseSkipBlocks >> 1, 2)) / (1 + cruiseSkipBlocks));
                } else {
                    tickCoolDown *= (Math.sqrt(Math.pow(1 + cruiseSkipBlocks, 2) + Math.pow(cruiseSkipBlocks >> 1, 2) + 1) / (1 + cruiseSkipBlocks));
                }
            } else if (dive) {
                tickCoolDown *= (Math.sqrt(Math.pow(1 + cruiseSkipBlocks, 2) + 1) / (1 + cruiseSkipBlocks));
            }
        }
        if (Math.abs(ticksElapsed) < tickCoolDown)
            continue;
        cooldownCache.remove(craft);
        int dx = 0;
        int dz = 0;
        int dy = 0;
        int vertCruiseSkipBlocks = (int) craft.getType().getPerWorldProperty(CraftType.PER_WORLD_VERT_CRUISE_SKIP_BLOCKS, craft.getWorld());
        // ascend
        if (craft.getCruiseDirection() == CruiseDirection.UP)
            dy = 1 + vertCruiseSkipBlocks;
        // descend
        if (craft.getCruiseDirection() == CruiseDirection.DOWN) {
            dy = -1 - vertCruiseSkipBlocks;
            if (craft.getHitBox().getMinY() <= w.getSeaLevel())
                dy = -1;
        } else if (dive) {
            dy = -((cruiseSkipBlocks + 1) >> 1);
            if (craft.getHitBox().getMinY() <= w.getSeaLevel())
                dy = -1;
        }
        // ship faces west
        if (craft.getCruiseDirection() == CruiseDirection.WEST) {
            dx = -1 - cruiseSkipBlocks;
            if (bankRight)
                dz = (-1 - cruiseSkipBlocks) >> 1;
            if (bankLeft)
                dz = (1 + cruiseSkipBlocks) >> 1;
        }
        // ship faces east
        if (craft.getCruiseDirection() == CruiseDirection.EAST) {
            dx = 1 + cruiseSkipBlocks;
            if (bankLeft)
                dz = (-1 - cruiseSkipBlocks) >> 1;
            if (bankRight)
                dz = (1 + cruiseSkipBlocks) >> 1;
        }
        // ship faces north
        if (craft.getCruiseDirection() == CruiseDirection.SOUTH) {
            dz = 1 + cruiseSkipBlocks;
            if (bankRight)
                dx = (-1 - cruiseSkipBlocks) >> 1;
            if (bankLeft)
                dx = (1 + cruiseSkipBlocks) >> 1;
        }
        // ship faces south
        if (craft.getCruiseDirection() == CruiseDirection.NORTH) {
            dz = -1 - cruiseSkipBlocks;
            if (bankLeft)
                dx = (-1 - cruiseSkipBlocks) >> 1;
            if (bankRight)
                dx = (1 + cruiseSkipBlocks) >> 1;
        }
        if (craft.getType().getBoolProperty(CraftType.CRUISE_ON_PILOT)) {
            dy = craft.getType().getIntProperty(CraftType.CRUISE_ON_PILOT_VERT_MOVE);
        }
        if (craft.getType().getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_CRUISE_SKIP_BLOCKS)) {
            final int gearshift = craft.getCurrentGear();
            dx *= gearshift;
            dy *= gearshift;
            dz *= gearshift;
        }
        craft.translate(dx, dy, dz);
        craft.setLastTranslation(new MovecraftLocation(dx, dy, dz));
        craft.setLastCruiseUpdate(System.currentTimeMillis());
    }
}
Also used : Player(org.bukkit.entity.Player) SinkingCraft(net.countercraft.movecraft.craft.SinkingCraft) PilotedCraft(net.countercraft.movecraft.craft.PilotedCraft) PlayerCraft(net.countercraft.movecraft.craft.PlayerCraft) Craft(net.countercraft.movecraft.craft.Craft) World(org.bukkit.World) PlayerCraft(net.countercraft.movecraft.craft.PlayerCraft) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

Example 53 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation 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();
}
Also used : HitBox(net.countercraft.movecraft.util.hitboxes.HitBox) ArrayList(java.util.ArrayList) World(org.bukkit.World) UpdateCommand(net.countercraft.movecraft.mapUpdater.update.UpdateCommand) BlockData(org.bukkit.block.data.BlockData) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) BlockCreateCommand(net.countercraft.movecraft.mapUpdater.update.BlockCreateCommand) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) Location(org.bukkit.Location) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) HashSet(java.util.HashSet)

Example 54 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class AsyncManager method processDetection.

private void processDetection() {
    long ticksElapsed = (System.currentTimeMillis() - lastContactCheck) / 50;
    if (ticksElapsed > 21) {
        for (World w : Bukkit.getWorlds()) {
            if (w == null)
                continue;
            for (Craft craft : CraftManager.getInstance().getPlayerCraftsInWorld(w)) {
                if (!recentContactTracking.containsKey(craft))
                    recentContactTracking.put(craft, new HashMap<>());
                for (Craft target : craft.getContacts()) {
                    MovecraftLocation craftCenter = craft.getHitBox().getMidPoint();
                    MovecraftLocation targetCenter = target.getHitBox().getMidPoint();
                    int diffx = craftCenter.getX() - targetCenter.getX();
                    int diffz = craftCenter.getZ() - targetCenter.getZ();
                    int distsquared = craftCenter.distanceSquared(targetCenter);
                    // minute, or is completely new?
                    if (System.currentTimeMillis() - recentContactTracking.get(craft).getOrDefault(target, 0L) <= 60000)
                        continue;
                    Component notification = I18nSupport.getInternationalisedComponent("Contact - New Contact").append(Component.text(": "));
                    if (target.getName().length() >= 1)
                        notification = notification.append(Component.text(target.getName() + " ("));
                    notification = notification.append(Component.text(target.getType().getStringProperty(CraftType.NAME)));
                    if (target.getName().length() >= 1)
                        notification = notification.append(Component.text(")"));
                    notification = notification.append(Component.text(" ")).append(I18nSupport.getInternationalisedComponent("Contact - Commanded By")).append(Component.text(" "));
                    if (target instanceof PilotedCraft)
                        notification = notification.append(Component.text(((PilotedCraft) target).getPilot().getDisplayName()));
                    else
                        notification = notification.append(Component.text("NULL"));
                    notification = notification.append(Component.text(", ")).append(I18nSupport.getInternationalisedComponent("Contact - Size")).append(Component.text(": ")).append(Component.text(target.getOrigBlockCount())).append(Component.text(", ")).append(I18nSupport.getInternationalisedComponent("Contact - Range")).append(Component.text(": ")).append(Component.text((int) Math.sqrt(distsquared))).append(Component.text(" ")).append(I18nSupport.getInternationalisedComponent("Contact - To The")).append(Component.text(" "));
                    if (Math.abs(diffx) > Math.abs(diffz)) {
                        if (diffx < 0)
                            notification = notification.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - East"));
                        else
                            notification = notification.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - West"));
                    } else if (diffz < 0)
                        notification = notification.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - South"));
                    else
                        notification = notification.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - North"));
                    notification = notification.append(Component.text("."));
                    craft.getAudience().sendMessage(notification);
                    var object = craft.getType().getObjectProperty(CraftType.COLLISION_SOUND);
                    if (!(object instanceof Sound))
                        throw new IllegalStateException("COLLISION_SOUND must be of type Sound");
                    craft.getAudience().playSound((Sound) object);
                    long timestamp = System.currentTimeMillis();
                    recentContactTracking.get(craft).put(target, timestamp);
                }
            }
        }
        lastContactCheck = System.currentTimeMillis();
    }
}
Also used : SinkingCraft(net.countercraft.movecraft.craft.SinkingCraft) PilotedCraft(net.countercraft.movecraft.craft.PilotedCraft) PlayerCraft(net.countercraft.movecraft.craft.PlayerCraft) Craft(net.countercraft.movecraft.craft.Craft) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) PilotedCraft(net.countercraft.movecraft.craft.PilotedCraft) Sound(net.kyori.adventure.sound.Sound) World(org.bukkit.World) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) Component(net.kyori.adventure.text.Component)

Example 55 with MovecraftLocation

use of net.countercraft.movecraft.MovecraftLocation in project Movecraft by APDevTeam.

the class BlockListener method onPhysics.

// prevent fragile items from dropping on cruising crafts
@EventHandler(priority = EventPriority.HIGHEST)
public void onPhysics(BlockPhysicsEvent event) {
    if (event.isCancelled()) {
        return;
    }
    Block block = event.getBlock();
    CraftManager.getInstance().getCraftsInWorld(block.getWorld());
    for (Craft tcraft : CraftManager.getInstance().getCraftsInWorld(block.getWorld())) {
        MovecraftLocation mloc = new MovecraftLocation(block.getX(), block.getY(), block.getZ());
        if (!MathUtils.locIsNearCraftFast(tcraft, mloc)) {
            continue;
        }
        if (Tags.FRAGILE_MATERIALS.contains(event.getBlock().getType())) {
            BlockData m = block.getBlockData();
            BlockFace face = BlockFace.DOWN;
            boolean faceAlwaysDown = block.getType() == Material.COMPARATOR || block.getType() == Material.REPEATER;
            if (m instanceof Attachable && !faceAlwaysDown)
                face = ((Attachable) m).getAttachedFace();
            if (!event.getBlock().getRelative(face).getType().isSolid()) {
                event.setCancelled(true);
                return;
            }
        }
    }
}
Also used : Craft(net.countercraft.movecraft.craft.Craft) BlockFace(org.bukkit.block.BlockFace) Block(org.bukkit.block.Block) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) BlockData(org.bukkit.block.data.BlockData) Attachable(org.bukkit.material.Attachable) EventHandler(org.bukkit.event.EventHandler)

Aggregations

MovecraftLocation (net.countercraft.movecraft.MovecraftLocation)60 Craft (net.countercraft.movecraft.craft.Craft)18 EventHandler (org.bukkit.event.EventHandler)18 Material (org.bukkit.Material)17 World (org.bukkit.World)15 Sign (org.bukkit.block.Sign)14 BlockState (org.bukkit.block.BlockState)13 Block (org.bukkit.block.Block)12 ArrayList (java.util.ArrayList)11 Location (org.bukkit.Location)11 Map (java.util.Map)10 HashMap (java.util.HashMap)9 SetHitBox (net.countercraft.movecraft.util.hitboxes.SetHitBox)9 HashSet (java.util.HashSet)8 SinkingCraft (net.countercraft.movecraft.craft.SinkingCraft)8 NotNull (org.jetbrains.annotations.NotNull)8 MovecraftRotation (net.countercraft.movecraft.MovecraftRotation)6 HitBox (net.countercraft.movecraft.util.hitboxes.HitBox)6 SolidHitBox (net.countercraft.movecraft.util.hitboxes.SolidHitBox)6 LinkedList (java.util.LinkedList)5