Randomly Generating Map

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Randomly Generating Map

Post by Nexus »

Hey guys, for my game still standing I was wondering how I would accomplish when players go into unexplored area it generates new squares(I am basing my map system off of forsaken sanctum) would I do this by using an INSERT into table paired with some PHP code to tell it that the player is on a new tile?
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: Randomly Generating Map

Post by Perry »

Are you trying to create areas that change every time you enter or are you trying to randomly create a map once and save it to be reloaded later? I am not sure I understand. To build my map I ran a query like this one time.

Code: Select all

//this builds initial MAP table. run only once
  for ($i = 1; $i <= 30; $i+= 1)
  {
   for ($c = 1; $c <= 30; $c+= 1)
    {
      $tile = rand(0,5);
      if($tile == 1)
        {
          $tile = "woods1";
        }
      else if($tile == 2)
        {
          $tile = "water1";
        }
      else if($tile == 4)
        {
          $tile = "rock1";
        }
      else
        {
          $tile = "grass1";
        }
      $buildmap = "INSERT INTO map (mapr, mapc, tile)Values('$i','$c', '$tile')";
      $buildmap2=mysql_query($buildmap) or die("Could not get build map");
    }
  } 
If you are trying to dynamically create maps that change every time you enter an area, you might try running that every time the area is entered. That would build a different map every time an area was entered, but the old map would not be saved.

I might be able to help more and be more specific. Can you clarify what you are trying to do? Hope this makes sense, if not let me know.
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Randomly Generating Map

Post by Nexus »

I am trying to create a map that stores an area and it stays that way and when you uncover ground it randomly generates that. Think of Forsaken Sanctum that has a couple of squares to begin with. The player then moves out and uncovers a randomly generated world that saves itself and you can constantly explore more.
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: Randomly Generating Map

Post by Perry »

Which Forsaken Sanctum? I don't remember how the second was, but the first used something similar to what I just showed you. I actually modeled that code off of what I found in the Forsaken Sanctum 1 source.
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Randomly Generating Map

Post by Nexus »

Yeah that is the one I kinda got the idea from Minecraft for the randomly generating never ending map haha, but yeah I am using the source code that halls came out with about a year ago which I think is 1.0
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Randomly Generating Map

Post by hallsofvallhalla »

Just run some PHp generating the random material and have it update the map. Not hard at all.

So build the map like FS but leave the tables empty, fill them as players enter them. It really depends on your movement system and map system.

The above code is what I wrote to build the map for the first time. You could still use it but make the terrain empty but really I see no difference in creating the terrain before they enter or when they enter.
Zorber
Posts: 103
Joined: Wed Jun 15, 2011 2:51 am

Re: Randomly Generating Map

Post by Zorber »

Nexus wrote:Yeah that is the one I kinda got the idea from Minecraft for the randomly generating never ending map haha, but yeah I am using the source code that halls came out with about a year ago which I think is 1.0
I actually found out that your world on minecraft is the same as Everyone Else's. it's just HUGE and you spawn in different parts of it.
I also heard that the Nether is at one end of it, and it take's 19 hours of walking to find it.
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Randomly Generating Map

Post by Nexus »

O_O you can walk to find the nether? With the addition of world seeding are you talking about a certain seed?
Seisatsu
Posts: 34
Joined: Mon Aug 17, 2009 3:40 pm

Re: Randomly Generating Map

Post by Seisatsu »

Zorber wrote:
Nexus wrote:Yeah that is the one I kinda got the idea from Minecraft for the randomly generating never ending map haha, but yeah I am using the source code that halls came out with about a year ago which I think is 1.0
I actually found out that your world on minecraft is the same as Everyone Else's. it's just HUGE and you spawn in different parts of it.
I also heard that the Nether is at one end of it, and it take's 19 hours of walking to find it.
I am very, very, VERY sure that isn't true. Minecraft uses a lazy map generator with a random seed, so the world is only the same if you use the same seed. I have found, however, that if you use the same seed to generate a map, it is the same map and you do spawn at a random place. Also, the nether is implemented as a separate map, and has a different sky texture and different map generation algorithm. It's impossible to get there by walking.

