-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenworldvoronoi.go
195 lines (168 loc) · 4.64 KB
/
genworldvoronoi.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
// Package genworldvoronoi is a port of redblobgames' amazing planet generator.
// See: https://www.redblobgames.com/x/1843-planet-generation
// And: https://github.com/redblobgames/1843-planet-generation
package genworldvoronoi
import (
"github.com/Flokey82/genworldvoronoi/bio"
"github.com/Flokey82/genworldvoronoi/geo"
)
type Map struct {
*geo.Geo // Geography / geology
*bio.Bio // Plants / animals / funghi
*Civ // Civilization
// *TileCache
// CoarseMeshes []*SphereMesh // Coarse meshes for each zoom level.
}
func NewMapFromConfig(seed int64, cfg *Config) (*Map, error) {
if cfg == nil {
cfg = NewConfig()
}
// Initialize the planet.
geo, err := geo.NewGeo(seed, cfg.GeoConfig)
if err != nil {
return nil, err
}
// Initialize the map.
m := &Map{
Geo: geo,
Bio: bio.NewBio(geo, cfg.BioConfig),
Civ: NewCiv(geo, cfg.CivConfig),
}
m.generateMap()
/*
m.TileCache = NewTileCache(m.BaseObject)
// Generate coarse meshes for LOD.
m.CoarseMeshes = make([]*SphereMesh, 5)
for i := range m.CoarseMeshes {
if i == len(m.CoarseMeshes)-1 {
m.CoarseMeshes[i] = m.SphereMesh
continue
}
n := 1 << utils.Max(len(m.CoarseMeshes)-i-1, 0)
log.Println("Generating coarse mesh for zoom level", i, "with", n, "iterations")
mesh, err := m.MakeCoarseSphereMesh(n)
if err != nil {
return nil, err
}
m.CoarseMeshes[i] = mesh
}*/
return m, nil
}
func NewMap(seed int64, numPlates, numPoints, numVolcanoes int, jitter float64) (*Map, error) {
cfg := NewConfig()
cfg.NumPlates = numPlates
cfg.NumPoints = numPoints
cfg.NumVolcanoes = numVolcanoes
cfg.Jitter = jitter
return NewMapFromConfig(seed, cfg)
}
/*
func (m *Map) getCoarseForZoom(zoom int) (*SphereMesh, int) {
n := 1 << utils.Max(len(m.CoarseMeshes)-zoom-1, 0)
log.Println("Zoom level", zoom, "has", n, "iterations")
return m.CoarseMeshes[utils.Min(zoom, len(m.CoarseMeshes)-1)], n
}
*/
func (m *Map) generateMap() {
// Build geography / geology / climate.
m.GenerateGeology()
// Build plants / animals / funghi.
m.GenerateBiology()
// Build civilization.
m.GenerateCivilization()
}
// Tick advances the map by one tick.
func (m *Map) Tick() {
m.Geo.Tick()
m.Bio.Tick()
m.Civ.Tick()
}
/*
// TileCache is a cache for tiles stored as a quadtree.
//
// This cache is used to retrieve tiles for rendering.
// If a tile is not found in the cache, the closest parent is returned, and
// the tile is generated in the background.
type TileCache struct {
*TileTree
}
func NewTileCache(bo *BaseObject) *TileCache {
return &TileCache{
TileTree: &TileTree{
BaseObject: bo,
},
}
}
// TileTree is a quadtree of tiles.
type TileTree struct {
Parent *TileTree
Children [4]*TileTree
X, Y, Zoom int
*BaseObject
}
func (t *TileTree) Get(x, y, zoom int) *TileTree {
x, y = wrapTileCoordinates(x, y, zoom)
log.Println("Get tile:", x, y, zoom)
// If the tile is not in the quadtree, return.
if zoom < t.Zoom {
return nil
}
// If the tile is exactly the one requested, return.
if zoom == t.Zoom && x == t.X && y == t.Y {
log.Println("Found tile in cache:", t.X, t.Y, t.Zoom)
return t
}
// Check if the tile is a parent of the tile we are looking for.
xAtCurrentTile := x >> uint(zoom-t.Zoom)
yAtCurrentTile := y >> uint(zoom-t.Zoom)
if xAtCurrentTile != t.X || yAtCurrentTile != t.Y {
return nil
}
// Find the points in the child quadtrees.
// Get the index of the child that will contain the tile that we are looking for.
childIndex := 0
xChild := x >> uint(zoom-t.Zoom-1)
yChild := y >> uint(zoom-t.Zoom-1)
if xChild%2 == 1 {
childIndex += 1
}
if yChild%2 == 1 {
childIndex += 2
}
// If the child is not nil, find the points in the child.
if t.Children[childIndex] != nil {
log.Println("Cache hit: Returning child tile", xChild, yChild, zoom+1)
return t.Children[childIndex].Get(x, y, zoom)
}
log.Println("Cache miss: Generating tile", xChild, yChild, zoom+1)
tbb := newTileBoundingBox(xChild, yChild, t.Zoom+1)
la1, lo1, la2, lo2 := tbb.toLatLon()
// Make sure we have some padding around the tile.
la1 -= 0.1
lo1 -= 0.1
la2 += 0.1
lo2 += 0.1
res := t.BaseObject.getBoundingBoxRegions(la1, lo1, la2, lo2)
childZoom := t.Zoom + 1
var baseObject *BaseObject
if childZoom%2 == 1 {
ipl, err := t.BaseObject.interpolate(res.Regions)
if err != nil {
return nil
}
baseObject = &ipl.BaseObject
} else {
baseObject = t.BaseObject
}
// If the child is nil, return the current tile.
// TODO: GENERATE THE TILE AND CACHE IT.
t.Children[childIndex] = &TileTree{
Parent: t,
X: xChild,
Y: yChild,
Zoom: childZoom,
BaseObject: baseObject,
}
return t.Children[childIndex].Get(x, y, zoom)
}
*/