Search in sources :

Example 1 with MutableBlockPos

use of net.minecraft.util.math.BlockPos.MutableBlockPos in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class PhysicsObject method assembleShip.

private void assembleShip(EntityPlayer player, SpatialDetector detector, BlockPos centerInWorld) {
    MutableBlockPos pos = new MutableBlockPos();
    TIntIterator iter = detector.foundSet.iterator();
    int radiusNeeded = 1;
    while (iter.hasNext()) {
        int i = iter.next();
        detector.setPosWithRespectTo(i, BlockPos.ORIGIN, pos);
        int xRad = Math.abs(pos.getX() >> 4);
        int zRad = Math.abs(pos.getZ() >> 4);
        radiusNeeded = Math.max(Math.max(zRad, xRad), radiusNeeded + 1);
    }
    //		radiusNeeded = Math.max(radiusNeeded, 5);
    iter = detector.foundSet.iterator();
    radiusNeeded = Math.min(radiusNeeded, ValkyrienWarfareMod.chunkManager.getManagerForWorld(wrapper.worldObj).maxChunkRadius);
    // System.out.println(radiusNeeded);
    claimNewChunks(radiusNeeded);
    claimedChunks = new Chunk[(ownedChunks.radius * 2) + 1][(ownedChunks.radius * 2) + 1];
    claimedChunksEntries = new PlayerChunkMapEntry[(ownedChunks.radius * 2) + 1][(ownedChunks.radius * 2) + 1];
    for (int x = ownedChunks.minX; x <= ownedChunks.maxX; x++) {
        for (int z = ownedChunks.minZ; z <= ownedChunks.maxZ; z++) {
            Chunk chunk = new Chunk(worldObj, x, z);
            injectChunkIntoWorld(chunk, x, z, true);
            claimedChunks[x - ownedChunks.minX][z - ownedChunks.minZ] = chunk;
        }
    }
    //Prevents weird shit from spawning at the edges of a ship
    replaceOuterChunksWithAir();
    VKChunkCache = new VWChunkCache(worldObj, claimedChunks);
    int minChunkX = claimedChunks[0][0].xPosition;
    int minChunkZ = claimedChunks[0][0].zPosition;
    refrenceBlockPos = getRegionCenter();
    centerCoord = new Vector(refrenceBlockPos.getX(), refrenceBlockPos.getY(), refrenceBlockPos.getZ());
    createPhysicsCalculations();
    //The ship just got build, how can it not be the latest?
    physicsProcessor.isShipPastBuild90 = true;
    BlockPos centerDifference = refrenceBlockPos.subtract(centerInWorld);
    while (iter.hasNext()) {
        int i = iter.next();
        detector.setPosWithRespectTo(i, centerInWorld, pos);
        IBlockState state = detector.cache.getBlockState(pos);
        TileEntity worldTile = detector.cache.getTileEntity(pos);
        pos.setPos(pos.getX() + centerDifference.getX(), pos.getY() + centerDifference.getY(), pos.getZ() + centerDifference.getZ());
        ownedChunks.chunkOccupiedInLocal[(pos.getX() >> 4) - minChunkX][(pos.getZ() >> 4) - minChunkZ] = true;
        Chunk chunkToSet = claimedChunks[(pos.getX() >> 4) - minChunkX][(pos.getZ() >> 4) - minChunkZ];
        int storageIndex = pos.getY() >> 4;
        if (chunkToSet.storageArrays[storageIndex] == chunkToSet.NULL_BLOCK_STORAGE) {
            chunkToSet.storageArrays[storageIndex] = new ExtendedBlockStorage(storageIndex << 4, true);
        }
        chunkToSet.storageArrays[storageIndex].set(pos.getX() & 15, pos.getY() & 15, pos.getZ() & 15, state);
        if (worldTile != null) {
            NBTTagCompound tileEntNBT = new NBTTagCompound();
            tileEntNBT = worldTile.writeToNBT(tileEntNBT);
            // Change the Block position to be inside of the Ship
            tileEntNBT.setInteger("x", pos.getX());
            tileEntNBT.setInteger("y", pos.getY());
            tileEntNBT.setInteger("z", pos.getZ());
            //Fuck this old code
            //				TileEntity newInstance = VKChunkCache.getTileEntity(pos);
            //				newInstance.readFromNBT(tileEntNBT);
            TileEntity newInstance = TileEntity.create(worldObj, tileEntNBT);
            newInstance.validate();
            Class tileClass = newInstance.getClass();
            Field[] fields = tileClass.getDeclaredFields();
            for (Field field : fields) {
                try {
                    field.setAccessible(true);
                    Object o = field.get(newInstance);
                    if (o != null) {
                        if (o instanceof BlockPos) {
                            BlockPos inTilePos = (BlockPos) o;
                            int hash = detector.getHashWithRespectTo(inTilePos.getX(), inTilePos.getY(), inTilePos.getZ(), detector.firstBlock);
                            if (detector.foundSet.contains(hash)) {
                                if (!(o instanceof MutableBlockPos)) {
                                    inTilePos = inTilePos.add(centerDifference.getX(), centerDifference.getY(), centerDifference.getZ());
                                    field.set(newInstance, inTilePos);
                                } else {
                                    MutableBlockPos mutable = (MutableBlockPos) o;
                                    mutable.setPos(inTilePos.getX() + centerDifference.getX(), inTilePos.getY() + centerDifference.getY(), inTilePos.getZ() + centerDifference.getZ());
                                }
                            }
                        }
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            worldObj.setTileEntity(newInstance.getPos(), newInstance);
            newInstance.markDirty();
        }
    // chunkCache.setBlockState(pos, state);
    // worldObj.setBlockState(pos, state);
    }
    iter = detector.foundSet.iterator();
    short[][] changes = new short[claimedChunks.length][claimedChunks[0].length];
    while (iter.hasNext()) {
        int i = iter.next();
        // BlockPos respectTo = detector.getPosWithRespectTo(i, centerInWorld);
        detector.setPosWithRespectTo(i, centerInWorld, pos);
        // detector.cache.setBlockState(pos, Blocks.air.getDefaultState());
        // TODO: Get this to update on clientside as well, you bastard!
        TileEntity tile = worldObj.getTileEntity(pos);
        if (tile != null) {
            tile.invalidate();
        }
        worldObj.setBlockState(pos, Blocks.AIR.getDefaultState(), 2);
    }
    for (int x = ownedChunks.minX; x <= ownedChunks.maxX; x++) {
        for (int z = ownedChunks.minZ; z <= ownedChunks.maxZ; z++) {
            claimedChunks[x - ownedChunks.minX][z - ownedChunks.minZ].isTerrainPopulated = true;
        //				claimedChunks[x - ownedChunks.minX][z - ownedChunks.minZ].generateSkylightMap();
        // claimedChunks[x-ownedChunks.minX][z-ownedChunks.minZ].checkLight();
        }
    }
    detectBlockPositions();
    //TODO: This fixes the lighting, but it adds lag; maybe remove this
    for (int x = ownedChunks.minX; x <= ownedChunks.maxX; x++) {
        for (int z = ownedChunks.minZ; z <= ownedChunks.maxZ; z++) {
            //				claimedChunks[x - ownedChunks.minX][z - ownedChunks.minZ].isTerrainPopulated = true;
            claimedChunks[x - ownedChunks.minX][z - ownedChunks.minZ].generateSkylightMap();
            claimedChunks[x - ownedChunks.minX][z - ownedChunks.minZ].checkLight();
        }
    }
    coordTransform = new CoordTransformObject(this);
    physicsProcessor.processInitialPhysicsData();
    physicsProcessor.updateParentCenterOfMass();
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) VWChunkCache(ValkyrienWarfareBase.Relocation.VWChunkCache) IBlockState(net.minecraft.block.state.IBlockState) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) SPacketUnloadChunk(net.minecraft.network.play.server.SPacketUnloadChunk) Chunk(net.minecraft.world.chunk.Chunk) ExtendedBlockStorage(net.minecraft.world.chunk.storage.ExtendedBlockStorage) TileEntity(net.minecraft.tileentity.TileEntity) Field(java.lang.reflect.Field) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) BlockPos(net.minecraft.util.math.BlockPos) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) Vector(ValkyrienWarfareBase.API.Vector)

