Feb 24, 2021 - BSP Lock and Key

Comments

I’ve been working on C-Dogs SDL lately, and one thing that often bothered me about that game is that its default random map generator provides an uneven experience. It’s exciting at the start when everything’s unexplored and there are many corridors and rooms to visit, but in the late stage when you are hunting for keys and objectives, backtracking all over the map, it can be quite tedious.

C-Dogs’s map generator is quite configurable and produces a wide variety of maps, but at its core it’s pretty simple:

  • Start with an open area
  • Randomly place rooms around the place
  • Randomly lock some rooms, and place keys in the rooms

C-Dogs map

The lock-and-key system is in series: the yellow key is in an unlocked room, the green key is in a room locked with the yellow key, the blue key is in a room locked with the green key and so on. Here’s a tree to illustrate this simple structure:

C-Dogs map graph

Because the keys (and objectives) could be in any one of several randomly placed rooms, you may need to criss-cross the map many times to collect them all. That’s a lot of backtracking! Another issue is the structure - rooms are isolated from each other, all leading outside. This gives all maps a similar feel. What if there were a map generator that could:

  • Provide a more interesting structure
  • Feel more like an indoors experience, like a building interior or dungeon?

I was first inspired by sauer2, maker of many C-Dogs SDL custom campaigns, which often take place indoors. These have cool features like:

  • Corridors linking many rooms
  • Rooms connected directly to each other
  • Locks separating entire sections of the map at a time
  • Branching pathways
  • Minimal required backtracking

Space Pirates map

Searching online, I came across a neat technique that uses binary space partitioning to generate a building interior. The algorithm recursively splits the area in half, placing a corridor at the split, and continues splitting (while alternating horizontal/vertical splits). Areas left unsplit are then filled with rooms. It’s a simple yet flexible technique.

BSP algo

As the structure of the map is a deeper tree, by finding a critical path that starts and ends at leaf nodes but passes through the root, we can get a fairly long main path, with shorter branches along the way. Here’s what that looks like, in my initial adaptation:

BSP interior demo

Here’s where lock-and-key systems come in. Many games, including turn-based RPGs and action games, employ a lock and key puzzle system to get the player to explore and provide a sense of progress. Games like The Legend of Zelda do this particularly well - an analysis of the dungeons shows that most dungeons are quite linear, with few and shallow side branches, but the clever placement of locks and keys makes the dungeons feel more complex. The key to this is the placement of locks before the key that opens them, which provides foreshadowing.

Zelda dungeon

It was quite easy to adapt this to the BSP map. As the critical path starts at a leaf node, goes up through parent nodes to the root, then down through child nodes to another leaf node, we can place locks in the intermediate parent nodes, and place keys somewhere in the sibling hierarchy.

C-Dogs BSP path

Not only do players encounter the locks before keys, because of needing to sidetrack to find the key and then return to the lock, but the map is also made to feel bigger and more complex than otherwise.

C-Dogs BSP graph

Finishing touches like removed rooms, locked rooms where hidden objectives or powerups are placed, are then added.

C-Dogs BSP demo

It’s a simple and effective technique!

For more reading about this kind of technique, also visit Mechanic #004 - Environment Tree

Jun 27, 2020 - Fantasy Consoles for Jams

Comments

I’ve done a lot of game jams and have used many engines and frameworks in these jams. They all have pros and cons, but lately, I’ve been using fantasy consoles as my platform of choice, and I think you should too - they are fantastic for game jams.

What are fantasy consoles? In short, they are virtual consoles that mimic retro computers and their limitations, but also provide a complete game development environment. Within the same application, you can draw simple sprites, compose chiptune music and sound effects, and write code using a limited API. Its severe limitations are also its appeal; it’s not only a piece of cake to pick up and learn, but the limitations encourage creativity. PICO-8 is the most popular, but there are many others to choose from.

PICO-8

One of the most important factors when choosing a platform for jams is development speed. You want to be able to put something playable together as quickly as possible, as time is the scarce resource. Here the popular engines do very well; Unity, for example, has a lot of functionality built-in, or easily accessed via its asset store, so you can quickly piece something rough together.

