-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomata.go
199 lines (161 loc) · 3.91 KB
/
automata.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
package main
import (
"image/color"
"math"
)
type CellType int
const (
Void = iota
Liquid
Solid
)
const minDraw = 0.01
const maxDraw = 1.1
const maxSpeed = 2.0
const maxVolume = 1.0
const maxPressure = 0.15
const minFlow = 0.001
const flowCoef = 0.6
const minVolume = 0.0001 // Treat cells with vol < minVol as void
const colorSlope = (50 - 0) / (1.0 - 0.0)
type Cell struct {
x, y int
cellType CellType
volume float64
deltaVolume float64
}
func (c* Cell) GetColor() color.RGBA {
if c.cellType == Liquid {
if c.volume <= 0 {
return color.RGBA{0xff, 0xff, 0xff, 0xff}
}
m := constrain(c.volume, minDraw, maxDraw)
r := uint8(50)
g := uint8(50)
var b uint8
b = uint8(mapRange(m, 0.01, 1, 255, 200))
r = uint8(constrain(mapRange(m, 0.01, 1, 240, 50), 50, 240))
g = r
b = uint8(constrain(float64(b), 140, 255))
return color.RGBA{r, g, b, 255}
} else if c.cellType == Solid {
return color.RGBA{0x00, 0x00, 0x33, 0xff}
}
return color.RGBA{0xff, 0xff, 0xff, 0xff}
}
func (c* Cell) GetPressure() float64 {
pressure := c.volume - maxVolume
if pressure < 0 {
pressure = 0
}
return pressure
}
func (c* Cell) GetVolume() float64 {
volume := c.volume
if volume > maxVolume {
volume = maxVolume
}
return volume
}
func (c* Cell) Tick() {
c.volume += c.deltaVolume
c.deltaVolume = 0
}
func NewCell(x, y int, cellType CellType, volume float64) Cell {
return Cell{x, y, cellType, volume, 0}
}
func simulate(cells [][]Cell) {
for x := 1; x < len(cells) - 1; x++ {
for y := 1; y < len(cells[x]) - 1; y++ {
if cells[x][y].cellType == Solid {
continue
}
var this = &cells[x ][y ]
var left = &cells[x-1][y ]
var right = &cells[x+1][y ]
var up = &cells[x ][y-1]
var bottom = &cells[x ][y+1]
if FallDown(this, bottom) {
if Spread(this, left, right) {
Decompress(this, up)
}
}
}
}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
if cells[x][y].cellType != Solid {
if cells[x][y].volume > minVolume {
cells[x][y].cellType = Liquid
} else {
cells[x][y].cellType = Void
}
}
cells[x][y].Tick()
}
}
}
func FallDown(c1* Cell, c2* Cell) bool {
if c2.cellType != Solid {
remaining_mass := c1.volume - c1.deltaVolume
flow := getStableState(remaining_mass + c2.volume) - c2.volume
flow *= 2.0
if flow > minFlow { flow *= flowCoef }
flow = constrain(flow, 0, math.Min(maxSpeed, remaining_mass))
c1.deltaVolume -= flow
c2.deltaVolume += flow
}
return c1.volume - c1.deltaVolume > 0
}
func Spread(c, left, right *Cell) bool {
if left.cellType != Solid {
flow := (c.volume - left.volume) / 1
if flow > minFlow { flow *= flowCoef }
flow = constrain(flow, 0, c.volume - c.deltaVolume)
c.volume -= flow
left.volume += flow
}
if c.volume - c.deltaVolume <= 0 { return false }
if right.cellType != Solid {
flow := (c.volume - right.volume) / 1
if flow > minFlow { flow *= flowCoef }
flow = constrain(flow, 0, c.volume - c.deltaVolume)
c.volume -= flow
right.volume += flow
}
return c.volume - c.deltaVolume > 0
}
func Decompress(c, up *Cell) bool {
if c.cellType != Solid {
remaining_mass := c.volume - c.deltaVolume
flow := remaining_mass - getStableState(remaining_mass + up.volume)
flow /= 2.0
if flow > minFlow { flow *= flowCoef }
flow = constrain(flow, 0, math.Min(maxSpeed, remaining_mass))
c.deltaVolume -= flow
up.deltaVolume += flow
}
return c.volume - c.deltaVolume > 0
}
func getStableState(totalMass float64) float64 {
if totalMass <= 1 {
return 1
} else if totalMass < 2 * maxVolume + maxPressure {
return (maxVolume * maxVolume + totalMass * maxPressure) / maxVolume - maxPressure
} else {
return (totalMass + maxPressure) / 2
}
}
func constrain(x, a, b float64) float64 {
result := x
if x > b {
result = b
}
if result < a {
result = a
}
return result
}
func mapRange(t, a, b, c, d float64) float64 {
return c + ((d - c) / (b - a)) * (t - a)
}