VacantShade
Member
Hey all!
Currently I have a system I'm using for "fog of war" or "vision" in my game. Here is what it looks like:
I have a "Vision Grid" where each cell holds a true or false value, stating whether or not it is visible to the player. This is updated only when needed, and is pretty much as optimized as I can get it.
The second stage is where I'm stumped as to how to optimize things.
I have a "Shadow Grid" where I hold which Shadow image I need to display. This is generated based on the values found in the Vision Grid of the surrounding tiles.
There are 16 Shadow images, used for the sides, corners, opposing corners, ect.
So to help visualize the system a bit, here is a display of each grid, the Vision Grid and then the Shadow Grid:
What seems to be the slowdown is calculating the Shadow Grid. Each cell that is visible checks the 8 adjacent cells. If they are not visible, it calculates which Shadow image should be shown.
Now, my math tells me there are 1000s of possible combinations, but I could be wrong on that...
So here is the code I'm currently using:
Basically I run through the Vision Grid looking for cells we can see.
- Then when I find one I jump to each of the adjacent 8 cells
- Then for each of those, if they are not visible to the player, I...
- Find which shadow image to use by checking each of the adjacent 8 cells to that one.
Any thoughts on how this, or any system like it really, could be optimized? Perhaps using a mathamatical equation to reduce or eliminate if statements? Would storing the check value in a var instead of calling the array each time be faster?
Thanks in advance for any input you have!
Currently I have a system I'm using for "fog of war" or "vision" in my game. Here is what it looks like:
I have a "Vision Grid" where each cell holds a true or false value, stating whether or not it is visible to the player. This is updated only when needed, and is pretty much as optimized as I can get it.
The second stage is where I'm stumped as to how to optimize things.
I have a "Shadow Grid" where I hold which Shadow image I need to display. This is generated based on the values found in the Vision Grid of the surrounding tiles.
There are 16 Shadow images, used for the sides, corners, opposing corners, ect.
So to help visualize the system a bit, here is a display of each grid, the Vision Grid and then the Shadow Grid:
What seems to be the slowdown is calculating the Shadow Grid. Each cell that is visible checks the 8 adjacent cells. If they are not visible, it calculates which Shadow image should be shown.
Now, my math tells me there are 1000s of possible combinations, but I could be wrong on that...
So here is the code I'm currently using:
Code:
// Cycle Through the whole Grid
for( xx = 0 ; xx < max_x ; xx += 1 )
{
for( yy = 0 ; yy < max_y ; yy += 1 )
{
// If We find a cell the player can see, enter it
if( Grid_Vision[ xx , yy ] = 1 )
{
// Set it so no shadow is shown
Grid_Shade[ xx , yy ] = 15;
// Step through the 8 cells around it
for( new_x = -1 ; new_x < 2 ; new_x += 1 )
{
for( new_y = -1 ; new_y < 2 ; new_y += 1 )
{
// Find which cell we are calculating the image for
shadow_x = xx + new_x;
shadow_y = yy + new_y;
// Lets ignore/skip cells that are outside the Grid/Room
if( shadow_x < 1 ){ shadow_x = 1 }
if( shadow_y < 1 ){ shadow_y = 1 }
if( shadow_x > ( max_x - 2 )){ shadow_x = ( max_x - 2 )}
if( shadow_y > ( max_y - 2 )){ shadow_y = ( max_y - 2 )}
// Make sure the cell we are checking for is not visible
image = Grid_Vision[ shadow_x , shadow_y ];
if( image = 0 and Grid_Shadow[ shadow_x , shadow_y ] = 0 )
{
// Step through adacent cells from the right in a counter-clockwise order
if( Grid_Vision[ shadow_x + 1 , shadow_y ] = 1 ){ image = 1 }
if( Grid_Vision[ shadow_x + 1 , shadow_y - 1 ] = 1 and image = 0 ){ image = 5 }
if( Grid_Vision[ shadow_x , shadow_y - 1 ] = 1 and image = 0 ){ image = 2 }
if( Grid_Vision[ shadow_x , shadow_y - 1 ] = 1 and image = 1 ){ image = 9 }
if( Grid_Vision[ shadow_x , shadow_y - 1 ] = 1 and image = 5 ){ image = 2 }
if( Grid_Vision[ shadow_x - 1 , shadow_y - 1 ] = 1 and image = 0 ){ image = 6 }
if( Grid_Vision[ shadow_x - 1 , shadow_y - 1 ] = 1 and image = 1 ){ image = 9 }
if( Grid_Vision[ shadow_x - 1 , shadow_y - 1 ] = 1 and image = 5 ){ image = 2 }
if( Grid_Vision[ shadow_x - 1 , shadow_y ] = 1 and image = 0 ){ image = 3 }
if( Grid_Vision[ shadow_x - 1 , shadow_y ] = 1 and image = 1 ){ image = 15 }
if( Grid_Vision[ shadow_x - 1 , shadow_y ] = 1 and image = 2 ){ image = 10 }
if( Grid_Vision[ shadow_x - 1 , shadow_y ] = 1 and image = 5 ){ image = 10 }
if( Grid_Vision[ shadow_x - 1 , shadow_y ] = 1 and image = 6 ){ image = 3 }
if( Grid_Vision[ shadow_x - 1 , shadow_y ] = 1 and image = 9 ){ image = 15 }
if( Grid_Vision[ shadow_x - 1 , shadow_y + 1 ] = 1 and image = 0 ){ image = 7 }
if( Grid_Vision[ shadow_x - 1 , shadow_y + 1 ] = 1 and image = 1 ){ image = 12 }
if( Grid_Vision[ shadow_x - 1 , shadow_y + 1 ] = 1 and image = 2 ){ image = 10 }
if( Grid_Vision[ shadow_x - 1 , shadow_y + 1 ] = 1 and image = 5 ){ image = 14 }
if( Grid_Vision[ shadow_x - 1 , shadow_y + 1 ] = 1 and image = 6 ){ image = 3 }
if( Grid_Vision[ shadow_x - 1 , shadow_y + 1 ] = 1 and image = 9 ){ image = 15 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 0 ){ image = 4 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 1 ){ image = 12 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 2 ){ image = 15 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 3 ){ image = 11 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 5 ){ image = 12 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 6 ){ image = 11 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 7 ){ image = 4 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 9 ){ image = 15 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 10 ){ image = 15 }
if( Grid_Vision[ shadow_x , shadow_y + 1 ] = 1 and image = 14 ){ image = 12 }
if( Grid_Vision[ shadow_x + 1 , shadow_y + 1 ] = 1 and image = 0 ){ image = 8 }
if( Grid_Vision[ shadow_x + 1 , shadow_y + 1 ] = 1 and image = 2 ){ image = 9 }
if( Grid_Vision[ shadow_x + 1 , shadow_y + 1 ] = 1 and image = 3 ){ image = 11 }
if( Grid_Vision[ shadow_x + 1 , shadow_y + 1 ] = 1 and image = 5 ){ image = 1 }
if( Grid_Vision[ shadow_x + 1 , shadow_y + 1 ] = 1 and image = 6 ){ image = 13 }
if( Grid_Vision[ shadow_x + 1 , shadow_y + 1 ] = 1 and image = 7 ){ image = 4 }
if( Grid_Vision[ shadow_x + 1 , shadow_y + 1 ] = 1 and image = 10 ){ image = 15 }
if( Grid_Vision[ shadow_x + 1 , shadow_y + 1 ] = 1 and image = 12 ){ image = 12 }
if( Grid_Vision[ shadow_x + 1 , shadow_y + 1 ] = 1 and image = 14 ){ image = 12 }
Grid_Shadow[ shadow_x , shadow_y ] = image;
}
}
}
}
}
}
- Then when I find one I jump to each of the adjacent 8 cells
- Then for each of those, if they are not visible to the player, I...
- Find which shadow image to use by checking each of the adjacent 8 cells to that one.
Any thoughts on how this, or any system like it really, could be optimized? Perhaps using a mathamatical equation to reduce or eliminate if statements? Would storing the check value in a var instead of calling the array each time be faster?
Thanks in advance for any input you have!
Last edited: