-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglsr.sh
executable file
·375 lines (321 loc) · 9 KB
/
glsr.sh
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/bash
# Go Left, Straight, or Right?
# Copyright (c) 2018 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERNHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
VERSION=0.2.0
R_INTN=1
# maximum routes, default is ~10% of terminal width
MAX_R="$(($(tput cols) * 10 / 100))"
# probability of actions
P_TURN=10
# P_SPLIT determines if it's going to split, if so, then P_SPLIT3 determines if
# it's going to splitting into 3 routes or just 2 routes.
P_SPLIT=5
P_SPLIT3=5
P_NEW=5
P_DEADEND=5
COLOR=1
DELAY=0.05
MAX_STEPS=0
SHOW_MARKS=1
HELP="Usage: $(basename $0) [OPTIONS]
Go Left, Straight, or Right?
Options:
-n [int > 0] initial number of routes (Default: $R_INTN)
-m [int > 0] maximum routes (Default: $MAX_R)
-t [float] walking interval in seconds (Default: $DELAY)
-s [int] max steps (Default: $MAX_STEPS)
-T [0-100] probability of turning (Default: $P_TURN)
-S [0-100] probability of splitting (Default: $P_SPLIT)
-3 [0-100] probability of splitting into 3 routes if splitting
(Default: $P_SPLIT3)
-N [0-100] probability of new route (Default: $P_NEW)
-Z [0-100] probability of hitting dead end (Default: $P_DEADEND)
-C no colors
-M no marks
-h this help message
-v print version number
"
parse()
{
while getopts "n:m:t:s:T:S:3:N:Z:CMhv" arg; do
case $arg in
n)
if ((OPTARG <= 0)); then
echo "-$arg: argument must be > 0" >&2
exit 1
fi
R_INTN="$OPTARG"
;;
m)
if ((OPTARG <= 0)); then
echo "-$arg: argument must be > 0" >&2
exit 1
fi
MAX_R="$OPTARG"
;;
t)
DELAY="$OPTARG"
;;
s)
MAX_STEPS="$OPTARG"
;;
T)
if ((OPTARG < 0 || OPTARG > 100)); then
echo "-$arg: argument must be in between 0 and 100" >&2
exit 1
fi
P_TURN="$OPTARG"
;;
S)
if ((OPTARG < 0 || OPTARG > 100)); then
echo "-$arg: argument must be in between 0 and 100" >&2
exit 1
fi
P_SPLIT="$OPTARG"
;;
3)
if ((OPTARG < 0 || OPTARG > 100)); then
echo "-$arg: argument must be in between 0 and 100" >&2
exit 1
fi
P_SPLIT3="$OPTARG"
;;
N)
if ((OPTARG < 0 || OPTARG > 100)); then
echo "-$arg: argument must be in between 0 and 100" >&2
exit 1
fi
P_NEW="$OPTARG"
;;
Z)
if ((OPTARG < 0 || OPTARG > 100)); then
echo "-$arg: argument must be in between 0 and 100" >&2
exit 1
fi
P_DEADEND="$OPTARG"
;;
C)
COLOR=0
;;
M)
SHOW_MARKS=0
;;
h)
echo -e "$HELP"
exit 0
;;
v)
echo "$(basename -- "$0") $VERSION"
exit 0
esac
done
if ((R_INTN > MAX_R)); then
echo 'initial number of routes can not be larger than maximum:' \
"$R_INTN > $MAX_R" >&2
exit 1
fi
}
check()
{
local MIN_BASH_VER=4
if ((BASH_VERSINFO[0] < MIN_BASH_VER)); then
echo "$(basename ${BASH_SOURNE[0]}) only supports" \
"Bash $MIN_BASH_VER+" >&2
exit 1
fi
}
reset_marks()
{
# MN: count
# MS: mark symbol
# MP: mark position
# MC: mark color
MN=0
declare -ga MS=() MP=() MC=()
}
new_mark()
{
MS+=("$1")
MP+=("$2")
((COLOR)) && MC+=("$3")
((MN++))
}
new_r()
{
((COLOR)) && RC+=($((31 + RANDOM * 6 / 32768)))
RD+=($((RANDOM * 3 / 32768)))
RP+=($((RANDOM * COLS / 32768)))
RN="${#RD[@]}"
((SHOW_MARKS)) || return
new_mark o ${RP[RN - 1]} $((COLOR ? RC[RN - 1] : 0))
}
init()
{
local i
# line length
COLS="$(tput cols)"
STEPS=0
# routes
# RN: count
# RC: color 31-36
# RD: direction value
# -> offset for walk() / calculating new position
# : RDS: direction symbol for draw()
# Going/Meaning
# 0 -> -1: / Left
# 1 -> 0: | Straight
# 2 -> 1: \ Right
# RDS: direction symbols
# RP: position, 0-based
RN=0
declare -ga RC=() RD=() RP=()
declare -ga RDS=([0]='/' [1]='|' [2]='\')
((COLOR)) && RDS[2]='\\'
for ((i = 0; i < $R_INTN; i++)); do
new_r
done
((SHOW_MARKS)) && reset_marks
}
form_walk()
{
local i c d s p
declare -ga L=()
for ((i = 0; i < RN; i++)); do
((COLOR)) && c="${RC[i]}"
d="${RD[i]}"
s="${RDS[d]}"
p="${RP[i]}"
((COLOR)) && L[p]="\\e[1;${c}m$s\\e[0m" || L[p]="$s"
done
}
form_update()
{
((SHOW_MARKS)) || return
local i s p
# place marks
for ((i = 0; i < MN; i++)); do
s="${MS[d]}"
p="${MP[i]}"
((COLOR)) && L[p]="\\e[1;${MC[i]}m$s\\e[0m" || L[p]="$s"
done
reset_marks
}
draw()
{
local i line
# fill up blanks
for ((i = 0; i < COLS; i++)); do
: ${L[i]:= }
done
# join the symbols
printf -v line '%s' "${L[@]}"
((COLOR)) && echo -e "$line" || echo "$line"
}
walk()
{
local i d p
# walk one step
for ((i = 0; i < RN; i++)); do
d="${RD[i]}"
p="${RP[i]}"
# boundary check / bounce off edges
((p <= 0 && d == 0)) && d=2 && p=-1
((p >= COLS - 1 && d == 2)) && d=0 && p="$COLS"
((p += d - 1))
RD[i]="$d"
RP[i]="$p"
done
((STEPS++))
}
update()
{
local i d p
# dead ends
if ((RN > 0 && RANDOM * 100 / 32768 < P_DEADEND)); then
((i = RANDOM * RN / 32768))
new_mark x ${RP[i]} $((COLOR ? RC[i] : 0))
((COLOR)) && unset RC[i]
unset RD[i]
unset RP[i]
# resetting indexes
((COLOR)) && RC=("${RC[@]}")
RD=("${RD[@]}")
RP=("${RP[@]}")
RN="${#RD[@]}"
fi
# turn
for ((i = 0; i < RN; i++)); do
((RANDOM * 100 / 32768 >= P_TURN)) && continue
# only two possible directions to turn
((d=(${RD[i]} + (RANDOM * 2 / 32768) + 1) % 3))
# adjust position for new direction (!= 1: straight)
((d != 1)) && ((RP[i] -= d - 1))
((RD[i] = d))
done
# if splitting, only splitting one route
if ((RN < MAX_R && RANDOM * 100 / 32768 < P_SPLIT)); then
((i = RANDOM * RN / 32768))
new_mark '*' ${RP[i]} $((COLOR ? RC[i] : 0))
d="${RD[i]}"
p="${RP[i]}"
# do 3 split if MAX_R allows and at odds
if ((RN - 1 < MAX_R && RANDOM * 100 / 32768 < P_SPLIT3)); then
# splitting into 3 routes
((COLOR)) && \
RC+=($((31 + RANDOM * 6 / 32768)) $((31 + RANDOM * 6 / 32768)))
RD+=($(((d + 1) % 3)) $(((d + 2) % 3)))
RP+=($p $p)
else
((split_d=(${RD[i]} + (RANDOM * 2 / 32768) + 1) % 3))
((COLOR)) && RC+=($((31 + RANDOM * 6 / 32768)))
RD+=($split_d)
RP+=($p)
fi
RN="${#RD[@]}"
fi
# new route
((RN < MAX_R && RANDOM * 100 / 32768 < P_NEW)) && new_r
}
roll()
{
stty -echo
while REPLY=; read -t $DELAY -n 1; [[ -z "$REPLY" ]] ; do
walk
form_walk
if ((MAX_STEPS > 0 && STEPS >= MAX_STEPS)); then
break
fi
update
form_update
draw
done
stty echo
# empty standard input
read -t 0.001 && cat </dev/stdin>/dev/null; :
}
main()
{
check
parse "$@"
init
roll
}
main "$@"