/
topo.go
287 lines (260 loc) · 6.52 KB
/
topo.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
// Copyright © 2012 the Minima Authors under the MIT license. See AUTHORS for the list of authors.
package main
import (
"github.com/eaburns/eaburns/djsets"
"github.com/mccoyst/min-game/world"
)
// topoMap are a set of connected components of
// equal heights.
type topoMap struct {
sets []djsets.Set
stride int
conts []*contour
}
// A contour represents connected set of locations
// that have the same height.
type contour struct {
// id is the unique small int that names this contour.
id int
// size is the number of locations in this group.
size int
// terrain, if non-nil, specifies the terrain type
// for all locations of this contour.
terrain *world.TerrainType
// height and depth are the height and depth values
// for all locations of this contour.
height, depth int
// adj is the list of adjacent countours.
adj []*contour
// set is the set for the canonical location.
set *djsets.Set
}
// topoMap returns a topological map of the world.
func makeTopoMap(w *world.World) topoMap {
sets := findSets(w)
m := topoMap{
stride: w.H,
sets: sets,
conts: findContours(w, sets),
}
linkContours(w, m)
return m
}
// getContour returns the contour on which the given
// x,y point resides.
func (m topoMap) getContour(x, y int) *contour {
return m.sets[x*m.stride+y].Find().Aux.(*contour)
}
// minima returns a slice of all contours that are local minima.
func (m topoMap) minima() (mins []*contour) {
for _, c := range m.conts {
min := true
for _, a := range c.adj {
if a.height < c.height {
min = false
break
}
}
if min {
mins = append(mins, c)
}
}
return
}
// flood returns all of the contours that would flood
// when raising raising liquid to the given height
// in the given contour.
func (t topoMap) flood(c *contour, ht int) (fl []*contour) {
t.walk(c, func(c *contour) bool {
if c.height > ht {
return false
}
fl = append(fl, c)
return true
})
return
}
// walk traverses the contours out from the initial
// in a depth-first order, calling foreach on each newly
// visited contour. If foreach returns false then the
// successors of the given contour are not traversed on
// this branch of the search and will only be encountered
// if they are reached via another path along which foreach
// always returns true.
func (t topoMap) walk(init *contour, foreach func(*contour) bool) {
seen := make([]bool, len(t.conts))
var stack []*contour
seen[init.id] = true
stack = append(stack, init)
for len(stack) > 0 {
n := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if !foreach(n) {
continue
}
for _, kid := range n.adj {
if !seen[kid.id] {
seen[kid.id] = true
stack = append(stack, kid)
}
}
}
}
// findSets returns a slice of set, one for each
// location, that are unioned together such that
// sets with matching canonical representations
// all belong to the same contour.
func findSets(w *world.World) (sets []djsets.Set) {
sets = make([]djsets.Set, w.W*w.H)
for x := 0; x < w.W-1; x++ {
for y := 0; y < w.H-1; y++ {
loc := w.At(x, y)
set := &sets[x*w.H+y]
if right := w.At(x+1, y); loc.Height() == right.Height() {
set.Union(&sets[(x+1)*w.H+y])
}
if down := w.At(x, y+1); loc.Height() == down.Height() {
set.Union(&sets[x*w.H+y+1])
}
if diag := w.At(x+1, y+1); loc.Height() == diag.Height() {
set.Union(&sets[(x+1)*w.H+y+1])
}
}
}
// Right edge of the map (wraps to x == 0)
x := w.W - 1
for y := 0; y < w.H-1; y++ {
loc := w.At(x, y)
set := &sets[x*w.H+y]
if right := w.At(0, y); loc.Height() == right.Height() {
set.Union(&sets[y])
}
if down := w.At(x, y+1); loc.Height() == down.Height() {
set.Union(&sets[x*w.H+y+1])
}
if diag := w.At(0, y+1); loc.Height() == diag.Height() {
set.Union(&sets[y+1])
}
}
// Bottom edge of the map (wraps to y==0)
y := w.H - 1
for x := 0; x < w.W-1; x++ {
loc := w.At(x, y)
set := &sets[x*w.H+y]
if right := w.At(x+1, y); loc.Height() == right.Height() {
set.Union(&sets[x*w.H+y])
}
if down := w.At(x, 0); loc.Height() == down.Height() {
set.Union(&sets[x*w.H])
}
if diag := w.At(x+1, 0); loc.Height() == diag.Height() {
set.Union(&sets[(x+1)*w.H])
}
}
// Bottom left corner
loc := w.At(x, y)
set := &sets[x*w.H+y]
if right := w.At(0, y); loc.Height() == right.Height() {
set.Union(&sets[y])
}
if down := w.At(x, 0); loc.Height() == down.Height() {
set.Union(&sets[x*w.H])
}
if diag := w.At(0, 0); loc.Height() == diag.Height() {
set.Union(&sets[0])
}
return
}
// findContours returns a slice of all contours.
func findContours(w *world.World, sets []djsets.Set) (comps []*contour) {
for x := 0; x < w.W; x++ {
for y := 0; y < w.H; y++ {
loc := w.At(x, y)
switch set := sets[x*w.H+y].Find(); {
case set.Aux != nil:
c := set.Aux.(*contour)
c.size++
default:
c := &contour{
id: len(comps),
size: 1,
terrain: loc.Terrain,
height: loc.Elevation,
depth: loc.Depth,
set: set,
}
set.Aux = c
comps = append(comps, c)
}
}
}
return
}
// linkContours links adjacent contours.
func linkContours(w *world.World, m topoMap) {
for x := 0; x < w.W-1; x++ {
for y := 0; y < w.H-1; y++ {
c := m.getContour(x, y)
if right := m.getContour(x+1, y); c != right {
addLink(c, right)
}
if down := m.getContour(x, y+1); c != down {
addLink(c, down)
}
if diag := m.getContour(x+1, y+1); c != diag {
addLink(c, diag)
}
}
}
// Right edge of the map (wraps to x == 0)
x := w.W - 1
for y := 0; y < w.H-1; y++ {
c := m.getContour(x, y)
if right := m.getContour(0, y); c != right {
addLink(c, right)
}
if down := m.getContour(x, y); c != down {
addLink(c, down)
}
if diag := m.getContour(0, y+1); c != diag {
addLink(c, diag)
}
}
// Bottom edge of the map (wraps to y==0)
y := w.H - 1
for x := 0; x < w.W-1; x++ {
c := m.getContour(x, y)
if right := m.getContour(x, y+1); c != right {
addLink(c, right)
}
if down := m.getContour(x, 0); c != down {
addLink(c, down)
}
if diag := m.getContour(x+1, 0); c != diag {
addLink(c, diag)
}
}
// Bottom left corner
c := m.getContour(x, y)
if right := m.getContour(0, y); c != right {
addLink(c, right)
}
if down := m.getContour(x, 0); c != down {
addLink(c, down)
}
if diag := m.getContour(0, 0); c != diag {
addLink(c, diag)
}
return
}
// addLink adds a link between the two contours if
// one did not already exist.
func addLink(a, b *contour) {
for _, l := range a.adj {
if l == b {
return
}
}
a.adj = append(a.adj, b)
b.adj = append(b.adj, a)
}