Ian Cheng — Emissary Forks At Perfection

Making a Roguelike Using Ncurses with Swift — Part 1

Federico Mazzini
Major League
Published in
3 min readJan 16, 2017

--

This article is the result of my ongoing efforts to understand the different mechanics involved in Roguelike game development.

I chose Swift to code my examples since it’s my main work tool in my role as an iOS developer at Lateral View, and because it’s fun. There would be little to no excitement in just trying to remake an already made C++ game mechanic example.

So let’s get started. For those of you who don’t know, ncurses is a toolkit for developing some king of guided user interface application software that runs under a terminal emulator. In fact, ncurses is a free-software emulation of the now discontinued library curses, curses is a terminal control library for Unix-like systems.

The first curses library was used for several games, most notably Rogue. Do you see what I’m getting at? Some of us could easily say a truly hardcore Roguelike game it’s written using the ncurses library (and C++, but that issue has been addressed at the beginning of this article).

After writing the Swift example on procedurally generated terrain, I’ve decided to give my following examples some context so I’ve coded a pseudo game that runs in the terminal, using the Darwin version of the ncures library. Which happens to have Swift headers. If you’re using Mac OS like I do, this is as simple as adding the following line to your Swift files:

Note: If you happen to use another platform than Mac OS, you can install Swift and make use of the Swift package manager to install the ncurses headers. Here’s an example on how to do that.

In this game, I make use of two well known Roguelike mechanics: map generation and field of view:

The map generator takes a 2d array of 0’s and 1’s and use these two different values to distinguish between two different kinds of map nodes. Those that are passable and those that are not. The non-passable nodes also block light casted by the player. This is a property of the map node class generated from the 2d array. The 2d array it’s hardcoded, I’m planning to implement a procedurally generated array in the future, like the one in my previous article on map generation.

Screenshot from my example game

The field of view or field of vision (abbreviated FOV) is the extent of the observable game world that is visible at any given moment. I implemented this feature using the recursive shadowcasting method, which happens to be an efficient and realistic way of approaching this feature, many popular games use it. There are several articles on the internet on how this method works and what are it’s underlying principles. This is a pretty good one!

Here is my implementation in swift:

I hope to introduce Pathfinding in the next post. For now, I leave you with a link to the example project.

If you want to know more about technology and innovation, check us out at Lateral View or subscribe to our newsletter!

--

--