bitburner-src/src/NetscriptFunctions/Grafting.ts

102 lines
3.8 KiB
TypeScript
Raw Normal View History

import type { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { Player } from "@player";
import { Grafting as IGrafting } from "@nsdefs";
import { AugmentationName, CityName } from "@enums";
import { Augmentations } from "../Augmentation/Augmentations";
2022-03-29 23:09:36 +02:00
import { hasAugmentationPrereqs } from "../Faction/FactionHelpers";
2022-03-29 20:09:17 +02:00
import { GraftableAugmentation } from "../PersonObjects/Grafting/GraftableAugmentation";
import { getGraftingAvailableAugs, calculateGraftingTimeWithBonus } from "../PersonObjects/Grafting/GraftingHelpers";
2022-03-23 18:35:15 +01:00
import { Router } from "../ui/GameRoot";
2022-12-04 09:14:06 +01:00
import { Page } from "../ui/Router";
2022-07-10 07:37:36 +02:00
import { GraftingWork } from "../Work/GraftingWork";
2022-08-08 19:43:41 +02:00
import { helpers } from "../Netscript/NetscriptHelpers";
import { getEnumHelper } from "../utils/EnumHelper";
2022-03-23 18:35:15 +01:00
2022-08-09 21:41:47 +02:00
export function NetscriptGrafting(): InternalAPI<IGrafting> {
2022-05-08 01:08:07 +02:00
const checkGraftingAPIAccess = (ctx: NetscriptContext): void => {
if (!Player.canAccessGrafting()) {
2022-08-08 19:43:41 +02:00
throw helpers.makeRuntimeErrorMsg(
ctx,
2022-03-23 18:35:15 +01:00
"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",
);
}
};
const isValidGraftingAugName = (augName: AugmentationName) => getGraftingAvailableAugs().includes(augName);
2022-03-23 18:35:15 +01:00
return {
getAugmentationGraftPrice: (ctx) => (_augName) => {
const augName = getEnumHelper("AugmentationName").nsGetMember(ctx, _augName);
checkGraftingAPIAccess(ctx);
if (!isValidGraftingAugName(augName)) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid aug: ${augName}`);
}
const graftableAug = new GraftableAugmentation(Augmentations[augName]);
return graftableAug.cost;
},
2022-03-23 18:35:15 +01:00
getAugmentationGraftTime: (ctx) => (_augName) => {
const augName = getEnumHelper("AugmentationName").nsGetMember(ctx, _augName);
checkGraftingAPIAccess(ctx);
if (!isValidGraftingAugName(augName)) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid aug: ${augName}`);
}
const graftableAug = new GraftableAugmentation(Augmentations[augName]);
return calculateGraftingTimeWithBonus(graftableAug);
},
2022-03-23 18:35:15 +01:00
getGraftableAugmentations: (ctx) => () => {
2022-05-08 01:08:07 +02:00
checkGraftingAPIAccess(ctx);
2022-09-06 15:07:12 +02:00
const graftableAugs = getGraftingAvailableAugs();
return graftableAugs;
},
2022-05-08 01:08:07 +02:00
graftAugmentation:
(ctx) =>
(_augName, _focus = true) => {
const augName = getEnumHelper("AugmentationName").nsGetMember(ctx, _augName);
2022-08-08 19:43:41 +02:00
const focus = !!_focus;
2022-05-08 01:08:07 +02:00
checkGraftingAPIAccess(ctx);
if (Player.city !== CityName.NewTokyo) {
2022-08-08 19:43:41 +02:00
throw helpers.makeRuntimeErrorMsg(ctx, "You must be in New Tokyo to begin grafting an Augmentation.");
2022-05-08 01:08:07 +02:00
}
if (!isValidGraftingAugName(augName)) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `Invalid aug: ${augName}`);
2022-05-08 01:08:07 +02:00
return false;
}
2022-03-23 18:35:15 +01:00
const wasFocusing = Player.focus;
2022-03-23 18:35:15 +01:00
const craftableAug = new GraftableAugmentation(Augmentations[augName]);
if (Player.money < craftableAug.cost) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `You don't have enough money to craft ${augName}`);
2022-05-08 01:08:07 +02:00
return false;
}
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
if (!hasAugmentationPrereqs(craftableAug.augmentation)) {
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `You don't have the pre-requisites for ${augName}`);
2022-05-08 01:08:07 +02:00
return false;
}
2022-03-29 23:09:36 +02:00
Player.startWork(
2022-07-10 07:37:36 +02:00
new GraftingWork({
singularity: true,
augmentation: augName,
}),
);
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
if (focus) {
Player.startFocusing();
2022-12-04 09:14:06 +01:00
Router.toPage(Page.Work);
2022-05-08 01:08:07 +02:00
} else if (wasFocusing) {
Player.stopFocusing();
2022-12-04 09:14:06 +01:00
Router.toPage(Page.Terminal);
2022-05-08 01:08:07 +02:00
}
2022-03-23 18:35:15 +01:00
2022-08-08 21:51:50 +02:00
helpers.log(ctx, () => `Began grafting Augmentation ${augName}.`);
2022-05-08 01:08:07 +02:00
return true;
},
2022-03-23 18:35:15 +01:00
};
}