@kitten-science/kitten-scientists
Version:
Add-on for the wonderful incremental browser game: https://kittensgame.com/web/
97 lines • 3.99 kB
JavaScript
import { isNil } from "@oliversalzburg/js-utils/data/nil.js";
import { Engine } from "./Engine.js";
import { ScienceSettings } from "./settings/ScienceSettings.js";
import { cl } from "./tools/Log.js";
import { UpgradeManager } from "./UpgradeManager.js";
import { UserScriptLoader } from "./UserScriptLoader.js";
export class ScienceManager extends UpgradeManager {
settings;
_workshopManager;
constructor(host, workshopManager, settings = new ScienceSettings()) {
super(host);
this.settings = settings;
this._workshopManager = workshopManager;
}
async tick(_context) {
if (!this.settings.enabled) {
return;
}
// If techs (science items) are enabled...
if (this.settings.techs.enabled && this._host.game.libraryTab.visible) {
await this.autoUnlock();
}
if (this.settings.policies.enabled && this._host.game.libraryTab.visible) {
await this.autoPolicy();
}
// Observe astronomical events.
if (this.settings.observe.enabled) {
this.observeStars();
}
}
async autoUnlock() {
const techs = this._host.game.science.techs;
const toUnlock = new Array();
workLoop: for (const setting of Object.values(this.settings.techs.techs)) {
if (!setting.enabled) {
continue;
}
const tech = techs.find(subject => subject.name === setting.tech);
if (isNil(tech)) {
console.error(...cl(`Tech '${setting.tech}' not found in game!`));
continue;
}
if (tech.researched || !tech.unlocked) {
continue;
}
let prices = UserScriptLoader.window.dojo.clone(tech.prices);
prices = this._host.game.village.getEffectLeader("scientist", prices);
for (const price of prices) {
const available = this._workshopManager.getValueAvailable(price.name);
const resource = this._workshopManager.getResource(price.name);
const trigger = Engine.evaluateSubSectionTrigger(this.settings.techs.trigger, setting.trigger);
if (trigger < 0 || available < resource.maxValue * trigger || available < price.val) {
continue workLoop;
}
}
toUnlock.push(tech);
}
for (const item of toUnlock) {
await this.upgrade(item, "science");
}
}
async autoPolicy() {
const policies = this._host.game.science.policies;
const toUnlock = new Array();
for (const setting of Object.values(this.settings.policies.policies)) {
if (!setting.enabled) {
continue;
}
const targetPolicy = policies.find(subject => subject.name === setting.policy);
if (isNil(targetPolicy)) {
console.error(...cl(`Policy '${setting.policy}' not found in game!`));
continue;
}
if (!targetPolicy.researched && !targetPolicy.blocked && targetPolicy.unlocked) {
if (targetPolicy.requiredLeaderJob === undefined ||
(this._host.game.village.leader !== null &&
this._host.game.village.leader.job === targetPolicy.requiredLeaderJob)) {
toUnlock.push(targetPolicy);
}
}
}
for (const item of toUnlock) {
await this.upgrade(item, "policy");
}
}
/**
* If there is currently an astronomical event, observe it.
*/
observeStars() {
if (this._host.game.calendar.observeBtn !== null) {
this._host.game.calendar.observeHandler();
this._host.engine.iactivity("act.observe", [], "ks-star");
this._host.engine.storeForSummary("stars", 1);
}
}
}
//# sourceMappingURL=ScienceManager.js.map