/
world.go
203 lines (174 loc) · 5.76 KB
/
world.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
// Package genmap2derosion is based on "Simple Hydrology"
// See: https://nickmcd.me/2020/04/15/procedural-hydrology/
package genmap2derosion
import (
"fmt"
"image"
"image/color"
"math"
"math/rand"
"github.com/Flokey82/go_gens/vectors"
)
const worldsize = 256
type Params struct {
StoreGIFFrames bool
StorePNGCycles bool
Height int64
Width int64
Seed int64
Size vectors.IVec2
UseWindErosion bool
}
var DefaultParams = &Params{
StoreGIFFrames: true,
StorePNGCycles: true,
Seed: DefaultSeed,
Size: vectors.IVec2{
X: worldsize,
Y: worldsize,
},
UseWindErosion: false,
}
type World struct {
r *rand.Rand
params *Params
images []*image.Paletted // Generated frame used to construct the GIF.
palette []color.Color // Default color palette.
delays []int // Delay for each individual frame (0 for now).
drainage float64 // Drainage factor from pools
scale float64 // "Physical" Height scaling of the map
heightmap []float64 // Flat Array containing height information
sediment []float64 // Sediment storage (stores information on eroded / deposited sediment)
windpath []float64 // Wind Path Storage
waterpath []float64 // Water Path Storage (Rivers)
waterpool []float64 // Water Pool Storage (Lakes / Ponds)
waterdrains []int // Water Pool drainage points
// Flux related information (experimental)
fluxwaterpool []float64 // (TEMP Flux) Water Pool Storage (Lakes / Ponds)
// Trees
// trees []Plant
// plantdensity [worldsize * worldsize]float64 //Density for Plants
// Sediment erosion resistance
// sedimentToughness [worldsize * worldsize]float64 // Sediment toughness storage
}
const DefaultSeed = 12356
func NewWorld(params *Params) *World {
if params == nil {
params = DefaultParams
}
idxSize := params.Size.X * params.Size.Y
w := &World{
params: params,
r: rand.New(rand.NewSource(params.Seed)),
drainage: 0.01,
scale: 20.0,
heightmap: make([]float64, idxSize),
sediment: make([]float64, idxSize),
windpath: make([]float64, idxSize),
waterpath: make([]float64, idxSize),
waterpool: make([]float64, idxSize),
waterdrains: make([]int, idxSize),
fluxwaterpool: make([]float64, idxSize),
}
// Prepare grayscale palette for GIF (0-255).
for i := 0; i <= 255; i++ {
w.palette = append(w.palette, color.RGBA{uint8(i), uint8(i), uint8(i), 0xff})
}
// Initialize all water drains to -1 (unset)
for i := range w.waterdrains {
w.waterdrains[i] = -1
}
// Generate basic heightmap.
w.genTerrain()
if w.params.UseWindErosion {
// Deposit some loose sediment on the heightmap to get us started.
for i, h := range w.heightmap {
w.sediment[i] = h / 5
}
// Optionally just draw a pyramid instead of using the generated heightmap.
drawPyramid := true
if drawPyramid {
// Note: Uncomment to place a huge pyramid in the middle
var min, max float64
for i := 0; i < int(w.params.Size.X*w.params.Size.Y); i++ {
if w.heightmap[i] > max {
max = w.heightmap[i]
}
if w.heightmap[i] < min {
min = w.heightmap[i]
}
}
for i := 0; i < int(w.params.Size.X*w.params.Size.Y); i++ {
if w.heightmap[i] > 0.0 {
w.heightmap[i] = 1.0
} else {
w.heightmap[i] = 0.0
}
}
sinkPyramid := 0.3
// Normalize
for i := 0; i < int(w.params.Size.X*w.params.Size.Y); i++ {
x := i / int(w.params.Size.Y)
y := i % int(w.params.Size.Y)
w.heightmap[i] = 1.2 - 2*(math.Abs(float64(x)-float64(w.params.Size.X)/2)/float64(w.params.Size.X)+math.Abs(float64(y)-float64(w.params.Size.Y)/2)/float64(w.params.Size.Y)) //0.9*(heightmap[i] - min)/(max - min);
w.heightmap[i] -= sinkPyramid
if w.heightmap[i] < 0.0 {
w.heightmap[i] = 0.0
}
if w.heightmap[i] > w.sediment[i] {
w.sediment[i] = 0.0
} else {
w.sediment[i] -= w.heightmap[i]
}
}
}
// Erode a few times.
for i := 0; i < 400; i++ {
w.erodeWind(250)
}
// Export heightmap and sediment.
w.ExportPng("b_image.png", w.heightmap)
w.ExportPng("b_image_sed.png", w.sediment)
// Bake the heightmap (add the sediment)
for i := 0; i < int(w.params.Size.X*w.params.Size.Y); i++ {
w.heightmap[i] += w.sediment[i]
}
// Export combined heightmap and sediment.
w.ExportPng("b_image_comb.png", w.heightmap)
} else {
// Generate climate.
// w.genClimate()
fmt.Println("generated")
// Erode a few times.
for j := 0; j < 5; j++ {
w.doErosion(50, 600)
// c := w.genClimate()
// TODO: Update heightmap in climate struct.
// w.erodeRain(1, c.AvgRainMap)
// Export hydrology data generated by the original algorithm.
if w.params.StorePNGCycles {
w.ExportPng(fmt.Sprintf("b_image%d.png", j), w.heightmap)
w.ExportPng(fmt.Sprintf("b_image%d_wp.png", j), w.waterpath)
w.ExportPng(fmt.Sprintf("b_image%d_wpo.png", j), w.waterpool)
w.ExportPng(fmt.Sprintf("b_image%d_sed.png", j), w.sediment)
w.exportCombined(fmt.Sprintf("b_image%d_combo.png", j), w.heightmap, w.waterpath, w.waterpool)
}
// Optional stuff...
// Call downhill, which will generate flux information.
// for i, dhs := range w.getDownhill() {
// if len(dhs) == 0 {
// fmt.Println(fmt.Sprintf("sink: %d", i))
// }
// }
// Export alternative hydrology data generated through flux.
// w.export(fmt.Sprintf("b_image%d_flux_raw.png", j), w.getRawFlux())
// w.export(fmt.Sprintf("b_image%d_flux.png", j), w.getFlux())
// w.export(fmt.Sprintf("b_image%d_flux_wpo.png", j), w.fluxwaterpool[:])
}
// Bake the heightmap (add the sediment)
for i := 0; i < int(w.params.Size.X*w.params.Size.Y); i++ {
w.heightmap[i] += w.sediment[i]
}
}
return w
}