@kitten-science/kitten-scientists
Version:
Add-on for the wonderful incremental browser game: https://kittensgame.com/web/
107 lines • 4.4 kB
JavaScript
import { mustExist } from "@oliversalzburg/js-utils/data/nil.js";
import { BulkPurchaseHelper } from "./helper/BulkPurchaseHelper.js";
import { SpaceSettings } from "./settings/SpaceSettings.js";
import { cl } from "./tools/Log.js";
export class SpaceManager {
_host;
settings;
_bulkManager;
_workshopManager;
constructor(host, workshopManager, settings = new SpaceSettings()) {
this._host = host;
this.settings = settings;
this._workshopManager = workshopManager;
this._bulkManager = new BulkPurchaseHelper(this._host, this._workshopManager);
}
tick(context) {
if (!this.settings.enabled) {
return;
}
this._bulkManager.resetPriceCache();
this.autoBuild(context);
if (this.settings.unlockMissions.enabled) {
this.autoUnlock(context);
}
}
/**
* Try to build as many of the passed buildings as possible.
* Usually, this is called at each iteration of the automation engine to
* handle the building of items on the Space tab.
*
* @param builds The buildings to build.
*/
autoBuild(context, builds = this.settings.buildings) {
const sectionTrigger = this.settings.trigger;
// Get the current metadata for all the referenced buildings.
const metaData = {};
for (const build of Object.values(builds)) {
metaData[build.building] = this._host.game.space.getBuilding(build.building);
}
const builder = (build) => {
this.build(build.id, build.count);
};
context.purchaseOrders.push({ builder, builds, metaData, sectionTrigger });
}
autoUnlock(_context) {
if (!this._host.game.tabs[6].visible) {
return;
}
const missions = this._host.game.space.meta[0].meta;
missionLoop: for (let i = 0; i < missions.length; i++) {
// If the mission is already purchased or not available yet, continue with the next one.
if (0 < missions[i].val ||
!missions[i].unlocked ||
!this.settings.unlockMissions.missions[missions[i].name].enabled) {
continue;
}
const button = this._host.game.time.queue.getQueueElementControllerAndModel({
name: missions[i].name,
type: "spaceMission",
});
const prices = mustExist(button.model.prices);
for (const resource of prices) {
// If we can't afford this resource price, continue with the next mission.
if (this._workshopManager.getValueAvailable(resource.name) < resource.val) {
continue missionLoop;
}
}
// Start mission.
button.controller.buyItem(button.model);
if (i === 7 || i === 12) {
this._host.engine.iactivity("upgrade.space.mission", [missions[i].label], "ks-upgrade");
}
else {
this._host.engine.iactivity("upgrade.space", [missions[i].label], "ks-upgrade");
}
}
}
build(name, amount) {
let amountConstructed = 0;
let label;
const button = this._host.game.time.queue.getQueueElementControllerAndModel({
name,
type: "spaceBuilding",
});
const itemMetaRaw = game.getUnlockByName(name, "spaceBuilding");
label = itemMetaRaw.label;
const controller = button.controller;
const model = button.model;
amountConstructed = this._bulkManager.construct(model, controller, amount);
if (amount !== amountConstructed) {
console.warn(...cl(`${label} Amount ordered: ${amount} Amount Constructed: ${amountConstructed}`));
// Bail out to not flood the log with garbage.
if (amountConstructed === 0) {
return 0;
}
}
this._host.engine.storeForSummary(label, amountConstructed, "build");
if (amountConstructed === 1) {
this._host.engine.iactivity("act.build", [label], "ks-build");
}
else {
this._host.engine.iactivity("act.builds", [label, this._host.renderAbsolute(amountConstructed)], "ks-build");
}
return amountConstructed;
}
}
//# sourceMappingURL=SpaceManager.js.map