GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2018-07-08 02:36:18

lostdarkwolf
Member
Registered: 2015-11-19
Posts: 16

generate a random data-based dungeon for a ds_grid

This one took a lot of effort, but definitely worth it! There is also five required scripts below. (A bug has been fixed (ds_grid value 16 was inadvertently skipped), If you got an older version of the script, note that room values have been subtracted by one.)

/// generate_dungeon(ds_grid, rooms*, dead_ends*, room_width_min*, room_width_max*, room_height_min*, room_height_max*, doors_min, doors_max, tries*)
// 
// Turns a ds_grid into a data-based dungeon. (See data values below.)
// Each cell only contains information about which neighbors the specified cell links to.
// 
// ds_grid           The ds_grid to turn into a data-based dungeon. real
// rooms             The ammount of rooms to TRY to make in the dungeon. real
// dead_ends         The ammount of dead ends to keep. real
// room_width_min    Minumum room width. real
// room_width_max    Maximum room width. real
// room_height_min   Minumum room height. real
// room_height_max   Maximum room height. real
// doors_min         Minumum ammount of doors per room. real
// doors_max         Maximum ammount of doors per room. real
// tries             The ammount of tries for room and door generation. real
// 
// A dead end links to nothing, so it has its own maze value.
// There is one dead end with foreward-facing links end created by the maze generation process.
// The foreward-facing dead end is the first dead end up for deletion, although it is procssed last for technical reasons.
//
/// GMLscripts.com/license

/***************************\
|  DS_GRID MAZE VALUES:     |
|-1  = dead end             |
| 0  = full solid cell      |
| 1  = open right           |
| 2  = open up              |
| 3  = open left            |
| 4  = open down            |
| 5  = open up & down       |
| 6  = open left & right    |
| 7  = open right & up      |
| 8  = open right & down    |
| 9  = open left & down     |
| 10 = open left & up       |
| 11 = closed right         | 
| 12 = closed up            |
| 13 = closed left          |
| 14 = closed down          |
| 15 = full open            |
| ROOM PIECES:  - - - - - - |
| 16 = room full open       |
| 17 = room edge E          |
| 18 = room edge N          |
| 19 = room edge W          |
| 20 = room edge S          |
| 21 = room corner NE       |
| 22 = room corner NW       |
| 23 = room corner SW       |
| 24 = room corner SE       |
| 25 = room door E          |
| 26 = room door N          |
| 27 = room door W          |
| 28 = room door S          |
\***************************/

DUV_grid=argument[0];
DUV_rooms=3;
if argument_count>2 DUV_rooms=argument[1];
DUV_dead_ends=0;
if argument_count>2 DUV_dead_ends=argument[2];
DUV_room_width_min=3;
if argument_count>3 DUV_room_width_min=max((floor(argument[3]/2)*2)+1,3);
DUV_room_width_max=4;
if argument_count>4 DUV_room_width_max=max((floor(argument[4]/2)*2)+1,3);
DUV_room_height_min=3;
if argument_count>5 DUV_room_height_min=max((floor(argument[5]/2)*2)+1,3);
DUV_room_height_max=4;
if argument_count>6 DUV_room_hight_max=max((floor(argument[6]/2)*2)+1,3);
DUV_doors_min=1;
if argument_count>7 DUV_doors_min=max(argument[7],1);
DUV_doors_max=3;
if argument_count>8 DUV_doors_max=max(argument[8],1);
DUV_tries=200;
if argument_count>9 DUV_tries=argument[9];

var var_height; // width of the entire dungeon grid minus one.
var var_width; // height of the entire dungeon grid minus one.
var var_current_dead_end; // used to index dead ends when finding dead ends
var var_delete_list; // ds_list used to randomize the delete order of dead ends
var ii; // used to populate the delete_list
var var_current_cell; // used in door generation and dead end deletion for tracking the current cell being worked on
var var_done; // used to trigger the end of certain loops throughout the entire script
var var_rooms_made; // used to track the current room in door generation and room generation
var var_tries_used; // used to track the ammount of failed attempts for door generation and room generation
var var_x, var_y; // used to track the current working position throughout the entire script
var var_history_stack_x; // used to track cell history in maze generation
var var_history_stack_y; // used to track cell history in maze generation
var var_east_check, var_north_check, var_west_check, var_south_check; // used to check surrounding cells in maze generation
var var_cell_value; // used to track the current cell value for maze generation
var var_found_cell; // used to determine when a new cell is found in maze generation
var var_start_x, var_start_y; // used to track the starting position of maze generation because it is the begining of a dead end unlike all other dead ends
var var_room_door_count; // used to track the ammount of doors that should be made for any one room
var var_door_side; // used to track which side of the room a door should go on
var var_next_door_turn_dir; // used to track the direction to use when generating the next door

