aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/misc
diff options
context:
space:
mode:
authorKiwi <42833050+Kiwi233@users.noreply.github.com>2020-12-13 21:24:20 +0800
committerGitHub <noreply@github.com>2020-12-13 21:24:20 +0800
commitd567ee9f63c6e11f2b21f26687cfc2ecaed200a1 (patch)
tree8e4c275624a966c4482b90ad85cda2375c6a63b9 /src/main/java/gregtech/common/misc
parentcec32d0a604156802eee3f9e8fefccb40a62d7ef (diff)
parent7ce77a615de68add2bb0fa71818b3e36241a02a7 (diff)
downloadGT5-Unofficial-d567ee9f63c6e11f2b21f26687cfc2ecaed200a1.tar.gz
GT5-Unofficial-d567ee9f63c6e11f2b21f26687cfc2ecaed200a1.tar.bz2
GT5-Unofficial-d567ee9f63c6e11f2b21f26687cfc2ecaed200a1.zip
Merge pull request #2 from GTNewHorizons/experimental
5.09.33.57
Diffstat (limited to 'src/main/java/gregtech/common/misc')
-rw-r--r--src/main/java/gregtech/common/misc/GT_ClientPollutionMap.java156
-rw-r--r--src/main/java/gregtech/common/misc/GT_Command.java50
2 files changed, 192 insertions, 14 deletions
diff --git a/src/main/java/gregtech/common/misc/GT_ClientPollutionMap.java b/src/main/java/gregtech/common/misc/GT_ClientPollutionMap.java
new file mode 100644
index 0000000000..7ba470e412
--- /dev/null
+++ b/src/main/java/gregtech/common/misc/GT_ClientPollutionMap.java
@@ -0,0 +1,156 @@
+package gregtech.common.misc;
+
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.entity.EntityClientPlayerMP;
+import net.minecraft.client.renderer.OpenGlHelper;
+import net.minecraft.util.ChatComponentText;
+import net.minecraft.util.MathHelper;
+import org.lwjgl.opengl.GL11;
+
+public class GT_ClientPollutionMap {
+ private static final byte RADIUS = 24;
+ private static final byte DISTANCE_RELOAD_MAP = 5; //When player moved x chunks, shift the map to new center.
+ private static final byte SIZE = RADIUS*2+1; //Area to keep stored.
+
+ private int x0, z0;
+ private int dim;
+
+ private boolean initialized = false;
+
+ private static short[][] chunkMatrix; //short because reasons.
+
+
+ public GT_ClientPollutionMap(){ }
+
+ public void reset() {
+ initialized = false;
+ }
+
+ private void initialize(int playerChunkX, int playerChunkZ, int dimension) {
+ initialized = true;
+ chunkMatrix = new short[SIZE][SIZE];
+ x0 = playerChunkX;
+ z0 = playerChunkZ;
+ dim = dimension;
+ }
+
+ public void addChunkPollution(int chunkX, int chunkZ, int pollution) {
+ EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer;
+ if (player == null || player.worldObj == null)
+ return;
+
+ int playerXChunk = MathHelper.floor_double(player.posX) >> 4;
+ int playerZChunk = MathHelper.floor_double(player.posZ) >> 4; //posX/Z seems to be always loaded,
+
+ if (!initialized) {
+ initialize(playerXChunk, playerZChunk, player.dimension);
+ }
+
+ if (dim != player.dimension) {
+ initialize(playerXChunk, playerZChunk, player.dimension);
+ }
+
+ if (Math.abs(x0 - playerXChunk) > DISTANCE_RELOAD_MAP || Math.abs(z0 - playerZChunk) > DISTANCE_RELOAD_MAP)
+ shiftCenter(playerXChunk, playerZChunk);
+
+ int relX = chunkX - x0 + RADIUS;
+ if (relX >= SIZE || relX < 0) //out of bounds
+ return;
+ int relZ = chunkZ - z0 + RADIUS;
+ if (relZ >= SIZE || relZ < 0) //out of bounds
+ return;
+
+ pollution = pollution/225;
+ if (pollution > Short.MAX_VALUE) //Sanity
+ chunkMatrix[relX][relZ] = Short.MAX_VALUE; //Max pollution = 7,3mill
+ else if (pollution < 0)
+ chunkMatrix[relX][relZ] = 0;
+ else
+ chunkMatrix[relX][relZ] = (short) (pollution);
+ }
+
+ //xy interpolation, between 4 chunks as corners, unknown treated as 0.
+ public int getPollution(double fx, double fz) {
+ if (!initialized)
+ return 0;
+ int x = MathHelper.floor_double(fx);
+ int z = MathHelper.floor_double(fz);
+ int xDiff = ((x-8) >> 4) - x0;
+ int zDiff = ((z-8) >> 4) - z0;
+
+ if (xDiff < -RADIUS || zDiff < -RADIUS || xDiff >= RADIUS || zDiff >= RADIUS )
+ return 0;
+
+ //coordinates in shifted chunk.
+ x = (x-8) % 16;
+ z = (z-8) % 16;
+ if (x < 0)
+ x = 16+x;
+ if (z < 0)
+ z = 16+z;
+
+ int xi = 15 - x;
+ int zi = 15 - z;
+
+ //read pollution in 4 corner chunks
+ int offsetX = RADIUS+xDiff;
+ int offsetZ = RADIUS+zDiff;
+
+ int c00 = chunkMatrix[offsetX][offsetZ];
+ int c10 = chunkMatrix[offsetX+1][offsetZ];
+ int c01 = chunkMatrix[offsetX][offsetZ+1];
+ int c11 = chunkMatrix[offsetX+1][offsetZ+1];
+
+ //Is divided by 15*15 but is handled when storing chunk data.
+ return c00*xi*zi + c10*x*zi + c01*xi*z + c11*x*z;
+ }
+
+ //shift the matrix to fit new center
+ private void shiftCenter(int chunkX, int chunkZ) {
+ int xDiff = chunkX - x0;
+ int zDiff = chunkZ - z0;
+ boolean[] allEmpty = new boolean[SIZE]; //skip check z row if its empty.
+ if (xDiff > 0)
+ for (byte x = 0; x < SIZE; x++) {
+ int xOff = x + xDiff;
+ if (xOff < SIZE) {
+ chunkMatrix[x] = chunkMatrix[xOff].clone();
+ } else {
+ chunkMatrix[x] = new short[SIZE];
+ allEmpty[x] = true;
+ }
+ }
+ else if (xDiff < 0)
+ for (byte x = SIZE-1; x >= 0; x--) {
+ int xOff = x + xDiff;
+ if (xOff > 0) {
+ chunkMatrix[x] = chunkMatrix[xOff].clone();
+ } else {
+ chunkMatrix[x] = new short[SIZE];
+ allEmpty[x] = true;
+ }
+ }
+
+ if (zDiff > 0)
+ for (byte x = 0; x < SIZE; x++) {
+ if (allEmpty[x])
+ continue;
+ for (int z = 0; z < SIZE ; z++) {
+ int zOff = z + zDiff;
+ chunkMatrix[x][z] = (zOff < SIZE) ? chunkMatrix[x][zOff] : 0;
+ }
+ }
+ else if (zDiff < 0)
+ for (byte x = 0; x < SIZE; x++) {
+ if (allEmpty[x])
+ continue;
+ for (int z = SIZE-1; z >= 0 ; z--) {
+ int zOff = z+zDiff;
+ chunkMatrix[x][z] = (zOff > 0) ? chunkMatrix[x][zOff] : 0;
+ }
+ }
+
+ x0 = chunkX;
+ z0 = chunkZ;
+ }
+}
diff --git a/src/main/java/gregtech/common/misc/GT_Command.java b/src/main/java/gregtech/common/misc/GT_Command.java
index 66ae7058bc..f3272ee341 100644
--- a/src/main/java/gregtech/common/misc/GT_Command.java
+++ b/src/main/java/gregtech/common/misc/GT_Command.java
@@ -1,11 +1,15 @@
package gregtech.common.misc;
+import gregtech.GT_Mod;
import gregtech.api.enums.GT_Values;
import gregtech.api.objects.GT_ChunkManager;
+import gregtech.common.GT_Pollution;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.util.ChatComponentText;
+import net.minecraft.util.ChunkCoordinates;
+
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
@@ -21,10 +25,10 @@ public final class GT_Command extends CommandBase {
@Override
public String getCommandUsage(ICommandSender sender) {
- return "Usage: gt <subcommand>. Valid subcommands are: toggle, chunks.";
+ return "Usage: gt <subcommand>. Valid subcommands are: toggle, chunks, pollution.";
}
private void printHelp(ICommandSender sender) {
- sender.addChatMessage(new ChatComponentText("Usage: gt <toggle|chunks>"));
+ sender.addChatMessage(new ChatComponentText("Usage: gt <toggle|chunks|pollution>"));
sender.addChatMessage(new ChatComponentText("\"toggle D1\" - toggles general.Debug (D1)"));
sender.addChatMessage(new ChatComponentText("\"toggle D2\" - toggles general.Debug2 (D2)"));
sender.addChatMessage(new ChatComponentText("\"toggle debugCleanroom\" - toggles cleanroom debug log"));
@@ -38,6 +42,10 @@ public final class GT_Command extends CommandBase {
sender.addChatMessage(new ChatComponentText("\"toggle debugStones\" - toggles worldgen stones debug"));
sender.addChatMessage(new ChatComponentText("\"toggle debugChunkloaders\" - toggles chunkloaders debug"));
sender.addChatMessage(new ChatComponentText("\"chunks\" - print a list of the force loaded chunks"));
+ sender.addChatMessage(new ChatComponentText(
+ "\"pollution <amount>\" - adds the <amount> of the pollution to the current chunk, " +
+ "\n if <amount> isnt specified, will add" + GT_Mod.gregtechproxy.mPollutionSmogLimit + "gibbl."
+ ));
}
@Override
@@ -45,25 +53,24 @@ public final class GT_Command extends CommandBase {
List<String> l = new ArrayList<>();
Stream<String> keywords = Arrays.stream(new String[]{"toggle", "chunks"});
String test = ss.length == 0 ? "" : ss[0].trim();
- if (ss.length == 0 || ss.length == 1 && (test.isEmpty() || keywords.anyMatch(s -> s.startsWith(test)))) {
- keywords.forEach(s -> {
- if (test.isEmpty() || s.startsWith(test))
- l.add(s);
- });
+ if (ss.length == 0 || ss.length == 1 && (test.isEmpty() || Stream.of("toggle", "chunks", "pollution").anyMatch(s -> s.startsWith(test)))) {
+ Stream.of("toggle", "chunks", "pollution")
+ .filter(s -> test.isEmpty() || s.startsWith(test))
+ .forEach(l::add);
} else if (test.equals("toggle")) {
- String test1 = ss.length == 1 ? "" : ss[1].trim();
- Arrays.stream(new String[]{"D1", "D2", "debugCleanroom", "debugDriller", "debugBlockPump", "debugBlockMiner", "debugWorldGen", "debugEntityCramming",
- "debugOrevein", "debugSmallOres", "debugStones", "debugChunkloaders"}).forEach(s -> {
- if (test1.isEmpty() || s.startsWith(test1))
- l.add(s);
- });
+ String test1 = ss[1].trim();
+ Stream.of("D1", "D2", "debugCleanroom", "debugDriller", "debugBlockPump", "debugBlockMiner", "debugWorldGen", "debugEntityCramming",
+ "debugOrevein", "debugSmallOres", "debugStones", "debugChunkloaders")
+ .filter(s -> test1.isEmpty() || s.startsWith(test1))
+ .forEach(l::add);
+
}
return l;
}
@Override
public void processCommand(ICommandSender sender, String[] strings) {
- if (strings.length < 1 || (!strings[0].equals("toggle") && !strings[0].equals("chunks"))) {
+ if (strings.length < 1) {
printHelp(sender);
return;
}
@@ -90,6 +97,21 @@ public final class GT_Command extends CommandBase {
GT_ChunkManager.printTickets();
sender.addChatMessage(new ChatComponentText("Forced chunks logged to GregTech.log"));
break;
+ case "pollution": {
+ ChunkCoordinates coordinates = sender.getPlayerCoordinates();
+ int amount = (strings.length < 2) ? GT_Mod.gregtechproxy.mPollutionSmogLimit : Integer.parseInt(strings[1]);
+ GT_Pollution.addPollution(sender
+ .getEntityWorld()
+ .getChunkFromBlockCoords(
+ coordinates.posX,
+ coordinates.posZ
+ ),
+ amount
+ );
+ break;
+ }
+ default:
+ printHelp(sender);
}
}
}