Author Topic: ISO simple ASCII procedural terrain  (Read 9932 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
ISO simple ASCII procedural terrain
« on: November 08, 2021, 05:44:06 pm »
I tried this for a few years, but I just cannot grasp the concept of "noise" "Parlin noise" or at least how to implement it in qb64.
Everything I try comes out too random.

looking for something as simple as this.
https://i.imgur.com/NunOIhN.png

do not need anything more 7 tilesets

so if anyone has a starter sample, or a link that explains how this is done (in english LOL) - found lots of resources for 3d like Minecraft etc.. but 2d ascii does not seem to exist.
thansk

« Last Edit: November 09, 2021, 02:18:04 pm by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #1 on: November 08, 2021, 07:05:22 pm »
When I think of 2D landscape I think of this:
Code: QB64: [Select]
  1. _Title "Drawlandscape Demo" ' b+ 2021-11-08
  2. Const Xmax = 800, Ymax = 600
  3. Screen _NewImage(Xmax, Ymax, 32)
  4. _ScreenMove 200, 50
  5.     drawLandscape
  6.     _Limit .5
  7.  
  8. Sub drawLandscape
  9.     'needs midInk, irnd
  10.  
  11.     Dim i As Long, startH As Single, rr As Long, gg As Long, bb As Long
  12.     Dim mountain As Long, Xright As Single, y As Single, upDown As Single, range As Single
  13.     Dim lastx As Single, X As Long
  14.     'the sky
  15.     For i = 0 To Ymax
  16.         midInk 0, 0, 128, 128, 128, 200, i / Ymax
  17.         Line (0, i)-(Xmax, i)
  18.     Next
  19.     'the land
  20.     startH = Ymax - 200
  21.     rr = 70: gg = 70: bb = 90
  22.     For mountain = 1 To 6
  23.         Xright = 0
  24.         y = startH
  25.         While Xright < Xmax
  26.             ' upDown = local up / down over range, change along Y
  27.             ' range = how far up / down, along X
  28.             upDown = (Rnd * .8 - .35) * (mountain * .5)
  29.             range = Xright + irnd&(15, 25) * 2.5 / mountain
  30.             lastx = Xright - 1
  31.             For X = Xright To range
  32.                 y = y + upDown
  33.                 Color _RGB(rr, gg, bb)
  34.                 Line (lastx, y)-(X, Ymax), , BF 'just lines weren't filling right
  35.                 lastx = X
  36.             Next
  37.             Xright = range
  38.         Wend
  39.         rr = irnd&(rr - 15, rr): gg = irnd&(gg - 15, gg): bb = irnd&(bb - 25, bb)
  40.         If rr < 0 Then rr = 0
  41.         If gg < 0 Then gg = 0
  42.         If bb < 0 Then bb = 0
  43.         startH = startH + irnd&(5, 20)
  44.     Next
  45.  
  46. Function irnd& (n1, n2) 'return an integer between 2 numbers
  47.     Dim l%, h%
  48.     If n1 > n2 Then l% = n2: h% = n1 Else l% = n1: h% = n2
  49.     irnd& = Int(Rnd * (h% - l% + 1)) + l%
  50.  
  51. Sub midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  52.     Color _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  53.  
  54.  

Then I thought of this:
Code: QB64: [Select]
  1. _Title "Drawlandscape Parallax test" 'started 2019-03-27
  2. 'test if can get end of landscape level to start for big looping background
  3. '2019-03-27 a more gentle adjustment back to Mountain starting height for
  4. 'more seamless connect of back end to front
  5. '2019-03-27 start this file with parallax drawing test
  6.  
  7.  
  8. Screen _NewImage(800, 600, 32)
  9. _ScreenMove 100, 20
  10. Type parallaxType
  11.     handle As Long
  12.     rate As Single 'number of pixels per frame added to le (leading edge)
  13.     le As Single
  14. nLevels = 6
  15. Dim Shared para(1 To nLevels) As parallaxType
  16.  
  17. Dim Shared scape&
  18. LoadLandscape
  19. scapeWidth = _Width(para(1).handle)
  20. scapeHeight = _Height(para(1).handle)
  21.  
  22. While t < 6000
  23.     Cls
  24.     For i = 1 To nLevels
  25.         If para(i).le + 800 > scapeWidth Then
  26.             te = scapeWidth - para(i).le
  27.             _PutImage (0, 0)-(te, scapeHeight), para(i).handle, 0, (scapeWidth - te, 0)-(scapeWidth, scapeHeight)
  28.             _PutImage (te, 0)-(800, scapeHeight), para(i).handle, 0, (0, 0)-(800 - te, scapeHeight)
  29.  
  30.         Else
  31.             _PutImage (0, 0)-(800, scapeHeight), para(i).handle, 0, (para(i).le, 0)-(para(i).le + 800, scapeHeight)
  32.         End If
  33.  
  34.         para(i).le = para(i).le - para(i).rate
  35.         If para(i).le < 0 Then para(i).le = scapeWidth
  36.     Next
  37.     t = t + 1
  38.     _Display
  39.     _Limit 120
  40.  
  41. Sub LoadLandscape
  42.     cur& = _Dest
  43.     xmax = 800 * 3.25: ymax = 600
  44.     hdl& = 1
  45.     para(hdl&).handle = _NewImage(xmax, ymax, 32)
  46.     _Dest para(hdl&).handle
  47.  
  48.     For i = 0 To ymax
  49.         midInk 0, 0, 128, 128, 128, 200, i / ymax
  50.         Line (0, i)-(xmax, i)
  51.     Next
  52.     'the land
  53.     startH = ymax - 200
  54.     rr = 70: gg = 70: bb = 90
  55.     For mountain = 1 To nLevels
  56.         If mountain > 1 Then
  57.             para(mountain).handle = _NewImage(xmax, ymax, 32)
  58.             _Dest para(mountain).handle
  59.         End If
  60.         Xright = 0
  61.         y = startH
  62.         Color _RGB(rr, gg, bb)
  63.         While Xright < xmax - 50
  64.             ' upDown = local up / down over range, change along Y
  65.             ' range = how far up / down, along X
  66.             upDown = (Rnd * .8 - .4) * (mountain * .5)
  67.             range = Xright + rand%(15, 25) * 2.5 / mountain
  68.             If range > xmax - 50 Then range = xmax - 50
  69.             lastx = Xright - 1
  70.             For x = Xright To range 'need less flat tops
  71.                 test = y + upDown
  72.                 test2 = y - upDown
  73.                 If Abs(test - startH) < .13 * startH Then y = test Else y = test2: upDown = -upDown
  74.                 Line (lastx, y)-(x, ymax), , BF 'just lines weren't filling right
  75.                 lastx = x
  76.             Next
  77.             Xright = range
  78.         Wend
  79.         x = lastx + 1
  80.         dy = (startH - y) / 50 'more gentle adjustment back to start of screen
  81.         While x <= xmax
  82.             y = y + dy
  83.             Line (lastx, y)-(x, ymax), , BF 'just lines weren't filling right
  84.             lastx = x
  85.             x = x + 1
  86.         Wend
  87.         rr = rand%(rr - 15, rr): gg = rand%(gg - 15, gg): bb = rand%(bb - 25, bb)
  88.         If rr < 0 Then rr = 0
  89.         If gg < 0 Then gg = 0
  90.         If bb < 0 Then bb = 0
  91.         startH = startH + mountain * rand%(2, 10)
  92.         para(mountain).le = xmax - 800
  93.         para(mountain).rate = mountain * .5
  94.     Next
  95.     _Dest cur&
  96.  
  97. Function rand% (lo%, hi%)
  98.     rand% = Int(Rnd * (hi% - lo% + 1)) + lo%
  99.  
  100. Sub midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  101.     Color _RGB(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  102.  
  103.  

Sure that could be reduced to ASCII?
« Last Edit: November 08, 2021, 07:25:25 pm by bplus »

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #2 on: November 08, 2021, 08:04:17 pm »
Love those
what I am trying to do (as in the image) is do a top down ascii style -
think dwarf fortress, but not that elaborate - just 5 or 6 tiles - I think DF has like 100 tiles LOL
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #3 on: November 10, 2021, 06:09:43 pm »
So I put this request up in 2012 in a game dev site, did not get many responses. I thought it was strange that a ascii top down terrain gen would be difficult (so at least I am not the only one LOL)
Many of the responses were like this - 3D models that were not land types.

So I'll take another stab at what I can do, post it here and see if someone can improve on it.

Team work
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #4 on: November 10, 2021, 06:51:38 pm »
@xra7en would you have images you would like converted, photos of something?

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #5 on: November 10, 2021, 08:26:48 pm »
... like an ASCII rpg?

ASCII. Old school. But cool.
Logic is the beginning of wisdom.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #6 on: November 10, 2021, 08:38:01 pm »
Quote from: bbplus
@xra7en would you have images you would like converted, photos of something?


O I've done that before - looks cool, but what I want is a procedurally generated one.

The first style I used to do for small games - and it served its purpose
Code: QB64: [Select]
  1. Dim tile(5) As String
  2. tile(1) = "T"
  3. tile(2) = "."
  4. tile(3) = "="
  5. tile(4) = "~"
  6. tile(5) = " "
  7.  
  8.  
  9. For i = 1 To 10
  10.     For j = 1 To 10
  11.         Print " "; tile(Int(Rnd * 5) + 1); " ";
  12.     Next
  13.     Print

but as I started looking at modern indi games, their land "made sense"  - like Minecraft, everything worked. This is too random, no clumping of areas


« Last Edit: November 10, 2021, 08:57:30 pm by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #7 on: November 10, 2021, 08:39:01 pm »
... like an ASCII rpg?

ASCII. Old school. But cool.

yup, have a simple one in mind, but want to have a large map that displays only a small portion where the player is at live.

easy to make, just obsessing over the random generator
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #8 on: November 10, 2021, 09:08:58 pm »
I think you could paint a map with a mouse, click an ascii on one screen maybe pick a color too then on the map screen paint it with a mouse, mouse down paints until mouse up.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #9 on: November 10, 2021, 09:34:03 pm »
Code: QB64: [Select]
  1. Dim tile(9) As String
  2. tile(0) = " "
  3. tile(1) = "T"
  4. tile(2) = "."
  5. tile(3) = "="
  6. tile(4) = "~"
  7. tile(5) = "b"
  8. tile(6) = "+"
  9. tile(7) = "M"
  10. tile(8) = Chr$(1)
  11. tile(9) = Chr$(3)
  12.  
  13.     k$ = InKey$
  14.     If k$ <> "" Then
  15.         If Asc(k$) >= 48 And Asc(k$) <= 57 Then kp = Asc(k$) - 48
  16.         Locate 1, 1: Print tile(kp);
  17.     End If
  18.     mx = _MouseX: my = _MouseY: mb1 = _MouseButton(1)
  19.     If my < 25 Then 'dang scrolling
  20.         If mb1 Then Locate my, mx: Print tile(kp);
  21.     End If
  22.  

  [ You are not allowed to view this attachment ]  

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #10 on: November 10, 2021, 09:49:56 pm »
very clever!! lol
At first look I was like WOW, smaller script than mine, then I saw the inkey$.
but the END RESULT is what i am looking for as procedural

working on one right now - I think I am on to something :-)
I'll post it here later.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #11 on: November 10, 2021, 10:00:59 pm »
I have added code to save to file with s keypress: (Had to start a Project Folder to save maps.)

Code: QB64: [Select]
  1. Dim tile(9) As String
  2. tile(0) = " "
  3. tile(1) = "T"
  4. tile(2) = "."
  5. tile(3) = "="
  6. tile(4) = "~"
  7. tile(5) = "b"
  8. tile(6) = "+"
  9. tile(7) = "M"
  10. tile(8) = Chr$(1)
  11. tile(9) = Chr$(3)
  12.  
  13.     k$ = InKey$
  14.     If k$ <> "" Then
  15.         If Asc(k$) >= 48 And Asc(k$) <= 57 Then
  16.             kp = Asc(k$) - 48: Locate 1, 1: Print tile(kp);
  17.         ElseIf k$ = "s" Then 'save pic
  18.             Open "Save Map.txt" For Output As #1
  19.             For y = 1 To 25
  20.                 b$ = ""
  21.                 For x = 1 To 80
  22.                     b$ = b$ + Chr$(Screen(y, x))
  23.                 Next
  24.                 Print #1, b$
  25.             Next
  26.             Close #1
  27.         End If
  28.     End If
  29.     mx = _MouseX: my = _MouseY: mb1 = _MouseButton(1)
  30.     If my < 25 Then 'dang scrolling
  31.         If mb1 Then Locate my, mx: Print tile(kp);
  32.     End If
  33.  

Here is sample of map (Save Map.txt) :
Code: QB64: [Select]
  1. +                                                                              
  2.                                                                                
  3.                                                              +++++              
  4.                                                             +++++++            
  5.                            bbbbb                            ++++++              
  6.                         b bb  bbbbbbbbbbbbbbb                ++ ++              
  7.                         bbbbbbb            bbb      ++++++++++++++++++++++      
  8.                           bbbbbbb          bbbb    ++++++++++++++++++++++++    
  9.                           b bbb             bbbb    +++++++++++++++++++++      
  10.                           b bb              bbbb             ++++              
  11.                           b bb              bb              +++++++            
  12.                           b bb            bbb               +++++++            
  13.                           bbb          bbbb                   +++              
  14.                           bbb          bbbbbbbb                                
  15.                           bb              bbbbbbbb                              
  16.                           bbb               bbbb b                              
  17.                           bbb                b bbb                              
  18.                           bbb              bbbbb                                
  19.                        bbbbbbbbbbbbbbbbbbbbbbb                                  
  20.                        bbbbbbbbbbbbbbbbbbbbbb                                  
  21.                           b   bbbbbbbbbbbbb                                    
  22.                              bb                                                
  23.                                                                                
  24.                                                                                
  25.                                                                              
  26.  

If you like it, save it under a new name otherwise it will just be written over with next drawing.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #12 on: November 10, 2021, 10:03:09 pm »
ok try this-
closer to what im shooting for

yes yes yes. redundant code (easy to clean up later)

currently just using squares.. but may change this to diamonds mix
Code: QB64: [Select]
  1.  
  2. Dim tile(5) As String
  3. Dim area(75, 20) As Integer
  4.  
  5. tile(1) = "T"
  6. tile(2) = "."
  7. tile(3) = "="
  8. tile(4) = "~"
  9. tile(5) = " "
  10.  
  11. '// Clear the map
  12. For i = 1 To 75: For j = 1 To 20: area(i, j) = 0: Next: Next
  13.  
  14. '// lets try some random squares
  15. For i = 1 To 30 ' <-- bigger number more dense
  16.     x = r(72)
  17.     y = r(18)
  18.     For k = y - 1 To y + 1
  19.         For j = x - 5 To x + 5
  20.             If j > 0 And j < 76 And k > 0 And k < 21 Then
  21.                 area(j, k) = 1
  22.             End If
  23.  
  24.         Next
  25.     Next
  26.  
  27. For i = 1 To 15 ' <-- bigger number more dense
  28.     x = r(72)
  29.     y = r(18)
  30.     For k = y - 1 To y + 1
  31.         For j = x - 5 To x + 5
  32.             If j > 0 And j < 76 And k > 0 And k < 21 Then
  33.                 area(j, k) = 2
  34.             End If
  35.  
  36.         Next
  37.     Next
  38.  
  39. For i = 1 To 7 ' <-- bigger number more dense
  40.     x = r(72)
  41.     y = r(18)
  42.     For k = y - 1 To y + 1
  43.         For j = x - 5 To x + 5
  44.             If j > 0 And j < 76 And k > 0 And k < 21 Then
  45.                 area(j, k) = 3
  46.             End If
  47.  
  48.         Next
  49.     Next
  50.  
  51. For i = 1 To 7 ' <-- bigger number more dense
  52.     x = r(72)
  53.     y = r(18)
  54.     For k = y - 1 To y + 1
  55.         For j = x - 5 To x + 5
  56.             If j > 0 And j < 76 And k > 0 And k < 21 Then
  57.                 area(j, k) = 4
  58.             End If
  59.  
  60.         Next
  61.     Next
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. For j = 1 To 20
  70.     For i = 1 To 75
  71.  
  72.         Select Case area(i, j)
  73.             Case 0: Print " ";
  74.             Case 1: Print "T";
  75.             Case 2: Print "#";
  76.             Case 3: Print ".";
  77.             Case 4: Print ".";
  78.  
  79.         End Select
  80.     Next
  81.  
  82.     Print
  83.  
  84.  
  85.     r = Int(Rnd * num) + 1
  86.  
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #13 on: November 10, 2021, 10:06:28 pm »
When you are ready to alter the look of the characters, I have a Character Editor.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: ISO simple ASCII procedural terrain
« Reply #14 on: November 10, 2021, 10:12:25 pm »
When you are ready to alter the look of the characters, I have a Character Editor.

thank you.. !!

for simplicity I'll keep it in the extended table for now..

is your editor similar to the DF characters?
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!