var_width=ds_grid_width(DUV_grid)-1;
var_height=ds_grid_height(DUV_grid)-1;

ds_grid_set_region(DUV_grid,0,0,var_width,var_height,0); // maze values

// generate rooms
var_done=0;
var_rooms_made=0;
var_tries_used=0;

while var_done=0 {
 var_room_width[var_rooms_made]=max((floor(irandom_range(DUV_room_width_min,DUV_room_width_max)/2)*2)+1,3);
 var_room_height[var_rooms_made]=max((floor(irandom_range(DUV_room_height_min,DUV_room_height_max)/2)*2)+1,3);
 var_room_x[var_rooms_made]=(floor(irandom_range(1,var_width-var_room_width[var_rooms_made]-1)/2)*2)+1;
 var_room_y[var_rooms_made]=(floor(irandom_range(1,var_width-var_room_height[var_rooms_made]-1)/2)*2)+1;
 if ds_grid_get_sum(DUV_grid,var_room_x[var_rooms_made],var_room_y[var_rooms_made],var_room_x[var_rooms_made]+var_room_width[var_rooms_made],var_room_y[var_rooms_made]+var_room_height[var_rooms_made])=0 {
  // room full open
  var_x=var_room_x[var_rooms_made]+1;
  var_y=var_room_y[var_rooms_made]+1;
  repeat var_room_height[var_rooms_made]-2 {
   repeat var_room_width[var_rooms_made]-2 {
    ds_grid_set(DUV_grid,var_x,var_y,16)
    var_x+=1;
   }
   var_x=var_room_x[var_rooms_made]+1;
   var_y+=1;
  }
  // room edges
  var_x=var_room_x[var_rooms_made]+var_room_width[var_rooms_made]-1;
  var_y=var_room_y[var_rooms_made]+1;
  repeat var_room_height[var_rooms_made]-2 {
   ds_grid_set(DUV_grid,var_x,var_y,17)
   var_y+=1;
  }
  var_x=var_room_x[var_rooms_made]+1;
  var_y=var_room_y[var_rooms_made];
  repeat var_room_width[var_rooms_made]-2 {
   ds_grid_set(DUV_grid,var_x,var_y,18)
   var_x+=1;
  }
  var_x=var_room_x[var_rooms_made];
  var_y=var_room_y[var_rooms_made]+1;
  repeat var_room_height[var_rooms_made]-2 {
   ds_grid_set(DUV_grid,var_x,var_y,19)
   var_y+=1;
  }
  var_x=var_room_x[var_rooms_made]+1;
  var_y=var_room_y[var_rooms_made]+var_room_height[var_rooms_made]-1;
  repeat var_room_width[var_rooms_made]-2 {
   ds_grid_set(DUV_grid,var_x,var_y,20)
   var_x+=1;
  }
  
  // room corners
  ds_grid_set(DUV_grid,var_room_x[var_rooms_made]+var_room_width[var_rooms_made]-1,var_room_y[var_rooms_made],21)
  ds_grid_set(DUV_grid,var_room_x[var_rooms_made],var_room_y[var_rooms_made],22)
  ds_grid_set(DUV_grid,var_room_x[var_rooms_made],var_room_y[var_rooms_made]+var_room_height[var_rooms_made]-1,23)
  ds_grid_set(DUV_grid,var_room_x[var_rooms_made]+var_room_width[var_rooms_made]-1,var_room_y[var_rooms_made]+var_room_height[var_rooms_made]-1,24)
  
  var_rooms_made+=1;
  var_tries_used=0;
  if var_rooms_made=DUV_rooms var_done=1
 }
 else {
  var_tries_used+=1;
  if var_tries_used>=DUV_tries {
   var_done=1;
   DUV_rooms=var_rooms_made;
  }
 }
}

//generate maze
var_history_stack_x=ds_stack_create(); // history
var_history_stack_y=ds_stack_create(); // history

