-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheva.sh
More file actions
executable file
·209 lines (188 loc) · 4.46 KB
/
eva.sh
File metadata and controls
executable file
·209 lines (188 loc) · 4.46 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
#!/bin/bash
if test ${BASH_VERSINFO[0]} -lt 4; then
OLDBASH=Y
fi
set -f
read H W <<<"$(stty size)"
SIDE=25
W=$(( W - SIDE ))
DISPLAPOS=$((W + 2 ))
MAPSIZE=$(( W * H ))
GAMEPID=$$
#GAME OPTIONS
FLIGHT="(@)"
ASTEROID="*"
LIFE=5
#INIT VALUES
POS=$(( (W - SIDE - ${#FLIGHT}) / 2 ))
LASTPOS=$POS
MAP=""
SCORE=0
LINES=0
LEVEL=0
#DESIGNS
LEVELCUTS=(50 100 150 200 300 500 900 1500 3000 5000 9000 20000 60000)
DELAYS=( 10 9 8 7 6 6 6 5 5 5 4 4 4)
DENSITIES=(60 58 56 54 52 50 48 46 44 42 40 38 36)
NEXTLINE=${LEVELCUTS[$LEVEL]}
DELAY=${DELAYS[$LEVEL]}
DENSITY=${DENSITIES[$LEVEL]}
EMPTYFLIGHT="$(printf %${#FLIGHT}s '')"
hidecursor() {
#HIDE
echo -ne "\x1b[?25l"
#RESET AUTO REPEAT
echo -ne "\x1b[?8l"
#ECHO OFF
echo -ne "\x1b[?12l"
#CLEAR SCREEN
echo -ne "\x1b[2J"
#MOVE LASTLINE
echo -ne "\x1b[${H};0H"
#SAVE POSITION AND CALL IT HOME
echo -ne "\x1b[s"
}
drawflight() {
local screenpos=$((POS + 1))
BASEIDX=$(( (H - 1) * W ))
LINE=${MAP:$BASEIDX:$W}
COLLIDE="${LINE:$POS:${#FLIGHT}}"
CHECKLINE=${CHECKLINE-0}
while test "${COLLIDE## }" != "${COLLIDE}"; do COLLIDE="${COLLIDE## }"; done
local H2=$((H - 2))
if test "$CHECKLINE" != "$LINES" -a -n "${COLLIDE}"; then
(( LIFE-- ))
if test $LIFE -eq 0; then
echo -ne "\x1b[${H};1H\x1bB"
echo "SCORE: $SCORE"
kill $GAMEPID 2>/dev/null
exit
fi
echo -ne "\x1b[1;${DISPLAPOS}HCOLLISION"
fi
if test "$CHECKLINE" -ne "$LINES" -o "$LASTPOS" -eq "$POS"; then
echo -ne "\x1b[${H};${screenpos}H${FLIGHT}"
else
if ((LASTPOS < POS)); then
screenpos=$((screenpos - 1))
echo -ne "\x1b[${H};${screenpos}H ${FLIGHT}"
elif ((LASTPOS > POS)); then
echo -ne "\x1b[${H};${screenpos}H${FLIGHT} "
fi
fi
echo -ne "\x1b[${H};${DISPLAPOS}H LIVES:$LIFE SCORE:$SCORE $POS"
CHECKLINE="$LINES"
}
mark() {
#DRAW ASTEROID and WALL
local loc="$1"
local art="$2"
local post=$(( loc + ${#art}))
local screenpos=$(( loc + 1 ))
echo -ne "\x1b[1;${screenpos}H${art}"
TOPLINE="${TOPLINE:0:$loc}${art}${TOPLINE:$post}"
}
scroll() {
exec <&-
trap _left SIGUSR1
trap _right SIGUSR2
if test -z "$BASHPID"; then
BASHPID=$(exec $BASH -c 'echo "$PPID"')
fi
BLANKLINE=$(printf %${W}s "")
while true
do
#SCROLL DOWN
echo -ne "\x1b[T"
echo -ne "\x1b[1;${W}H|"
(( LINES++ ))
if (( LINES == $NEXTLINE )); then
(( LEVEL++ ))
echo -ne "\x1b[1;${DISPLAPOS}HLEVEL: $LEVEL"
NEXTLINE=${LEVELCUTS[$LEVEL]:-$NEXTLINE}
DELAY=${DELAYS[$LEVEL]:-$DELAY}
DENSITY=${DENSITIES[$LEVEL]:-$DENSITY}
fi
if (( LINES > $H )); then
(( SCORE++ ))
fi
TOPLINE="$BLANKLINE"
REMAIN=W
while (( REMAIN > 0 )); do
if (( RANDOM % DENSITY < REMAIN )); then
ASTERPOS=$(( RANDOM % W ))
mark $ASTERPOS ${ASTEROID}
fi
(( REMAIN -= DENSITY ))
done
MAP="$TOPLINE$MAP"
MAP="${MAP:0:$MAPSIZE}"
drawflight
COUNT=$DELAY
while (( COUNT-- > 0 )); do
kill -s SIGSTOP $BASHPID || exit
done
done
}
_exit() {
#SHOW CURSOR
echo -ne "\x1b[?25h"
#SET AUTO REPEAT
echo -ne "\x1b[?8h"
#ECHO ON
echo -ne "\x1b[?12h"
kill $DRIVEPID $CLOCKPID 2>/dev/null || exit
echo ""
echo "BYE BYE!"
exit
}
_left() {
LASTPOS=$POS
POS=$(( POS > 0 ? POS - 1 : POS ))
drawflight
}
_right() {
LASTPOS=$POS
POS=$(( POS < W - ${#FLIGHT} - 1 ? POS + 1 : POS ))
drawflight
}
_clock() {
while true; do
sleep $CLOCKPULSE
kill -s SIGCONT $DRIVEPID
done
}
trap _exit SIGINT SIGTERM
hidecursor
scroll &
DRIVEPID=$!
CLOCKPULSE=.005
if test -n "$OLDBASH"; then
_clock &
CLOCKPID=$!
fi
while true
do
key=""
if test -n "$OLDBASH"; then
read -s -n1 key
else
read -s -n1 -t $CLOCKPULSE key
fi
case "$key" in
j|h)
kill -SIGUSR1 $DRIVEPID || exit
continue
;;
k|l)
kill -SIGUSR2 $DRIVEPID || exit
continue
;;
q)
_exit
;;
esac
if test -z "$OLDBASH"; then
kill -s SIGCONT $DRIVEPID || exit
fi
done