Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.jnk.blockles.levelgen;
- import java.util.Random;
- import net.jnk.blockles.Level;
- import net.jnk.blockles.Tile;
- public class lvl_caves_gen {
- private static int blockX;
- private static int blockY;
- //Cave Generation Method
- public static void doGenerate(){
- //Defining the block positions
- for(int y=0;y<Level.block[0].length;y++){
- for(int x=0;x<Level.block.length;x++){
- if(y > Level.worldH / 8) {
- //Try catch to prevent ArrayIndexOutOfBounds
- try{
- //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.
- if(new Random().nextInt(10000) < 5){
- //Initializing Generation Offset Variables
- blockX = x;
- blockY = y;
- //Debug Output Message
- //System.out.println("Cave generated at: " + x + ", " + y + "!");
- //For loop cycles through the creation of each indiviual block making up a cave.
- //Change the 175 to any valid integer to change cave size.
- for(int i = 0; i < 175; i++){
- //Generating new Offsets for X and Y
- blockX = getXOffset(new Random().nextInt(60), blockX);
- blockY = getYOffset(new Random().nextInt(60), blockY);
- //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.
- if(Level.block[blockX][blockY].id != Tile.bedrock && Level.block[blockX][blockY].id != Tile.solid_air) Level.block[blockX][blockY].id = Tile.air;
- if(Level.block[blockX][blockY].id != Tile.bedrock && Level.block[blockX][blockY].id != Tile.solid_air) Level.block[blockX + 1][blockY].id = Tile.air;
- if(Level.block[blockX][blockY].id != Tile.bedrock && Level.block[blockX][blockY].id != Tile.solid_air) Level.block[blockX][blockY + 1].id = Tile.air;
- }
- }
- } catch(Exception e) { }
- }
- }
- }
- }
- //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)
- private static int getXOffset(int RandomInt, int currentX){
- if (RandomInt > 34){
- currentX++;
- } else if (RandomInt <= 34 && RandomInt > 24){
- return currentX;
- } else if (RandomInt <= 24){
- currentX--;
- }
- return currentX;
- }
- //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)
- private static int getYOffset(int RandomInt, int currentY){
- if (RandomInt > 34){
- currentY++;
- } else if (RandomInt <= 34 && RandomInt > 24){
- return currentY;
- } else if (RandomInt <= 24){
- currentY--;
- }
- return currentY;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement