-
Notifications
You must be signed in to change notification settings - Fork 260
/
simpleautofarmer.user.js
331 lines (299 loc) · 12.4 KB
/
simpleautofarmer.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
// ==UserScript==
// @name [Pokeclicker] Simple Auto Farmer
// @namespace Pokeclicker Scripts
// @author Ephenia / Akwawa / perkasthor / davide-lugli (update 1.6) / Optimatum
// @description Adds options to automatically plant, harvest, replant, and remulch. Unlike harvesting, replanting only harvests berries right before they wither, to maximize any auras. Make sure the correct berry is selected before auto planting; the script will save your selection across restarts. Auto replant and mulch maintain the berry/mulch already in each plot.
// @copyright https://github.com/Ephenia
// @license GPL-3.0 License
// @version 1.7.3
// @homepageURL https://github.com/Ephenia/Pokeclicker-Scripts/
// @supportURL https://github.com/Ephenia/Pokeclicker-Scripts/issues
// @downloadURL https://raw.githubusercontent.com/Ephenia/Pokeclicker-Scripts/master/simpleautofarmer.user.js
// @updateURL https://raw.githubusercontent.com/Ephenia/Pokeclicker-Scripts/master/simpleautofarmer.user.js
// @match https://www.pokeclicker.com/
// @icon https://www.google.com/s2/favicons?domain=pokeclicker.com
// @grant unsafeWindow
// @run-at document-idle
// ==/UserScript==//
function initAutoFarm() {
var autoFarmLoop;
var plantState;
var harvestState;
var replantState;
var mulchState;
plantState = JSON.parse(localStorage.getItem('autoPlantState'));
harvestState = JSON.parse(localStorage.getItem('autoHarvestState'));
replantState = JSON.parse(localStorage.getItem('autoReplantState'));
mulchState = JSON.parse(localStorage.getItem('autoMulchState'));
let plantSelected = JSON.parse(localStorage.getItem('autoPlantSelected'));
FarmController.selectedBerry(plantSelected);
FarmController.navigateIndex(Math.floor(plantSelected / FarmController.BERRIES_PER_PAGE));
createMenu();
if (plantState || replantState || harvestState || mulchState) {
toggleFarmLoop();
}
function createMenu() {
var elemAF = document.createElement("div");
var divMenu1 = document.createElement("div");
var divMenu2 = document.createElement("div");
const shovelList = document.getElementById('shovelList');
divMenu1.className = "row justify-content-center py-0";
divMenu2.className = "row justify-content-center py-0";
elemAF.appendChild(divMenu1);
elemAF.appendChild(divMenu2);
shovelList.before(elemAF);
const createButton = (name, state, func, rl, top) => {
var buttonDiv = document.createElement('div');
buttonDiv.className = `col-6 p${rl}-0`;
var button = document.createElement('button');
button.setAttribute('id', `auto-${name}-toggle`);
button.className = 'btn btn-block btn-' + (state ? 'success' : 'danger');
button.style.height = '50px';
button.style.fontSize = '9pt';
button.textContent = `Auto ${name[0].toUpperCase() + name.slice(1)}\n[${plantState ? 'ON' : 'OFF'}]`;
button.onclick = function() { func(); };
buttonDiv.appendChild(button);
if (top) {
divMenu1.appendChild(buttonDiv);
} else {
divMenu2.appendChild(buttonDiv);
}
}
createButton('plant', plantState, autoPlantToggle, 'r', true);
createButton('harvest', harvestState, autoHarvestToggle, 'l', true);
createButton('replant', replantState, autoReplantToggle, 'r', false);
createButton('mulch', mulchState, autoMulchToggle, 'l', false);
}
function toggleFarmLoop() {
if (plantState || replantState || harvestState || mulchState) {
if (!autoFarmLoop) {
autoFarmLoop = setInterval(autoFarmTick, 1000);
}
} else {
autoFarmLoop = clearInterval(autoFarmLoop);
}
}
function autoFarmTick() {
if (replantState) {
doReplant();
} else {
if (harvestState) {
doHarvest();
}
if (plantState) {
doPlant();
}
}
if (mulchState) {
doMulch();
}
}
function autoPlantToggle() {
plantState = !plantState;
localStorage.setItem("autoPlantState", plantState);
toggleFarmLoop();
if (replantState) {
autoReplantToggle();
}
let elt = document.getElementById('auto-plant-toggle');
if (plantState) {
elt.innerText = "Auto Plant\n[ON]";
elt.classList.remove('btn-danger');
elt.classList.add('btn-success');
} else {
elt.innerText = "Auto Plant\n[OFF]";
elt.classList.remove('btn-success');
elt.classList.add('btn-danger');
}
}
function doPlant() {
App.game.farming.plantAll(FarmController.selectedBerry());
}
function autoHarvestToggle() {
harvestState = !harvestState;
localStorage.setItem("autoHarvestState", harvestState);
toggleFarmLoop();
if (replantState) {
autoReplantToggle();
}
let elt = document.getElementById('auto-harvest-toggle');
if (harvestState) {
elt.innerText = "Auto Harvest\n[ON]";
elt.classList.remove('btn-danger');
elt.classList.add('btn-success');
} else {
elt.innerText = "Auto Harvest\n[OFF]";
elt.classList.remove('btn-success');
elt.classList.add('btn-danger');
}
}
function doHarvest() {
App.game.farming.harvestAll();
}
function autoReplantToggle() {
replantState = !replantState;
localStorage.setItem("autoReplantState", replantState);
toggleFarmLoop();
if (plantState) {
autoPlantToggle();
}
if (harvestState) {
autoHarvestToggle();
}
let elt = document.getElementById('auto-replant-toggle');
if (replantState) {
elt.innerText = "Auto Replant\n[ON]";
elt.classList.remove('btn-danger');
elt.classList.add('btn-success');
} else {
elt.innerText = "Auto Replant\n[OFF]";
elt.classList.remove('btn-success');
elt.classList.add('btn-danger');
}
}
function doReplant() {
const berryData = App.game.farming.berryData;
// Check each tile
for (let i = 0; i < 25; i++) {
let plot = App.game.farming.plotList[i];
let berry = plot.berry;
if (berry >= 0) {
// Auto-harvest tiles with surprise mulch
if (plot.mulch == MulchType.Surprise_Mulch) {
App.game.farming.harvest(i, false);
continue;
}
var timeLeft = berryData[berry].growthTime[4] - plot.age;
timeLeft /= (App.game.farming.getGrowthMultiplier() * plot.getGrowthMultiplier());
if (timeLeft < 10) {
App.game.farming.harvest(i, false);
App.game.farming.plant(i, berry, false);
}
}
}
}
function autoMulchToggle() {
mulchState = !mulchState;
localStorage.setItem("autoMulchState", mulchState);
toggleFarmLoop();
let elt = document.getElementById('auto-mulch-toggle');
if (mulchState) {
elt.innerText = "Auto Mulch\n[ON]";
elt.classList.remove('btn-danger');
elt.classList.add('btn-success');
} else {
elt.innerText = "Auto Mulch\n[OFF]";
elt.classList.remove('btn-success');
elt.classList.add('btn-danger');
}
}
// Add more of same kind of mulch
function doMulch() {
for (let i = 0; i < 25; i++) {
var plot = App.game.farming.plotList[i];
if (plot.mulch != MulchType.None && plot.mulchTimeLeft < 15) {
App.game.farming.addMulch(i, plot.mulch);
}
}
}
}
function initSelectedBerryTracking() {
let berryEntry = document.getElementById('seeds').querySelector('li[data-bind*="selectedBerry"]');
let bound = berryEntry.getAttribute('data-bind');
bound = bound.replace(`click: function() {`, `click: function() {localStorage.setItem('autoPlantSelected', $data);`);
berryEntry.setAttribute('data-bind', bound);
const navigateLeft = FarmController.navigateLeft.bind(FarmController);
const navigateRight = FarmController.navigateRight.bind(FarmController);
FarmController.navigateLeft = function() {
var result = navigateLeft();
localStorage.setItem('autoPlantSelected', FarmController.selectedBerry());
return result;
}
FarmController.navigateRight = function() {
var result = navigateRight();
localStorage.setItem('autoPlantSelected', FarmController.selectedBerry());
return result;
}
}
function initLocalStorage(param, value) {
let curVal = localStorage.getItem(param);
try {
curVal = JSON.parse(curVal);
} catch (e) {
curVal = null;
}
if (curVal == null || (typeof curVal !== typeof value)) {
localStorage.setItem(param, value);
}
}
initLocalStorage("autoPlantState", false);
initLocalStorage("autoHarvestState", false);
initLocalStorage("autoReplantState", false);
initLocalStorage("autoMulchState", false);
initLocalStorage("autoPlantSelected", 0);
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('simpleautofarmer') === 'true') {
loadEpheniaScript('simpleautofarmer', initAutoFarm, initSelectedBerryTracking);
}