| 1 |
#!/usr/bin/env stap |
| 2 |
# jewel eater |
| 3 |
# Copyright (C) 2008 Masami Hiramatsu <[email protected]> |
| 4 |
# |
| 5 |
# This file is free software and is distributed under the terms of the GNU |
| 6 |
# General Public License (GPL); either version 2, or (at your option) any |
| 7 |
# later version. |
| 8 |
|
| 9 |
#TODO: |
| 10 |
# - power jewel |
| 11 |
# - increase snakes |
| 12 |
# - don't stop eater |
| 13 |
# - shortcut corner |
| 14 |
|
| 15 |
global pm=0, px=10, py=10, jewels=0 |
| 16 |
global pkm[4], mv, map, data, sx, sy, sm, _c |
| 17 |
global ph = 0 |
| 18 |
|
| 19 |
probe begin { |
| 20 |
pkm[0] = ">" |
| 21 |
pkm[1] = "<" |
| 22 |
pkm[2] = "V" |
| 23 |
pkm[3] = "A" |
| 24 |
data[1] = "#######################" |
| 25 |
data[2] = "##...................##" |
| 26 |
data[3] = "#..#.###.#####.###.#..#" |
| 27 |
data[4] = "#.##.o.#.......#.o.##.#" |
| 28 |
data[5] = "#.####.#.#####.#.####.#" |
| 29 |
data[6] = "#.....................#" |
| 30 |
data[7] = "###.####.## ##.####.###" |
| 31 |
data[8] = "##.......##U##.......##" |
| 32 |
data[9] = "#..#.###.#####.#.###..#" |
| 33 |
data[10] = "#.##.o.#.......#.o.##.#" |
| 34 |
data[11] = "#.####.#.#####.###.##.#" |
| 35 |
data[12] = "#.....................#" |
| 36 |
data[13] = "#######################" |
| 37 |
cursor_cls() |
| 38 |
|
| 39 |
for (y=1;y<14;y++) { |
| 40 |
x=0 |
| 41 |
c = substr(data[y], x, 1) |
| 42 |
cursor_move(x+1,y) |
| 43 |
while (c!="") { |
| 44 |
x++ |
| 45 |
map[x,y] = c |
| 46 |
print(c) |
| 47 |
c = substr(data[y], x, 1) |
| 48 |
if (c == ".") |
| 49 |
jewels++ |
| 50 |
if (c == "U") { |
| 51 |
sx[0]=x;sy[0]=y |
| 52 |
sx[1]=x;sy[1]=y |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
probe game.kbd { |
| 59 |
if (down) { |
| 60 |
mv = 1 |
| 61 |
if (code == GM_KBD_LEFT) |
| 62 |
pm = 0 |
| 63 |
else if (code == GM_KBD_RIGHT) |
| 64 |
pm = 1 |
| 65 |
else if (code == GM_KBD_UP) |
| 66 |
pm = 2 |
| 67 |
else if (code == GM_KBD_DOWN) |
| 68 |
pm = 3 |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
function crit(x, y, tx, ty) { |
| 73 |
dx = tx-x |
| 74 |
dy = ty-y |
| 75 |
if(map[x,y] != "#") |
| 76 |
return dx*dx+dy*dy |
| 77 |
return 1000000 |
| 78 |
} |
| 79 |
|
| 80 |
function dir_snake(x, y, tx, ty, m) { |
| 81 |
_c[0] = crit(x-1, y, tx, ty) |
| 82 |
_c[1] = crit(x+1, y, tx, ty) |
| 83 |
_c[2] = crit(x, y-1, tx, ty) |
| 84 |
_c[3] = crit(x, y+1, tx, ty) |
| 85 |
j=0 |
| 86 |
tc = _c[0] |
| 87 |
for (i=1;i<4;i++) { |
| 88 |
if (_c[i] <= _c[j]) j = i |
| 89 |
tc+=_c[i] |
| 90 |
} |
| 91 |
if (tc > 2000000 && _c[m]!=1000000) { |
| 92 |
return m |
| 93 |
} |
| 94 |
return j |
| 95 |
} |
| 96 |
|
| 97 |
function move_snake(i) { |
| 98 |
if (i==0) { |
| 99 |
tx = px; ty = py |
| 100 |
} else if (i==1) { |
| 101 |
tx = px; ty = py |
| 102 |
if (pm&2) |
| 103 |
ty += (pm&1)?3:-3 |
| 104 |
else |
| 105 |
tx += (pm&1)?3:-3 |
| 106 |
} |
| 107 |
sm[i] = dir_snake(sx[i], sy[i], tx, ty, sm[i]) |
| 108 |
if (sm[i]&2) |
| 109 |
dy = (sm[i]&1)?1:-1 |
| 110 |
else |
| 111 |
dx = (sm[i]&1)?1:-1 |
| 112 |
cursor_move(sx[i], sy[i]); print(map[sx[i], sy[i]]) |
| 113 |
sx[i]+=dx |
| 114 |
sy[i]+=dy |
| 115 |
cursor_move(sx[i], sy[i]); print("W") |
| 116 |
} |
| 117 |
|
| 118 |
probe timer.ms(200) { |
| 119 |
ph = 1 - ph |
| 120 |
for (i=0;i<2;i++) |
| 121 |
if (ph) { |
| 122 |
move_snake(i) |
| 123 |
} else { |
| 124 |
cursor_move(sx[i], sy[i]); print("M") |
| 125 |
if (sx[i] == px && sy[i] == py) |
| 126 |
exit() |
| 127 |
} |
| 128 |
cursor_move(1,25) |
| 129 |
} |
| 130 |
|
| 131 |
probe timer.ms(200) { |
| 132 |
if (mv == 1) { |
| 133 |
cursor_move(px,py); print(map[px,py]) |
| 134 |
if (pm&2) |
| 135 |
dy = (pm&1)?1:-1 |
| 136 |
else |
| 137 |
dx = (pm&1)?1:-1 |
| 138 |
if (map[px+dx, py+dy] != "#") { |
| 139 |
px += dx; py += dy |
| 140 |
} |
| 141 |
cursor_move(px,py); print(pkm[pm]) |
| 142 |
if (map[px, py] == ".") { |
| 143 |
jewels-- |
| 144 |
if (jewels == 0) |
| 145 |
exit() |
| 146 |
map[px, py] = " " |
| 147 |
} |
| 148 |
mv = 0 |
| 149 |
} else { |
| 150 |
cursor_move(px,py); print("O") |
| 151 |
} |
| 152 |
cursor_move(1,25) |
| 153 |
} |
| 154 |
|
| 155 |
probe end { |
| 156 |
if (jewels == 0) { |
| 157 |
cursor_move(1,10); print(" Game Clear ") |
| 158 |
} else { |
| 159 |
cursor_move(1,10); print(" Game Over! ") |
| 160 |
} |
| 161 |
cursor_move(1,25) |
| 162 |
} |