Advertisement
Guest User

generatemap.js

a guest
Mar 15th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getRandBool(){
  2.     //Returns a random boolean value
  3.     randomnumber=Math.floor(Math.random()*2) //Either Zero or One
  4.     return randomnumber
  5. }
  6.  
  7. function randomize_map(map){
  8.     //Fills a map with boolean noise
  9.     for (var i = 0; i < map.size; i++) {
  10.         for (var j = 0; j < map.size; j++) {
  11.             map.set_point(i,j,getRandBool())
  12.         }
  13.     }
  14. }
  15.  
  16. function remove_walls(map){
  17.     //My neat trick for creating twisty turny mazes all alike...
  18.     //Note: Not cellula automata, but similar
  19.     total = 0
  20.     for (var i = 0; i < map.size; i++) {
  21.         for (var j = 0; j < map.size; j++) {
  22.             if (map.get_point(i-1,j) == map.get_point(i+1,j) &
  23.                 map.get_point(i,j-1) == map.get_point(i,j+1) &
  24.                 map.get_point(i,j-1) == map.get_point(i-1,j)){
  25.                     map.set_point(i,j,0) //Remove walls
  26.                     total += 1
  27.             }
  28.         }
  29.     }
  30.     //console.log('Walls Removed:', total)
  31. }
  32.  
  33. function get_biggest_area(map){
  34.     //A rather messy way of finding the biggest area and flood-filling the rest. There must be a better way
  35.    
  36.     //Fill all empty spaces with incremental ideftifiers, recording the identifier and coordinates of the biggest area
  37.     biggest = [0, [0,0], 0] //(Area, [coords], num)
  38.     id = 3 //Not 0 or 1, which would be the walls or empty
  39.     for (var i = 0; i < map.size; i++) {
  40.         for (var j = 0; j < map.size; j++) {
  41.             id += 1
  42.             size = get_size(map,i,j, id)
  43.             if (size > biggest[0]){
  44.                 biggest = [size, [i,j], id]
  45.             }
  46.         }
  47.     }
  48.     //Flood-fill everything not the biggest area. Reset to being walls and empty.
  49.     for (var i = 0; i < map.size; i++) {
  50.         for (var j = 0; j < map.size; j++) {
  51.             if (map.get_point(i,j) != biggest[2]){
  52.                 map.set_point(i,j,1)
  53.             } else {
  54.                 map.set_point(i,j,0)
  55.             }
  56.         }
  57.     }
  58.     return biggest[0] //size, start_point
  59.    
  60. }
  61.  
  62. function get_size(map,x,y,i){
  63.     //Returns the size of the area starting at x,y. Note: recursive
  64.     //The variable i is an identifier and should NOT be 0 or 1
  65.     if (map.get_point(x,y) == 0){
  66.         map.set_point(x,y,i)
  67.         count = 1 + get_size(map, x+1, y, i) + get_size(map, x-1, y, i) + get_size(map, x, y+1, i) + get_size(map, x, y-1, i)
  68.         return count
  69.     } else {
  70.         return 0
  71.     }
  72. }
  73.  
  74. function newrandommap(size){
  75.     //Creates a new map with the remove_walls algorithm
  76.     map = new Map(size);
  77.    
  78.     randomize_map(map)
  79.     remove_walls(map)
  80.     area = get_biggest_area(map)
  81.     return [map, area]
  82. }
  83.  
  84. function get_tunnel_ends(map){
  85.     //Looks for structures where empty space is nearly completely surrounded
  86.     tunnel_ends = []
  87.    
  88.     for (var i = 0; i < map.size; i++) {
  89.         for (var j = 0; j < map.size; j++) {
  90.             if (map.get_point(i,j) == 0 && map.get_num_neighbours(i,j) > 6){
  91.                 tunnel_ends.push([i,j])
  92.                
  93.             }
  94.         }
  95.     }
  96.     return tunnel_ends
  97.    
  98. }
  99.  
  100. function newmap(size){
  101.     //Creates a new map of sensible area for map size (and with at least a start and end tunnel)
  102.     area = 0
  103.     while(area < (size*size*0.25) || area > (size*size*0.5) || ends.length < 2){
  104.         data = newrandommap(size)
  105.         map = data[0]
  106.         area = data[1]
  107.         ends = get_tunnel_ends(map)
  108.     }
  109.     return [map, ends]
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement