Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 2D map generation
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: Thu Sep 01, 2011 10:39 am   Post subject: 2D map generation

So I recenty looked up a page on map generation (http://gameprogrammer.com/fractal.html#diamond) and I was very interesting in how I would implement this into code. So I have my attempt here at the Square Diamond Algorithm:
Turing:

var map : array 1 .. 5, 1 .. 5 of real
var c : int
const Max := upper (map)

for i : 1 .. Max
    for j : 1 .. Max
        map (i, j) := 5
    end for
end for

map (1, Max) := Rand.Int (-1, 11)
map (Max, Max) := Rand.Int (-1, 11)
map (Max, 1) := Rand.Int (-1, 11)
map (1, 1) := Rand.Int (-1, 11)

proc points_calc (arr : array 1 .. *, 1 .. * of real, x, y : int, dis : real, SorD : string)
    var n1, n2, n3, n4 : real
    if dis < 1 then
    elsif SorD = "square" then
        n1 := map (x - round (dis), y - round (dis))
        n2 := map (x - round (dis), y + round (dis))
        n3 := map (x + round (dis), y + round (dis))
        n4 := map (x + round (dis), y - round (dis))

        map (x, y) := ((n1 + n2 + n3 + n4) / 4) + (Rand.Int (-1000, 1000) / 1000)

        points_calc (map, x - round (dis), y, dis, "diamond")
        points_calc (map, x + round (dis), y, dis, "diamond")
        points_calc (map, x, y - round (dis), dis, "diamond")
        points_calc (map, x, y + round (dis), dis, "diamond")

    elsif SorD = "diamond" then
        if x - dis < 1 then
           
            n1 := map (x - round (dis) + Max, y)
        else
            n1 := map (x - round (dis), y)
        end if

        if x + dis > Max then
            n2 := map (x + round (dis) - Max, y)
        else
            n2 := map (x + round (dis), y)
        end if

        if y + dis > Max then
            n3 := map (x, y + round (dis) - Max)
        else
            n3 := map (x, y + round (dis))
        end if

        if y - dis < 1 then
            n4 := map (x, y - round (dis) + Max)
        else
            n4 := map (x, y - round (dis))
        end if

        map (x, y) := ((n1 + n2 + n3 + n4) / 4) + (Rand.Int (-1000, 1000) / 1000)

        if x - dis div 2 > 0 and y - dis div 2 > 0 then
            points_calc (map, x - round (dis) div 2, y - round (dis) div 2, dis / 2, "square")
        elsif x - dis div 2 > 0 and y + dis div 2 < Max + 1 then
            points_calc (map, x - round (dis) div 2, y + round (dis) div 2, dis / 2, "square")
        elsif x - dis div 2 < Max + 1 and y - dis div 2 < Max + 1 then
            points_calc (map, x + round (dis) div 2, y + round (dis) div 2, dis / 2, "square")
        elsif x - dis div 2 < Max + 1 and y - dis div 2 > 0 then
            points_calc (map, x + round (dis) div 2, y - round (dis) div 2, dis / 2, "square")
        end if
    end if
end points_calc

points_calc (map, ceil (Max / 2), ceil (Max / 2), Max div 2, "square")

for i : 1 .. Max
    for j : 1 .. Max
        if map (i, j) > 10 then
            c := yellow
        elsif map (i, j) > 8 then
            c := 42
        elsif map (i, j) > 6 then
            c := brightred
        elsif map (i, j) > 4 then
            c := red
        elsif map (i, j) > 2 then
            c := blue
        elsif map (i, j) > 0 then
            c := brightblue
        else
            c := brightpurple
        end if
        Draw.FillBox (i * 20, j * 20, i * 20 + 20, j * 20 + 20, c)
    end for
end for


So this suprisingly almost works, exept for 1 problem. If you run it, you'll notice that on the top right side there are three squares that are always the same. If you expand the size to something like 17x17, the same thing happens, only at a larger scale. I assume its because the procedure doesn't ever reach those tiles. Can anyone help with this? I think I'm going about this wrong, but I am now stuck.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: