Author Topic: Generating a txt for terrain.  (Read 681 times)

I just made a code that reads a text document, and makes terrain based on it. An example of what it would read is:
Code: [Select]
11111
12221
12321
12221
11111
It reads each number and builds that many high, so that following example would be a somewhat like a pyrimid. Now my question is, how could I make a script that would generate the text files for me, and be somewhat realistic instead of just random spikes? Heres the script that reads and makes the terrain:
Code: [Select]
function makeMap(%map)
{
%file = new fileObject();
%file.openForRead("add-ons/chat_c/terrain/" @ %map @ ".txt");
while(!%file.isEOF())
{
%line = %file.readLine();
%len = strlen(%line);
for(%i=1;%i<%len;%i++)
{
%char = getsubstr(%line,%i,1);
for(%up=0;%up<%char;%up++)
{
plantBrick(1);
plantBrick(0);
superShiftBrickUp(1);
superShiftBrickUp(0);
}
for(%down=0;%down<%char;%down++)
{
superShiftBrickDown(1);
superShiftBrickDown(0);
}
superShiftBrickRight(1);
superShiftBrickRight(0);
}
superShiftBrickAway(1);
superShiftBrickAway(0);
for(%i=1;%i<%len;%i++)
{
superShiftBrickLeft(1);
superShiftBrickLeft(0);
}
}
%file.close();
%file.delete();
}

And heres the one that generates a text document to build, but its not very realistic.
Code: [Select]
function genMap(%x,%y,%map)
{
%lineNum = 1;
for(%mx=0;%mx<%x;%mx++)
{
%line = "";
for(%my=0;%my<%y;%my++)
{
%add = getRandom(1,5);
%line = %line @ %add;
}
%file = new fileObject();
%file.openForAppend("add-ons/chat_c/terrain/" @ %map @ ".txt");
%file.writeLine(%line);
%lineNum++;
%file.close();
%file.delete();
}
}

This is a complicated thing, to generate logical terrain.

It's not as simple as asking and then receiving.

This is a complicated thing, to generate logical terrain.

It's not as simple as asking and then receiving.
I'm not looking for something like minecraft with caves and everything, I just want some hills maybe. Nothing much.

The easiest way to do this is with circles. Pick a random point in an array of heights and raise/lower it as well as the points in a radius around it based on how far they are away from that original point. Repeat as needed.

