Skip to content

Commit 84fc4db

Browse files
committed
fix: when doDaylightCycle false, account for sign flip in timeOfDay
1 parent b263f43 commit 84fc4db

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed
Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,59 @@
11
package dev.hugeblank.asahi.client;
22

3+
import net.fabricmc.loader.api.FabricLoader;
4+
import org.spongepowered.asm.mixin.Unique;
5+
36
import java.util.function.Consumer;
47
import java.util.function.Supplier;
58

69
public class InterpolatedTickProperty {
7-
private double factor = 0;
10+
public static final byte TPS = 20;
11+
12+
private final EvictingList<Double> points = new EvictingList<>(10);
13+
private double factor = 1;
814
private double remainder = 0;
915

16+
private final String prefix;
1017
private final Consumer<Long> setProperty;
1118
private final Supplier<Long> getProperty;
1219

13-
public InterpolatedTickProperty(Consumer<Long> setProperty, Supplier<Long> getProperty) {
20+
public InterpolatedTickProperty(String prefix, Consumer<Long> setProperty, Supplier<Long> getProperty) {
21+
this.prefix = prefix;
1422
this.setProperty = setProperty;
1523
this.getProperty = getProperty;
1624
}
1725

18-
public void increment() {
26+
public void tick() {
1927
remainder += factor; // add remainder to factor
2028
long increment = (long) remainder; // truncate floating value
21-
this.setProperty.accept(this.getProperty.get() + increment);
29+
setProperty.accept(getProperty.get() + increment);
2230
// subtract the incremented integer, preserving the floating point remainder for later
2331
remainder -= increment;
2432
}
2533

26-
public void setFactor(double factor) {
27-
this.factor = factor;
34+
public void update(long serverValue) {
35+
int localDiff = (int) (serverValue - getProperty.get());
36+
float minMoveFactor = 1f / TPS; // MIN_MOVE_FACTOR
37+
points.add((double) (localDiff + TPS) / TPS);
38+
double avg = 0, weights = 0; // weighted average
39+
int size = points.size();
40+
for (int i = 0; i < size; i++) {
41+
double weight = size - i + 1;
42+
weight *= weight;
43+
weights += weight;
44+
avg += points.get(i) * weight;
45+
}
46+
avg /= weights;
47+
double factor = avg < 0 ? Math.min(avg, -minMoveFactor) : Math.max(avg, minMoveFactor);
48+
49+
// If the next value would take more than 60 seconds at the current TPS to reach, just skip to the position.
50+
if (Math.abs(serverValue-getProperty.get()) >= 60 * TPS) {
51+
this.factor = 1;
52+
setProperty.accept(serverValue);
53+
} else {
54+
this.factor = factor;
55+
if (FabricLoader.getInstance().isDevelopmentEnvironment())
56+
System.out.format("%s: %s server by %d ticks. Speed: %f\n", prefix, (localDiff < 0 ? "ahead of" : "behind"), Math.abs(localDiff), avg);
57+
}
2858
}
2959
}

src/main/java/dev/hugeblank/asahi/client/mixin/ClientWorldMixin.java

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
@Mixin(World.class)
2424
public abstract class ClientWorldMixin implements ExtendedBlockView, IWorld, AutoCloseable, TimeSmoother {
2525

26-
@Unique private final EvictingList<Double> points = new EvictingList<>(10);
27-
@Unique private long lastPacketTimeOfDay = 0;
2826
@Unique private boolean shouldTickDay = true;
2927

3028
@Unique private InterpolatedTickProperty timeProperty;
@@ -39,16 +37,16 @@ protected ClientWorldMixin(
3937
@Inject(at=@At("TAIL"), method = "<init>")
4038
private void init(LevelProperties levelProperties, DimensionType dimensionType, BiFunction biFunction, Profiler profiler, boolean bl, CallbackInfo ci) {
4139
if (this.isClient) {
42-
this.timeProperty = new InterpolatedTickProperty(this::setTime, this::getTime);
43-
this.dayTimeProperty = new InterpolatedTickProperty(this::setTimeOfDay, this::getTimeOfDay);
40+
this.timeProperty = new InterpolatedTickProperty("time", this::setTime, this::getTime);
41+
this.dayTimeProperty = new InterpolatedTickProperty("timeOfDay", this::setTimeOfDay, this::getTimeOfDay);
4442
}
4543
}
4644

4745
@Inject(at=@At("HEAD"), method = "tickTime", cancellable = true)
4846
public void tickTime(CallbackInfo ci) {
4947
if (this.isClient) {
50-
timeProperty.increment();
51-
if (shouldTickDay) dayTimeProperty.increment();
48+
timeProperty.tick();
49+
if (shouldTickDay) dayTimeProperty.tick();
5250
ci.cancel();
5351
}
5452
}
@@ -65,39 +63,9 @@ public void tickTime(CallbackInfo ci) {
6563

6664
@Override
6765
public void asahi$updateTimes(WorldTimeUpdateS2CPacket packet) {
68-
if (lastPacketTimeOfDay == packet.getTimeOfDay()) {
69-
if (!shouldTickDay) {
70-
this.setTimeOfDay(packet.getTimeOfDay());
71-
}
72-
shouldTickDay = false;
73-
} else {
74-
shouldTickDay = true;
75-
}
76-
77-
final int TPS = 20;
78-
int localDiff = (int) (packet.getTime() - this.getTime());
79-
if (Math.abs(localDiff) >= 60 * TPS) { // SKIP_DURATION
80-
this.setTime(packet.getTime());
81-
if (shouldTickDay) this.setTimeOfDay(packet.getTimeOfDay());
82-
} else {
83-
float minMoveFactor = 1f/ TPS; // MIN_MOVE_FACTOR
84-
points.add((double) (localDiff + TPS) / TPS);
85-
double avg = 0, weights = 0; // weighted average
86-
int size = points.size();
87-
for (int i = 0; i < size; i++) {
88-
double weight = size - i + 1;
89-
weight *= weight;
90-
weights += weight;
91-
avg += points.get(i)*weight;
92-
}
93-
avg /= weights;
94-
double factor = avg < 0 ? Math.min(avg, -minMoveFactor) : Math.max(avg, minMoveFactor);
95-
timeProperty.setFactor(factor);
96-
dayTimeProperty.setFactor(factor);
66+
shouldTickDay = packet.getTimeOfDay() > 0;
9767

98-
if (FabricLoader.getInstance().isDevelopmentEnvironment())
99-
System.out.format("%s server by %d ticks. Speed: %f\n", (localDiff < 0 ? "ahead of" : "behind"), Math.abs(localDiff), avg);
100-
}
101-
lastPacketTimeOfDay = packet.getTimeOfDay();
68+
timeProperty.update(packet.getTime());
69+
dayTimeProperty.update(shouldTickDay ? packet.getTimeOfDay() : packet.getTimeOfDay()*-1);
10270
}
10371
}

0 commit comments

Comments
 (0)