bitburner-src/src/NetscriptFunctions/Grafting.ts

89 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-03-23 18:35:15 +01:00
import { CityName } from "../Locations/data/CityNames";
import { Augmentations } from "../Augmentation/Augmentations";
import { getRamCost } from "../Netscript/RamCostGenerator";
import { WorkerScript } from "../Netscript/WorkerScript";
2022-03-29 20:09:17 +02:00
import { GraftableAugmentation } from "../PersonObjects/Grafting/GraftableAugmentation";
2022-03-23 18:35:15 +01:00
import { getAvailableAugs } from "../PersonObjects/Grafting/ui/GraftingRoot";
import { IPlayer } from "../PersonObjects/IPlayer";
import { Grafting as IGrafting } from "../ScriptEditor/NetscriptDefinitions";
import { Router } from "../ui/GameRoot";
import { INetscriptHelper } from "./INetscriptHelper";
export function NetscriptGrafting(player: IPlayer, workerScript: WorkerScript, helper: INetscriptHelper): IGrafting {
2022-03-29 20:09:17 +02:00
const checkGraftingAPIAccess = (func: string): void => {
2022-03-23 21:48:39 +01:00
if (!player.canAccessGrafting()) {
2022-03-23 18:35:15 +01:00
throw helper.makeRuntimeErrorMsg(
`grafting.${func}`,
"You do not currently have access to the Grafting API. This is either because you are not in BitNode 10 or because you do not have Source-File 10",
);
}
};
return {
2022-03-29 20:09:17 +02:00
getAugmentationGraftPrice: (_augName: unknown): number => {
const augName = helper.string("getAugmentationGraftPrice", "augName", _augName);
helper.updateDynamicRam("getAugmentationGraftPrice", getRamCost(player, "grafting", "getAugmentationGraftPrice"));
checkGraftingAPIAccess("getAugmentationGraftPrice");
2022-03-23 18:35:15 +01:00
if (!Augmentations.hasOwnProperty(augName)) {
2022-03-29 20:09:17 +02:00
throw helper.makeRuntimeErrorMsg("grafting.getAugmentationGraftPrice", `Invalid aug: ${augName}`);
2022-03-23 18:35:15 +01:00
}
2022-03-29 20:09:17 +02:00
const craftableAug = new GraftableAugmentation(Augmentations[augName]);
2022-03-23 18:35:15 +01:00
return craftableAug.cost;
},
2022-03-29 20:09:17 +02:00
getAugmentationGraftTime: (_augName: string): number => {
const augName = helper.string("getAugmentationGraftTime", "augName", _augName);
helper.updateDynamicRam("getAugmentationGraftTime", getRamCost(player, "grafting", "getAugmentationGraftTime"));
checkGraftingAPIAccess("getAugmentationGraftTime");
2022-03-23 18:35:15 +01:00
if (!Augmentations.hasOwnProperty(augName)) {
2022-03-29 20:09:17 +02:00
throw helper.makeRuntimeErrorMsg("grafting.getAugmentationGraftTime", `Invalid aug: ${augName}`);
2022-03-23 18:35:15 +01:00
}
2022-03-29 20:09:17 +02:00
const craftableAug = new GraftableAugmentation(Augmentations[augName]);
2022-03-23 18:35:15 +01:00
return craftableAug.time;
},
2022-03-29 20:09:17 +02:00
graftAugmentation: (_augName: string, _focus: unknown = true): boolean => {
const augName = helper.string("graftAugmentation", "augName", _augName);
const focus = helper.boolean(_focus);
helper.updateDynamicRam("graftAugmentation", getRamCost(player, "grafting", "graftAugmentation"));
checkGraftingAPIAccess("graftAugmentation");
2022-03-23 18:35:15 +01:00
if (player.city !== CityName.NewTokyo) {
throw helper.makeRuntimeErrorMsg(
2022-03-29 20:09:17 +02:00
"grafting.graftAugmentation",
2022-03-23 18:35:15 +01:00
"You must be in New Tokyo to begin crafting an Augmentation.",
);
}
if (!getAvailableAugs(player).includes(augName)) {
2022-03-29 20:09:17 +02:00
workerScript.log("grafting.graftAugmentation", () => `Invalid aug: ${augName}`);
2022-03-23 18:35:15 +01:00
return false;
}
const wasFocusing = player.focus;
if (player.isWorking) {
const txt = player.singularityStopWork();
2022-03-29 20:09:17 +02:00
workerScript.log("graftAugmentation", () => txt);
2022-03-23 18:35:15 +01:00
}
2022-03-29 20:09:17 +02:00
const craftableAug = new GraftableAugmentation(Augmentations[augName]);
2022-03-23 18:35:15 +01:00
if (player.money < craftableAug.cost) {
2022-03-29 20:09:17 +02:00
workerScript.log("grafting.graftAugmentation", () => `You don't have enough money to craft ${augName}`);
2022-03-27 21:33:03 +02:00
return false;
2022-03-23 18:35:15 +01:00
}
player.loseMoney(craftableAug.cost, "augmentations");
2022-03-29 20:09:17 +02:00
player.startGraftAugmentationWork(augName, craftableAug.time);
2022-03-23 18:35:15 +01:00
if (focus) {
player.startFocusing();
Router.toWork();
} else if (wasFocusing) {
player.stopFocusing();
Router.toTerminal();
}
2022-03-29 20:09:17 +02:00
workerScript.log("grafting.graftAugmentation", () => `Began crafting Augmentation ${augName}.`);
2022-03-23 18:35:15 +01:00
return true;
},
};
}