switch irandom(3) {
 case 0:
  MAV_x=var_width;
  MAV_y=irandom(var_height-2)+1;
 break;
 case 1:
  MAV_x=irandom(var_width-2)+1;
  MAV_y=0;
 break;
 case 2:
  MAV_x=0;
  MAV_y=irandom(var_height-2)+1;
 break;
 case 3:
  MAV_x=irandom(var_width-2)+1;
  MAV_y=var_height;
 break;
}
var_start_x=MAV_x;
var_start_y=MAV_y;
var_cell_value=ds_grid_get(DUV_grid,MAV_x,MAV_y);

while ds_grid_value_exists(DUV_grid,0,0,var_width,var_height,0) {
 var_found_cell=0;
 
 // check if the current cell can link to each neighboring cell
 if MAV_x<var_width var_east_check=ds_grid_get(DUV_grid,MAV_x+1,MAV_y)=0;
 else var_east_check=0;
 if MAV_y>0 var_north_check=ds_grid_get(DUV_grid,MAV_x,MAV_y-1)=0; 
 else var_north_check=0;
 if MAV_x>0 var_west_check=ds_grid_get(DUV_grid,MAV_x-1,MAV_y)=0;
 else var_west_check=0;
 if MAV_y<var_height var_south_check=ds_grid_get(DUV_grid,MAV_x,MAV_y+1)=0;
 else var_south_check=0;
 
 while var_found_cell=0 { // do not stop looping until a new cell is found
  var_cell_value=ds_grid_get(DUV_grid,MAV_x,MAV_y);
  
  if var_east_check+var_north_check+var_west_check+var_south_check>0 { // if ANY direction is availible
   chosen_dir=irandom(3); // make the direction choice random
   repeat 4 { // choose a direction that works
    var_done=0;
    if chosen_dir=0 and not var_east_check and not var_done {
     chosen_dir+=1;
     var_done=1;
    }
    if chosen_dir=1 and not var_north_check and not var_done {
     chosen_dir+=1;
     var_done=1;
    }
    if chosen_dir=2 and not var_west_check and not var_done {
     chosen_dir+=1;
     var_done=1;
    }
    if chosen_dir=3 and not var_south_check and not var_done {
     chosen_dir-=3;
     var_done=1;
    }
   }
 
   if chosen_dir=0 { // move current cell and write link choice to the maze grid and to the history stack.
    var_cell_value=maze_cell_generate(1,maze_cell_link_n(var_cell_value),maze_cell_link_w(var_cell_value),maze_cell_link_s(var_cell_value));
    ds_grid_set(DUV_grid,MAV_x,MAV_y,var_cell_value);
    ds_stack_push(var_history_stack_x,MAV_x);
    ds_stack_push(var_history_stack_y,MAV_y);
    MAV_x+=1;
    var_found_cell=1;
   }
   if chosen_dir=1 {
    var_cell_value=maze_cell_generate(maze_cell_link_e(var_cell_value),1,maze_cell_link_w(var_cell_value),maze_cell_link_s(var_cell_value));
    ds_grid_set(DUV_grid,MAV_x,MAV_y,var_cell_value);
    ds_stack_push(var_history_stack_x,MAV_x);
    ds_stack_push(var_history_stack_y,MAV_y);
    MAV_y-=1;
    var_found_cell=1;
   }
   if chosen_dir=2 {
    var_cell_value=maze_cell_generate(maze_cell_link_e(var_cell_value),maze_cell_link_n(var_cell_value),1,maze_cell_link_s(var_cell_value));
    ds_grid_set(DUV_grid,MAV_x,MAV_y,var_cell_value);
    ds_stack_push(var_history_stack_x,MAV_x);
    ds_stack_push(var_history_stack_y,MAV_y);
    MAV_x-=1;
    var_found_cell=1;
   }
   if chosen_dir=3 {
    var_cell_value=maze_cell_generate(maze_cell_link_e(var_cell_value),maze_cell_link_n(var_cell_value),maze_cell_link_w(var_cell_value),1);
    ds_grid_set(DUV_grid,MAV_x,MAV_y,var_cell_value);
    ds_stack_push(var_history_stack_x,MAV_x);
    ds_stack_push(var_history_stack_y,MAV_y);
    MAV_y+=1;
    var_found_cell=1;
   }
  }
  else { // back tracking
   ds_grid_set(DUV_grid,MAV_x,MAV_y,-1); // set dead end (or else an infinite loop will occor)
   while var_east_check+var_north_check+var_west_check+var_south_check=0 and ds_stack_size(var_history_stack_x)>0 {
    MAV_x=ds_stack_pop(var_history_stack_x);
    MAV_y=ds_stack_pop(var_history_stack_y);
    
    if MAV_x<var_width var_east_check=ds_grid_get(DUV_grid,MAV_x+1,MAV_y)=0;
    else var_east_check=0;
    if MAV_y>0 var_north_check=ds_grid_get(DUV_grid,MAV_x,MAV_y-1)=0; 
    else var_north_check=0;
    if MAV_x>0 var_west_check=ds_grid_get(DUV_grid,MAV_x-1,MAV_y)=0;
    else var_west_check=0;
    if MAV_y<var_height var_south_check=ds_grid_get(DUV_grid,MAV_x,MAV_y+1)=0;
    else var_south_check=0;
   }
  }
  if ds_stack_size(var_history_stack_x)=0 var_found_cell=1;
 }
}
ds_stack_destroy(var_history_stack_x);
ds_stack_destroy(var_history_stack_y);

