Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'The following code is by sancarn:
- 'www.youtube.com/sancarn
- 'Excel Terrain Generator using Noise Map
- 'LARGELY seed dependant
- Function GenerateColumnTopDown(MaxHeight, TopGrass, Y_startcell, X_startcell)
- a = Now()
- 'This function generates a single column of "terrain values". We are basically giving the
- 'cells ID values which will be used later in the game.
- '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.
- 'Cells(Y,X) = ID - sets the value of cell (X,Y) to ID.
- 'a = Now() and b = Now() and the "Debug.Print "ColumnGen operation took: "; (b - a)" are
- 'merely used to find out how long the function takes to execute. This prints to the
- 'immediate window!
- Range(Cells(Y_startcell, X_startcell), Cells(Y_startcell + TopGrass, X_startcell)) = 0
- Cells(Y_startcell + TopGrass + 1, X_startcell) = 2
- Range(Cells(Y_startcell + TopGrass + 2, X_startcell), Cells(Y_startcell + TopGrass + 3, X_startcell)) = 3
- Range(Cells(Y_startcell + TopGrass + 4, X_startcell), Cells(Y_startcell + MaxHeight - 1, X_startcell)) = 1
- Cells(Y_startcell + MaxHeight, X_startcell) = 7
- ‘GenerateColumnTopDown = 0
- b = Now()
- Debug.Print "ColumnGen operation took: "; (b - a)
- End Function
- Function NewInternalSeed(seed)
- 'This function takes the old seed and provides a new seed. We find this new seed by multiplying the
- 'old seed by Pi.
- 'InternalSeed = InternalSeed * π
- Pi = 3.14159265358979
- 'Finally we take the floor of this value, to make it an integer. Then we take the rightmost 10 characters with the Right() function.
- 'And finally we take the value depicted by the string from the Right() function with the Val() function.
- NewInternalSeed = Val(Right(Int(seed * Pi), 10))
- End Function
- Function NewTopGrass(seed, AmpCoefficient, prevTopGrass)
- 'This function generates the new height of the grass.
- 'This uses a noise map I created given by certain AmplificationCoefficients between 0 and 5
- 'AmpCoef| Rand# 0 1 2 3 4 5 6 7 8 9
- '0 | 0 -1 0 0 0 0 0 0 0 1
- '1 | 0 -1 -1 0 0 0 0 0 1 1
- '2 | 0 -2 -1 -1 0 0 0 1 1 2
- '3 | 0 -3 -2 -1 0 0 0 1 2 3
- '4 | 0 -6 -4 -2 0 0 0 2 4 6
- '5 | 0 -8 -6 -4 -2 0 2 4 6 8
- 'First we change the amp coefficient to the modified amp coefficient which helps with the calculations (instead of having
- ' to use a look up table)
- ModifiedAmpCoefficient = Abs(AmpCoefficient - 5)
- If ModifiedAmpCoefficient = 0 Then ModifiedAmpCoefficient = 0.5
- 'Now we find the Rand# from the newly generated seed.
- RandNo = Val(Right(seed, 1))
- 'Printing off some debug code to the immediate window.
- Debug.Print "RandNo = "; RandNo
- Debug.Print "ModifiedAmpCoefficient = "; ModifiedAmpCoefficient
- 'Finally we use the case function to select certain rules to map the Rand# to the correct value in the noise map
- 'relative to the modified amp coefficient
- Select Case RandNo
- Case 0
- NewTopGrass = prevTopGrass
- Case 1, 2, 3, 4
- RandNo = RandNo - 5
- 'x = RandNo / ModifiedAmpCoefficient 'Old buggy code
- 'RandNo = Int(x) + (x - Int(x) > 0) 'Old buggy code
- RandNo = -1 * Int(Abs(RandNo) / ModifiedAmpCoefficient)
- Case 5, 6, 7, 8, 9
- RandNo = RandNo - 5
- RandNo = Int(RandNo / ModifiedAmpCoefficient)
- End Select
- 'Finally the NewTopGrass is equal to the old top grass + the random number defined by the noise map.
- NewTopGrass = prevTopGrass + RandNo
- End Function
- Sub generateWorld()
- 'This generates the start of a world in excel.
- MaxHeight = 35 'Max Height of world
- Y_startcell = 1 'Verticle starting cell position
- X_startcell = 50 'Horrizontal starting cell position
- seed = 1234967899 'The maps seed
- AmpCoefficient = 2 'Amplification Coefficient - From 0 to 5, 0 being flat and 5 being mountainous. 3 suggested
- MaxGenCollumns = 60 'Max number of columns to be generated
- prevTopGrass = 15 'initial default topgrass (from the top)
- 'prevTopGrass = Abs(prevTopGrass - MaxHeight) 'attempt to make top grass from the bottom instead of from the top
- For x = 0 To MaxGenCollumns
- Debug.Print "Trial"; x
- Xval = X_startcell + x
- seed = NewInternalSeed(seed)
- prevTopGrass = NewTopGrass(seed, AmpCoefficient, prevTopGrass)
- GenColFunctVariable = GenerateColumnTopDown(MaxHeight, prevTopGrass, Y_startcell, Xval)
- Next x
- 'Finally we do some cursor positioning to try and aid the player into the rendered terrain zone.
- Columns.ColumnWidth = 2
- Rows.RowHeight = 15
- Columns.Select
- Cells(Y_startcell + MaxHeight, X_startcell + MaxGenCollumns).Select
- 'Cells(Y_startcell, X_startcell).Select
- Cells(Y_startcell + MaxHeight / 2, X_startcell + MaxGenCollumns / 2).Select
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement