change the way charge works

This commit is contained in:
Olivier Gagnon 2021-10-17 18:59:37 -04:00
parent 4bef2f09a5
commit 35a5e2f343
10 changed files with 66 additions and 39 deletions

22
dist/vendor.bundle.js vendored

File diff suppressed because one or more lines are too long

@ -12,12 +12,11 @@ placedFragments() Netscript Function
// In world coordinates // In world coordinates
x: number; x: number;
y: number; y: number;
heat: number;
charge: number; charge: number;
id: number; id: number;
shape: boolean[][]; shape: boolean[][];
type: string; type: string;
magnitude: number; power: number;
limit: number; limit: number;
} }
] ]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -7,12 +7,13 @@ export interface IStaneksGift {
fragments: ActiveFragment[]; fragments: ActiveFragment[];
width(): number; width(): number;
height(): number; height(): number;
charge(worldX: number, worldY: number, ram: number): number; charge(rootX: number, rootY: number, ram: number): number;
process(p: IPlayer, n: number): void; process(p: IPlayer, n: number): void;
effect(fragment: ActiveFragment): number; effect(fragment: ActiveFragment): number;
canPlace(x: number, y: number, rotation: number, fragment: Fragment): boolean; canPlace(x: number, y: number, rotation: number, fragment: Fragment): boolean;
place(x: number, y: number, rotation: number, fragment: Fragment): boolean; place(x: number, y: number, rotation: number, fragment: Fragment): boolean;
fragmentAt(worldX: number, worldY: number): ActiveFragment | null; findFragment(rootX: number, rootY: number): ActiveFragment | undefined;
fragmentAt(worldX: number, worldY: number): ActiveFragment | undefined;
deleteAt(worldX: number, worldY: number): boolean; deleteAt(worldX: number, worldY: number): boolean;
clear(): void; clear(): void;
count(fragment: Fragment): number; count(fragment: Fragment): number;

@ -29,9 +29,9 @@ export class StaneksGift implements IStaneksGift {
return Math.floor(this.baseSize() / 2 + 0.6); return Math.floor(this.baseSize() / 2 + 0.6);
} }
charge(worldX: number, worldY: number, ram: number): number { charge(rootX: number, rootY: number, ram: number): number {
const af = this.fragmentAt(worldX, worldY); const af = this.findFragment(rootX, rootY);
if (af === null) return 0; if (af === undefined) return 0;
const charge = CalculateCharge(ram); const charge = CalculateCharge(ram);
af.charge += charge; af.charge += charge;
@ -60,8 +60,8 @@ export class StaneksGift implements IStaneksGift {
// find the neighbooring active fragments. // find the neighbooring active fragments.
const maybeFragments = cells.map((n) => this.fragmentAt(n[0], n[1])); const maybeFragments = cells.map((n) => this.fragmentAt(n[0], n[1]));
// Filter out nulls with typescript "Type guard". Whatever // Filter out undefined with typescript "Type guard". Whatever
let neighboors = maybeFragments.filter((v: ActiveFragment | null): v is ActiveFragment => !!v); let neighboors = maybeFragments.filter((v: ActiveFragment | undefined): v is ActiveFragment => !!v);
neighboors = neighboors.filter((fragment) => fragment.fragment().type === FragmentType.Booster); neighboors = neighboors.filter((fragment) => fragment.fragment().type === FragmentType.Booster);
let boost = 1; let boost = 1;
@ -90,14 +90,18 @@ export class StaneksGift implements IStaneksGift {
return true; return true;
} }
fragmentAt(worldX: number, worldY: number): ActiveFragment | null { findFragment(rootX: number, rootY: number): ActiveFragment | undefined {
return this.fragments.find((f) => f.x === rootX && f.y === rootY);
}
fragmentAt(worldX: number, worldY: number): ActiveFragment | undefined {
for (const aFrag of this.fragments) { for (const aFrag of this.fragments) {
if (aFrag.fullAt(worldX, worldY)) { if (aFrag.fullAt(worldX, worldY)) {
return aFrag; return aFrag;
} }
} }
return null; return undefined;
} }
count(fragment: Fragment): number { count(fragment: Fragment): number {

@ -9,7 +9,7 @@ import Typography from "@mui/material/Typography";
type IProps = { type IProps = {
gift: IStaneksGift; gift: IStaneksGift;
fragment: ActiveFragment | null; fragment: ActiveFragment | undefined;
x: number; x: number;
y: number; y: number;
}; };
@ -23,7 +23,7 @@ export function FragmentInspector(props: IProps): React.ReactElement {
return () => clearInterval(id); return () => clearInterval(id);
}, []); }, []);
if (props.fragment === null) { if (props.fragment === undefined) {
return ( return (
<Paper> <Paper>
<Typography> <Typography>

@ -49,7 +49,7 @@ export function MainBoard(props: IProps): React.ReactElement {
for (let i = 0; i < gift.width(); i++) { for (let i = 0; i < gift.width(); i++) {
for (let j = 0; j < gift.height(); j++) { for (let j = 0; j < gift.height(); j++) {
const fragment = gift.fragmentAt(i, j); const fragment = gift.fragmentAt(i, j);
if (fragment === null) continue; if (!fragment) continue;
newgrid[i][j] = 1; newgrid[i][j] = 1;
} }
} }
@ -64,6 +64,7 @@ export function MainBoard(props: IProps): React.ReactElement {
const [selectedFragment, setSelectedFragment] = React.useState(NoneFragment); const [selectedFragment, setSelectedFragment] = React.useState(NoneFragment);
function moveGhost(worldX: number, worldY: number, rotation: number): void { function moveGhost(worldX: number, worldY: number, rotation: number): void {
setPos([worldX, worldY]);
if (selectedFragment.type === FragmentType.None || selectedFragment.type === FragmentType.Delete) return; if (selectedFragment.type === FragmentType.None || selectedFragment.type === FragmentType.Delete) return;
const newgrid = zeros([props.gift.width(), props.gift.height()]); const newgrid = zeros([props.gift.width(), props.gift.height()]);
for (let y = 0; y < selectedFragment.height(rotation); y++) { for (let y = 0; y < selectedFragment.height(rotation); y++) {
@ -76,7 +77,6 @@ export function MainBoard(props: IProps): React.ReactElement {
} }
setGhostGrid(newgrid); setGhostGrid(newgrid);
setPos([worldX, worldY]);
} }
function deleteAt(worldX: number, worldY: number): boolean { function deleteAt(worldX: number, worldY: number): boolean {
@ -97,9 +97,10 @@ export function MainBoard(props: IProps): React.ReactElement {
function color(worldX: number, worldY: number): string { function color(worldX: number, worldY: number): string {
if (ghostGrid[worldX][worldY] && grid[worldX][worldY]) return "red"; if (ghostGrid[worldX][worldY] && grid[worldX][worldY]) return "red";
if (ghostGrid[worldX][worldY]) return "white"; if (ghostGrid[worldX][worldY]) return "white";
if (grid[worldX][worldY]) { if (grid[worldX][worldY]) {
const fragment = props.gift.fragmentAt(worldX, worldY); const fragment = props.gift.fragmentAt(worldX, worldY);
if (fragment === null) throw new Error("ActiveFragment should not be null"); if (!fragment) throw new Error("ActiveFragment should not be null");
return randomColor(fragment); return randomColor(fragment);
} }
return ""; return "";

@ -8,12 +8,15 @@ import { staneksGift } from "../CotMG/Helper";
import { Fragments, FragmentById } from "../CotMG/Fragment"; import { Fragments, FragmentById } from "../CotMG/Fragment";
export interface INetscriptStanek { export interface INetscriptStanek {
charge(worldX: number, worldY: number): any; width(): number;
height(): number;
charge(rootX: number, rootY: number): any;
fragmentDefinitions(): any; fragmentDefinitions(): any;
placedFragments(): any; placedFragments(): any;
clear(): void; clear(): void;
canPlace(worldX: number, worldY: number, rotation: number, fragmentId: number): boolean; canPlace(worldX: number, worldY: number, rotation: number, fragmentId: number): boolean;
place(worldX: number, worldY: number, rotation: number, fragmentId: number): boolean; place(worldX: number, worldY: number, rotation: number, fragmentId: number): boolean;
findFragment(rootX: any, rootY: any): any;
fragmentAt(worldX: number, worldY: number): any; fragmentAt(worldX: number, worldY: number): any;
deleteAt(worldX: number, worldY: number): boolean; deleteAt(worldX: number, worldY: number): boolean;
} }
@ -24,28 +27,38 @@ export function NetscriptStanek(
helper: INetscriptHelper, helper: INetscriptHelper,
): INetscriptStanek { ): INetscriptStanek {
return { return {
charge: function (worldX: any, worldY: any): any { width: function (): number {
return staneksGift.width();
},
height: function (): number {
return staneksGift.height();
},
charge: function (rootX: any, rootY: any): any {
helper.updateDynamicRam("charge", getRamCost("stanek", "charge")); helper.updateDynamicRam("charge", getRamCost("stanek", "charge"));
//checkStanekAPIAccess("charge"); //checkStanekAPIAccess("charge");
const fragment = staneksGift.fragmentAt(worldX, worldY); const fragment = staneksGift.findFragment(rootX, rootY);
if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.charge", `No fragment at (${worldX}, ${worldY})`); if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.charge", `No fragment with root (${rootX}, ${rootY}).`);
const time = staneksGift.inBonus() ? 200 : 1000; const time = staneksGift.inBonus() ? 200 : 1000;
return netscriptDelay(time, workerScript).then(function () { return netscriptDelay(time, workerScript).then(function () {
if (workerScript.env.stopFlag) { if (workerScript.env.stopFlag) {
return Promise.reject(workerScript); return Promise.reject(workerScript);
} }
const ram = workerScript.scriptRef.ramUsage * workerScript.scriptRef.threads; const ram = workerScript.scriptRef.ramUsage * workerScript.scriptRef.threads;
return Promise.resolve(staneksGift.charge(worldX, worldY, ram)); const charge = staneksGift.charge(rootX, rootY, ram);
workerScript.log("stanek.charge", `Charged fragment for ${charge} charge.`);
return Promise.resolve(charge);
}); });
}, },
fragmentDefinitions: function () { fragmentDefinitions: function () {
helper.updateDynamicRam("fragmentDefinitions", getRamCost("stanek", "fragmentDefinitions")); helper.updateDynamicRam("fragmentDefinitions", getRamCost("stanek", "fragmentDefinitions"));
//checkStanekAPIAccess("fragmentDefinitions"); //checkStanekAPIAccess("fragmentDefinitions");
workerScript.log("stanek.fragmentDefinitions", `Returned ${Fragments.length} fragments`);
return Fragments.map((f) => f.copy()); return Fragments.map((f) => f.copy());
}, },
placedFragments: function () { placedFragments: function () {
helper.updateDynamicRam("placedFragments", getRamCost("stanek", "placedFragments")); helper.updateDynamicRam("placedFragments", getRamCost("stanek", "placedFragments"));
//checkStanekAPIAccess("placedFragments"); //checkStanekAPIAccess("placedFragments");
workerScript.log("stanek.placedFragments", `Returned ${staneksGift.fragments.length} fragments`);
return staneksGift.fragments.map((af) => { return staneksGift.fragments.map((af) => {
return { ...af.copy(), ...af.fragment().copy() }; return { ...af.copy(), ...af.fragment().copy() };
}); });
@ -53,6 +66,7 @@ export function NetscriptStanek(
clear: function () { clear: function () {
helper.updateDynamicRam("clear", getRamCost("stanek", "clear")); helper.updateDynamicRam("clear", getRamCost("stanek", "clear"));
//checkStanekAPIAccess("clear"); //checkStanekAPIAccess("clear");
workerScript.log("stanek.clear", `Cleared Stanek's Gift.`);
staneksGift.clear(); staneksGift.clear();
}, },
canPlace: function (worldX: any, worldY: any, rotation: any, fragmentId: any): any { canPlace: function (worldX: any, worldY: any, rotation: any, fragmentId: any): any {
@ -60,7 +74,8 @@ export function NetscriptStanek(
//checkStanekAPIAccess("canPlace"); //checkStanekAPIAccess("canPlace");
const fragment = FragmentById(fragmentId); const fragment = FragmentById(fragmentId);
if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.canPlace", `Invalid fragment id: ${fragmentId}`); if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.canPlace", `Invalid fragment id: ${fragmentId}`);
return staneksGift.canPlace(worldX, worldY, rotation, fragment); const can = staneksGift.canPlace(worldX, worldY, rotation, fragment);
return can;
}, },
place: function (worldX: any, worldY: any, rotation: any, fragmentId: any): any { place: function (worldX: any, worldY: any, rotation: any, fragmentId: any): any {
helper.updateDynamicRam("place", getRamCost("stanek", "place")); helper.updateDynamicRam("place", getRamCost("stanek", "place"));
@ -69,12 +84,19 @@ export function NetscriptStanek(
if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.place", `Invalid fragment id: ${fragmentId}`); if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.place", `Invalid fragment id: ${fragmentId}`);
return staneksGift.place(worldX, worldY, rotation, fragment); return staneksGift.place(worldX, worldY, rotation, fragment);
}, },
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;
},
fragmentAt: function (worldX: any, worldY: any): any { fragmentAt: function (worldX: any, worldY: any): any {
helper.updateDynamicRam("fragmentAt", getRamCost("stanek", "fragmentAt")); helper.updateDynamicRam("fragmentAt", getRamCost("stanek", "fragmentAt"));
//checkStanekAPIAccess("fragmentAt"); //checkStanekAPIAccess("fragmentAt");
const fragment = staneksGift.fragmentAt(worldX, worldY); const fragment = staneksGift.fragmentAt(worldX, worldY);
if (fragment !== null) return fragment.copy(); if (fragment !== undefined) return fragment.copy();
return null; return undefined;
}, },
deleteAt: function (worldX: any, worldY: any): any { deleteAt: function (worldX: any, worldY: any): any {
helper.updateDynamicRam("deleteAt", getRamCost("stanek", "deleteAt")); helper.updateDynamicRam("deleteAt", getRamCost("stanek", "deleteAt"));

@ -751,7 +751,7 @@ export function SidebarRoot(props: IProps): React.ReactElement {
<Typography color={props.page !== Page.Options ? "secondary" : "primary"}>Options</Typography> <Typography color={props.page !== Page.Options ? "secondary" : "primary"}>Options</Typography>
</ListItemText> </ListItemText>
</ListItem> </ListItem>
{process.env.NODE_ENV === "development" && ( {true && (
<ListItem <ListItem
classes={{ root: classes.listitem }} classes={{ root: classes.listitem }}
button button