// generate doors
var_done=0;
var_tries_used=0;
var_rooms_made=0;
var_doors_made=0;

repeat DUV_rooms {
 var_door_side=irandom(3);
 var_next_door_turn_dir=irandom(1);
 var_tries_used=0;
 while var_done=0 {
  if var_doors_made=0 var_room_door_count=irandom_range(DUV_doors_min,DUV_doors_max);
  switch var_door_side {
   case 0:
    MAV_x=var_room_x[var_rooms_made]+var_room_width[var_rooms_made]-1;
    MAV_y=var_room_y[var_rooms_made]+irandom(var_room_height[var_rooms_made]-3)+1
    if ds_grid_get(DUV_grid,MAV_x,MAV_y)=17 {
     ds_grid_set(DUV_grid,MAV_x,MAV_y,25);
     var_doors_made+=1;
     if var_next_door_turn_dir var_door_side=(var_door_side+1+irandom(1)) mod 4;
     else {
      var_door_side=var_door_side-1-irandom(1);
      while var_door_side<0 var_door_side+=4;
     }
     if var_doors_made>=var_room_door_count {
      var_doors_made=0;
      var_rooms_made+=1;
     }
     if var_rooms_made>=DUV_rooms var_done=1;
     var_current_cell=ds_grid_get(DUV_grid,MAV_x+1,MAV_y); 
     // link whatever is on the other side of the door to the door
     ds_grid_set(DUV_grid,MAV_x+1,MAV_y,maze_cell_generate(maze_cell_link_e(var_current_cell),maze_cell_link_n(var_current_cell),1,maze_cell_link_s(var_current_cell)));
    }
    else {
     var_tries_used+=1;
     if var_tries_used>=DUV_tries var_done=1;
     if var_tries_used>=DUV_tries/2 var_door_side=irandom(3);
    }
   break;
   case 1:
    MAV_x=var_room_x[var_rooms_made]+irandom(var_room_width[var_rooms_made]-3)+1
    MAV_y=var_room_y[var_rooms_made];
    if ds_grid_get(DUV_grid,MAV_x,MAV_y)=18 {
     ds_grid_set(DUV_grid,MAV_x,MAV_y,26);
     var_doors_made+=1;
     if var_next_door_turn_dir var_door_side=(var_door_side+1+irandom(1)) mod 4;
     else {
      var_door_side=var_door_side-1-irandom(1);
      while var_door_side<0 var_door_side+=4;
     }
     if var_doors_made>=var_room_door_count {
      var_doors_made=0;
      var_rooms_made+=1;
     }
     if var_rooms_made>=DUV_rooms var_done=1;
     var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y-1);
     // link whatever is on the other side of the door to the door
     ds_grid_set(DUV_grid,MAV_x,MAV_y-1,maze_cell_generate(maze_cell_link_e(var_current_cell),maze_cell_link_n(var_current_cell),maze_cell_link_w(var_current_cell),1));
    }
    else {
     var_tries_used+=1;
     if var_tries_used>=DUV_tries var_done=1;
     if var_tries_used>=DUV_tries/2 var_door_side=irandom(3);
    }
   break;
   case 2:
    MAV_x=var_room_x[var_rooms_made];
    MAV_y=var_room_y[var_rooms_made]+irandom(var_room_height[var_rooms_made]-3)+1;
    if ds_grid_get(DUV_grid,MAV_x,MAV_y)=19 {
     ds_grid_set(DUV_grid,MAV_x,MAV_y,27);
     var_doors_made+=1;
     if var_next_door_turn_dir var_door_side=(var_door_side+1+irandom(1)) mod 4;
     else {
      var_door_side=var_door_side-1-irandom(1);
      while var_door_side<0 var_door_side+=4;
     }
     if var_doors_made>=var_room_door_count {
      var_doors_made=0;
      var_rooms_made+=1;
     }
     if var_rooms_made>=DUV_rooms var_done=1;
     var_current_cell=ds_grid_get(DUV_grid,MAV_x-1,MAV_y);
     // link whatever is on the other side of the door to the door
     ds_grid_set(DUV_grid,MAV_x-1,MAV_y,maze_cell_generate(1,maze_cell_link_n(var_current_cell),maze_cell_link_w(var_current_cell),maze_cell_link_s(var_current_cell)));
    }
    else {
     var_tries_used+=1;
     if var_tries_used>=DUV_tries var_done=1;
     if var_tries_used>=DUV_tries/2 var_door_side=irandom(3);
    }
   break;
   case 3:
    MAV_x=var_room_x[var_rooms_made]+irandom(var_room_width[var_rooms_made]-3)+1
    MAV_y=var_room_y[var_rooms_made]+var_room_height[var_rooms_made]-1;
    if ds_grid_get(DUV_grid,MAV_x,MAV_y)=20 {
     ds_grid_set(DUV_grid,MAV_x,MAV_y,28);
     var_doors_made+=1;
     if var_next_door_turn_dir var_door_side=(var_door_side+1+irandom(1)) mod 4;
     else {
      var_door_side=var_door_side-1-irandom(1);
      while var_door_side<0 var_door_side+=4;
     }
     if var_doors_made>=var_room_door_count {
      var_doors_made=0;
      var_rooms_made+=1;
     }
     if var_rooms_made>=DUV_rooms var_done=1;
     var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y+1);
     // link whatever is on the other side of the door to the door
     ds_grid_set(DUV_grid,MAV_x,MAV_y+1,maze_cell_generate(maze_cell_link_e(var_current_cell),1,maze_cell_link_w(var_current_cell),maze_cell_link_s(var_current_cell)));
    }
    else {
     var_tries_used+=1;
     if var_tries_used>=DUV_tries var_done=1;
     if var_tries_used>=min(DUV_tries/2,25) var_door_side=irandom(3);
    }
   break;
  }
 }
}

