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

Commit

Permalink
Create AABBHelper, fix room spawn saving+lookup, add a RandSrc helper
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Feb 23, 2024
1 parent 8992925 commit 7e0f6f8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dev.compactmods.machines.api.util;

import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;

public abstract class AABBHelper {

public static Vec3 minCorner(AABB aabb) {
return new Vec3(aabb.minX, aabb.minY, aabb.minZ);
}

public static Vec3 maxCorner(AABB aabb) {
return new Vec3(aabb.maxX, aabb.maxY, aabb.maxZ);
}

public static AABB normalize(AABB source) {
Vec3 offset = minCorner(source).reverse();
return source.move(offset);
}

public static AABB normalizeWithin(AABB source, AABB within) {
Vec3 offset = minCorner(source).subtract(minCorner(within)).reverse();
return source.move(offset);
}

public static AABB alignFloor(AABB source, AABB within) {
double targetY = within.minY;
return alignFloor(source, targetY);
}

public static AABB alignFloor(AABB source, double targetY) {
double offset = source.minY - targetY;
return source.move(0, offset * -1, 0);
}

public static String toString(AABB aabb) {
return "%s,%s,%s".formatted(aabb.getXsize(), aabb.getYsize(), aabb.getZsize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

public class BlockSpaceUtil {

public static String aabbToString(AABB aabb) {
return "%s,%s,%s".formatted(aabb.getXsize(), aabb.getYsize(), aabb.getZsize());
}

public static Stream<BlockPos> blocksInside(AABB bounds) {
return BlockPos.betweenClosedStream(bounds.contract(1, 1, 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.compactmods.machines.api.Constants;
import dev.compactmods.machines.api.dimension.CompactDimension;
import dev.compactmods.machines.api.dimension.MissingDimensionException;
import dev.compactmods.machines.api.util.AABBHelper;
import dev.compactmods.machines.data.CodecBackedSavedData;
import dev.compactmods.machines.room.graph.node.RoomRegistrationNode;
import dev.compactmods.machines.util.MathUtil;
Expand Down Expand Up @@ -69,7 +70,7 @@ public AABB getNextBoundaries(RoomTemplate template) {
final var region = MathUtil.getRegionPositionByIndex(registrationNodes.size());
final var floor = MathUtil.getCenterWithY(region, 0);

return template.getZeroBoundaries().move(floor);
return AABBHelper.alignFloor(template.getZeroBoundaries().move(floor), 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ public Optional<String> findRoomByChunk(ChunkPos chunk) {
if (!chunks.containsKey(chunk)) return Optional.empty();
final var chunkNode = chunks.get(chunk);

return graph.adjNodeStream(GraphNodes.LOOKUP_ROOM_REGISTRATION, chunkNode)
.map(RoomRegistrationNode::code)
return graph.inboundEdges(chunkNode, RoomReferenceNode.class)
.map(GraphEdge::source)
.map(WeakReference::get)
.filter(Objects::nonNull)
.map(RoomReferenceNode::code)
.findFirst();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SpawnManager extends CodecBackedSavedData<SpawnManager> implements

private final Logger LOGS = LogManager.getLogger();

private static final UnboundedMapCodec<UUID, RoomSpawn> PLAYER_SPAWNS_CODEC = Codec.unboundedMap(UUIDUtil.CODEC, RoomSpawn.CODEC);
private static final UnboundedMapCodec<UUID, RoomSpawn> PLAYER_SPAWNS_CODEC = Codec.unboundedMap(UUIDUtil.STRING_CODEC, RoomSpawn.CODEC);
private static final Codec<SpawnManager> CODEC = RecordCodecBuilder.create(inst -> inst.group(
Codec.STRING.fieldOf("roomCode").forGetter(x -> x.roomCode),
PLAYER_SPAWNS_CODEC.fieldOf("player_spawns").forGetter(x -> x.playerSpawns),
Expand All @@ -50,36 +50,38 @@ public SpawnManager(String roomCode) {
this.defaultSpawn = null;
}

public SpawnManager(String roomCode, IRoomBoundaries roomBounds) {
this(roomCode, Collections.emptyMap(), new RoomSpawn(roomBounds.defaultSpawn(), Vec2.ZERO));
this.roomBounds = roomBounds.innerBounds();
}

public SpawnManager(String roomCode, Map<UUID, RoomSpawn> playerSpawns, RoomSpawn defaultSpawn) {
super(CODEC, () -> new SpawnManager(roomCode));
this.roomCode = roomCode;
this.playerSpawns = new HashMap<>(playerSpawns);
this.defaultSpawn = defaultSpawn;
}


public static SpawnManager forRoom(MinecraftServer server, String roomCode, IRoomBoundaries roomBounds) throws MissingDimensionException {
String roomFilename = Constants.MOD_ID + "_room_" + roomCode;
var manager = CompactDimension.forServer(server)
.getDataStorage()
.computeIfAbsent(new CodecWrappedSavedData<>(CODEC, () -> new SpawnManager(roomCode)).sd(), roomFilename);
.computeIfAbsent(new CodecWrappedSavedData<>(CODEC, () -> new SpawnManager(roomCode, roomBounds)).sd(), roomFilename);

manager.setBoundaries(roomBounds.innerBounds());
manager.setDefaultSpawn(roomBounds.defaultSpawn(), Vec2.ZERO);
return manager;
}

private void setBoundaries(AABB roomBounds) {
this.roomBounds = roomBounds;
}

@Override
public void resetPlayerSpawn(UUID player) {
playerSpawns.remove(player);
this.setDirty();
}

@Override
public void setDefaultSpawn(Vec3 position, Vec2 rotation) {
defaultSpawn = new RoomSpawn(position, rotation);
this.setDirty();
}

@Override
Expand All @@ -98,6 +100,8 @@ public void setPlayerSpawn(UUID player, Vec3 location, Vec2 rotation) {
playerSpawns.replace(player, new RoomSpawn(location, rotation));
else
playerSpawns.put(player, new RoomSpawn(location, rotation));

this.setDirty();
}

private record RoomSpawns(RoomSpawn defaultSpawn, Map<UUID, RoomSpawn> playerSpawnsSnapshot) implements IRoomSpawns {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.compactmods.machines.util;

import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.joml.Math;

import java.util.stream.Stream;

public abstract class RandomSourceUtil {

public static Stream<Vec3> randomVec3Stream(RandomSource source) {
return Stream.generate(() -> randomVec3(source));
}

@NotNull
public static Vec3 randomVec3(RandomSource source) {
return new Vec3(source.nextDouble(), source.nextDouble(), source.nextDouble());
}
}

0 comments on commit 7e0f6f8

Please sign in to comment.