Commit | Line | Data |
---|---|---|
21c4e167 H |
1 | #!/bin/bash |
2 | #OpenGameBook engine implemented in BASH | |
3 | #<~~Harvie 2oo8 | |
4 | ||
5 | title="OpenGameBook - BASH engine"; | |
6 | game="hacker.txt"; | |
7 | room="2"; | |
8 | ||
9 | export XDIALOG_HIGH_DIALOG_COMPAT=true; | |
10 | dialog="dialog"; #(dialog|whiptail|Xdialog|gdialog) | |
11 | ||
12 | #Args | |
13 | if [ -z "$1" ]; then | |
14 | echo "Usage: gbook game.txt [room] [dialog_binary]"; | |
15 | exit; | |
16 | fi; | |
17 | if [ -a "$1" ]; then | |
18 | game="$1"; | |
19 | else echo "File not exist!"; | |
20 | fi; | |
21 | if [ -n "$2" ]; then room="$2"; fi; | |
22 | if [ -n "$3" ]; then dialog="$3"; fi; | |
23 | ||
24 | #Functions | |
25 | #nop() { echo -n; } | |
26 | ||
27 | dial_escape() { | |
28 | #sed "s/'/\\\\'/g" | sed 's/"/\\"/g' | sed 's/!/\!/g'; | |
29 | iconv -c | sed "s/'/\\\\'/g"; | |
30 | } | |
31 | ||
32 | fline() { | |
33 | head "$1" -n "$2" | tail -n 1 | dial_escape; | |
34 | } | |
35 | ||
36 | enter_room() { | |
37 | line="$1"; | |
38 | #echo -n "$menu"; | |
39 | echo -n "$line" | grep -o '\[[^]]*\]' | while read item; do | |
40 | mroom="$(echo "$item" | grep -o '[0-9][0-9]*' | head -n 1)"; | |
41 | echo "'$mroom'"; echo "'$item'"; | |
42 | done; | |
43 | echo '>'; echo "' '"; | |
44 | echo 'c'; echo 'Cheat!'; | |
45 | } | |
46 | ||
47 | xcompat() { | |
48 | grep -v 'Gtk-' | |
49 | } | |
50 | ||
51 | #Init | |
52 | tmpfile="/tmp/ogb-$$.tmp"; | |
53 | dtmpfile="/tmp/ogbd-$$.tmp"; | |
54 | head="$(fline "$game" 1)"; | |
55 | ||
56 | #Code | |
57 | "$dialog" --msgbox "$head" 0 0 --backtitle "$title: $game" --title "$game intro"; | |
58 | while true; do | |
59 | line="$(fline "$game" "$room")"; | |
60 | ||
61 | menuargs="--backtitle '$title: $game' --title '$game/$room' --menu '#$room: $line' 0 0 10" | |
62 | echo "$(echo "$menuargs" && enter_room "$line")" | xargs "$dialog" 2>"$tmpfile"; | |
63 | set="$?"; seterr=123; #123 in xargs == 1 in command | |
64 | if [ "$set" == "$seterr" ]; then | |
65 | "$dialog" --backtitle "$title: $game" --title "Exiting $game" --yesno "End? - Konec?" 0 0 | |
66 | if [ "$?" == 0 ]; then break; fi; | |
67 | fi; | |
68 | ||
69 | if [ "$(cat "$tmpfile" | xcompat)" == "c" ]; then | |
70 | "$dialog" --backtitle "$title: $game" --title "Cheating in $game" --inputbox "Room? - Mistnost?" 0 0 2>"$tmpfile"; | |
71 | set="$?"; | |
72 | fi; | |
73 | ||
74 | echo -n "$(cat "$tmpfile" | xcompat)" | grep "[^0-9]" > /dev/null | |
75 | if [[ "$?" != 0 && "$set" != "$seterr" && -n "$(cat "$tmpfile" | xcompat)" ]]; then room="$(cat "$tmpfile" | xcompat)"; fi; | |
76 | done; | |
77 | ||
78 | clear; | |
79 | rm -f "$tmpfile"; | |
80 | rm -f "$dtmpfile"; | |
81 | echo "$title"; | |
82 | echo Game "$game" aborted by user in room "$room"; | |
83 | exit; |