Skip to content

Commit

Permalink
Optimized blob function
Browse files Browse the repository at this point in the history
  • Loading branch information
SIsilicon committed Mar 22, 2024
1 parent ceebfe5 commit 0378e63
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 119 deletions.
11 changes: 11 additions & 0 deletions src/library/utils/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { system } from "@minecraft/server";

export function doOn(command: string, callback: () => void) {
system.afterEvents.scriptEventReceive.subscribe(
(ev) => {
if (ev.id !== "dev:" + command) return;
callback();
},
{ namespaces: ["dev"] }
);
}
107 changes: 0 additions & 107 deletions src/library/utils/formatter.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/library/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from "./bounds.js";
export * from "./contentlog.js";
export * from "./formatter.js";
export * from "./multithreading.js";
export * from "./debug.js";
export * from "./rawtext.js";
export * from "./scheduling.js";
export * from "./vector.js";
Expand Down
23 changes: 12 additions & 11 deletions src/server/modules/pattern.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Vector3, BlockPermutation, Player, Dimension } from "@minecraft/server";
import { CustomArgType, commandSyntaxError, Vector, Server } from "@notbeer-api";
import { CustomArgType, commandSyntaxError, Vector, Server, Timer, contentLog, doOn } from "@notbeer-api";
import { PlayerSession } from "server/sessions.js";
import { wrap } from "server/util.js";
import { Token } from "./extern/tokenizr.js";
Expand Down Expand Up @@ -549,16 +549,21 @@ class BlobPattern extends PatternNode {
}

getPermutation(block: BlockUnit, context: patternContext): BlockPermutation {
const size = this.size;
const blockLoc = Vector.from(block.location);
const cellLoc = blockLoc.div(this.size).floor().mul(this.size);
const cellLoc = blockLoc.div(size).floor().mul(size);
let closestCell = 0;
let minDist = Infinity;
for (const offset of this.offsets) {
const loc = cellLoc.add(offset);
const neighbour = this.getCellKey(loc);
if (!(neighbour in this.points)) this.points[neighbour] = new Vector(this.randomNum(), this.randomNum(), this.randomNum());

const distance = loc.add(this.points[neighbour]).distanceTo(blockLoc);
const locX = cellLoc.x + offset.x;
const locY = cellLoc.y + offset.y;
const locZ = cellLoc.z + offset.z;
// cell key
const neighbour = Math.floor(Math.floor(locX / size) * 4576.498 + Math.floor(locY / size) * 76392.953 + Math.floor(locZ / size) * 203478.295) % 1024;
if (!this.points[neighbour]) this.points[neighbour] = new Vector(this.randomNum(), this.randomNum(), this.randomNum());
const point = this.points[neighbour];

const distance = Math.hypot(locX + point.x - blockLoc.x, locY + point.y - blockLoc.y, locZ + point.z - blockLoc.z);
if (distance < minDist) {
closestCell = neighbour;
minDist = distance;
Expand All @@ -569,10 +574,6 @@ class BlobPattern extends PatternNode {
return this.perms[closestCell];
}

private getCellKey(location: Vector3) {
return `${Math.floor(location.x / this.size)} ${Math.floor(location.y / this.size)} ${Math.floor(location.z / this.size)}`.hashCode();
}

private randomNum() {
return Math.random() * (this.size - 1);
}
Expand Down

0 comments on commit 0378e63

Please sign in to comment.