2022-04-14 19:19:51 +02:00
|
|
|
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-23 18:35:15 +01:00
|
|
|
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-04-22 04:48:18 +02:00
|
|
|
import { getGraftingAvailableAugs, calculateGraftingTimeWithBonus } from "../PersonObjects/Grafting/GraftingHelpers";
|
2022-03-23 18:35:15 +01:00
|
|
|
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",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-31 15:41:19 +02:00
|
|
|
const updateRam = (funcName: string): void =>
|
|
|
|
helper.updateDynamicRam(funcName, getRamCost(player, "grafting", funcName));
|
|
|
|
|
2022-03-23 18:35:15 +01:00
|
|
|
return {
|
2022-03-29 20:09:17 +02:00
|
|
|
getAugmentationGraftPrice: (_augName: unknown): number => {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("getAugmentationGraftPrice");
|
2022-03-29 20:09:17 +02:00
|
|
|
const augName = helper.string("getAugmentationGraftPrice", "augName", _augName);
|
|
|
|
checkGraftingAPIAccess("getAugmentationGraftPrice");
|
2022-04-14 19:19:51 +02:00
|
|
|
if (!getGraftingAvailableAugs(player).includes(augName) || !StaticAugmentations.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-04-26 14:58:39 +02:00
|
|
|
const graftableAug = new GraftableAugmentation(StaticAugmentations[augName]);
|
2022-04-22 04:48:18 +02:00
|
|
|
return graftableAug.cost;
|
2022-03-23 18:35:15 +01:00
|
|
|
},
|
|
|
|
|
2022-03-29 20:09:17 +02:00
|
|
|
getAugmentationGraftTime: (_augName: string): number => {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("getAugmentationGraftTime");
|
2022-03-29 20:09:17 +02:00
|
|
|
const augName = helper.string("getAugmentationGraftTime", "augName", _augName);
|
|
|
|
checkGraftingAPIAccess("getAugmentationGraftTime");
|
2022-04-14 19:19:51 +02:00
|
|
|
if (!getGraftingAvailableAugs(player).includes(augName) || !StaticAugmentations.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-04-26 14:58:39 +02:00
|
|
|
const graftableAug = new GraftableAugmentation(StaticAugmentations[augName]);
|
2022-04-22 04:48:18 +02:00
|
|
|
return calculateGraftingTimeWithBonus(player, graftableAug);
|
2022-03-23 18:35:15 +01:00
|
|
|
},
|
|
|
|
|
2022-04-13 16:43:27 +02:00
|
|
|
getGraftableAugmentations: (): string[] => {
|
|
|
|
updateRam("getGraftableAugmentations");
|
|
|
|
checkGraftingAPIAccess("getGraftableAugmentations");
|
|
|
|
const graftableAugs = getGraftingAvailableAugs(player);
|
|
|
|
return graftableAugs;
|
|
|
|
},
|
|
|
|
|
2022-03-29 20:09:17 +02:00
|
|
|
graftAugmentation: (_augName: string, _focus: unknown = true): boolean => {
|
2022-03-31 15:41:19 +02:00
|
|
|
updateRam("graftAugmentation");
|
2022-03-29 20:09:17 +02:00
|
|
|
const augName = helper.string("graftAugmentation", "augName", _augName);
|
|
|
|
const focus = helper.boolean(_focus);
|
|
|
|
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-04-11 18:07:15 +02:00
|
|
|
"You must be in New Tokyo to begin grafting an Augmentation.",
|
2022-03-23 18:35:15 +01:00
|
|
|
);
|
|
|
|
}
|
2022-04-14 19:19:51 +02:00
|
|
|
if (!getGraftingAvailableAugs(player).includes(augName) || !StaticAugmentations.hasOwnProperty(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-04-14 19:19:51 +02:00
|
|
|
const craftableAug = new GraftableAugmentation(StaticAugmentations[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
|
|
|
}
|
|
|
|
|
2022-03-29 23:09:36 +02:00
|
|
|
if (!hasAugmentationPrereqs(craftableAug.augmentation)) {
|
|
|
|
workerScript.log("grafting.graftAugmentation", () => `You don't have the pre-requisites for ${augName}`);
|
|
|
|
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-04-11 18:07:15 +02:00
|
|
|
workerScript.log("grafting.graftAugmentation", () => `Began grafting Augmentation ${augName}.`);
|
2022-03-23 18:35:15 +01:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|