// Build a bit mask of all 8 surrounding tiles, setting the bit if the tile is not an
// ocean tile. Starting with the tile to the left as the least significant bit and
// going clockwise
bitmask =
(leftTile != ocean ? 1 : 0)|
(topLeftTile != ocean ? 2 : 0)|
(topTile != ocean ? 4 : 0)|
(topRightTile != ocean ? 8 : 0)|
(rightTile != ocean ? 16 : 0)|
(botRightTile != ocean ? 32 : 0)|
(botTile != ocean ? 64 : 0)|
(botLeftTile != ocean ? 128 : 0);
if (bitmask == 0) {
// This means the tile is surrounded by ocean, so simply draw the ocean tile with
// no coast
// screen_x, screen_y, sprite_x, sprite_y, width, height
drawImage(0, 0, 0, 176, 16, 16);
} else {
// There are at least one surrounding tile that is not ocean, so we need to render
// coast. We divide the tile into four 8x8 subtiles and for each of these we want
// a 3 bit bitmask of the surrounding tiles. We do this by looking at the 3 least
// significant bits for the top left subtile and shift the mask to the right as we
// are going around the tile. This way we are "rotating" our bitmask. The result
// are our x offsets into ter257.pic
topLeftSubtileOffset = bitmask&7;
topRightSubtileOffset = (bitmask>>2)&7);
bottomRightSubtileOffset = (bitmask>>4)&7);
bottomLeftSubtileOffset = (bitmask>>6)&7)|((bitmask&1)<<2);
// screen_x, screen_y, sprite_x, sprite_y, width, height
drawImage(0, 0, topLeftSubtileOffset<<4, 176, 8, 8);
drawImage(8, 0, (topRightSubtileOffset<<4)+8, 176, 8, 8);
drawImage(8, 8, (bottomRightSubtileOffset<<4)+8, 176+8, 8, 8);
drawImage(0, 8, bottomLeftSubtileOffset<<4, 176+8, 8, 8);
}