bitburner-src/src/NetscriptFunctions/Stanek.ts

116 lines
4.7 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 { staneksGift } from "../CotMG/Helper";
import { Fragments, FragmentById } from "../CotMG/Fragment";
2021-11-14 05:45:26 +01:00
import {
Fragment as IFragment,
ActiveFragment as IActiveFragment,
2022-04-07 02:00:54 +02:00
Stanek as IStanek,
2021-11-14 05:45:26 +01:00
} from "../ScriptEditor/NetscriptDefinitions";
import { AugmentationNames } from "../Augmentation/data/AugmentationNames";
2022-03-30 01:11:13 +02:00
import { NetscriptContext, InternalAPI } from "src/Netscript/APIWrapper";
2021-11-14 05:45:26 +01:00
2022-04-07 02:00:54 +02:00
export function NetscriptStanek(
player: IPlayer,
workerScript: WorkerScript,
helper: INetscriptHelper,
): InternalAPI<IStanek> {
2021-11-14 05:45:26 +01:00
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 {
giftWidth: function (): number {
2022-03-29 20:20:40 +02:00
checkStanekAPIAccess("giftWidth");
2021-10-18 00:59:37 +02:00
return staneksGift.width();
},
giftHeight: function (): number {
2022-03-29 20:20:40 +02:00
checkStanekAPIAccess("giftHeight");
2021-10-18 00:59:37 +02:00
return staneksGift.height();
},
chargeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): Promise<void> {
const rootX = ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY);
2022-03-29 20:20:40 +02:00
checkStanekAPIAccess("chargeFragment");
2021-10-18 00:59:37 +02:00
const fragment = staneksGift.findFragment(rootX, rootY);
if (!fragment) throw ctx.makeRuntimeErrorMsg(`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);
ctx.log(() => `Charged fragment for ${charge} charge.`);
2021-11-14 05:45:26 +01:00
return Promise.resolve();
2021-10-05 04:31:07 +02:00
});
},
fragmentDefinitions: function (ctx: NetscriptContext): IFragment[] {
2021-11-14 05:45:26 +01:00
checkStanekAPIAccess("fragmentDefinitions");
ctx.log(() => `Returned ${Fragments.length} fragments`);
2021-10-05 04:31:07 +02:00
return Fragments.map((f) => f.copy());
},
activeFragments: function (ctx: NetscriptContext): IActiveFragment[] {
2021-11-14 05:45:26 +01:00
checkStanekAPIAccess("activeFragments");
ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`);
2021-10-05 04:31:07 +02:00
return staneksGift.fragments.map((af) => {
return { ...af.copy(), ...af.fragment().copy() };
});
},
clearGift: function (ctx: NetscriptContext): void {
2022-03-29 20:20:40 +02:00
checkStanekAPIAccess("clearGift");
ctx.log(() => `Cleared Stanek's Gift.`);
2021-10-05 04:31:07 +02:00
staneksGift.clear();
},
2022-04-07 02:00:54 +02:00
canPlaceFragment: function (
ctx: NetscriptContext,
_rootX: unknown,
_rootY: unknown,
_rotation: unknown,
_fragmentId: unknown,
): boolean {
const rootX = ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY);
const rotation = ctx.helper.number("rotation", _rotation);
const fragmentId = ctx.helper.number("fragmentId", _fragmentId);
2022-03-29 20:20:40 +02:00
checkStanekAPIAccess("canPlaceFragment");
2021-10-05 04:31:07 +02:00
const fragment = FragmentById(fragmentId);
if (!fragment) throw ctx.makeRuntimeErrorMsg(`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-04-07 02:00:54 +02:00
placeFragment: function (
ctx: NetscriptContext,
_rootX: unknown,
_rootY: unknown,
_rotation: unknown,
_fragmentId: unknown,
): boolean {
const rootX = ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY);
const rotation = ctx.helper.number("rotation", _rotation);
const fragmentId = ctx.helper.number("fragmentId", _fragmentId);
2022-03-29 20:20:40 +02:00
checkStanekAPIAccess("placeFragment");
2021-10-05 04:31:07 +02:00
const fragment = FragmentById(fragmentId);
if (!fragment) throw ctx.makeRuntimeErrorMsg(`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
},
getFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): IActiveFragment | undefined {
const rootX = ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY);
2022-03-29 20:20:40 +02:00
checkStanekAPIAccess("getFragment");
2021-10-18 00:59:37 +02:00
const fragment = staneksGift.findFragment(rootX, rootY);
if (fragment !== undefined) return fragment.copy();
return undefined;
},
removeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): boolean {
const rootX = ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY);
2022-03-29 20:20:40 +02:00
checkStanekAPIAccess("removeFragment");
2021-11-14 05:45:26 +01:00
return staneksGift.delete(rootX, rootY);
2021-10-05 04:31:07 +02:00
},
};
}