Skip to content

Commit

Permalink
Report only remainder amounts for bulk compressed variants
Browse files Browse the repository at this point in the history
Closes #34
  • Loading branch information
62832 committed Jan 14, 2023
1 parent 023d215 commit 8549fb3
Showing 1 changed file with 37 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import static gripe._90.megacells.definition.MEGAItems.COMPRESSION_CARD;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;

import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
Expand Down Expand Up @@ -173,22 +174,26 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour

private BigInteger compressedTransferFactor(AEItemKey what) {
if (compressed.getInt(what) > 0) {
var variantKeys = new ArrayList<>(compressed.keySet());
var variantKeys = new LinkedList<>(compressed.keySet());
var toDecompress = new Object2IntLinkedOpenHashMap<>(compressed);
toDecompress.keySet().retainAll(variantKeys.subList(0, variantKeys.indexOf(what) + 1));

var factor = unitFactor;
for (var i : toDecompress.values()) {
factor *= i;
}

return BigInteger.valueOf(factor);
} else if (decompressed.getInt(what) > 0) {
var variantKeys = new ArrayList<>(decompressed.keySet());
var variantKeys = new LinkedList<>(decompressed.keySet());
var toCompress = new Object2IntLinkedOpenHashMap<>(decompressed);
toCompress.keySet().retainAll(variantKeys.subList(variantKeys.indexOf(what) + 1, variantKeys.size()));

var factor = 1L;
for (var i : toCompress.values()) {
factor *= i;
}

return BigInteger.valueOf(factor);
} else {
return BigInteger.valueOf(unitFactor);
Expand Down Expand Up @@ -227,37 +232,39 @@ public void persist() {

@Override
public void getAvailableStacks(KeyCounter out) {
var stackLimit = (long) Math.pow(2, 42);
if (storedItem != null) {
out.add(storedItem, clampedLong(unitCount.divide(BigInteger.valueOf(unitFactor)), stackLimit));
getVariantStacks(out);
}
}

public void getVariantStacks(KeyCounter out) {
var stackLimit = (long) Math.pow(2, 42);
if (compressionEnabled && storedItem.equals(filterItem)) {
var compressionFactor = unitFactor;
for (var variant : compressed.keySet()) {
compressionFactor *= compressed.getInt(variant);
var count = unitCount.divide(BigInteger.valueOf(compressionFactor));

if (count.signum() == 1) {
out.add(variant, clampedLong(count, stackLimit));
var stackLimit = (long) Math.pow(2, 42);

if (compressionEnabled && storedItem.equals(filterItem)) {
var allVariants = new Object2IntLinkedOpenHashMap<AEItemKey>();

if (!decompressed.isEmpty()) {
var decompressedKeys = new LinkedList<>(decompressed.keySet());
Collections.reverse(decompressedKeys);
decompressedKeys.forEach(k -> allVariants.put(k, decompressed.getInt(k)));

allVariants.put(storedItem, decompressed.getInt(decompressedKeys.getLast()));
allVariants.putAll(compressed);
} else if (!compressed.isEmpty()) {
allVariants.put(storedItem, compressed.values().intStream().findFirst().orElseThrow());
allVariants.putAll(compressed);
} else {
allVariants.put(storedItem, 1);
}
}

if (!decompressed.isEmpty()) {
var keys = new ArrayList<>(decompressed.keySet());
var baseUnit = keys.get(keys.size() - 1);
out.add(baseUnit, clampedLong(unitCount, stackLimit));
var count = unitCount;

var decompressionFactor = 1;
for (var variant : keys) {
decompressionFactor *= decompressed.getInt(variant);
var count = unitCount.divide(BigInteger.valueOf(decompressionFactor));
out.add(variant, clampedLong(count, stackLimit));
for (var variant : allVariants.keySet()) {
if (count.signum() != 1) {
break;
}

var compressionFactor = BigInteger.valueOf(allVariants.getInt(variant));
out.add(variant, clampedLong(count.remainder(compressionFactor), stackLimit));
count = count.divide(compressionFactor);
}
} else {
out.add(storedItem, clampedLong(unitCount.divide(BigInteger.valueOf(unitFactor)), stackLimit));
}
}
}
Expand All @@ -267,6 +274,7 @@ public boolean isPreferredStorageFor(AEKey what, IActionSource source) {
if (!(what instanceof AEItemKey item)) {
return false;
}

return partitionList.isListed(item) || compressed.containsKey(item) || decompressed.containsKey(item);
}

Expand Down

0 comments on commit 8549fb3

Please sign in to comment.