added batteries

This commit is contained in:
Your Name
2023-07-23 11:49:12 -04:00
parent b0cbdf5625
commit 95ecd3e97e
3 changed files with 31 additions and 0 deletions

View File

@ -13,6 +13,7 @@ export class Myrian {
world: string[][] = [];
resources = 0;
sleeves: MyrianSleeve[] = [];
storedCycles = 0;
constructor() {
this.world = DefaultWorld;
@ -31,6 +32,30 @@ export class Myrian {
return [1, 1];
}
/** Main process function called by the engine loop every game cycle */
process(numCycles = 1): void {
const CYCLE_CHUNK = 10;
this.storedCycles += numCycles;
if (this.storedCycles < CYCLE_CHUNK) return;
// Calculate how many cycles to actually process.
const cycles = Math.min(this.storedCycles, CYCLE_CHUNK);
try {
for (let x = 0; x < this.world.length; x++) {
for (let y = 0; y < this.world[x].length; y++) {
const tile = this.world[x][y];
if (tile === "d" && Math.random() > 0.99) {
this.world[x][y] = "b";
}
}
}
this.storedCycles -= cycles;
} catch (e: unknown) {
console.error(`Exception caught when processing Gang: ${e}`);
}
}
/** Serialize the current object to a JSON save state. */
toJSON(): IReviverValue {
return Generic_toJSON("Myrian", this);

View File

@ -17,7 +17,10 @@ const move = async (ctx: NetscriptContext, sleeve: MyrianSleeve, x: number, y: n
};
const drain = async (ctx: NetscriptContext, sleeve: MyrianSleeve, x: number, y: number) => {
const tile = myrian.world[y][x];
if (tile !== "b") throw new Error(`Invalid tile. Must be 'b' but is ${tile}`);
return helpers.netscriptDelay(ctx, 100).then(() => {
myrian.world[y][x] = "d";
return Promise.resolve();
});
};

View File

@ -43,6 +43,7 @@ import React from "react";
import { setupUncaughtPromiseHandler } from "./UncaughtPromiseHandler";
import { Button, Typography } from "@mui/material";
import { SnackbarEvents } from "./ui/React/Snackbar";
import { myrian } from "./Myrian/Helpers";
/** Game engine. Handles the main game loop. */
const Engine: {
@ -101,6 +102,8 @@ const Engine: {
// Staneks gift
staneksGift.process(numCycles);
myrian.process(numCycles);
// Corporation
if (Player.corporation) {
Player.corporation.storeCycles(numCycles);