Boulderdash Cave Data

Sat, Oct 29, 2011

Previously, I released an experimental javascript Boulderdash game and promised to write up some articles about how the game works:


The topics I wanted to cover include:

So, lets finish up by talking about the Boulderdash cave data…

Decoding the c64 Cave Data

The c64 had a very restrictive amount of memory, every byte had to be made to count. While it has 64k memory only about 35k was available for programs.

The boulderdash caves consist of 880 cells (40x22), and there are 20 levels, so even if each cell only required a single byte, a verbose definition of all 20 levels would take over 17k - that would be a really tight squeeze once you add all the game code and graphics.

So to save space, caves are not defined as a fully expanded 40x22 array of bytes.

Instead they are declared with 2 parts:

  • random items - using a predictable random number generator, each cell initially contains one of (up to) 4 random items, usually SPACE, BOULDER or DIAMOND
  • placed items - a list of objects, lines or rectangles are overlaid over the random items

This allows caves to be defined very compactly, for example level 1 consists of:

  • random items - each cell starts off with a chance of being:

    • SPACE: 23%
    • BOULDER: 19%
    • DIAMOND: 3%
  • placed items

    • line of BRICK from (1, 9), length = 30, direction = right
    • line of BRICK from (9, 16), length = 30, direction = right
    • single PREROCKFORD at (3, 4)
    • single PREOUTBOX at (38, 18)

This can all be stored in about 25 bytes

The exact format used has been described in detail by Jeff Bevis and Peter Broadribb, who also wrote the original decodecaves.c program and then described the decoded cave data.

Summary

In hindsight, I could have just taken the already decoded data and simply reformatted it manually into JSON format…

… but for some reason I decided to port the decodecaves.c program to javascript, and thats what you will see in the caves.js source file. This could come in useful if we were to expand our javascript version to be able to load in other caves, such as those from Boulderdash 2 or 3, or the construction kit.

And that just about wraps it up for Boulderdash!

To summarize, we have looked at:

  • a game loop and rendering loop that are independent.
  • a game logic engine that is independent of how it is rendered.
  • an HTML5 canvas renderer for the game engine.
  • a decoder to parse the original c64 cave data.

Enjoy!

More information…

Game specs…