Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
refactor(core): Cleanup
Browse files Browse the repository at this point in the history
Remove remaining reliance on `Options` when not interacting with the the (currently only existing) legacy settings storage.
  • Loading branch information
oliversalzburg committed Sep 28, 2022
1 parent b757327 commit 092415b
Show file tree
Hide file tree
Showing 24 changed files with 213 additions and 391 deletions.
170 changes: 89 additions & 81 deletions packages/userscript/source/TimeControlManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ export class TimeControlManager {
this._spaceManager = new SpaceManager(this._host);
}

tick(context: TickContext) {
async tick(context: TickContext) {
if (!this.settings.enabled) {
return;
}

this.autoTimeControl();
return this.autoReset(this._host.engine);
if (this.settings.accelerateTime.enabled) {
this.accelerateTime();
}
if (this.settings.timeSkip.enabled) {
this.timeSkip();
}
if (this.settings.reset.enabled) {
await this.autoReset(this._host.engine);
}
}

load(settings: TimeControlSettings) {
Expand Down Expand Up @@ -309,94 +316,95 @@ export class TimeControlManager {
//=============================================================
}

autoTimeControl() {
const optionVals = this.settings;

// Tempus Fugit
// If it's enabled and we have enough Temporal Flux to reach the trigger,
// accelerate time.
if (optionVals.accelerateTime.enabled && !this._host.gamePage.time.isAccelerated) {
const temporalFlux = this._host.gamePage.resPool.get("temporalFlux");
if (temporalFlux.value >= temporalFlux.maxValue * optionVals.accelerateTime.trigger) {
this._host.gamePage.time.isAccelerated = true;
this._host.iactivity("act.accelerate", [], "ks-accelerate");
this._host.storeForSummary("accelerate", 1);
}
accelerateTime() {
if (this._host.gamePage.time.isAccelerated) {
return;
}
const temporalFlux = this._host.gamePage.resPool.get("temporalFlux");
if (temporalFlux.value >= temporalFlux.maxValue * this.settings.accelerateTime.trigger) {
this._host.gamePage.time.isAccelerated = true;
this._host.iactivity("act.accelerate", [], "ks-accelerate");
this._host.storeForSummary("accelerate", 1);
}
}

// Combust time crystal
// If time skipping is enabled and Chronoforge has been researched.
if (optionVals.timeSkip.enabled && this._host.gamePage.workshop.get("chronoforge").researched) {
// TODO: Not sure when this would ever be true.
if (this._host.gamePage.calendar.day < 0) {
return;
}
timeSkip() {
if (!this._host.gamePage.workshop.get("chronoforge").researched) {
return;
}

// If we have less time crystals than our required trigger value, bail out.
const timeCrystal = this._host.gamePage.resPool.get("timeCrystal");
if (timeCrystal.value < optionVals.timeSkip.trigger) {
return;
}
// TODO: Not sure when this would ever be true.
if (this._host.gamePage.calendar.day < 0) {
return;
}

// If skipping during this season was disabled, bail out.
const season = this._host.gamePage.calendar.season;
if (!optionVals.timeSkip[this._host.gamePage.calendar.seasons[season].name]) {
return;
}
// If we have less time crystals than our required trigger value, bail out.
const timeCrystal = this._host.gamePage.resPool.get("timeCrystal");
if (timeCrystal.value < this.settings.timeSkip.trigger) {
return;
}

// If skipping during this cycle was disabled, bail out.
const currentCycle = this._host.gamePage.calendar.cycle;
if (!optionVals.timeSkip[currentCycle]) {
return;
}
// If skipping during this season was disabled, bail out.
const season = this._host.gamePage.calendar.season;
if (!this.settings.timeSkip[this._host.gamePage.calendar.seasons[season].name]) {
return;
}

// If we have too much stored heat, wait for it to cool down.
const heatMax = this._host.gamePage.getEffect("heatMax");
const heatNow = this._host.gamePage.time.heat;
if (heatMax <= heatNow) {
return;
}
// If skipping during this cycle was disabled, bail out.
const currentCycle = this._host.gamePage.calendar.cycle;
if (!this.settings.timeSkip[currentCycle]) {
return;
}

const yearsPerCycle = this._host.gamePage.calendar.yearsPerCycle;
const remainingYearsCurrentCycle = yearsPerCycle - this._host.gamePage.calendar.cycleYear;
const cyclesPerEra = this._host.gamePage.calendar.cyclesPerEra;
const factor = this._host.gamePage.challenges.getChallenge("1000Years").researched ? 5 : 10;
// How many times/years we can skip before we reach our max heat.
let canSkip = Math.min(Math.floor((heatMax - heatNow) / factor), optionVals.timeSkip.maximum);
// The amount of skips to perform.
let willSkip = 0;
// If the cycle has more years remaining than we can even skip, skip all of them.
// I guess the idea here is to not skip through years of another cycle, if that
// cycle may not be enabled for skipping.
if (canSkip < remainingYearsCurrentCycle) {
willSkip = canSkip;
} else {
willSkip += remainingYearsCurrentCycle;
canSkip -= remainingYearsCurrentCycle;
let skipCycles = 1;
while (
yearsPerCycle < canSkip &&
optionVals.timeSkip[((currentCycle + skipCycles) % cyclesPerEra) as CycleIndices]
) {
willSkip += yearsPerCycle;
canSkip -= yearsPerCycle;
skipCycles += 1;
}
if (
optionVals.timeSkip[((currentCycle + skipCycles) % cyclesPerEra) as CycleIndices] &&
0 < canSkip
) {
willSkip += canSkip;
}
// If we have too much stored heat, wait for it to cool down.
const heatMax = this._host.gamePage.getEffect("heatMax");
const heatNow = this._host.gamePage.time.heat;
if (heatMax <= heatNow) {
return;
}

const yearsPerCycle = this._host.gamePage.calendar.yearsPerCycle;
const remainingYearsCurrentCycle = yearsPerCycle - this._host.gamePage.calendar.cycleYear;
const cyclesPerEra = this._host.gamePage.calendar.cyclesPerEra;
const factor = this._host.gamePage.challenges.getChallenge("1000Years").researched ? 5 : 10;
// How many times/years we can skip before we reach our max heat.
let canSkip = Math.min(
Math.floor((heatMax - heatNow) / factor),
this.settings.timeSkip.maximum
);
// The amount of skips to perform.
let willSkip = 0;
// If the cycle has more years remaining than we can even skip, skip all of them.
// I guess the idea here is to not skip through years of another cycle, if that
// cycle may not be enabled for skipping.
if (canSkip < remainingYearsCurrentCycle) {
willSkip = canSkip;
} else {
willSkip += remainingYearsCurrentCycle;
canSkip -= remainingYearsCurrentCycle;
let skipCycles = 1;
while (
yearsPerCycle < canSkip &&
this.settings.timeSkip[((currentCycle + skipCycles) % cyclesPerEra) as CycleIndices]
) {
willSkip += yearsPerCycle;
canSkip -= yearsPerCycle;
skipCycles += 1;
}
// If we found we can skip any years, do so now.
if (0 < willSkip) {
const shatter = this._host.gamePage.timeTab.cfPanel.children[0].children[0]; // check?
this._host.iactivity("act.time.skip", [willSkip], "ks-timeSkip");
shatter.controller.doShatterAmt(shatter.model, willSkip);
this._host.storeForSummary("time.skip", willSkip);
if (
this.settings.timeSkip[((currentCycle + skipCycles) % cyclesPerEra) as CycleIndices] &&
0 < canSkip
) {
willSkip += canSkip;
}
}
// If we found we can skip any years, do so now.
if (0 < willSkip) {
const shatter = this._host.gamePage.timeTab.cfPanel.children[0].children[0]; // check?
this._host.iactivity("act.time.skip", [willSkip], "ks-timeSkip");
shatter.controller.doShatterAmt(shatter.model, willSkip);
this._host.storeForSummary("time.skip", willSkip);
}
}

getBuild(
Expand Down
32 changes: 16 additions & 16 deletions packages/userscript/source/UserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ export class UserScript {

this.engine = new Engine(this);
this._userInterface = new UserInterface(this, this.engine);

this._userInterface.construct();
this.injectOptions(new Options());
this._userInterface.refreshUi();

this._activitySummary = new ActivitySummary(this);

Expand Down Expand Up @@ -129,16 +128,6 @@ export class UserScript {
this._userInterface.refreshUi();
}

/**
* Inject a different set of settings into the userscript.
*
* @param options The settings to use for the scientists.
*/
injectOptions(options: Options): void {
this.options = options;
this.refreshUi();
}

loadLegacyOptions(source: KittenStorageType) {
this.engine.load({
bonfire: BonfireSettings.fromLegacyOptions(source),
Expand All @@ -159,12 +148,11 @@ export class UserScript {
*
* @param updater A function that will manipulate the settings before they're saved.
*/
updateOptions(updater?: (currentOptions: Options) => void): void {
updateOptions(updater?: () => void): void {
cdebug("Settings will be updated.");
if (updater) {
updater(this.options);
updater();
}
this.options = this._userInterface?.getState();
this._optionsDirty = true;
}

Expand All @@ -176,7 +164,19 @@ export class UserScript {

saveSettings() {
this._optionsDirty = false;
const toExport = this.options.asLegacyOptions();

const toExport = Options.asLegacyOptions({
bonfire: this.engine.bonfireManager.settings,
engine: this.engine.settings,
religion: this.engine.religionManager.settings,
science: this.engine.scienceManager.settings,
space: this.engine.spaceManager.settings,
time: this.engine.timeManager.settings,
timeControl: this.engine.timeControlManager.settings,
trading: this.engine.tradingManager.settings,
village: this.engine.villageManager.settings,
workshop: this.engine.workshopManager.settings,
});
SettingsStorage.setLegacySettings(toExport);
clog("Kitten Scientists settings saved.");
}
Expand Down
3 changes: 1 addition & 2 deletions packages/userscript/source/options/BonfireSettings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { objectEntries } from "../tools/Entries";
import { Building } from "../types";
import { BuildingUpgradeSettings } from "./BuildingUpgradeSettings";
import { Requirement } from "./Options";
import { Setting, SettingMax } from "./Settings";
import { Requirement, Setting, SettingMax } from "./Settings";
import { SettingsSectionTrigger } from "./SettingsSection";
import { KittenStorageType } from "./SettingsStorage";

Expand Down
3 changes: 2 additions & 1 deletion packages/userscript/source/options/MissionSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export class MissionSettings extends SettingsSection {

static validateGame(game: GamePage, settings: MissionSettings) {
const inSettings = Object.keys(settings.items);
const inGame = game.space.missions.map(mission => mission.name);
// TODO: Find a better place in the game where this information is *always* available.
const inGame = (game.space.missions ?? []).map(mission => mission.name);

const missingInSettings = difference(inGame, inSettings);
const redundantInSettings = difference(inSettings, inGame);
Expand Down
Loading

0 comments on commit 092415b

Please sign in to comment.