Random Tile Generation Using Arrays

Non-platform specific questions.

Random Tile Generation Using Arrays

Postby DragonRift » Thu May 16, 2013 9:53 pm

Is there any examples around on doing random tile generation using arrays in Game-Editor?
Last edited by DragonRift on Fri May 17, 2013 1:40 am, edited 1 time in total.
DragonRift
 
Posts: 70
Joined: Mon May 13, 2013 5:05 am
Score: 0 Give a positive score

Re: Rantom Tile Generation Using Arrays

Postby skydereign » Thu May 16, 2013 10:20 pm

Do you actually mean random? If so, here is an example, though not very useful.
Code: Select all
int array[10];
for(int i=0; i<10; i++)
{
    array[i] = rand(5); // fills array with random numbers 0-4
    // could also create actual tile actors here, and set their animpos to array[i]
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Random Tile Generation Using Arrays

Postby DragonRift » Thu May 16, 2013 11:36 pm

Last edited by DragonRift on Fri May 17, 2013 1:41 am, edited 1 time in total.
DragonRift
 
Posts: 70
Joined: Mon May 13, 2013 5:05 am
Score: 0 Give a positive score

Re: Random Tile Generation Using Arrays

Postby DragonRift » Fri May 17, 2013 1:39 am

skydereign wrote:Do you actually mean random? If so, here is an example, though not very useful.
Code: Select all
int array[10];
for(int i=0; i<10; i++)
{
    array[i] = rand(5); // fills array with random numbers 0-4
    // could also create actual tile actors here, and set their animpos to array[i]
}



I can't seem to get anything to work from this example.
DragonRift
 
Posts: 70
Joined: Mon May 13, 2013 5:05 am
Score: 0 Give a positive score

Re: Random Tile Generation Using Arrays

Postby DragonRift » Fri May 17, 2013 5:08 am

After many hours with work, I made some headway:

Code: Select all
//Step 1: Create array
int tilegen[100][10];
for(w=0; w<100; w++) //Initiate for loop for width
{
    for(d=0; d<10; d++) //initiate for loop for depth
    {
    if(tilegen[w][d]>0)
    {
      CreateActor("Dirt_Tile", "DirtTile", "(none)", "(none)", w*32, d*32, true);
    }
    else
        CreateActor("Stone_Tile", "StoneTile", "(none)", "(none)", w*32, d*32, true);
    //finalcount++; this is a test to determine loop is run.
    }
}
//sprintf(text, "%d", finalcount); This is an output loop test result.
Attachments
buildworld.jpg
DragonRift
 
Posts: 70
Joined: Mon May 13, 2013 5:05 am
Score: 0 Give a positive score

Re: Random Tile Generation Using Arrays

Postby DragonRift » Fri May 17, 2013 3:28 pm

After much thinking, I have decided that I am going to try and push this farther.

I want to expand/redesign the code to a more fractal based landscape but then it comes into the question of how am I going to loop the east/west ends of the world to "appear" to be seamless.
DragonRift
 
Posts: 70
Joined: Mon May 13, 2013 5:05 am
Score: 0 Give a positive score

Re: Random Tile Generation Using Arrays

Postby skydereign » Fri May 17, 2013 6:36 pm

DragonRift wrote:how am I going to loop the east/west ends of the world to "appear" to be seamless.

You would have to create enough overlap on both sides of the level, so that they can move half a screen to the right or left of the limits. When they do, warp them to the other side of the level.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Random Tile Generation Using Arrays

Postby DragonRift » Fri May 17, 2013 7:03 pm

skydereign wrote:
DragonRift wrote:how am I going to loop the east/west ends of the world to "appear" to be seamless.

You would have to create enough overlap on both sides of the level, so that they can move half a screen to the right or left of the limits. When they do, warp them to the other side of the level.


I am likely going to have to change my entire game design because this just will not work for me.
DragonRift
 
Posts: 70
Joined: Mon May 13, 2013 5:05 am
Score: 0 Give a positive score

Re: Random Tile Generation Using Arrays

Postby skydereign » Fri May 17, 2013 7:13 pm

DragonRift wrote:I am likely going to have to change my entire game design because this just will not work for me.

Which part? Procedurally generated levels, or looping? Both are doable in gameEditor, though procedurally generated levels is not something you can easily do, even with a substantial coding background.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Random Tile Generation Using Arrays

Postby Hblade » Fri May 17, 2013 7:23 pm

Dragon, your going to run into massive slowdown unless you come up with a way to store the locations somehow and delete the tiles, then recreate them. Even if they're off screen and visually set to 0(disabled) this could cause atomic slowdown. 100x100 (10,000 tiles) causes massive slowdown by 30FPS (60 to 32 ish), much less infinite world :P But then again, 10000 tiles is a poop load of tiles. and I had air tiles so that could also have been an issue
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Random Tile Generation Using Arrays

Postby DragonRift » Fri May 17, 2013 7:29 pm

Hblade wrote:Dragon, your going to run into massive slowdown unless you come up with a way to store the locations somehow and delete the tiles, then recreate them. Even if they're off screen and visually set to 0(disabled) this could cause atomic slowdown. 100x100 (10,000 tiles) causes massive slowdown by 30FPS (60 to 32 ish), much less infinite world :P But then again, 10000 tiles is a poop load of tiles. and I had air tiles so that could also have been an issue


My test on the current code gets nearly 250 FPS :p

meanwhile the new code I am trying to get working in my env is:
Code: Select all
    /* Define these variables at the top before you begin
     
    numberOfWaves - Float   // Number of waves you wish to add to the terrain
    Amplitudes - Float[]   // Array of Amplitudes of each wave.
    Frequencies - Float[]   // Array of Amplitudes of each wave.
    PhaseDifferences - Float[]   // Array of Amplitudes of each wave.
     
    baseHeight - float // the height of terrain to begin with before adding sine waves.
    terrainHeight - Float[] // stores the height of terrain at each value of X
     
    i = 0 // used to run through for loops, just a temp variable.
    j = 0 // same as i.
     
    blockPrefab - transform // holds the prefab of the block that will be created when generating the level.
     
     
    */
     
    // First we start by generating a random array of amplitudes frequencies and phase differences to randomize a set number of waves.
    Amplitudes = new float[numberOfWaves];
    Frequencies = new float[numberOfWaves];
    PhaseDifferences = new float[numberOfWaves];
     
    for(i = 0; i<numberOfWaves; i+=1)
    {
        // now we set random values in set ranges. These ranges can cause really bumpy terrain if not chosen carefully.
        Amplitude[i] = Random.Range(minAmplitude, maxAmplitude);
        Frequencies[i] = Random.Range(minFrequency, maxFrequency);
        PhaseDifferences = Random.Range(minDifference, masDifference);
        // true minimum phase difference is 0, and max is 2*Mathf.PI, Due to the nature of the Sine Wave.
    }
     
    for(i=0; i<maxWidth; i+=1)
    {
         terrainHeight[i] = baseHeight;
         // first set the base height of the terrain
     
         for(j=0; j<numberofWaves; j+=1)
         {
              // Now we add on each wave height in turn.
              terrainHeight[i] += Amplitude[j]*Mathf.Sin(Frequency[j] + PhaseDifference[j]);
         }
     
         for(j = terrainHeight[i]; j > 0; j-=1)
         {
              // Create Block, or modify terrain mesh here...
              // so for creating blocks we would do:
              Instantiate(blockPrefab, Vector3(i, j, 0), Quaternion.identity);
         }
    }


This has not been tested yet, nor is it ready to compile in the engine.

Essentially before designing any actual game, I am running a series of tests and experiments, developing Re-Usable Systems & Technologies.
DragonRift
 
Posts: 70
Joined: Mon May 13, 2013 5:05 am
Score: 0 Give a positive score

Re: Random Tile Generation Using Arrays

Postby DragonRift » Fri May 17, 2013 9:03 pm

ok, I am working through porting that code into GE and I have run into an issue:

Code: Select all
float NumberOfWaves;
float Amplitudes[];
float Frequencies[];
float PhaseDifferences[];

float BaseHeight;
float TerrainHeight[];
int i = 0;
int j = 0;


this section of code works but as soon as I do this:

Code: Select all
float NumberOfWaves;
float Amplitudes[];
float Frequencies[];
float PhaseDifferences[];

float BaseHeight;
float TerrainHeight[];
int i = 0;
int j = 0;

Amplitudes[NumberOfWaves];


The script is now asking for a pointer but when I do this:

Code: Select all
float *NumberOfWaves;
float Amplitudes[];
float Frequencies[];
float PhaseDifferences[];

float BaseHeight;
float TerrainHeight[];t;
int i = 0;
int j = 0;

Amplitudes[NumberOfWaves];


It says that I am declaring the code wrong, can someone please assist?
Last edited by DragonRift on Fri May 17, 2013 9:12 pm, edited 1 time in total.
DragonRift
 
Posts: 70
Joined: Mon May 13, 2013 5:05 am
Score: 0 Give a positive score

Re: Random Tile Generation Using Arrays

Postby skydereign » Fri May 17, 2013 9:11 pm

If you want to declare a variable as an array, you need to do so in the same line as you declare it.
Code: Select all
float Amplitudes[10];

Note, you need to put a constant int for the size of the array. If you want to have variable sized arrays, you'll need to use a pointer, and dynamic memory allocation.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Random Tile Generation Using Arrays

Postby DragonRift » Fri May 17, 2013 9:13 pm

This says incompatible Type, but if I change it to int it works. However I think I am going to have to use a floating point variable to do that code I am porting... am I not correct?

Code: Select all
float NumberOfWaves;
float Amplitudes[0];
float Frequencies[0];
float PhaseDifferences[0];

float BaseHeight;
float TerrainHeight[0];
int i = 0;
int j = 0;

Amplitudes[NumberOfWaves];


Then when I get here:

Code: Select all
int NumberOfWaves;
int Amplitude[0];
int Frequencies[0];
int PhaseDifferences[0];

int BaseHeight;
int TerrainHeight[0];
int i = 0;
int j = 0;

Amplitude[NumberOfWaves];
Frequencies[NumberOfWaves];
PhaseDifferences[NumberOfWaves];

    for(i = 0; i<NumberOfWaves; i+=1)
    {
        // now we set random values in set ranges. These ranges can cause really bumpy terrain if not chosen carefully.
       
        Amplitude[i] = random.range(minAmplitude, maxAmplitude);
        Frequencies[i] = random.range(minFrequency, maxFrequency);
        PhaseDifferences[i] = random.range(minDifference, masDifference);
       
        // true minimum phase difference is 0, and max is 2*Mathf.PI, Due to the nature of the Sine Wave.
    }


I have problems saying undeclared identifier for random.

I have had nothing but problems getting random operation to work in GE Scripts.

Then I go like this:

Code: Select all
int NumberOfWaves;
int Amplitude[0];
int Frequencies[0];
int PhaseDifferences[0];

int BaseHeight;
int TerrainHeight[0];
int i = 0;
int j = 0;

Amplitude[NumberOfWaves];
Frequencies[NumberOfWaves];
PhaseDifferences[NumberOfWaves];

    for(i = 0; i<NumberOfWaves; i+=1)
    {
        // now we set random values in set ranges. These ranges can cause really bumpy terrain if not chosen carefully.
       
        Amplitude[i] = rand(10);
        Frequencies[i] = rand(10);
        PhaseDifferences[i] = rand(10);
       
        // true minimum phase difference is 0, and max is 2*Mathf.PI, Due to the nature of the Sine Wave.
    }


and things compile.
DragonRift
 
Posts: 70
Joined: Mon May 13, 2013 5:05 am
Score: 0 Give a positive score

Re: Random Tile Generation Using Arrays

Postby skydereign » Fri May 17, 2013 9:49 pm

DragonRift wrote:This says incompatible Type, but if I change it to int it works. However I think I am going to have to use a floating point variable to do that code I am porting... am I not correct

As I mentioned, you need to put a const int for the size of the array. This means you either need to use a literal int (like 10, or any other number) or a const int. Essentially you need to specify a fixed size for the array at the point that you declare it (before the game even starts).
Code: Select all
const int size = 10;
int array[size];

It sounds like though you want these arrays to change size within the game. If that is the case, you need to use pointers and memory allocation.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Next

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron