This function converts a DS grid into an MP grid, where all 0 values become "empty" and all others "occupied".
An optional mapping function can be provided to determine which values in the DS grid should be considered "empty" or "occupied".
NOTE To convert from MP grid to DS grid, see mp_grid_to_ds_grid.
WARNING The DS grid and MP grid must have the same width and height. If the grids' dimensions don't match, the conversion will fail and an error "Error, grid sizes do not match" is thrown.
The optional mapping function that you pass to this function should take the following parameters:
Argument | Type | Description |
---|---|---|
value | Real | The value in the current cell |
x | Real | The x index/position of the grid cell |
y | Real | The y index/position of the grid cell |
ds_grid_to_mp_grid(ds_grid, mp_grid[, func]);
Argument | Type | Description |
---|---|---|
ds_grid | DS Grid | The DS grid to convert |
mp_grid | MP Grid ID | The MP grid to write the data to |
func | Function | OPTIONAL The mapping function to use |
var _size = 10;
grd_data = ds_grid_create(_size, _size);
repeat(20)
{
ds_grid_set(grd_data, irandom(_size-1), irandom(_size-1), choose(0, false, true, "7", 47));
}
mpg_data = mp_grid_create(0, 0, _size, _size, 16, 16);
ds_grid_to_mp_grid(grd_data, mpg_data);
The above code first creates a DS grid of 10 cells by 10 cells (initialised in the _size variable) using ds_grid_create. It then assigns one of 5 possible values, selected using the choose function, to each of 20 randomly selected cells in the grid. After that, it creates an MP grid using mp_grid_create and fills it using the contents of the DS grid by calling the ds_grid_to_mp_grid function. DS grid cells containing 0 or false will be marked as "empty" in the MP grid, cells containing true, "7" or 47 marked as "occupied".
width = 128;
height = 128;
cellsize = 4;
function occupied(value, x, y)
{
switch(value)
{
case "Mountains":
case "Water":
case "Forest":
return true;
case "Grass":
return false;
default:
return false;
}
}
grd_level = ds_grid_create(width, height);
ds_grid_clear(grd_level, "Grass");
ds_grid_set_disk(grd_level, 60, 60, 20, "Water");
ds_grid_set_disk(grd_level, 100, 60, 20, "Mountains");
ds_grid_set_region(grd_level, 60, 100, 110, 110, "Forest");
mpg_level = mp_grid_create(0, 0, width, height, 4, 4);
ds_grid_to_mp_grid(grd_level, mpg_level, occupied);
The above code first shows an example of how to use a mapping function. First it defines width, height and cellsize as instance variables, as well as a mapping function occupied that returns true if the value is one of "Mountains", "Water" or "Forest" or false if it is either "Grass" or any other value. It then creates a DS grid grd_level of the given width and height and fills it with a couple of terrain types. After that, an MP grid of the same width and height is created using mp_grid_create and its ID stored in a variable mpg_level. Finally the MP grid is initialized with the contents of the DS grid, using ds_grid_to_mp_grid.