- Terrain CE: An Isometric Terrain Generator in TI-Basic
- 25 May 2020 05:56:40 pm Permalink
- Last edited by Michael2_3B on 26 Jun 2020 12:28:21 pm; edited 9 times in total

>Terrain-CE on GitHub<

>>Download v1.0 From The Archives!<<

So I've started gradually programming a bit more lately and here's my latest bored experiment.

I don't know if you'd call this perlin noise or not, but I made some code last night that randomly generates smooth(ish) terrain, or perhaps you might just call it a height map, using a matrix and height values in each element.

The main idea for the code is that it will generate a map where each block that is next to each other will be no more than 1 block higher or lower than the block next to it. After getting that to work, I implemented a smoothing algorithm that looks for single block dips or spikes and flattens them so they are the same height as the blocks around them.

I also limited it to only 6 height values bc of the ti-basic colors, I used white thru black but replaced the white with light blue, or you might imagine it as water. Black areas are high, blue is low.

Here's the (unoptimized) code, if you would like to optimize it be my guest:

With that said, here's an example with a 20x20 map:

The limitations with this code is that of course the matrix can be no larger than 20x20, so perhaps converting storage to a string or list might work better. There's also no way to implement caves or anything else at the moment. It would be cool to represent this in a 3d space (like an isometric view perhaps) but for the moment this is just a demo/experiment.

It's not the best of course, I definitely think I could optimize the smoothing code. But I hope you like it!

>>Download v1.0 From The Archives!<<

So I've started gradually programming a bit more lately and here's my latest bored experiment.

I don't know if you'd call this perlin noise or not, but I made some code last night that randomly generates smooth(ish) terrain, or perhaps you might just call it a height map, using a matrix and height values in each element.

The main idea for the code is that it will generate a map where each block that is next to each other will be no more than 1 block higher or lower than the block next to it. After getting that to work, I implemented a smoothing algorithm that looks for single block dips or spikes and flattens them so they are the same height as the blocks around them.

I also limited it to only 6 height values bc of the ti-basic colors, I used white thru black but replaced the white with light blue, or you might imagine it as water. Black areas are high, blue is low.

Here's the (unoptimized) code, if you would like to optimize it be my guest:

**Code:**```
//Initialize
```

ClrHome

Float

0->Xmin:264->Xmax

~164->Ymin:0->Ymax

Input N //Input map size

DelVar [A]{N,N->dim([A]

randInt(20,25->[A](1,1 //Generate random height for first element

~1->C //flag variable used for start of program only

//Progress Bar Variables

100/(2N-2->G

Ans->P

//Generate Map

For(A,1,N

Output(2,1,toString(int(P))+"%" //Display progress

P+G->P

For(B,1,N

If C=~1

DelVar C2->B

If A=1

[A](A,B-1)+randInt(~1,1->R

If A>1 and B=1

[A](A-1,B)+randInt(~1,1->R

If A>1 and B>1

Then

If [A](A,B-1)!=[A](A-1,B

Then

int(median({[A](A,B-1),[A](A-1,B->R

Else

[A](A,B-1)+randInt(~1,1->R

End

End

If R<20:20->R

If R>25:25->R

R->[A](A,B

End

End

//Smoothing Code

//Smooth Corners

If [A](1,1)!=[A](2,1) and [A](2,1)=[A](1,2

[A](2,1)->[A](1,1

If [A](1,N)!=[A](2,N) and [A](2,N)=[A](1,N-1

[A](2,N)->[A](1,N

If [A](N,1)!=[A](N,2) and [A](N,2)=[A](N-1,1

[A](N,2)->[A](N,1

If [A](N,N)!=[A](N-1,N) and [A](N-1,N)=[A](N,N-1

[A](N-1,N)->[A](N,N

//Smooth Edges

For(C,2,N-1

If [A](1,C)!=[A](1,C-1) and not(variance({[A](1,C-1),[A](2,C),[A](1,C+1

[A](1,C-1)->[A](1,C

End

For(C,2,N-1

If [A](C,1)!=[A](C-1,1) and not(variance({[A](C-1,1),[A](C,2),[A](C+1,1

[A](C-1,1)->[A](C,1

End

For(C,2,N-1

If [A](C,N)!=[A](C-1,N) and not(variance({[A](C-1,N),[A](C,N-1),[A](C+1,N

[A](C-1,N)->[A](C,N

End

For(C,2,N-1

If [A](N,C)!=[A](N,C-1) and not(variance({[A](N,C-1),[A](N-1,C),[A](N,C+1

[A](N,C-1)->[A](N,C

End

//Smooth everything else on the inside

For(A,2,N-1

Output(2,1,toString(int(P))+"%"

P+G->P

For(B,2,N-1

If [A](A,B)!=[A](A-1,B) and not(variance({[A](A-1,B),[A](A+1,B),[A](A,B-1),[A](A,B+1)

[A](A-1,B)->[A](A,B

End

End

//Draw Map

ClrHome

ClrDraw

For(B,0,N-1

For(A,0,N-1

[A](A+1,B+1

Ans-13(Ans=25)-2(Ans=20

Pt-On(7A+3,~7B-3,1,Ans

Pt-On(7A+3,~7B-3,2,Ans

End

End

With that said, here's an example with a 20x20 map:

The limitations with this code is that of course the matrix can be no larger than 20x20, so perhaps converting storage to a string or list might work better. There's also no way to implement caves or anything else at the moment. It would be cool to represent this in a 3d space (like an isometric view perhaps) but for the moment this is just a demo/experiment.

It's not the best of course, I definitely think I could optimize the smoothing code. But I hope you like it!