-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwave.go
298 lines (259 loc) · 8.11 KB
/
wave.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 wfc
import (
"errors"
"image"
"image/color"
"image/draw"
"math/rand"
)
var (
ErrNoSolution = errors.New("no possible modules for slot")
)
// Wave holds the state of a wave collapse function as described by Oskar
// Stalberg.
//
// The wave is a recursive algorithm that collapses the possibility space of a
// 2D grid into a single output. Specifically, it is a 2D array of slots that
// are all in a superposition state of one or more modules; each module is a
// possible tile that might exist at that slot.
//
// The algorithm is described in detail by Oskar Stalberg at several
// conferences. It is described in detail during the following talk:
// https://www.youtube.com/watch?v=0bcZb-SsnrA&t=350s
//
// An example implmentation of the algorithm can be found here:
// https://oskarstalberg.com/game/wave/wave.html
type Wave struct {
Width, Height int // Width and height of the grid
Input []*Module // Input tiles (possible tiles at each slot)
PossibilitySpace []*Slot // The 2D grid of slots
History []*Slot // Slots that have been visited during the current/last collapse iteration
// Override this if you'd like custom logic when checking if a state is
// possible from a direction. This is useful if you'd like to slow down the
// collapse or add probabilities.
IsPossibleFn IsPossibleFunc
}
// New creates a new wave collapse function with the given width and height and
// possible image tiles.
//
// Constraints are automatically generated by looking at the color values of the
// pixels along each of the four edges of the tile. Any tiles that should
// potentially be neighbors should have the same color values along the edges.
// Otherwise, the tile will not be considered a neighbor.
//
// When generating constraints, only 3 pixels per edge are considered. For
// example, for the top edge, it looks at the top-left, then top-middle, then
// top-right pixels. likewise for the right edge, it would look at the
// top-right, then middle-right, then bottom-right.
func New(tiles []image.Image, width, height int) *Wave {
return NewWithCustomConstraints(tiles, width, height, DefaultConstraintFunc)
}
// NewWithCustomConstraints creates a new wave collapse function with the given
// adjacency constraint calculation function. Use this if you'd like custom
// logic for specifying constraints.
func NewWithCustomConstraints(tiles []image.Image, width, height int, fn ConstraintFunc) *Wave {
wave := &Wave{
Width: width,
Height: height,
Input: make([]*Module, len(tiles)),
IsPossibleFn: DefaultIsPossibleFunc,
}
// Automatically generate adjacency constraints for each input tile.
for i, tile := range tiles {
module := Module{Index: i, Image: tile}
for _, d := range Directions {
module.Adjacencies[d] = fn(tile, d)
}
wave.Input[i] = &module
}
return wave
}
// Initialize sets up the wave collapse function so that every slot is in a
// superposition of all input tiles/modules.
//
// Each module is equally likely to be at each slot.
func (w *Wave) Initialize(seed int) {
rand.Seed(int64(seed)) // TODO: move off rand... this isn't thread safe; we can do better :)
w.PossibilitySpace = make([]*Slot, w.Width*w.Height)
for x := 0; x < w.Width; x++ {
for y := 0; y < w.Height; y++ {
slot := Slot{
X: x, Y: y,
Superposition: make([]*Module, len(w.Input)),
}
copy(slot.Superposition, w.Input)
w.PossibilitySpace[x+y*w.Width] = &slot
}
}
}
// Collapse recursively collapses the possibility space for each slot into a
// single module.
//
// Important: Not all tile sets will allways produce a solution, so this
// function can return an error if a contradiction is found. You can still
// export the image of a failed collapse to see which of your tiles is causing
// issues for you.
func (w *Wave) Collapse(attempts int) error {
for i := 0; i < attempts; i++ {
err := w.Recurse()
if err != nil {
return err
}
w.History = make([]*Slot, 0)
}
return nil
}
// CollapseRandomSlot takes a random slot and collapses it into a single module.
// If the slot is already collapsed, it will pick another slot and try again.
func (w *Wave) CollapseRandomSlot() *Slot {
num_collapsed := 0
for _, s := range w.PossibilitySpace {
entropy := len(s.Superposition)
if entropy <= 1 {
num_collapsed++
}
}
// If all slots are already collapsed, we're done.
if num_collapsed == len(w.PossibilitySpace) {
return nil
}
// Pick a random slot that is not collapsed.
for {
slot := w.PossibilitySpace[rand.Intn(len(w.PossibilitySpace))]
if len(slot.Superposition) <= 1 {
continue
}
slot.Collapse()
return slot
}
}
// Recurse collapses the wave collapse function recursively.
func (w *Wave) Recurse() error {
if w.IsCollapsed() {
return nil
}
// Check if we need to pick a starting point
if len(w.History) == 0 {
slot := w.CollapseRandomSlot()
w.History = append(w.History, slot)
}
previous := w.History[len(w.History)-1]
for _, d := range Directions {
if !w.HasNeighbor(previous, d) {
continue
}
next := w.GetNeighbor(previous, d)
if w.HasVisited(next) {
continue
}
s := w.GetPossibleModules(previous, next, d)
if len(s) == len(next.Superposition) {
// Same state as before, no reason to recurse further
continue
} else {
// New superposition detected, we need to go deeper and remove
// impossible modules from the neighbor tiles
next.Superposition = s
}
// Check if we have a contradiction
if len(next.Superposition) == 0 {
return ErrNoSolution
}
w.History = append(w.History, next)
err := w.Recurse()
if err != nil {
return err
}
w.History = w.History[:len(w.History)-1]
}
return nil
}
// GetPossibleModules returns a list of modules that are possible when traveling
// from slot "a" to slot "b" with the provided direction.
func (w *Wave) GetPossibleModules(a, b *Slot, d Direction) []*Module {
res := make([]*Module, 0)
for _, m := range b.Superposition {
if w.IsPossibleFn(m, a, b, d) {
res = append(res, m)
}
}
// Slot "a" has a state that does not allow any of the modules in slot "b".
return res
}
// GetSlot returns the slot at the given coordinates in this wave function.
func (w *Wave) GetSlot(x, y int) *Slot {
return w.PossibilitySpace[x+y*w.Width]
}
// HasVisited checks if the given slot has been visited during the current
// collapse iteration. This is used to prevent infinite recursion.
func (w *Wave) HasVisited(s *Slot) bool {
for _, h := range w.History {
if h == s {
return true
}
}
return false
}
// IsCollapsed checks if the given slot is collapsed. Either in a contradiction
// state or to a single possible value.
func (w *Wave) IsCollapsed() bool {
for _, s := range w.PossibilitySpace {
if len(s.Superposition) > 1 {
return false
}
}
return true
}
// HasNeighbor checks if the given slot has a neighbor in the given direction
// (edges of the grid don't have neighbors).
func (w *Wave) HasNeighbor(s *Slot, d Direction) bool {
switch d {
case Up:
return s.Y > 0
case Down:
return s.Y < w.Height-1
case Left:
return s.X > 0
case Right:
return s.X < w.Width-1
}
return false
}
// GetNeighbor returns the slot in the given direction from the given slot.
func (w *Wave) GetNeighbor(s *Slot, d Direction) *Slot {
switch d {
case Up:
return w.GetSlot(s.X, s.Y-1)
case Down:
return w.GetSlot(s.X, s.Y+1)
case Left:
return w.GetSlot(s.X-1, s.Y)
case Right:
return w.GetSlot(s.X+1, s.Y)
}
return nil
}
// Export takes the current state of the wave collapse function and exports it
// as an image. Any slots that have not been collapsed will be transparent.
// Contradictions will be red.
func (w *Wave) ExportImage() image.Image {
u := w.Input[0].Image.Bounds().Max.X
v := w.Input[0].Image.Bounds().Max.Y
img := image.NewRGBA(image.Rect(0, 0, w.Width*u, w.Height*v))
for _, s := range w.PossibilitySpace {
if len(s.Superposition) == 1 {
draw.Draw(img,
image.Rect(s.X*u, s.Y*v, (s.X+1)*u, (s.Y+1)*v),
s.Superposition[0].Image, image.ZP, draw.Over)
}
if len(s.Superposition) == 0 {
c := color.RGBA{255, 0, 0, 255}
for x := s.X * u; x < (s.X+1)*u; x++ {
for y := s.Y * v; y < (s.Y+1)*v; y++ {
img.Set(x, y, c)
}
}
}
}
return img
}