add toArray, scanChunkInto usage coming soon

This commit is contained in:
Logan Darklock 2019-08-31 00:51:42 -07:00
parent 9f3eaac3df
commit ae6ee5688c
No known key found for this signature in database
GPG Key ID: B8C37CEDE1AC60EA
4 changed files with 63 additions and 7 deletions

View File

@ -17,14 +17,18 @@
package baritone.launch.mixins; package baritone.launch.mixins;
import baritone.utils.accessor.IBitArray;
import net.minecraft.util.BitArray; import net.minecraft.util.BitArray;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import java.util.Arrays;
@Mixin(BitArray.class) @Mixin(BitArray.class)
public abstract class MixinBitArray { public abstract class MixinBitArray implements IBitArray {
@Shadow @Shadow
@Final @Final
private long[] longArray; private long[] longArray;
@ -37,6 +41,10 @@ public abstract class MixinBitArray {
@Final @Final
private long maxEntryValue; private long maxEntryValue;
@Shadow
@Final
private int arraySize;
/** /**
* why did mojang divide by 64 instead of shifting right by 6 (2^6=64)? * why did mojang divide by 64 instead of shifting right by 6 (2^6=64)?
* why did mojang modulo by 64 instead of ANDing with 63? * why did mojang modulo by 64 instead of ANDing with 63?
@ -47,16 +55,48 @@ public abstract class MixinBitArray {
@Overwrite @Overwrite
public int getAt(int index) { public int getAt(int index) {
final int b = bitsPerEntry; final int b = bitsPerEntry;
final long mev = maxEntryValue;
final int i = index * b; final int i = index * b;
final int j = i >> 6; final int j = i >> 6;
final int l = i & 63; final int l = i & 63;
final int k = ((index + 1) * b - 1) >> 6; final int k = ((index + 1) * b - 1) >> 6;
if (j == k) { if (j == k) {
return (int) (this.longArray[j] >>> l & maxEntryValue); return (int) (this.longArray[j] >>> l & mev);
} else { } else {
int i1 = 64 - l; return (int) ((this.longArray[j] >>> l | longArray[k] << (64 - l)) & mev);
return (int) ((this.longArray[j] >>> l | longArray[k] << i1) & maxEntryValue);
} }
} }
@Unique
public int[] toArrayBad() {
int[] out = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
out[i] = getAt(i);
}
return out;
}
@Override
public int[] toArray() {
int[] out = new int[arraySize];
for (int idx = 0, kl = bitsPerEntry - 1; idx < arraySize; idx++, kl += bitsPerEntry) {
final int i = idx * bitsPerEntry;
final int j = i >> 6;
final int l = i & 63;
final int k = kl >> 6;
final long jl = longArray[j] >>> l;
if (j == k) {
out[idx] = (int) (jl & maxEntryValue);
} else {
out[idx] = (int) ((jl | longArray[k] << (64 - l)) & maxEntryValue);
}
}
return out;
}
} }

View File

@ -17,6 +17,7 @@
package baritone.launch.mixins; package baritone.launch.mixins;
import baritone.utils.accessor.IBitArray;
import baritone.utils.accessor.IBlockStateContainer; import baritone.utils.accessor.IBlockStateContainer;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.util.BitArray; import net.minecraft.util.BitArray;
@ -48,4 +49,14 @@ public abstract class MixinBlockStateContainer implements IBlockStateContainer {
public IBlockState getFast(int index) { public IBlockState getFast(int index) {
return palette.getBlockState(storage.getAt(index)); return palette.getBlockState(storage.getAt(index));
} }
@Override
public IBlockState getAtPalette(int index) {
return palette.getBlockState(index);
}
@Override
public int[] storageArray() {
return ((IBitArray) storage).toArray();
}
} }

View File

@ -0,0 +1,5 @@
package baritone.utils.accessor;
public interface IBitArray {
int[] toArray();
}

View File

@ -11,7 +11,7 @@ public interface IBlockStateContainer {
IBlockState getFast(int index); IBlockState getFast(int index);
default IBlockState getFast(int x, int y, int z) { IBlockState getAtPalette(int index);
return getFast(y << 8 | z << 4 | x);
}; int[] storageArray();
} }