Advertisement
Guest User

Block Integrity snippet

a guest
Mar 25th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1.     /**
  2.      * Tries to load a chunk from disk. If the chunk is not present,
  3.      * it is created from scratch.
  4.      *
  5.      * @return True if a generation has been executed
  6.      */
  7.     public boolean generate() {
  8.         if (isFresh()) {
  9.             for (ChunkGenerator gen : _parent.getGeneratorManager().getChunkGenerators()) {
  10.                 gen.generate(this);
  11.             }
  12.  
  13.             // Use a TeraArray here to test for integrity - how well the blocks are supported by the world
  14.             TeraArray integrity = new TeraArray(CHUNK_DIMENSION_X, CHUNK_DIMENSION_Y, CHUNK_DIMENSION_Z);
  15.             System.out.println("Generating chunk" + getPosition());
  16.  
  17.             // Fill the bottom layer (which should always be MantleStone) with the highest possible integrity value
  18.             for (int x = 0; x < CHUNK_DIMENSION_X; x++) {
  19.                 for (int z = 0; z < CHUNK_DIMENSION_Z; z++) {
  20.                     integrity.set(x, 0, z, (byte) 255);
  21.                 }
  22.             }
  23.  
  24.             // Now fill in values for all blocks that are immediately supported by the bottom layer (so we start at y = 1)
  25.             for (int y = 1; y < CHUNK_DIMENSION_Y; y++) {
  26.                 for (int x = 0; x < CHUNK_DIMENSION_X; x++) {
  27.                     for (int z = 0; z < CHUNK_DIMENSION_Z; z++) {
  28.                         byte b = _blocks.get(x, y, z);
  29.                         //System.out.println("Block ID at " + x + ", " + y + ", " + z + " is " + _blocks.get(x, y, z));
  30.                         //Block myBlock = BlockManager.getInstance().getBlock(b);
  31.                         //System.out.println("That Block is: " + myBlock);
  32.  
  33.                         //Block below = BlockManager.getInstance().getBlock(_blocks.get(x, y - 1, z));
  34.                         //System.out.println("The Block below it is: " + below);
  35.  
  36.                         //Block above = BlockManager.getInstance().getBlock(_blocks.get(x, y + 1, z));
  37.                         //System.out.println("The Block above it is: " + above);
  38.  
  39.                         //Block left = BlockManager.getInstance().getBlock(_blocks.get(x - 1, y, z));
  40.                         //System.out.println("The Block to the left of it is: " + left);
  41.  
  42.                         //Block right = BlockManager.getInstance().getBlock(_blocks.get(x + 1, y, z));
  43.                         //System.out.println("The Block to the right of it is: " + right);
  44.  
  45.                         //Block front = BlockManager.getInstance().getBlock(_blocks.get(x, y, z - 1));
  46.                         //System.out.println("The Block to its front is: " + front);
  47.  
  48.                         //Block back = BlockManager.getInstance().getBlock(_blocks.get(x, y, z + 1));
  49.                         //System.out.println("The Block to its back is: " + back);
  50.  
  51.                         // If the below block has non-zero integrity it is supported by the world. Propagate the support upwards
  52.                         byte targetIntegrity = integrity.get(x, y - 1, z);
  53.                         if ( targetIntegrity != 0 && b != 0) {
  54.                             integrity.set(x, y, z, targetIntegrity);
  55.                             //if (x == 5 && z == 5)
  56.                                 //System.out.println("Assigning integrity of " + targetIntegrity + " to 5, " + y + ", 5");
  57.  
  58.                         } else {
  59.                             _blocks.set(x, y, z, (byte) 0);
  60.                             //if (x == 5 && z == 5)
  61.                                 //System.out.println("Erasing block at 5, " + y + ", 5");
  62.                         }
  63.  
  64.                         //myIntegrity += below.getHardness() + left.getHardness() + right.getHardness() + front.getHardness() + back.getHardness();
  65.                     }
  66.                 }
  67.             }
  68.  
  69.             generateSunlight();
  70.             setFresh(false);
  71.  
  72.             return true;
  73.         }
  74.         return false;
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement