Random encounters have been on my mind a lot recently, because I’ve been playing Final Fantasy VII for the first time. However, even before I was 25 years late to that seminal JRPG party, the well-established Baldur’s Gate influence ensured that random encounters would be on Exon‘s roadmap. In the mood for a break from working on the dark underground mine levels (and the obligatory Towers of Hanoi puzzle within), I decided it was high time I got some sun-soaked wilderness battle arenas in there.
You Have Been Waylaid
First question: why do it at all? Lots of people malign random encounters for being pointless busywork, interruptions that break the flow with unrelated nonsense. (And in the case of Final Fantasy VII, I’d frequently be inclined to agree.)
However, I want to make it clear that living off the grid as an exon is dangerous — that’s why not everybody is one. The cities, obvious roads and train tracks might be fairly well guarded, but the beaten paths do not extend far into the wilderness. There’s loot out there in the ruins… if you think you’re hard enough.
Also: it’s fundamentally an action game, this is a way to introduce more action.

On the face of it, it’s a simple enough task to implement random battles. Make a very small map with some enemy spawn points, plonk down a random group of enemies at start-up, and let all the standard gameplay systems kick in. Then all we need is a way to redirect the player into it.
Luckily that part’s not a problem at all, because I’ve already over-engineered Exon‘s world travel system. Each area you can find on the map is linked to its neighbours by specific routes, primarily to encode their travel times. When you select an area to go to, travel is performed by adding up the durations of all the links you need to follow to get to your destination — classic computer science graph traversal stuff, we’ve all done it before (even if not for… a very long time…). It’s trivial for me to add more data to a link, such as a probability for getting waylaid and a level or two in which to set the ambush.
Then, while simulating travel by… fast-forwarding the game clock and doing nothing else… all I need to do is roll the dice for each link to see if you’ve been waylaid on it, then stop the clock early and load the interruption level instead of the intended target.

It was actually leaving the battle zone that gave me a heap of unanticipated headaches.
Because a battle occurs “half-way” along a particular link between two real levels, resuming your journey is a bit wobbly. Initially I faffed with trying to simulate starting from half-way along the link, until I realised it would be a lot easier to cook up a transient map node with new links to both sides of the original link. This also seamlessly supports you changing your mind and going somewhere else instead of literally resuming the same journey to the same destination. (Maybe you took a few too many hits and need to turn back to visit the shops?)
(Let’s sidestep for the moment that the world map is in fact a directed graph, so you may be able to hop from area A to area B but not necessarily from B back to A. So far this only exists to ensure you arrive at the correct side of your destination, but maybe I can include… uh… one-way waterslides on the world map. Y-yeah…)

There is another snag that comes from the same root: repeated visits to the same level represent independent encounters. You’re not unlocking a specific location that you might return to time and again like the Academy or a Waystation, you’re stopping at an abstract representation of some point on the road that happens to be good for an ambush. So what if that particular arrangement of trees and rocks looks suspiciously familiar?
That means a random encounter level has to be drilled out of the saving and loading system. While you should be able to save during an encounter (because dinner waits for no man), once you leave that area it is gone forever: I’m reusing the same scene because I’m a lazy dev, but it’s not actually the same place. So once you exit this level, I need an extra step to expunge its datafiles from your saved game — reset the fog of war, reset away all the wreckage, corpses and dropped items. You’ve never been here before, this is not the level you’re looking for. (Hint: make sure you don’t leave anything good behind!)

In terms of who you face in that random battle… That’s back to being easy again. I already have an “Encounter” system to populate the blobs of enemies you might have seen in the wilderness around the Exon Academy, so all I need to do is plop one of those down and let it fill in all the enemies naturally (albeit without respawning them later, once they’re all dead).
Although… The final angle for consideration is that I don’t necessarily want to make every random encounter an ambush. The world of exon includes Peddlers, travelling merchants who walk these roads too, and who should be more than happy to offer their wares to friendly passers-by. Maybe a Peddler is just another kind of Encounter, composed of only one dude who talks rather than four ravenous transgenics? Hmmm.
Maybe I’ll finish this off and then unlock travel to the Friendly Arm Waystation in the demo, but give you a 100% chance to be waylaid en route. Tee hee hee.