Computer Science Canada

Intelligent Random Map Generation V2

Author:  pers2981 [ Fri Dec 30, 2011 2:27 pm ]
Post subject:  Intelligent Random Map Generation V2

Hey there,

This thread is a continuation from my last. (Click Me). I managed to create a some what cool looking map generation but theres a problem. Sometimes it works perfectly with no errors were as others it breaks.


Posted Image, might have been reduced in size. Click Image to view fullscreen.
Image broken?
Direct url : http://img689.imageshack.us/img689/5422/sexygen.png


Turing:

%===========================================================================%
% = == = == = == = == = == =  Initial Setup = == = == = == = == = == = == = %
%===========================================================================%
setscreen ("graphics:500;500,nobuttonbar,offscreenonly")

%===========================================================================%
% = == = == = == = == = == =  GenerateWorld = == = == = == = == = == = == = %
%===========================================================================%
var RowX : int := 0
var RowY : int := 0
var TileType : array 1 .. 100 of string
var TempColor : int := 0
var TempOdds : int := 0
procedure GenerateWorld
    for i : 1 .. 100
        if RowX > 9 then
            RowX := 0
            RowY := RowY + 1
        end if
        if RowX > 0 and RowX < 9 and RowY > 0 and RowY < 9 then
           
            if TileType (i - 1) = "Water" and TileType (i - 10) = "Water" then
                TempOdds := TempOdds + 50 + Rand.Int (0, 50)
                if TempOdds > 75 then
                    TileType (i) := "Water"
                    TempColor := 100
                end if
                if TempOdds < 75 then
                    TileType (i) := "Sand"
                    TempColor := 14
                end if
            end if

            if TileType (i - 1) = "Sand" and TileType (i - 10) = "Water" then
                TempOdds := TempOdds + 50 + Rand.Int (0, 50)
                if TempOdds > 80 then
                    TileType (i) := "Water"
                    TempColor := 100
                end if
                if TempOdds < 80 then
                    TileType (i) := "Sand"
                    TempColor := 14
                end if
            end if
           
            if TileType (i - 1) = "Water" and TileType (i - 10) = "Sand" then
                TempOdds := TempOdds + 50 + Rand.Int (0, 50)
                if TempOdds > 95 then
                    TileType (i) := "Water"
                    TempColor := 100
                end if
                if TempOdds < 95 then
                    TileType (i) := "Sand"
                    TempColor := 14
                end if
            end if
           
            if TileType (i - 1) = "Sand" and TileType (i - 10) = "Sand" then
                TempOdds := TempOdds + 50 + Rand.Int (0, 50)
                if TempOdds > 95 then
                    TileType (i) := "Water"
                    TempColor := 100
                end if
                if TempOdds < 95 then
                    TileType (i) := "Sand"
                    TempColor := 14
                end if
            end if


        end if
        if RowX = 0 or RowX = 9 or RowY = 0 or RowY = 9 then
            TileType (i) := "Water"
            TempColor := 100
        end if
        Draw.FillBox (RowX * 50, RowY * 50, RowX * 50 + 50, RowY * 50 + 50, TempColor)
        RowX := RowX + 1
        TempOdds := 0
    end for
end GenerateWorld
%===========================================================================%
% = == = == = == = == = == =    Game Loop   = == = == = == = == = == = == = %
%===========================================================================%
GenerateWorld
loop
    View.Update
end loop


If you run it a few times you should get it to work. Im using Turing 4.1.1

