keeping track of randomly generated dynamic objects

Started by
6 comments, last by detlion1643 12 years, 7 months ago
I'm not sure which sub-forum this post would be best in wink.gif.

I've developed a random tile based generator (not a tile map persay), but a random assortment of tiles that generate a path.

It places an image (the tile) at a fixed position in the center of the screen, then chooses a random left/right/up/down choice to place the next tile. From that newest placed tile, it chooses another random left/right/up/down choice and places another tile, etc, etc...

Now, I have found that since the tiles can go in any direction, I don't think I can just create an array and keep track of placements, since I don't know what shape the tiles will make up (no idea how big to make the array). Any options for easy tracking of this (as of right now tiles can overlap)?

Ex) Tile starts at 1, moves to 2, moves to 3, moves to 4, can't move back to 3 or on top of 1 at this point? I'd like to avoid checking all tiles in a loop for locations (as a max number of tiles is constantly changing).
1 - 2
4 - 3

Also, I would like a way to see if I can keep some tiles more block shaped (so it's not just a single path)?

Ex) Sometimes I get stuff like this:
1-2-3
----4-5
------6-7-8-9
When I'd rather have something like this:
1-2-3
--5-4
--6-7-8
----0-9



private void gridjumpergeneratelevel(int tilesmaxwidth, int tilesmaxheight, int tilesmax, int screen)
{
Random randomnumber = new Random();
for(int i=1;i<=tilesmax;i++)
{
int randomnum = randomnumber.Next(1,4);
bool tileplaced
if(i==1) {} //first tile (place at predefined x,y)
else{
do{
//check random number, 1=top,2=left,3=right,4=bottom
//***** Notice, NO Previous Detection Yet ******
switch(rrandomnum){
case 1:
//take previous tile y loc and -120 for new tile
tileplaced=true;
break;
..
}
}while(!tileplaced);
}
}
}

That's essentially a breakdown of the main parts of the function...
Advertisement
It depends on what your goal in setting this up is, but it shouldn't be too hard. Implementation will vary depending on what language you're using (c#? Just a guess), but this is what I would do generally:

Initialize a 2-D array with all tiles initialized as dummies. Then place your first tile, and use a weighted random function to determine the direction of the next tile to promote overall layouts to your liking.

A switch statement can take the direction determined by the weighted randomizer and mark the next tile as active. Then rinse and repeat.

I'm at work now, and I can't write out code well on my phone, but I can give more detail and example code when I get home if you want.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

Yes, using c#. Let's look at the first part of the problem... I'll ignore the optimized path directions for now and focus on not overlapping the tile placements.

If I don't know how wide or tall the "tile area" is, how can I initialize an array to keep track of it:

Maybe I'll try looping through all the previously placed tiles and comparing positions, but that would be bad once I start placing 30+,40+,etc tiles...
array[,], if starting tile is at array[4,4], what happens if the tiles happen to go left 5 times? I'd be at array[-1,4], is that possible? It's not feasible to keep track of array[,] and start at array[250,250] just to make sure it won't go outside the array bounds, is it?
Hmmmmmm.

I'm admittedly not very great when it comes to datastructures and algorithms (and this will be one of those sort of solutions), but maybe a hash table would work well if this thing grew to a large size.

If memory's not a tremendous concern, you can just throw a 2D array at it. It would certainly be the simplest solution. I know you keep saying, "I can't", but answer this.

Does your world have a finite, maximum size? It doesn't matter what its current size is, but does it have one?

If so, you can allocate an array that is the biggest your world could possibly be and use that. It doesn't matter if you end up wasting a ton of memory so long as you don't run out of memory. It's one of those tradeoffs; memory consumption for processing speed. Before you start thinking "It's too darn inefficient!" actually code it and test it. A simple approach like this shouldn't take long to do, and if it doesn't work you can bring out bigger, more time consuming solutions. If it works, awesome! You're done and you save yourself a lot of time.

If we're talking some sort of infinitely sized world, then maybe you can use asset streaming to stream in chunks of the path from secondary storage. Most large world games have to do this, since there is a practical limit to how much data you can cram into RAM at once. In memory, you'll use a big 'ol array or something to hold the chunks of your currently loaded level segments. As the player walks along, you load in more chunks of the path ahead of them so they never see the transition.
Just store a list of coordinates where you have already placed tiles. As you place a tile, append an entry to the end of the list.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

The world size is never defined, but it won't take much memory or processing time. Right now, I'm just moving an image (character) around surrounding images (tiles) not in 3d (just pngs)...

Apoch, that was my initial thought, but then I gotta split off all coordinates and check them, can't just check against the last one in case the tiles 'move around' in a square formation, not I'd have to check back 3 coordinates or even more...

I think I'll just try checking against all that are placed already, and for debuggin, ramp up the tile amounts and see how it runs... Doubt anyone would play long enough to get that far (it's meant as one of those waste a couple minutes games).
Suppose your world size is 20x20.

Suppose you have placed a path 15 tiles long.


Which is faster: iterating across a list of 15 tiles to make sure you don't duplicate a tile, or checking 400 tiles in an array?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

You know, it seemed like I couldn't figure out what direction I needed to go, but knew what I wanted to do. After some posts/replies, you get that duh! moment...

Apoch, I think you've been that duh! moment for me in a couple threads now this week! :shame:

This topic is closed to new replies.

Advertisement