Lachlan's misadventures in games programming

Monday, 26 September 2016

Golf, The RPG: Procedurally Generating a Golf Course

9/26/2016 09:46:00 pm Posted by Lachlan , , No comments


So – I’ve been working on a game in spare time for a while, that (as always) may or may not end up amounting to much. But, I’ve done some interesting work that I’d really like to share – such that anybody with an interest in procedural generation or programming might be able to glean some insights.

The game I’ve been working on has (thus far) been called Golf, The RPG. It is intended on being a top down, roguelike inspired, procedural , Golf RPG with interactive fiction elements. The idea is that I want a golf game where the shots are chosen by the player,  but the success the shot is determined by the skill of the golfer as opposed to a player-dexterity game like most conventional golf video games. The second idea is that when I personally golf, I care more about the time I spend talking to the people I’m playing with then the golfing itself – and I wanted to try to incorporate that into a game.

The part that this series of articles is going to be about is the process of procedurally generating a golf course. Bit of the approach might change and be refined as the game is developed - but you can see some of the thinking of how it happened.

This is the first of what will be hopefully 5 articles, appearing sporadically throughout the next fortnight.

Procedural generation - A Brief Introduction

So - what exactly is Procedural generation? If you've already got a good idea, this is the chunk to skip.

Procedural generation is getting the computer to do some of the creative work that a human would normally do in a conventional game. Sometimes, procedural generation is used for small bits of a game - such as some of the textures, or some of the sound. Sometimes it is used for level design. Sometimes, it's used as the basis of the entire game (such as No Man's Sky - which recently brought procedural generation to the limelight).

Rogue: The classic game, with procedural dungeons
My interest in procedural generation started with ADOM - a Roguelike game. Roguelikes are a genre of game, usually defined by procedural generation of levels, and permanent death. The procedural generation of levels means the game can stay interesting for a long time - because you can always keep exploring and finding new things, or face new challenges.

Atlas Warriors: My less classic game with procedural dungeons, and procedural fire animations!

Procedural generation generally works as a set of rules that the computer has to follow to generate a level. In Atlas Warriors (above), generally speaking, rooms are placed next to other rooms if they fit, and door ways are randomly added. Groups of enemies are added, in such a quantity to be about as difficult as the player should be able to handle at that level. A couple of enemies are forced to appear - such as the [spoiler] near the end, and finally the [spoiler].

The Chase: My other less classic game with procedural level generation!
In The Chase (above), trees are randomly placed in such a way that a player can always walk past those trees, with a gap guaranteed somewhere near the gap in the previous column of trees. 

Obviously - the universe generation in No Man's Sky is way more complicated, but you can see the basics of how it might work.

Let's Build A Golf Course!

I've taken lots of shortcuts. My golf course generation algorithm in no way replicates the thought process of an actual human golf course designer. Instead, I looked at the output I wanted to get, and thought through the process of how I might get something that was similar.

The end results

So you can see where we're getting to - below are some examples of the kinda of courses that I'm able to generate.

A procedural golf course.

Yup. That's a golf course. Some dangerous water though.

Guess What: It's a golf course!

The Broad Process

Broadly speaking, this is how Golf, The RPG generates a golf course:

  1. Plant some vegetation
  2. Lay the basic shape of some holes over the course
  3. Figure out the order of the holes
  4. Fit some water in, if possible
  5. Do the low level detail of the holes

Step 1. Planting Vegetation

Planting vegetation is a super simple step. I'm even willing to share the entire source code for it. (But... it should be noted... it's entirely possible I'll share the rest of the source later anyway)

        // Start with rough
        for (int ix = 0; ix < c.Width(); ++ix)
            for (int iy = 0; iy < c.Height(); ++iy)
                c.SetTile(ix, iy, 2);

        // Put some basic fawna around
        // We'll get this into a configuration option
        for (int ix = 0; ix < c.Width(); ++ix)
            for (int iy = 0; iy < c.Height(); ++iy)
                // 50/50 chance of some fawna
                if (randf() < 0.5)
                    char fawna_type = 0;
                    switch (randi(0, 4))
                    case 0:
                        fawna_type = VEGETATION;
                    case 1:
                        fawna_type = ROUGH_SHRUB;
                    case 2:
                        fawna_type = ROUGH_TREE;
                    case 3:
                        fawna_type = BIG_TREE;
                    case 4:
                        fawna_type = SMALL_TREE;
                    c.SetTile(ix, iy, fawna_type);

If you don't read C++, it's pretty simple: Place some rough everywhere, and then randomly place vegetation, shrubs, trees, bigger trees and smaller trees. That's it. We're starting with an unmolested block of empty land: Just a like a real golf course!

So - we've so far got the whole level looking something like:


Next article - I'll be looking at how I do the broad layout of the holes.


Post a Comment