Search in sources :

Example 31 with ChunkCache

use of net.minecraft.world.ChunkCache in project PneumaticCraft by MineMaarten.

the class AchievementHandler method checkFor9x9.

public static void checkFor9x9(EntityPlayer player, int x, int y, int z) {
    ChunkCache cache = new ChunkCache(player.worldObj, x - 8, y, z - 8, x + 8, y, z + 8, 0);
    ForgeDirection[] dirs = { ForgeDirection.NORTH, ForgeDirection.WEST };
    for (ForgeDirection dir : dirs) {
        int wallLength = 1;
        int minX = x;
        int minZ = z;
        int maxX = x;
        int maxZ = z;
        int newX = x + dir.offsetX;
        int newZ = z + dir.offsetZ;
        while (wallLength < 9 && cache.getBlock(newX, y, newZ) == Blocks.cobblestone) {
            wallLength++;
            minX = Math.min(minX, newX);
            minZ = Math.min(minZ, newZ);
            maxX = Math.max(maxX, newX);
            maxZ = Math.max(maxZ, newZ);
            newX += dir.offsetX;
            newZ += dir.offsetZ;
        }
        newX = x - dir.offsetX;
        newZ = z - dir.offsetZ;
        while (wallLength < 9 && cache.getBlock(newX, y, newZ) == Blocks.cobblestone) {
            wallLength++;
            minX = Math.min(minX, newX);
            minZ = Math.min(minZ, newZ);
            maxX = Math.max(maxX, newX);
            maxZ = Math.max(maxZ, newZ);
            newX -= dir.offsetX;
            newZ -= dir.offsetZ;
        }
        if (wallLength == 9) {
            if (checkFor9x9(cache, x, y, z, minX, minZ, maxX, maxZ)) {
                giveAchievement(player, "dw9x9");
                return;
            }
        }
    }
}
Also used : ChunkCache(net.minecraft.world.ChunkCache) ForgeDirection(net.minecraftforge.common.util.ForgeDirection)

Example 32 with ChunkCache

use of net.minecraft.world.ChunkCache in project PneumaticCraft by MineMaarten.

the class ProgWidgetAreaItemBase method getCache.

public static IBlockAccess getCache(Collection<ChunkPosition> area, World world) {
    if (area.size() == 0)
        return world;
    int minX, minY, minZ, maxX, maxY, maxZ;
    Iterator<ChunkPosition> iterator = area.iterator();
    ChunkPosition p = iterator.next();
    minX = maxX = p.chunkPosX;
    minY = maxY = p.chunkPosY;
    minZ = maxZ = p.chunkPosZ;
    while (iterator.hasNext()) {
        p = iterator.next();
        minX = Math.min(minX, p.chunkPosX);
        minY = Math.min(minY, p.chunkPosY);
        minZ = Math.min(minZ, p.chunkPosZ);
        maxX = Math.max(maxX, p.chunkPosX);
        maxY = Math.max(maxY, p.chunkPosY);
        maxZ = Math.max(maxZ, p.chunkPosZ);
    }
    return new ChunkCache(world, minX, minY, minZ, maxX, maxY, maxZ, 0);
}
Also used : ChunkCache(net.minecraft.world.ChunkCache) ChunkPosition(net.minecraft.world.ChunkPosition)

Example 33 with ChunkCache

use of net.minecraft.world.ChunkCache in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class WorldPhysicsCollider method isBlockInWorldSolidFast.

private boolean isBlockInWorldSolidFast(final int posX, final int posY, final int posZ) {
    if (posY < 0 || posY >= 256) {
        return false;
    }
    final ChunkCache surroundingChunks = parent.getCachedSurroundingChunks();
    final int relativeChunkX = (posX >> 4) - surroundingChunks.chunkX;
    final int relativeChunkZ = (posZ >> 4) - surroundingChunks.chunkZ;
    if (relativeChunkX < 0 || relativeChunkX >= surroundingChunks.chunkArray.length || relativeChunkZ < 0 || relativeChunkZ >= surroundingChunks.chunkArray[relativeChunkX].length) {
        return false;
    }
    final Chunk chunk = surroundingChunks.chunkArray[relativeChunkX][relativeChunkZ];
    if (chunk == null) {
        return false;
    }
    final ExtendedBlockStorage blockStorage = chunk.storageArrays[posY >> 4];
    if (blockStorage == null) {
        return false;
    }
    final IBitOctree octree = ITerrainOctreeProvider.class.cast(blockStorage.data).getSolidOctree();
    return octree.get(posX & 15, posY & 15, posZ & 15);
}
Also used : ChunkCache(net.minecraft.world.ChunkCache) ITerrainOctreeProvider(org.valkyrienskies.mod.common.util.datastructures.ITerrainOctreeProvider) IBitOctree(org.valkyrienskies.mod.common.util.datastructures.IBitOctree) Chunk(net.minecraft.world.chunk.Chunk) ExtendedBlockStorage(net.minecraft.world.chunk.storage.ExtendedBlockStorage)

Example 34 with ChunkCache

use of net.minecraft.world.ChunkCache in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class WorldPhysicsCollider method updatePotentialCollisionCache.

// TODO: The greatest physics lag starts here.
private void updatePotentialCollisionCache() {
    ticksSinceCacheUpdate = 0D;
    // in the same tick
    if (Math.random() > .5) {
        ticksSinceCacheUpdate -= .05D;
    }
    int oldSize = cachedPotentialHits.size();
    // Resets the potential hits array in O(1) time! Isn't that something.
    // cachedPotentialHits.resetQuick();
    cachedPotentialHits.clear();
    AxisAlignedBB shipBBOriginal = parent.getPhysicsTransformAABB();
    if (shipBBOriginal == null) {
        return;
    }
    final AxisAlignedBB shipBB = shipBBOriginal.grow(3);
    // Use the physics tick collision box instead of the game tick collision box.
    // We are using grow(3) on both because for some reason if we don't then ships start
    // jiggling through the ground. God I can't wait for a new physics engine.
    final AxisAlignedBB collisionBB = shipBB.grow(AABB_EXPANSION).expand(calculator.getLinearVelocity().x * .2, calculator.getLinearVelocity().y * .2, calculator.getLinearVelocity().z * .2);
    // Ship is outside of world blockSpace, just skip this all togvalkyrium
    if (collisionBB.maxY < 0 || collisionBB.minY > 255) {
        return;
    }
    // Has a -1 on the minY value, I hope this helps with preventing things from
    // falling through the floor
    BlockPos min = new BlockPos(collisionBB.minX, Math.max(collisionBB.minY - 1, 0), collisionBB.minZ);
    BlockPos max = new BlockPos(collisionBB.maxX, Math.min(collisionBB.maxY, 255), collisionBB.maxZ);
    centerPotentialHit = new BlockPos((min.getX() + max.getX()) / 2D, (min.getY() + max.getY()) / 2D, (min.getZ() + max.getZ()) / 2D);
    ChunkCache cache = parent.getCachedSurroundingChunks();
    if (cache == null) {
        System.err.println("VS Cached Surrounding Chunks was null! This is going to cause catastophric terrible events!!");
        return;
    }
    int chunkMinX = min.getX() >> 4;
    int chunkMaxX = (max.getX() >> 4) + 1;
    int chunkMinZ = min.getZ() >> 4;
    int chunkMaxZ = (max.getZ() >> 4) + 1;
    // long startTime = System.nanoTime();
    int minX = min.getX();
    int minY = min.getY();
    int minZ = min.getZ();
    int maxX = max.getX();
    int maxY = max.getY();
    int maxZ = max.getZ();
    // More multithreading!
    if (VSConfig.MULTITHREADING_SETTINGS.multithreadCollisionCacheUpdate && parent.getBlockPositions().size() > 100) {
        List<Triple<Integer, Integer, TIntList>> tasks = new ArrayList<>();
        for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
            for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
                tasks.add(new ImmutableTriple<>(chunkX, chunkZ, new TIntArrayList()));
            }
        }
        Consumer<Triple<Integer, Integer, TIntList>> consumer = i -> {
            // i is a Tuple<Integer, Integer>
            // updateCollisionCacheParrallel(cache, cachedPotentialHits, i.getFirst(),
            // i.getSecond(), minX, minY, minZ, maxX, maxY, maxZ);
            updateCollisionCacheSequential(cache, i.getLeft(), i.getMiddle(), minX, minY, minZ, maxX, maxY, maxZ, shipBB, i.getRight());
        };
        ValkyrienSkiesMod.getPhysicsThreadPool().submit(() -> tasks.parallelStream().forEach(consumer)).join();
        tasks.forEach(task -> cachedPotentialHits.addAll(task.getRight()));
    } else {
        // Cast to double to avoid overflow errors
        double size = ((double) (chunkMaxX - chunkMinX)) * ((double) (chunkMaxZ - chunkMinZ));
        if (size > 300000) {
            // Sanity check; don't execute the rest of the code because we'll just freeze the physics thread.
            return;
        }
        // TODO: VS thread freezes here.
        for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
            for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
                updateCollisionCacheSequential(cache, chunkX, chunkZ, minX, minY, minZ, maxX, maxY, maxZ, shipBB, cachedPotentialHits);
            }
        }
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Triple(org.apache.commons.lang3.tuple.Triple) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) SpatialDetector(org.valkyrienskies.mod.common.ships.block_relocation.SpatialDetector) TIntArrayList(gnu.trove.list.array.TIntArrayList) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ITerrainOctreeProvider(org.valkyrienskies.mod.common.util.datastructures.ITerrainOctreeProvider) ArrayList(java.util.ArrayList) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) ValkyrienSkiesMod(org.valkyrienskies.mod.common.ValkyrienSkiesMod) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) Vector3d(org.joml.Vector3d) PhysicsCalculations(org.valkyrienskies.mod.common.physics.PhysicsCalculations) Chunk(net.minecraft.world.chunk.Chunk) Triple(org.apache.commons.lang3.tuple.Triple) TIntList(gnu.trove.list.TIntList) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) IBitOctree(org.valkyrienskies.mod.common.util.datastructures.IBitOctree) ChunkCache(net.minecraft.world.ChunkCache) Iterator(java.util.Iterator) Collection(java.util.Collection) TransformType(valkyrienwarfare.api.TransformType) BlockPos(net.minecraft.util.math.BlockPos) Consumer(java.util.function.Consumer) IBlockState(net.minecraft.block.state.IBlockState) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) List(java.util.List) VSConfig(org.valkyrienskies.mod.common.config.VSConfig) MathHelper(net.minecraft.util.math.MathHelper) Vector3dc(org.joml.Vector3dc) ExtendedBlockStorage(net.minecraft.world.chunk.storage.ExtendedBlockStorage) ChunkCache(net.minecraft.world.ChunkCache) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) BlockPos(net.minecraft.util.math.BlockPos) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 35 with ChunkCache