Fantasy consoles represent an alternate approach - with limitations and fewer tools at your disposal, there is less time wasted in choosing how to make your game and more time on focusing on gameplay and making something fun. It’s easy and quick to put together something playable, so you can start iterating and polishing on top of a solid base.

Here I’ll show you my weapon-of-choice: TIC-80. It’s very similar to other popular fantasy consoles, so the experience is transferrable. Within the same tool, you can:

  • Code in Lua*, against a simple sprite-and-tile based 2D API
  • Draw sprites and tiles using a 16-colour palette
  • Create chiptune sound effects and music with up to 4 channels in a simple tracker
  • Edit tilemaps
  • Export to HTML

TIC-80

Coding

TIC-80 code editor

TIC-80 primarily uses Lua for coding, but you get other language choices - Moonscript, JavaScript, Wren and Fennel. I like Moonscript the best because it’s like Lua without the bad parts. Its class syntax is excellent, something that sucks in Lua.

As a fantasy console, the editor window is tiny. While charming, I highly recommend getting the pro version for $5, which lets you code using any text editor.

This way, the entire game fits in a single text file, even graphics and sound, which is great for version control and collaboration between your artists and musicians. No more headaches from merging everyone’s work - it’s just lines of code in a file!

TIC-80 pro code

Graphics

TIC-80 has a mainly sprite-based 2D API, with built-in sprite editor and tilemap editor. They’re nothing to write home about, and get the job done.

TIC-80 sprite editor

Worth mentioning are the shape drawing functions available. In addition to the expected lines, rectangles and circles, you can also draw textured triangles, and which some enterprising folks have made 3D engines out of this. Not that you should do this for your jams, but they’re nifty tools to have. Check out the API for more details.

TIC-80 sprite rotation

Sound & Music

Sound and music work differently in TIC-80 than you might be used to, but in a way that makes it quite fun to use. In essence, you build these up in layers:

  • Create waveforms - you get 16 of these. You get the standard square, triangle, sawtooth and noise waves, but you can also draw your own, something which few other tools let you do.

    TIC-80 waveform editor

  • Edit envelopes - these are the sound effects, defined as four parameters: waveform, volume, arpeggio, and pitch. It’s a bit hard to explain, but here are some examples of things you can do:
    • Create a plucked-instrument with a sharp attack, by starting with a noise wave
    • Fade in or out using the volume envelope
    • Create a melodic chime with the arpeggio envelope
    • Create a vibrato effect with the pitch envelope

    TIC-80 envelope editor

  • Edit music. Here the editor is a more conventional tracker - you use instruments (which are the sound effects created earlier) and lay notes on patterns. Patterns are per-channel and can be reused, which is handy because you only get 60 of them.

    TIC-80 music editor

I’ve found it handy to keep a notebook of all the sound effects and pattern numbers used, as this is perhaps the clunkiest part of TIC-80.

Exporting

Exporting is probably the best part of TIC-80 for jams - how easy it is to export games from it. You can:

  • Save the game as a .tic cartridge, loadable by anyone running TIC-80
  • Export to a single .html file using export html
  • Export to an executable using export native
  • Take a screenshot using F8
  • Record a gif using F9

So preparing a jam submission is a cinch. What might take an hour messing around with screen recorders, exporters and so on, take only minutes with TIC-80. Take screenshots and gifs early and often and tweet/blog your progress!

Try it out!

That’s it! Fantasy consoles are so simple to learn, use, and are perfect for jams. Here are some games I’ve made with TIC-80:

And you can even take a look at the source code and how the game was put together - these fantasy consoles are editor-and-player all-in-one.

Try it out and happy jamming!

Apr 12, 2020 - The Language of Music

Comments

For a while there was this idea that music was the universal language. Our human languages may be mutually unintelligible but we could always enjoy each other’s music. Music is such an important part of our cultures that we even sent it out on the Voyager golden record.

But is it really as universal as we thought? Even hearing some of the songs in the golden record, they could sound beautiful yet alien to those of us accustomed to Western music, like this haunting Navajo chant:

