-
Notifications
You must be signed in to change notification settings - Fork 260
/
enhancedautomine.user.js
361 lines (338 loc) · 16.1 KB
/
enhancedautomine.user.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// ==UserScript==
// @name [Pokeclicker] Enhanced Auto Mine
// @namespace Pokeclicker Scripts
// @author Ephenia (Credit: falcon71, KarmaAlex, umbralOptimatum, Pastaficionado)
// @description Automatically mines the Underground with Bombs. Features adjustable settings as well.
// @copyright https://github.com/Ephenia
// @license GPL-3.0 License
// @version 2.2.4
// @homepageURL https://github.com/Ephenia/Pokeclicker-Scripts/
// @supportURL https://github.com/Ephenia/Pokeclicker-Scripts/issues
// @downloadURL https://raw.githubusercontent.com/Ephenia/Pokeclicker-Scripts/master/enhancedautomine.user.js
// @updateURL https://raw.githubusercontent.com/Ephenia/Pokeclicker-Scripts/master/enhancedautomine.user.js
// @match https://www.pokeclicker.com/
// @icon https://www.google.com/s2/favicons?domain=pokeclicker.com
// @grant unsafeWindow
// @run-at document-idle
// ==/UserScript==
var mineState;
var smallRestoreState;
var setThreshold;
var autoMineTimer;
var layersMined;
var sellTreasureState;
var treasureHunter;
var itemThreshold;
function initAutoMine() {
const minerHTML = document.createElement("div");
minerHTML.innerHTML = `<button id="auto-mine-start" class="col-12 col-md-2 btn btn-${mineState ? 'success' : 'danger'}">Auto Mine [${mineState ? 'ON' : 'OFF'}]</button>
<button id="small-restore-start" class="col-12 col-md-3 btn btn-${smallRestoreState ? 'success' : 'danger'}">Auto Small Restore [${smallRestoreState ? 'ON' : 'OFF'}]</button>
<div id="threshold-input" class="col-12 col-md-3 btn-secondary"><img title="Money" src="assets/images/currency/money.svg" height="25px">
<input title="Value at which to stop buying Small Restores." type="text" id="small-restore"></div>
<select id="treasure-hunter" class="col-12 col-md-2 btn">
<option value="-1">All Items</option>
<option value="0">Fossils</option>
<option value="1">Evolution Items</option>
<option value="2">Gem Plates</option>
<option value="3">Shards</option>
<option value="4">Mega Stones</option>
<option value="5">Diamond Value</option>
</select>
<div id="item-threshold-input" class="col-12 col-md-2 btn-secondary"><img id="treasure-image" src="assets/images/currency/money.svg" height="25px">
<input title="Skips layers with fewer target items than this value." type="text" id="item-threshold"></div>`
document.querySelectorAll('#mineBody + div')[0].prepend(minerHTML);
$("#auto-mine-start").unwrap();
document.getElementById('small-restore').value = setThreshold.toLocaleString('en-US');
document.getElementById('treasure-hunter').value = treasureHunter;
document.getElementById('item-threshold').value = itemThreshold.toLocaleString('en-US');
setTreasureImage();
const autoSeller = document.createElement("div");
autoSeller.innerHTML = `<div>
<button id="auto-sell-treasure" class="col-12 col-md-3 btn btn-${sellTreasureState ? 'success' : 'danger'}">Auto Sell Treasure [${sellTreasureState ? 'ON' : 'OFF'}]</button>
</div>`
document.getElementById('treasures').prepend(autoSeller);
document.getElementById('auto-mine-start').addEventListener('click', event => { startAutoMine(event); });
document.getElementById('small-restore-start').addEventListener('click', event => { autoRestore(event); });
document.getElementById('auto-sell-treasure').addEventListener('click', event => { autoSellTreasure(event); });
document.getElementById('treasure-hunter').addEventListener('input', event => { treasureHunt(event); });
document.querySelector('#small-restore').addEventListener('input', event => {
setThreshold = +event.target.value.replace(/[A-Za-z!@#$%^&*()]/g, '').replace(/[,]/g, "");
localStorage.setItem("autoBuyThreshold", setThreshold);
event.target.value = setThreshold.toLocaleString('en-US');
});
document.querySelector('#item-threshold').addEventListener('input', event => {
itemThreshold = +event.target.value.replace(/[A-Za-z!@#$%^&*()]/g, '').replace(/[,]/g, "");
localStorage.setItem("itemThreshold", itemThreshold);
event.target.value = itemThreshold.toLocaleString('en-US');
});
addGlobalStyle('#threshold-input { display:flex;flex-direction:row;flex-wrap:wrap;align-content:center;justify-content:space-evenly;align-items:center; }');
addGlobalStyle('#item-threshold-input { display:flex;flex-direction:row;flex-wrap:wrap;align-content:center;justify-content:space-evenly;align-items:center; }');
addGlobalStyle('#small-restore { width:150px; }');
addGlobalStyle('#item-threshold { width:75px; }');
if (mineState) {
// Wait a few seconds to not mine before underground is fully loaded
setTimeout(() => {
autoMineTimer = setInterval(function () {
doAutoMine();
}, 1000);
}, 5000);
}
}
function startAutoMine(event) {
const element = event.target;
mineState = !mineState
mineState ? element.classList.replace('btn-danger', 'btn-success') : element.classList.replace('btn-success', 'btn-danger');
element.textContent = `Auto Mine [${mineState ? 'ON' : 'OFF'}]`;
if (mineState) {
autoMineTimer = setInterval(function () {
doAutoMine();
}, 1000); // Happens every 1 second
} else {
clearInterval(autoMineTimer)
}
localStorage.setItem('autoMineState', mineState);
}
function doAutoMine() {
const treasureHunting = Math.sign(treasureHunter) >= 0 && itemThreshold > 0;
const treasureTypes = ['Fossils', 'Evolution Items', 'Gem Plates', 'Shards', 'Mega Stones', 'Diamond Value'];
const surveyResult = Mine.surveyResult();
let treasureAmount;
if (Mine.loadingNewLayer) {
// Do nothing while the new layer is loading
return;
}
if (treasureHunting && surveyResult) {
// Parse survey for the treasure type we want
try {
let re = new RegExp(String.raw`${treasureTypes[treasureHunter]}: (\d+)`);
treasureAmount = +re.exec(surveyResult)[1];
// Count fossil pieces as fossils
if (treasureHunter == 0) {
re = new RegExp(`Fossil Pieces: (\d+)`);
treasureAmount += +re.exec(surveyResult)[1];
}
} catch (err) {
treasureAmount = 0;
}
}
if (treasureHunting && !surveyResult) {
// Survey the layer
mineMain();
} else if (treasureHunting && treasureAmount < itemThreshold && Mine.skipsRemaining() > 0) {
// Too few of the desired treasure type, skip
resetLayer();
} else if (!treasureHunting && Mine.itemsBuried() < itemThreshold && Mine.skipsRemaining() > 0) {
// Too few items, skip
resetLayer();
} else {
// Either the layer meets requirements or we're out of skips
mineMain();
}
if (sellTreasureState && layersMined != App.game.statistics.undergroundLayersMined()) {
Underground.sellAllMineItems();
layersMined = JSON.stringify(App.game.statistics.undergroundLayersMined());
localStorage.setItem('undergroundLayersMined', layersMined);
}
function mineMain() {
if (smallRestoreState) {
if ((ItemList["SmallRestore"].price() == 30000) && (player.itemList["SmallRestore"]() == 0) && (App.game.wallet.currencies[GameConstants.Currency.money]() >= setThreshold + 30000)) {
ItemList["SmallRestore"].buy(1);
}
if (Math.floor(App.game.underground.energy) < Math.max(App.game.underground.getSurvey_Cost(), Underground.BOMB_ENERGY)) {
if (player.itemList["LargeRestore"]() > 0) {
ItemList["LargeRestore"].use();
} else if (player.itemList["MediumRestore"]() > 0) {
ItemList["MediumRestore"].use();
} else {
ItemList["SmallRestore"].use();
}
}
}
if (!surveyResult && treasureHunting && Mine.skipsRemaining() != 0) {
if (Math.floor(App.game.underground.energy) >= App.game.underground.getSurvey_Cost()) {
Mine.survey();
$('#mine-survey-result').tooltip("hide");
}
return true;
} else {
if (Math.floor(App.game.underground.energy) >= 1) {
// Get location of all reward tiles
let rewards = Mine.rewardGrid.flatMap((row, y) => {
return row.map((tile, x) => {
return (tile ? {item: tile.value, revealed: tile.revealed, 'x': x, 'y': y} : 0);
}).filter((tile) => tile != 0);
});
// Calculate number of distinct items visible
let rewardsSeen = new Set();
rewards.forEach((tile) => {
if (tile.revealed) {
rewardsSeen.add(tile.item);
}
});
if (Mine.itemsBuried() > rewardsSeen.size) {
// Use bombs while there are still items left to uncover
if (Math.floor(App.game.underground.energy) >= Underground.BOMB_ENERGY) {
Mine.bomb();
}
} else {
// All items have at least one tile revealed, let's excavate them
if (Mine.toolSelected() != 0) {
Mine.toolSelected(Mine.Tool.Chisel);
}
let tilesToMine = rewards.filter((tile) => rewardsSeen.has(tile.item) && !tile.revealed)
while (tilesToMine.length && Math.floor(App.game.underground.energy) >= Underground.CHISEL_ENERGY) {
let tile = tilesToMine.pop();
Mine.click(tile.y, tile.x);
}
}
}
}
}
function resetLayer() {
if (!Mine.loadingNewLayer) {
Mine.loadingNewLayer = true;
setTimeout(Mine.completed, 1500);
if (Mine.skipsRemaining() > 0) {
GameHelper.incrementObservable(Mine.skipsRemaining, -1);
}
}
}
}
function autoRestore(event) {
const element = event.target;
smallRestoreState = !smallRestoreState;
smallRestoreState ? element.classList.replace('btn-danger', 'btn-success') : element.classList.replace('btn-success', 'btn-danger');
element.textContent = `Auto Small Restore [${smallRestoreState ? 'ON' : 'OFF'}]`;
localStorage.setItem('autoSmallRestore', smallRestoreState);
}
function autoSellTreasure(event) {
const element = event.target;
sellTreasureState = !sellTreasureState;
sellTreasureState ? element.classList.replace('btn-danger', 'btn-success') : element.classList.replace('btn-success', 'btn-danger');
element.textContent = `Auto Sell Treasure [${sellTreasureState ? 'ON' : 'OFF'}]`;
localStorage.setItem('autoSellTreasure', sellTreasureState);
}
function treasureHunt(event) {
const element = event.target;
const value = +element.value;
treasureHunter = value;
localStorage.setItem('treasureHunter', value);
setTreasureImage();
}
function setTreasureImage() {
const imageSources = ['items/underground/Hard Stone.png', 'breeding/Helix Fossil.png', 'items/evolution/Fire_stone.png',
'items/underground/Flame Plate.png', 'items/underground/Red Shard.png', 'megaStone/142.png', 'currency/diamond.svg'];
const imageTitles = ['Item', 'Fossil', 'Evolution Stone', 'Plate', 'Shard', 'Mega Stone', 'Diamond'];
document.getElementById('treasure-image').src = `assets/images/${imageSources[1 + treasureHunter]}`;
document.getElementById('treasure-image').title = imageTitles[1 + treasureHunter];
}
if (!validParse(localStorage.getItem('autoMineState'))) {
localStorage.setItem("autoMineState", false);
}
if (!validParse(localStorage.getItem('autoSmallRestore'))) {
localStorage.setItem("autoSmallRestore", false);
}
if (!validParse(localStorage.getItem('autoBuyThreshold'))) {
localStorage.setItem("autoBuyThreshold", 0);
}
if (!validParse(localStorage.getItem('autoSellTreasure'))) {
localStorage.setItem("autoSellTreasure", false);
}
if (!validParse(localStorage.getItem('treasureHunter'))) {
localStorage.setItem("treasureHunter", -1);
}
if (!validParse(localStorage.getItem('itemThreshold'))) {
localStorage.setItem("itemThreshold", 0);
}
mineState = JSON.parse(localStorage.getItem('autoMineState'));
smallRestoreState = JSON.parse(localStorage.getItem('autoSmallRestore'));
setThreshold = JSON.parse(localStorage.getItem('autoBuyThreshold'));
sellTreasureState = JSON.parse(localStorage.getItem('autoSellTreasure'));
treasureHunter = JSON.parse(localStorage.getItem('treasureHunter'));
itemThreshold = JSON.parse(localStorage.getItem('itemThreshold'));
function validParse(key) {
try {
if (key === null) {
throw new Error;
}
JSON.parse(key);
return true;
} catch (e) {
return false;
}
}
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
function loadEpheniaScript(scriptName, initFunction, priorityFunction) {
function reportScriptError(scriptName, error) {
console.error(`Error while initializing '${scriptName}' userscript:\n${error}`);
Notifier.notify({
type: NotificationConstants.NotificationOption.warning,
title: scriptName,
message: `The '${scriptName}' userscript crashed while loading. Check for updates or disable the script, then restart the game.\n\nReport script issues to the script developer, not to the Pokéclicker team.`,
timeout: GameConstants.DAY,
});
}
const windowObject = !App.isUsingClient ? unsafeWindow : window;
// Inject handlers if they don't exist yet
if (windowObject.epheniaScriptInitializers === undefined) {
windowObject.epheniaScriptInitializers = {};
const oldInit = Preload.hideSplashScreen;
var hasInitialized = false;
// Initializes scripts once enough of the game has loaded
Preload.hideSplashScreen = function (...args) {
var result = oldInit.apply(this, args);
if (App.game && !hasInitialized) {
// Initialize all attached userscripts
Object.entries(windowObject.epheniaScriptInitializers).forEach(([scriptName, initFunction]) => {
try {
initFunction();
} catch (e) {
reportScriptError(scriptName, e);
}
});
hasInitialized = true;
}
return result;
}
}
// Prevent issues with duplicate script names
if (windowObject.epheniaScriptInitializers[scriptName] !== undefined) {
console.warn(`Duplicate '${scriptName}' userscripts found!`);
Notifier.notify({
type: NotificationConstants.NotificationOption.warning,
title: scriptName,
message: `Duplicate '${scriptName}' userscripts detected. This could cause unpredictable behavior and is not recommended.`,
timeout: GameConstants.DAY,
});
let number = 2;
while (windowObject.epheniaScriptInitializers[`${scriptName} ${number}`] !== undefined) {
number++;
}
scriptName = `${scriptName} ${number}`;
}
// Add initializer for this particular script
windowObject.epheniaScriptInitializers[scriptName] = initFunction;
// Run any functions that need to execute before the game starts
if (priorityFunction) {
$(document).ready(() => {
try {
priorityFunction();
} catch (e) {
reportScriptError(scriptName, e);
// Remove main initialization function
windowObject.epheniaScriptInitializers[scriptName] = () => null;
}
});
}
}
if (!App.isUsingClient || localStorage.getItem('enhancedautomine') === 'true') {
loadEpheniaScript('enhancedautomine', initAutoMine);
}