bitburner-src/src/NetscriptFunctions/Stanek.ts

112 lines
5.9 KiB
TypeScript
Raw Normal View History

2021-10-05 04:31:07 +02:00
import { INetscriptHelper } from "./INetscriptHelper";
import { IPlayer } from "../PersonObjects/IPlayer";
import { WorkerScript } from "../Netscript/WorkerScript";
import { netscriptDelay } from "../NetscriptEvaluator";
import { getRamCost } from "../Netscript/RamCostGenerator";
import { staneksGift } from "../CotMG/Helper";
import { Fragments, FragmentById } from "../CotMG/Fragment";
2021-11-14 05:45:26 +01:00
import {
Stanek as IStanek,
Fragment as IFragment,
ActiveFragment as IActiveFragment,
} from "../ScriptEditor/NetscriptDefinitions";
import { AugmentationNames } from "../Augmentation/data/AugmentationNames";
export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, helper: INetscriptHelper): IStanek {
function checkStanekAPIAccess(func: string): void {
if (!player.hasAugmentation(AugmentationNames.StaneksGift1, true)) {
helper.makeRuntimeErrorMsg(func, "Requires Stanek's Gift installed.");
}
}
2021-10-05 04:31:07 +02:00
return {
2022-03-29 20:20:40 +02:00
giftWidth: function (): number {
helper.updateDynamicRam("giftWidth", getRamCost(player, "stanek", "giftWidth"));
checkStanekAPIAccess("giftWidth");
2021-10-18 00:59:37 +02:00
return staneksGift.width();
},
2022-03-29 20:20:40 +02:00
giftHeight: function (): number {
helper.updateDynamicRam("giftHeight", getRamCost(player, "stanek", "giftHeight"));
checkStanekAPIAccess("giftHeight");
2021-10-18 00:59:37 +02:00
return staneksGift.height();
},
2022-03-29 20:20:40 +02:00
chargeFragment: function (_rootX: unknown, _rootY: unknown): Promise<void> {
const rootX = helper.number("stanek.chargeFragment", "rootX", _rootX);
const rootY = helper.number("stanek.chargeFragment", "rootY", _rootY);
2021-11-14 05:45:26 +01:00
2022-03-29 20:20:40 +02:00
helper.updateDynamicRam("chargeFragment", getRamCost(player, "stanek", "chargeFragment"));
checkStanekAPIAccess("chargeFragment");
2021-10-18 00:59:37 +02:00
const fragment = staneksGift.findFragment(rootX, rootY);
2022-03-29 20:20:40 +02:00
if (!fragment)
throw helper.makeRuntimeErrorMsg("stanek.chargeFragment", `No fragment with root (${rootX}, ${rootY}).`);
2021-10-08 09:16:51 +02:00
const time = staneksGift.inBonus() ? 200 : 1000;
return netscriptDelay(time, workerScript).then(function () {
2021-11-17 16:59:15 +01:00
const charge = staneksGift.charge(player, fragment, workerScript.scriptRef.threads);
2022-03-29 20:20:40 +02:00
workerScript.log("stanek.chargeFragment", () => `Charged fragment for ${charge} charge.`);
2021-11-14 05:45:26 +01:00
return Promise.resolve();
2021-10-05 04:31:07 +02:00
});
},
2021-11-14 05:45:26 +01:00
fragmentDefinitions: function (): IFragment[] {
2022-01-05 01:09:34 +01:00
helper.updateDynamicRam("fragmentDefinitions", getRamCost(player, "stanek", "fragmentDefinitions"));
2021-11-14 05:45:26 +01:00
checkStanekAPIAccess("fragmentDefinitions");
2021-12-03 21:35:45 +01:00
workerScript.log("stanek.fragmentDefinitions", () => `Returned ${Fragments.length} fragments`);
2021-10-05 04:31:07 +02:00
return Fragments.map((f) => f.copy());
},
2021-11-14 05:45:26 +01:00
activeFragments: function (): IActiveFragment[] {
2022-01-05 01:09:34 +01:00
helper.updateDynamicRam("activeFragments", getRamCost(player, "stanek", "activeFragments"));
2021-11-14 05:45:26 +01:00
checkStanekAPIAccess("activeFragments");
2021-12-03 21:35:45 +01:00
workerScript.log("stanek.activeFragments", () => `Returned ${staneksGift.fragments.length} fragments`);
2021-10-05 04:31:07 +02:00
return staneksGift.fragments.map((af) => {
return { ...af.copy(), ...af.fragment().copy() };
});
},
2022-03-29 20:20:40 +02:00
clearGift: function (): void {
helper.updateDynamicRam("clearGift", getRamCost(player, "stanek", "clearGift"));
checkStanekAPIAccess("clearGift");
workerScript.log("stanek.clearGift", () => `Cleared Stanek's Gift.`);
2021-10-05 04:31:07 +02:00
staneksGift.clear();
},
2022-03-29 20:20:40 +02:00
canPlaceFragment: function (_rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
const rootX = helper.number("stanek.canPlaceFragment", "rootX", _rootX);
const rootY = helper.number("stanek.canPlaceFragment", "rootY", _rootY);
const rotation = helper.number("stanek.canPlaceFragment", "rotation", _rotation);
const fragmentId = helper.number("stanek.canPlaceFragment", "fragmentId", _fragmentId);
helper.updateDynamicRam("canPlaceFragment", getRamCost(player, "stanek", "canPlaceFragment"));
checkStanekAPIAccess("canPlaceFragment");
2021-10-05 04:31:07 +02:00
const fragment = FragmentById(fragmentId);
2022-03-29 20:20:40 +02:00
if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.canPlaceFragment", `Invalid fragment id: ${fragmentId}`);
2021-11-14 05:45:26 +01:00
const can = staneksGift.canPlace(rootX, rootY, rotation, fragment);
2021-10-18 00:59:37 +02:00
return can;
2021-10-05 04:31:07 +02:00
},
2022-03-29 20:20:40 +02:00
placeFragment: function (_rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
const rootX = helper.number("stanek.placeFragment", "rootX", _rootX);
const rootY = helper.number("stanek.placeFragment", "rootY", _rootY);
const rotation = helper.number("stanek.placeFragment", "rotation", _rotation);
const fragmentId = helper.number("stanek.placeFragment", "fragmentId", _fragmentId);
helper.updateDynamicRam("placeFragment", getRamCost(player, "stanek", "placeFragment"));
checkStanekAPIAccess("placeFragment");
2021-10-05 04:31:07 +02:00
const fragment = FragmentById(fragmentId);
2022-03-29 20:20:40 +02:00
if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.placeFragment", `Invalid fragment id: ${fragmentId}`);
2021-11-14 05:45:26 +01:00
return staneksGift.place(rootX, rootY, rotation, fragment);
2021-10-05 04:31:07 +02:00
},
2022-03-29 20:20:40 +02:00
getFragment: function (_rootX: unknown, _rootY: unknown): IActiveFragment | undefined {
const rootX = helper.number("stanek.getFragment", "rootX", _rootX);
const rootY = helper.number("stanek.getFragment", "rootY", _rootY);
helper.updateDynamicRam("getFragment", getRamCost(player, "stanek", "getFragment"));
checkStanekAPIAccess("getFragment");
2021-10-18 00:59:37 +02:00
const fragment = staneksGift.findFragment(rootX, rootY);
if (fragment !== undefined) return fragment.copy();
return undefined;
},
2022-03-29 20:20:40 +02:00
removeFragment: function (_rootX: unknown, _rootY: unknown): boolean {
const rootX = helper.number("stanek.removeFragment", "rootX", _rootX);
const rootY = helper.number("stanek.removeFragment", "rootY", _rootY);
helper.updateDynamicRam("removeFragment", getRamCost(player, "stanek", "removeFragment"));
checkStanekAPIAccess("removeFragment");
2021-11-14 05:45:26 +01:00
return staneksGift.delete(rootX, rootY);
2021-10-05 04:31:07 +02:00
},
};
}