// begin deletion of unwanted dead ends

// find dead ends
var_current_dead_end=0;
for (MAV_x=var_width; MAV_x>=0; MAV_x-=1;) {
 for (MAV_y=var_height; MAV_y>=0; MAV_y-=1;) {
  var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
  if var_current_cell=-1 {
   var_dead_end_x[var_current_dead_end]=MAV_x;
   var_dead_end_y[var_current_dead_end]=MAV_y;
   var_current_dead_end+=1;
  }
 }
}

// decide which dead ends to delete
var_delete_list=ds_list_create();

for (ii=var_current_dead_end-1; ii>=0; ii-=1) ds_list_add(var_delete_list,ii)
ds_list_shuffle(var_delete_list)
repeat min(DUV_dead_ends,ds_list_size(var_delete_list)) ds_list_delete(var_delete_list,0)

// delete dead ends
while ds_list_size(var_delete_list)>0 {
 MAV_x=var_dead_end_x[ds_list_find_value(var_delete_list,0)]
 MAV_y=var_dead_end_y[ds_list_find_value(var_delete_list,0)]
 ds_list_delete(var_delete_list,0);
 
 var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
 while (maze_cell_link_n(var_current_cell)+maze_cell_link_e(var_current_cell)+maze_cell_link_w(var_current_cell)+maze_cell_link_s(var_current_cell))<=1 {
  var_done=0;
  if maze_cell_link_w(ds_grid_get(DUV_grid,MAV_x+1,MAV_y)) {
   ds_grid_set(DUV_grid,MAV_x,MAV_y,0)
   MAV_x+=1;
   var_done=1;
   var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
   ds_grid_set(DUV_grid,MAV_x,MAV_y,maze_cell_generate(maze_cell_link_e(var_current_cell),maze_cell_link_n(var_current_cell),0,maze_cell_link_s(var_current_cell)))
  }
  if maze_cell_link_s(ds_grid_get(DUV_grid,MAV_x,MAV_y-1)) and var_done=0 {
   ds_grid_set(DUV_grid,MAV_x,MAV_y,0)
   MAV_y-=1;
   var_done=1;
   var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
   ds_grid_set(DUV_grid,MAV_x,MAV_y,maze_cell_generate(maze_cell_link_e(var_current_cell),maze_cell_link_n(var_current_cell),maze_cell_link_w(var_current_cell),0))
  }
  if maze_cell_link_e(ds_grid_get(DUV_grid,MAV_x-1,MAV_y)) and var_done=0 {
   ds_grid_set(DUV_grid,MAV_x,MAV_y,0)
   MAV_x-=1;
   var_done=1;
   var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
   ds_grid_set(DUV_grid,MAV_x,MAV_y,maze_cell_generate(0,maze_cell_link_n(var_current_cell),maze_cell_link_w(var_current_cell),maze_cell_link_s(var_current_cell)))
  }
  if maze_cell_link_n(ds_grid_get(DUV_grid,MAV_x,MAV_y+1)) and var_done=0 {
   ds_grid_set(DUV_grid,MAV_x,MAV_y,0)
   MAV_y+=1;
   var_done=1;
   var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
   ds_grid_set(DUV_grid,MAV_x,MAV_y,maze_cell_generate(maze_cell_link_e(var_current_cell),0,maze_cell_link_w(var_current_cell),maze_cell_link_s(var_current_cell)))
  }
 }
}