use of net.minecraft.world.ChunkCache in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class WorldWaterCollider method updatePotentialCollisionCache.

private void updatePotentialCollisionCache() {
    secondsSinceCollisionCacheUpdate = 0;
    // ships from all updating in the same tick
    if (Math.random() > .5) {
        secondsSinceCollisionCacheUpdate -= .01;
    }
    cachedPotentialHits.clear();
    AxisAlignedBB shipBBOriginal = parent.getPhysicsTransformAABB();
    if (shipBBOriginal == null) {
        return;
    }
    // We are using grow(3) because its good.
    final AxisAlignedBB shipBB = shipBBOriginal.grow(3);
    final AxisAlignedBB collisionBB = shipBB.grow(AABB_EXPANSION).grow(2 * Math.ceil(RANGE_CHECK));
    // Ship is outside of world blockSpace, just skip this
    if (collisionBB.maxY < 0 || collisionBB.minY > 255) {
        return;
    }
    // Has a -1 on the minY value, I hope this helps with preventing things from
    // falling through the floor
    final BlockPos min = new BlockPos(collisionBB.minX, Math.max(collisionBB.minY - 1, 0), collisionBB.minZ);
    final BlockPos max = new BlockPos(collisionBB.maxX, Math.min(collisionBB.maxY, 255), collisionBB.maxZ);
    centerPotentialHit = new BlockPos((min.getX() + max.getX()) / 2.0, (min.getY() + max.getY()) / 2.0, (min.getZ() + max.getZ()) / 2.0);
    final ChunkCache cache = parent.getCachedSurroundingChunks();
    if (cache == null) {
        System.err.println("VS Cached Surrounding Chunks was null! This is going to cause catastophric terrible events!!");
        return;
    }
    final int chunkMinX = min.getX() >> 4;
    final int chunkMaxX = (max.getX() >> 4) + 1;
    final int chunkMinZ = min.getZ() >> 4;
    final int chunkMaxZ = (max.getZ() >> 4) + 1;
    final int minX = min.getX();
    final int minY = min.getY();
    final int minZ = min.getZ();
    final int maxX = max.getX();
    final int maxY = max.getY();
    final int maxZ = max.getZ();
    // More multithreading!
    if (VSConfig.MULTITHREADING_SETTINGS.multithreadCollisionCacheUpdate && parent.getBlockPositions().size() > 100) {
        final List<Triple<Integer, Integer, TIntList>> tasks = new ArrayList<>();
        for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
            for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
                tasks.add(new ImmutableTriple<>(chunkX, chunkZ, new TIntArrayList()));
            }
        }
        Consumer<Triple<Integer, Integer, TIntList>> consumer = i -> updateCollisionCacheSequential(cache, i.getLeft(), i.getMiddle(), minX, minY, minZ, maxX, maxY, maxZ, shipBB, i.getRight());
        ValkyrienSkiesMod.getPhysicsThreadPool().submit(() -> tasks.parallelStream().forEach(consumer)).join();
        tasks.forEach(task -> cachedPotentialHits.addAll(task.getRight()));
    } else {
        // Cast to double to avoid overflow errors
        final double size = ((double) (chunkMaxX - chunkMinX)) * ((double) (chunkMaxZ - chunkMinZ));
        if (size > 300000) {
            // Sanity check; don't execute the rest of the code because we'll just freeze the physics thread.
            return;
        }
        for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
            for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
                updateCollisionCacheSequential(cache, chunkX, chunkZ, minX, minY, minZ, maxX, maxY, maxZ, shipBB, cachedPotentialHits);
            }
        }
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) Triple(org.apache.commons.lang3.tuple.Triple) TIntList(gnu.trove.list.TIntList) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) SpatialDetector(org.valkyrienskies.mod.common.ships.block_relocation.SpatialDetector) IBitOctree(org.valkyrienskies.mod.common.util.datastructures.IBitOctree) TIntArrayList(gnu.trove.list.array.TIntArrayList) ChunkCache(net.minecraft.world.ChunkCache) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) TransformType(valkyrienwarfare.api.TransformType) BlockPos(net.minecraft.util.math.BlockPos) ITerrainOctreeProvider(org.valkyrienskies.mod.common.util.datastructures.ITerrainOctreeProvider) ArrayList(java.util.ArrayList) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) Consumer(java.util.function.Consumer) List(java.util.List) VSConfig(org.valkyrienskies.mod.common.config.VSConfig) MathHelper(net.minecraft.util.math.MathHelper) ValkyrienSkiesMod(org.valkyrienskies.mod.common.ValkyrienSkiesMod) Vector3d(org.joml.Vector3d) PhysicsCalculations(org.valkyrienskies.mod.common.physics.PhysicsCalculations) Chunk(net.minecraft.world.chunk.Chunk) Triple(org.apache.commons.lang3.tuple.Triple) ExtendedBlockStorage(net.minecraft.world.chunk.storage.ExtendedBlockStorage) ChunkCache(net.minecraft.world.ChunkCache) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.util.math.BlockPos) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Aggregations

ChunkCache (net.minecraft.world.ChunkCache)36 TileEntity (net.minecraft.tileentity.TileEntity)16 BlockPos (net.minecraft.util.math.BlockPos)9 IBlockState (net.minecraft.block.state.IBlockState)8 Block (net.minecraft.block.Block)5 World (net.minecraft.world.World)5 LazyBlockState (com.almuradev.content.type.block.state.LazyBlockState)4 WeightedLazyBlockState (com.almuradev.content.util.WeightedLazyBlockState)4 ArrayList (java.util.ArrayList)4 BlockBush (net.minecraft.block.BlockBush)4 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)4 IBlockAccess (net.minecraft.world.IBlockAccess)4 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 BlockCactus (net.minecraft.block.BlockCactus)3 ItemStack (net.minecraft.item.ItemStack)3 PathEntity (net.minecraft.pathfinding.PathEntity)3 PathPoint (net.minecraft.pathfinding.PathPoint)3 MutableBlockPos (net.minecraft.util.math.BlockPos.MutableBlockPos)3 Chunk (net.minecraft.world.chunk.Chunk)3 ExtendedBlockStorage (net.minecraft.world.chunk.storage.ExtendedBlockStorage)3