Advertisement
Guest User

stubz - level generation

a guest
Aug 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Counter = 0 ;
  2.  
  3. //width height div cell
  4. gWidth = room_width div CELL;
  5. gHeight = room_height div CELL;
  6.  
  7. //create grid... be mindful of edge
  8. PlacesGrid = ds_grid_create(gWidth, gHeight); //not so sure about the edges
  9.  
  10. //set entire grid region as void
  11. ds_grid_set_region(PlacesGrid, 0, 0, gWidth -1 , gHeight  , FLOOR);
  12.  
  13. //distribute types according to odds
  14. for(iX = 0; iX < gWidth; iX ++) {
  15.     for(iY = 0; iY < gHeight; iY ++){
  16.         var TileOdds = irandom(99);
  17.         if TileOdds <= 33 {
  18.         PlacesGrid[# iX,iY] = WALL;
  19.         }
  20.     }
  21. }
  22.  
  23. //add the boarder
  24. for(iX = 0; iX < gWidth; iX ++) {
  25.     for(iY = 0; iY < gHeight; iY ++){
  26.     PlacesGrid[# iX, 0] = WALL;
  27.     PlacesGrid[# 0, iY] = WALL;
  28.     PlacesGrid[# iX, gHeight -1] = WALL;
  29.     PlacesGrid[# gWidth -1, iY] = WALL;
  30.     }
  31. }
  32.  
  33. //"cellular automation" I guess
  34. repeat(2){
  35.     for(iX = 1; iX < gWidth -1; iX ++) {
  36.         for(iY = 1; iY < gHeight -1; iY ++){
  37.              var TileCount = 0;
  38.             if PlacesGrid[# iX, iY] = FLOOR {
  39.                 TileCount +=1;
  40.                 if (PlacesGrid[# iX +1, iY] == FLOOR) TileCount +=1;
  41.                 if (PlacesGrid[# iX +1, iY -1] == FLOOR) TileCount +=1;
  42.                 if (PlacesGrid[# iX, iY -1] == FLOOR) TileCount +=1;
  43.                 if (PlacesGrid[# iX -1, iY -1] == FLOOR) TileCount +=1;
  44.                 if (PlacesGrid[# iX -1, iY] == FLOOR) TileCount +=1;
  45.                 if (PlacesGrid[# iX -1, iY +1] == FLOOR) TileCount +=1;
  46.                 if (PlacesGrid[# iX, iY +1] == FLOOR) TileCount +=1;
  47.                 if (PlacesGrid[# iX +1, iY +1] == FLOOR) TileCount +=1;
  48.                 if (TileCount <= 4) PlacesGrid[# iX,iY] = WALL;
  49.             }
  50.             if PlacesGrid[# iX, iY] = WALL{
  51.                 TileCount +=1;
  52.                 if (PlacesGrid[# iX +1, iY] == WALL) TileCount +=1;
  53.                 if (PlacesGrid[# iX +1, iY -1] == WALL) TileCount +=1;
  54.                 if (PlacesGrid[# iX, iY -1] == WALL) TileCount +=1;
  55.                 if (PlacesGrid[# iX -1, iY -1] == WALL) TileCount +=1;
  56.                 if (PlacesGrid[# iX -1, iY] == WALL) TileCount +=1;
  57.                 if (PlacesGrid[# iX -1, iY +1] == WALL) TileCount +=1;
  58.                 if (PlacesGrid[# iX, iY +1] == WALL) TileCount +=1;
  59.                 if (PlacesGrid[# iX +1, iY +1] == WALL) TileCount +=1;
  60.                 if (TileCount < 4) PlacesGrid[# iX,iY] = FLOOR;
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. //Get rid of alone floor tiles
  67. for(iX = 0; iX < gWidth -1; iX++){
  68.     for(iY = 0; iY < gHeight -1; iY++){
  69.         if PlacesGrid[#iX, iY] == FLOOR {
  70.             if PlacesGrid[# iX + 1, iY] == WALL and
  71.                PlacesGrid[# iX, iY -1] == WALL and
  72.                PlacesGrid[# iX -1 , iY] == WALL and
  73.                PlacesGrid[# iX, iY + 1] == WALL {
  74.                     PlacesGrid[# iX, iY] = WALL
  75.                }
  76.            
  77.         }
  78.     }
  79. }
  80.  
  81. //Make sure there's no island FLOOR tiles or isolated clusters of them
  82.  
  83. //add the boarder again
  84. for(iX = 0; iX < gWidth; iX ++) {
  85.     for(iY = 0; iY < gHeight; iY ++){
  86.     PlacesGrid[# iX, 0] = WALL;
  87.     PlacesGrid[# 0, iY] = WALL;
  88.     PlacesGrid[# iX, gHeight -1] = WALL;
  89.     PlacesGrid[# gWidth -1, iY] = WALL;
  90.     }
  91. }
  92.  
  93. //Choose a starting CELL towards the centre that is FLOOR
  94. XRand = floor(irandom_range(gWidth /2 -10, gWidth /2 + 10 ));
  95. YRand = floor(irandom_range(gHeight /2 -10, gHeight /2 +10));
  96. StartCell = PlacesGrid[# XRand, YRand];
  97.  
  98. while StartCell != FLOOR {
  99. XRand = irandom_range(10, gWidth -10 );
  100. YRand = irandom_range(10, gHeight -10);
  101. StartCell = PlacesGrid[# XRand, YRand];
  102. }
  103.  
  104. //add objCell to FLOOR tiles
  105. for (var iX = 0; iX < gWidth; iX++){
  106. for (var iY = 0; iY < gHeight; iY++) {
  107.     if PlacesGrid[# iX, iY] == WALL {
  108.         NewCell = instance_create_depth(iX * CELL, iY * CELL, 0, objCell32);
  109.         NewCell.iX = iX;
  110.         NewCell.iY = iY;
  111.         NewCell.gWidth = gWidth;
  112.         NewCell.gHeight = gHeight;
  113.         }//if PlacesGrid[# iX, iY] == FLOOR {
  114.     }
  115. }
  116.  
  117. //Landlocked = path_add();
  118. LandLockedGrid = mp_grid_create(0,0,gWidth,gHeight,CELL,CELL);
  119. mp_grid_add_instances(LandLockedGrid, objCell32,0);
  120.  
  121. //try to trace a path back from each floor tile
  122. for(iX = 0; iX < gWidth; iX++) {
  123.     for(iY = 0; iY < gHeight; iY++){
  124.         if PlacesGrid[# iX, iY] == FLOOR {
  125.             var Landlocked = path_add();
  126.             if mp_grid_path(LandLockedGrid, Landlocked, iX * CELL + (CELL /2), iY * CELL + (CELL /2), XRand * CELL + (CELL /2), YRand * CELL + (CELL /2),false) == false {
  127.             PlacesGrid[# iX, iY] = WALL;
  128.             path_delete(Landlocked);
  129.             }
  130.            
  131.         }
  132.     }
  133. }
  134.  
  135. with objCell32 {
  136. instance_destroy();
  137. }
  138.  
  139. //create the move grid and add the wall instances...
  140. //... and THEN destroy the wall objects
  141. MoveGrid = mp_grid_create(CELL, CELL, gWidth -2, gHeight -2, 32,32);
  142.  
  143. for (iX = 0; iX < gWidth; iX++){
  144.     for(iY = 0; iY < gHeight; iY++){
  145.         if PlacesGrid[# iX, iY] == WALL {
  146.             instance_create_depth(iX * CELL,iY * CELL,0,objWall32);
  147.         }
  148.     }
  149. }
  150.  
  151. mp_grid_add_instances(MoveGrid, objWall32, false);
  152.  
  153. with objWall32 {
  154.     instance_destroy();
  155. }
  156.  
  157. //doing corner things...
  158. //... so we won't be taking note of EVERY corner in the room, just isolated ones
  159. for (iX = 0; iX < gWidth; iX++){
  160.     for (iY = 0; iY < gHeight; iY++) {
  161.        
  162.         if PlacesGrid[# iX, iY] == FLOOR {
  163.        
  164.         var GridCount = instance_create_depth(iX * CELL, iY * CELL, global.PD +11, objCell32);
  165.             with GridCount {
  166.                 if Places.PlacesGrid[# MyIX +1, MyIY +1] == WALL {
  167.                     Points +=1;
  168.                 }
  169.                 if Places.PlacesGrid[# MyIX +1, MyIY] == WALL {
  170.                     Points +=2;
  171.                 }
  172.                 if Places.PlacesGrid[# MyIX +1, MyIY -1] == WALL {
  173.                     Points +=1;
  174.                 }
  175.                 if Places.PlacesGrid[# MyIX, MyIY -1] == WALL {
  176.                     Points +=2;
  177.                 }
  178.                 if Places.PlacesGrid[# MyIX -1, MyIY -1] == WALL {
  179.                     Points +=1;
  180.                 }
  181.                 if Places.PlacesGrid[# MyIX -1, MyIY] == WALL {
  182.                     Points +=2;
  183.                 }
  184.                 if Places.PlacesGrid[# MyIX -1, MyIY +1] == WALL {
  185.                     Points +=1;
  186.                 }
  187.                 if Places.PlacesGrid[# MyIX, MyIY +1] == WALL {
  188.                     Points +=2;
  189.                 }
  190.                 if (Points != 1) instance_destroy();   
  191.                
  192.             }
  193.         }
  194.     }
  195. }
  196.  
  197. //get rid of clusters of diagonals
  198. with objCell32 {
  199.     if place_meeting(x + CELL, y + CELL, objCell32) and
  200.         place_meeting(x - CELL, y - CELL, objCell32) {
  201.             instance_destroy();
  202.     }
  203.     if place_meeting(x - CELL, y + CELL, objCell32) and
  204.         place_meeting(x + CELL, y - CELL, objCell32) {
  205.             instance_destroy();
  206.         }
  207. }
  208.  
  209. var Height = instance_number(objCell32);
  210. //CornerIdGrid... 0=id,1=x,2=y,3=distance to center
  211. CornerIdGrid = ds_grid_create(4,Height);
  212.  
  213. with objCell32 {
  214. ds_grid_add(Places.CornerIdGrid,0,Places.Counter,id);
  215. ds_grid_add(Places.CornerIdGrid,1,Places.Counter,x);
  216. ds_grid_add(Places.CornerIdGrid,2,Places.Counter,y);
  217. var Center = point_distance(x,y,room_width/2,room_height/2);
  218. ds_grid_add(Places.CornerIdGrid,3,Places.Counter,Center);
  219. Places.Counter +=1;
  220. }
  221.  
  222. with objCell32 {
  223.     instance_destroy();
  224. }
  225.  
  226. ds_grid_sort(CornerIdGrid,3,true);
  227.  
  228.  
  229. //this is the actual BASIC spawner so far
  230. for(iN = 0; iN < 30; iN++){
  231. var Inst = ds_grid_get(CornerIdGrid,0,iN);
  232. var InstX = ds_grid_get(CornerIdGrid,1,iN);
  233. var InstY = ds_grid_get(CornerIdGrid,2,iN);
  234. instance_create_depth(InstX,InstY,global.PD +10,objTowerMount);
  235. //instance_create_depth(Inst.x,Inst.y,global.PD +10,objTowerMount);
  236. }
  237.  
  238. //add the tiles LAST STEP
  239. for(iX = 0; iX < gWidth; iX ++) {
  240.     for(iY = 0; iY < gHeight; iY ++){
  241.         if PlacesGrid[# iX, iY] == WALL {
  242.         //instance_create_depth(iX * CELL,iY * CELL,0,objWall)
  243.         tile_add(sprWall32,0,0,CELL,CELL,iX *CELL, iY * CELL,global.PD + 15)
  244.         }
  245.         if PlacesGrid[# iX, iY] == FLOOR {
  246.        
  247.         tile_add(bgBrown32,choose(0,CELL,CELL * 2,CELL * 3) ,choose(0,CELL,CELL * 2,CELL * 3),DUnit,DUnit,iX * CELL,iY * CELL, global.PD + 15)
  248.        
  249.         }
  250.     }
  251. }
  252.  
  253. //now call "lights!"
  254. instance_create_depth(0,0, -1, Lights);
  255.  
  256. //now the object to spawn the player
  257. instance_create_depth(room_width /2, room_height /2, -90, objPlacing);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement