|
| 1 | +package net.Realism.mixin; |
| 2 | + |
| 3 | +import com.simibubi.create.content.trains.entity.Carriage; |
| 4 | +import com.simibubi.create.content.trains.entity.CarriageBogey; |
| 5 | +import com.simibubi.create.content.trains.entity.CarriageContraptionEntity; |
| 6 | +import com.simibubi.create.content.trains.entity.TravellingPoint; |
| 7 | +import com.simibubi.create.content.trains.graph.TrackEdge; |
| 8 | +import com.simibubi.create.content.trains.track.BezierConnection; |
| 9 | +import net.Realism.Interfaces.IOrientedContraptionEntity; |
| 10 | +import net.Realism.RNetworking; |
| 11 | +import net.Realism.config.RealismConfig; |
| 12 | +import net.Realism.network.RollSyncPacket; |
| 13 | +import net.createmod.catnip.data.Couple; |
| 14 | +import net.createmod.catnip.math.VecHelper; |
| 15 | +import net.minecraft.util.Mth; |
| 16 | +import net.minecraft.world.phys.Vec3; |
| 17 | +import org.spongepowered.asm.mixin.Final; |
| 18 | +import org.spongepowered.asm.mixin.Mixin; |
| 19 | +import org.spongepowered.asm.mixin.Shadow; |
| 20 | +import org.spongepowered.asm.mixin.Unique; |
| 21 | +import org.spongepowered.asm.mixin.injection.At; |
| 22 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 23 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 24 | + |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +/** |
| 28 | + * Mixin for Carriage.DimensionalCarriageEntity to calculate banking angles |
| 29 | + */ |
| 30 | +@Mixin(targets = "com.simibubi.create.content.trains.entity.Carriage$DimensionalCarriageEntity", remap = false) |
| 31 | +public class CarriageDimensionalEntityMixin { |
| 32 | + |
| 33 | + @Shadow |
| 34 | + @Final |
| 35 | + Carriage this$0; // The outer Carriage instance |
| 36 | + |
| 37 | + @Shadow |
| 38 | + public Couple<Vec3> rotationAnchors; |
| 39 | + |
| 40 | + /** |
| 41 | + * Inject banking calculation into alignEntity method |
| 42 | + */ |
| 43 | + @Inject(method = "alignEntity", at = @At("TAIL")) |
| 44 | + private void calculateAndApplyBanking(CarriageContraptionEntity entity, CallbackInfo ci) { |
| 45 | + // Check if banking is enabled |
| 46 | + if (!RealismConfig.CLIENT.enableBanking.get()) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Ensure we have valid rotation anchors |
| 51 | + if (rotationAnchors.either(Objects::isNull)) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // Update previous roll |
| 56 | + IOrientedContraptionEntity orientedEntity = (IOrientedContraptionEntity) entity; |
| 57 | + orientedEntity.realism$setPrevRoll(orientedEntity.realism$getRoll()); |
| 58 | + |
| 59 | + // Calculate banking |
| 60 | + float banking = realism$calculateBanking(entity); |
| 61 | + orientedEntity.realism$setRoll(banking); |
| 62 | + RNetworking.sendToAll(new RollSyncPacket(orientedEntity.realism$getRoll(),orientedEntity.realism$getPrevRoll(),orientedEntity.getuid())); |
| 63 | + |
| 64 | + // For first position update, ensure prevRoll matches roll |
| 65 | + if (entity.firstPositionUpdate) { |
| 66 | + orientedEntity.realism$setPrevRoll(banking); |
| 67 | + RNetworking.sendToAll(new RollSyncPacket(orientedEntity.realism$getRoll(),orientedEntity.realism$getPrevRoll(),orientedEntity.getuid())); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Calculate banking angle based on track curvature and train speed |
| 73 | + */ |
| 74 | + @Unique |
| 75 | + private float realism$calculateBanking(CarriageContraptionEntity entity) { |
| 76 | + // Get the leading bogey |
| 77 | + CarriageBogey leadingBogey = this$0.leadingBogey(); |
| 78 | + if (leadingBogey == null) { |
| 79 | + return 0f; |
| 80 | + } |
| 81 | + |
| 82 | + // Get the leading travelling point |
| 83 | + TravellingPoint leadingPoint = leadingBogey.leading(); |
| 84 | + if (leadingPoint == null || leadingPoint.edge == null) { |
| 85 | + return 0f; |
| 86 | + } |
| 87 | + |
| 88 | + TrackEdge edge = leadingPoint.edge; |
| 89 | + |
| 90 | + // Only apply banking on curves (BezierConnections) |
| 91 | + if (!edge.isTurn()) { |
| 92 | + return(0f); |
| 93 | + } |
| 94 | + |
| 95 | + BezierConnection turn = edge.getTurn(); |
| 96 | + if (turn == null || turn.normals == null) { |
| 97 | + return 0f; |
| 98 | + } |
| 99 | + |
| 100 | + // Calculate normalized position along the curve (0.0 to 1.0) |
| 101 | + double edgeLength = edge.getLength(); |
| 102 | + double t = edgeLength == 0 ? 0.5 : leadingPoint.position / edgeLength; |
| 103 | + t = Mth.clamp(t, 0.0, 1.0); |
| 104 | + |
| 105 | + // Get interpolated track normal at current position |
| 106 | + Vec3 trackNormal = realism$getTrackNormalAt(turn, t); |
| 107 | + if (trackNormal == null) { |
| 108 | + return 0f; |
| 109 | + } |
| 110 | + |
| 111 | + // Get train's forward direction |
| 112 | + Vec3 positionVec = rotationAnchors.getFirst(); |
| 113 | + Vec3 coupledVec = rotationAnchors.getSecond(); |
| 114 | + |
| 115 | + double diffX = positionVec.x - coupledVec.x; |
| 116 | + double diffY = positionVec.y - coupledVec.y; |
| 117 | + double diffZ = positionVec.z - coupledVec.z; |
| 118 | + |
| 119 | + Vec3 forward = new Vec3(diffX, diffY, diffZ).normalize(); |
| 120 | + Vec3 worldUp = new Vec3(0, 1, 0); |
| 121 | + Vec3 right = forward.cross(worldUp).normalize(); |
| 122 | + |
| 123 | + // If right vector is too small, we're going straight up/down - no banking |
| 124 | + if (right.lengthSqr() < 0.001) { |
| 125 | + return 0f; |
| 126 | + } |
| 127 | + |
| 128 | + // Project track normal onto right vector to get banking |
| 129 | + double bankingDot = trackNormal.dot(right); |
| 130 | + bankingDot = Mth.clamp(bankingDot, -1.0, 1.0); |
| 131 | + float bankingAngle = (float) Math.toDegrees(Math.asin(bankingDot)); |
| 132 | + |
| 133 | + // Apply intensity multiplier |
| 134 | + float intensity = RealismConfig.CLIENT.bankingIntensity.get().floatValue(); |
| 135 | + bankingAngle *= intensity; |
| 136 | + |
| 137 | + // Apply speed-based scaling |
| 138 | + double speed = Math.abs(this$0.train.speed); |
| 139 | + float minSpeed = RealismConfig.CLIENT.bankingMinSpeed.get().floatValue(); |
| 140 | + float speedFactor = minSpeed > 0 ? (float) Mth.clamp(speed / minSpeed, 0, 1) : 1f; |
| 141 | + |
| 142 | + float targetBanking = bankingAngle * speedFactor; |
| 143 | + |
| 144 | + // Apply smoothing to prevent jarring transitions |
| 145 | + float currentRoll = ((IOrientedContraptionEntity) entity).realism$getRoll(); |
| 146 | + float smoothness = RealismConfig.CLIENT.bankingSmoothness.get().floatValue(); |
| 147 | + |
| 148 | + // Clamp to maximum banking angle |
| 149 | + float maxAngle = RealismConfig.CLIENT.maxBankingAngle.get().floatValue(); |
| 150 | + return Mth.clamp(targetBanking, -maxAngle, maxAngle); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Interpolate track normal at position t along the curve |
| 155 | + */ |
| 156 | + @Unique |
| 157 | + private Vec3 realism$getTrackNormalAt(BezierConnection turn, double t) { |
| 158 | + if (turn.normals == null) { |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + Vec3 normal1 = turn.axes.getFirst(); |
| 163 | + Vec3 normal2 = turn.axes.getSecond(); |
| 164 | + |
| 165 | + // Linear interpolation between start and end normals |
| 166 | + Vec3 interpolated = VecHelper.lerp((float) t, normal1, normal2); |
| 167 | + |
| 168 | + // Normalize to ensure it's a unit vector |
| 169 | + double length = interpolated.length(); |
| 170 | + if (length < 0.001) { |
| 171 | + return new Vec3(0, 1, 0); // Default to up |
| 172 | + } |
| 173 | + |
| 174 | + return interpolated.normalize(); |
| 175 | + } |
| 176 | +} |
| 177 | + |
0 commit comments