• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Optimising Help for Expansion Code

dialgpalkia

Member
Hi everyone.

I am looking for some suggestions to optimise (and preferably simplify) a code I am writing for one of my games. The idea of the game is, using a ds_grid, create 'tunnels' and 'rooms' (rooms are 2x2 sections of 'tunnels') which can be expanded as necessary. I am using the content of the ds grid to define each type of cell (so 0 for nothing, 1 for a tunnel, 2 for an empty room, 3 for a different type of room etc etc).

The thing that I want to happen and to optimise is the ability to expand each room. I currently have it so that each time a new cell is 'dug', it runs a bunch of tests to see if it can be incorporated into an existing room, or if it can create a new room.

The problem is that there are a lot of test conditions that I need the code to run through to ensure the rooms can 'legally' be expanded, such as checking corner cells and adjacent cells.
This is my current code:

This is a script that runs in the STEP event of the Tunnel controller object. It is used to create rooms and in the expansion of the rooms:
Code:
//Deconstruct the chunk for it's properties.
for(var xx = 4; xx<objControl_Tunnel.gridWidth-1; xx++){
   for(var yy = 7; yy<objControl_Tunnel.gridHeight-1; yy++){
       //Unpack the room data from adjacent cells.
       var arrCurrChunkProperties = ds_grid_get(global.gridTemp,xx,yy);
       var arrLeft = ds_grid_get(global.gridTemp,xx-1,yy);
       var arrTopLeft = ds_grid_get(global.gridTemp,xx-1,yy-1);
       var arrBottomLeft = ds_grid_get(global.gridTemp,xx-1,yy+1);
       var arrRight = ds_grid_get(global.gridTemp,xx+1,yy);
       var arrTopRight = ds_grid_get(global.gridTemp,xx+1,yy-1);
       var arrBottomRight = ds_grid_get(global.gridTemp,xx+1,yy+1);
       var arrTop = ds_grid_get(global.gridTemp,xx,yy-1);
       var arrBottom = ds_grid_get(global.gridTemp,xx,yy+1);
       
       //2x2 Room Creation
       if(arrCurrChunkProperties == 1){
           if((arrLeft==1)&&(arrBottom==1)&&(arrBottomLeft==1)) {
               ds_grid_set(global.gridTemp,xx-1,yy,2); // Left Chunk
               ds_grid_set(global.gridTemp,xx,yy+1,2); // Bottom Chunk
               ds_grid_set(global.gridTemp,xx-1,yy+1,2); // Bottom Left Chunk
               ds_grid_set(global.gridTemp,xx,yy,2); // Current Chunk
           } else if ((arrRight==1)&&(arrBottom==1)&&(arrBottomRight==1)){
               ds_grid_set(global.gridTemp,xx+1,yy,2); // Right Chunk
               ds_grid_set(global.gridTemp,xx,yy+1,2); // Bottom Chunk
               ds_grid_set(global.gridTemp,xx+1,yy+1,2); // Bottom Right Chunk
               ds_grid_set(global.gridTemp,xx,yy,2); // Current Chunk
           } else if ((arrLeft==1)&&(arrTop==1)&&(arrTopLeft==1)){
               ds_grid_set(global.gridTemp,xx-1,yy,2); // Left Chunk
               ds_grid_set(global.gridTemp,xx,yy-1,2); // Top Chunk
               ds_grid_set(global.gridTemp,xx-1,yy-1,2); // Top Left Chunk
               ds_grid_set(global.gridTemp,xx,yy,2); // Current Chunk
           } else if ((arrRight==1)&&(arrTop==1)&&(arrTopRight==1)){
               ds_grid_set(global.gridTemp,xx+1,yy,2); // Right Chunk
               ds_grid_set(global.gridTemp,xx,yy-1,2); // Top Chunk
               ds_grid_set(global.gridTemp,xx+1,yy-1,2); // Top Right Chunk
               ds_grid_set(global.gridTemp,xx,yy,2); // Current Chunk
           }
           
       //Room Expansion
           else{
               for(var i = 2; i<ds_list_size(global.roomTypes); i++){
                   if((arrLeft==1)&&(arrBottom==i)&&(arrBottomLeft==i)){
                       ds_grid_set(global.gridTemp,xx-1,yy,i); //Left Chunk
                       ds_grid_set(global.gridTemp,xx,yy,i); // Current Chunk
                   } else if((arrLeft==1)&&(arrTop==i)&&(arrTopLeft==i)){
                       ds_grid_set(global.gridTemp,xx-1,yy,i); //Left Chunk
                       ds_grid_set(global.gridTemp,xx,yy,i); // Current Chunk
                   } else if((arrRight==1)&&(arrBottom==i)&&(arrBottomRight==i)){
                       ds_grid_set(global.gridTemp,xx+1,yy,i); //Right Chunk
                       ds_grid_set(global.gridTemp,xx,yy,i); // Current Chunk
                   } else if((arrRight==1)&&(arrTop==i)&&(arrTopRight==i)){
                       ds_grid_set(global.gridTemp,xx+1,yy,i); // Right Chunk
                       ds_grid_set(global.gridTemp,xx,yy,i); // Current Chunk
                   } else if((arrBottom==1)&&(arrLeft==i)&&(arrBottomLeft==i)){
                       ds_grid_set(global.gridTemp,xx,yy+1,i); // Bottom Chunk
                       ds_grid_set(global.gridTemp,xx,yy,i); // Current Chunk
                   } else if((arrBottom==1)&&(arrRight==i)&&(arrBottomRight==i)){
                       ds_grid_set(global.gridTemp,xx,yy+1,i); // Bottom Chunk
                       ds_grid_set(global.gridTemp,xx,yy,i); // Current Chunk
                   } else if((arrTop==1)&&(arrLeft==i)&&(arrTopLeft==i)){
                       ds_grid_set(global.gridTemp,xx,yy-1,i); // Top Chunk
                       ds_grid_set(global.gridTemp,xx,yy,i); // Current Chunk
                   } else if((arrTop==1)&&(arrRight==i)&&(arrTopRight==i)){
                       ds_grid_set(global.gridTemp,xx,yy-1,i); // Top Chunk
                       ds_grid_set(global.gridTemp,xx,yy,i); // Current Chunk
                   } else if((arrRight==i)&&(arrBottom==i)&&(arrCurrChunkProperties==1)){
                       ds_grid_set(global.gridTemp,xx,yy,i);
                   } else if((arrLeft==i)&&(arrBottom==i)&&(arrCurrChunkProperties==1)){
                       ds_grid_set(global.gridTemp,xx,yy,i);
                   } else if((arrRight==i)&&(arrTop==i)&&(arrCurrChunkProperties==1)){
                       ds_grid_set(global.gridTemp,xx,yy,i);
                   } else if((arrLeft==i)&&(arrTop==i)&&(arrCurrChunkProperties==1)){
                       ds_grid_set(global.gridTemp,xx,yy,i);
                   }
               }
           }
       }
   }
}