Example 2 with MutableBlockPos

use of net.minecraft.util.math.BlockPos.MutableBlockPos in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class SpatialDetector method calculateSpatialOccupation.

public void calculateSpatialOccupation() {
    nextQueue.add(firstBlock.getY() + maxRange * maxRangeHalved + maxRangeSquared * maxRangeHalved);
    MutableBlockPos inRealWorld = new MutableBlockPos();
    int hash;
    while (!nextQueue.isEmpty() && !cleanHouse) {
        TIntIterator queueIter = nextQueue.iterator();
        foundSet.addAll(nextQueue);
        nextQueue = new TIntHashSet();
        while (queueIter.hasNext()) {
            hash = queueIter.next();
            setPosWithRespectTo(hash, firstBlock, inRealWorld);
            if (corners) {
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() - 1, inRealWorld.getZ() - 1, hash - maxRange - 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() - 1, inRealWorld.getZ(), hash - maxRange - 1);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() - 1, inRealWorld.getZ() + 1, hash - maxRange - 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY(), inRealWorld.getZ() - 1, hash - maxRange - maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY(), inRealWorld.getZ(), hash - maxRange);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY(), inRealWorld.getZ() + 1, hash - maxRange + maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() + 1, inRealWorld.getZ() - 1, hash - maxRange + 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() + 1, inRealWorld.getZ(), hash - maxRange + 1);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY() + 1, inRealWorld.getZ() + 1, hash - maxRange + 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() - 1, inRealWorld.getZ() - 1, hash - 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() - 1, inRealWorld.getZ(), hash - 1);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() - 1, inRealWorld.getZ() + 1, hash - 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() - 1, hash - maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() + 1, hash + maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() + 1, inRealWorld.getZ() - 1, hash + 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() + 1, inRealWorld.getZ(), hash + 1);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() + 1, inRealWorld.getZ() + 1, hash + 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() - 1, inRealWorld.getZ() - 1, hash + maxRange - 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() - 1, inRealWorld.getZ(), hash + maxRange - 1);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() - 1, inRealWorld.getZ() + 1, hash + maxRange - 1 + maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY(), inRealWorld.getZ() - 1, hash + maxRange - maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY(), inRealWorld.getZ(), hash + maxRange);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY(), inRealWorld.getZ() + 1, hash + maxRange + maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() + 1, inRealWorld.getZ() - 1, hash + maxRange + 1 - maxRangeSquared);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() + 1, inRealWorld.getZ(), hash + maxRange + 1);
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY() + 1, inRealWorld.getZ() + 1, hash + maxRange + 1 + maxRangeSquared);
            } else {
                tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY(), inRealWorld.getZ(), hash + maxRange);
                tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY(), inRealWorld.getZ(), hash - maxRange);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() + 1, inRealWorld.getZ(), hash + 1);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY() - 1, inRealWorld.getZ(), hash - 1);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() + 1, hash + maxRangeSquared);
                tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() - 1, hash - maxRangeSquared);
            }
        }
    }
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) TIntHashSet(gnu.trove.set.hash.TIntHashSet)

