Random Movement Based Map Generator
Author: | Xhunterko
|
Submitted: | 16th May, 2006
|
Views: | 5634
| Rated: |
|
|
Here's the second example of a random generator. These are to be used as utilities by the programmer before creating a game. These are not meant to be true programs. Just randomizers that the programmer can tweak for desired results.
I have no way of knowing whether or not this has been done before. Yes, I’ve looked through the articles and found many level generators. However, most of them seemed thoroughly complicated or difficult to interpret. So I’ve come up with my own simple system based on movement. To begin, I will start with showing you what you need to make the generator. Most of the objects will be terrain pieces with three variants.
Objects needed:
3 Water tiles: dark, medium, and light shades
3 Grass tiles: dark, medium, and light shades
3 Dirt tiles: dark, medium, and light shades
3 Tree tiles: dark, medium, and light shades
1 House tile
2 Mountain tiles
1 Maker object
If your feeling adventurous, you can add three glacier tiles as well for variety. The two tiles I’ll explain are the House and Mountain tiles. House tiles are generic representations of cities or towns. While they don’t specifically look like them, they still represent them on your map. The 2 Mountain tiles are for height variants. The dark, medium, and light shaded variants of the terrain squares represent elevation. Dark being the lowest point, and light being the highest. The Maker object is what deposit’s the tiles on the map. All these are active objects by the way. So, to begin coding, we need a few groups first. Group1 is called TheMaker group. Group2 is called RandomWorld. There are a few event lines we need to program outside the groups. We have a start of frame event, a key press event, and a couple collision events. You can make the frame dimensions any way you want, but I recommend 320x480. (Make the background a nice color to, like green or blue.) Also, all the active objects are 32x32 in size. Be sure and give all the terrain tiles the Neutral classification. Remember, all the objects are active here, so you’d want to save as much memory as possible. For the ungrouped code, we begin with an start of frame event. We want the Maker object as fresh as possible so we set a few things. First we make it stick to the frames origins, then we check it’s flag0 on.
1. Start of Frame
>TheMaker: Set X pos to 0
>TheMaker: Set Y pos to 0
>TheMaker: Set Flag0 on
Okay, we separate the X and Y from each other so as not to confuse the program and to provide greater accuracy. We set it’s flag 0 on to make sure it stays there. To ensure it works the way we want it to, we need to ensure that both groups a permanently inactive. So uncheck the “Active when frame starts box” for each group. Now we need to create an event that activates both the TheMaker group and the RandomWorld group. We do this by using a key press.
2. Upon pressing “1”
>Special: Activate group TheMaker
>Special: Activate group RandomWorld
Why do we activate both groups? Simple. TheMaker group contains the Maker’s movement. The RandomWorld group contains the Maker’s seed number and tile distribution variables. Here’s what we’re trying to accomplish with both groups. We want the Maker to move from right to left from top to bottom. So whenever it hits the right side, it goes down one and goes towards the left side. When it hits the left side, we want it to go back to the right and then to repeat. While it’s moving, we want it to drop a random terrain tile on every space. So, now that we know what we want it to do, let’s begin with the TheMaker group code.
Group: TheMaker
/First, we tell it to go to the right.
1. Every 00-10”
+TheMaker: Flag0 is on
>TheMaker: Set X(TheMaker)+32
/Now when it hits the edge, we tell it to go down one space. The Frame Width insures that it obeys this law /no matter what the frame size is.
2. X pos of TheMaker = Frame Width
>TheMaker: Set Flag0 off
>TheMaker: Set Y(TheMaker)+32
>TheMaker: Set Flag1 on
/Flag1 tells TheMaker to go to the left so we subtract, instead of adding 32, we subtract it.
3. Every 00-10”
+TheMaker: Flag1 is on
>TheMaker: Set X(TheMaker)-32
/Now here’s a gimmick. When checking my code after rewriting it, I noticed that the correct code did not go /all the way back to the edge. It would skip a space and not place a tile. So I had to make it go over the edge. /This makes sure the edge space is filled and it’s so natural you don’t even notice it. Then we reset the flags /back with flag0 active and 1 off.
4. X pos of TheMaker <= -32
+TheMaker: Flag1 is on
>TheMaker: Set Flag1 off
>TheMaker: Set Y(TheMaker)+32
>TheMaker: Set Flag0 on
/Now, this will tell TheMaker to go all the way to the bottom of the frame. Since we don’t want excess tiles /eating up the memory, we destroy TheMaker as soon as it goes beyond the bottom.
5. Y pos of TheMaker = Y Bottom Frame +32
>TheMaker: Destroy
/This ensures that TheMaker will not create farther then is necessary. Effectively stopping memory usage.
Now, run the frame. You will notice that TheMaker obeys the laws of our pseudo-code. It goes from side to side all the way to the bottom. If not, go back and check that you have the X and Y values matching the correct flag sets and vice versa. Now we want it to drop a terrain tile for every space it goes over. The reason I chose every 00-10” is so it won’t go too fast and skip a space. I also didn’t want it going too slow. On to the RandomWorld group. We have with us 6 basic terrain types that need to be placed on our map. These are the tiles we’re going to be using for our random world: 1 House tile, 1 Mountain higher tile, 1 medium Grass, 1 medium Water tile, 1 dark Water tile, 1 medium Dirt tile, and 1 medium Tree tile. For a total of 7 objects. We want each of these objects to be placed randomly every 00-10” of a second. So for our pseudo-code, we want to first set a value of each of the objects to a random 7 (for each object) every 00-10”. This sets our terrain seed. Now we set TheMaker seed to a steady flow of objects. At the same time rate, we add 1 to TheMaker’s Value A. When it equals the specified terrain type’s Value A, then we drop one down at the exact spot. However, we need to make sure that each terrain type has a chance to be placed. Granted, there are 7 types, but because MMF starts counting at 0, the last number often gets left out. So we need to have it add until it goes one higher, so in this case it goes to 8. When it goes above 8, we need to reset it back to 0.
/There’s actually two parts in dealing with this group. The first deals with seeding random numbers out of 7 /every 00-10” to match the speed of TheMaker.
1. Every 00-10”
>House: Set Alt Val A to Random(7)
>Mountain1: Set Alt Val A to Random(7)
>Medium Grass: Set Alt Val A to Random(7)
>Medium Water: Set Alt Val A to Random(7)
>Dark Water : Set Alt Val A to Random(7)
>Medium Dirt: Set Alt Val A to Random(7)
>Medium Tree: Set Alt Val A to Random(7)
/Now we flow the seed of TheMaker object. Remember that we want it to reset after it reaches 8 because we /want all the objects to be placed.
2. Every 00-10”
+TheMaker Alt Val A<=0
>TheMaker: Add 1 to Alt Val A
3. Alt Val A (TheMaker) >=8
>TheMaker: Set Alt Val A to 0
/Now we have to check to see if the tile value matches TheMaker’s value and create one accordingly.
4. Alt Val A (House) = Alt Val A (TheMaker)
> Create House at 0’0 from TheMaker
5. Alt Val A (Mountain1) = Alt Val A (TheMaker)
> Create Mountain1 at 0’0 from TheMaker
6. Alt Val A (Grass) = Alt Val A (TheMaker)
> Create Grass at 0’0 from TheMaker
7. Alt Val A (Water) = Alt Val A (TheMaker)
> Create Water at 0’0 from TheMaker
8. Alt Val A (Dark Water) = Alt Val A (TheMaker)
> Create Dark Water at 0’0 from TheMaker
9. Alt Val A (Dirt) = Alt Val A (TheMaker)
> Create Dirt at 0’0 from TheMaker
10. Alt Val A (Tree) = Alt Val A (TheMaker)
> Create Tree at 0’0 from TheMaker
/Now we have another problem. Sometimes some tiles will not get placed at all because the values don’t /equal TheMaker’s. So we need to make sure that a tile is placed. Let’s go with the grass tile.
11. Every 00-10”
+Class Neutral Alt Val A<> Alt Val A of TheMaker
> Create Grass at 0’0 from TheMaker
Now that we have a creation system with space checking, we need to control it a little bit more. So, let’s go back to the ungrouped code. There are two things we don’t want to happen. We don’t want all the active’s eating up the memory, and we only want one tile per spot. So now we need two new lines of code.
/Note, that TheMaker object collides with objects every time it puts one down. I found this out the hard /way. We want the tiles not to eat up the memory so we turn them into backdrops and then destroy them. /They won’t be destroyed from the frame but will be destroyed from memory.
1. Collision between TheMaker and Class Neutral
> Class Neutral: Add Backdrop (Not an Obstacle)
>Class Neutral: Destroy
2. Collision between Class Neutral and Class Neutral
>Class Neutral: Destroy
Now run the frame and press key 1 on your keyboard. You should see TheMaker faithfully paint a map that you would never think of. Well, yeah, it’s a little jumbled but it is still a map. Well, what do you do with the other objects and shades you created. Well, for my generator, I have a couple different groups with a diverse mix of objects with different shades. The purpose of the shades is so you can decide on elevation and diversity when you actually make the map. This generator does not create the actual map you’ll build in your game. It just acts like a guide you use to help yourself make more genuine play fields. In part 2, we’ll explore how to adapt this to plat formers. Questions? Comments? Something you think I forgot? Post it here. Thanks in advance.
|
|
|