http://dl.dropbox.com/u/551734/perlin.cs
Code: [Select]
exec("./perlin.cs");
$Perlin = new ScriptObject()
{
class = perlin;
};
function genMap(%width,%length,%map)
{
%lineNum = 1;
%path = "config/" @ %map @ ".txt";
%file = new FileObject();
%file.openForWrite(%path);

%size = %width * %length;
%quality = 2;
%z = getRandom() * 1024;//this is the seed

%columnNum = 1;
for(%j=0;%j<4;%j++)
{
for(%i=0;%i<%size;%i++)
{
if(%data[%i] $= "")
%data[%i] = 0;
%x = %i % %width;
%y = mFloor(%i / %length);
%data[%i] = $Perlin.noise(%x / %quality,%y / %quality,%z) * %quality;
if(%j == 3)
{
%elev = mAbs(mFloor(%data[%i] * 0.2));
//now we need to clamp the elevation between 0 and 9 because your loader only accepts 1 digit numbers
%elev = mClamp(%elev,0,9);
if(%columnNum >= %width)
{
%file.writeLine(%line);
%line = "";
%lineNum++;
%columnNum = 1;
continue;
}
else
%line = %line @ %elev;
%columnNum++;
}
}
%quality *= 4;
}
%file.close();
%file.delete();
}
genMap(64,64,"mapname");
Code: [Select]
8888888888888888888888888888877777777766666665555554444433333222221111100000011111222222233333333444444444444444444444444444444
8888888888888888888888888887777777777666666655555544444333333222221111100000111112222222333333334444444444444445555555555544444
8888888888888888888888887777777777776666666555555444444333332222211111000001111122222223333333444444444444555555555555555555555
7778888888888888888877777777777777666666665555554444443333322222111110000011111222222333333334444444444555555555555555555555555
7777777777777777777777777777777766666666655555544444433333222221111100000111112222223333333444444444455555555555555555555555555
7777777777777777777777777777777666666665555555444444333332222211111000001111112222233333334444444445555555555555555555555555555
7777777777777777777777777777766666666655555554444443333322222111110000011111122222333333344444444555555555555555555555555555555
7777777777777777777777777776666666666555555544444433333222222111110000011111222223333333444444445555555555555556666666666666555
7777777777777777777777776666666666665555555444444333333222221111100000111112222233333334444444555555555555666666666666666666666
6677777777777777777776666666666666555555554444443333332222211111000001111122222233333344444445555555555566666666666666666666666
6666666666777766666666666666666665555555544444433333322222111110000011111222222333333444444455555555566666666666666666666666666
6666666666666666666666666666666555555555444444433333222221111110000011111222223333334444444555555555666666666666666666666666666
6666666666666666666666666666665555555554444444333332222221111100000111112222233333344444455555555566666666666666666666666666666
6666666666666666666666666666555555555544444443333332222211111000001111122222233333444444455555555666666666666666666666666666666
6666666666666666666666666665555555555444444433333322222211111000001111122222333334444444555555566666666666666777777777777777777
6666666666666666666666666555555555554444444333333222222111110000011111222223333334444445555555666666666666777777777777777777777
6666666666666666666666655555555555544444444333333222221111110000011111222223333344444455555556666666666677777777777777777777777
5555666666666666666655555555555555444444443333332222221111100000111112222233333344444455555556666666666777777777777777777777777
5555555556666665555555555555555554444444433333332222211111100000111112222233333444444555555566666666677777777777777777777777777
5555555555555555555555555555555544444444433333322222211111000001111122222333333444445555555666666666777777777777777777777777777
5555555555555555555555555555555544444444333333322222211111000001111122222333334444445555555666666667777777777777777777777777777
5555555555555555555555555555555444444444333333222222111111000001111122222333334444445555556666666667777777777777777777777777777
5555555555555555555555555555554444444443333333222222111110000011111222223333334444455555556666666677777777777777777778888777777
5555555555555555555555555555544444444443333333222222111110000011111222223333344444455555566666666777777777777777788888888888888
5555555555555555555555555555544444444433333332222221111110000011111222223333344444455555566666666777777777777778888888888888888
5555555555555555555555555555444444444433333332222221111110000011111222223333344444455555566666666777777777777788888888888888888
5555555555555555555555555555444444444433333332222221111110000011111222223333344444455555566666667777777777777888888888888888888
5555555555555555555555555554444444444433333332222221111110000011111222223333344444455555566666667777777777778888888888888888888
5555555555555555555555555554444444444433333332222221111110000011111222223333344444455555566666667777777777778888888888888888888
5555555555555555555555555554444444444433333332222221111110000011111222223333344444455555566666667777777777778888888888888888888
5555555555555555555555555554444444444433333332222221111110000011111222223333344444455555566666667777777777778888888888888888888
5555555555555555555555555554444444444433333332222222111110000011111222223333334444455555566666666777777777778888888888888888888
5555555555555555555555555554444444444433333333222222111110000001111122222333334444455555556666666777777777778888888888888888888
5555555555555555555555555555444444444443333333222222111111000001111122222333334444445555556666666777777777777888888888888888888
5555555555555555555555555555444444444443333333222222211111000001111122222333333444445555556666666677777777777888888888888888888
5555555555555555555555555555544444444443333333322222211111100000111112222233333444444555555666666667777777777788888888888888888
5555555555555555555555555555544444444444333333322222211111100000111112222233333344444555555566666667777777777778888888888888888
5555555555555555555555555555554444444444433333332222221111110000011111222223333344444455555566666666777777777777788888888888888
5555555555555555555555555555555444444444433333332222222111110000011111122222333334444445555556666666677777777777778888888888888
5555555555555555555555555555555544444444443333333222222111111000001111122222333333444445555555666666667777777777777788888888888
5555555555555555555555555555555554444444444333333322222211111100000111112222233333344444555555566666666777777777777777888888888
5555555555555555555555555555555555444444444433333332222221111100000011111222223333344444455555556666666677777777777777777888888
5555555555555555555555555555555555554444444443333333222222111110000011111122222333334444445555555666666666777777777777777777788
5555555555555555555555555555555555555444444444333333322222211111000001111112222233333444444555555566666666677777777777777777777
5555555566666666666665555555555555555544444444433333332222221111100000111112222223333344444455555555666666666777777777777777777
5555666666666666666666666555555555555555444444443333333222222111110000011111122222333334444444555555566666666667777777777777777
6666666666666666666666666666555555555555544444444333333322222211111000001111112222233333344444455555555666666666677777777777777
6666666666666666666666666666665555555555555444444433333332222221111100000111111222223333334444445555555566666666666677777777777
6666666666666666666666666666666665555555555544444444333333222222111110000001111122222333333444444455555555666666666666677777777
6666666666666666666666666666666666655555555555444444433333322222211111100000111112222223333334444444555555556666666666666677777
6666666666666666666666666666666666666555555555544444443333333222221111110000011111122222333333444444455555555566666666666666667
6666666666666666666666666666666666666665555555555444444433333322222211111000000111112222223333334444444555555555566666666666666
6666666666666667777766666666666666666666655555555544444443333333222221111110000011111222222333333344444445555555555666666666666
6666666777777777777777777777666666666666666555555555444444433333322222211111000001111112222223333334444444455555555555666666666
6777777777777777777777777777777766666666666665555555554444444333333222221111110000011111122222333333344444444555555555555666666
7777777777777777777777777777777777766666666666655555555544444433333322222211111000000111112222223333333444444444555555555555666
7777777777777777777777777777777777777776666666666655555554444444333333222221111110000011111122222233333334444444445555555555555
7777777777777777777777777777777777777777766666666666555555544444443333322222211111000000111111222222333333344444444445555555555
7777777777777777777777777777777777777777777766666666665555555444444333333222222111110000001111122222223333333344444444445555555
7777777777777788888888888888777777777777777777766666666655555554444443333332222211111100000111111222222233333333444444444445555
7777778888888888888888888888888888877777777777777666666666555555544444433333322222111111000001111112222222333333333444444444445
8888888888888888888888888888888888888887777777777777666666665555555444444333332222221111100000011111122222222333333334444444444
8888888888888888888888888888888888888888888777777777776666666655555554444443333322222211111000000111111122222223333333334444444
8888888888888888888888888888888888888888888888777777777766666666555555544444333333222222111110000001111111222222233333333334444
8888888888888888888888888888888888888888888888888777777777766666665555554444443333332222211111100000011111112222222233333333334
8888888888888888899999999999999999988888888888888888777777777666666655555544444433333322222111111000000111111122222222233333333
8888888888999999999999999999999999999999988888888888888777777777666666555555444444333333222221111110000000111111122222222233333
8888999999999999999999999999999999999999999999888888888888777777776666665555554444443333332222211111100000001111111222222222333
9999999999999999999999999999999999999999999999999988888888887777777766666665555554444433333322222111111000000011111111222222222
9999999999999999999999999999999999999999999999999999988888888887777777666666655555544444333333222221111111000000111111112222222
9999999999999999999999999999999999999999999999999999999988888888887777777666666555555444443333332222221111110000000111111112222
9999999999999999999999999999999999999999999999999999999999998888888877777776666665555554444433333322222211111100000001111111112
9999999999999999999999999999999999999999999999999999999999999988888888777777766666655555544444333333222222111111000000001111111
9999999999999999999999999999999999999999999999999999999999999999988888888777777666666555555444443333332222221111111000000011111
9999999999999999999999999999999999999999999999999999999999999999999988888887777776666665555554444433333322222211111110000000011
9999999999999999999999999999999999999999999999999999999999999999999999888888877777766666655555544444333333222222111111100000000
9999999999999999999999999999999999999999999999999999999999999999999999999888888877777666666555555444443333332222222111111100000
9999999999999999999999999999999999999999999999999999999999999999999999999998888888777777666665555554444433333322222221111111000
9999999999999999999999999999999999999999999999999999999999999999999999999999998888887777776666655555544444333333222222211111110
9999999999999999999999999999999999999999999999999999999999999999999999999999999988888877777766666555555444443333332222222111111
9999999999999999999999999999999999999999999999999999999999999999999999999999999999888888777777666665555554444433333332222221111
9999999999999999999999999999999999999999999999999999999999999999999999999999999999998888887777766666655555544444333333322222211
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888877777666666555554444443333333222222
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888888777776666665555544444433333332222
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888887777766666655555444444333333322
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888777777666665555554444443333332
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888887777776666655555544444433333
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888877777666666555555444444333
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888888777776666665555544444433
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888877777766666555555444444
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888777776666665555554444
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888887777766666555555444
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888777776666665555554
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888877777766666555555
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888777776666665555
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888877777766666555
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888888777776666655
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888877777666666
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888887777766666
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888777777666
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888877777766
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888887777776
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888777777
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888877777
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888887777
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888777
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888877
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888877
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888887
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999988
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998
8999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998
8888888999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998
8888888888899999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998

Thank you Destiny and Truce.
Edit: Destiny, your code generates a file with all the same number for some reason.
« Last Edit: May 08, 2011, 11:33:17 AM by halcynthis »

Did you do genMap(width,height,"filename")?
Also, it takes a seed, so sometimes the data will actually be flat.