blob: b9c6c983571401730387b3ce751d4005ff48b597 (
plain)
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
|
#!/bin/bash
###
# ASCII fire
# TODO:
# Better gradient for cooling flame.
r=$'\e[31m'
y=$'\e[33m'
br=$'\e[1;31m'
by=$'\e[1;33m'
c=$'\e[0m'
bs=$'\e[1;30m'
#w=$'\e[37m'
flame_gradient=(
# "$w#$c"
"$by@$c"
"$by#$c"
"$y#$c"
"$y@$c"
"$br@$c"
"$br#$c"
"$r#$c"
# "$bs($c"
# "$bs($c"
# "$bs)$c"
# "$bs.$c"
" "
)
# TODO: Use 256 color palette.
declare -ia cells=()
##
# init
tput clear
n_rows=$(tput lines)
n_cols=$(tput cols)
printf 'rows: %d\ncols: %d\n' "$n_rows" "$n_cols"
#for cell in $(seq 0 $(((n_rows * n_cols) - 1)))
for ((cell=0;cell<n_rows*n_cols;++cell))
do cells[cell]=0
done
###
# It is best if the bottom row is row #0.
# Even if the terminal is resized,
# there will always be a row #0.
# Of course this means the rows will be drawn in reverse order.
new_base_row(){
# TODO: If the terminal is resized, re-evaluate the number of columns.
# for cell in $(seq 0 $((n_cols-1)))
declare -i delta=32 # ±16
for ((cell=0;cell<n_cols;++cell))
do cells[cell]=$(((cells[cell]+RANDOM%delta-(delta/2)+256)%256))
done
# TODO: Adjust delta to size of terminal? Not sure how.
}
# end init
##
num_to_cell(){
# Cells have a value between 0 and 255, inclusive.
# Arrays are 0-indexed.
echo -n "${flame_gradient[(255-$1)*${#flame_gradient[@]}/256]}"
# printf makes no apparent difference.
}
##
# Compute the values of the indicated row
# from the values of the row below.
# Indicated row should not be the bottom row.
# TODO: Does a lot of repeatedly computing the same numbers; could be avoided.
burn_row(){
declare -i row_number=$1
declare -i cell
for ((cell=row_number*n_cols;cell<(row_number+1)*n_cols;++cell))
do
cell_l=${cells[$cell-$n_cols-1]}
cell_b=${cells[$cell-$n_cols]}
cell_r=${cells[$cell-$n_cols+1]}
# The flames cool between rows.
cells[cell]=$(((cell_l+cell_b+cell_r)/3-(256/n_rows)))
[[ ${cells[cell]} -lt 0 ]] && cells[cell]=0
done
}
# By burning iteratively, the entire array can be processed at once.
# It is only a matter of direction.
burn(){
declare -i cell cell_l cell_b cell_r
for ((cell=${#cells[@]}-1;cell>=n_cols;--cell))
do
cell_l=${cells[$cell-$n_cols-1]}
cell_b=${cells[$cell-$n_cols]} # cell_l from previous iteration.
cell_r=${cells[$cell-$n_cols+1]} # cell_b from previous iteration.
# The flames cool between rows.
cells[cell]=$(((cell_l+cell_b+cell_r)/3-(256/n_rows)))
[[ ${cells[cell]} -lt 0 ]] && cells[cell]=0
done
}
print_all(){
# Wrong order. This burns downwards.
declare -i cell
for cell in "${cells[@]}"
do
num_to_cell $cell
done
}
print_rows(){
declare -i row
for ((row=n_rows-1;row>=0;--row))
do
# Print rows top to bottom:
tput cup $((n_rows-row)) 0
print_row $row
# Print rows bottom to top:
# tput cup $row 0
# print_row $((n_rows-1-row))
done
# TODO:
# It should be more efficient to print all at once.
}
print_row(){
declare -i row=$1
declare -i cell
for ((cell=row*n_cols;cell<(row+1)*n_cols;++cell))
do
num_to_cell "${cells[cell]}"
done
}
test_fire() {
# Saturate base row.
for ((cell=0;cell<n_cols;++cell))
do cells[cell]=255
done
# Print buffer.
tput cup 0 0
print_rows
# Burn.
for ((counter=0;counter<n_rows;++counter))
do
burn
print_rows
done
}
##
# main
#test_fire
#exit
new_base_row
while :
do
burn
print_rows
new_base_row
tput cup 0 0
# sleep 0.2
# sleep 1
done
|