-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.go
271 lines (232 loc) · 6.35 KB
/
scene.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
package main
import (
"bufio"
"fmt"
"github.com/muesli/termenv"
"github.com/ojrac/opensimplex-go"
"io"
"os"
"path"
"unicode"
)
// Style represents a pair of colors, foreground and background.
type Style struct {
fg termenv.Color
bg termenv.Color
}
// Convert is a wrapper for termenv.Profile.Convert, it converts the foreground
// and background colors of a Style to be within a given terminal's Profile.
func (s Style) Convert(profile termenv.Profile) Style {
return Style{
profile.Convert(s.fg),
profile.Convert(s.bg),
}
}
// String fills the Stringer interface and returns an ANSI escape code
// formatted to produce text in the specified Style.
func (s Style) String() string {
if s.fg == nil {
s.fg = termenv.NoColor{}
}
if s.bg == nil {
s.bg = termenv.NoColor{}
}
// TODO: Looks like the background color is overriding the foreground color. What's the proper way to do this?
return fmt.Sprintf("%s%s;%sm", termenv.CSI, s.fg.Sequence(false), s.bg.Sequence(true))
}
// Scene holds all the necessary information to make a dynamic, weather-y ASCII
// landscape.
type Scene struct {
foreground [][]rune
background [][]rune
windground [][]rune
depth [][]rune
forecast Forecast
generator opensimplex.Noise
Width int
Height int
}
const (
fgPath = "foreground.txt"
depthPath = "depth.txt"
windPath = "wind.txt"
earlyPath = "early.txt"
morningPath = "morning.txt"
afternoonPath = "afternoon.txt"
nightPath = "night.txt"
)
var timeToPath = []string{
earlyPath,
morningPath,
afternoonPath,
nightPath,
}
// NewScene accepts a path to a folder containing foreground, windground,
// depth, and background files and loads them from disk as needed to generate
// imagery for the given forecast.
func NewScene(scenePath string, forecast Forecast) (Scene, error) {
var s Scene
var err error
s.forecast = forecast
s.generator = opensimplex.NewNormalized(0)
s.foreground, err = readRunesFromFile(path.Join(scenePath, fgPath))
if err != nil {
return s, err
}
s.depth, err = readRunesFromFile(path.Join(scenePath, depthPath))
if err != nil {
return s, err
}
s.windground, err = readRunesFromFile(path.Join(scenePath, windPath))
if err != nil {
return s, err
}
bgPath := timeToPath[forecast.time]
s.background, err = readRunesFromFile(path.Join(scenePath, bgPath))
if err != nil {
return s, err
}
s.normalize()
/*m.depthData = make([][]uint8, 0)
for i := range depthRunes {
m.depthData = append(m.depthData, make([]uint8, 0))
for j := range depthRunes[i] {
m.depthData[i] = append(m.depthData[i], uint8(depthRunes[i][j])-48)
}
}*/
return s, nil
}
// normalize adjusts the foreground, windground, depth map, and background to
// have matching widths and heights. It adjusts by cropping to the shortest
// number of lines between the four and adjusts each line to the shortest of
// any lines in any of the four maps.
func (s *Scene) normalize() {
scenes := [...][][]rune{s.foreground, s.windground, s.depth, s.background}
s.Height = 999999999
for i := range scenes {
if len(scenes[i]) < s.Height {
s.Height = len(scenes[i])
}
}
for i := range scenes {
scenes[i] = scenes[i][:s.Height]
}
s.Width = 999999999
for j := 0; j < s.Height; j++ {
for i := range scenes {
if len(scenes[i][j]) < s.Width {
s.Width = len(scenes[i][j])
}
}
for i := range scenes {
scenes[i][j] = scenes[i][j][:s.Width]
}
}
// TODO: Change this to a map? Map can be iterated over and still referenced by name.
s.foreground = scenes[0]
s.windground = scenes[1]
s.depth = scenes[2]
s.background = scenes[3]
}
func (s Scene) GetCell(x, y, time int) (rune, Style) {
fx := float64(x)
fy := float64(y)
ftime := float64(time)
fcloud := float64(s.forecast.cloudiness)
fwind := float64(s.forecast.windiness)
frain := float64(s.forecast.raininess)
char := ' '
style := Style{
termenv.ANSI256Color(255),
termenv.ANSI256Color(232),
}
// Out of bounds
if y >= s.Height || y < 0 {
return char, Style{}
}
if x >= s.Width || x < 0 {
return char, Style{}
}
depth := uint8(s.depth[y][x] - 48)
// Char selection
char = s.foreground[y][x]
// Pull from wind map if the current cell is windswept.
if fwind > 0.0 && s.generator.Eval3(fx/40+fwind/8*ftime*2, fy/20, ftime/5+fwind/10*ftime/2) > 0.6 {
char = s.windground[y][x]
}
// Depth 9 is considered "transparent," so use the char from the background.
if depth == 9 {
char = s.background[y][x]
}
if frain > 0.0 && 0.1+frain/20 > s.generator.Eval3(fx+ftime*fwind*3, fy-ftime*5, ftime/15) {
char = '|'
if fwind > 1.0 {
char = '/'
}
}
// Style selection
// Calculate fog
fog := (depth - 1) + uint8(s.forecast.visibility-1)*2
if s.forecast.visibility == 0 {
fog = (depth + uint8(s.forecast.visibility)) / 2
}
// Don't show fog in the sky during the night.
if depth == 9 && (s.forecast.time == Night || s.forecast.time == EarlyMorning) {
fog = 0
}
// Add clouds
cloudiness := 0
if depth > 8 && fcloud/5 > s.generator.Eval3(fx/50+ftime/24+ftime*(fwind/8), fy/12, 1000+ftime/80) {
cloudiness += 10
}
if depth > 6 && fcloud/7 > s.generator.Eval3(fx/20+ftime/14+ftime*(fwind/8), fy/6, 0+ftime/80) {
cloudiness += 10
}
// At night, fog obscures distant objects with darkness.
if s.forecast.time == Night || s.forecast.time == EarlyMorning {
style.fg = termenv.ANSI256Color(255 - fog - fog/2)
} else {
// Merge fog with clouds during daytime.
cloudiness += int(fog)
}
if cloudiness > 23 {
cloudiness = 23
}
if cloudiness > 0 {
style.bg = termenv.ANSI256Color(232 + cloudiness)
} else {
style.bg = termenv.ANSI256Color(232 + fog)
}
return char, style
}
// readRunesFromFile reads the file at the given path and reads it character by
// character, line by line into a slice of slices of runes.
func readRunesFromFile(filepath string) ([][]rune, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
br := bufio.NewReader(file)
img := make([][]rune, 0)
img = append(img, make([]rune, 0))
for {
r, s, err := br.ReadRune()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
// Invalid unicode characters are skipped.
if r == unicode.ReplacementChar && s == 1 {
continue
}
// Start a new slice on newline, otherwise append character to current slice.
if r == '\n' {
img = append(img, make([]rune, 0))
} else {
img[len(img)-1] = append(img[len(img)-1], r)
}
}
return img, nil
}