Friday, March 31, 2006

Her smile was not a smile

More town generation. Yes it seems as though I've found my new full time hobby.
I've been wrestling a bit with keeping things modular.

At the moment I'm moving towards building generation. I want to have buildings constructed gradually over time. This means careful organization of the code. How the buildings themselves are generated is handled by some interface so there's potentially quite a few methods I could employ. For now the only method I'll be doing is the exicting "variably sized rectangle". First it will be in ascii too.

The test I've just written shows that a city can make trade goods and this will increase it's resources each turn ... which is nice but quite far away from being useful. I'm looking forward to writing the city management AI.

No links on the web have caught my interest lately :( So no links with this post.

Wednesday, March 29, 2006

Town generation

The town generators taken a clumsy step forward but it's still needs a little polish at the base level. Also I haven't even started on buildings, infact it's taken one and half days to knock the first item off my to-do list, it seemed so simple as well ...

Metadata is still non-existant for the city, and I'm happy to ignore it for now. It will be needed later but it's a bit too scary for me to have a go at yet. Nest up is building generation which I think with be certain kinds of exciting box for now ... though it would be cool to grow buildings in a fractal way like using the Koch one for walls.

The priority is getting towns out of the console into the world of loving 2D textures.

EDIT: A little later

I'm now onto building generation.
I want to know a couple of things but basically when I'm in a building, or something is in building. This means the building needs some type of region data.

(I could just mark things in when they pass through one of the doors but what about teleporting and that type of thing - region intersection is best.)

It would be boring world if all my buildings where square blocks, therefore I need to generate the buidling region in some sort of bounding polygon. I'm guess this is pretty easy and I'll just have some the scurries around the inner outline of the building and marks points where it has to change direction, and there's the polygon.

As for the buildings themselves... how to create interesting buildings? I thought maybe sometype of growth.

# -> # # -> ... continued growth and deletion of it's self.

A cellular automata approach I guess. Or I could slap together premade parts like circles and squares.

Also I thought of throwing out four or points, that must land some certain distance apart and then, join those points up with lines. That could give some interesting buildings.


Also partial tests seem to me, the most logical place to put unit tests. What them in big #ifdebug and forget - access to all the private methods, so they can easily be tested to without having to make everything protected, if your going to inherit from the class, or worse internal or public. Currently my tests are in totally seperate but I don't think they will be for long.

Monday, March 27, 2006

Eight out of ten kittens

I went to Kyoto over the weekend but I'm back now and doing some work.

This week I created a meta-grammar that produces a grammar to be used for language, name generation and that sort of thing. It's ok but need more restriction so that the artifical language looks richer.

Today back to working on city generation.

Sunday, March 19, 2006

Did Jesus poo? (if so, was it, or is it still, magic?)

I have pile of notes about writing my city generation code in a modular manner. But there's not much actual code. I've got a very simple line plotting demo.

I want my cities to have some sense. I also want to generate meta data - what's a building, what are walls, what are rooms, a court yard, a road, an alleyway - etc etc. This is too much work to do in one coding swoop, rather I'll do one little piece and then add the rest in increments.

The simplest city generation code I can think of is to have a box drawer on a tile map. You give it coordinates and dimensions.

DrawABox(int startX, int startY, int width, int height) {//...}

Then it can be loaded with constrained random parameters and whoo boxes everywhere, some even overlap. But wait doors! How will our little NPCs get into their houses in New Megasupraopolis?

If you record each point where you place a wall then our function can return a list of it's wall-tiles.

List DrawABox(int startX, int startY, int width, int height) {//...}

One more step and we could add doors - of course we need nicely placed doors.

A 3x3 room A poorly place door

### ###
# # # #
### ##+

So we need a test for corner positions.

bool IsCorner(Point suspectWall, List walls) {//...}

Then with this we can add a door to each random generated room.

Point MakeDoor(List roomWalls)
int r = random.Next(roomWalls.Count);

while(IsCorner(roomWalls[r], roomWalls) != true)
r = random.Next(roomWalls.Count);

return r;

You might want to add code to ensure the loop terminates under crazy conditions where you've managed to create a room of corner tiles.

And with those functions, there's your simple city.

Real cities generally have more structure though - think Sim City. There are important key buildings, and there are roads that buildings are lined up against.

Anyway I'm getting off track. All I've done this weekend is make a new grass tile, which is probably less attractive than the previous grass tile :D I'm currently deciding how big I'm going to make my building walls.

I also need to create a list box in my GUI. Tuesday is a bank holiday, so this is the day I'll probably get cracking.

I need a goal for Tuesday - 9 new tiles: dirt, and 8 grass dirt transitions (maybe some flipping, if I get bored!). a city generation frame work and some rudimentary city generation, a city management structure (linked into the turn scheme).

Usually I like one goal ... so there are three here, I'm being ambitious :D Tile art will probably cause me the most grief because I'm so out of touch with it. (To be honest I was never really in touch with it)

Also a few days back I got fullscreen back in, so that's nice.

(all code here was written off the top of my head - if it doesn't compile, there's the reason!)

Saturday, March 11, 2006


This weekend I want to combine my two region classes and have them work together nicely. I need to clean one of the menu classes up a bit and some crude character creation code.

I think it's important when coding something large to strive to always be in a state of "playable" or "finishable". That is you can always do a run through of your game no matter how crippled parts are. I think in the pragmatic programmer this is refered to as using a Tracer Bullet

I also need to develop the IO a bit more.

The main body of the code is under source control now, which is nice. I'm using subversion and the tortoise tools.

This morning I'm just reading a bit more of code complete.

Monday, March 06, 2006

Poorly named animals

I've been pushing my new world generation into my main code.
It's actually gone suprisingly smoothly just dropping the classes in adding a superclass and changing one line of code.

Unfortunately tile generation seems to have been broken at some time in the past - it also looks like it needs a bit of rewriting anyway.

Oh and's class designer is pretty good for documenting how code works. It's extremely rough though (its seems very hard to delete things or get rid of inheritance arrows you don't want because they're just clutter and not relevant to whats being explained), and it's quite slow. But because it's linked into the code it's theortically always up to date.

Also, I'm curious if there are any premade virtual file systems coded up in C#? I want one but don't want to write it.

Sunday, March 05, 2006

Friends of cat milk

I've been re-architecturing the code. I'm in stage 3 of my game at the moment. Each stage is a quite vague but large chunk of the game. Chunk 1 was pretty much Game Programming For Monkeys, Chunk 2 was a GUI system and getting display bits perfect.

The goal for this particular chunk is cities and talking NPCs. The NPCs will talk but you won't be able to join in the conversation. According to my todo list this goal is really close, luckily I know better it's actually miles away!

Recently I finished the city placement alogrithm - it's good but has a few flaws. Here's some psuedo code:

Before I even start, my mapdata goes through a preprocessing stage
where all landmasses are picked out from the sea. Then the
landmassesare sorted into continents, islands and desert islands
- according to some fuzzy-rules.

Once that's all done I'm ready to place my cities.

First I try to place a city on each continent.
If I run out of cities then I return success.
If I run out of continents then onto the next phase.

I make a copy of all the continents.
Each copy is list of points where a city can be placed on a
continent. I go through the cities I've already placed and
remove these positions from the copied-continents, I also
remove the all tiles in a 9 square radius.

From these copies I then randomly pick positions.
I place the city at the picked position and then remove its
postion and a 9 square radius footprint from the continent copy.
If all cities become placed I return a success.
Otherwise when all the position continent copies are empty
then I reduce the number of citys to match what I've place and
I return success.

Notes: I don't even try to place on islands and some of those
can be quite big. Otherwise it's not a bad algorimth.

My current implementation does actually pick islands as well but only one city per island and it favours islands towards the north of globe. Anyway the results are nice.

I need to patch that generation algorithm into my main code but before that I'm poking around. In my game menu when I'm selecting what game to play - I want to be able to see a preview of the world. Therefore I need to be able to get the data much higher up in my code. So shuffling a'plenty.

Also it's getting nearer and nearer the absolute end of putting off some new tile art. So I'm going to have to have a go at the soon.

There's an interesting thread about Random City generation over on

Also from the C# Faq, a question I was recently pondering.

How are return values from a delegate handled?

Q: How are multiple return values from a delegate handled?

In C#, it's possible to write a delegate such as:

delegate double GetResult(params double p);

If there is more than one method on this delegate, there are multiple return values.

A: In this situation, the return value is the value that is returned from the last delegate.

If you want more control on this, you can call GetInvocationList() on the delegate, which will allow you to call each method individually, and process the double values appropriately.