-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashtaker.sh
More file actions
executable file
·479 lines (418 loc) · 12.2 KB
/
bashtaker.sh
File metadata and controls
executable file
·479 lines (418 loc) · 12.2 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/env bash
. "./assets/vars.sh"
declare -A keys
declare -A tiles
declare -A spikes
declare -A sprites
declare -A font
redraw() {
while :;do
shopt -s checkwinsize; (:;:)
if [[ $COLUMNS -lt $map_width || $LINES -lt $map_height ]]; then
printf '\e[2J'
printf "%s\n%s\n" "Terminal window is too small!" "please resize it and press anything. "
read -rsn1
else
break
fi
done
map_width=$((X_TILES * TILE_SIZE_X))
map_height=$((Y_TILES * TILE_SIZE_Y))
margin_x=$(((COLUMNS - map_width)/2))
margin_y=$(((LINES - map_height)/2))
draw_grid
draw_status
draw_keybinds
}
clean() {
printf "%s%s" "$SHOW_CURSOR" "$RESET"
stty echo icanon
}
check_file() {
local last_id
local file
local paths=("$@")
((last_id=${#paths[@]} - 1))
local url=${paths[last_id]}
unset "paths[last_id]"
for path in "${paths[@]}"; do
[ -f "$path" ] && file="$path"
done
if [ -z "$file" ];then
printf "%sNo config file found. \nCopying from github repo into %s\n" "$YELLOW" "${paths[0]}$RESET"
curl "$url" -o "${paths[0]}"
printf "%sCHECK DOWNLOADED FILE%s(%s) and run this script again\n%s" "$RED" "$YELLOW" "${paths[0]}" "$RESET"
exit 1
fi
echo "$file"
}
parse_config() {
local config
config=$(check_file "${CONFIG_PATHS[@]}" "$DEFAULT_CONFIG_URL")
mapfile -t tile_sprite < <(jq -r '.tile[]' "$config")
TILE_SIZE_X=${#tile_sprite[0]}
TILE_SIZE_Y=${#tile_sprite[@]}
SPIKE_SPRITE_POSITIONS=()
for y in "${!tile_sprite[@]}"; do
for ((x=0; x<TILE_SIZE_X; x++)); do
if [[ "${tile_sprite[y]:$x:1}" == "x" ]]; then
CHAR_X="$x"
CHAR_Y="$y"
tile_sprite[y]="${tile_sprite[y]/x/ }"
elif [[ "${tile_sprite[y]:$x:1}" == "s" ]]; then
SPIKE_SPRITE_POSITIONS+=("$x,$y")
tile_sprite[y]="${tile_sprite[y]/s/ }"
fi
done
done
# Make binds array from config file
eval "$(jq -r '
.keys | to_entries |
map("[\(.value|@sh)]=\(.key|@sh)") |
["keys=("] + . + [")"] |
.[]' "$config"
)"
eval "$(jq -r '
.sprites | to_entries |
map("sprites[\(.key|@sh)]=\(.value|@sh)") |
.[]' "$config"
)"
default_map=$(jq -r '.default_map' "$config")
font_width=$(jq -r '.font_width' "$config")
font_height=$(jq -r '.font_height' "$config")
parse_font
}
parse_font() {
local font_file
font_file=$(check_file "${FONT_PATHS[@]}" "$DEFAULT_FONT_URL")
for char in $(jq -r 'keys[]' "$font_file"); do
font["$char"]=$(jq -r --arg char "$char" '.[$char][]' "$font_file")
font["$char"]="${font["$char"]//$'\n'/}" # clean all new lines
done
}
load_map() {
local map=$1
[ -f "$map" ] || {
[ -z "$default_map" ] && exit 1
map="$default_map"
}
for var in moves spikes_move key_x key_y; do
eval $var="$(jq -r ".$var" "$map")"
done
local tile_rows=() spike_rows=()
local tile_row spike_row
mapfile -t tile_rows < <(jq -r '.tiles[]' "$map")
mapfile -t spike_rows < <(jq -r '.spikes[]' "$map")
X_TILES=${#tile_rows[0]}
Y_TILES=${#tile_rows[@]}
MAP_NAME=$(jq -r '.name' "$map")
local char
for (( y=0; y<Y_TILES; y++ )); do
tile_row=${tile_rows[$y]}
spike_row=${spike_rows[$y]}
for (( x=0; x<X_TILES; x++ )); do
char=${tile_row:$x:1}
if [[ "$char" != "_" ]]; then
case "$char" in
"p") player_x="$x"; player_y="$y";;
" ") tiles["$x,$y"]="wall";;
"r") tiles["$x,$y"]="rock";;
"s") tiles["$x,$y"]="skeleton";;
"l") tiles["$x,$y"]="lock";;
"g") tiles["$x,$y"]="girl";;
esac
fi
char=${spike_row:$x:1}
if [[ "$char" != "_" ]]; then
spikes["$x,$y"]=$char
fi
done
done
}
get_screen_position() {
local x=$1
local y=$2
local row=$((y * TILE_SIZE_Y + margin_y))
local col=$((x * TILE_SIZE_X + margin_x))
echo "$row $col"
}
get_tile_info() {
local x=$1
local y=$2
local type=${tiles["$x,$y"]}
local color char
if [[ "$player_x" == "$x" && "$player_y" == "$y" ]];then
char="${sprites["player"]}"
color="$RED"
else
case "$type" in
"rock") char="${sprites["rock"]}"; color="$WHITE";;
"skeleton") char="${sprites["skeleton"]}"; color="$WHITE";;
"lock") char="${sprites["lock"]}"; color="$YELLOW";;
"girl") char="${sprites["girl"]}"; color="$MAGENTA";;
*) char=" "; color="$BLACK";;
esac
fi
if [[ "$key_x" == "$x" && "$key_y" == "$y" ]];then
[[ "$char" == " " ]] && char="${sprites["key"]}"
color="$YELLOW"
fi
echo "$char,$color,$type"
}
get_spike_info() {
local x=$1
local y=$2
spike=" "
spike_color="$BLACK"
if [[ ${spikes["$x,$y"]} ]];then
spike="${sprites["spike"]}"
if [[ ${spikes["$x,$y"]} == 1 ]]; then
spike_color="$WHITE"
else
spike_color="$BLACK"
fi
fi
echo "$spike,$spike_color"
}
draw_tile() {
local x=$1
local y=$2
local char color type
IFS="," read -r char color type <<<"$(get_tile_info "$x" "$y")"
local spike spike_color
IFS="," read -r spike spike_color <<<"$(get_spike_info "$x" "$y")"
local row col
read -r row col <<<"$(get_screen_position "$x" "$y")"
local tile=("${tile_sprite[@]}")
[[ $type == "wall" ]] && return 0 # dont draw empty tile
# Draw Blank tile
local i
for ((i = 0; i < TILE_SIZE_Y; i++)); do
line="${tile[i]:0:TILE_SIZE_X}"
printf "\033[%d;%dH%s" $((row + i)) "$col" "$color$line$RESET"
done
# Draw char
printf "\033[%d;%dH%s" $((row + CHAR_Y )) $(( col + CHAR_X )) "$color$char$RESET"
# Draw Spikes
for spike_coords in "${SPIKE_SPRITE_POSITIONS[@]}";do
local x y
IFS="," read -r x y <<< "$spike_coords"
printf "\033[%d;%dH%s" $((row + y )) $(( col + x )) "$spike_color$spike$RESET"
done
}
draw_grid() {
printf '\e[2J'
local x y
for ((y = 0; y < Y_TILES; y++)); do
for ((x = 0; x < X_TILES; x++)); do
draw_tile "$x" "$y"
done
done
}
get_coords() {
local x=$1
local y=$2
local direction=$3
local new_x new_y
case "$direction" in
left) new_x=$((x - 1)); new_y=$y ;;
right) new_x=$((x + 1)); new_y=$y ;;
up) new_x=$x; new_y=$((y - 1)) ;;
down) new_x=$x; new_y=$((y + 1)) ;;
esac
echo "$new_x $new_y"
}
is_blocked() {
local x=$1
local y=$2
# out of bounds
if (( x < 0 || x >= X_TILES || y < 0 || y >= Y_TILES )); then
return 0
fi
# blocked by wall or rock
case "${tiles["$x,$y"]}" in
"wall"|"rock") return 0 ;;
"lock")
if [[ "$has_key" == 1 ]]; then
tiles["$x,$y"]=0
return 1
else
return 0
fi
;;
esac
return 1
}
can_push() {
local x=$1
local y=$2
local direction=$3
local new_x new_y
read -r new_x new_y <<<"$(get_coords "$x" "$y" "$direction")"
(( new_x < 0 || new_x >= X_TILES || new_y < 0 || new_y >= Y_TILES )) && return 1
[[ -n "${tiles["$new_x,$new_y"]}" ]] && return 1
return 0
}
push() {
local x=$1
local y=$2
local direction=$3
local type=$4
if ! can_push "$x" "$y" "$direction"; then
[[ "$type" == "rock" ]] && return 1
[[ "$type" == "skeleton" ]] && {
unset "tiles[\"$x,$y\"]"
draw_tile "$x" "$y"
return 0
}
fi
local new_x new_y
read -r new_x new_y <<<"$(get_coords "$x" "$y" "$direction")"
unset "tiles[\"$x,$y\"]"
draw_tile "$x" "$y"
if ! [[ $type == "skeleton" && ${spikes["$new_x,$new_y"]} == 1 ]];then
tiles["$new_x,$new_y"]="$type"
draw_tile "$new_x" "$new_y"
fi
return 0
}
draw_status() {
draw_string "$((moves / 10))$((moves % 10))" "$((LINES - font_height - 1))" 1 "$RED"
draw_string "$MAP_NAME" "$((LINES - font_height - 1))" $((COLUMNS - font_width * ${#MAP_NAME} - 1)) "$RED"
}
draw_string() {
local string=$1
local height=$2
local width=$3
local color=$4
local i
for (( i = 0; i < ${#string}; i++ )); do
draw_char "${string:i:1}" "$height" "$((width + i * font_width))" "$color"
done
}
draw_char() {
local char=${font["$1"]}
local height=$2
local width=$3
local color=$4
[ -z "$height" ] && height=0
[ -z "$width" ] && width=0
[ -z "$color" ] && color="$WHITE"
local i
for ((i = 0; i < font_height; i++)); do
line="${char:$((i * font_width)):font_width}"
printf "\033[%d;%dH%s" "$((height + i))" "$width" "$color$line$RESET"
done
}
draw_keybinds() {
local left up down right
local output
for key in $(printf "%s\n" "${!keys[@]}" | sort -r); do
case "${keys[$key]}" in
"left") left="${key}";;
"right") right="${key}";;
"up") up="${key}";;
"down") down="${key}";;
*)
output+="$RED${key}$RESET: ${keys[$key]^}, "
;;
esac
done
output="$RED$left$down$up$right$RESET: Movement, $output"
printf "\033[%d;%dH" $((LINES - 2)) $(( (COLUMNS - ${#output} / 2) / 2 ))
printf "%s" "${output%, }"
}
check_win() {
local x=$player_x
local y=$player_y
local neighbors=(
"$((x - 1)),$y"
"$((x + 1)),$y"
"$x,$((y - 1))"
"$x,$((y + 1))"
)
for pos in "${neighbors[@]}"; do
if [[ "${tiles["$pos"]}" == "girl" ]]; then
printf "\033[%d;%dH" $((Y_TILES * TILE_SIZE_Y)) 0
printf "%sYou Won!%s\n" "$GREEN" "$RESET"
exit 0
fi
done
}
check_key() {
local x=$1
local y=$2
if [[ "$has_key" == 0 && "$x" == "$key_x" && "$y" == "$key_y" ]]; then
has_key=1
unset key_x key_y
fi
}
switch_spikes() {
if [[ "$spikes_move" == 0 ]]; then
return 0
fi
local x y
for spike in "${!spikes[@]}"; do
spikes["$spike"]=$(( 1 - spikes["$spike"]))
[[ ${spikes["$spike"]} == 1 && ${tiles["$spike"]} == "skeleton" ]] && tiles["$spike"]=0 # Remove skeleton
IFS="," read -r x y <<<"$spike"
draw_tile "$x" "$y"
done
}
move() {
local direction="$1"
read -r new_x new_y <<<"$(get_coords "$player_x" "$player_y" "$direction")"
tile="${tiles["$new_x,$new_y"]}"
case "$tile" in
"rock"|"skeleton")
switch_spikes
push "$new_x" "$new_y" "$direction" "$tile"
;;
*)
if ! is_blocked "$new_x" "$new_y"; then
switch_spikes # i know that calling this twice is stupid but its the easiest
old_x=$player_x
old_y=$player_y
player_x=$new_x
player_y=$new_y
check_key "$player_x" "$player_y"
draw_tile "$old_x" "$old_y"
draw_tile "$player_x" "$player_y"
else
return
fi
;;
esac
if [[ "${spikes["$player_x,$player_y"]}" == 1 ]]; then
((moves -= 2))
else
((moves--))
fi
draw_status
check_win
}
parse_config
load_map "$1"
stty -echo -icanon time 0 min 0
printf "%s" "$HIDE_CURSOR"
trap 'clean' EXIT
# trap 'redraw' WINCH # doesnt work when blocked by another process so its shitty af
redraw
# Main Loop
while :; do
if (( "$moves" <= 0 )); then
printf "\033[%d;%dH" $((map_height + margin_y)) $(((map_width)/2 + margin_x - 14))
printf "%sOut of moves! Restarting...%s" "$RED" "$RESET"
sleep 1
exec "$0" "$@"
fi
read -rsn1 key
[[ "${#key}" == 1 ]] || continue # temporary fix for special characters (TODO)
case "${keys[$key]}" in
"left"|"down"|"up"|"right") move "${keys[$key]}";;
"redraw") redraw;;
"reset") exec "$0" "$@" ;;
"quit") exit ;;
*) continue ;;
esac
done