| 1 |
#!/bin/bash |
| 2 |
//usr/bin/env stty -echo; sudo staprun -T 30 `stap -p4 "$0"`; stty echo; exit |
| 3 |
|
| 4 |
// 2048.stp - written by NeoCat |
| 5 |
// Inspired by https://github.com/gabrielecirulli/2048/ |
| 6 |
// Licensed under MIT license |
| 7 |
|
| 8 |
// Join the numbers and get to the 2048 tile! |
| 9 |
// HOW TO PLAY: Use your arrow keys to move the tiles. When two tiles with the same number touch, they merge into one! |
| 10 |
|
| 11 |
global k, F, F_copy, clear, changed, score |
| 12 |
global animate, moving, moved, spawning |
| 13 |
|
| 14 |
function color(i) { |
| 15 |
if (i < 0) |
| 16 |
printf("\033[0m"); |
| 17 |
else |
| 18 |
printf("\033[48;5;%dm\033[38;5;%dm", |
| 19 |
i == 0 ? 96 : |
| 20 |
i == 1 ? 95 : |
| 21 |
i == 2 ? 252 : |
| 22 |
i == 4 ? 225 : |
| 23 |
i == 8 ? 218 : |
| 24 |
i == 16 ? 136 : |
| 25 |
i == 32 ? 202 : |
| 26 |
i == 64 ? 196 : |
| 27 |
i == 128 ? 214 : |
| 28 |
i == 256 ? 178 : |
| 29 |
i == 512 ? 178 : 136, |
| 30 |
i < 8 ? 0 : 255) |
| 31 |
} |
| 32 |
function cmove(x,y) { |
| 33 |
printf("\033[%d;%dH", y, x) |
| 34 |
} |
| 35 |
function disp_number(n, j) { |
| 36 |
if (!n || j != 1) |
| 37 |
printf(" ") |
| 38 |
else if (n < 100) |
| 39 |
printf(" %3d ", n) |
| 40 |
else |
| 41 |
printf(" %4d ", n) |
| 42 |
} |
| 43 |
function display() { |
| 44 |
cmove(0,0) |
| 45 |
frame = "\033[48;5;95m \033[0m" |
| 46 |
printf("%s\n", frame) |
| 47 |
for (y = 0; y < 4; y++) { |
| 48 |
for (j = 0; j < 3; j++) { |
| 49 |
for (x = 0; x < 4; x++) { |
| 50 |
i = x + y*4 |
| 51 |
color(1) |
| 52 |
printf(" ") |
| 53 |
color(F[i]) |
| 54 |
disp_number(F[i], j) |
| 55 |
} |
| 56 |
color(1) |
| 57 |
printf(" ") |
| 58 |
color(-1) |
| 59 |
printf("\n") |
| 60 |
} |
| 61 |
printf("%s\n", frame) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
function spawn() { |
| 66 |
cnt = 0 |
| 67 |
for (i = 0; i < 16; i++) |
| 68 |
if (!F[i]) |
| 69 |
cnt++; |
| 70 |
if (cnt == 0) return -1 |
| 71 |
r = get_cycles() % cnt |
| 72 |
cnt = 0 |
| 73 |
for (i = 0; i < 16; i++) { |
| 74 |
if (!F[i]) |
| 75 |
if (cnt++ == r) { |
| 76 |
F[i] = get_cycles()%20 ? 2 : 4 |
| 77 |
return i |
| 78 |
} |
| 79 |
} |
| 80 |
return -1 |
| 81 |
} |
| 82 |
|
| 83 |
function move(from, to) { |
| 84 |
if (!F[from]) |
| 85 |
return 0 |
| 86 |
if (!F[to]) { |
| 87 |
moving[from] = F[from] |
| 88 |
moved = 1 |
| 89 |
F[to] = F[from] |
| 90 |
F[from] = 0 |
| 91 |
return 1 |
| 92 |
} |
| 93 |
if (F[from] == F[to] && !changed[from] && !changed[to]) { |
| 94 |
moving[from] = F[from] |
| 95 |
moved = 1 |
| 96 |
F[to] += F[from] |
| 97 |
F[from] = 0 |
| 98 |
changed[to] = 1 |
| 99 |
score += F[to] |
| 100 |
if (F[to] == 2048) clear = 1 |
| 101 |
return 1 |
| 102 |
} |
| 103 |
return 0 |
| 104 |
} |
| 105 |
function up() { |
| 106 |
updated = 0 |
| 107 |
for (i = 4; i < 16; i++) |
| 108 |
updated = move(i, i-4) || updated |
| 109 |
return updated |
| 110 |
} |
| 111 |
function down() { |
| 112 |
updated = 0 |
| 113 |
for (i = 11; i >= 0; i--) |
| 114 |
updated = move(i, i+4) || updated |
| 115 |
return updated |
| 116 |
} |
| 117 |
function left() { |
| 118 |
updated = 0 |
| 119 |
for (i = 0; i < 16; i++) |
| 120 |
if (i % 4) |
| 121 |
updated = move(i, i-1) || updated |
| 122 |
return updated |
| 123 |
} |
| 124 |
function right() { |
| 125 |
updated = 0 |
| 126 |
for (i = 15; i >= 0; i--) |
| 127 |
if (i % 4 != 3) |
| 128 |
updated = move(i, i+1) || updated |
| 129 |
return updated |
| 130 |
} |
| 131 |
|
| 132 |
probe begin { |
| 133 |
for (i = 0; i < 16; i++) |
| 134 |
F[i] = 0 |
| 135 |
spawn() |
| 136 |
|
| 137 |
printf("\033[2J") // clear |
| 138 |
display() |
| 139 |
} |
| 140 |
|
| 141 |
probe timer.ms(30) { |
| 142 |
if (!animate) next |
| 143 |
if (animate > 3) { |
| 144 |
display() |
| 145 |
animate = 0 |
| 146 |
next_move() |
| 147 |
next |
| 148 |
} |
| 149 |
if (k == 103) { |
| 150 |
dx = 0 |
| 151 |
dy = -animate |
| 152 |
} |
| 153 |
if (k == 105) { |
| 154 |
dx = -animate*2-2 |
| 155 |
dy = 0 |
| 156 |
} |
| 157 |
if (k == 106) { |
| 158 |
dx = animate*2-2 |
| 159 |
dy = 0 |
| 160 |
} |
| 161 |
if (k == 108) { |
| 162 |
dx = 0 |
| 163 |
dy = animate |
| 164 |
} |
| 165 |
for (y = 0; y < 4; y++) { |
| 166 |
for (x = 0; x < 4; x++) { |
| 167 |
i = x + y*4; |
| 168 |
if (!moving[i]) continue |
| 169 |
for (j = 0; j < 3; j++) { |
| 170 |
cmove(x*8+3+dx, y*4+2+j+dy) |
| 171 |
if (k == 105 || k == 106) { |
| 172 |
color(0) |
| 173 |
printf(" ") |
| 174 |
} |
| 175 |
color(moving[i]) |
| 176 |
disp_number(moving[i], j) |
| 177 |
if (k == 105 || k == 106) { |
| 178 |
color(0) |
| 179 |
printf(" ") |
| 180 |
} |
| 181 |
color(-1) |
| 182 |
} |
| 183 |
if (dy) { |
| 184 |
cmove(x*8+3+dx, y*4+dy+(dy<0?5:1)) |
| 185 |
color(0) |
| 186 |
printf(" ") |
| 187 |
color(-1) |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
animate++ |
| 192 |
} |
| 193 |
|
| 194 |
function next_move() { |
| 195 |
delete moving |
| 196 |
if (spawning) { |
| 197 |
spawning = 0 |
| 198 |
display() |
| 199 |
for (i = 0; i < 16; i++) |
| 200 |
F_copy[i] = F[i]; |
| 201 |
if (!up() && !down() && !left() && !right()) |
| 202 |
gameover = 1 |
| 203 |
for (i = 0; i < 16; i++) |
| 204 |
F[i] = F_copy[i]; |
| 205 |
cmove(0,18) |
| 206 |
printf(" Score: %d\n", score) |
| 207 |
if (clear) { |
| 208 |
printf(" !! Y O U W I N !! \n\n") |
| 209 |
printf(" Score: %d\n", score) |
| 210 |
} |
| 211 |
if (gameover) { |
| 212 |
printf(" !! G A M E O V E R !! \n\n") |
| 213 |
printf(" Score: %d\n", score) |
| 214 |
exit() |
| 215 |
} |
| 216 |
return 0 |
| 217 |
} |
| 218 |
if (k == 103) |
| 219 |
animate = up() |
| 220 |
else if (k == 105) |
| 221 |
animate = left() |
| 222 |
else if (k == 106) |
| 223 |
animate = right() |
| 224 |
else if (k == 108) |
| 225 |
animate = down() |
| 226 |
if (!animate && moved && !spawning) { |
| 227 |
animate = 9 |
| 228 |
spawning = spawn() |
| 229 |
cmove(spawning%4*8+4, spawning/4*4+3) |
| 230 |
color(F[spawning]) |
| 231 |
printf(" %d ", F[spawning]) |
| 232 |
} |
| 233 |
return animate |
| 234 |
} |
| 235 |
|
| 236 |
probe kernel.function("kbd_event") { |
| 237 |
if ($value != 1) next |
| 238 |
key = $event_code |
| 239 |
if (!key || key == 4) next |
| 240 |
//cmove(0,19) printf("key = %d ", key) |
| 241 |
onkeydown(key) |
| 242 |
} |
| 243 |
|
| 244 |
/* RANDOM MOVE: run with option `stap -G rand=1 2048.stp' or |
| 245 |
`staprun -T 30 /path/to/*.ko rand=1' */ |
| 246 |
global c, rand=0 |
| 247 |
probe timer.ms(500) { |
| 248 |
if (!rand) next |
| 249 |
dir = get_cycles()/256%4 |
| 250 |
c[0] = 103 c[1] = 105 c[2] = 106 c[3] = 108 |
| 251 |
key = c[dir] |
| 252 |
cmove(0,19) printf("key = %d ", key) |
| 253 |
onkeydown(key) |
| 254 |
} |
| 255 |
|
| 256 |
function onkeydown(key) { |
| 257 |
if (animate) { |
| 258 |
while (next_move()); |
| 259 |
animate = 0 |
| 260 |
display() |
| 261 |
} |
| 262 |
k = key |
| 263 |
delete changed |
| 264 |
moved = 0 |
| 265 |
next_move() |
| 266 |
} |