Author:  Aange10 [ Fri Dec 30, 2011 3:42 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

Like I said you need to keep track of all your variables and see what variables are different in each execution and why they are causing an error. Instead of trying to have us do it for you

Author:  pers2981 [ Fri Dec 30, 2011 8:32 pm ]
Post subject:  Re: RE:Intelligent Random Map Generation V2

Aange10 @ Fri Dec 30, 2011 3:42 pm wrote:
Like I said you need to keep track of all your variables and see what variables are different in each execution and why they are causing an error. Instead of trying to have us do it for you


I don't recall you ever telling me this, Also do you really think I didn't attempt to fix it already. I only post asking for help after several hours of trying to fix it myself. How dare you sir. How dare you. BooHoo

Author:  chipanpriest [ Fri Dec 30, 2011 8:51 pm ]
Post subject:  Re: Intelligent Random Map Generation V2

Turing:
var y : int := 500

View.Set ("graphics:500;500")

for i : 0 .. 500
    for j : 0 .. 500
        drawdot (j, y, Rand.Int (9, 14))
    end for
    y -= 1
end for


Heres my random map generation Very Happy

Author:  mirhagk [ Fri Dec 30, 2011 8:59 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

Turing:

    for i : 1 .. 100
        if RowX > 9 then
            RowX := 0
            RowY := RowY + 1
        end if
        if RowX > 0 and RowX < 9 and RowY > 0 and RowY < 9 then

            if TileType (i - 1) = "Water" and TileType (i - 10) = "Water" then

There is your problem. Let's say that i is 1, and that the if statement for rowX is between 0 and 9 and rowY between 0 and 9, well then what is the value of TileType(i-10)? well that is TileType(-9), which doesn't exist.

Author:  pers2981 [ Fri Dec 30, 2011 9:10 pm ]
Post subject:  Re: RE:Intelligent Random Map Generation V2

mirhagk @ Fri Dec 30, 2011 8:59 pm wrote:
Turing:

    for i : 1 .. 100
        if RowX > 9 then
            RowX := 0
            RowY := RowY + 1
        end if
        if RowX > 0 and RowX < 9 and RowY > 0 and RowY < 9 then

            if TileType (i - 1) = "Water" and TileType (i - 10) = "Water" then

There is your problem. Let's say that i is 1, and that the if statement for rowX is between 0 and 9 and rowY between 0 and 9, well then what is the value of TileType(i-10)? well that is TileType(-9), which doesn't exist.


In order for RowX to be between 0 and 9 and rowY between 0 and 9 "i" would have to be higher then 1. RowX/RowY are both set to 0 before the for loop and on each pass RowX is increased by one, If RowX is higher then 9 then its set to 0 and RowY is increased by one. Therefore if RowX is between 0 and 9 then I is between 11 and 88

Author:  Raknarg [ Fri Dec 30, 2011 9:11 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

Also, he's not generating a fractal map, which is ugly and unrealistic. He's making a map that created tiles in a logical order. This makes an island, while yours creates nothing, in reality

Author:  RandomLetters [ Fri Dec 30, 2011 9:32 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

Nature is not logical. Please do not generate circular islands

In fact, nature is quite well represented by fractals, http://gameprogrammer.com/fractal.html#heightmaps

Author:  Raknarg [ Fri Dec 30, 2011 9:40 pm ]
Post subject:  Re: Intelligent Random Map Generation V2

So I couldn't find out why there were empty tiles, so instead I initially set all the tiles to water before random generation. It seems to had rectfied the issue.

A;so, I rewrote it, if you're interested. It's not better or anything, I just like it better this way Razz

Turing:

%===========================================================================%
% = == = == = == = == = == =  Initial Setup = == = == = == = == = == = == = %
%===========================================================================%
setscreen ("graphics:500;500,nobuttonbar,offscreenonly")

%===========================================================================%
% = == = == = == = == = == =  GenerateWorld = == = == = == = == = == = == = %
%===========================================================================%
var TileType : array 0 .. 9, 0 .. 9 of int
var TempOdds : int := 0
procedure GenerateWorld
    for j : 0 .. 9
        for i : 0 .. 9
            TileType (i, j) := 100
        end for
    end for
    for j : 0 .. 9
        for i : 0 .. 9
            if i > 0 and i < 9 and j > 0 and j < 9 then

                if TileType (i - 1, j) = 100 and TileType (i, j - 1) = 100 then
                    TempOdds := 50 + Rand.Int (0, 50)
                    if TempOdds > 75 then
                        TileType (i, j) := 100
                    end if
                    if TempOdds < 75 then
                        TileType (i, j) := 14
                    end if
                end if

                if TileType (i - 1, j) = 14 and TileType (i, j - 1) = 100 then
                    TempOdds := 50 + Rand.Int (0, 50)
                    if TempOdds > 80 then
                        TileType (i, j) := 100
                    end if
                    if TempOdds < 80 then
                        TileType (i, j) := 14
                    end if
                end if

                if TileType (i - 1, j) = 100 and TileType (i, j - 1) = 14 then
                    TempOdds := 50 + Rand.Int (0, 50)
                    if TempOdds > 95 then
                        TileType (i, j) := 100
                    end if
                    if TempOdds < 95 then
                        TileType (i, j) := 14
                    end if
                end if

                if TileType (i - 1, j) = 14 and TileType (i, j - 1) = 14 then
                    TempOdds := 50 + Rand.Int (0, 50)
                    if TempOdds > 95 then
                        TileType (i, j) := 100
                    end if
                    if TempOdds < 95 then
                        TileType (i, j) := 14
                    end if
                end if
            end if
            Draw.FillBox (i * 50, j * 50, i * 50 + 50, j * 50 + 50, TileType (i, j))
            TempOdds := 0
        end for
    end for
end GenerateWorld
%===========================================================================%
% = == = == = == = == = == =    Game Loop   = == = == = == = == = == = == = %
%===========================================================================%
GenerateWorld
loop
    View.Update
end loop


One change you'll notice is that I made the TempColour obsolete by saving the type as an integer according to its colour. If you end up using my code, you may want to change that to accomodate for tiles other than solid colours. Anyways, hope that helps.

Author:  pers2981 [ Fri Dec 30, 2011 9:40 pm ]
Post subject:  Re: RE:Intelligent Random Map Generation V2

RandomLetters @ Fri Dec 30, 2011 9:32 pm wrote:
Nature is not logical. Please do not generate circular islands

In fact, nature is quite well represented by fractals, http://gameprogrammer.com/fractal.html#heightmaps


Nature doesn't exactly look like this either.

Posted Image, might have been reduced in size. Click Image to view fullscreen.
In event of image failure click the link below.
http://img207.imageshack.us/img207/1682/choop392bb11917a3198cf1.png

I want sometime in-between. Something 'Logical' hence the title of the thread, Intelligent-Random map generation.

Author:  Raknarg [ Fri Dec 30, 2011 9:42 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

@Randomletters no, it isnt logical. However, it's not absolutely random, either. It's a mixture of both, technically. However, if I were to make a choice, a logical island would make more sense than a fractal island, with sand more common in the middle than the outside.

Author:  RandomLetters [ Fri Dec 30, 2011 9:47 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

I don't know what you're talking about. This looks pretty real to me

http://www.nextrevision.com/r5/multifractal0.jpg

Author:  Raknarg [ Fri Dec 30, 2011 9:49 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

You should note that some things in nature are more chaotic than others. You can use this island as an example. If you look at an island as a single entity, the island itself isnt very random. It has a logical order where the innermost parts of the island are higher than the outside. The shape may vary, but that idea is constant. If you were to look at a string of islands, than that heightmap example is more meaningful, as islands occur in random places in a non logical order.

Same thing goes with that picture. The island is the same as a mountain. In itself, a mountain is less of a fractal than the land altogether is.

Author:  pers2981 [ Fri Dec 30, 2011 9:56 pm ]
Post subject:  Re: RE:Intelligent Random Map Generation V2

RandomLetters @ Fri Dec 30, 2011 9:47 pm wrote:
I don't know what you're talking about. This looks pretty real to me

http://www.nextrevision.com/r5/multifractal0.jpg


Exactly. I don't want my island to be a perfect circle BUT I do want the sand to be on the outside (As seen in your picture), stone on the steep areas and what not.

Can we get this back on topic instead of debating the "More realistic" forms of map generation.

Author:  Raknarg [ Fri Dec 30, 2011 9:59 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

Sure. Now do you have any other questions? IF you didnt read it back there, all I did was set all the tiles to water to begin with and it worked fine.

Author:  pers2981 [ Fri Dec 30, 2011 10:14 pm ]
Post subject:  Re: RE:Intelligent Random Map Generation V2

Raknarg @ Fri Dec 30, 2011 9:59 pm wrote:
Sure. Now do you have any other questions? IF you didnt read it back there, all I did was set all the tiles to water to begin with and it worked fine.

Thanks alot, I didn't see you state this earlier. I would +Karma but I can't. Thanks again!

Author:  RandomLetters [ Fri Dec 30, 2011 10:15 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

Take a fractal heightmap.

Then, take a height to be sea level, and cover everything below it with water. Just above sea level, label it sand, a step up from that height, color it forest, and finally at the highest range, set it to moutains. Islands (high in the middle and surrounded by sand) will naturally appear.

That picture was a fractal. Take a look at the first link. It shows you how to create it.

If you want to use your own method, that's great as well, but it seems like Raknarg has already solved your problem then. I'm just trying to show you some alternatives.

Author:  Raknarg [ Sat Dec 31, 2011 2:34 pm ]
Post subject:  RE:Intelligent Random Map Generation V2

Well that's great, except that true fractal heightmap generation is a tad more complex. I would just leave it at this for now.

Author:  Aange10 [ Sat Dec 31, 2011 3:43 pm ]
Post subject:  Re: RE:Intelligent Random Map Generation V2

pers2981 @ 30/12/2011, 7:32 pm wrote:
Aange10 @ Fri Dec 30, 2011 3:42 pm wrote:
Like I said you need to keep track of all your variables and see what variables are different in each execution and why they are causing an error. Instead of trying to have us do it for you


I don't recall you ever telling me this, Also do you really think I didn't attempt to fix it already. I only post asking for help after several hours of trying to fix it myself. How dare you sir. How dare you. BooHoo



Skype @ 6:59 PM wrote:

[12/29/2011 6:54:10 PM] Jacob P***: Yes
[12/29/2011 6:54:17 PM] Jacob P***: sometimes it works perfectly.
[12/29/2011 6:54:18 PM] Jacob P***i: Why is this
[12/29/2011 6:59:22 PM] David H******: look into the documentation for error catching, and output the RowX, RowY, I, TileType (i) and what not
[12/29/2011 6:59:25 PM] David H******: i dont have time


Now that I've jogged your memory.

My origional post:

Quote:

Like I said you need to keep track of all your variables and see what variables are different in each execution and why they are causing an error. Instead of trying to have us do it for you


: