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";
|
|
|
|
|
|
|
|
export interface INetscriptStanek {
|
2021-10-18 00:59:37 +02:00
|
|
|
width(): number;
|
|
|
|
height(): number;
|
|
|
|
charge(rootX: number, rootY: number): any;
|
2021-10-05 04:31:07 +02:00
|
|
|
fragmentDefinitions(): any;
|
|
|
|
placedFragments(): any;
|
2021-10-05 05:51:39 +02:00
|
|
|
clear(): void;
|
2021-10-16 23:12:04 +02:00
|
|
|
canPlace(worldX: number, worldY: number, rotation: number, fragmentId: number): boolean;
|
|
|
|
place(worldX: number, worldY: number, rotation: number, fragmentId: number): boolean;
|
2021-10-18 00:59:37 +02:00
|
|
|
findFragment(rootX: any, rootY: any): any;
|
2021-10-05 05:51:39 +02:00
|
|
|
fragmentAt(worldX: number, worldY: number): any;
|
|
|
|
deleteAt(worldX: number, worldY: number): boolean;
|
2021-10-05 04:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function NetscriptStanek(
|
|
|
|
player: IPlayer,
|
|
|
|
workerScript: WorkerScript,
|
|
|
|
helper: INetscriptHelper,
|
|
|
|
): INetscriptStanek {
|
|
|
|
return {
|
2021-10-18 00:59:37 +02:00
|
|
|
width: function (): number {
|
|
|
|
return staneksGift.width();
|
|
|
|
},
|
|
|
|
height: function (): number {
|
|
|
|
return staneksGift.height();
|
|
|
|
},
|
|
|
|
charge: function (rootX: any, rootY: any): any {
|
2021-10-05 04:31:07 +02:00
|
|
|
helper.updateDynamicRam("charge", getRamCost("stanek", "charge"));
|
|
|
|
//checkStanekAPIAccess("charge");
|
2021-10-18 00:59:37 +02:00
|
|
|
const fragment = staneksGift.findFragment(rootX, rootY);
|
|
|
|
if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.charge", `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-10-05 04:31:07 +02:00
|
|
|
if (workerScript.env.stopFlag) {
|
|
|
|
return Promise.reject(workerScript);
|
|
|
|
}
|
|
|
|
const ram = workerScript.scriptRef.ramUsage * workerScript.scriptRef.threads;
|
2021-10-18 00:59:37 +02:00
|
|
|
const charge = staneksGift.charge(rootX, rootY, ram);
|
|
|
|
workerScript.log("stanek.charge", `Charged fragment for ${charge} charge.`);
|
|
|
|
return Promise.resolve(charge);
|
2021-10-05 04:31:07 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
fragmentDefinitions: function () {
|
|
|
|
helper.updateDynamicRam("fragmentDefinitions", getRamCost("stanek", "fragmentDefinitions"));
|
|
|
|
//checkStanekAPIAccess("fragmentDefinitions");
|
2021-10-18 00:59:37 +02:00
|
|
|
workerScript.log("stanek.fragmentDefinitions", `Returned ${Fragments.length} fragments`);
|
2021-10-05 04:31:07 +02:00
|
|
|
return Fragments.map((f) => f.copy());
|
|
|
|
},
|
|
|
|
placedFragments: function () {
|
|
|
|
helper.updateDynamicRam("placedFragments", getRamCost("stanek", "placedFragments"));
|
|
|
|
//checkStanekAPIAccess("placedFragments");
|
2021-10-18 00:59:37 +02:00
|
|
|
workerScript.log("stanek.placedFragments", `Returned ${staneksGift.fragments.length} fragments`);
|
2021-10-05 04:31:07 +02:00
|
|
|
return staneksGift.fragments.map((af) => {
|
|
|
|
return { ...af.copy(), ...af.fragment().copy() };
|
|
|
|
});
|
|
|
|
},
|
|
|
|
clear: function () {
|
|
|
|
helper.updateDynamicRam("clear", getRamCost("stanek", "clear"));
|
|
|
|
//checkStanekAPIAccess("clear");
|
2021-10-18 00:59:37 +02:00
|
|
|
workerScript.log("stanek.clear", `Cleared Stanek's Gift.`);
|
2021-10-05 04:31:07 +02:00
|
|
|
staneksGift.clear();
|
|
|
|
},
|
2021-10-16 23:12:04 +02:00
|
|
|
canPlace: function (worldX: any, worldY: any, rotation: any, fragmentId: any): any {
|
2021-10-05 04:31:07 +02:00
|
|
|
helper.updateDynamicRam("canPlace", getRamCost("stanek", "canPlace"));
|
|
|
|
//checkStanekAPIAccess("canPlace");
|
|
|
|
const fragment = FragmentById(fragmentId);
|
|
|
|
if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.canPlace", `Invalid fragment id: ${fragmentId}`);
|
2021-10-18 00:59:37 +02:00
|
|
|
const can = staneksGift.canPlace(worldX, worldY, rotation, fragment);
|
|
|
|
return can;
|
2021-10-05 04:31:07 +02:00
|
|
|
},
|
2021-10-16 23:12:04 +02:00
|
|
|
place: function (worldX: any, worldY: any, rotation: any, fragmentId: any): any {
|
2021-10-05 04:31:07 +02:00
|
|
|
helper.updateDynamicRam("place", getRamCost("stanek", "place"));
|
|
|
|
//checkStanekAPIAccess("place");
|
|
|
|
const fragment = FragmentById(fragmentId);
|
|
|
|
if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.place", `Invalid fragment id: ${fragmentId}`);
|
2021-10-16 23:12:04 +02:00
|
|
|
return staneksGift.place(worldX, worldY, rotation, fragment);
|
2021-10-05 04:31:07 +02:00
|
|
|
},
|
2021-10-18 00:59:37 +02:00
|
|
|
findFragment: function (rootX: any, rootY: any): any {
|
|
|
|
helper.updateDynamicRam("findFragment", getRamCost("stanek", "findFragment"));
|
|
|
|
//checkStanekAPIAccess("fragmentAt");
|
|
|
|
const fragment = staneksGift.findFragment(rootX, rootY);
|
|
|
|
if (fragment !== undefined) return fragment.copy();
|
|
|
|
return undefined;
|
|
|
|
},
|
2021-10-05 04:31:07 +02:00
|
|
|
fragmentAt: function (worldX: any, worldY: any): any {
|
|
|
|
helper.updateDynamicRam("fragmentAt", getRamCost("stanek", "fragmentAt"));
|
|
|
|
//checkStanekAPIAccess("fragmentAt");
|
|
|
|
const fragment = staneksGift.fragmentAt(worldX, worldY);
|
2021-10-18 00:59:37 +02:00
|
|
|
if (fragment !== undefined) return fragment.copy();
|
|
|
|
return undefined;
|
2021-10-05 04:31:07 +02:00
|
|
|
},
|
|
|
|
deleteAt: function (worldX: any, worldY: any): any {
|
|
|
|
helper.updateDynamicRam("deleteAt", getRamCost("stanek", "deleteAt"));
|
|
|
|
//checkStanekAPIAccess("deleteAt");
|
|
|
|
return staneksGift.deleteAt(worldX, worldY);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|