Example 3 with MutableBlockPos

use of net.minecraft.util.math.BlockPos.MutableBlockPos in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class BalloonDetector method calculateSpatialOccupation.

@Override
public void calculateSpatialOccupation() {
    nextQueue.add(firstBlock.getY() + maxRange * maxRangeHalved + maxRangeSquared * maxRangeHalved);
    MutableBlockPos inRealWorld = new MutableBlockPos();
    int hash;
    while (!nextQueue.isEmpty() && !cleanHouse) {
        TIntIterator queueIter = nextQueue.iterator();
        foundSet.addAll(nextQueue);
        nextQueue = new TIntHashSet();
        while (queueIter.hasNext()) {
            hash = queueIter.next();
            setPosWithRespectTo(hash, firstBlock, inRealWorld);
            tryExpanding(inRealWorld.getX() + 1, inRealWorld.getY(), inRealWorld.getZ(), hash + maxRange);
            tryExpanding(inRealWorld.getX() - 1, inRealWorld.getY(), inRealWorld.getZ(), hash - maxRange);
            tryExpanding(inRealWorld.getX(), inRealWorld.getY() + 1, inRealWorld.getZ(), hash + 1);
            // tryExpanding(inRealWorld.getX(),inRealWorld.getY()-1,inRealWorld.getZ(),hash-1);
            tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() + 1, hash + maxRangeSquared);
            tryExpanding(inRealWorld.getX(), inRealWorld.getY(), inRealWorld.getZ() - 1, hash - maxRangeSquared);
        }
    }
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) TIntHashSet(gnu.trove.set.hash.TIntHashSet)

