Advertisement
hadesvine

[JAVA]2d island map generation helper

Dec 23rd, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  package com.hadesvine.tiledgame.examples;
  2.  
  3. import org.newdawn.slick.geom.Polygon;
  4. import java.util.Random;
  5.  
  6. public class MapHelper {
  7.  
  8.     public static final int DEFAULTWIDTH = 32;
  9.     public static final int DEFAULTHEIGHT = 25;
  10.     public static final int CLARIFICATION = 4;
  11.  
  12.     public MapHelper() {
  13.     }
  14.  
  15.     public static int[][][] getNewMap(int width, int height, int clarification) {
  16.         Polygon[][] rand = new Polygon[width][height];
  17.         int[][][] grid;
  18.         int tempax = 0, tempay = 0;
  19.         Random r = new Random();
  20.         int x = 20;
  21.         int y = 20;
  22.         int tempx = 0;
  23.         int tempy = 0;
  24.         int checksum = 0;
  25.         int chance = 0;
  26.         grid = new int[5][width][height];
  27.         for (int z = 0; z < width; z++) {
  28.             for (int p = 0; p < height; p++) {
  29.                 rand[z][p] = new Polygon(new float[]{
  30.                             0, 0,
  31.                             0, y,
  32.                             x, y,
  33.                             x, 0
  34.                         });
  35.                 chance = r.nextInt(2);
  36.                 grid[0][z][p] = chance;
  37.                 if (chance == 1) {
  38.                     rand[z][p].setLocation(z * x, p * y);
  39.                 } else {
  40.                     rand[z][p].setLocation(900, 900);
  41.                 }
  42.                 tempx++;
  43.             }
  44.             tempy++;
  45.             tempx = 0;
  46.         }
  47.         for (int i = 0; i < clarification; i++) {
  48.             for (int mattb = 0; mattb < width; mattb++) {
  49.                 for (int mattx = 0; mattx < height; mattx++) {
  50.                     if (mattb - 1 >= 0 && mattb + 1 < width && mattx - 1 >= 0 && mattx + 1 < height) {
  51.                         checksum = grid[i][mattb - 1][mattx - 1] + grid[i][mattb - 1][mattx] + grid[i][mattb - 1][mattx + 1]
  52.                                 + grid[i][mattb][mattx - 1] + grid[i][mattb][mattx + 1] + grid[i][mattb + 1][mattx - 1]
  53.                                 + grid[i][mattb + 1][mattx] + grid[i][mattb + 1][mattx + 1];
  54.  
  55.                         if (checksum >= clarification && grid[i][mattb][mattx] == 1) {
  56.                             grid[i + 1][mattb][mattx] = 1;
  57.                         }
  58.  
  59.                         if (checksum >= 5) {
  60.                             grid[i + 1][mattb][mattx] = 1;
  61.                         }
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         for (int ax = 0; ax < width; ax++) {
  67.             for (int ay = 0; ay < height; ay++) {
  68.                 if (grid[CLARIFICATION][ax][ay] == 1) {
  69.                     rand[ax][ay].setLocation(ax * x, ay * y);
  70.                     tempax++;
  71.                 }
  72.             }
  73.             tempay++;
  74.             tempax = 0;
  75.         }
  76.         return grid ;
  77.     }
  78.    
  79.     public static void main(String[] args) {
  80.         //int [][][]testMap = getNewMap(32,24);
  81.         printRandomMapToConsole(800,600,4);
  82.     }
  83.    
  84.     public static void printRandomMapToConsole(int width, int height, int clarification){
  85.         int [][][]grid = getNewMap(width, height,clarification);
  86.                 for (int d = 0; d < height; d++) {
  87.             for (int o = 0; o < width; o++) {
  88.                 if (grid[clarification][d][o] == 1) {
  89.                     System.out.print("#");
  90.                 } else {
  91.                     System.out.print(" ");
  92.                 }
  93.             }
  94.             System.out.println();
  95.         }
  96.     }
  97.     public static void printRandomMapToConsole(int width, int height){
  98.         int [][][]grid = getNewMap(width, height,CLARIFICATION);
  99.                 for (int d = 0; d < height; d++) {
  100.             for (int o = 0; o < width; o++) {
  101.                 if (grid[CLARIFICATION][d][o] == 1) {
  102.                     System.out.print("#");
  103.                 } else {
  104.                     System.out.print(" ");
  105.                 }
  106.             }
  107.             System.out.println();
  108.         }
  109.     }
  110.    
  111.     public static int[][] getClarified2DIslandMap(int width, int height){
  112.         int [][][] gridWithClarification = getNewMap(width,height,4);
  113.         int [][] clarifiedGrid = gridWithClarification[4];
  114.         return clarifiedGrid;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement