diff options
author | Martin Robertz <dream-master@gmx.net> | 2021-11-28 17:39:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-28 17:39:58 +0100 |
commit | 8843f54841b40fba3b6221c7c40a807ac5940c0a (patch) | |
tree | 51a0305421137d589ac0da9b513f67652f49363d /src/main/java/gregtech | |
parent | d016a5221be86799ac1264aa7e6231d959bfc796 (diff) | |
parent | 9d432a42b6b7e502f8593b437fb2c110f5de8082 (diff) | |
download | GT5-Unofficial-8843f54841b40fba3b6221c7c40a807ac5940c0a.tar.gz GT5-Unofficial-8843f54841b40fba3b6221c7c40a807ac5940c0a.tar.bz2 GT5-Unofficial-8843f54841b40fba3b6221c7c40a807ac5940c0a.zip |
Merge pull request #757 from GTNewHorizons/patch-pollution
Fix pollution ticking and reading
Diffstat (limited to 'src/main/java/gregtech')
-rw-r--r-- | src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java | 2 | ||||
-rw-r--r-- | src/main/java/gregtech/common/GT_Pollution.java | 73 |
2 files changed, 48 insertions, 27 deletions
diff --git a/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java b/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java index bd9148b516..aa779b0359 100644 --- a/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java +++ b/src/main/java/gregtech/api/util/GT_ChunkAssociatedData.java @@ -296,7 +296,7 @@ public abstract class GT_ChunkAssociatedData<T extends GT_ChunkAssociatedData.ID } public boolean isCreated(int subRegionX, int subRegionZ) { - return this.data[getIndex(subRegionX, subRegionZ)] == null; + return this.data[getIndex(subRegionX, subRegionZ)] != null; } public ChunkCoordIntPair getCoord() { diff --git a/src/main/java/gregtech/common/GT_Pollution.java b/src/main/java/gregtech/common/GT_Pollution.java index 865fa83f7e..8ad39306f1 100644 --- a/src/main/java/gregtech/common/GT_Pollution.java +++ b/src/main/java/gregtech/common/GT_Pollution.java @@ -27,12 +27,16 @@ import net.minecraftforge.event.world.ChunkDataEvent; import net.minecraftforge.event.world.ChunkWatchEvent; import net.minecraftforge.event.world.WorldEvent; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.function.Consumer; import static gregtech.api.objects.XSTR.XSTR_INSTANCE; import static gregtech.common.GT_Proxy.dimensionWisePollution; @@ -73,11 +77,12 @@ public class GT_Pollution { * Muffler Hatch Pollution reduction: ** inaccurate ** * LV (0%), MV (30%), HV (52%), EV (66%), IV (76%), LuV (84%), ZPM (89%), UV (92%), MAX (95%) */ - private List<ChunkCoordIntPair> pollutionList = new ArrayList<>();//chunks left to process - private final List<ChunkCoordIntPair> chunkData = new ArrayList<>();//link to chunk data that is saved/loaded + private List<ChunkCoordIntPair> pollutionList = new ArrayList<>();//chunks left to process in this cycle + private final Set<ChunkCoordIntPair> pollutedChunks = new HashSet<>();// a global list of all chunks with positive pollution private int operationsPerTick = 0;//how much chunks should be processed in each cycle private static final short cycleLen = 1200; private final World world; + private boolean blank = true; public static int mPlayerPollution; private static int POLLUTIONPACKET_MINVALUE = 1000; @@ -103,16 +108,18 @@ public class GT_Pollution { private void tickPollutionInWorld(int aTickID) {//called from method above //gen data set - if (aTickID == 0) { + if (aTickID == 0 || blank) { // make a snapshot of what to work on - // counterintuitive as it seems, but this is the fastest way java collections framework offers us. - pollutionList = new ArrayList<>(chunkData); + pollutionList = new ArrayList<>(pollutedChunks); //set operations per tick - if (pollutionList.size() > 0) operationsPerTick = (pollutionList.size() / cycleLen); - else operationsPerTick = 0;//SANity + if (pollutionList.size() > 0) + operationsPerTick = Math.max(1, pollutionList.size() / cycleLen); + else + operationsPerTick = 0; //SANity + blank = false; } - for (int chunksProcessed = 0; chunksProcessed <= operationsPerTick; chunksProcessed++) { + for (int chunksProcessed = 0; chunksProcessed < operationsPerTick; chunksProcessed++) { if (pollutionList.size() == 0) break;//no more stuff to do ChunkCoordIntPair actualPos = pollutionList.remove(pollutionList.size() - 1);//faster //get pollution @@ -120,10 +127,8 @@ public class GT_Pollution { int tPollution = currentData.getAmount(); //remove some tPollution = (int) (0.9945f * tPollution); - //tPollution -= 2000;//This does not really matter... - if (tPollution <= 0) tPollution = 0;//SANity check - else if (tPollution > 400000) {//Spread Pollution + if (tPollution > 400000) {//Spread Pollution ChunkCoordIntPair[] tNeighbors = new ChunkCoordIntPair[4];//array is faster tNeighbors[0] = (new ChunkCoordIntPair(actualPos.chunkXPos + 1, actualPos.chunkZPos)); @@ -138,7 +143,7 @@ public class GT_Pollution { tDiff = tDiff / 20; neighborPollution = GT_Utility.safeInt((long) neighborPollution + tDiff);//tNPol += tDiff; tPollution -= tDiff; - neighbor.setAmount(neighborPollution); + setChunkPollution(neighborPosition, neighborPollution); } } @@ -200,7 +205,7 @@ public class GT_Pollution { } } //Write new pollution to Hashmap !!! - currentData.setAmount(tPollution); + setChunkPollution(actualPos, tPollution); //Send new value to players nearby if (tPollution > POLLUTIONPACKET_MINVALUE) { @@ -210,6 +215,10 @@ public class GT_Pollution { } } + private void setChunkPollution(ChunkCoordIntPair coord, int pollution) { + mutatePollution(world, coord.chunkXPos, coord.chunkZPos, c -> c.setAmount(pollution), pollutedChunks); + } + private static void damageBlock(World world, int x, int y, int z, boolean sourRain) { if (world.isRemote) return; Block tBlock = world.getBlock(x, y, z); @@ -265,13 +274,33 @@ public class GT_Pollution { } } + private static GT_Pollution getPollutionManager(World world) { + return dimensionWisePollution.computeIfAbsent(world.provider.dimensionId, i -> new GT_Pollution(world)); + } + public static void addPollution(IGregTechTileEntity te, int aPollution) { - addPollution(te.getWorld().getChunkFromBlockCoords(te.getXCoord(), te.getZCoord()), aPollution); + if (!GT_Mod.gregtechproxy.mPollution || aPollution == 0) return; + mutatePollution(te.getWorld(), te.getXCoord() >> 4, te.getZCoord() >> 4, d -> d.changeAmount(aPollution), null); } public static void addPollution(Chunk ch, int aPollution) { if (!GT_Mod.gregtechproxy.mPollution || aPollution == 0) return; - STORAGE.get(ch).changeAmount(aPollution); + mutatePollution(ch.worldObj, ch.xPosition, ch.zPosition, d -> d.changeAmount(aPollution), null); + } + + private static void mutatePollution(World world, int x, int z, Consumer<ChunkData> mutator, @Nullable Set<ChunkCoordIntPair> chunks) { + ChunkData data = STORAGE.get(world, x, z); + boolean hadPollution = data.getAmount() > 0; + mutator.accept(data); + boolean hasPollution = data.getAmount() > 0; + if (hasPollution != hadPollution) { + if (chunks == null) + chunks = getPollutionManager(world).pollutedChunks; + if (hasPollution) + chunks.add(new ChunkCoordIntPair(x, z)); + else + chunks.remove(new ChunkCoordIntPair(x, z)); + } } public static int getPollution(IGregTechTileEntity te) { @@ -341,14 +370,11 @@ public class GT_Pollution { @Override protected ChunkData readElement(DataInput input, int version, World world, int chunkX, int chunkZ) throws IOException { ChunkData data = new ChunkData(input.readInt()); - getChunkData(world).add(new ChunkCoordIntPair(chunkX, chunkZ)); + if (data.getAmount() > 0) + getPollutionManager(world).pollutedChunks.add(new ChunkCoordIntPair(chunkX, chunkZ)); return data; } - private List<ChunkCoordIntPair> getChunkData(World world) { - return dimensionWisePollution.computeIfAbsent(world.provider.dimensionId, i -> new GT_Pollution(world)).chunkData; - } - @Override protected ChunkData createElement(World world, int chunkX, int chunkZ) { return new ChunkData(); @@ -359,11 +385,6 @@ public class GT_Pollution { super.loadAll(w); } - public void set(World world, ChunkCoordIntPair coord, ChunkData data) { - set(world, coord.chunkXPos, coord.chunkZPos, data); - getChunkData(world).add(coord); - } - public boolean isCreated(World world, ChunkCoordIntPair coord) { return isCreated(world.provider.dimensionId, coord.chunkXPos, coord.chunkZPos); } @@ -377,7 +398,7 @@ public class GT_Pollution { } private ChunkData(int amount) { - this.amount = amount; + this.amount = Math.max(0, amount); } /** |