-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.go
More file actions
276 lines (246 loc) · 6.39 KB
/
Copy pathengine.go
File metadata and controls
276 lines (246 loc) · 6.39 KB
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
package main
import (
"math/rand"
"os"
"strings"
)
var matrixSymbols = []rune("0123456789@#$%^&*+-=/\\|:<>?")
const (
katakanaStart = 0xFF66
katakanaCount = 56
asciiStart = 33
asciiCount = 94
glitchChance = 20
spawnOffset = 5
matrixSymbolChance = 3
matrixSymbolTotal = 10
tailDivisor = 3
envLang = "LANG"
envLcAll = "LC_ALL"
envTerm = "TERM"
termDumb = "dumb"
termLinux = "linux"
utf8Suffix = "utf-8"
)
// Cell represents a single point on the screen.
type Cell struct {
Char rune
Head bool
Tail int
Flash bool
T1Len int
T2Len int
CRTBleed bool
CRTChar rune
}
// Column tracks the state of a single falling matrix line.
type Column struct {
YPos int
Tail int
PendingFlash bool
T1Len int
T2Len int
}
// Engine manages the state of the matrix effect.
type Engine struct {
Width int
Height int
Grid []Cell
Columns []Column
Theme Theme
ASCIIOnly bool
StarMode bool
StarCount int
CRTMode bool
ShowHelp bool
GradientMode int
UseTrueColor bool
T1Percent int
T2Percent int
}
// NewEngine creates and initializes a new matrix engine.
func NewEngine(w, h int, theme Theme, ascii bool) *Engine {
if !ascii {
lang := os.Getenv(envLang)
lcAll := os.Getenv(envLcAll)
term := os.Getenv(envTerm)
if !strings.Contains(strings.ToLower(lang), utf8Suffix) &&
!strings.Contains(strings.ToLower(lcAll), utf8Suffix) {
ascii = true
}
if term == termDumb || term == termLinux {
ascii = true
}
}
e := &Engine{Theme: theme, ASCIIOnly: ascii, StarCount: DefaultStarCnt}
e.resize(w, h)
return e
}
// resize re-initializes the grid and columns for the given dimensions.
func (e *Engine) resize(w, h int) {
if w <= 0 || h <= 0 {
w, h = 1, 1
}
e.Width = w
e.Height = h
e.Grid = make([]Cell, w*h)
e.Columns = make([]Column, w)
for i := 0; i < w; i++ {
e.initColumnLengths(i)
e.Columns[i].YPos = rand.Intn(h) - h // start above screen
}
}
// initColumnLengths assigns random total and gradient tail lengths.
func (e *Engine) initColumnLengths(x int) {
minTail := e.Height / 2
tailRange := e.Height - minTail
e.Columns[x].Tail = rand.Intn(max(1, tailRange)) + minTail
tMax := max(1, e.Columns[x].Tail/tailDivisor)
e.Columns[x].T1Len = rand.Intn(tMax) + tailDivisor
e.Columns[x].T2Len = rand.Intn(tMax) + tailDivisor
}
// randomRange returns a random integer in [min, max] inclusive.
func randomRange(min, max int) int {
if min >= max {
return min
}
// rand.Intn(n) is exclusive of n. We add an inclusive offset
// to ensure the max value can actually be generated.
inclusiveOffset := 1
return rand.Intn(max-min+inclusiveOffset) + min
}
// randChar returns a random display character.
func (e *Engine) randChar() rune {
if e.ASCIIOnly {
return rune(randomRange(asciiStart, asciiStart + asciiCount - 1))
}
if rand.Intn(matrixSymbolTotal) < matrixSymbolChance {
return matrixSymbols[rand.Intn(len(matrixSymbols))]
}
// Half-width katakana
kEnd := katakanaStart + katakanaCount - 1
return rune(randomRange(katakanaStart, kEnd))
}
// Step advances the matrix effect by one frame.
func (e *Engine) Step() {
for x := 0; x < e.Width; x++ {
e.Columns[x].YPos++
y := e.Columns[x].YPos
tail := e.Columns[x].Tail
if y-tail > e.Height {
e.Columns[x].YPos = randomRange(-spawnOffset, -1)
e.initColumnLengths(x)
e.Columns[x].PendingFlash = false
continue
}
for cy := 0; cy < e.Height; cy++ {
idx := x*e.Height + cy
if cy == y {
e.Grid[idx].Char = e.randChar()
e.Grid[idx].Head = true
e.Grid[idx].Tail = 0
e.Grid[idx].Flash = e.Columns[x].PendingFlash
e.Grid[idx].T1Len = e.Columns[x].T1Len
e.Grid[idx].T2Len = e.Columns[x].T2Len
e.Columns[x].PendingFlash = false
} else if cy < y && cy >= y-tail {
e.Grid[idx].Head = false
e.Grid[idx].Tail = y - cy
e.Grid[idx].Flash = false
e.Grid[idx].T1Len = e.Columns[x].T1Len
e.Grid[idx].T2Len = e.Columns[x].T2Len
if rand.Intn(glitchChance) == 0 {
e.Grid[idx].Char = e.randChar()
}
} else {
e.Grid[idx].Char = ' '
e.Grid[idx].Head = false
e.Grid[idx].Tail = -1
e.Grid[idx].Flash = false
e.Grid[idx].T1Len = 0
e.Grid[idx].T2Len = 0
}
}
}
if e.StarMode {
e.generateStars()
}
for i := 0; i < len(e.Grid); i++ {
e.Grid[i].CRTBleed = false
e.Grid[i].CRTChar = ' '
}
if e.CRTMode {
for x := 1; x < e.Width; x++ {
for y := 0; y < e.Height; y++ {
idx := x*e.Height + y
leftIdx := (x-1)*e.Height + y
if e.Grid[leftIdx].Flash {
e.Grid[idx].CRTBleed = true
e.Grid[idx].CRTChar = e.Grid[leftIdx].Char
}
}
}
}
}
// generateStars randomly selects cells to flash based on StarCount.
func (e *Engine) generateStars() {
var candidates []int
for i := 0; i < len(e.Grid); i++ {
c := e.Grid[i]
if c.Head || (c.Tail >= 0 && c.Tail < c.T1Len) {
candidates = append(candidates, i)
}
}
if len(candidates) > 0 {
n := randomRange(0, e.StarCount)
if n > len(candidates) {
n = len(candidates)
}
rand.Shuffle(len(candidates), func(i, j int) {
candidates[i], candidates[j] = candidates[j], candidates[i]
})
for i := 0; i < n; i++ {
e.Grid[candidates[i]].Flash = true
}
}
}
// TriggerFlash sets a random column to flash next tick.
func (e *Engine) TriggerFlash() {
if e.Width == 0 {
return
}
x := rand.Intn(e.Width)
e.Columns[x].PendingFlash = true
}
// IncrementStarCount increases the star count up to maxCnt.
func (e *Engine) IncrementStarCount(maxCnt int) {
if e.StarCount < maxCnt {
e.StarCount++
}
}
// DecrementStarCount decreases the star count down to minCnt.
func (e *Engine) DecrementStarCount(minCnt int) {
if e.StarCount > minCnt {
e.StarCount--
}
}
// ToggleStarMode toggles the star background effect.
func (e *Engine) ToggleStarMode() {
e.StarMode = !e.StarMode
}
// ToggleCRTMode toggles the CRT bleed effect.
func (e *Engine) ToggleCRTMode() {
e.CRTMode = !e.CRTMode
}
// ToggleHelp toggles the help menu display.
func (e *Engine) ToggleHelp() {
e.ShowHelp = !e.ShowHelp
}
// ToggleGradientMode cycles through the available gradient modes.
func (e *Engine) ToggleGradientMode(modeCount int) {
e.GradientMode = cycleNext(e.GradientMode, modeCount)
}
// ToggleTrueColor toggles TrueColor vs Dithering rendering.
func (e *Engine) ToggleTrueColor() {
e.UseTrueColor = !e.UseTrueColor
}