-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhatcheryHelperTweaks.user.js
More file actions
490 lines (440 loc) · 17.2 KB
/
hatcheryHelperTweaks.user.js
File metadata and controls
490 lines (440 loc) · 17.2 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// ==UserScript==
// @name [Pokeclicker] Hatchery Helper Tweaks
// @namespace Pokeclicker Scripts
// @author wizanyx
// @description Tweaks hatchery helpers: cost scaling, max helpers, and bonus controls.
// @copyright https://github.com/wizanyx
// @license GPL-3.0 License
// @version 0.0.1
// @homepageURL https://github.com/wizanyx/Pokeclicker-Scripts/
// @supportURL https://github.com/wizanyx/Pokeclicker-Scripts/issues
// @downloadURL https://raw.githubusercontent.com/wizanyx/Pokeclicker-Scripts/master/hatcheryHelperTweaks.user.js
// @updateURL https://raw.githubusercontent.com/wizanyx/Pokeclicker-Scripts/master/hatcheryHelperTweaks.user.js
// @match https://www.pokeclicker.com/
// @icon https://www.google.com/s2/favicons?domain=pokeclicker.com
// @grant unsafeWindow
// @run-at document-idle
// ==/UserScript==
const SETTINGS = {
STORAGE_KEY: "pokeclicker_hatcheryHelperTweaks_settings",
UI_CONTAINER_ID: "customScriptsContainer",
};
/**
* Manager for Hatchery Helper tweaks.
*/
const HatcheryHelperTweaks = {
costScalingEnabled: ko.observable(true),
maxHelpersEnabled: ko.observable(false),
bonusMode: ko.observable("bounded"),
bonusMax: ko.observable(50),
costScaleMode: ko.observable("fixed50"),
initialized: false,
_mapGeneratedBonus: -1,
_mapGeneratedHatched: 0,
load() {
try {
const json = localStorage.getItem(SETTINGS.STORAGE_KEY);
if (json) {
const data = JSON.parse(json);
if (data.costScalingEnabled !== undefined) {
this.costScalingEnabled(data.costScalingEnabled);
} else if (data.enabled !== undefined) {
this.costScalingEnabled(data.enabled);
}
if (data.maxHelpersEnabled !== undefined) {
this.maxHelpersEnabled(data.maxHelpersEnabled);
}
if (data.bonusMode !== undefined) {
this.bonusMode(data.bonusMode);
}
if (data.bonusMax !== undefined) {
this.bonusMax(data.bonusMax);
}
if (data.costScaleMode !== undefined) {
this.costScaleMode(data.costScaleMode);
}
}
} catch (e) {
console.error("[HatcheryHelperTweaks] Failed to load settings:", e);
}
},
save() {
if (!this.initialized) return;
localStorage.setItem(
SETTINGS.STORAGE_KEY,
JSON.stringify({
costScalingEnabled: this.costScalingEnabled(),
maxHelpersEnabled: this.maxHelpersEnabled(),
bonusMode: this.bonusMode(),
bonusMax: this.bonusMax(),
costScaleMode: this.costScaleMode(),
}),
);
},
isUnboundBonus() {
return this.bonusMode() === "unbound";
},
getBonusCap() {
const cap = Math.floor(Number(this.bonusMax()));
if (!Number.isFinite(cap)) return 50;
return Math.max(0, cap);
},
getRawHatchBonus(hatched) {
return Math.floor(Math.sqrt(hatched / 50) * 10) / 10;
},
calcHatchBonus(hatched) {
const raw = this.getRawHatchBonus(hatched);
if (this.isUnboundBonus()) return raw;
return Math.min(this.getBonusCap(), raw);
},
getCostScaleCap() {
if (this.costScaleMode() === "bonusMax") {
return Math.max(1, this.getBonusCap());
}
return 50;
},
getMaxHelpers() {
return this.maxHelpersEnabled() ? 4 : 3;
},
/**
* Returns the scaled cost based on hatch bonus.
* @param {number} baseAmount
* @param {number} hatchBonus
*/
getScaledCost(baseAmount, hatchBonus) {
const cap = this.getCostScaleCap();
const multiplier = Math.max(0, 1 - hatchBonus / cap);
return Math.max(0, Math.round(baseAmount * multiplier));
},
applyMaxHelpers() {
const helpers = App.game.breeding.hatcheryHelpers;
if (!helpers) return;
helpers.MAX_HIRES = this.getMaxHelpers();
if (!helpers._maxHelpersPatched) {
helpers._maxHelpersPatched = true;
helpers.canHire = ko.pureComputed(() => {
return (
helpers.hired().length <
Math.min(this.getMaxHelpers(), helpers.hatchery.eggSlots)
);
});
}
},
applyBonusSettings() {
this.resetBonusMap();
this.refreshHelpers();
},
resetBonusMap() {
if (!HatcheryHelperMinBonusMap) return;
Object.keys(HatcheryHelperMinBonusMap).forEach((key) => {
delete HatcheryHelperMinBonusMap[key];
});
this._mapGeneratedBonus = -1;
this._mapGeneratedHatched = 0;
if (!this.isUnboundBonus()) {
this.extendBonusMapTo(this.getBonusCap());
}
},
extendBonusMapTo(targetBonus) {
if (!HatcheryHelperMinBonusMap) return;
let bonus = this._mapGeneratedBonus;
let hatched = this._mapGeneratedHatched;
const target = Math.max(targetBonus, bonus);
while (bonus < target) {
const current = this.calcHatchBonus(hatched);
if (current > bonus) {
HatcheryHelperMinBonusMap[current] = hatched;
bonus = current;
}
hatched++;
if (hatched > 5_000_000) break;
}
this._mapGeneratedBonus = bonus;
this._mapGeneratedHatched = hatched;
},
ensureBonusMapFor(bonusTarget) {
if (this.isUnboundBonus()) {
this.extendBonusMapTo(bonusTarget);
}
},
refreshHelpers() {
if (!HatcheryHelpers?.list?.length) return;
HatcheryHelpers.list.forEach((helper) => helper.updateBonus());
},
/**
* Applies cost scaling to a single helper.
* @param {HatcheryHelper} helper
*/
applyToHelper(helper) {
if (!helper || !helper.cost) return;
if (helper._baseCostAmount === undefined) {
helper._baseCostAmount = helper.cost.amount;
}
if (this.costScalingEnabled()) {
helper.cost.amount = this.getScaledCost(
helper._baseCostAmount,
helper.hatchBonus(),
);
} else {
helper.cost.amount = helper._baseCostAmount;
}
},
/**
* Applies scaling to all helpers.
*/
applyToAll() {
if (!HatcheryHelpers?.list?.length) return;
HatcheryHelpers.list.forEach((helper) => this.applyToHelper(helper));
},
};
/**
* Patch HatcheryHelper bonus updates to keep cost in sync.
*/
const GameMechanics = {
applyPatches() {
if (!HatcheryHelper?.prototype?.updateBonus) return;
const originalUpdateBonus = HatcheryHelper.prototype.updateBonus;
HatcheryHelper.prototype.updateBonus = function () {
const hatchBonus = HatcheryHelperTweaks.calcHatchBonus(
this.hatched(),
);
this.hatchBonus(hatchBonus);
this.stepEfficiency(this.stepEfficiencyBase + hatchBonus);
this.attackEfficiency(this.attackEfficiencyBase + hatchBonus);
HatcheryHelperTweaks.ensureBonusMapFor(hatchBonus + 0.1);
this.prevBonus(HatcheryHelperMinBonusMap[hatchBonus] || 0);
this.nextBonus(
HatcheryHelperMinBonusMap[(hatchBonus * 10 + 1) / 10] || 1,
);
HatcheryHelperTweaks.applyToHelper(this);
if (originalUpdateBonus) {
// Keep any side effects from the original update
// without overwriting our custom values.
}
};
if (
HatcheryHelper?.prototype?.charge &&
!HatcheryHelper._chargePatched
) {
HatcheryHelper._chargePatched = true;
const originalCharge = HatcheryHelper.prototype.charge;
HatcheryHelper.prototype.charge = function () {
if (this.cost?.amount === 0) return;
return originalCharge.call(this);
};
}
},
};
/**
* Handles User Interface Injection and Customizations.
*/
const UserInterface = {
/**
* Injects the shared scripts container into the Left Column.
*/
createContainer() {
if (document.getElementById(SETTINGS.UI_CONTAINER_ID)) return;
const leftColumn = document.getElementById("left-column");
if (leftColumn) {
const div = document.createElement("div");
div.id = SETTINGS.UI_CONTAINER_ID;
div.className = "card sortable border-secondary mb-3";
div.innerHTML = `
<div class="card-header p-0" data-toggle="collapse" href="#customScriptsBody">
<span>Scripts</span>
</div>
<div id="customScriptsBody" class="card-body p-0 show"></div>
`;
leftColumn.appendChild(div);
}
},
/**
* Injects the script card into the shared container.
*/
injectScriptCard() {
if (document.getElementById("hatcheryHelperTweaksDisplay")) return;
const displayDiv = document.createElement("div");
displayDiv.id = "hatcheryHelperTweaksDisplay";
const html = `
<div class="card-header p-0 border-top" data-toggle="collapse" href="#hatcheryHelperTweaksInner">
<span>Hatchery Helper Tweaks</span>
</div>
<div id="hatcheryHelperTweaksInner" class="collapse show">
<div class="card-body p-0">
<div class="p-2">
<div class="mb-2 font-weight-bold">Cost Scaling</div>
<button class="btn btn-block btn-sm mb-2"
data-bind="
click: function() { HatcheryHelperTweaks.costScalingEnabled(!HatcheryHelperTweaks.costScalingEnabled()); },
class: HatcheryHelperTweaks.costScalingEnabled() ? 'btn-success' : 'btn-danger',
text: 'Cost Scaling [' + (HatcheryHelperTweaks.costScalingEnabled() ? 'ON' : 'OFF') + ']'
">
</button>
<div class="form-group mb-3">
<label class="mb-1">Cost Scaling Cap</label>
<select class="form-control form-control-sm"
data-bind="value: HatcheryHelperTweaks.costScaleMode">
<option value="fixed50">Fixed 50%</option>
<option value="bonusMax">Use Max Bonus</option>
</select>
</div>
<div class="mb-2 font-weight-bold">Hatch Bonus</div>
<div class="form-group mb-2">
<label class="mb-1">Max Hatch Bonus</label>
<select class="form-control form-control-sm"
data-bind="value: HatcheryHelperTweaks.bonusMode">
<option value="bounded">Bounded</option>
<option value="unbound">Unbound</option>
</select>
</div>
<!-- ko if: HatcheryHelperTweaks.bonusMode() === 'bounded' -->
<div class="form-group mb-3">
<label class="mb-1">Max Bonus (Integer)</label>
<input type="number" class="form-control form-control-sm" min="0" step="1"
data-bind="value: HatcheryHelperTweaks.bonusMax">
</div>
<!-- /ko -->
<div class="mb-2 font-weight-bold">Hiring</div>
<div class="custom-control custom-switch mb-0">
<input type="checkbox" class="custom-control-input" id="hhMaxHelpersToggle"
data-bind="checked: HatcheryHelperTweaks.maxHelpersEnabled">
<label class="custom-control-label" for="hhMaxHelpersToggle">
Allow 4 Helpers
</label>
</div>
</div>
</div>
</div>
`;
displayDiv.innerHTML = html;
const scriptBody = document.getElementById("customScriptsBody");
scriptBody.appendChild(displayDiv);
ko.applyBindings({ HatcheryHelperTweaks }, displayDiv);
},
};
/**
* Main Initialization Function
*/
function initializeHatcheryHelperTweaks() {
HatcheryHelperTweaks.load();
HatcheryHelperTweaks.costScalingEnabled.subscribe(() => {
HatcheryHelperTweaks.applyToAll();
HatcheryHelperTweaks.save();
});
HatcheryHelperTweaks.maxHelpersEnabled.subscribe(() => {
HatcheryHelperTweaks.applyMaxHelpers();
HatcheryHelperTweaks.save();
});
HatcheryHelperTweaks.bonusMode.subscribe(() => {
HatcheryHelperTweaks.applyBonusSettings();
HatcheryHelperTweaks.save();
});
HatcheryHelperTweaks.bonusMax.subscribe(() => {
HatcheryHelperTweaks.applyBonusSettings();
HatcheryHelperTweaks.save();
});
HatcheryHelperTweaks.costScaleMode.subscribe(() => {
HatcheryHelperTweaks.applyToAll();
HatcheryHelperTweaks.save();
});
HatcheryHelperTweaks.applyBonusSettings();
HatcheryHelperTweaks.applyToAll();
HatcheryHelperTweaks.applyMaxHelpers();
HatcheryHelperTweaks.initialized = true;
UserInterface.injectScriptCard();
}
/**
* Run patches before game start
*/
function runPriorityPatches() {
GameMechanics.applyPatches();
UserInterface.createContainer();
}
// Loader
function loadScript(scriptName, initFunction, priorityFunction) {
function reportScriptError(scriptName, error) {
const details =
error?.stack ||
error?.message ||
error?.toString?.() ||
String(error);
console.error(
`Error while initializing '${scriptName}' userscript:\n${error}`,
);
console.error(details);
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.\n\n${details}`,
timeout: GameConstants.DAY,
});
}
const windowObject = !App.isUsingClient ? unsafeWindow : window;
// Inject handlers if they don't exist yet
if (windowObject.ScriptInitializers === undefined) {
windowObject.ScriptInitializers = {};
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.ScriptInitializers).forEach(
([scriptName, initFunction]) => {
try {
initFunction();
console.log(`'${scriptName}' userscript loaded.`);
} catch (e) {
reportScriptError(scriptName, e);
}
},
);
hasInitialized = true;
}
return result;
};
}
// Prevent issues with duplicate script names
if (windowObject.ScriptInitializers[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.ScriptInitializers[`${scriptName} ${number}`] !==
undefined
) {
number++;
}
scriptName = `${scriptName} ${number}`;
}
// Add initializer for this particular script
windowObject.ScriptInitializers[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.ScriptInitializers[scriptName] = () => null;
}
});
}
}
if (!App.isUsingClient) {
unsafeWindow.HatcheryHelperTweaks = HatcheryHelperTweaks;
} else {
window.HatcheryHelperTweaks = HatcheryHelperTweaks;
}
loadScript(
"Hatchery Helper Tweaks",
initializeHatcheryHelperTweaks,
runPriorityPatches,
);