bitburner-src/src/Corporation/Warehouse.ts

85 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-09-05 01:09:30 +02:00
import { Material } from "./Material";
2022-09-20 12:47:54 +02:00
import { Corporation } from "./Corporation";
import { Division } from "./Division";
import { MaterialInfo } from "./MaterialInfo";
import { Generic_fromJSON, Generic_toJSON, IReviverValue, constructorsForReviver } from "../utils/JSONReviver";
import { CityName } from "../Enums";
import { materialNames } from "./data/Constants";
import { createFullRecordFromEntries, getRecordEntries } from "../Types/Record";
import { CorpUnlockName } from "./data/Enums";
import { Player } from "@player";
interface IConstructorParams {
division: Division;
loc: CityName;
size: number;
}
export class Warehouse {
2021-09-05 01:09:30 +02:00
// Warehouse's level, which affects its maximum size
level = 1;
// City that this Warehouse is in
city = CityName.Sector12;
2021-09-05 01:09:30 +02:00
// Map of Materials held by this Warehouse
materials = createFullRecordFromEntries(materialNames.map((matName) => [matName, new Material({ name: matName })]));
2021-09-05 01:09:30 +02:00
// Maximum amount warehouse can hold
size = 0;
2021-09-05 01:09:30 +02:00
// Amount of space currently used by warehouse
sizeUsed = 0;
// Whether Smart Supply is enabled for this Industry (the Industry that this Warehouse is for)
smartSupplyEnabled = false;
// Decide if smart supply should use the amount of materials imported into account when deciding on the amount to buy.
smartSupplyOptions = createFullRecordFromEntries(materialNames.map((matName) => [matName, "leftovers"]));
2021-09-05 01:09:30 +02:00
// Stores the amount of product to be produced. Used for Smart Supply unlock.
// The production tracked by smart supply is always based on the previous cycle,
// so it will always trail the "true" production by 1 cycle
smartSupplyStore = 0;
constructor(params: IConstructorParams | null = null) {
const corp = Player.corporation;
if (!corp || params === null) return;
this.city = params.loc;
this.size = params.size;
this.updateSize(corp, params.division);
2021-09-15 04:01:23 +02:00
// Default smart supply to being enabled if the upgrade is unlocked
if (corp.unlocks.has(CorpUnlockName.SmartSupply)) {
2021-09-15 04:01:23 +02:00
this.smartSupplyEnabled = true;
}
2021-09-05 01:09:30 +02:00
}
// Re-calculate how much space is being used by this Warehouse
updateMaterialSizeUsed(): void {
this.sizeUsed = 0;
for (const [matName, mat] of getRecordEntries(this.materials)) {
this.sizeUsed += mat.stored * MaterialInfo[matName].size;
}
2021-09-05 01:09:30 +02:00
if (this.sizeUsed > this.size) {
2021-09-09 05:47:34 +02:00
console.warn("Warehouse size used greater than capacity, something went wrong");
}
2021-09-05 01:09:30 +02:00
}
updateSize(corporation: Corporation, division: Division): void {
this.size = this.level * 100 * corporation.getStorageMultiplier() * division.getStorageMultiplier();
2021-09-05 01:09:30 +02:00
}
// Serialize the current object to a JSON save state.
2022-07-15 01:00:10 +02:00
toJSON(): IReviverValue {
2021-09-05 01:09:30 +02:00
return Generic_toJSON("Warehouse", this);
}
2022-10-09 07:25:31 +02:00
// Initializes a Warehouse object from a JSON save state.
2022-07-15 01:00:10 +02:00
static fromJSON(value: IReviverValue): Warehouse {
2021-09-05 01:09:30 +02:00
return Generic_fromJSON(Warehouse, value.data);
}
}
constructorsForReviver.Warehouse = Warehouse;