aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/WirelessComputationPacket.java
diff options
context:
space:
mode:
authorNotAPenguin <michiel.vandeginste@gmail.com>2024-09-23 22:21:30 +0200
committerGitHub <noreply@github.com>2024-09-23 20:21:30 +0000
commitb9f4304cc182802c32197ac15e153d0e840f7a4d (patch)
tree0a891fb8432cec0aa60ffbfcfd69c134ecac3b0b /src/main/java/gregtech/common/WirelessComputationPacket.java
parent4a38c5955dfa24785d495e77bbfd86118449c88b (diff)
downloadGT5-Unofficial-b9f4304cc182802c32197ac15e153d0e840f7a4d.tar.gz
GT5-Unofficial-b9f4304cc182802c32197ac15e153d0e840f7a4d.tar.bz2
GT5-Unofficial-b9f4304cc182802c32197ac15e153d0e840f7a4d.zip
Wireless data refactor & fixes (#3264)
Co-authored-by: Martin Robertz <dream-master@gmx.net>
Diffstat (limited to 'src/main/java/gregtech/common/WirelessComputationPacket.java')
-rw-r--r--src/main/java/gregtech/common/WirelessComputationPacket.java76
1 files changed, 30 insertions, 46 deletions
diff --git a/src/main/java/gregtech/common/WirelessComputationPacket.java b/src/main/java/gregtech/common/WirelessComputationPacket.java
index 1b485a63c9..e4d3fcb328 100644
--- a/src/main/java/gregtech/common/WirelessComputationPacket.java
+++ b/src/main/java/gregtech/common/WirelessComputationPacket.java
@@ -4,8 +4,6 @@ import static gregtech.common.misc.GlobalVariableStorage.GlobalWirelessComputati
import java.util.UUID;
-import com.gtnewhorizon.structurelib.util.Vec3Impl;
-
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.common.misc.spaceprojects.SpaceProjectManager;
import tectech.mechanics.dataTransport.QuantumDataPacket;
@@ -15,24 +13,33 @@ public class WirelessComputationPacket {
public boolean wirelessEnabled = false;
// The main idea: 'Clearing' the computation net advances the index and sets the computation stored
- // for this index to zero. Uploading is always done to the current index, but data can be downloaded from
- // both indices
+ // for this index to zero. Uploading is always done to the current index, downloading is always done from the
+ // other index. This is essentially just a double buffered computation storage. The reason for this is that
+ // every upload needs to be done before every download happens.
private final long[] computationStored = new long[] { 0, 0 };
- private long computationDownloaded = 0;
private int currentIndex = 0;
+ private long lastUpdateTick = -1;
- public Vec3Impl controllerPosition = null;
+ private int uploadIndex() {
+ return currentIndex;
+ }
- public long getTotalComputationStored() {
- return computationStored[0] + computationStored[1];
+ private int downloadIndex() {
+ return (currentIndex + 1) % 2;
}
- private long getAvailableComputationStored() {
- return getTotalComputationStored() - computationDownloaded;
+ public long getAvailableComputationStored() {
+ return computationStored[downloadIndex()];
}
private QuantumDataPacket download(long dataIn, long aTick) {
- if (!wirelessEnabled || controllerPosition == null) return new QuantumDataPacket(0L);
+ if (!wirelessEnabled) return new QuantumDataPacket(0L);
+
+ // If the net hasn't been updated yet this tick, make sure to do so
+ if (lastUpdateTick < aTick) {
+ this.update();
+ lastUpdateTick = aTick;
+ }
// If we have enough computation 'stored', download it
// Note that this means that if you do not have enough computation to go to all
@@ -40,34 +47,21 @@ public class WirelessComputationPacket {
// This also means that if you don't have enough computation for a hatch, it will not receive any computation
// at all. This is also fine.
if (getAvailableComputationStored() >= dataIn) {
- computationDownloaded += dataIn;
+ computationStored[downloadIndex()] -= dataIn;
return new QuantumDataPacket(dataIn);
} else return new QuantumDataPacket(0L);
}
- private void update(IGregTechTileEntity entity, long aTick) {
+ private void update() {
// The reason we want this complex index cycling system is because hatches may upload and download computation
// in the same tick as the currently stored computation is cleared. To avoid interruptions, we want to
// try to double buffer these updates. This means that we keep two computation values around, and every update
- // we only clear one of them. Only the most recent entry can be used for uploading computation, but we allow
- // downloading computation from both the current and the previous index.
+ // we only clear one of them.
// Remove downloaded computation previous index (which is also the next index since there are only two),
// then remove the leftover from current index.
- int nextIndex = (currentIndex + 1) % 2;
- long availableInPrevious = computationStored[nextIndex];
- // Clear stored computation for the next index, since we don't want to allow players to accumulate
- // computation in their wireless network indefinitely. This would allow for cheesing research by passively
- // banking computation and then setting the input hatch to a high value when the computation is needed.
- computationStored[nextIndex] = 0;
- if (computationDownloaded > availableInPrevious) {
- long toDrainFromCurrent = computationDownloaded - availableInPrevious;
- computationStored[currentIndex] -= toDrainFromCurrent;
- }
- // Reset our current tally of downloaded computation
- computationDownloaded = 0;
- // Now advance the current index to the next index
- currentIndex = nextIndex;
+ computationStored[downloadIndex()] = 0;
+ currentIndex = (currentIndex + 1) % 2;
}
private void setWirelessEnabled(boolean wirelessEnabled) {
@@ -75,8 +69,13 @@ public class WirelessComputationPacket {
}
private void upload(long dataOut, long aTick) {
+ // If the net hasn't been updated yet this tick, make sure to do so
+ if (lastUpdateTick < aTick) {
+ this.update();
+ lastUpdateTick = aTick;
+ }
// Store computation that is uploaded internally
- computationStored[currentIndex] += dataOut;
+ computationStored[uploadIndex()] += dataOut;
}
public static QuantumDataPacket downloadData(UUID userId, long dataIn, long aTick) {
@@ -87,26 +86,11 @@ public class WirelessComputationPacket {
getPacketByUserId(userId).upload(dataOut, aTick);
}
- public static void updatePacket(IGregTechTileEntity entity, long aTick) {
- getPacketByUserId(entity.getOwnerUuid()).update(entity, aTick);
- }
-
- public static boolean enableWirelessNetWork(IGregTechTileEntity entity) {
- var packet = getPacketByUserId(entity.getOwnerUuid());
- Vec3Impl pos = new Vec3Impl(entity.getXCoord(), entity.getYCoord(), entity.getZCoord());
- if (packet.wirelessEnabled && packet.controllerPosition != null
- && pos.compareTo(packet.controllerPosition) != 0) return false;
+ public static void enableWirelessNetWork(IGregTechTileEntity entity) {
getPacketByUserId(entity.getOwnerUuid()).setWirelessEnabled(true);
- if (packet.controllerPosition == null) {
- packet.controllerPosition = new Vec3Impl(entity.getXCoord(), entity.getYCoord(), entity.getZCoord());
- }
- return true;
}
public static void disableWirelessNetWork(IGregTechTileEntity entity) {
- var packet = getPacketByUserId(entity.getOwnerUuid());
- Vec3Impl pos = new Vec3Impl(entity.getXCoord(), entity.getYCoord(), entity.getZCoord());
- if (packet.controllerPosition != null && packet.controllerPosition.compareTo(pos) != 0) return;
getPacketByUserId(entity.getOwnerUuid()).setWirelessEnabled(false);
}