Advertisement
Jnk1296

Cave Generation

Feb 8th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package net.jnk.blockles.levelgen;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.jnk.blockles.Level;
  6. import net.jnk.blockles.Tile;
  7.  
  8. public class lvl_caves_gen {
  9.    
  10.     private static int blockX;
  11.     private static int blockY;
  12.    
  13.     //Cave Generation Method
  14.     public static void doGenerate(){
  15.  
  16.         //Defining the block positions
  17.         for(int y=0;y<Level.block[0].length;y++){
  18.             for(int x=0;x<Level.block.length;x++){
  19.                 if(y > Level.worldH / 8) {
  20.                    
  21.                     //Try catch to prevent ArrayIndexOutOfBounds
  22.                     try{
  23.                        
  24.                         //IF statements grants a 1 in 2000 chance of spawning a cave. The reason this percentage is so low being that caves themselves will be quite large.
  25.                         if(new Random().nextInt(10000) < 5){
  26.                            
  27.                             //Initializing Generation Offset Variables
  28.                             blockX = x;
  29.                             blockY = y;
  30.                            
  31.                             //Debug Output Message
  32.                             //System.out.println("Cave generated at: " + x + ", " + y + "!");
  33.                            
  34.                             //For loop cycles through the creation of each indiviual block making up a cave.
  35.                             //Change the 175 to any valid integer to change cave size.
  36.                             for(int i = 0; i < 175; i++){
  37.                                
  38.                                 //Generating new Offsets for X and Y
  39.                                 blockX = getXOffset(new Random().nextInt(60), blockX);
  40.                                 blockY = getYOffset(new Random().nextInt(60), blockY);
  41.                                
  42.                                 //If block we are attempting to change is not one of the level border blocks, then, using the newly generated offset values, replace the block(s) defined by the offsets.
  43.                                 if(Level.block[blockX][blockY].id != Tile.bedrock && Level.block[blockX][blockY].id != Tile.solid_air) Level.block[blockX][blockY].id = Tile.air;
  44.                                 if(Level.block[blockX][blockY].id != Tile.bedrock && Level.block[blockX][blockY].id != Tile.solid_air) Level.block[blockX + 1][blockY].id = Tile.air;
  45.                                 if(Level.block[blockX][blockY].id != Tile.bedrock && Level.block[blockX][blockY].id != Tile.solid_air) Level.block[blockX][blockY + 1].id = Tile.air;
  46.                             }
  47.                         }
  48.                     } catch(Exception e) { }
  49.                 }
  50.             }
  51.         }
  52.     }
  53.    
  54.     //When given a random integer and the current X position of the block selected, then return a new X value (aka the new offset for X)
  55.     private static int getXOffset(int RandomInt, int currentX){
  56.         if (RandomInt > 34){
  57.             currentX++;
  58.         } else if (RandomInt <= 34 && RandomInt > 24){
  59.             return currentX;
  60.         } else if (RandomInt <= 24){
  61.             currentX--;
  62.         }
  63.        
  64.         return currentX;       
  65.     }
  66.    
  67.     //When given a random integer and the current Y position of the block selected, then return a new Y value (aka the new offset for Y)
  68.     private static int getYOffset(int RandomInt, int currentY){
  69.         if (RandomInt > 34){
  70.             currentY++;
  71.         } else if (RandomInt <= 34 && RandomInt > 24){
  72.             return currentY;
  73.         } else if (RandomInt <= 24){
  74.             currentY--;
  75.         }
  76.        
  77.         return currentY;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement