Wednesday, 2 June 2010

Roguelike, part 2: Random rooms

OK, so things are taking shape! I figured that a great starting point for this would be getting single room instances working first, which is what I've managed to get done.

I went for an array-based grid idea, based on this tutorial. Getting a hard-coded room was easy enough to do, all I needed to do was loop through each row vertically and then sub loop horizontally, testing for the numbers that apply to each tile type (currently only "wall", "floor" and blank tiles for use in gaps). This hard-coded approach (combined with Nethack tiles) gave me this:


This looks great and all, but it wouldn't be a rougelike without some serious randomness at work. Luckily it was pretty simple to create a suitable array. In fact, generating the level was more challenging in terms of me visualising it in my head, than actually coding it. I'd originally thought along the lines of randomly generating the top-left and bottom-right tiles, then drawing the room between them, but this was really difficult straight away. Not only that, but it would have meant that I would have much less control over individual tiles (more on why this is important later). Anyway, as with most things, the best answer (that I know of so far) was the simplest one: generate a random room width and height, and go from there.

That makes it sound so simple, in reality it's *slightly* more complicated but still not too scary:
- Create a whole row of wall tiles for the top row (set to our random width)
- Create a bunch of middle rows (going downwards vertically), each with a wall tile at either end
- Create another whole row of wall tiles and add it to the bottom of the stack, to close the room

Like so:


Once this was done, the array could be passed into my tiling system and whammo: one random room.

Looking at the code, I think I could automate it even further, but as it stands at the moment this double-loop system means I have access to every single tile in order as it's created. Why is this important? Because the next stage is to add doors/stairs/obstacles to my rooms, and for that I need to know exactly where I can and can't add stuff.

Oh, and the player avatar is randomly placed too, and is added on the basis of what tile type it's been spawned above (for example, the player will never spawn on a wall tile). However I don't think I'm doing it entirely correctly, and need a much more robust system of creating and spawning objects, which is a whole different post entirely (not to mention a long way off from where I'm at now!)

I'm sure that any hardcore roguelike devs reading this will be laughing at the simplicity of what I've achieved so far, but at the moment these concepts are all pretty new to me,  so personally I'm glad it's taking shape so quickly... I'm sure I'll be tearing my hair out further down the line, but at the moment it's looking good.

The next step will be to create entire dungeon floors: first I want a couple of non-overlapping rooms spawned each time, and then a corridor system to link the rooms up (which I'm sure will be a nightmare to write, or so I've been told). After that will be basic character movement, then I'll add stairs so the basic structure of the dungeon will be complete before I start going completely mental with monsters and items and stuff.

If anyone has anything to add (i.e. if I'm doing it all wrong) then I would love to hear it, next time I'll be posting actual code too.

The quest continues!

No comments:

Post a comment