The PicoBreed game uses auto-tiles. You place a single tile as a trigger on the map editor and this function takes care of placing the correct tiles. It’s called once in the _init().
The function takes advantage of the ‘flags’ that can be set inside the pico8 editor. each sprite can have a set of flags set. When we detect this flag, we know that this tile is a candidate to be replaced.
We need a set of sprites to use however, ones specially made to connect together with neighbouring sprites. In this game there are two types of walls, standard metal and blocks that are infected. They are all lined up.
Lets walk through the function and pull it apart.
function auto_tile() for i=0,128 do --we loop the entire map, all the way across for j=0,64 do --and all the way down to 64 tile=mget(i, j) --we get the current tile if tile == 64 or tile == 80 then --is this a metal or infected block? this_flag=fget(mget(i, j), 2) --is the second flag set? (true/false) if this_flag then --it was set, so... -- we need to find out if there are tiles -- all around and that they also have the flag set -- we only check above, below, left and right if fget(mget(i, j-1), 2) then flag_above = 1 else flag_above = 0 end if fget(mget(i, j+1), 2) then flag_below = 1 else flag_below = 0 end if fget(mget(i-1, j), 2) then flag_left = 1 else flag_left = 0 end if fget(mget(i+1, j), 2) then flag_right = 1 else flag_right = 0 end -- this magic is called 'bitmasking' new_tile = (1*flag_above) + (2*flag_left) + (4*flag_right) + (8*flag_below) mset(i, j, new_tile+tile) --set this tile to a new tile end end end end end
How does bitmasking work?
In our tile set we need different combinations of tiles that connect together. In a grid we need only check for connections above, below, to the left and right. This gives us 16 tiles in total. Here are the tiles we need:
In the PicoBreed there are two sets of these and they look like this:
If you notice, each row has exactly 16 sprites, the correct amount for this type of automatic tile.
In the code we multiply some numbers and add them together.
new_tile = (1*flag_above) + (2*flag_left) + (4*flag_right) + (8*flag_below)
What we’re doing here is using the presence of the blocks around the outside to determine which one of our sprites were going to use to replace the current sprite.
We are adding a number to the original tile and replacing it with that number. This means we can use different tiles as starting points, like the metal and the infected.
Here is a shot of the level editor before changes
Here is a shot of the game after auto tile has run.
Here is some dog tax.
Till next time, happy coding.
p.s. if you want to know about the maths of bitmasking then check out this article:
In this loop we can also add things like spawners to our map, since we are reading all the tiles, this means if we find one we want to swap out, we can execute some code in this location and swap out the tile for something. Normally we would swap out the map tile for a blank tile.
Here is an example of swapping out a tile and spawning something.
if (tile == 32) then mk_spawn(45,2,i,j); mset(i, j, 125); end
In this case, if we see sprite 32, then create a spawner here and set the tile to something empty, so that when the spawner is defeated, the space is empty.
And that about wraps up auto-tiling, and sneaky spawning of enemies and objects based on map tiles.
If you are enjoying this series of snippets and ideas for Pico8 then hop on over to my Patreon and show me some love. Until next time, thanks for reading.