Advertisement
Guest User

Excel 2D Terrain Generation by Sancarn

a guest
May 5th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. 'The following code is by sancarn:
  2. 'www.youtube.com/sancarn
  3.  
  4. 'Excel Terrain Generator using Noise Map
  5. 'LARGELY seed dependant
  6.  
  7.  
  8. Function GenerateColumnTopDown(MaxHeight, TopGrass, Y_startcell, X_startcell)
  9. a = Now()
  10.  
  11. 'This function generates a single column of "terrain values". We are basically giving the
  12. 'cells ID values which will be used later in the game.
  13.  
  14. 'Range(Cells(Y_1,X_1),Cells(Y_2,X_2)) = ID - sets the value of all cells in the array between Cells (X_1,Y_1) and (X_2,Y_2) to ID.
  15. 'Cells(Y,X) = ID - sets the value of cell (X,Y) to ID.
  16.  
  17. 'a = Now() and b = Now() and the "Debug.Print "ColumnGen operation took: "; (b - a)" are
  18. 'merely used to find out how long the function takes to execute. This prints to the
  19. 'immediate window!
  20.  
  21. Range(Cells(Y_startcell, X_startcell), Cells(Y_startcell + TopGrass, X_startcell)) = 0
  22. Cells(Y_startcell + TopGrass + 1, X_startcell) = 2
  23. Range(Cells(Y_startcell + TopGrass + 2, X_startcell), Cells(Y_startcell + TopGrass + 3, X_startcell)) = 3
  24. Range(Cells(Y_startcell + TopGrass + 4, X_startcell), Cells(Y_startcell + MaxHeight - 1, X_startcell)) = 1
  25. Cells(Y_startcell + MaxHeight, X_startcell) = 7
  26.  
  27. ‘GenerateColumnTopDown = 0
  28.  
  29. b = Now()
  30.  
  31. Debug.Print "ColumnGen operation took: "; (b - a)
  32. End Function
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. Function NewInternalSeed(seed)
  40.  
  41. 'This function takes the old seed and provides a new seed. We find this new seed by multiplying the
  42. 'old seed by Pi.
  43. 'InternalSeed = InternalSeed * π
  44. Pi = 3.14159265358979
  45.  
  46. 'Finally we take the floor of this value, to make it an integer. Then we take the rightmost 10 characters with the Right() function.
  47. 'And finally we take the value depicted by the string from the Right() function with the Val() function.
  48.  
  49. NewInternalSeed = Val(Right(Int(seed * Pi), 10))
  50.  
  51. End Function
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. Function NewTopGrass(seed, AmpCoefficient, prevTopGrass)
  59.  
  60. 'This function generates the new height of the grass.
  61.  
  62. 'This uses a noise map I created given by certain AmplificationCoefficients between 0 and 5
  63. 'AmpCoef| Rand# 0 1 2 3 4 5 6 7 8 9
  64. '0 | 0 -1 0 0 0 0 0 0 0 1
  65. '1 | 0 -1 -1 0 0 0 0 0 1 1
  66. '2 | 0 -2 -1 -1 0 0 0 1 1 2
  67. '3 | 0 -3 -2 -1 0 0 0 1 2 3
  68. '4 | 0 -6 -4 -2 0 0 0 2 4 6
  69. '5 | 0 -8 -6 -4 -2 0 2 4 6 8
  70.  
  71. 'First we change the amp coefficient to the modified amp coefficient which helps with the calculations (instead of having
  72. ' to use a look up table)
  73.  
  74. ModifiedAmpCoefficient = Abs(AmpCoefficient - 5)
  75. If ModifiedAmpCoefficient = 0 Then ModifiedAmpCoefficient = 0.5
  76.  
  77. 'Now we find the Rand# from the newly generated seed.
  78.  
  79. RandNo = Val(Right(seed, 1))
  80.  
  81. 'Printing off some debug code to the immediate window.
  82.  
  83. Debug.Print "RandNo = "; RandNo
  84. Debug.Print "ModifiedAmpCoefficient = "; ModifiedAmpCoefficient
  85.  
  86. 'Finally we use the case function to select certain rules to map the Rand# to the correct value in the noise map
  87. 'relative to the modified amp coefficient
  88.  
  89. Select Case RandNo
  90. Case 0
  91. NewTopGrass = prevTopGrass
  92. Case 1, 2, 3, 4
  93. RandNo = RandNo - 5
  94. 'x = RandNo / ModifiedAmpCoefficient 'Old buggy code
  95. 'RandNo = Int(x) + (x - Int(x) > 0) 'Old buggy code
  96. RandNo = -1 * Int(Abs(RandNo) / ModifiedAmpCoefficient)
  97. Case 5, 6, 7, 8, 9
  98. RandNo = RandNo - 5
  99. RandNo = Int(RandNo / ModifiedAmpCoefficient)
  100. End Select
  101.  
  102. 'Finally the NewTopGrass is equal to the old top grass + the random number defined by the noise map.
  103. NewTopGrass = prevTopGrass + RandNo
  104. End Function
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. Sub generateWorld()
  112.  
  113. 'This generates the start of a world in excel.
  114.  
  115. MaxHeight = 35 'Max Height of world
  116. Y_startcell = 1 'Verticle starting cell position
  117. X_startcell = 50 'Horrizontal starting cell position
  118. seed = 1234967899 'The maps seed
  119. AmpCoefficient = 2 'Amplification Coefficient - From 0 to 5, 0 being flat and 5 being mountainous. 3 suggested
  120. MaxGenCollumns = 60 'Max number of columns to be generated
  121.  
  122. prevTopGrass = 15 'initial default topgrass (from the top)
  123.  
  124. 'prevTopGrass = Abs(prevTopGrass - MaxHeight) 'attempt to make top grass from the bottom instead of from the top
  125.  
  126. For x = 0 To MaxGenCollumns
  127. Debug.Print "Trial"; x
  128. Xval = X_startcell + x
  129. seed = NewInternalSeed(seed)
  130. prevTopGrass = NewTopGrass(seed, AmpCoefficient, prevTopGrass)
  131.  
  132. GenColFunctVariable = GenerateColumnTopDown(MaxHeight, prevTopGrass, Y_startcell, Xval)
  133. Next x
  134.  
  135. 'Finally we do some cursor positioning to try and aid the player into the rendered terrain zone.
  136.  
  137. Columns.ColumnWidth = 2
  138. Rows.RowHeight = 15
  139. Columns.Select
  140. Cells(Y_startcell + MaxHeight, X_startcell + MaxGenCollumns).Select
  141. 'Cells(Y_startcell, X_startcell).Select
  142. Cells(Y_startcell + MaxHeight / 2, X_startcell + MaxGenCollumns / 2).Select
  143. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement