March 22, 2013
Development of Duke Nukem 2 for iOS

DN2 for iOS runs on top of an i486 PC emulator called Aemula, but you don't throw old exe files into an emulator and call it a game port. We've done our best to make sure it is most enjoyable and as close to a native version as possible.

Here is when we started

The Aemula project has been going on for more than two years now The Development of Aemula. In January 2011, it was started and only became stable in mid 2012. Unfortunately, Apple doesn't want a standalone emulator on the App Store, so I ended up releasing a beta version for jailbroken devices.

One day in January this year, Frederik contacted me saying that he was currently looking for a partner in bringing back Duke Nukem 1 and 2, for iOS and Android devices and wanted to know if Aemula can be used for the project. Since source code for the original Duke Nukem game is long lost, using emulation became the only solution at the moment, especially when you want to preserve the original game style.

The next day I replied with a quick demo, then the development kicked off. The first prototype was a very simple wrap up, with a DPad, shoot and jump buttons. As clumsy as it was, the most important ideas were there and there was still as much fun as any native platformer on the iOS.

Game control

The original game is controlled by keyboard, for example, CTRL key to jump, ALT key to shoot, F2 to save and F3 to load. Nowadays you can't expect a gamer to look for these keys.

At first, we created buttons for both shoot and jump, as you can see in the reveal trailer video. And there is the DPad that can be configured to joystick style or arrow style. Finally we found having two buttons is still too much, especially for iPhone's small screen. So we switched to screen tap as jump and got rid of the jump button. And used pointing upwards on the Dpad for openning a lock or inserting a chip at check points as well. In the end, now you have only two icons on the screen. This minimal setup makes it easy to focus on the game playing. It is surprisingly playable, there is no problem for you to pass Episode 1 boss level on touch screen.

Pixel detection

We can't always show the gamepad overlay. It doesn't make sense having a shoot button while you are in menus. For menu navigation, the up/down arrows on the dpad are good enough. Left/right arrows are not necessary.

Obviously now we need to hook up to a game state variable and display controls according to current state. The problem is that, without the source code it is very difficult to locate such a memory location. Therefore we try to check the screen pixels and guess where we are in the game.
The pixel detection runs every second, and we will show or hide or adjust game controls according to the result.

This kind of deep customization is something you can't have in a general emulator.

Touchable menu

The original game used up and down arrow keys for navigating through menu items. Then one day, Scott Miller show a demo to a friend, and found that he completely ignore the arrows and try to tap on the menu directly. So the question is: can we do something about that?

It turns out we can!

First we check which item is tapped, that can be done because each item has a fixed position on the screen.

Second we check which item is currently selected, that is a little bit difficult because we need to remember the pixels of each highlighted item. But still that is something we can work out.

Third we decide what keys we can use to move the focus to newly tapped item. For example, if currently we are at "Restore game", and we tapped on the "Start a game", then we need one UP + ENTER. In the skill select screen, if currently we are at "Hard", and decide to change to "Easy", then we will need UP + UP + ENTER.

Every key event needs a small delay otherwise will be ignored by the game. That's also the reason you will see a slight delay while you are tapping.

We have to keep the arrows for some of the screens, like restore or save games, because there are way too many menu items to use the same hack.

There are some other sweet features. One is using swipe to select episodes. The other is to show the iOS keyboard after you press OK in the save game screen, so that you can type in the saved file name. In the new high score screen, keyboard will show up immediately and the game screen will be shifted upward for a small amount so that the input line won't be covered by iOS keyboard. After you're done with typing, press return will dismiss the keyboard.

Hacking game files

The original main menu has way too many items, like option, ordering information and story are not really that interesting now. To make the touchable menu work better, we need to remove the unnecessary items. Fortunately, it is something hackable, thanks to the flexible design of original game. There is a resource file including description of menus, and it's a human readable format. We can make small adjustments to this file without rebuilding the whole game.

The sprites and scene graphics can also be edited with a tool developed by a modder "King Duke". I watched Leon, who is really good with pixels, putting on sunglasses on Duke's sprites. There are tons of them, and there are also Duke graphics in the intro scenes. All are upgraded.

Another good design of the original game is that the single NUKEM2.EXE is also a pack of many resource files. If you provide a new resource file in the same directory, the program will pick up the new version automatically. Very clever, isn't it?

Things are not perfect

Exit game

There are certian things that are very difficult to do right. One is to exit game and return to launching screen.

One of Aemula's powerful features is that you can terminate the emulator and start over at any time. In the case of Duke Nukem 2, however, you have to exit the game from its main menu, otherwise saved games won't be written to disk. A brutal termination will lose them.

We add an "Exit Game" item in the PAUSE menu. What it does is to emulate a sequence of ESC + Y. If you are inside the game, it exits the game playing to the menu. If you are on the menu, then it will quit the emulation and go back to launch screen. It is recommended that when you want to take a break, you should always use "Exit Game" repeatedly until you go back to the launch screen.

Sometimes you are watching the intro scenes and haven't gone to the main menu yet, and then if you use ``Exit Game'' function, it will terminate the emulation immediately because there is nothing to save at this point.

Game saving

Mobile games nowadays always save progress as you play. In Duke2, you can only save at certain check points. If you die before reaching to a check point, you will have to restart the level. If you exit game before that, you also have to start over too. A casual gamer may find Duke2 incredibly hard, given the numerous challenges and dangerous creatures present in every level. Yet this is exactly what it was in the old days.

Performance

Aemula is very CPU intensive, so you can not have a smooth playing on either iPhone 4 or earlier models. iPhone 4S is the first model featuring a dual core CPU, and since Aemula is optimized with multi-threading, performance on iPhone 4S is far better than on iPhone 4.

Duke Nukem 2 plays smoothly on all iPad models, even on iPad 1. Like iPhone 4, iPad 1 has a single core, but it's clocked at much higher frequency and that helps a lot.

After words

Life changes, memory fades, but bits never rot. When I show a prototype to a friend, he was very excited: ``I remember this one!'' He then took my iPhone and within a minute he's already shooting happily. He said that he don't remember the keys any more, but the on screen game controls are really helpful to get him started.

It's true not all games can be adapted to mobile devices as well as Duke2. This gives all game designers an interesting challenge: could you design a game that can still be running happily in 20 years or even more?