Question

I'm starting to go into random world generating, I have an idea on how random number generating works (Actually pseudorandom numbers), but I don't have a good idea of how to make the world look "nice", in other words not just place blocks based on a random x, y that it gives me, but make it look smooth.

This will be a 1 time generation per world. So everything is created at start.

I was thinking of an algorithm a few moments ago, but the problem is that it would just use be an endless amount of nested if loops, which would probably take a more than the necessary time. I was thinking of the following:

  1. Choose a random location on the map and place the spawn point in that location.
  2. Start building the street based on the spawn location, like if the spawn location is 16 spaces near the edge of the world build a house, otherwise start building a street.
  3. Based on the previously generated street's place structures around.
  4. Place misc.

Conceptualizing the algorithm isn't much of a problem, what I'm having difficulty with is starting the actual code from step 2 and below. Based on the above algorithm or an algorithm you think of, how would you start the code? I'm not asking for the actual code to be made, just an idea of how it would look.

I know this question isn't precise and can have multiple answers, but I've seen many questions similar to this one having a strange approach.

Was it helpful?

Solution

hmm looks like planar(or cubic) map filling. from my point of view firstly you need some databases

struct _object
    {
    string name,help,info; // texts for later use
    int siz[3];            // grid size of object
    int pos[3];            // placement pos (center or what ever)
    // other stuff like: mesh-id,color,hit points,...
    };

struct _dependency
    {
    int objid
    List<int> near; // can be near this objects (can add probability)
    List<int> far;  // cannot be near this objects (can add probability,min distance)
    };

List<_object> object;      // DBS of all object types
List<_dependency> depend;  // DBS of object dependency

Then you need to initialize this DBS from ini files or whatever. After that you need to create world map. For simplicity let it by only a single squared town and single layer(no sub-terrain), size and placement can be random.

List<_object> obj;   // DBS of placed objects, could be lesser derivate of previous _object to conserve memory requirements
const int N=100;
int[N][N] map;       // placement map, containing placed obj id, or -1 for empty space

so now you need some town generating function that fills map[N][N]:

void genere()
    {
    int i,j,x,y,xx,yy,objid,objix;
    int _min_complexity=N/10; // this can also be random
    int _max_complexity=N;    // this can also be random
    // clear map
    for (i=0;i<N;i++)
     for (j=0;j<N;j++)
      map[i][j]=-1;
    int complexity=_min_complexity+random(_max_complexity-_min_complexity);
    for (i=0;i<complexity;)
        {
        // random placenet position
        x=random(N);
        y=random(N);
        // random object, should take in mind object[].near and closest objects in map[y][x]
        objid=random(object.num);
        if (check if map[][] is empty enough to place object[objid] to x,y,z)
         if (check if near x,y position is not bad type of object already object[].far)
            {
            // add new object to list
            objix=obj.add(object[objid]);
            // add new object to map
            int *siz=obj[objix].siz
            int *pos=obj[objix].pos
            x+=pos[0];
            y+=pos[1];
            for (yy=0;yy<siz[1];yy++)
             for (xx=0;xx<siz[0];xx++)
              map[y+yy][x+xx]=objid;
            i++;
            }
        }
    }

also the position can be double[3] + orientation matrix and map coordinates will than be aligned to grid. There are many ways to tweak this code, its just an starting template. Hope it helps a little.

P.S.

List<type> l;
l.num - number of items in list
l[i]  - i item from list
i=l.add(a) - add new item a to list and returns its index
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top