Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Add the Roaming Glitch
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Gagnon committed Jun 10, 2024
1 parent e9edcd2 commit e0cd360
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
72 changes: 71 additions & 1 deletion src/Myrian/Helper.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { DeviceType, Component, Lock, Glitch } from "@nsdefs";
import { Myrian, findDevice } from "./Myrian";
import { Myrian, findDevice, inMyrianBounds } from "./Myrian";
import { getNextISocketRequest } from "./formulas/formulas";
import { decodeBase64 } from "bcryptjs";
import { roamingTime } from "./formulas/glitches";

export const myrianSize = 12;

Expand Down Expand Up @@ -178,4 +180,72 @@ setInterval(() => {
}
}, 30000);

const clamp = (min: number, v: number, max: number) => Math.min(Math.max(v, min), max);
let globalOffset = 0;
const dirDiff = (v: number): number => {
globalOffset++;
const r = Math.random();
const d = v - (myrianSize - 1) / 2;
const h = d > 0 ? -1 : 1;

const dv = Math.floor(r * 3 + h * Math.random() * Math.sin(globalOffset * 0.05) * Math.abs(d)) - 1;
return clamp(-1, dv, 1);
};

const isEmpty = (x: number, y: number) => {
if (!inMyrianBounds(x, y)) return false;
return !findDevice([x, y]);
};

const dirs = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];

const applyRoaming = () => {
const roaming = myrian.glitches[Glitch.Roaming];
setTimeout(applyRoaming, roamingTime(roaming));
if (roaming === 0) return;
myrian.devices.forEach((device) => {
if (device.type !== DeviceType.OSocket && device.type !== DeviceType.ISocket) return;
if (device.isBusy) return;
let canMove = false;
for (const dir of dirs) {
const [dx, dy] = dir;
if (isEmpty(device.x + dx, device.y + dy)) {
canMove = true;
break;
}
}
let x = -1;
let y = -1;
if (canMove) {
let dx = dirDiff(device.x);
let dy = dirDiff(device.y);

if (dx !== 0 && dy !== 0) {
if (Math.random() > 0.5) {
dx = 0;
} else {
dy = 0;
}
}
x = device.x + dx;
y = device.y + dy;
} else {
x = Math.floor(Math.random() * myrianSize);
y = Math.floor(Math.random() * myrianSize);
}

if (findDevice([x, y])) return;
if (!inMyrianBounds(x, y)) return;
device.x = x;
device.y = y;
});
};

setTimeout(applyRoaming, 0);

resetMyrian();
2 changes: 2 additions & 0 deletions src/Myrian/formulas/glitches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ export const jammingMult = (lvl: number) => Math.pow(1.3, lvl);

// energy loss
export const magnetismLoss = (lvl: number) => lvl;

export const roamingTime = (lvl: number) => 30000 * Math.pow(0.7, lvl);
2 changes: 1 addition & 1 deletion src/Myrian/ui/MyrianRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const YHeader = () => {
interface IProps {}

export const MyrianRoot = (__props: IProps): React.ReactElement => {
useRerender(200);
useRerender(50);
return (
<Container maxWidth="lg" disableGutters sx={{ mx: 0 }}>
<Typography variant="h4">Myrian OS</Typography>
Expand Down

0 comments on commit e0cd360

Please sign in to comment.