From 760bffe343ea84e5775c6c89a3dc59a645a139e1 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 23 Dec 2021 15:32:29 -0800 Subject: [PATCH] Start work on solver --- .../content/contraptions/KineticSolver.java | 78 +++++++++++++++++++ .../relays/elementary/ShaftBlock.java | 20 ++++- 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/KineticSolver.java diff --git a/src/main/java/com/simibubi/create/content/contraptions/KineticSolver.java b/src/main/java/com/simibubi/create/content/contraptions/KineticSolver.java new file mode 100644 index 000000000..e6fb470d1 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/KineticSolver.java @@ -0,0 +1,78 @@ +package com.simibubi.create.content.contraptions; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.world.level.Level; + +public class KineticSolver { + + private final Map connectionsFrom = new HashMap<>(); + private final Map connectionsTo = new HashMap<>(); + + private final List goals = new ArrayList<>(); + + private boolean needsUpdate; + + public void solve() { + if (!needsUpdate) return; + needsUpdate = false; + + + } + + public void addFact(BlockPos pos) { + + } + + public void addGoal(Goal goal) { + goals.add(goal); + needsUpdate = true; + } + + public interface SolverBlock { + void created(KineticSolver solver, Level level, BlockPos pos); + } + + public static abstract class Connection { + public final BlockPos from; + public final BlockPos to; + + public Connection(BlockPos from, BlockPos to) { + this.from = from; + this.to = to; + } + + public abstract boolean isCompatible(Connection that); + + public static final class Shaft extends Connection { + + public Shaft(BlockPos pos, Direction face) { + super(pos, pos.relative(face)); + } + + @Override + public boolean isCompatible(Connection that) { + return that instanceof Shaft; + } + } + } + + public static class Goal { + public final Connection connection; + + public Goal(Connection connection) { + this.connection = connection; + } + + public static final class EqualSpeed extends Goal { + public EqualSpeed(Connection connection) { + super(connection); + } + } + } +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/ShaftBlock.java b/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/ShaftBlock.java index ac1d647e8..ab3601d06 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/ShaftBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/ShaftBlock.java @@ -4,6 +4,7 @@ import java.util.function.Predicate; import com.simibubi.create.AllBlocks; import com.simibubi.create.AllShapes; +import com.simibubi.create.content.contraptions.KineticSolver; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.relays.encased.EncasedShaftBlock; import com.simibubi.create.foundation.advancement.AllTriggers; @@ -26,7 +27,7 @@ import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; -public class ShaftBlock extends AbstractShaftBlock { +public class ShaftBlock extends AbstractShaftBlock implements KineticSolver.SolverBlock { private static final int placementHelperId = PlacementHelpers.register(new PlacementHelper()); @@ -69,7 +70,7 @@ public class ShaftBlock extends AbstractShaftBlock { if (world.isClientSide) return InteractionResult.SUCCESS; - + AllTriggers.triggerFor(AllTriggers.CASING_SHAFT, player); KineticTileEntity.switchToBlockState(world, pos, encasedShaft.defaultBlockState() .setValue(AXIS, state.getValue(AXIS))); @@ -83,6 +84,21 @@ public class ShaftBlock extends AbstractShaftBlock { return InteractionResult.PASS; } + @Override + public void created(KineticSolver solver, Level level, BlockPos pos) { + BlockState state = level.getBlockState(pos); + + Direction.Axis axis = state.getValue(AXIS); + + Direction positive = Direction.fromAxisAndDirection(axis, Direction.AxisDirection.POSITIVE); + + KineticSolver.Connection.Shaft c1 = new KineticSolver.Connection.Shaft(pos, positive); + KineticSolver.Connection.Shaft c2 = new KineticSolver.Connection.Shaft(pos, positive.getOpposite()); + + solver.addGoal(new KineticSolver.Goal.EqualSpeed(c1)); + solver.addGoal(new KineticSolver.Goal.EqualSpeed(c2)); + } + @MethodsReturnNonnullByDefault private static class PlacementHelper extends PoleHelper { //used for extending a shaft in its axis, like the piston poles. works with shafts and cogs