Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getRandBool(){
- //Returns a random boolean value
- randomnumber=Math.floor(Math.random()*2) //Either Zero or One
- return randomnumber
- }
- function randomize_map(map){
- //Fills a map with boolean noise
- for (var i = 0; i < map.size; i++) {
- for (var j = 0; j < map.size; j++) {
- map.set_point(i,j,getRandBool())
- }
- }
- }
- function remove_walls(map){
- //My neat trick for creating twisty turny mazes all alike...
- //Note: Not cellula automata, but similar
- total = 0
- for (var i = 0; i < map.size; i++) {
- for (var j = 0; j < map.size; j++) {
- if (map.get_point(i-1,j) == map.get_point(i+1,j) &
- map.get_point(i,j-1) == map.get_point(i,j+1) &
- map.get_point(i,j-1) == map.get_point(i-1,j)){
- map.set_point(i,j,0) //Remove walls
- total += 1
- }
- }
- }
- //console.log('Walls Removed:', total)
- }
- function get_biggest_area(map){
- //A rather messy way of finding the biggest area and flood-filling the rest. There must be a better way
- //Fill all empty spaces with incremental ideftifiers, recording the identifier and coordinates of the biggest area
- biggest = [0, [0,0], 0] //(Area, [coords], num)
- id = 3 //Not 0 or 1, which would be the walls or empty
- for (var i = 0; i < map.size; i++) {
- for (var j = 0; j < map.size; j++) {
- id += 1
- size = get_size(map,i,j, id)
- if (size > biggest[0]){
- biggest = [size, [i,j], id]
- }
- }
- }
- //Flood-fill everything not the biggest area. Reset to being walls and empty.
- for (var i = 0; i < map.size; i++) {
- for (var j = 0; j < map.size; j++) {
- if (map.get_point(i,j) != biggest[2]){
- map.set_point(i,j,1)
- } else {
- map.set_point(i,j,0)
- }
- }
- }
- return biggest[0] //size, start_point
- }
- function get_size(map,x,y,i){
- //Returns the size of the area starting at x,y. Note: recursive
- //The variable i is an identifier and should NOT be 0 or 1
- if (map.get_point(x,y) == 0){
- map.set_point(x,y,i)
- 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)
- return count
- } else {
- return 0
- }
- }
- function newrandommap(size){
- //Creates a new map with the remove_walls algorithm
- map = new Map(size);
- randomize_map(map)
- remove_walls(map)
- area = get_biggest_area(map)
- return [map, area]
- }
- function get_tunnel_ends(map){
- //Looks for structures where empty space is nearly completely surrounded
- tunnel_ends = []
- for (var i = 0; i < map.size; i++) {
- for (var j = 0; j < map.size; j++) {
- if (map.get_point(i,j) == 0 && map.get_num_neighbours(i,j) > 6){
- tunnel_ends.push([i,j])
- }
- }
- }
- return tunnel_ends
- }
- function newmap(size){
- //Creates a new map of sensible area for map size (and with at least a start and end tunnel)
- area = 0
- while(area < (size*size*0.25) || area > (size*size*0.5) || ends.length < 2){
- data = newrandommap(size)
- map = data[0]
- area = data[1]
- ends = get_tunnel_ends(map)
- }
- return [map, ends]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement