Search in sources :

Example 46 with MovecraftLocation

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

the class TranslationTask method captureYield.

private void captureYield(List<MovecraftLocation> harvestedBlocks) {
    if (harvestedBlocks.isEmpty()) {
        return;
    }
    ArrayList<Inventory> chests = new ArrayList<>();
    // find chests
    for (MovecraftLocation loc : oldHitBox) {
        Block block = craft.getWorld().getBlockAt(loc.getX(), loc.getY(), loc.getZ());
        if (Tags.CHESTS.contains(block.getType()))
            chests.add(((InventoryHolder) (block.getState())).getInventory());
    }
    for (MovecraftLocation harvestedBlock : harvestedBlocks) {
        Block block = craft.getWorld().getBlockAt(harvestedBlock.getX(), harvestedBlock.getY(), harvestedBlock.getZ());
        List<ItemStack> drops = new ArrayList<>(block.getDrops());
        // generate seed drops
        if (block.getType() == Material.WHEAT) {
            Random rand = new Random();
            int amount = rand.nextInt(4);
            if (amount > 0) {
                ItemStack seeds = new ItemStack(Material.WHEAT_SEEDS, amount);
                drops.add(seeds);
            }
        }
        // get contents of inventories before deposting
        if (block.getState() instanceof InventoryHolder) {
            if (block.getState() instanceof Chest) {
                drops.addAll(Arrays.asList(((Chest) block.getState()).getBlockInventory().getContents()));
            } else {
                drops.addAll(Arrays.asList((((InventoryHolder) block.getState()).getInventory().getContents())));
            }
        }
        // call event
        final ItemHarvestEvent harvestEvent = new ItemHarvestEvent(craft, drops, harvestedBlock.toBukkit(craft.getWorld()));
        Bukkit.getServer().getPluginManager().callEvent(harvestEvent);
        for (ItemStack drop : drops) {
            ItemStack retStack = putInToChests(drop, chests);
            if (retStack != null)
                // drop items on position
                updates.add(new ItemDropUpdateCommand(new Location(craft.getWorld(), harvestedBlock.getX(), harvestedBlock.getY(), harvestedBlock.getZ()), retStack));
        }
    }
}
Also used : Chest(org.bukkit.block.Chest) ItemHarvestEvent(net.countercraft.movecraft.events.ItemHarvestEvent) ItemDropUpdateCommand(net.countercraft.movecraft.mapUpdater.update.ItemDropUpdateCommand) ArrayList(java.util.ArrayList) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Block(org.bukkit.block.Block) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory) Location(org.bukkit.Location) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

Example 47 with MovecraftLocation

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

the class BaseCraft method getTickCooldown.

@Override
public int getTickCooldown() {
    if (this instanceof SinkingCraft)
        return type.getIntProperty(CraftType.SINK_RATE_TICKS);
    if (materials.isEmpty()) {
        for (MovecraftLocation location : hitBox) {
            materials.add(location.toBukkit(w).getBlock().getType());
        }
    }
    int chestPenalty = 0;
    for (Material m : Tags.CHESTS) {
        chestPenalty += materials.get(m);
    }
    chestPenalty *= type.getDoubleProperty(CraftType.CHEST_PENALTY);
    if (!cruising)
        return ((int) type.getPerWorldProperty(CraftType.PER_WORLD_TICK_COOLDOWN, w) + chestPenalty) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1);
    // Ascent or Descent
    if (cruiseDirection == CruiseDirection.UP || cruiseDirection == CruiseDirection.DOWN)
        return ((int) type.getPerWorldProperty(CraftType.PER_WORLD_VERT_CRUISE_TICK_COOLDOWN, w) + chestPenalty) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1);
    // Dynamic Fly Block Speed
    int cruiseTickCooldown = (int) type.getPerWorldProperty(CraftType.PER_WORLD_CRUISE_TICK_COOLDOWN, w);
    if (type.getDoubleProperty(CraftType.DYNAMIC_FLY_BLOCK_SPEED_FACTOR) != 0) {
        EnumSet<Material> flyBlockMaterials = type.getMaterialSetProperty(CraftType.DYNAMIC_FLY_BLOCK);
        double count = 0;
        for (Material m : flyBlockMaterials) {
            count += materials.get(m);
        }
        double ratio = count / hitBox.size();
        double speed = (type.getDoubleProperty(CraftType.DYNAMIC_FLY_BLOCK_SPEED_FACTOR) * 1.5) * (ratio - 0.5) + (20.0 / cruiseTickCooldown) + 1;
        return Math.max((int) Math.round((20.0 * ((int) type.getPerWorldProperty(CraftType.PER_WORLD_CRUISE_SKIP_BLOCKS, w) + 1)) / speed) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1), 1);
    }
    if (type.getDoubleProperty(CraftType.DYNAMIC_LAG_SPEED_FACTOR) == 0.0 || type.getDoubleProperty(CraftType.DYNAMIC_LAG_POWER_FACTOR) == 0.0 || Math.abs(type.getDoubleProperty(CraftType.DYNAMIC_LAG_POWER_FACTOR)) > 1.0)
        return (cruiseTickCooldown + chestPenalty) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1);
    if (stats.getCount() == 0)
        return (int) Math.round(20.0 * ((cruiseTickCooldown + 1.0) / type.getDoubleProperty(CraftType.DYNAMIC_LAG_MIN_SPEED)) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1));
    int cruiseSkipBlocks = (int) type.getPerWorldProperty(CraftType.PER_WORLD_CRUISE_SKIP_BLOCKS, w);
    if (Settings.Debug) {
        Bukkit.getLogger().info("Skip: " + cruiseSkipBlocks);
        Bukkit.getLogger().info("Tick: " + cruiseTickCooldown);
        Bukkit.getLogger().info("SpeedFactor: " + type.getDoubleProperty(CraftType.DYNAMIC_LAG_SPEED_FACTOR));
        Bukkit.getLogger().info("PowerFactor: " + type.getDoubleProperty(CraftType.DYNAMIC_LAG_POWER_FACTOR));
        Bukkit.getLogger().info("MinSpeed: " + type.getDoubleProperty(CraftType.DYNAMIC_LAG_MIN_SPEED));
        Bukkit.getLogger().info("CruiseTime: " + getMeanCruiseTime() * 1000.0 + "ms");
    }
    // Dynamic Lag Speed
    double speed = 20.0 * (cruiseSkipBlocks + 1.0) / (float) cruiseTickCooldown;
    speed -= type.getDoubleProperty(CraftType.DYNAMIC_LAG_SPEED_FACTOR) * Math.pow(getMeanCruiseTime() * 1000.0, type.getDoubleProperty(CraftType.DYNAMIC_LAG_POWER_FACTOR));
    speed = Math.max(type.getDoubleProperty(CraftType.DYNAMIC_LAG_MIN_SPEED), speed);
    return (int) Math.round((20.0 * (cruiseSkipBlocks + 1.0)) / speed) * (type.getBoolProperty(CraftType.GEAR_SHIFTS_AFFECT_TICK_COOLDOWN) ? currentGear : 1);
// In theory, the chest penalty is not needed for a DynamicLag craft.
}
Also used : Material(org.bukkit.Material) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

Example 48 with MovecraftLocation

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

the class BaseCraft method getContacts.

/**
 * Gets the crafts that have made contact with this craft
 *
 * @return a set of crafts in contact with this craft
 */
@NotNull
@Override
public Set<Craft> getContacts() {
    final Set<Craft> contacts = new HashSet<>();
    for (Craft contact : CraftManager.getInstance().getCraftsInWorld(w)) {
        if (contact instanceof PilotedCraft && this instanceof PilotedCraft && ((PilotedCraft) contact).getPilot() == ((PilotedCraft) this).getPilot())
            continue;
        MovecraftLocation ccenter = getHitBox().getMidPoint();
        MovecraftLocation tcenter = contact.getHitBox().getMidPoint();
        int distsquared = ccenter.distanceSquared(tcenter);
        double detectionMultiplier;
        if (// TODO: fix the water line
        tcenter.getY() > 65)
            detectionMultiplier = (double) contact.getType().getPerWorldProperty(CraftType.PER_WORLD_DETECTION_MULTIPLIER, contact.getWorld());
        else
            detectionMultiplier = (double) contact.getType().getPerWorldProperty(CraftType.PER_WORLD_UNDERWATER_DETECTION_MULTIPLIER, contact.getWorld());
        int detectionRange = (int) (contact.getOrigBlockCount() * detectionMultiplier);
        detectionRange = detectionRange * 10;
        if (distsquared > detectionRange)
            continue;
        contacts.add(contact);
    }
    return contacts;
}
Also used : MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 49 with MovecraftLocation

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

the class ChunkManager method getChunks.

public static Set<MovecraftChunk> getChunks(Iterable<MovecraftLocation> oldHitBox, World world, int dx, int dy, int dz) {
    Set<MovecraftChunk> chunks = new HashSet<>();
    for (MovecraftLocation oldLocation : oldHitBox) {
        var location = oldLocation.translate(dx, dy, dz);
        int chunkX = location.getX() / 16;
        if (location.getX() < 0)
            chunkX--;
        int chunkZ = location.getZ() / 16;
        if (location.getZ() < 0)
            chunkZ--;
        MovecraftChunk chunk = new MovecraftChunk(chunkX, chunkZ, world);
        chunks.add(chunk);
    }
    return chunks;
}
Also used : MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) MovecraftChunk(net.countercraft.movecraft.MovecraftChunk) HashSet(java.util.HashSet)

Example 50 with MovecraftLocation

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

the class CraftTranslateCommand 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();
    World oldWorld = craft.getWorld();
    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()) {
        // translate the craft
        Movecraft.getInstance().getWorldHandler().translateCraft(craft, displacement, world);
        craft.setWorld(world);
        // trigger sign events
        sendSignEvents();
    } else {
        SetHitBox originalLocations = new SetHitBox();
        for (MovecraftLocation movecraftLocation : craft.getHitBox()) {
            originalLocations.add(movecraftLocation.subtract(displacement));
        }
        final Set<MovecraftLocation> to = Sets.difference(craft.getHitBox().asSet(), originalLocations.asSet());
        // place phased blocks
        for (MovecraftLocation location : to) {
            var data = location.toBukkit(world).getBlock().getBlockData();
            if (passthroughBlocks.contains(data.getMaterial())) {
                craft.getPhaseBlocks().put(location.toBukkit(world), data);
            }
        }
        // The subtraction of the set of coordinates in the HitBox cube and the HitBox itself
        final var invertedHitBox = Sets.difference(craft.getHitBox().boundingHitBox().asSet(), craft.getHitBox().asSet());
        // place phased blocks
        final Set<Location> overlap = new HashSet<>(craft.getPhaseBlocks().keySet());
        overlap.removeIf((location -> !craft.getHitBox().contains(MathUtils.bukkit2MovecraftLoc(location))));
        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(Sets.difference(hitBox.asSet(), craft.getHitBox().asSet()));
        }
        // Check to see which locations in the from set are actually outside of the craft
        final Set<MovecraftLocation> confirmed = craft instanceof SinkingCraft ? invertedHitBox.copyInto(new LinkedHashSet<>()) : verifyExterior(invertedHitBox, validExterior);
        // A set of locations that are confirmed to be "exterior" locations
        final Set<MovecraftLocation> failed = Sets.difference(invertedHitBox, confirmed).copyInto(new LinkedHashSet<>());
        final WorldHandler handler = Movecraft.getInstance().getWorldHandler();
        for (MovecraftLocation location : failed) {
            var data = location.toBukkit(world).getBlock().getBlockData();
            if (!passthroughBlocks.contains(data.getMaterial()))
                continue;
            craft.getPhaseBlocks().put(location.toBukkit(world), data);
        }
        // translate the craft
        handler.translateCraft(craft, displacement, world);
        craft.setWorld(world);
        // trigger sign events
        sendSignEvents();
        for (MovecraftLocation l : failed) {
            MovecraftLocation orig = l.subtract(displacement);
            if (craft.getHitBox().contains(orig) || failed.contains(orig)) {
                continue;
            }
            confirmed.add(orig);
        }
        // place confirmed blocks if they have been un-phased
        for (MovecraftLocation location : confirmed) {
            Location bukkit = location.toBukkit(craft.getWorld());
            if (!craft.getPhaseBlocks().containsKey(bukkit))
                continue;
            // Do not place if it is at a collapsed HitBox location
            if (!craft.getCollapsedHitBox().isEmpty() && craft.getCollapsedHitBox().contains(location))
                continue;
            var phaseBlock = craft.getPhaseBlocks().remove(bukkit);
            handler.setBlockFast(bukkit, phaseBlock);
            craft.getPhaseBlocks().remove(bukkit);
        }
        for (MovecraftLocation location : originalLocations) {
            Location bukkit = location.toBukkit(oldWorld);
            if (!craft.getHitBox().contains(location) && craft.getPhaseBlocks().containsKey(bukkit)) {
                var phaseBlock = craft.getPhaseBlocks().remove(bukkit);
                handler.setBlockFast(bukkit, phaseBlock);
            }
        }
        for (MovecraftLocation location : failed) {
            Location bukkit = location.toBukkit(oldWorld);
            var data = bukkit.getBlock().getBlockData();
            if (passthroughBlocks.contains(data.getMaterial())) {
                craft.getPhaseBlocks().put(bukkit, data);
                handler.setBlockFast(bukkit, Material.AIR.createBlockData());
            }
        }
    // waterlog();
    }
    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()) + ". Displacement of: " + displacement);
    // Only add cruise time if cruising
    if (craft.getCruising() && displacement.getY() == 0 && (displacement.getX() == 0 || displacement.getZ() == 0))
        craft.addCruiseTime(time / 1e9f);
}
Also used : SinkingCraft(net.countercraft.movecraft.craft.SinkingCraft) Arrays(java.util.Arrays) Tag(org.bukkit.Tag) Hash(it.unimi.dsi.fastutil.Hash) Object2ObjectMap(it.unimi.dsi.fastutil.objects.Object2ObjectMap) SolidHitBox(net.countercraft.movecraft.util.hitboxes.SolidHitBox) Waterlogged(org.bukkit.block.data.Waterlogged) HashMap(java.util.HashMap) CraftReleaseEvent(net.countercraft.movecraft.events.CraftReleaseEvent) SignTranslateEvent(net.countercraft.movecraft.events.SignTranslateEvent) WorldHandler(net.countercraft.movecraft.WorldHandler) MathUtils(net.countercraft.movecraft.util.MathUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) Block(org.bukkit.block.Block) Location(org.bukkit.Location) World(org.bukkit.World) Map(java.util.Map) LinkedList(java.util.LinkedList) CraftManager(net.countercraft.movecraft.craft.CraftManager) Material(org.bukkit.Material) LinkedHashSet(java.util.LinkedHashSet) HitBox(net.countercraft.movecraft.util.hitboxes.HitBox) Bukkit(org.bukkit.Bukkit) Sign(org.bukkit.block.Sign) CraftType(net.countercraft.movecraft.craft.type.CraftType) Object2ObjectOpenCustomHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap) Craft(net.countercraft.movecraft.craft.Craft) BlockState(org.bukkit.block.BlockState) Set(java.util.Set) SetHitBox(net.countercraft.movecraft.util.hitboxes.SetHitBox) Logger(java.util.logging.Logger) Movecraft(net.countercraft.movecraft.Movecraft) Sets(com.google.common.collect.Sets) Settings(net.countercraft.movecraft.config.Settings) Objects(java.util.Objects) List(java.util.List) Tags(net.countercraft.movecraft.util.Tags) Queue(java.util.Queue) NotNull(org.jetbrains.annotations.NotNull) ArrayDeque(java.util.ArrayDeque) Comparator(java.util.Comparator) Collections(java.util.Collections) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) LinkedHashSet(java.util.LinkedHashSet) 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) Material(org.bukkit.Material) Logger(java.util.logging.Logger) World(org.bukkit.World) WorldHandler(net.countercraft.movecraft.WorldHandler) SinkingCraft(net.countercraft.movecraft.craft.SinkingCraft) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Location(org.bukkit.Location) MovecraftLocation(net.countercraft.movecraft.MovecraftLocation)

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