Example 4 with MutableBlockPos

use of net.minecraft.util.math.BlockPos.MutableBlockPos in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class BalloonProcessor method checkBalloonForSplit.

// Loop through all balloon air positions, and if some are split; remove the smaller group
private void checkBalloonForSplit() {
    HashSet<BlockPos> posititionsNeedingAtatchment = new HashSet<BlockPos>();
    ArrayList<BalloonAirDetector> foundSets = new ArrayList<BalloonAirDetector>();
    MutableBlockPos mutable = new MutableBlockPos();
    posititionsNeedingAtatchment.addAll(internalAirPositions);
    while (posititionsNeedingAtatchment.size() > 0) {
        BlockPos start = getRandomPosFromSet(posititionsNeedingAtatchment);
        BalloonAirDetector detector = new BalloonAirDetector(start, parent.worldObj, currentBalloonSize, this, posititionsNeedingAtatchment);
        foundSets.add(detector);
        TIntIterator iterator = detector.foundSet.iterator();
        while (iterator.hasNext()) {
            int hash = iterator.next();
            detector.setPosWithRespectTo(hash, start, mutable);
            posititionsNeedingAtatchment.remove(mutable);
        }
    }
    if (foundSets.size() > 1) {
        for (BalloonAirDetector split : foundSets) {
            System.out.println(split.foundSet.size());
        }
        processFoundSplits(foundSets);
    // System.out.println("Post: "+internalAirPositions.size());
    }
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) ArrayList(java.util.ArrayList) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) BlockPos(net.minecraft.util.math.BlockPos) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) HashSet(java.util.HashSet)

Example 5 with MutableBlockPos

use of net.minecraft.util.math.BlockPos.MutableBlockPos in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class WorldPhysicsCollider method processPotentialCollisionsAccurately.

