Saturday, July 20, 2019

Prep for testing

My goal is to have the first area of the game ready for a few folks to sanity check and test for me soon. The goal of this first round of testing is just to make sure I'm not completely off-track. Does it actually run properly for other humans? Is it playable? Is the difficulty completely out of wack? What about the controls?

That means that before I hand it over to folks I need to add just enough polish on everything that it's not a frustrating experience. There are tons of things that aren't done, but that's ok. It just needs to be enough that you get to experience the game properly.

I've been playing through the first area a few times to make sure if feels ok to play to me. It's not very big -- only about 20 different rooms. But that's enough to take a few minutes exploring. And enough to get frustrated by if it doesn't play well. I've already had to change a few rooms that ended up being annoyingly hard.

The other thing that I spent some time on was getting saving/loading working. I fully expect my testers to die a couple times, even this early. I don't want them to have to start completely over every time they die, which means getting game saves working. I have another blog post I need to write about how flash saves work on the mapper I'm using. But I had that part working. What I didn't have working was everything that interacts with actually saving: What happens when you die? How do you load a saved game? How do you start over instead of continuing from your saved game? Does everything get loaded properly when you resume (answer: NO)

I think I finally have all that debugged and working. Tonight was the final piece of it: adding a simple UI for letting the player choose to Start or Continue. Which means title/menu work. Which always takes significantly longer than you expect, and is really boring. Yeehaw.

I'm almost there though. Time to do some more playthroughs.

Now it's a fairly short list of things that I hoped to have in this demo but are missing, and probably won't make it to this first testing round:
  • Super NES controller support (there's a lot of "hold down + A" type things in the game. If the player has a SNES to NES controller adapter, they can use dedicated buttons for those)
  • New Enemy Graphics (I'm using tons of placeholders)
  • The ability to shoot upward at an angle once you find the tank
  • Fix buggy behavior with the health-hungry blob enemy

Friday, July 19, 2019

Pseudo-fixed bank

I wrote about bank switching a few years ago when working on Atari Anguna. The idea being that the 6502 (or worse, the 6507 which is the variant of 6502 in the Atari) can only address a certain amount of ROM memory, so larger cartridges use multiple "banks" where you can switch which bank of ROM you're using. Nintendo cartridges used the same technique, so bank switching is important on both consoles.

Many variants of cartridge hardware (commonly known as mappers) divide ROM space into 2 (or more) sections. One that always stays active, and a second section that you can switch. This is a lot easier than switching the entire ROM space at once, as you can control things from the fixed bank, and just load data or call routines from the swappable banks as needed.

The mapper that I'm using for Halcyon (GTROM) doesn't have a fixed bank. Like Atari Anguna, it swaps out the entire bank all at once. Which means that you have to have special code to call a routine in a different bank. You put a stub of code (commonly called a trampoline) in the same place in both banks (or in RAM, which doesn't get swapped). You call that stub, it changes banks, then calls the new routine.

That's worked well for me so far on Halcyon. But now I've realized I have about 15 subroutines that I call a lot and from all sorts of different banks. I decided it would be faster (both in terms of writing code, and in execution) to avoid trampolines for those, and instead, duplicate just those routines into every bank that uses them.

Unfortunately, cc65 (the compiler/assembler suite I'm using) doesn't have direct support for duplicating routines multiple times in different places. So instead I'm doing some ugly hacks.

First, I compile the whole project once, leaving blank space in the places where the code should be duplicated to. One of the linker outputs is a nice map file, which tells the address of every piece of code in the final assembled output. So I have a python script that analyzes the map file, and extracts the portion of compiled binary that should be duplicated in other banks. It writes this to a file, and compiles everything again, this time injecting that extracted binary into each place that it needs to be duplicated.

This worked perfectly until last night when I started getting weird crashes calling code in this duplicated pseudo-fixed bank. Why was it crashing?  After an hour of angry debugging, I discovered the issue:  My python script had an off-by-one error. It was omitting the last byte of the pseudo-fixed bank.  Which means I had half an instruction dangling without the other half. That would do it.

Sunday, July 14, 2019

First Boss

I feel like I'm finally making real progress. Using some placeholder graphics from the ever-amazing surt, I've got the first boss fight finished. This boss is the main obstacle in your way from finding the tank vehicle, so you have to fight it on foot.

In Halcyon, bosses are mostly just another instance of a regular enemy, just harder. There were a few differences which I'll talk about in a minute, but interestingly, those differences aren't what took me the most time with this fight. Really, the things that took the longest were bugs in my main enemy-handling code that only revealed themselves while developing the boss fight:
  • The boss shoots missiles. The system for spawning a bullet for a boss had a bug based on the sign of the vertical offset.  Meaning if you spawned a bullet above an enemy's reference point, it failed. (every bullet I had spawned until this point was shot downward from an enemy, so I never noticed)
  • The edge-of-screen clipping was broken on the left side of the screen. Other enemies were narrow enough that I didn't notice, but enemies would disappear once about halfway off the left edge.
  • My enemy animation system was broken for animations that had multiple different repeating sections
This guy's look might change entirely once Frankengraphics gets ahold of him.
I'm also not sure about the vertical placement of the enemy health bar. We'll see.

Once I got those things worked out, developing the things specific to bosses was pretty quick:
  • I wanted a boss HP meter so you had some idea of how the fight was going. I thought this might take a while to develop, but I was able to reuse the code from the player's HP meter without much work, so this only took about 10 minutes to get working!
  • Bosses need a way to stay dead once you kill them. I already had the engine infrastructure in place to persist important global game state, so I just needed to add hooks before spawning the boss, and after killing it, to read and write that game state.
  • I added another entity, a type of gate, that blocks a pathway until all other enemies on the screen have been killed. This could be generally useful in other screens where I want to force a fight, but was important to make you actually stay and fight the boss.
The two things that I'm still missing are special boss music (which should be easy, but I just need new music), and a larger and more interesting explosion for bosses.

That said, other bosses in the game might require something more complicated -- larger bosses might be made from background tiles, and require some sort of background-scrolling magic to animate them. We'll cross that bridge when we come to it.

It's almost time for another round of testing, to see what's completely broken, or if the challenge level is acceptable. If you're reading this, and are interested in helping test the game, send me a message!

Thursday, July 11, 2019

Defining enemies

Enemies are the type of data-driven thing in video games that are fun to think about, in terms of tooling and development.  They have lots of stats (health, size, damage they do, etc), and a lot of different behaviors attached (What function runs to update them? Does anything special happen when they take damage? What about when they deal damage? Or when they die?)

There are tons of solutions for how to handle this. But one of my soap-boxes is that game data should be defined in a textual format, and be easy to edit (either by editing the text format, or with an easy UI tool).

For Halcyon, I decided to use a YAML format for enemy definitions. YAML is a data definition language, similar to XML or JSON. If you read about it, there's all sorts of reasons people say you shouldn't use it. But it has one important thing going for it: it's really easy to read and write. Which is what I need for this game.

- name:        topgunner
  hp:          4
  width:       16
  height:      16
  dmg:         1
  on-collide:  NORMAL
  on-offscreen: KILL
  update:      enemyb_topgunnerUpdate
  animation:   anim_topgunner
  frames:      frames_topgunner
  init:        enemyb_topgunnerInit

- name:        topgunnerBullet
  hp:          4
  width:       4
  height:      4
  dmg:         1
  on-collide:  NORMAL
  on-offscreen: KILL
  on-shoot:    NONE
  update:      enemyb_8wayBulletUpdate
  animation:   anim_shooterBullet
  frames:      frames_shooterBullet
  flags:       BULLET

I define my enemies in a YAML format, specifying both their numeric stats, the name of some animation data to display them, as well as various routines and properties to define their behavior. My build script then converts this YAML at compile-time into an assembly language file that defines arrays of data to represent each enemy.  Symbol names (like enemyb_8wayBulletUpdate) get injected right into the generated assembly code, and the linker knows how to translate those into the addresses of the functions that they refer to.

Sunday, July 7, 2019

Big and Tall

One of the things that can use up a lot of your CPU time on the NES is metasprite rendering.  Each hardware sprite is 8x8 pixels (or optionally 8x16). Complicated characters are then made up of a bunch of different sprites together, which is often referred to as a metasprite

Rendering a metasprite isn't complicated -- you push X, Y, tile, and color values into the right spot for the first sprite, then read the next sprite, adjust X and Y appropriately, and repeat.  The problem is twofold. First, it's just a lot of instructions on the 6502 to read, add, and write the sprites, when there's a lot going on onscreen.  Second, you have to deal with clipping.

Sprites can be positioned from pixel 0 to 256 horizontally, which is represented in a single byte. If you wrap around from 256 to 0, the sprite will appear on the left side. So for a large metasprite that's near the edge of the screen, you have two options.  First is to hide the object entirely if any of the sprites go off the edge. This is faster, but looks bad -- the character pops onto or off of the screen. A lot of old nes games do that, and you get used to it as a player, but it looks cheesy.  The second is do check each single sprite as you're computing its position. If a sprite's X value wraps around from 256 to 0 (or vice versa), you omit drawing that sprite. That gives a much better appearance of smoothly scrolling off the edge of the screen.  Unfortunately, it's that much more math you're doing for each sprite in the game.  Since enemies are often made of at least 4 sprites, and often 8 or more, that can become cumbersome very quickly.

In Halcyon, I wasn't happy with the popping-off-the-edge solution, so I opted for taking the time to check each sprite. To try to keep it easier on the CPU time, I have a number of ugly optimizations that make the wrapping/clipping checks a bit faster.  Unfortunately, one side effect of those optimizations is that my metasprites can only be 32 pixels wide or tall.

That was fine until this week, when I wanted to make a tall barrier that opens when you defeat a boss.  It needed to be more than 32 pixels tall.

That ugly thing in the middle is an "enemy" wall that can raise/lower depending on what other enemies are alive. The graphics are just placeholders.

That was tonight's challenge -- how to work around my optimizations and allow larger enemies?

The answer ended up being fairly simple: I still limit each metasprite to 32x32 pixels, but I allow a metasprite to specify a new base position and start a new metasprite from there.  So really, a metasprite can be more of a meta-metasprite, being made of a few different metasprites at different offsets.

Ugly?  You bet.  But it works.

NES Anguna

Well, I had a little bit of time still, while Frankengraphics is finishing up her game Project Blue, to have a little downtime on Halcyon, s...