diff options
author | NotAPenguin <michiel.vandeginste@gmail.com> | 2024-07-18 17:08:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-18 22:08:27 +0700 |
commit | 11a2735b633623cb6a754247c4f42ff14810b38b (patch) | |
tree | 1ff7c3e4c11a437f80d410e0800b3b2e5521417f /src/main/java/gregtech/common | |
parent | e3a650785887866e4b8533b61044479ecb80aee7 (diff) | |
download | GT5-Unofficial-11a2735b633623cb6a754247c4f42ff14810b38b.tar.gz GT5-Unofficial-11a2735b633623cb6a754247c4f42ff14810b38b.tar.bz2 GT5-Unofficial-11a2735b633623cb6a754247c4f42ff14810b38b.zip |
Add wireless computation and data stick hatches (#2724)
* start on wireless computation
* initial implementation of wireless computation hatch
* Implement wireless data sticks
* Fix wireless computation by simplifying the implementation greatly
* Delete dead code
* Add scanner output for wireless computation in network
* Rename wireless computation hatch to cloud hatch
* Final rename to client/server
Diffstat (limited to 'src/main/java/gregtech/common')
4 files changed, 167 insertions, 13 deletions
diff --git a/src/main/java/gregtech/common/WirelessComputationPacket.java b/src/main/java/gregtech/common/WirelessComputationPacket.java new file mode 100644 index 0000000000..cafe4d93dc --- /dev/null +++ b/src/main/java/gregtech/common/WirelessComputationPacket.java @@ -0,0 +1,120 @@ +package gregtech.common; + +import static gregtech.common.misc.GlobalVariableStorage.GlobalWirelessComputation; + +import java.util.UUID; + +import com.github.technus.tectech.mechanics.dataTransport.QuantumDataPacket; +import com.gtnewhorizon.structurelib.util.Vec3Impl; + +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import gregtech.common.misc.spaceprojects.SpaceProjectManager; + +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 + private final long[] computationStored = new long[] { 0, 0 }; + private long computationDownloaded = 0; + private int currentIndex = 0; + + public Vec3Impl controllerPosition = null; + + public long getTotalComputationStored() { + return computationStored[0] + computationStored[1]; + } + + private long getAvailableComputationStored() { + return getTotalComputationStored() - computationDownloaded; + } + + private QuantumDataPacket download(long dataIn, long aTick) { + if (!wirelessEnabled || controllerPosition == null) return new QuantumDataPacket(0L); + + // If we have enough computation 'stored', download it + // Note that this means that if you do not have enough computation to go to all + // destinations, it won't be distributed equally. This is fine. + // 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; + return new QuantumDataPacket(dataIn); + } else return new QuantumDataPacket(0L); + } + + private void update(IGregTechTileEntity entity, long aTick) { + // 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. + + // 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; + } + + private void setWirelessEnabled(boolean wirelessEnabled) { + this.wirelessEnabled = wirelessEnabled; + } + + private void upload(long dataOut, long aTick) { + // Store computation that is uploaded internally + computationStored[currentIndex] += dataOut; + } + + public static QuantumDataPacket downloadData(UUID userId, long dataIn, long aTick) { + return getPacketByUserId(userId).download(dataIn, aTick); + } + + public static void uploadData(UUID userId, long dataOut, long aTick) { + 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; + 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); + } + + public static WirelessComputationPacket getPacketByUserId(UUID userId) { + UUID team = SpaceProjectManager.getLeader(userId); + if (GlobalWirelessComputation.get(team) == null) { + GlobalWirelessComputation.put(team, new WirelessComputationPacket()); + } + return GlobalWirelessComputation.get(team); + } +} diff --git a/src/main/java/gregtech/common/WirelessDataStore.java b/src/main/java/gregtech/common/WirelessDataStore.java new file mode 100644 index 0000000000..4016a2440b --- /dev/null +++ b/src/main/java/gregtech/common/WirelessDataStore.java @@ -0,0 +1,36 @@ +package gregtech.common; + +import static gregtech.common.misc.GlobalVariableStorage.GlobalWirelessDataSticks; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import net.minecraft.item.ItemStack; + +import gregtech.common.misc.spaceprojects.SpaceProjectManager; + +public class WirelessDataStore { + + private final ArrayList<ItemStack> dataSticks = new ArrayList<>(); + + public void clearData() { + dataSticks.clear(); + } + + public void uploadData(List<ItemStack> sticks) { + dataSticks.addAll(sticks); + } + + public List<ItemStack> downloadData() { + return dataSticks; + } + + public static WirelessDataStore getWirelessDataSticks(UUID uuid) { + UUID team = SpaceProjectManager.getLeader(uuid); + if (GlobalWirelessDataSticks.get(team) == null) { + GlobalWirelessDataSticks.put(team, new WirelessDataStore()); + } + return GlobalWirelessDataSticks.get(team); + } +} diff --git a/src/main/java/gregtech/common/misc/GlobalVariableStorage.java b/src/main/java/gregtech/common/misc/GlobalVariableStorage.java index 27aad0a11f..33abc66f13 100644 --- a/src/main/java/gregtech/common/misc/GlobalVariableStorage.java +++ b/src/main/java/gregtech/common/misc/GlobalVariableStorage.java @@ -4,12 +4,20 @@ import java.math.BigInteger; import java.util.HashMap; import java.util.UUID; +import gregtech.common.WirelessComputationPacket; +import gregtech.common.WirelessDataStore; + public abstract class GlobalVariableStorage { // --------------------- NEVER access these maps! Use the methods provided! --------------------- // Global EU map. public static HashMap<UUID, BigInteger> GlobalEnergy = new HashMap<>(100, 0.9f); - // ---------------------------------------------------------------------------------------------- + // Global Wireless Data map + public static HashMap<UUID, WirelessComputationPacket> GlobalWirelessComputation = new HashMap<>(100, 0.9f); + // Global Wireless Data Stick map + public static HashMap<UUID, WirelessDataStore> GlobalWirelessDataSticks = new HashMap<>(100, 0.9f); + + // ---------------------------------------------------------------------------------------------- } diff --git a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_AssemblyLine.java b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_AssemblyLine.java index 7c7c27c880..aa422505d7 100644 --- a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_AssemblyLine.java +++ b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_AssemblyLine.java @@ -395,7 +395,7 @@ public class GT_MetaTileEntity_AssemblyLine extends /** * @param state using bitmask, 1 for IntegratedCircuit, 2 for DataStick, 4 for DataOrb */ - private boolean isCorrectDataItem(ItemStack aStack, int state) { + private static boolean isCorrectDataItem(ItemStack aStack, int state) { if ((state & 1) != 0 && ItemList.Circuit_Integrated.isStackEqual(aStack, true, true)) return true; if ((state & 2) != 0 && ItemList.Tool_DataStick.isStackEqual(aStack, false, true)) return true; return (state & 4) != 0 && ItemList.Tool_DataOrb.isStackEqual(aStack, false, true); @@ -410,17 +410,7 @@ public class GT_MetaTileEntity_AssemblyLine extends rList.add(mInventory[1]); } for (GT_MetaTileEntity_Hatch_DataAccess tHatch : filterValidMTEs(mDataAccessHatches)) { - for (int i = 0; i < tHatch.getBaseMetaTileEntity() - .getSizeInventory(); i++) { - if (tHatch.getBaseMetaTileEntity() - .getStackInSlot(i) != null && isCorrectDataItem( - tHatch.getBaseMetaTileEntity() - .getStackInSlot(i), - state)) - rList.add( - tHatch.getBaseMetaTileEntity() - .getStackInSlot(i)); - } + rList.addAll(tHatch.getInventoryItems(stack -> isCorrectDataItem(stack, state))); } return rList; } |