💾 Archived View for lyk.so › files › maze.sh captured on 2024-12-17 at 12:06:43.

View Raw

More Information

-=-=-=-=-=-=-

#!/usr/bin/env bash

set -e

debug() {
	[ ! -e ~/maze/debug ] || echo "$@" >> ~/maze/debug
}
export debug

# Returns the value of the variable named by the value of the variable given.
unwrap() {
	echo "$(eval "echo \$(eval echo $1)")"
}
export unwrap

left() {
	case "$1" in
		N) echo "W" ;;
		S) echo "E" ;;
		E) echo "N" ;;
		W) echo "S" ;;
	esac
}
export left

right() {
	case "$1" in
		N) echo "E" ;;
		S) echo "W" ;;
		E) echo "S" ;;
		W) echo "N" ;;
	esac
}
export right

tput civis

while true; do
	debug "Start of loop"

	x="$(cat ~/maze/x)"
	y="$(cat ~/maze/y)"
	facing="$(cat ~/maze/facing)"

	clear
	cat ~/maze/curr
	printf "($x,$y) :: facing $facing"

	# North, South, East, West
	N="$(cat ~/maze/curr | sed 's/█/./g; s/🐁/  /g; s/🐀/@@/g; s/`,/  /' | tail -n7 |  head -n1 | cut -c12)"
	S="$(cat ~/maze/curr | sed 's/█/./g; s/🐁/  /g; s/🐀/@@/g; s/`,/  /' | tail -n5 |  head -n1 | cut -c12)"
	E="$(cat ~/maze/curr | sed 's/█/./g; s/🐁/  /g; s/🐀/@@/g; s/`,/  /' | tail -n6 |  head -n1 | cut -c14)"
	W="$(cat ~/maze/curr | sed 's/█/./g; s/🐁/  /g; s/🐀/@@/g; s/`,/  /' | tail -n6 |  head -n1 | cut -c11)"

	#echo "N: $N" >> ~/maze/debug
	#echo "S: $S" >> ~/maze/debug
	#echo "E: $E" >> ~/maze/debug
	#echo "W: $W" >> ~/maze/debug
	#echo "Press key to step..." >> ~/maze/debug
	#read -n 1 -s foobar
	#exit

	# If the direction to the right is open, turn to face it.
	if [ -z "$(unwrap $(right $facing))" ]; then
		facing="$(right $facing)"
		#echo "Turning right to face $facing" >> ~/maze/debug

	# If the direction ahead is blocked, turn to the left until it's not.
	elif [ -n "$(unwrap $facing)" ]; then
		while [ -n "$(unwrap $facing)" ]; do
			facing="$(left $facing)"
			#echo "Blocked by '"$(unwrap $facing)"'." >> ~/maze/debug
			#echo "Turning left to face $facing" >> ~/maze/debug
			#read -n 1 -s foobar
		done
	fi
	#echo "Stepping forward, toward $facing" >> ~/maze/debug

	echo "$facing" > ~/maze/facing
	key="$facing" # Always step forward

	#continue

	#read -n 1 -s key

	case "$key" in
		w|N) dir='u' ;;
		a|W) dir='l' ;;
		s|S) dir='d' ;;
		d|E) dir='r' ;;
		q) exit  ;;
		*) continue ;;
	esac

	diohsc -e "" gemini://emii.gay/maze/app?$dir > ~/maze/page 2> /dev/null
 	cat ~/maze/page | head -n11 | cut -c2- > ~/maze/next

	[ -n "$(diff -q ~/maze/next ~/maze/curr)" ] || continue

	debug "Calculating move '$key' from $x,$y"
	# Expressions that evaluate to 0 result in a non-zero exit code.
	case "$key" in
		w|N) expr "$y" + 1 > ~/maze/y || true ;;
		a|W) expr "$x" - 1 > ~/maze/x || true ;;
		s|S) expr "$y" - 1 > ~/maze/y || true ;;
		d|E) expr "$x" + 1 > ~/maze/x || true ;;
	esac
	debug "Result: $x,$y"

	if [ ! -e ~/maze/"$x,$y-page" ]; then
		cp -- ~/maze/page ~/maze/"$x,$y-page"
		cp -- ~/maze/next ~/maze/"$x,$y-maze"
	fi
	mv ~/maze/next ~/maze/curr

	[ ! -e ~/maze/positions/"$x,$y,$facing" ] || {
		echo "Repeating! Leaving now."
		break
	}
	touch ~/maze/positions/"$x,$y,$facing"

	sleep 0.1
	[ ! -e ~/maze/quit ] || break}
	#echo "Taking next step..." >> ~/maze/debug
	debug "End of loop"
done
tput cnorm