-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworldgen.go
298 lines (238 loc) · 7 KB
/
worldgen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"fmt"
"math/rand"
"rapidengine/child"
"rapidengine/procedural"
"time"
)
func initializeWorldTree() {
loadBlocks()
WorldMap = NewWorldTree()
}
var AverageWorldHeight = float32(0.5)
func generateWorldTree() {
Engine.Logger.Info("Loading blocks...")
ProgressText.Text = "Loading blocks..."
ProgressBar.SetPercentage(10)
updateLoadingScreen()
// Randomize seed
randomizeSeed()
Engine.Logger.Info("Placing dirt...")
ProgressText.Text = "Placing dirt..."
ProgressBar.SetPercentage(20)
updateLoadingScreen()
// Generate heightmap and place grass
generateHeightMap()
// Fill everything underneath grass with dirt
generateDirt()
Engine.Logger.Info("Placing stone...")
ProgressText.Text = "Placing stone..."
ProgressBar.SetPercentage(30)
updateLoadingScreen()
// Generate stone based on height
generateStone()
// Clean up stone above ground
cleanStone()
Engine.Logger.Info("Generating caves...")
ProgressText.Text = "Generating caves..."
ProgressBar.SetPercentage(40)
updateLoadingScreen()
// Generate caves
generateCaves()
// Clean back dirt
cleanBackDirt()
Engine.Logger.Info("Growing grass...")
ProgressText.Text = "Growing grass..."
ProgressBar.SetPercentage(50)
updateLoadingScreen()
// Put grass and some top grass on dirt with air above it
growGrass()
Engine.Logger.Info("Generating nature...")
ProgressText.Text = "Generating nature..."
ProgressBar.SetPercentage(60)
updateLoadingScreen()
// Create clouds
generateClouds()
// Place flowers and pebbles above grass
generateNature()
Engine.Logger.Info("Generating structures...")
ProgressText.Text = "Generating structures..."
ProgressBar.SetPercentage(70)
updateLoadingScreen()
// Generate structure
generateStructures()
generateAllDungeons()
Engine.Logger.Info("Orienting blocks...")
ProgressText.Text = "Orienting blocks..."
ProgressBar.SetPercentage(80)
updateLoadingScreen()
// Fix the orientation of blocks in the world
orientBlocks("dirt", true)
orientBlocks("grass", true)
orientBlocks("stone", true)
orientBlocks("leaves", true)
// Fix backdirt
createAllExtraBackdirt()
orientBlocks("backdirt", true)
Engine.Logger.Info("Creating light...")
ProgressText.Text = "Creating light..."
ProgressBar.SetPercentage(90)
updateLoadingScreen()
// Light up all blocks
CreateLighting(WorldWidth/2, HeightMap[WorldWidth/2]+5, 0.9)
// Save world to image
WorldMap.writeToImage()
ProgressBar.SetPercentage(100)
updateLoadingScreen()
// Set player starting position
Player1.PlayerChild.SetPosition(float32(WorldWidth*BlockSize/2), float32((HeightMap[WorldWidth/2]+50)*BlockSize))
Player1.CenterX = Player1.PlayerChild.X + (Player1.PlayerChild.ScaleX / 2) - (Player1.Hitbox1.DAABB.Width / 2)
Player1.CenterY = Player1.PlayerChild.Y + (Player1.PlayerChild.ScaleY / 2) - (Player1.Hitbox1.LAABB.Height / 2)
Engine.SceneControl.SetCurrentScene(WorldScene)
HotbarScene.Activate()
}
func generateTestWorldTree() {
randomizeSeed()
for x := 1300; x < 1600; x++ {
HeightMap[x] = 500
createWorldBlock(x, 500, "stone")
}
for x := 1300; x < 1600; x++ {
orientSingleBlock("stone", false, x, 500)
}
// Save world to image
WorldMap.writeToImage()
CreateLighting(1500, 500, 0.9)
Player1.PlayerChild.SetPosition(float32(BlockSize)*1500, float32(BlockSize)*600)
Player1.CenterX = Player1.PlayerChild.X + (Player1.PlayerChild.ScaleX / 2) - (Player1.Hitbox1.DAABB.Width / 2)
Player1.CenterY = Player1.PlayerChild.Y + (Player1.PlayerChild.ScaleY / 2) - (Player1.Hitbox1.LAABB.Height / 2)
Engine.SceneControl.SetCurrentScene(WorldScene)
HotbarScene.Activate()
}
// --------------------------------------------------
// World Generation Functions
// --------------------------------------------------
func generateHeightMap() {
randomizeSeed()
gen := procedural.NewSimplexGenerator(0.001, 1, 0.5, 8, Seed)
minHeight := float64(1)
maxHeight := float64(0)
for x := 0; x < WorldWidth; x++ {
ht := gen.Noise1D(float64(x))
if ht < minHeight {
minHeight = ht
}
if ht > maxHeight {
maxHeight = ht
}
HeightMap[x] = GrassMinimum + int(Flatness*ht*float64(WorldHeight))
}
AverageWorldHeight = float32(minHeight+maxHeight) / 2.0
for x := 0; x < WorldWidth; x++ {
createWorldBlock(x, HeightMap[x], "dirt")
}
}
func generateDirt() {
for x := 0; x < WorldWidth; x++ {
for y := 0; y < WorldHeight-1; y++ {
createWorldBlock(x, y, "dirt")
if WorldMap.GetWorldBlockName(x, y+1) == "dirt" {
break
}
}
}
}
func generateStone() {
randomizeSeed()
gen := procedural.NewSimplexGenerator(10, 1, 0.5, 5, Seed)
for x := 0; x < WorldWidth; x++ {
stoneFrequency := float64(StoneStartingFrequency)
for y := HeightMap[x]; y >= 0; y-- {
n := gen.Noise2D(float64(x)/300, float64(y)/300)
if n < stoneFrequency {
createWorldBlock(x, y, "stone")
}
stoneFrequency += StoneFrequencyDelta
if stoneFrequency > StoneEndingFrequency {
stoneFrequency = StoneEndingFrequency
}
}
}
}
func cleanStone() {
for x := 0; x < WorldWidth; x++ {
grassHeight := HeightMap[x]
if WorldMap.GetWorldBlockName(x, grassHeight) == "stone" {
for y := grassHeight + StoneTopDeviation; y < WorldHeight; y++ {
createWorldBlock(x, y, "sky")
}
} else {
for y := grassHeight + 1; y < WorldHeight; y++ {
createWorldBlock(x, y, "sky")
}
}
}
}
func cleanBackDirt() {
}
func growGrass() {
for x := 0; x < WorldWidth; x++ {
for y := 0; y < WorldHeight; y++ {
if WorldMap.GetWorldBlockName(x, y) == "dirt" && (WorldMap.GetWorldBlockName(x, y+1) == "sky" || WorldMap.GetBackBlockName(x, y+1) == "backdirt") {
createGrassBlock(x, y, "grasstop")
}
}
}
}
func generateClouds() {
for x := 0; x < WorldWidth; x++ {
if rand.Float32() < 0.4 {
CloudChild.AddCopy(
child.ChildCopy{
X: float32(x * BlockSize),
Y: float32((rand.Intn(20) + HeightMap[x] + 15) * BlockSize),
Material: cloudMaterial,
Darkness: 1,
},
)
x += 400 / BlockSize
}
}
}
func generateNature() {
for x := 1; x < WorldWidth-1; x++ {
if WorldMap.GetWorldBlockName(x, HeightMap[x]+1) == "sky" || WorldMap.GetWorldBlockName(x, HeightMap[x]+1) == "backdirt" {
natureRand := rand.Intn(16)
if natureRand == 15 && WorldMap.GetWorldBlockName(x-1, HeightMap[x]+2) != "treetrunk" {
} else if natureRand > 13 {
floraRand := rand.Intn(4) + 1
floraType := fmt.Sprintf("flower%d", floraRand)
if floraRand != 4 {
createNatureBlock(x, HeightMap[x]+1, floraType)
} else {
createNatureBlock(x, HeightMap[x]+1, "pebble")
}
} else if natureRand > 9 {
grassRand := rand.Intn(3) + 1
grassType := fmt.Sprintf("topGrass%d", grassRand)
createNatureBlock(x, HeightMap[x]+1, grassType)
}
}
}
}
// --------------------------------------------------
// World Generation Helpers
// --------------------------------------------------
func isBackBlock(name string) bool {
for _, transparent := range TransparentBlocks {
if name == transparent {
return true
}
}
return false
}
func randomizeSeed() {
Seed = time.Now().UTC().UnixNano()
rand.Seed(Seed)
}