// delete the foreward-facing dead end (if desired)
if var_current_dead_end>=DUV_dead_ends {
 MAV_x=var_start_x;
 MAV_y=var_start_y;
 var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
 while (maze_cell_link_n(var_current_cell)+maze_cell_link_e(var_current_cell)+maze_cell_link_w(var_current_cell)+maze_cell_link_s(var_current_cell))<=1 {
  var_done=0;
  if maze_cell_link_e(ds_grid_get(DUV_grid,MAV_x,MAV_y)) {
   ds_grid_set(DUV_grid,MAV_x,MAV_y,0)
   MAV_x+=1;
   var_done=1;
   var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
  }
  if maze_cell_link_n(ds_grid_get(DUV_grid,MAV_x,MAV_y)) and var_done=0 {
   ds_grid_set(DUV_grid,MAV_x,MAV_y,0)
   MAV_y-=1;
   var_done=1;
   var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
  }
  if maze_cell_link_w(ds_grid_get(DUV_grid,MAV_x,MAV_y)) and var_done=0 {
   ds_grid_set(DUV_grid,MAV_x,MAV_y,0)
   MAV_x-=1;
   var_done=1;
   var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
  }
  if maze_cell_link_s(ds_grid_get(DUV_grid,MAV_x,MAV_y)) and var_done=0 {
   ds_grid_set(DUV_grid,MAV_x,MAV_y,0)
   MAV_y+=1;
   var_done=1;
   var_current_cell=ds_grid_get(DUV_grid,MAV_x,MAV_y);
  }
 }
}

ds_list_destroy(var_delete_list)
// clear array variables
var_dead_end_x=0;
var_dead_end_y=0;
var_room_x=0;
var_room_y=0;
var_room_height=0;
var_room_width=0;

This script might also help, but it is not required. This script also requires the five scripts below.

///maze_dual_link(ds_grid)
// 
// Dual links maze tiles on a ds grid so they will link forward and backward.
//
// ds_grid   The maze or ds grid to dual link.
//
/// GMLscripts.com/license

/***************************\
|  DS_GRID MAZE VALUES:     |
|-1  = dead end             |
| 0  = full solid cell      |
| 1  = open right           |
| 2  = open up              |
| 3  = open left            |
| 4  = open down            |
| 5  = open up & down       |
| 6  = open left & right    |
| 7  = open right & up      |
| 8  = open right & down    |
| 9  = open left & down     |
| 10 = open left & up       |
| 11 = closed right         | 
| 12 = closed up            |
| 13 = closed left          |
| 14 = closed down          |
| 15 = full open            |
\***************************/

