#4? Where’d #3 go? Post #3 would be useless because, from where I finished at #2, I’ve completely rebuilt the code in a separate file. Refactoring, joy.
Last time I posted, I had an algorithm that was reasonably good at generating a network of hallways. I don’t think I have any screenshots, but with some edits to the tileset, you can get some really wacky maps generated out of it, one of which could work as a ruined set of roads that I might save later, though I don’t have a picture of it. I’ll probably stash away that code file for later.
Put simply, most of the previous code followed this basic pattern:
make a matrix, and randomly assign an index to it, with the index corresponding to a certain image of a predef.
afterwards, go through and check each matrix cell for connections between predefs that should be there, and connect them.
The important part about it was that each index was a binary number from 0001 -> 1111. When you introduce in rooms, however, that scheme needs to be so modified that my “randomly do it and fix it later” approach fell to the ground. Not only do you have connections for 0 and 1 (nothing and hallway), you’ve got 2, 3, and 4 (room lower, room upper, room filled), and so the numbers just get rather large when trying to do it the old way. What I’ve got now, looks like this:
make a matrix of zeros set the corner tiles to something that does not leave the grid and has 2 connections going inside for each tile in the top row: pick a tile whose connection type (0,1,2,3,4) on the left of the tile matches that of the left tile's right side for each tile in the left column: [repeat, checking upper tile instead of left tile] for each tile inside the bounds of the matrix: [again repeat, checking upper and left tile's connections so they match up] for each tile on the bottom row: [repeat, checking left tile, upper tile]
and so on, until the matrix has been filled, then draw the appropriate tiles to the screen in the right positions. So, instead of doing it all and checking it later, we’re immediately putting in a tile that is known to fit the previously known connections. What really delayed me was that the corners, top, left, right, bottom, and inside all check for different things–the top tiles have to set themselves to be a good tile who’s top is a 0 and left matches the adjacent tile; the inside tiles have to check the left tile and the top tile; the bottom row has to have a bottom of 0, match the top, and match the left tile, and so forth. As a result there wasn’t a very easy way for me to take all four of those action and combine them into one thing.
What’s the result now? Here are some shots of the same code, but with edits to the tileset.png part:
There’s a few glitches in part because sometimes, it’s impossible to connect the right tile–that combination just can’t exist, so you get a few gaps there when defining say, the walls in that second picture in the bottom right–optimally all the white should be outlined by red. Eventually I might fix that, either by adding in those “impossible tiles” into the set or something else, though that’ll be saved for later. I’ll also have to re-check the code once more to see if there isnt just a simple glitch that’s getting in the way, too.
Regardless, the results themselves look nice to me. I have coded in place a way to (somewhat painstakingly) add in additional tiles. Compared to the generator that gave me the idea, if you select only Dungeon 1 tiles, the results are pretty close. If I want to change the distribution and say, add more hallways than rooms to a map, I could always add in additional hallway predefs, so that the random.choice will be more inclined to pick hallway connections than room connections. Likewise, the pictures above show how altering a few of the ~40 or so tiles can make the map look significantly different.
Where will this little thing go next? I probably won’t bother spending a post writing about it, but pygame has a surfarray subsection to the module that lets me convert pixels on an image into a numpy matrix. From there it’s rather trivial to integrate what I’ve got into the standard Map.py set of code I’ve got and get it playable.
Code of this and the dialogue system will be up on my other site..eventually.