This is a code I also use to test IF a tunnel can be dug, when the mouse is pressed above as cell of the ds grid, which returns TRUE if a tunnel can be dug, and FALSE if a tunnel cannot be dug. This then loops back into the above code to create or expand rooms.

Code:
/// @function                   funcTunnel_Check_for_Autotile(x,y);
/// @param   {int}   x
/// @param   {int}   y

var xx = argument0;
var yy = argument1;

var arrCurrChunkProperties = ds_grid_get(global.gridTemp,xx,yy);
var Width = objControl_Tunnel.gridWidth-1;
var Height = objControl_Tunnel.gridHeight-1;

for(var i = 2; i<ds_list_size(global.roomTypes); i++){
   if(arrCurrChunkProperties!=i){
       //Checking for Autotiles
       //Top Left Corner
       if(xx==3 && yy==6){
           var arrRight = ds_grid_get(global.gridTemp,xx+1,yy);
           var arrBottom = ds_grid_get(global.gridTemp,xx,yy+1);
       
           if((arrRight>=1)||(arrBottom>=1)){return true}
       
       //Top Right Corner
       } else if(xx==(Width) && yy==6){
           var arrLeft = ds_grid_get(global.gridTemp,xx-1,yy);
           var arrBottom = ds_grid_get(global.gridTemp,xx,yy+1);
       
           if((arrLeft>=1)||(arrBottom>=1)){return true}
       
       //Bottom Left Corner
       } else if(xx==3 && yy==(Height)){
           var arrRight = ds_grid_get(global.gridTemp,xx+1,yy);
           var arrTop = ds_grid_get(global.gridTemp,xx,yy-1);
       
           if((arrRight>=1)||(arrTop>=1)){return true}
       
       //Bottom Right Corner
       } else if(xx=(Width) && yy==(Height)){
           var arrLeft = ds_grid_get(global.gridTemp,xx-1,yy);
           var arrTop = ds_grid_get(global.gridTemp,xx,yy-1);
       
           if((arrLeft>=1)||(arrTop>=1)){return true}
       
       //Left Side
       } else if(xx==3){
           var arrRight = ds_grid_get(global.gridTemp,xx+1,yy);
           var arrTop = ds_grid_get(global.gridTemp,xx,yy-1);
           var arrBottom = ds_grid_get(global.gridTemp,xx,yy+1);
       
           if((arrRight>=1)||(arrTop>=1)||(arrBottom>=1)){return true}
       
       //Top Side
       } else if(yy==6){
           var arrLeft = ds_grid_get(global.gridTemp,xx-1,yy);
           var arrRight = ds_grid_get(global.gridTemp,xx+1,yy);
           var arrBottom = ds_grid_get(global.gridTemp,xx,yy+1);
       
           if((arrRight>=1)||(arrLeft>=1)||(arrBottom>=1)){return true}
       
       //Right Side
       } else if(xx==(Width)){
           var arrLeft = ds_grid_get(global.gridTemp,xx-1,yy);
           var arrTop = ds_grid_get(global.gridTemp,xx,yy-1);
           var arrBottom = ds_grid_get(global.gridTemp,xx,yy+1);
       
           if((arrLeft>=1)||(arrTop>=1)||(arrBottom>=1)){return true}
       
       //Bottom Side
       } else if(yy==(Height)){
           var arrLeft = ds_grid_get(global.gridTemp,xx-1,yy);
           var arrRight = ds_grid_get(global.gridTemp,xx+1,yy);
           var arrTop = ds_grid_get(global.gridTemp,xx,yy-1);
       
           if((arrLeft>=1)||(arrTop>=1)||(arrRight>=1)){return true}
       
       }else{
           var arrLeft = ds_grid_get(global.gridTemp,xx-1,yy);
           var arrRight = ds_grid_get(global.gridTemp,xx+1,yy);
           var arrTop = ds_grid_get(global.gridTemp,xx,yy-1);
           var arrBottom = ds_grid_get(global.gridTemp,xx,yy+1);

           if((arrBottom==1)&&(arrLeft==i)&&(arrRight!=i)){return false} //Merge Rooms
           else if((arrTop==1)&&(arrLeft==i)&&(arrRight!=i)){return false} //Merge Rooms
           else if((arrRight==1)&&(arrTop==i)&&(arrBottom!=i)){return false} //Merge Rooms
           else if((arrLeft==1)&&(arrTop==i)&&(arrBottom!=i)){return false} //Merge Rooms
           
           else if((arrBottom==1)&&(arrLeft==i)&&(arrRight==i)){return true} //Merge Rooms
           else if((arrTop==1)&&(arrLeft==i)&&(arrRight==i)){return true} //Merge Rooms
           else if((arrRight==1)&&(arrTop==i)&&(arrBottom==i)){return true} //Merge Rooms
           else if((arrLeft==1)&&(arrTop==i)&&(arrBottom==i)){return true} //Merge Rooms
           else if((arrLeft>=1)||(arrRight>=1)||(arrTop>=1)||(arrBottom>=1)){return true}
       }
   } else {return false}
}

This code in particular I am looking to find a way to optimise and simplify. I do not want to have to add new if else statements for every possible permutation and commutation of possible cell contents. I have considered a switch statement but each statement is so unique I haven't even bothered to try it.

Does anyone know of a good way to do this, and can suggest some options to me, or know of something that accomplishes something similar that I can model off of?

Thanks guys!
 
Top