2022-04-13 03:06:35 +02:00
|
|
|
package com.simibubi.create.content.logistics.trains.track;
|
|
|
|
|
2022-04-14 02:23:55 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
2022-04-13 03:06:35 +02:00
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
import com.jozufozu.flywheel.util.transform.TransformStack;
|
|
|
|
import com.mojang.blaze3d.vertex.PoseStack;
|
|
|
|
import com.mojang.blaze3d.vertex.VertexConsumer;
|
|
|
|
import com.simibubi.create.AllShapes;
|
2023-05-09 18:23:47 +02:00
|
|
|
import com.simibubi.create.AllTags;
|
2022-04-14 02:23:55 +02:00
|
|
|
import com.simibubi.create.content.logistics.trains.BezierConnection;
|
2022-04-13 03:06:35 +02:00
|
|
|
import com.simibubi.create.foundation.utility.AngleHelper;
|
2022-04-14 02:23:55 +02:00
|
|
|
import com.simibubi.create.foundation.utility.AnimationTickHolder;
|
2022-05-15 21:19:02 +02:00
|
|
|
import com.simibubi.create.foundation.utility.Iterate;
|
2022-04-14 02:23:55 +02:00
|
|
|
import com.simibubi.create.foundation.utility.RaycastHelper;
|
|
|
|
import com.simibubi.create.foundation.utility.VecHelper;
|
|
|
|
import com.simibubi.create.foundation.utility.WorldAttached;
|
2022-04-13 03:06:35 +02:00
|
|
|
|
|
|
|
import net.minecraft.client.Minecraft;
|
2022-04-14 02:23:55 +02:00
|
|
|
import net.minecraft.client.player.LocalPlayer;
|
|
|
|
import net.minecraft.client.renderer.MultiBufferSource;
|
2022-04-13 03:06:35 +02:00
|
|
|
import net.minecraft.client.renderer.RenderType;
|
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.Direction;
|
2022-04-14 02:23:55 +02:00
|
|
|
import net.minecraft.core.Direction.Axis;
|
2022-04-13 03:06:35 +02:00
|
|
|
import net.minecraft.util.Mth;
|
2022-04-14 02:23:55 +02:00
|
|
|
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
|
2022-07-08 02:57:09 +02:00
|
|
|
import net.minecraft.world.level.GameType;
|
2022-04-13 03:06:35 +02:00
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2022-04-14 02:23:55 +02:00
|
|
|
import net.minecraft.world.phys.AABB;
|
2022-04-13 03:06:35 +02:00
|
|
|
import net.minecraft.world.phys.BlockHitResult;
|
2022-04-14 02:23:55 +02:00
|
|
|
import net.minecraft.world.phys.HitResult.Type;
|
2022-04-13 03:06:35 +02:00
|
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
import net.minecraft.world.phys.shapes.Shapes;
|
|
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.client.event.DrawSelectionEvent;
|
2022-04-14 02:23:55 +02:00
|
|
|
import net.minecraftforge.common.ForgeMod;
|
2022-04-13 03:06:35 +02:00
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
|
|
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
|
|
|
|
|
|
|
|
@EventBusSubscriber(Dist.CLIENT)
|
|
|
|
public class TrackBlockOutline {
|
|
|
|
|
2022-04-14 02:23:55 +02:00
|
|
|
public static WorldAttached<Map<BlockPos, TrackTileEntity>> TRACKS_WITH_TURNS =
|
|
|
|
new WorldAttached<>(w -> new HashMap<>());
|
|
|
|
|
2022-04-15 00:12:40 +02:00
|
|
|
public static BezierPointSelection result;
|
2022-04-14 02:23:55 +02:00
|
|
|
|
|
|
|
public static void pickCurves() {
|
|
|
|
Minecraft mc = Minecraft.getInstance();
|
|
|
|
if (!(mc.cameraEntity instanceof LocalPlayer player))
|
|
|
|
return;
|
|
|
|
if (mc.level == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Vec3 origin = player.getEyePosition(AnimationTickHolder.getPartialTicks(mc.level));
|
|
|
|
|
|
|
|
double maxRange = mc.hitResult == null ? Double.MAX_VALUE
|
|
|
|
: mc.hitResult.getLocation()
|
|
|
|
.distanceToSqr(origin);
|
|
|
|
|
|
|
|
result = null;
|
|
|
|
|
|
|
|
AttributeInstance range = player.getAttribute(ForgeMod.REACH_DISTANCE.get());
|
|
|
|
Vec3 target = RaycastHelper.getTraceTarget(player, Math.min(maxRange, range.getValue()) + 1, origin);
|
|
|
|
Map<BlockPos, TrackTileEntity> turns = TRACKS_WITH_TURNS.get(mc.level);
|
|
|
|
|
|
|
|
for (TrackTileEntity te : turns.values()) {
|
|
|
|
for (BezierConnection bc : te.connections.values()) {
|
|
|
|
if (!bc.isPrimary())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
AABB bounds = bc.getBounds();
|
|
|
|
if (!bounds.contains(origin) && bounds.clip(origin, target)
|
|
|
|
.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
float[] stepLUT = bc.getStepLUT();
|
|
|
|
int segments = (int) (bc.getLength() * 2);
|
|
|
|
AABB segmentBounds = AllShapes.TRACK_ORTHO.get(Direction.SOUTH)
|
|
|
|
.bounds();
|
|
|
|
segmentBounds = segmentBounds.move(-.5, segmentBounds.getYsize() / -2, -.5);
|
|
|
|
|
|
|
|
int bestSegment = -1;
|
|
|
|
double bestDistance = Double.MAX_VALUE;
|
|
|
|
double newMaxRange = maxRange;
|
|
|
|
|
|
|
|
for (int i = 0; i < stepLUT.length - 2; i++) {
|
|
|
|
float t = stepLUT[i] * i / segments;
|
|
|
|
float t1 = stepLUT[i + 1] * (i + 1) / segments;
|
|
|
|
float t2 = stepLUT[i + 2] * (i + 2) / segments;
|
|
|
|
|
|
|
|
Vec3 v1 = bc.getPosition(t);
|
|
|
|
Vec3 v2 = bc.getPosition(t2);
|
|
|
|
Vec3 diff = v2.subtract(v1);
|
|
|
|
Vec3 angles = TrackRenderer.getModelAngles(bc.getNormal(t1), diff);
|
|
|
|
|
|
|
|
Vec3 anchor = v1.add(diff.scale(.5));
|
|
|
|
Vec3 localOrigin = origin.subtract(anchor);
|
|
|
|
Vec3 localDirection = target.subtract(origin);
|
|
|
|
localOrigin = VecHelper.rotate(localOrigin, AngleHelper.deg(-angles.x), Axis.X);
|
|
|
|
localOrigin = VecHelper.rotate(localOrigin, AngleHelper.deg(-angles.y), Axis.Y);
|
|
|
|
localDirection = VecHelper.rotate(localDirection, AngleHelper.deg(-angles.x), Axis.X);
|
|
|
|
localDirection = VecHelper.rotate(localDirection, AngleHelper.deg(-angles.y), Axis.Y);
|
|
|
|
|
|
|
|
Optional<Vec3> clip = segmentBounds.clip(localOrigin, localOrigin.add(localDirection));
|
|
|
|
if (clip.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (bestSegment != -1 && bestDistance < clip.get()
|
|
|
|
.distanceToSqr(0, 0.25f, 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
double distanceToSqr = clip.get()
|
|
|
|
.distanceToSqr(localOrigin);
|
|
|
|
if (distanceToSqr > maxRange)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bestSegment = i;
|
|
|
|
newMaxRange = distanceToSqr;
|
|
|
|
bestDistance = clip.get()
|
|
|
|
.distanceToSqr(0, 0.25f, 0);
|
|
|
|
|
|
|
|
BezierTrackPointLocation location = new BezierTrackPointLocation(bc.getKey(), i);
|
|
|
|
result = new BezierPointSelection(te, location, anchor, angles, diff.normalize());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bestSegment != -1)
|
|
|
|
maxRange = newMaxRange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mc.hitResult != null && mc.hitResult.getType() != Type.MISS) {
|
|
|
|
Vec3 priorLoc = mc.hitResult.getLocation();
|
|
|
|
mc.hitResult = BlockHitResult.miss(priorLoc, Direction.UP, new BlockPos(priorLoc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 13:05:16 +02:00
|
|
|
public static void drawCurveSelection(PoseStack ms, MultiBufferSource buffer, Vec3 camera) {
|
2022-07-08 02:57:09 +02:00
|
|
|
Minecraft mc = Minecraft.getInstance();
|
|
|
|
if (mc.options.hideGui || mc.gameMode.getPlayerMode() == GameType.SPECTATOR)
|
|
|
|
return;
|
|
|
|
|
2022-04-14 02:23:55 +02:00
|
|
|
BezierPointSelection result = TrackBlockOutline.result;
|
|
|
|
if (result == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VertexConsumer vb = buffer.getBuffer(RenderType.lines());
|
2023-05-08 13:05:16 +02:00
|
|
|
Vec3 vec = result.vec()
|
|
|
|
.subtract(camera);
|
2022-04-14 02:23:55 +02:00
|
|
|
Vec3 angles = result.angles();
|
|
|
|
TransformStack.cast(ms)
|
|
|
|
.pushPose()
|
|
|
|
.translate(vec.x, vec.y + .125f, vec.z)
|
|
|
|
.rotateYRadians(angles.y)
|
|
|
|
.rotateXRadians(angles.x)
|
|
|
|
.translate(-.5, -.125f, -.5);
|
|
|
|
|
2023-05-09 18:23:47 +02:00
|
|
|
boolean holdingTrack = AllTags.AllBlockTags.TRACKS.matches(Minecraft.getInstance().player.getMainHandItem());
|
2022-04-14 02:23:55 +02:00
|
|
|
renderShape(AllShapes.TRACK_ORTHO.get(Direction.SOUTH), ms, vb, holdingTrack ? false : null);
|
|
|
|
ms.popPose();
|
|
|
|
}
|
|
|
|
|
2022-04-13 03:06:35 +02:00
|
|
|
@SubscribeEvent
|
|
|
|
public static void drawCustomBlockSelection(DrawSelectionEvent.HighlightBlock event) {
|
|
|
|
Minecraft mc = Minecraft.getInstance();
|
|
|
|
BlockHitResult target = event.getTarget();
|
|
|
|
BlockPos pos = target.getBlockPos();
|
|
|
|
BlockState blockstate = mc.level.getBlockState(pos);
|
|
|
|
|
|
|
|
if (!(blockstate.getBlock() instanceof TrackBlock))
|
|
|
|
return;
|
|
|
|
if (!mc.level.getWorldBorder()
|
|
|
|
.isWithinBounds(pos))
|
|
|
|
return;
|
|
|
|
|
|
|
|
VertexConsumer vb = event.getMultiBufferSource()
|
|
|
|
.getBuffer(RenderType.lines());
|
|
|
|
Vec3 camPos = event.getCamera()
|
|
|
|
.getPosition();
|
|
|
|
|
2022-04-14 02:23:55 +02:00
|
|
|
PoseStack ms = event.getPoseStack();
|
|
|
|
|
|
|
|
ms.pushPose();
|
2022-04-13 03:06:35 +02:00
|
|
|
ms.translate(pos.getX() - camPos.x, pos.getY() - camPos.y, pos.getZ() - camPos.z);
|
|
|
|
|
2023-05-09 18:23:47 +02:00
|
|
|
boolean holdingTrack = AllTags.AllBlockTags.TRACKS.matches(Minecraft.getInstance().player.getMainHandItem());
|
2022-04-14 02:23:55 +02:00
|
|
|
TrackShape shape = blockstate.getValue(TrackBlock.SHAPE);
|
|
|
|
boolean isJunction = shape.isJunction();
|
|
|
|
walkShapes(shape, TransformStack.cast(ms), s -> {
|
|
|
|
renderShape(s, ms, vb, holdingTrack ? !isJunction : null);
|
2022-04-13 03:06:35 +02:00
|
|
|
event.setCanceled(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
ms.popPose();
|
2022-04-14 02:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void renderShape(VoxelShape s, PoseStack ms, VertexConsumer vb, Boolean valid) {
|
|
|
|
PoseStack.Pose transform = ms.last();
|
|
|
|
s.forAllEdges((x1, y1, z1, x2, y2, z2) -> {
|
|
|
|
float xDiff = (float) (x2 - x1);
|
|
|
|
float yDiff = (float) (y2 - y1);
|
|
|
|
float zDiff = (float) (z2 - z1);
|
|
|
|
float length = Mth.sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff);
|
|
|
|
|
|
|
|
xDiff /= length;
|
|
|
|
yDiff /= length;
|
|
|
|
zDiff /= length;
|
|
|
|
|
|
|
|
float r = 0f;
|
|
|
|
float g = 0f;
|
|
|
|
float b = 0f;
|
2022-04-13 03:06:35 +02:00
|
|
|
|
2022-04-14 02:23:55 +02:00
|
|
|
if (valid != null && valid) {
|
|
|
|
g = 1f;
|
|
|
|
b = 1f;
|
|
|
|
r = 1f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid != null && !valid) {
|
|
|
|
r = 1f;
|
|
|
|
b = 0.125f;
|
|
|
|
g = 0.25f;
|
|
|
|
}
|
|
|
|
|
|
|
|
vb.vertex(transform.pose(), (float) x1, (float) y1, (float) z1)
|
|
|
|
.color(r, g, b, .4f)
|
|
|
|
.normal(transform.normal(), xDiff, yDiff, zDiff)
|
|
|
|
.endVertex();
|
|
|
|
vb.vertex(transform.pose(), (float) x2, (float) y2, (float) z2)
|
|
|
|
.color(r, g, b, .4f)
|
|
|
|
.normal(transform.normal(), xDiff, yDiff, zDiff)
|
|
|
|
.endVertex();
|
|
|
|
|
|
|
|
});
|
2022-04-13 03:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static final VoxelShape LONG_CROSS =
|
|
|
|
Shapes.or(TrackVoxelShapes.longOrthogonalZ(), TrackVoxelShapes.longOrthogonalX());
|
|
|
|
private static final VoxelShape LONG_ORTHO = TrackVoxelShapes.longOrthogonalZ();
|
2022-05-15 21:19:02 +02:00
|
|
|
private static final VoxelShape LONG_ORTHO_OFFSET = TrackVoxelShapes.longOrthogonalZOffset();
|
2022-04-13 03:06:35 +02:00
|
|
|
|
|
|
|
private static void walkShapes(TrackShape shape, TransformStack msr, Consumer<VoxelShape> renderer) {
|
|
|
|
float angle45 = Mth.PI / 4;
|
|
|
|
|
|
|
|
if (shape == TrackShape.XO || shape == TrackShape.CR_NDX || shape == TrackShape.CR_PDX)
|
|
|
|
renderer.accept(AllShapes.TRACK_ORTHO.get(Direction.EAST));
|
|
|
|
else if (shape == TrackShape.ZO || shape == TrackShape.CR_NDZ || shape == TrackShape.CR_PDZ)
|
|
|
|
renderer.accept(AllShapes.TRACK_ORTHO.get(Direction.SOUTH));
|
|
|
|
|
2022-05-15 21:19:02 +02:00
|
|
|
if (shape.isPortal()) {
|
|
|
|
for (Direction d : Iterate.horizontalDirections) {
|
|
|
|
if (TrackShape.asPortal(d) != shape)
|
|
|
|
continue;
|
|
|
|
msr.rotateCentered(Direction.UP, AngleHelper.rad(AngleHelper.horizontalAngle(d)));
|
|
|
|
renderer.accept(LONG_ORTHO_OFFSET);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 03:06:35 +02:00
|
|
|
if (shape == TrackShape.PD || shape == TrackShape.CR_PDX || shape == TrackShape.CR_PDZ) {
|
|
|
|
msr.rotateCentered(Direction.UP, angle45);
|
|
|
|
renderer.accept(LONG_ORTHO);
|
|
|
|
} else if (shape == TrackShape.ND || shape == TrackShape.CR_NDX || shape == TrackShape.CR_NDZ) {
|
|
|
|
msr.rotateCentered(Direction.UP, -Mth.PI / 4);
|
|
|
|
renderer.accept(LONG_ORTHO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shape == TrackShape.CR_O)
|
|
|
|
renderer.accept(AllShapes.TRACK_CROSS);
|
|
|
|
else if (shape == TrackShape.CR_D) {
|
|
|
|
msr.rotateCentered(Direction.UP, angle45);
|
|
|
|
renderer.accept(LONG_CROSS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(shape == TrackShape.AE || shape == TrackShape.AN || shape == TrackShape.AW || shape == TrackShape.AS))
|
|
|
|
return;
|
|
|
|
|
|
|
|
msr.translate(0, 1, 0);
|
|
|
|
msr.rotateCentered(Direction.UP, Mth.PI - AngleHelper.rad(shape.getModelRotation()));
|
|
|
|
msr.rotateXRadians(angle45);
|
|
|
|
msr.translate(0, -3 / 16f, 1 / 16f);
|
|
|
|
renderer.accept(LONG_ORTHO);
|
|
|
|
}
|
|
|
|
|
2022-04-14 02:23:55 +02:00
|
|
|
public static record BezierPointSelection(TrackTileEntity te, BezierTrackPointLocation loc, Vec3 vec, Vec3 angles,
|
|
|
|
Vec3 direction) {
|
|
|
|
}
|
|
|
|
|
2022-04-13 03:06:35 +02:00
|
|
|
}
|