-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid-Based Follow Mouse Mode
174 lines (145 loc) · 7.16 KB
/
Grid-Based Follow Mouse Mode
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
//IMPORTANT: This is not a bookmarket. To use this you must copy-paste this into the console in the inspect element. Use ctrl+shift+I and a box should show up on your screen. At the top of this box, click console. There should be an area to type. Paste the following code there.
/*
Hack developed by YourMajestyTheEye
Repository: github.com/YourMajestyTheEye/Google-Snake
*/
(async () => {
function findGlobal(func, obj=window) {
for (let [key, value] of Object.entries(obj)) {
if (!value) continue;
try {
const res = func(value, key);
if (res) return res;
} catch (exc) {}
};
}
const renderObj = findGlobal(x => x.prototype.render && x.prototype.render.toString().includes('canvas.width') ? x : undefined);
if (!renderObj) throw new Error('Render prot not found, signature scan isnt working anymore');
const [,gameInstanceVarName] = findGlobal(x => x.prototype.render.toString().match(/this\.(\w+\.\w+)\.direction/)) || [];
if (!gameInstanceVarName) throw new Error('Game instance var name not found, signature scan isnt working anymore');
const [,bodyVarName] = findGlobal(x => x.prototype.reset.toString().match(/this\.(\w+)\.push\(new/)) || [];
if (!bodyVarName) throw new Error('Body var name not found, signature scan isnt working anymore');
const [,fruitVarName1, fruitVarName2] = findGlobal(x => x.prototype.reset.toString().match(/this.(\w+)\[0\]\.(\w+)\.x/)) || [];
if (!fruitVarName1 || !fruitVarName2) throw new Error('Fruit var names not found, signature scan isnt working anymore');
const [,mapSizeVarName] = findGlobal(x => x.prototype.reset.toString().match(/this\.(\w+\.\w+)\.width%2/)) || [];
if (!mapSizeVarName) throw new Error('Map size var name not found, signature scan isnt working anymore');
console.log('renderObj.name', renderObj.name);
console.log('gameInstanceVarName', gameInstanceVarName);
console.log('bodyVarName', bodyVarName);
console.log('fruitVarName1', fruitVarName1);
console.log('fruitVarName2', fruitVarName2);
console.log('mapSizeVarName', mapSizeVarName);
function getGameInstance() {
console.log(renderObj.prototype.render);
return new Promise(resolve => {
const original = renderObj.prototype.render;
renderObj.prototype.render = function (...args) {
console.log('Game Instance Hooked');
resolve(gameInstanceVarName.split('.').reduce((a,b) => a[b], this));
renderObj.prototype.render = original;
return original.call(this, ...args);
}
});
}
function changeDir(instance, newDir) {
instance.direction = newDir;
}
function getDir(instance) {
return instance.direction;
}
function getBody(instance) {
return instance[bodyVarName];
}
function getFruitPos(instance) {
//return instance[fruitVarName1][0][fruitVarName2];
return {x: window.x||0, y: window.y||0}
}
function getMapSize(instance) {
return mapSizeVarName.split('.').reduce((a,b) => a[b], instance);
}
function buildMap(instance) {
const body = getBody(instance);
const mapSize = getMapSize(instance);
const map = new Array(mapSize.width).fill().map(x => new Array(mapSize.height).fill(0));
body.forEach(({x, y}) => map[x][y] = 1);
return map;
}
function getHeatMap(map, pos) {
const width = map.length;
const height = map[0].length;
const heatMap = new Array(width).fill().map(x => new Array(height).fill(0));
const stack = [{x: pos.x, y: pos.y, c: 1}];
while(stack.length > 0) {
const {x, y, c} = stack.shift();
if (c > 1 && map[x][y] > 0) continue;
if (heatMap[x][y] != 0) continue;
heatMap[x][y] = c;
if (x > 0) stack.push({c: c+1, x: x-1, y});
if (x < width-1) stack.push({c: c+1, x: x+1, y});
if (y > 0) stack.push({c: c+1, x, y: y-1});
if (y < height-1) stack.push({c: c+1, x, y: y+1});
}
//console.log(map.map(x => x.map(x => String(x).padStart(2, '0')).join(' ')).join('\n'));
//console.log('-----');
//console.log(heatMap.map(x => x.map(x => String(x).padStart(2, '0')).join(' ')).join('\n'));
return heatMap;
}
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function run() {
/*console.log(getHeatMap(
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] ],
{x: 16, y: 14},
));*/
const instance = await getGameInstance();
/*console.log('instance', instance);
console.log('changeDir', changeDir(instance, 'RIGHT'));
console.log('getDir', getDir(instance));
console.log('getBody', getBody(instance));
console.log('getFruitPos', getFruitPos(instance));
console.log('getMapSize', getMapSize(instance));
return;*/
const canvas = document.querySelector('canvas');
canvas.addEventListener('mousemove', (e) => {
const {width, height} = getMapSize(instance);
const margin = 28;
window.x = Math.max(0, Math.min(width-1, Math.floor((e.offsetX-margin) / (canvas.width-margin*2) * width)));
window.y = Math.max(0, Math.min(height-1, Math.floor((e.offsetY-margin) / (canvas.height-margin*2) * height)));
});
let lastHash = 0;
const calcHash = (x, y) => x * 100000 + y;
let start = Date.now();
while(true) {
const body = getBody(instance);
const {x: headX, y: headY} = body[0];
const {x: tailX, y: tailY} = body[body.length-1];
const hash = calcHash(headX, headY);
if (lastHash != hash) {
lastHash = hash;
const {x: fruitX, y: fruitY} = getFruitPos(instance);
const map = buildMap(instance);
const tailHeatMap = getHeatMap(map, {x: tailX, y: tailY});
const fruitHeatMap = getHeatMap(map, {x: fruitX, y: fruitY});
//console.log(map.map(x => x.map(x => String(x).padStart(2, '0')).join(' ')).join('\n'));
//console.log('-----');
//console.log(heatMap.map(x => x.map(x => String(x).padStart(2, '0')).join(' ')).join('\n'));
const nextDir = () => {
const actions = [];
if (tailHeatMap[headX+1]?.[headY]) actions.push(['RIGHT', fruitHeatMap[headX+1]?.[headY] || 1000]);
if (tailHeatMap[headX-1]?.[headY]) actions.push(['LEFT', fruitHeatMap[headX-1]?.[headY] || 1000]);
if (tailHeatMap[headX]?.[headY+1]) actions.push(['DOWN', fruitHeatMap[headX]?.[headY+1] || 1000]);
if (tailHeatMap[headX]?.[headY-1]) actions.push(['UP', fruitHeatMap[headX]?.[headY-1] || 1000]);
const action = actions.sort((a, b) => a[1]-b[1])[0][0];
console.log('actions', actions, '->', action);
return action;
};
const newDir = nextDir();
console.log({headX, headY, tailX, tailY, fruitX, fruitY, newDir});
changeDir(instance, newDir);
}
await sleep(50);
}
}
run();
})()