PDA

View Full Version : Random 2d Terrain



Discord
06-12-2004, 09:39 PM
Hi, I've been thinking of coding a new game based off of the tank wars / scorched earth style combat. I have alot of experience in programming in delphi but very little in DelphiX etc.. and I need some advice on howto generate a random 2d terrain. I have looked at tutorials that show how to create the terrain based off a predesigned image but, i don't want users to get to familiar with every map. So can anyone point me in the right direction of how to generate random 2d terrain with DelphiX?
Thanks

Paulius
06-12-2004, 10:44 PM
DelphiX wont help you here, you?¢_~ll have to do it yourself. You should group map tiles into terrain formation sets with their connectivity information (like a horizontal river can only have a water set to the left and right and anything else in other directions). When generating terrain simply fill empty map spaces with some random fitting set taking into account neighboring sets restrictions. One thing you should watch out for is connectivity between players spawn points, use some path finding algorithm to see if they connect, if they don?¢_Tt, put passable terrain over the shortest barrier between them.

Discord
07-12-2004, 01:10 AM
I was thinking of trying to start at ~ 30% of the dxdraw area height and use delphis random function with random(3) to get 0 1 or 2. Couldn't i then just loop through the entire width of the screen and draw a line up 1 pixel with 0, same height with 1 and down with 2 all while moving left across the screen to produce a fairly unique terrain? I'm just not sure on how to draw a line using delphi x. Sorry, I'm a newbie.


*edit*
Here i made a quick example of what i'm thinking using a tchart.
http://sighost.fuelie.org/files/example1.exe

WILL
07-12-2004, 03:25 AM
Paulius: I'm guessing you've never played Scorched Earth... Shame... very fun game. Perhaps the next popular game made within it's genre... Worms(Worms 2, Worms Armageddon, etc)?


Well, I had actually started writting a tutorial on how to make a 'Scorched Earth' type tank game sometime last year, but as it would have it no time. I do plan on finishing it as I have had great fun toying with the techniques back in my highschool days many years ago.

Reguarding your randomly generated landscape; You will need some form of array to store your 'dirt' situation. I have used a simple 1 dimentional array of Integers that spans the width of my game map. This stores the heights of the dirt at each x element of your array.

Example: To find the height of the land at location (x,y) you just find the height of the land by looking at the value in LandHeight[x].


Though this way does not allow for 'tunneling', it's at least a start for now. Always start simple!

Here is a clip of my source I used to generate the 'dirt' map.

procedure TBattlefield.GenerateLand(ScreenWidth, ScreenHeight, Highest, Lowest, Variation: Integer; Land, Sky: TColor; useSkyImage: Boolean; SkyImageIndex: Integer);
var i, j: Integer;
rand: Real;
begin
LandColor := Land;
SkyColor := Sky;
isSkyImage := useSkyImage;
SkyImage := SkyImageIndex;

Width := ScreenWidth;
Height := ScreenHeight;

LandHeight[0] := Lowest + Round(Random * (Highest - Lowest));
for i := 1 to ScreenWidth - 1 do
begin
repeat
rand := Random;
LandHeight[ i ] := Round(LandHeight + (rand * Variation) - Variation / 2);
if (LandHeight[ i ] < Lowest) then
LandHeight[ i ] := Lowest;
until (LandHeight[ i ] <= Highest);
end;
end;

TBattlefield is my object that stores my variables about my current game map/arena. And GenerateLand() it's method to generate the land map.

:arrow: LandHeight is the array that stores the actual land map.

Example: Length(LandHeight) = ScreenWidth should return true!

:arrow: Highest and Lowest are the max/min possible heights of the dirt in your game map.

:arrow: Variation is simply the max amount that the land will vary in each element in the array as it goes along from one to the next until the end.

[i]Land and Sky are the colors of the sky and the dirt.
useSkyImage and SkyImageIndex are just for setting the background image of the map.

Land, Sky, useSkyImage and SkyImageIndex are not important to this example and do not effect the generation of the map, ignore their use here...


To draw your map just draw lines from (x, 0) to (x, LandHeight[x]) for each element in your array...

I'll leave it to you to figure out how to make the land a bit smoother. :)


:!: Don't get too wrapped up in trying to create too many features this early. You'll need to master the basics first to understand how to handle the more advanced things later on. :!:


I hope this helps some. Best of luck! A 'Scorched Earth' clone is a great game project to start with. 8) It's one of the first types of games I made when I started out and I learned much from it.


Welcome to the site and I hope you come back to visit often. ;)

Discord
07-12-2004, 04:45 AM
Hey,
Thanks for the assitance and insight :D

Discord
07-12-2004, 06:46 PM
After using your technique I have coded a simple land generator that will draw sky and ground.


http://sighost.fuelie.org/files/LandGen.exe

Thanks Again :D

Discord
08-12-2004, 12:04 AM
Ok, i've got to the point where i can locate where i want the tanks to go but i'm having some problems displaying them. I tried putting my graphic into a DXImageList and drawing it with:



DXImageList1.Items&#91;0&#93;.Draw&#40;DxDraw1.Surface, i, LandHeight&#91;i&#93;, 0&#41;;
DxDraw1.Surface.Canvas.Release;
DxDraw1.Flip;


But i can't get the tank to show up. What am i doing wrong?

WILL
08-12-2004, 02:10 AM
This is turning into a DelphiX topic now, but you don't need to release your TDXDirectDrawSurface canvas unless you actually use it and then only release it after you are done as it will only slow down your routeens. I don't know if that will solve your problem though.

But also, you may want to make sure you only Flip after everythingis drawn, NOT each time you draw to the screen.

Other than that... I can't tell from what you've specified.