If you walk for several hundred hours though, you can reach the "farlands", where variables in the map generator overflow and the generator goes berserk, creating an almost unplayable landscape. Maybe this is what you heard about.

Sorry to move off topic.
Image
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Randomly Generating Map

Post by Jackolantern »

I am not familiar with Minecraft, and have only done something similar in the browser myself, but I wanted to offer a cursory warning from my background with MUDs, MUCKs, and MOOs. In MUCKs, users are able to create the world however they want. Every player is a builder, and that is a prime aspect of the game (although the creation of the world is not a prime aspect of your type of browser games, the fact that it occurs automatically likely balances it out). What would happen in many MUCKs (TinyMUCK included), is that the game would begin growing out of control. After several months full of even a handful of dedicated players, the world could grow so large that it becomes untraversable, and (back in those days) would eventually over-run the server hard drive. While the hard drive space is likely of little concern these days, the size of database tables could be.

I would suggest you build the system from the ground up to automatically create and name regions (or allow explorers to do the naming within guidelines) and also make automatic ways of travel. If this crucial infrastructure is built along with the world creator, I think it would really help the players manager their own "creation" better.

Now, more on topic and as far as the coding goes, pretty much what Halls touched on, the key here would be to be able to express a randomly generated swatch of map in as little data as possible. You would likely want to keep a "master props table" that has keys to every graphic you will use to add touches to the map, which can be references solely based on their propID. Then you have another table that actually lists each map, with a simple X and Y coordinate about where it exists in the world. You then have a "user prop table" that keeps track of single entries of everything that is actually to show up on each individual map, also with X and Y coordinates to load right into the page's dynamic CSS. A (probably over) simplified example of the database tables may be something like this:

Code: Select all

Master Prop Table
----------------------------
propID | propName | propFileName | dimensionsX  | dimensionsY
0              Tree            tree1.jpg              32                    32
1              Rock1          rock1.jpg              24                    24
2              Cactus         cactus2.jpg           24                    24
3              OldHouse     oldhouse.jpg         124                  124
4              Waterfall     waterfall.jpg         124                   64

Master World Maps Table
------------------------------
mapID | mapName | worldX | worldY
0             map0          0            1
1             map1          1            1
2             map2          2            1
3             map3          3            1 //you can see the direction the player was walking as this was generated: east

Prop in the world Table
------------------------------
pusedID | mapID | propID | propX | propY
0               0           1             32      119
1               0           4             92       16
2               0           2             241     72
3               1           0             38       199           
4               1           3             64       70
5               2           0             53       188
6               2           0             67       190
7               2           4             213      91
8               3           3             112      5
9               3           1             61        76
10             4           3             145      39
11             4           0             93        47
You see how you could go and assemble the maps yourself with just this data? It also leaves room to add as many props as you want on any map. Now the more complicated side of it is the engine that actually generated it, because you must do all kinds of checks to ensure you aren't awkwardly dropping props on top of other props. I have some ugly code I could share with you from when I was hacking together something similar to this. It was going to be a kind of like Final Fantasy Tactics, but every map would be autogenerated. Now in my game, because the map was nothing but decoration, the map generation was mostly client-side in jQuery and did not match for both players (out of simplicity), but a lot of the logic could easily be converted to server-side so that it stays in sync between players (just read data from the database to find out where props are and how big they are instead of getting it client-side through jQuery). You would just have to recursively iterate through the process of creating a random prop, create another, check to make sure you don't place it on any other prop that exists, and on and on.

Give me a few minutes to find the file. Keep in mind it is ugly because I was mostly just hacking a prototype together, but it should be well-commented.
The indelible lord of tl;dr
Post Reply

Return to “Coding”