From 8dd64d25caf626c538dcb628408a239ee724fc5e Mon Sep 17 00:00:00 2001 From: basdxz Date: Sun, 15 Nov 2020 15:29:30 +0000 Subject: Tesla Speedup -Switched from sorted HashMap to ArrayListMultiMap -Fixed issue where interface lacked proper methods --- .../tectech/mechanics/tesla/ITeslaConnectable.java | 23 +++--- .../multi/GT_MetaTileEntity_TM_teslaCoil.java | 18 ++++ .../single/GT_MetaTileEntity_TeslaCoil.java | 96 +++++++++++++--------- 3 files changed, 87 insertions(+), 50 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/github/technus/tectech/mechanics/tesla/ITeslaConnectable.java b/src/main/java/com/github/technus/tectech/mechanics/tesla/ITeslaConnectable.java index 291acf393d..d505ca8e09 100644 --- a/src/main/java/com/github/technus/tectech/mechanics/tesla/ITeslaConnectable.java +++ b/src/main/java/com/github/technus/tectech/mechanics/tesla/ITeslaConnectable.java @@ -1,19 +1,20 @@ package com.github.technus.tectech.mechanics.tesla; import com.github.technus.tectech.mechanics.spark.ThaumSpark; +import com.google.common.collect.Multimap; -import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Objects; -import static com.github.technus.tectech.util.Util.entriesSortedByValues; import static java.lang.Math.sqrt; public interface ITeslaConnectable extends ITeslaConnectableSimple { //Map with all Teslas in the same dimension and the distance to them //TODO Range - HashMap teslaNodeMap = new HashMap<>(); + Multimap getTeslaNodeMap(); + //ThaumCraft lighting coordinate pairs, so we can send them in bursts and save on lag - HashSet sparkList = new HashSet<>(); + HashSet getSparkList(); //-128 to -1 disables capability //0 means any source or target @@ -36,7 +37,7 @@ public interface ITeslaConnectable extends ITeslaConnectableSimple { public static final HashSet teslaNodeSet = new HashSet<>();//Targets for power transmission public static void generateTeslaNodeMap(ITeslaConnectable origin) { - origin.teslaNodeMap.clear(); + origin.getTeslaNodeMap().clear(); for (ITeslaConnectableSimple target : teslaNodeSet) { //Sanity checks if (target == null) { @@ -59,13 +60,13 @@ public interface ITeslaConnectable extends ITeslaConnectableSimple { //Skip if the range is too vast continue; } - origin.teslaNodeMap.put(target, distance); + origin.getTeslaNodeMap().put(distance, target); } } public static void cleanTeslaNodeMap(ITeslaConnectable origin) { //Wipes all null objects, in practice this is unloaded or improperly removed tesla objects - origin.teslaNodeMap.keySet().removeIf(Objects::isNull); + origin.getTeslaNodeMap().keySet().removeIf(Objects::isNull); } public static long powerTeslaNodeMap(ITeslaConnectable origin) { @@ -76,18 +77,18 @@ public interface ITeslaConnectable extends ITeslaConnectableSimple { long remainingAmperes = origin.getTeslaOutputCurrent(); while (remainingAmperes > 0) { long startingAmperes = remainingAmperes; - for (HashMap.Entry Rx : entriesSortedByValues(teslaNodeMap)) { + for (Map.Entry Rx : origin.getTeslaNodeMap().entries()) { if (origin.getTeslaStoredEnergy() < (origin.isOverdriveEnabled() ? origin.getTeslaOutputVoltage() * 2 : origin.getTeslaOutputVoltage())) { //Return and end the tick if we're out of energy to send return origin.getTeslaOutputCurrent() - remainingAmperes; } - ITeslaConnectableSimple target = Rx.getKey(); + ITeslaConnectableSimple target = Rx.getValue(); //Continue if the target can't receive if(!target.isTeslaReadyToReceive()) continue; - int distance = Rx.getValue(); + int distance = Rx.getKey(); //Calculate the voltage output long outputVoltageInjectable; @@ -109,7 +110,7 @@ public interface ITeslaConnectable extends ITeslaConnectableSimple { if (target.teslaInjectEnergy(outputVoltageInjectable)) { origin.teslaDrainEnergy(outputVoltageConsumption); - sparkList.add(new ThaumSpark(origin.getTeslaPosition(), target.getTeslaPosition(), origin.getTeslaDimension())); + origin.getSparkList().add(new ThaumSpark(origin.getTeslaPosition(), target.getTeslaPosition(), origin.getTeslaDimension())); remainingAmperes--; } if (remainingAmperes == 0) { diff --git a/src/main/java/com/github/technus/tectech/thing/metaTileEntity/multi/GT_MetaTileEntity_TM_teslaCoil.java b/src/main/java/com/github/technus/tectech/thing/metaTileEntity/multi/GT_MetaTileEntity_TM_teslaCoil.java index 93f6e50c5a..5f6e0fffaa 100644 --- a/src/main/java/com/github/technus/tectech/thing/metaTileEntity/multi/GT_MetaTileEntity_TM_teslaCoil.java +++ b/src/main/java/com/github/technus/tectech/thing/metaTileEntity/multi/GT_MetaTileEntity_TM_teslaCoil.java @@ -8,6 +8,7 @@ import com.github.technus.tectech.mechanics.spark.ThaumSpark; import com.github.technus.tectech.mechanics.structure.Structure; import com.github.technus.tectech.mechanics.structure.adders.IHatchAdder; import com.github.technus.tectech.mechanics.tesla.ITeslaConnectable; +import com.github.technus.tectech.mechanics.tesla.ITeslaConnectableSimple; import com.github.technus.tectech.thing.metaTileEntity.hatch.GT_MetaTileEntity_Hatch_Capacitor; import com.github.technus.tectech.thing.metaTileEntity.hatch.GT_MetaTileEntity_Hatch_DynamoMulti; import com.github.technus.tectech.thing.metaTileEntity.hatch.GT_MetaTileEntity_Hatch_EnergyMulti; @@ -19,6 +20,8 @@ import com.github.technus.tectech.thing.metaTileEntity.multi.base.Parameters; import com.github.technus.tectech.thing.metaTileEntity.multi.base.render.TT_RenderedExtendedFacingTexture; import com.github.technus.tectech.util.CommonValues; import com.github.technus.tectech.util.Vec3Impl; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import gregtech.api.enums.Materials; @@ -35,6 +38,7 @@ import net.minecraft.util.EnumChatFormatting; import net.minecraftforge.fluids.FluidStack; import java.util.ArrayList; +import java.util.HashSet; import static com.github.technus.tectech.mechanics.structure.Structure.adders; import static com.github.technus.tectech.mechanics.tesla.ITeslaConnectable.TeslaUtil.*; @@ -47,6 +51,10 @@ import static java.lang.Math.*; import static net.minecraft.util.StatCollector.translateToLocal; public class GT_MetaTileEntity_TM_teslaCoil extends GT_MetaTileEntity_MultiblockBase_EM implements IConstructable, ITeslaConnectable { + //Interface fields + Multimap teslaNodeMap = ArrayListMultimap.create(); + HashSet sparkList = new HashSet<>(); + //region variables private static final int transferRadiusTowerFromConfig = TecTech.configTecTech.TESLA_MULTI_RANGE_TOWER;//Default is 32 private static final int transferRadiusTransceiverFromConfig = TecTech.configTecTech.TESLA_MULTI_RANGE_TRANSCEIVER;//Default is 16 @@ -701,6 +709,16 @@ public class GT_MetaTileEntity_TM_teslaCoil extends GT_MetaTileEntity_Multiblock return 0; } + @Override + public Multimap getTeslaNodeMap() { + return teslaNodeMap; + } + + @Override + public HashSet getSparkList() { + return sparkList; + } + @Override public byte getTeslaTransmissionCapability() { return 1; diff --git a/src/main/java/com/github/technus/tectech/thing/metaTileEntity/single/GT_MetaTileEntity_TeslaCoil.java b/src/main/java/com/github/technus/tectech/thing/metaTileEntity/single/GT_MetaTileEntity_TeslaCoil.java index 388a253b27..cc42929806 100644 --- a/src/main/java/com/github/technus/tectech/thing/metaTileEntity/single/GT_MetaTileEntity_TeslaCoil.java +++ b/src/main/java/com/github/technus/tectech/thing/metaTileEntity/single/GT_MetaTileEntity_TeslaCoil.java @@ -1,6 +1,7 @@ package com.github.technus.tectech.thing.metaTileEntity.single; import com.github.technus.tectech.mechanics.tesla.ITeslaConnectable; +import com.github.technus.tectech.mechanics.tesla.ITeslaConnectableSimple; import com.github.technus.tectech.util.CommonValues; import com.github.technus.tectech.TecTech; import com.github.technus.tectech.loader.NetworkDispatcher; @@ -9,6 +10,8 @@ import com.github.technus.tectech.mechanics.spark.ThaumSpark; import com.github.technus.tectech.thing.metaTileEntity.multi.GT_MetaTileEntity_TM_teslaCoil; import com.github.technus.tectech.util.Util; import com.github.technus.tectech.util.Vec3Impl; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; import eu.usrv.yamcore.auxiliary.PlayerChatHelper; import gregtech.api.interfaces.ITexture; import gregtech.api.interfaces.metatileentity.IMetaTileEntity; @@ -23,6 +26,7 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.reflect.FieldUtils; import java.util.Arrays; +import java.util.HashSet; import static com.github.technus.tectech.mechanics.tesla.ITeslaConnectable.TeslaUtil.*; import static com.github.technus.tectech.util.CommonValues.V; @@ -32,6 +36,10 @@ import static net.minecraft.util.StatCollector.translateToLocal; import static net.minecraft.util.StatCollector.translateToLocalFormatted; public class GT_MetaTileEntity_TeslaCoil extends GT_MetaTileEntity_BasicBatteryBuffer implements ITeslaConnectable { + //Interface fields + Multimap teslaNodeMap = ArrayListMultimap.create(); + HashSet sparkList = new HashSet<>(); + private final static int transferRadiusMax = TecTech.configTecTech.TESLA_SINGLE_RANGE;//Default is 20 private final static int perBlockLoss = TecTech.configTecTech.TESLA_SINGLE_LOSS_PER_BLOCK;//Default is 1 private final static float overDriveLoss = TecTech.configTecTech.TESLA_SINGLE_LOSS_FACTOR_OVERDRIVE;//Default is 0.25F @@ -179,45 +187,45 @@ public class GT_MetaTileEntity_TeslaCoil extends GT_MetaTileEntity_BasicBatteryB return new GT_MetaTileEntity_TeslaCoil(mName, mTier, mDescription, mTextures, mInventory.length); } - private void thaumLightning(IGregTechTileEntity mte, IGregTechTileEntity node) { - int x = mte.getXCoord(); - int y = mte.getYCoord(); - int z = mte.getZCoord(); - - byte xR; - byte yR; - byte zR; - - IMetaTileEntity nodeInside = node.getMetaTileEntity(); - if (nodeInside instanceof GT_MetaTileEntity_TM_teslaCoil) { - GT_MetaTileEntity_TM_teslaCoil nodeTesla = (GT_MetaTileEntity_TM_teslaCoil) nodeInside; - xR = (byte) (nodeTesla.posTop.get0() - x); - yR = (byte) (nodeTesla.posTop.get1() - y); - zR = (byte) (nodeTesla.posTop.get2() - z); - } else { - xR = (byte) (node.getXCoord() - x); - yR = (byte) (node.getYCoord() - y); - zR = (byte) (node.getZCoord() - z); - } - - int wID = mte.getWorld().provider.dimensionId; - - sparkList.add(new ThaumSpark(x, y, z, xR, yR, zR, wID)); - } - - private long[] getOutputVoltage(long outputVoltage, int distance, boolean overDriveToggle) { - long outputVoltageInjectable; - long outputVoltageConsumption; - - if (overDriveToggle) { - outputVoltageInjectable = outputVoltage; - outputVoltageConsumption = outputVoltage + (distance * perBlockLoss) + (long) Math.round(overDriveLoss * outputVoltage); - } else { - outputVoltageInjectable = outputVoltage - (distance * perBlockLoss); - outputVoltageConsumption = outputVoltage; - } - return new long[]{outputVoltageInjectable, outputVoltageConsumption}; - } +// private void thaumLightning(IGregTechTileEntity mte, IGregTechTileEntity node) { +// int x = mte.getXCoord(); +// int y = mte.getYCoord(); +// int z = mte.getZCoord(); +// +// byte xR; +// byte yR; +// byte zR; +// +// IMetaTileEntity nodeInside = node.getMetaTileEntity(); +// if (nodeInside instanceof GT_MetaTileEntity_TM_teslaCoil) { +// GT_MetaTileEntity_TM_teslaCoil nodeTesla = (GT_MetaTileEntity_TM_teslaCoil) nodeInside; +// xR = (byte) (nodeTesla.posTop.get0() - x); +// yR = (byte) (nodeTesla.posTop.get1() - y); +// zR = (byte) (nodeTesla.posTop.get2() - z); +// } else { +// xR = (byte) (node.getXCoord() - x); +// yR = (byte) (node.getYCoord() - y); +// zR = (byte) (node.getZCoord() - z); +// } +// +// int wID = mte.getWorld().provider.dimensionId; +// +// sparkList.add(new ThaumSpark(x, y, z, xR, yR, zR, wID)); +// } +// +// private long[] getOutputVoltage(long outputVoltage, int distance, boolean overDriveToggle) { +// long outputVoltageInjectable; +// long outputVoltageConsumption; +// +// if (overDriveToggle) { +// outputVoltageInjectable = outputVoltage; +// outputVoltageConsumption = outputVoltage + (distance * perBlockLoss) + (long) Math.round(overDriveLoss * outputVoltage); +// } else { +// outputVoltageInjectable = outputVoltage - (distance * perBlockLoss); +// outputVoltageConsumption = outputVoltage; +// } +// return new long[]{outputVoltageInjectable, outputVoltageConsumption}; +// } @Override public void onFirstTick(IGregTechTileEntity aBaseMetaTileEntity) { @@ -308,6 +316,16 @@ public class GT_MetaTileEntity_TeslaCoil extends GT_MetaTileEntity_BasicBatteryB return 1; } + @Override + public Multimap getTeslaNodeMap() { + return teslaNodeMap; + } + + @Override + public HashSet getSparkList() { + return sparkList; + } + @Override public byte getTeslaTransmissionCapability() { return 2; -- cgit