And then consider how different everyone’s musical tastes can be, how they are shaped by teenage experience in particular. The kind of music we listen to as adolescents usually sticks with us for all time.

So how universal is music really? To get to the bottom of this we need to go to the fundamentals of music: melody, harmony and rhythm.

Music is made up of sound; consider a single note, which is a vibration at a constant frequency, or pitch. A melody is a sequence of such notes. But what pitches should these notes be? Why do we have things such as scales?

Well if you look at a string instrument, the vibrating string creates the sound. Halve the length of the string, and it creates a pitch that is exactly one octave up - it sounds very similar, but is a higher pitch. Play the string at 1/3 (or 2/3) the length, and we get a pitch corresponding to a perfect fifth.

overtones

If we keep going, we’ll get the major third as well. These three notes comprise the major chord, a very harmonic and uplifting chord very commonly found in many styles of music, especially Western music. Ode to Joy in particular uses predominantly major chords in its main theme, and it’s a very powerful piece.

So that’s pretty cool - the major chord in fact has a mathematical basis, and since math is universal, the major chord is too. If we go further, we find that the pentatonic scale can also be constructed using just intonation, and this is a scale that’s very widely used, like in Eastern music, blues, and many of their descendants.

Mathmagic land

However if we keep going and look at further harmonics, we notice something odd. In Western music we use an equal temperament scale - that is, every tone is separated by the next by an equal ratio. This doesn’t happen with harmonics, where if you tried to construct a scale out of natural harmonics, some notes will sound slightly out of tune:

This was especially frustrating to the ancient Greeks, who were obsessed with perfect mathematical representations of reality. The Pythagorean scale, which is constructed completely out of the 3:2 ratio, is ever so slightly off compared to the equal temperament scale.

Although we’d like to think there’s something universal about the Western scales, in reality it does contain arbitrary elements. The Western scale is based on 12 equal interval semitones; look outside that system and we find ones like those used in gamelan music, made up of equal 5ths or 7ths:

Or various Middle Eastern scales, which use quarter tones:

Many listeners accustomed to Western music will find these dissonant, but there is nothing inherently dissonant about these, any more than Western music is inherently harmonious. The only thing notable about the Western scale is that it both incorporates equal temperament (12 semitones) and many of the pure intonations. 12 can be evenly divided by 2, 3, 4 and 6, which is quite a lot. But varying that, we could construct many alternate scales. There is no one perfect scale.

Let’s look at rhythm, since it seems there might be more universality there. The most basic rhythm is a steady beat, but at what frequency? Research shows the most popular kinds of music fall within the 120-130 beats per minute range, and this may have to do with the heart rate of a brisk walk. The most popular time signature - that is, how many beats per measure, or “bar” - is 2 or 4 beats. It seems no coincidence that this even number corresponds to the physical act of walking - tick, tock, tick tock; or left, right, left, right… the rhythm we prefer is tightly coupled to our physiology. So much so that our bodies actually want to sync up with the rhythm. Slow music soothes and relaxes us, whereas fast music gets us excited. A lot of other activities syncs up to this tempo, whether that’s walking pace, or even the speed of clapping. Our bodies have a natural rhythm, and that translates to our music.

We see more of this correspondence between our bodies and rhythm in music. Add elements like shuffling, swings, or syncopation, and a dancer is compelled to follow using more complex movements. Music played slightly ahead of the beat sounds eager; played slightly behind the beat and it sounds laid back.

This might be the case for humans, but it might apply for animals too. By varying the tones, pitch and rhythm to suit their physiology, we’ve seen that animals can respond to music too, but this music would sound incomprehensible to us, just as sounds that are too high or low pitched, too slow or too fast, would sound uninteresting. It seems there’s much truth to the slow-talking of the Ents.

So what the heck is music? It’s perhaps a bit of everything: something based on universal mathematical properties, but also with cultural variations analogous to our languages or even accents, and rooted in our unique physiology. It is a language but much more; universal, expressive, and fundamentally human.