8000 add orientation utils by dhvanipa · Pull Request #281 · dustproject/dust · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add orientation utils #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
< 10000 span data-view-component="true"> Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions packages/world/src/ObjectType.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1163,9 +1163,36 @@ library ObjectTypeLib {
}

function isOrientationSupported(ObjectType self, Orientation orientation) internal pure returns (bool) {
if (self == ObjectTypes.TextSign) {
return orientation == Orientation.wrap(0) || orientation == Orientation.wrap(1)
|| orientation == Orientation.wrap(40) || orientation == Orientation.wrap(44);
}
if (self == ObjectTypes.ForceField) {
return orientation == Orientation.wrap(0) || orientation == Orientation.wrap(1)
|| orientation == Orientation.wrap(40) || orientation == Orientation.wrap(44);
}
if (self == ObjectTypes.Chest) {
return orientation == Orientation.wrap(0) || orientation == Orientation.wrap(1)
|| orientation == Orientation.wrap(40) || orientation == Orientation.wrap(44);
}
if (self == ObjectTypes.SpawnTile) {
return orientation == Orientation.wrap(0) || orientation == Orientation.wrap(1)
|| orientation == Orientation.wrap(40) || orientation == Orientation.wrap(44);
}
if (self == ObjectTypes.Bed) {
return orientation == Orientation.wrap(0) || orientation == Orientation.wrap(40)
|| orientation == Orientation.wrap(1) || orientation == Orientation.wrap(44);
return orientation == Orientation.wrap(1) || orientation == Orientation.wrap(44);
}
if (self == ObjectTypes.Workbench) {
return orientation == Orientation.wrap(0) || orientation == Orientation.wrap(1)
|| orientation == Orientation.wrap(40) || orientation == Orientation.wrap(44);
}
if (self == ObjectTypes.Powerstone) {
return orientation == Orientation.wrap(0) || orientation == Orientation.wrap(1)
|| orientation == Orientation.wrap(40) || orientation == Orientation.wrap(44);
}
if (self == ObjectTypes.Furnace) {
return orientation == Orientation.wrap(0) || orientation == Orientation.wrap(1)
|| orientation == Orientation.wrap(40) || orientation == Orientation.wrap(44);
}

return orientation == Orientation.wrap(0);
Expand Down
1 change: 1 addition & 0 deletions packages/world/ts/exports/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "../recipes";
export * from "../trees";
export * from "../vec3";
export * from "../entityid";
export * from "../orientation";
75 changes: 68 additions & 7 deletions packages/world/ts/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,16 @@ export const objectDef: Optional<ObjectDefinition, "id">[] = [
{ name: "GoldOre", mass: 1600000000000000000n },
{ name: "DiamondOre", mass: 5000000000000000000n },
{ name: "NeptuniumOre", mass: 5000000000000000000n },
{ name: "TextSign", mass: 18000000000000000n },
{
name: "TextSign",
mass: 18000000000000000n,
supportedOrientations: [
getOrientation("PositiveX"),
getOrientation("NegativeX"),
getOrientation("PositiveZ"),
10000 getOrientation("NegativeZ"),
],
},
{ name: "OakPlanks", mass: 4500000000000000n },
{ name: "BirchPlanks", mass: 4500000000000000n },
{ name: "JunglePlanks", mass: 4500000000000000n },
Expand Down Expand Up @@ -453,22 +462,74 @@ export const objectDef: Optional<ObjectDefinition, "id">[] = [
growableEnergy: 232000000000000000n,
timeToGrow: 345600n,
},
{ name: "ForceField", mass: 1035000000000000000n },
{ name: "Chest", mass: 36000000000000000n },
{ name: "SpawnTile", mass: 6435000000000000000n },
{
name: "ForceField",
mass: 1035000000000000000n,
supportedOrientations: [
getOrientation("PositiveX"),
getOrientation("NegativeX"),
getOrientation("PositiveZ"),
getOrientation("NegativeZ"),
],
},
{
name: "Chest",
mass: 36000000000000000n,
supportedOrientations: [
getOrientation("PositiveX"),
getOrientation("NegativeX"),
getOrientation("PositiveZ"),
getOrientation("NegativeZ"),
],
},
{
name: "SpawnTile",
mass: 6435000000000000000n,
supportedOrientations: [
getOrientation("PositiveX"),
getOrientation("NegativeX"),
getOrientation("PositiveZ"),
getOrientation("NegativeZ"),
],
},
{
name: "Bed",
mass: 13500000000000000n,
supportedOrientations: [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a limitation from our client where the bed only supports these 2

getOrientation("NegativeX"),
getOrientation("NegativeZ"),
],
},
{
name: "Workbench",
mass: 18000000000000000n,
supportedOrientations: [
getOrientation("PositiveX"),
getOrientation("NegativeX"),
getOrientation("PositiveZ"),
getOrientation("NegativeZ"),
],
},
{
name: "Powerstone",
mass: 80000000000000000n,
supportedOrientations: [
getOrientation("PositiveX"),
getOrientation("NegativeX"),
getOrientation("PositiveZ"),
getOrientation("NegativeZ"),
],
},
{
name: "Furnace",
mass: 108000000000000000n,
supportedOrientations: [
getOrientation("PositiveX"),
getOrientation("NegativeX"),
getOrientation("PositiveZ"),
getOrientation("NegativeZ"),
],
},
{ name: "Workbench", mass: 18000000000000000n },
{ name: "Powerstone", mass: 80000000000000000n },
{ name: "Furnace", mass: 108000000000000000n },
{ name: "Torch", mass: 1125000000000000n },

// Non blocks
Expand Down
149 changes: 115 additions & 34 deletions packages/world/ts/orientation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import config from "../mud.config";
import type { Vec3 } from "./vec3";

// Direction is an array of strings
config.enums.Direction;

Expand All @@ -17,54 +19,76 @@ const SUPPORTED_DIRECTION_VECTORS: {

type SupportedDirection = keyof typeof SUPPORTED_DIRECTION_VECTORS;

const PERMUTATIONS: [number, number, number][] = [
[0, 1, 2],
[0, 2, 1],
[1, 0, 2],
[1, 2, 0],
[2, 0, 1],
[2, 1, 0],
];

const REFLECTIONS: [number, number, number][] = [
[0, 0, 0],
[1, 0, 0],
[0, 1, 0],
[1, 1, 0],
[0, 0, 1],
[1, 0, 1],
[0, 1, 1],
[1, 1, 1],
];

const CANONICAL_UP: [number, number, number] = [0, 1, 0];
const CANONICAL_FORWARD: [number, number, number] = [1, 0, 0];

function applyOrientation(
v: [number, number, number],
perm: [number, number, number],
refl: [number, number, number],
): [number, number, number] {
const out: [number, number, number] = [v[perm[0]]!, v[perm[1]]!, v[perm[2]]!];
export type Orientation = number; // uint8 in Solidity

type Axis = 0 | 1 | 2;
export type Permute = readonly [Axis, Axis, Axis];
export type Reflect = readonly [0 | 1, 0 | 1, 0 | 1];

// 6 possible permutations
export const PERMUTATIONS: readonly [
Permute,
Permute,
Permute,
Permute,
Permute,
Permute,
] = [
[0, 1, 2], // Original orientation
[0, 2, 1], // Swap y and z
[1, 0, 2], // Swap x and y
[1, 2, 0], // Rotate x->y->z->x
[2, 0, 1], // Rotate x->z->y->x
[2, 1, 0], // Swap x and z
] as const;

// 8 possible reflections
export const REFLECTIONS: readonly [
Reflect,
Reflect,
Reflect,
Reflect,
Reflect,
Reflect,
Reflect,
Reflect,
] = [
[0, 0, 0], // No reflection
[1, 0, 0], // Reflect x
[0, 1, 0], // Reflect y
[1, 1, 0], // Reflect x and y
[0, 0, 1], // Reflect z
[1, 0, 1], // Reflect x and z
[0, 1, 9E81 1], // Reflect y and z
[1, 1, 1], // Reflect all axes
] as const;

const CANONICAL_UP: Vec3 = [0, 1, 0];
const CANONICAL_FORWARD: Vec3 = [1, 0, 0];

export function applyOrientation(v: Vec3, perm: Permute, refl: Reflect): Vec3 {
const out: Vec3 = [v[perm[0]]!, v[perm[1]]!, v[perm[2]]!];
if (refl[0]) out[0] = -out[0];
if (refl[1]) out[1] = -out[1];
if (refl[2]) out[2] = -out[2];
return out;
}

export function getOrientation(forwardDirection: SupportedDirection): number {
export function getOrientation(
forwardDirection: SupportedDirection,
): Orientation {
return getOrientationGeneric(forwardDirection, "PositiveY");
}

export function getOrientationGeneric(
forwardDirection: SupportedDirection,
upDirection: SupportedDirection,
): number {
): Orientation {
const targetForward = SUPPORTED_DIRECTION_VECTORS[forwardDirection]!;
const targetUp = SUPPORTED_DIRECTION_VECTORS[upDirection]!;

for (let permIdx = 0; permIdx < 6; ++permIdx) {
for (let reflIdx = 0; reflIdx < 8; ++reflIdx) {
for (let permIdx = 0; permIdx < PERMUTATIONS.length; ++permIdx) {
for (let reflIdx = 0; reflIdx < REFLECTIONS.length; ++reflIdx) {
const perm = PERMUTATIONS[permIdx]!;
const refl = REFLECTIONS[reflIdx]!;

Expand All @@ -79,9 +103,66 @@ export function getOrientationGeneric(
forward[1] === targetForward[1] &&
forward[2] === targetForward[2]
) {
return permIdx * 8 + reflIdx;
return encodeOrientation(perm, refl);
}
}
}
throw new Error("Unable to find orientation");
}

export function getDirection(orientation: Orientation): SupportedDirection {
return getDirectionGeneric(orientation, "PositiveY");
}

export function getDirectionGeneric(
orientation: Orientation,
upDirection: SupportedDirection,
): SupportedDirection {
const [perm, refl] = decodeOrientation(orientation);
const up = applyOrientation(CANONICAL_UP, perm, refl);
const targetUp = SUPPORTED_DIRECTION_VECTORS[upDirection]!;

const forward = applyOrientation(CANONICAL_FORWARD, perm, refl);

const forwardDirection = Object.keys(SUPPORTED_DIRECTION_VECTORS).find(
(dir) => {
const targetForward =
SUPPORTED_DIRECTION_VECTORS[dir as SupportedDirection]!;
return (
up[0] === targetUp[0] &&
up[1] === targetUp[1] &&
up[2] === targetUp[2] &&
forward[0] === targetForward[0] &&
forward[1] === targetForward[1] &&
forward[2] === targetForward[2]
);
},
);
if (!forwardDirection) throw new Error("Unable to find direction");
return forwardDirection as SupportedDirection;
}

export function encodeOrientation(perm: Permute, refl: Reflect): Orientation {
// Find the index of the permutation in PERMUTATIONS array
const permIdx = PERMUTATIONS.findIndex(
(p) => p[0] === perm[0] && p[1] === perm[1] && p[2] === perm[2],
);
if (permIdx === -1) throw new Error("Invalid permutation");

// Find the index of the reflection in REFLECTIONS array
const reflIdx = REFLECTIONS.findIndex(
(r) => r[0] === refl[0] && r[1] === refl[1] && r[2] === refl[2],
);
if (reflIdx === -1) throw new Error("Invalid reflection");

// Combine the indices into a single orientation number
return (permIdx << 3) | reflIdx;
}

export function decodeOrientation(
orientation: Orientation,
): [Permute, Reflect] {
const permIdx = orientation >> 3;
const reflIdx = orientation & 7;
return [PERMUTATIONS[permIdx]!, REFLECTIONS[reflIdx]!];
}
3 changes: 2 additions & 1 deletion packages/world/ts/scripts/genObjectTypeSol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ ${Object.entries(categories)
return `if (self == ObjectTypes.${obj.name}) {
return ${obj.supportedOrientations!.map((orientation) => `orientation == Orientation.wrap(${orientation})`).join(" || ")};
}`;
})}
})
.join("\n ")}

return orientation == Orientation.wrap(0);
}
Expand Down
Loading
0