MAV_grid=argument[0];
var var_width; var_width=ds_grid_width(MAV_grid)-1;
var var_height; var_height=ds_grid_height(MAV_grid)-1;
var iix, iiy, var_current_value;
for (iix=var_width; iix>=0; iix-=1;) {
 for (iiy=var_width; iiy>=0; iiy-=1;) {
  var_current_value=ds_grid_get(MAV_grid,iix,iiy)
  if var_current_value=median(var_current_value,-1,16) {
   ds_grid_set(MAV_grid,iix,iiy,maze_cell_generate(maze_cell_link_e(var_current_value) or maze_cell_link_w(ds_grid_get(MAV_grid,iix+1,iiy)),
                                                   maze_cell_link_n(var_current_value) or maze_cell_link_s(ds_grid_get(MAV_grid,iix,iiy-1)),
                                                   maze_cell_link_w(var_current_value) or maze_cell_link_e(ds_grid_get(MAV_grid,iix-1,iiy)),
                                                   maze_cell_link_s(var_current_value) or maze_cell_link_n(ds_grid_get(MAV_grid,iix,iiy+1))));
  }
 }
}

REQUIRED SCRIPTS:  (five total)

///maze_cell_generate(right,up,left,down)
// 
// returns a maze cell value given its accessible directions.
//
// right   true if the right cell can be accessed
// up      true if the upper cell can be accessed
// left    true if the left cell can be accessed
// down    true if the lower cell can be accessed
//
/// GMLscripts.com/license

/***************************\
|  DS_GRID MAZE VALUES:     |
| 0  = full solid cell      |
| 1  = open right           |
| 2  = open up              |
| 3  = open left            |
| 4  = open down            |
| 5  = open up & down       |
| 6  = open left & right    |
| 7  = open right & up      |
| 8  = open right & down    |
| 9  = open left & down     |
| 10 = open left & up       |
| 11 = closed right         | 
| 12 = closed up            |
| 13 = closed left          |
| 14 = closed down          |
| 15 = full open            |
\***************************/

//         right                up              left              down
if argument[0]=0 and argument[1]=0 and argument[2]=0 and argument[3]=0 return 0;
if argument[0]=1 and argument[1]=0 and argument[2]=0 and argument[3]=0 return 1;
if argument[0]=0 and argument[1]=1 and argument[2]=0 and argument[3]=0 return 2;
if argument[0]=0 and argument[1]=0 and argument[2]=1 and argument[3]=0 return 3;
if argument[0]=0 and argument[1]=0 and argument[2]=0 and argument[3]=1 return 4;
if argument[0]=0 and argument[1]=1 and argument[2]=0 and argument[3]=1 return 5;
if argument[0]=1 and argument[1]=0 and argument[2]=1 and argument[3]=0 return 6;
if argument[0]=1 and argument[1]=1 and argument[2]=0 and argument[3]=0 return 7;
if argument[0]=1 and argument[1]=0 and argument[2]=0 and argument[3]=1 return 8;
if argument[0]=0 and argument[1]=0 and argument[2]=1 and argument[3]=1 return 9;
if argument[0]=0 and argument[1]=1 and argument[2]=1 and argument[3]=0 return 10;
if argument[0]=0 and argument[1]=1 and argument[2]=1 and argument[3]=1 return 11;
if argument[0]=1 and argument[1]=0 and argument[2]=1 and argument[3]=1 return 12;
if argument[0]=1 and argument[1]=1 and argument[2]=0 and argument[3]=1 return 13;
if argument[0]=1 and argument[1]=1 and argument[2]=1 and argument[3]=0 return 14;
if argument[0]=1 and argument[1]=1 and argument[2]=1 and argument[3]=1 return 15;
///maze_cell_link_e(maze_value)
// 
// returns true if the defined maze value is linked to the eastern neighbor cell.
// 
// maze_value   value contained in any one maze cell. real
// 
/// GMLscripts.com/license

/***************************\
|  DS_GRID MAZE VALUES:     |
| 0  = full solid cell      |
| 1  = open right           |
| 2  = open up              |
| 3  = open left            |
| 4  = open down            |
| 5  = open up & down       |
| 6  = open left & right    |
| 7  = open right & up      |
| 8  = open right & down    |
| 9  = open left & down     |
| 10 = open left & up       |
| 11 = closed right         | 
| 12 = closed up            |
| 13 = closed left          |
| 14 = closed down          |
| 15 = full open            |
\***************************/

