Skip to content

Commit

Permalink
Block/chunk pos calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Tisawesomeness committed Sep 20, 2023
1 parent 59729a1 commit 302795f
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.tisawesomeness.minecord.mc.pos;

import com.tisawesomeness.minecord.util.Mth;

public class BlockPos extends Vec3i {

// Max distance from origin nether portals can generate, 128 blocks from max border
public static final int MAX_PORTAL_DISTANCE = 29_999_872;

public BlockPos(int x, int y, int z) {
super(x, y, z);
}
public BlockPos(Vec3i vec) {
super(vec.getX(), vec.getY(), vec.getZ());
}

public BlockPos overworldToNether() {
int netherY = Mth.clamp(y, 0, 127);
return new BlockPos(horizontal().floorDiv(8).withY(netherY));
}
public BlockPos netherToOverworld() {
int overworldX = Mth.clamp(x * 8, -MAX_PORTAL_DISTANCE, MAX_PORTAL_DISTANCE);
int overworldZ = Mth.clamp(z * 8, -MAX_PORTAL_DISTANCE, MAX_PORTAL_DISTANCE);
return new BlockPos(overworldX, y, overworldZ);
}

public SectionPos getSection() {
return new SectionPos(floorDiv(16));
}
public Vec3i getPosWithinSection() {
return floorMod(16);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.tisawesomeness.minecord.mc.pos;

public class SectionPos extends Vec3i {
public SectionPos(int x, int y, int z) {
super(x, y, z);
}
public SectionPos(Vec3i vec) {
super(vec.getX(), vec.getY(), vec.getZ());
}

public String getRegionFileName() {
Vec2i regionVec = horizontal().floorDiv(32);
return String.format("r.%d.%d.mca", regionVec.getX(), regionVec.getZ());
}
public Vec2i getPosWithinRegion() {
return horizontal().floorMod(32);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.tisawesomeness.minecord.mc.pos;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@AllArgsConstructor
@EqualsAndHashCode
public class Vec2i {
protected final int x;
protected final int z;

public Vec2i floorDiv(int n) {
return new Vec2i(Math.floorDiv(x, n), Math.floorDiv(z, n));
}
public Vec2i floorMod(int n) {
return new Vec2i(Math.floorMod(x, n), Math.floorMod(z, n));
}

public Vec3i withY(int y) {
return new Vec3i(x, y, z);
}

@Override
public String toString() {
return x + ", " + z;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.tisawesomeness.minecord.mc.pos;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@AllArgsConstructor
@EqualsAndHashCode
public class Vec3i {
protected final int x;
protected final int y;
protected final int z;

public Vec3i floorDiv(int n) {
return new Vec3i(Math.floorDiv(x, n), Math.floorDiv(y, n), Math.floorDiv(z, n));
}
public Vec3i floorMod(int n) {
return new Vec3i(Math.floorMod(x, n), Math.floorMod(y, n), Math.floorMod(z, n));
}

public Vec2i horizontal() {
return new Vec2i(x, z);
}

@Override
public String toString() {
return x + ", " + y + ", " + z;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.tisawesomeness.minecord.mc.pos;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class BlockPosTest {

@Test
public void testOverworldToNether() {
assertThat(new BlockPos(1872, 62, -5661).overworldToNether())
.isEqualTo(new BlockPos(234, 62, -708));
}
@Test
public void testOverworldToNetherZero() {
assertThat(new BlockPos(0, 0, 0).overworldToNether())
.isEqualTo(new BlockPos(0, 0, 0));
}
@Test
public void testOverworldToNetherLimit() {
assertThat(new BlockPos(0, -16, 0).overworldToNether())
.isEqualTo(new BlockPos(0, 0, 0));
}
@Test
public void testOverworldToNetherLimit2() {
assertThat(new BlockPos(0, 306, 0).overworldToNether())
.isEqualTo(new BlockPos(0, 127, 0));
}

@Test
public void testNetherToOverworld() {
assertThat(new BlockPos(1872, 62, -5661).netherToOverworld())
.isEqualTo(new BlockPos(14976, 62, -45288));
}
@Test
public void testNetherToOverworldZero() {
assertThat(new BlockPos(0, 0, 0).netherToOverworld())
.isEqualTo(new BlockPos(0, 0, 0));
}
@Test
public void testNetherToOverworldLimit() {
assertThat(new BlockPos(10_000_000, 64, -10_000_000).netherToOverworld())
.isEqualTo(new BlockPos(BlockPos.MAX_PORTAL_DISTANCE, 64, -BlockPos.MAX_PORTAL_DISTANCE));
}

@Test
public void testSection() {
assertThat(new BlockPos(-115, 60, -30).getSection())
.isEqualTo(new SectionPos(-8, 3, -2));
}
@Test
public void testSectionZero() {
assertThat(new BlockPos(0, 0, 0).getSection())
.isEqualTo(new SectionPos(0, 0, 0));
}

@Test
public void testWithinSection() {
assertThat(new BlockPos(-115, 60, -30).getPosWithinSection())
.isEqualTo(new Vec3i(13, 12, 2));
}
@Test
public void testWithinSectionZero() {
assertThat(new BlockPos(0, 0, 0).getPosWithinSection())
.isEqualTo(new Vec3i(0, 0, 0));
}

@Test
public void testToString() {
assertThat(new BlockPos(27, 95, -205))
.hasToString("27, 95, -205");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.tisawesomeness.minecord.mc.pos;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class SectionPosTest {

@Test
public void testRegion() {
assertThat(new SectionPos(1500, 0, -600).getRegionFileName())
.isEqualTo("r.46.-19.mca");
}
@Test
public void testRegionZero() {
assertThat(new SectionPos(0, 0, 0).getRegionFileName())
.isEqualTo("r.0.0.mca");
}

@Test
public void testWithinRegion() {
assertThat(new SectionPos(30, 0, -3).getPosWithinRegion())
.isEqualTo(new Vec2i(30, 29));
}
@Test
public void testWithinRegionZero() {
assertThat(new SectionPos(0, 0, 0).getPosWithinRegion())
.isEqualTo(new Vec2i(0, 0));
}

@Test
public void testToString() {
assertThat(new SectionPos(27, 95, -205))
.hasToString("27, 95, -205");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.tisawesomeness.minecord.mc.pos;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class Vec2iTest {

@Test
public void testNew() {
assertThat(new Vec2i(0, 0))
.extracting(Vec2i::getX, Vec2i::getZ)
.containsExactly(0, 0);
}

@Test
public void testDivide() {
assertThat(new Vec2i(-4, 11).floorDiv(4))
.isEqualTo(new Vec2i(-1, 2));
}
@Test
public void testModulo() {
assertThat(new Vec2i(-2, 15).floorMod(7))
.isEqualTo(new Vec2i(5, 1));
}

@Test
public void testToString() {
assertThat(new Vec2i(27, -205))
.hasToString("27, -205");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.tisawesomeness.minecord.mc.pos;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class Vec3iTest {

@Test
public void testNew() {
assertThat(new Vec3i(0, 0, 0))
.extracting(Vec3i::getX, Vec3i::getY, Vec3i::getZ)
.containsExactly(0, 0, 0);
}

@Test
public void testDivide() {
assertThat(new Vec3i(-4, 0, 11).floorDiv(4))
.isEqualTo(new Vec3i(-1, 0, 2));
}
@Test
public void testModulo() {
assertThat(new Vec3i(-2, 14, 15).floorMod(7))
.isEqualTo(new Vec3i(5, 0, 1));
}

@Test
public void testToString() {
assertThat(new Vec3i(27, 95, -205))
.hasToString("27, 95, -205");
}

}

0 comments on commit 302795f

Please sign in to comment.