bitburner-src/src/NetscriptFunctions/Grafting.ts

101 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-05-08 01:08:07 +02:00
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { StaticAugmentations } from "../Augmentation/StaticAugmentations";
2022-03-29 23:09:36 +02:00
import { hasAugmentationPrereqs } from "../Faction/FactionHelpers";
import { CityName } from "../Locations/data/CityNames";
2022-03-29 20:09:17 +02:00
import { GraftableAugmentation } from "../PersonObjects/Grafting/GraftableAugmentation";
import { getGraftingAvailableAugs, calculateGraftingTimeWithBonus } from "../PersonObjects/Grafting/GraftingHelpers";
2022-08-09 21:41:47 +02:00
import { Player as player } from "../Player";
2022-03-23 18:35:15 +01:00
import { Grafting as IGrafting } from "../ScriptEditor/NetscriptDefinitions";
import { Router } from "../ui/GameRoot";
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";
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 => {
2022-03-23 21:48:39 +01:00
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",
);
}
};
return {
2022-05-08 01:08:07 +02:00
getAugmentationGraftPrice:
(ctx: NetscriptContext) =>
(_augName: unknown): number => {
2022-08-08 19:43:41 +02:00
const augName = helpers.string(ctx, "augName", _augName);
2022-05-08 01:08:07 +02:00
checkGraftingAPIAccess(ctx);
2022-09-06 15:07:12 +02:00
if (!getGraftingAvailableAugs().includes(augName) || !StaticAugmentations.hasOwnProperty(augName)) {
2022-08-08 19:43:41 +02:00
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid aug: ${augName}`);
2022-05-08 01:08:07 +02:00
}
const graftableAug = new GraftableAugmentation(StaticAugmentations[augName]);
return graftableAug.cost;
},
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
getAugmentationGraftTime:
(ctx: NetscriptContext) =>
(_augName: string): number => {
2022-08-08 19:43:41 +02:00
const augName = helpers.string(ctx, "augName", _augName);
2022-05-08 01:08:07 +02:00
checkGraftingAPIAccess(ctx);
2022-09-06 15:07:12 +02:00
if (!getGraftingAvailableAugs().includes(augName) || !StaticAugmentations.hasOwnProperty(augName)) {
2022-08-08 19:43:41 +02:00
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid aug: ${augName}`);
2022-05-08 01:08:07 +02:00
}
const graftableAug = new GraftableAugmentation(StaticAugmentations[augName]);
2022-09-06 15:07:12 +02:00
return calculateGraftingTimeWithBonus(graftableAug);
2022-05-08 01:08:07 +02:00
},
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
getGraftableAugmentations: (ctx: NetscriptContext) => (): string[] => {
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: NetscriptContext) =>
(_augName: string, _focus: unknown = true): boolean => {
2022-08-08 19:43:41 +02:00
const augName = helpers.string(ctx, "augName", _augName);
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
}
2022-09-06 15:07:12 +02:00
if (!getGraftingAvailableAugs().includes(augName) || !StaticAugmentations.hasOwnProperty(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
2022-05-08 01:08:07 +02:00
const wasFocusing = player.focus;
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
const craftableAug = new GraftableAugmentation(StaticAugmentations[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
2022-07-14 23:43:08 +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();
Router.toWork();
} else if (wasFocusing) {
player.stopFocusing();
Router.toTerminal();
}
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
};
}