// Runs through the cache ArrayList, checking each possible BlockPos for SOLID blocks that can collide, if it finds any it will
// move to the next method
//TODO: Optimize from here, this is taking 10x the processing time of updating collision cache!
private void processPotentialCollisionsAccurately() {
    final MutableBlockPos localCollisionPos = new MutableBlockPos();
    final Vector inWorld = new Vector();
    int minX, minY, minZ, maxX, maxY, maxZ, x, y, z;
    final double rangeCheck = .65D;
    TIntIterator intIterator = cachedPotentialHits.iterator();
    while (intIterator.hasNext()) {
        // Converts the int to a mutablePos
        SpatialDetector.setPosWithRespectTo(intIterator.next(), centerPotentialHit, mutablePos);
        inWorld.X = mutablePos.getX() + .5;
        inWorld.Y = mutablePos.getY() + .5;
        inWorld.Z = mutablePos.getZ() + .5;
        parent.coordTransform.fromGlobalToLocal(inWorld);
        minX = MathHelper.floor_double(inWorld.X - rangeCheck);
        minY = MathHelper.floor_double(inWorld.Y - rangeCheck);
        minZ = MathHelper.floor_double(inWorld.Z - rangeCheck);
        maxX = MathHelper.floor_double(inWorld.X + rangeCheck);
        maxY = MathHelper.floor_double(inWorld.Y + rangeCheck);
        maxZ = MathHelper.floor_double(inWorld.Z + rangeCheck);
        /**
			 * Something here is causing the game to freeze :/
			 */
        int minChunkX = minX >> 4;
        int minChunkY = minY >> 4;
        int minChunkZ = minZ >> 4;
        int maxChunkX = maxX >> 4;
        int maxChunkY = maxY >> 4;
        int maxChunkZ = maxZ >> 4;
        entireLoop: if (!(minChunkY > 15 || maxChunkY < 0)) {
            for (int chunkX = minChunkX; chunkX <= maxChunkX; chunkX++) {
                for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; chunkZ++) {
                    if (parent.ownsChunk(chunkX, chunkZ)) {
                        final Chunk chunkIn = parent.VKChunkCache.getChunkAt(chunkX, chunkZ);
                        int minXToCheck = chunkX << 4;
                        int maxXToCheck = minXToCheck + 15;
                        int minZToCheck = chunkZ << 4;
                        int maxZToCheck = minZToCheck + 15;
                        minXToCheck = Math.max(minXToCheck, minX);
                        maxXToCheck = Math.min(maxXToCheck, maxX);
                        minZToCheck = Math.max(minZToCheck, minZ);
                        maxZToCheck = Math.min(maxZToCheck, maxZ);
                        for (int chunkY = minChunkY; chunkY <= maxChunkY; chunkY++) {
                            ExtendedBlockStorage storage = chunkIn.storageArrays[chunkY];
                            if (storage != null) {
                                int minYToCheck = chunkY << 4;
                                int maxYToCheck = minYToCheck + 15;
                                minYToCheck = Math.max(minYToCheck, minY);
                                maxYToCheck = Math.min(maxYToCheck, maxY);
                                for (x = minXToCheck; x <= maxXToCheck; x++) {
                                    for (z = minZToCheck; z <= maxZToCheck; z++) {
                                        for (y = minYToCheck; y <= maxYToCheck; y++) {
                                            final IBlockState state = storage.get(x & 15, y & 15, z & 15);
                                            if (state.getMaterial().isSolid()) {
                                                localCollisionPos.setPos(x, y, z);
                                                boolean brokeAWorldBlock = handleLikelyCollision(mutablePos, localCollisionPos, parent.surroundingWorldChunksCache.getBlockState(mutablePos), state);
                                                if (brokeAWorldBlock) {
                                                    int positionRemoved = SpatialDetector.getHashWithRespectTo(mutablePos.getX(), mutablePos.getY(), mutablePos.getZ(), centerPotentialHit);
                                                    cachedHitsToRemove.add(positionRemoved);
                                                    break entireLoop;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    //The old way of doing things
    /*for (x = minX; x <= maxX; x++) {
				for (z = minZ; z <= maxZ; z++) {
					if (parent.ownsChunk(x >> 4, z >> 4)) {
						for (y = minY; y <= maxY; y++) {
							final Chunk chunkIn = parent.VKChunkCache.getChunkAt(x >> 4, z >> 4);
							final IBlockState state = chunkIn.getBlockState(x, y, z);
							if (state.getMaterial().isSolid()) {
								localCollisionPos.setPos(x, y, z);

								handleLikelyCollision(mutablePos, localCollisionPos, parent.surroundingWorldChunksCache.getBlockState(mutablePos), state);
							}
						}
					}
				}
			}*/
    }
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) IBlockState(net.minecraft.block.state.IBlockState) Chunk(net.minecraft.world.chunk.Chunk) ExtendedBlockStorage(net.minecraft.world.chunk.storage.ExtendedBlockStorage) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) Vector(ValkyrienWarfareBase.API.Vector)

Aggregations

TIntIterator (gnu.trove.iterator.TIntIterator)5 MutableBlockPos (net.minecraft.util.math.BlockPos.MutableBlockPos)5 Vector (ValkyrienWarfareBase.API.Vector)2 TIntHashSet (gnu.trove.set.hash.TIntHashSet)2 IBlockState (net.minecraft.block.state.IBlockState)2 BlockPos (net.minecraft.util.math.BlockPos)2 Chunk (net.minecraft.world.chunk.Chunk)2 ExtendedBlockStorage (net.minecraft.world.chunk.storage.ExtendedBlockStorage)2 VWChunkCache (ValkyrienWarfareBase.Relocation.VWChunkCache)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 SPacketUnloadChunk (net.minecraft.network.play.server.SPacketUnloadChunk)1 TileEntity (net.minecraft.tileentity.TileEntity)1