From 3e89d9a8788ff69d388b7deab2dac259829d3484 Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Tue, 6 Sep 2022 15:41:17 +0200 Subject: [PATCH] Lag science - Trains now try to wait for chunks to load in when player passengers are present - Trains now sync clientside speed with the servers' tps to avoid overcorrection - Hoppers can now be picked up by the wrench --- src/generated/resources/.cache/cache | 2 +- .../create/tags/blocks/wrench_pickup.json | 3 +- .../java/com/simibubi/create/AllTags.java | 2 +- .../logistics/trains/entity/Carriage.java | 44 ++++++++++++++++--- .../trains/entity/CarriageSyncData.java | 15 ++++++- .../logistics/trains/entity/Train.java | 13 +++--- 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/generated/resources/.cache/cache b/src/generated/resources/.cache/cache index 57579baa9..bc6fa1798 100644 --- a/src/generated/resources/.cache/cache +++ b/src/generated/resources/.cache/cache @@ -5636,7 +5636,7 @@ d063e12c9ef75f39518c6d129ea35d833464d547 data/create/tags/blocks/toolboxes.json 50936b211d94167a35ec78c89954082a336b6269 data/create/tags/blocks/valve_handles.json eac71740fb12bdb38b5dfaa2268613d7ba82b809 data/create/tags/blocks/windmill_sails.json 74700d556ca80c7a1db5fd4efb09c3ddb26cad66 data/create/tags/blocks/windowable.json -893a01e6004d6d8272bd1658e98da88bb572ee57 data/create/tags/blocks/wrench_pickup.json +c71f8ee0103d0c1b0d1b0727f1ecdc4d1999f1f6 data/create/tags/blocks/wrench_pickup.json a8bdc387cfa6296ebcc4af14323e2ddb632234dc data/create/tags/fluids/bottomless/allow.json 74700d556ca80c7a1db5fd4efb09c3ddb26cad66 data/create/tags/fluids/bottomless/deny.json 74700d556ca80c7a1db5fd4efb09c3ddb26cad66 data/create/tags/items/blaze_burner_fuel/regular.json diff --git a/src/generated/resources/data/create/tags/blocks/wrench_pickup.json b/src/generated/resources/data/create/tags/blocks/wrench_pickup.json index c3da555b0..1e8c3abd1 100644 --- a/src/generated/resources/data/create/tags/blocks/wrench_pickup.json +++ b/src/generated/resources/data/create/tags/blocks/wrench_pickup.json @@ -16,6 +16,7 @@ "minecraft:tripwire", "minecraft:tripwire_hook", "minecraft:daylight_detector", - "minecraft:target" + "minecraft:target", + "minecraft:hopper" ] } \ No newline at end of file diff --git a/src/main/java/com/simibubi/create/AllTags.java b/src/main/java/com/simibubi/create/AllTags.java index f5c1153d9..a513e70d0 100644 --- a/src/main/java/com/simibubi/create/AllTags.java +++ b/src/main/java/com/simibubi/create/AllTags.java @@ -412,7 +412,7 @@ public class AllTags { AllBlockTags.WRENCH_PICKUP.includeAll(BlockTags.PRESSURE_PLATES); AllBlockTags.WRENCH_PICKUP.add(Blocks.REDSTONE_WIRE, Blocks.REDSTONE_TORCH, Blocks.REPEATER, Blocks.LEVER, Blocks.COMPARATOR, Blocks.OBSERVER, Blocks.REDSTONE_WALL_TORCH, Blocks.PISTON, Blocks.STICKY_PISTON, - Blocks.TRIPWIRE, Blocks.TRIPWIRE_HOOK, Blocks.DAYLIGHT_DETECTOR, Blocks.TARGET); + Blocks.TRIPWIRE, Blocks.TRIPWIRE_HOOK, Blocks.DAYLIGHT_DETECTOR, Blocks.TARGET, Blocks.HOPPER); AllBlockTags.ORE_OVERRIDE_STONE.includeAll(BlockTags.STONE_ORE_REPLACEABLES); diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/Carriage.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/Carriage.java index 43fbb12ee..6b4b71e51 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/Carriage.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/Carriage.java @@ -118,9 +118,14 @@ public class Carriage { return entities.get(dimension); } - public double travel(Level level, TrackGraph graph, double distance, - Function forwardControl, - Function backwardControl, int type) { + public double travel(Level level, TrackGraph graph, double distance, TravellingPoint toFollowForward, + TravellingPoint toFollowBackward, int type) { + + Function forwardControl = + toFollowForward == null ? train.navigation::control : mp -> mp.follow(toFollowForward); + Function backwardControl = + toFollowBackward == null ? train.navigation::control : mp -> mp.follow(toFollowBackward); + boolean onTwoBogeys = isOnTwoBogeys(); double stress = train.derailed ? 0 : onTwoBogeys ? bogeySpacing - getAnchorDiff() : 0; blocked = false; @@ -825,13 +830,38 @@ public class Carriage { double diffY = positionVec.y - coupledVec.y; double diffZ = positionVec.z - coupledVec.z; - if (!entity.level.isClientSide()) - entity.setServerSidePrevPosition(); - - entity.setPos(positionAnchor); entity.prevYaw = entity.yaw; entity.prevPitch = entity.pitch; + if (!entity.level.isClientSide()) { + Vec3 lookahead = positionAnchor.add(positionAnchor.subtract(entity.position()) + .normalize() + .scale(16)); + + for (Entity e : entity.getPassengers()) { + if (!(e instanceof Player)) + continue; + if (e.distanceToSqr(entity) > 32 * 32) + continue; + if (CarriageEntityHandler.isActiveChunk(entity.level, new BlockPos(lookahead))) + break; + train.carriageWaitingForChunks = id; + return; + } + + if (entity.getPassengers() + .stream() + .anyMatch(p -> p instanceof Player) + ) { + } + + if (train.carriageWaitingForChunks == id) + train.carriageWaitingForChunks = -1; + + entity.setServerSidePrevPosition(); + } + + entity.setPos(positionAnchor); entity.yaw = (float) (Mth.atan2(diffZ, diffX) * 180 / Math.PI) + 180; entity.pitch = (float) (Math.atan2(diffY, Math.sqrt(diffX * diffX + diffZ * diffZ)) * 180 / Math.PI) * -1; diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageSyncData.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageSyncData.java index 1cf7226cb..54ebac747 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageSyncData.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageSyncData.java @@ -21,6 +21,7 @@ import com.simibubi.create.content.logistics.trains.entity.TravellingPoint.ITrac import com.simibubi.create.foundation.utility.Couple; import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.Pair; +import com.simibubi.create.foundation.utility.ServerSpeedProvider; import com.simibubi.create.foundation.utility.VecHelper; import net.minecraft.network.FriendlyByteBuf; @@ -39,6 +40,7 @@ public class CarriageSyncData { private TravellingPoint[] pointsToApproach; private float[] pointDistanceSnapshot; private float destinationDistanceSnapshot; + private int ticksSince; public CarriageSyncData() { wheelLocations = new Vector<>(4); @@ -48,6 +50,7 @@ public class CarriageSyncData { fallbackPointSnapshot = null; destinationDistanceSnapshot = 0; leadingCarriage = false; + ticksSince = 0; for (int i = 0; i < 4; i++) { wheelLocations.add(null); pointsToApproach[i] = new TravellingPoint(); @@ -92,6 +95,7 @@ public class CarriageSyncData { public void read(FriendlyByteBuf buffer) { leadingCarriage = buffer.readBoolean(); boolean fallback = buffer.readBoolean(); + ticksSince = 0; if (fallback) { fallbackLocations = @@ -212,8 +216,17 @@ public class CarriageSyncData { destinationDistanceSnapshot = (float) (distanceToDestination - carriage.train.navigation.distanceToDestination); } - public void approach(CarriageContraptionEntity entity, Carriage carriage, float partial) { + public void approach(CarriageContraptionEntity entity, Carriage carriage, float partialIn) { DimensionalCarriageEntity dce = carriage.getDimensional(entity.level); + + int updateInterval = entity.getType() + .updateInterval(); + if (ticksSince >= updateInterval * 2) + partialIn /= ticksSince - updateInterval * 2 + 1; + partialIn *= ServerSpeedProvider.get(); + final float partial = partialIn; + + ticksSince++; if (fallbackLocations != null && fallbackPointSnapshot != null) { dce.positionAnchor = approachVector(partial, dce.positionAnchor, fallbackLocations.getFirst(), diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/Train.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/Train.java index 148c06159..a5a371013 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/Train.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/Train.java @@ -14,7 +14,6 @@ import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiConsumer; import java.util.function.Consumer; -import java.util.function.Function; import javax.annotation.Nullable; @@ -32,7 +31,6 @@ import com.simibubi.create.content.logistics.trains.TrackGraph; import com.simibubi.create.content.logistics.trains.TrackNode; import com.simibubi.create.content.logistics.trains.entity.Carriage.DimensionalCarriageEntity; import com.simibubi.create.content.logistics.trains.entity.TravellingPoint.IEdgePointListener; -import com.simibubi.create.content.logistics.trains.entity.TravellingPoint.ITrackSelector; import com.simibubi.create.content.logistics.trains.entity.TravellingPoint.SteerDirection; import com.simibubi.create.content.logistics.trains.management.edgePoint.EdgeData; import com.simibubi.create.content.logistics.trains.management.edgePoint.EdgePointType; @@ -82,6 +80,7 @@ public class Train { public double speed = 0; public double targetSpeed = 0; public Double speedBeforeStall = null; + public int carriageWaitingForChunks = -1; public double throttle = 1; public boolean honk = false; @@ -278,6 +277,9 @@ public class Train { int carriageCount = carriages.size(); boolean stalled = false; double maxStress = 0; + + if (carriageWaitingForChunks != -1) + distance = 0; for (int i = 0; i < carriageCount; i++) { Carriage carriage = carriages.get(i); @@ -366,18 +368,13 @@ public class Train { : carriages.get(i + 1) .getLeadingPoint(); - Function forwardControl = - toFollowForward == null ? navigation::control : mp -> mp.follow(toFollowForward); - Function backwardControl = - toFollowBackward == null ? navigation::control : mp -> mp.follow(toFollowBackward); - double totalStress = derailed ? 0 : leadingStress + trailingStress; boolean first = i == 0; boolean last = i == carriageCount - 1; int carriageType = first ? last ? Carriage.BOTH : Carriage.FIRST : last ? Carriage.LAST : Carriage.MIDDLE; double actualDistance = - carriage.travel(level, graph, distance + totalStress, forwardControl, backwardControl, carriageType); + carriage.travel(level, graph, distance + totalStress, toFollowForward, toFollowBackward, carriageType); blocked |= carriage.blocked; boolean onTwoBogeys = carriage.isOnTwoBogeys();