if argument[0]=0 return 0;
if argument[0]=1 return 1;
if argument[0]=2 return 0;
if argument[0]=3 return 0;
if argument[0]=4 return 0;
if argument[0]=5 return 0;
if argument[0]=6 return 1;
if argument[0]=7 return 1;
if argument[0]=8 return 1;
if argument[0]=9 return 0;
if argument[0]=10 return 0;
if argument[0]=11 return 0;
if argument[0]=12 return 1;
if argument[0]=13 return 1;
if argument[0]=14 return 1;
if argument[0]=15 return 1;
///maze_cell_link_n(maze_value)
// 
// returns true if the defined maze value is linked to the northern neighbor cell.
// 
// maze_value   value contained in any one maze cell. real
// 
/// GMLscripts.com/license

/***************************\
|  DS_GRID MAZE VALUES:     |
| 0  = full solid cell      |
| 1  = open right           |
| 2  = open up              |
| 3  = open left            |
| 4  = open down            |
| 5  = open up & down       |
| 6  = open left & right    |
| 7  = open right & up      |
| 8  = open right & down    |
| 9  = open left & down     |
| 10 = open left & up       |
| 11 = closed right         | 
| 12 = closed up            |
| 13 = closed left          |
| 14 = closed down          |
| 15 = full open            |
\***************************/

if argument[0]=0 return 0;
if argument[0]=1 return 0;
if argument[0]=2 return 1;
if argument[0]=3 return 0;
if argument[0]=4 return 0;
if argument[0]=5 return 1;
if argument[0]=6 return 0;
if argument[0]=7 return 1;
if argument[0]=8 return 0;
if argument[0]=9 return 0;
if argument[0]=10 return 1;
if argument[0]=11 return 1;
if argument[0]=12 return 0;
if argument[0]=13 return 1;
if argument[0]=14 return 1;
if argument[0]=15 return 1;
///maze_cell_link_w(maze_value)
// 
// returns true if the defined maze value is linked to the western neighbor cell.
// 
// maze_value   value contained in any one maze cell. real
// 
/// GMLscripts.com/license

/***************************\
|  DS_GRID MAZE VALUES:     |
| 0  = full solid cell      |
| 1  = open right           |
| 2  = open up              |
| 3  = open left            |
| 4  = open down            |
| 5  = open up & down       |
| 6  = open left & right    |
| 7  = open right & up      |
| 8  = open right & down    |
| 9  = open left & down     |
| 10 = open left & up       |
| 11 = closed right         | 
| 12 = closed up            |
| 13 = closed left          |
| 14 = closed down          |
| 15 = full open            |
\***************************/

if argument[0]=0 return 0;
if argument[0]=1 return 0;
if argument[0]=2 return 0;
if argument[0]=3 return 1;
if argument[0]=4 return 0;
if argument[0]=5 return 0;
if argument[0]=6 return 1;
if argument[0]=7 return 0;
if argument[0]=8 return 0;
if argument[0]=9 return 1;
if argument[0]=10 return 1;
if argument[0]=11 return 1;
if argument[0]=12 return 1;
if argument[0]=13 return 0;
if argument[0]=14 return 1;
if argument[0]=15 return 1;
///maze_cell_link_s(maze_value)
// 
// returns true if the defined maze value is linked to the southern neighbor cell.
// 
// maze_value   value contained in any one maze cell. real
// 
/// GMLscripts.com/license

/***************************\
|  DS_GRID MAZE VALUES:     |
| 0  = full solid cell      |
| 1  = open right           |
| 2  = open up              |
| 3  = open left            |
| 4  = open down            |
| 5  = open up & down       |
| 6  = open left & right    |
| 7  = open right & up      |
| 8  = open right & down    |
| 9  = open left & down     |
| 10 = open left & up       |
| 11 = closed right         | 
| 12 = closed up            |
| 13 = closed left          |
| 14 = closed down          |
| 15 = full open            |
\***************************/

if argument[0]=0 return 0;
if argument[0]=1 return 0;
if argument[0]=2 return 0;
if argument[0]=3 return 0;
if argument[0]=4 return 1;
if argument[0]=5 return 1;
if argument[0]=6 return 0;
if argument[0]=7 return 0;
if argument[0]=8 return 1;
if argument[0]=9 return 1;
if argument[0]=10 return 0;
if argument[0]=11 return 1;
if argument[0]=12 return 1;
if argument[0]=13 return 1;
if argument[0]=14 return 0;
if argument[0]=15 return 1;

Last edited by lostdarkwolf (2018-07-14 14:01:59)

Offline

Board footer

Powered by FluxBB